Skip to content

Commit d865b43

Browse files
authored
Rollup merge of #158399 - joboet:netbsd_threadname_truncate, r=jhpratt
std: truncate thread names on NetBSD NetBSD [enforces limits on the length of thread names](https://man.netbsd.org/pthread_setname_np.3#DESCRIPTION). On all similar platforms (e.g. Linux, macOS) we truncate the Rust name so that at least some of the name is visible in the OS. CC @semarie
2 parents db75b9a + ba2c9ba commit d865b43

1 file changed

Lines changed: 6 additions & 0 deletions

File tree

library/std/src/sys/thread/unix.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -392,6 +392,7 @@ pub fn current_os_id() -> Option<u64> {
392392
target_os = "vxworks",
393393
target_os = "cygwin",
394394
target_vendor = "apple",
395+
target_os = "netbsd",
395396
))]
396397
fn truncate_cstr<const MAX_WITH_NUL: usize>(cstr: &CStr) -> [libc::c_char; MAX_WITH_NUL] {
397398
let mut result = [0; MAX_WITH_NUL];
@@ -463,7 +464,12 @@ pub fn set_name(name: &CStr) {
463464

464465
#[cfg(target_os = "netbsd")]
465466
pub fn set_name(name: &CStr) {
467+
// See https://github.com/NetBSD/src/blob/8d40872b4c550a802379f3b9c22a40212d5e149d/lib/libpthread/pthread.h#L281
468+
// FIXME: move to libc.
469+
const PTHREAD_MAX_NAMELEN_NP: usize = 32;
470+
466471
unsafe {
472+
let name = truncate_cstr::<{ PTHREAD_MAX_NAMELEN_NP }>(name);
467473
let res = libc::pthread_setname_np(
468474
libc::pthread_self(),
469475
c"%s".as_ptr(),

0 commit comments

Comments
 (0)