Skip to content

Commit d29d554

Browse files
committed
prctl instead of pthread on linux for name setup
This is more portable as far as linux is concerned.
1 parent b0aad7d commit d29d554

File tree

1 file changed

+8
-11
lines changed

1 file changed

+8
-11
lines changed

src/libstd/sys/unix/thread.rs

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -227,19 +227,16 @@ pub unsafe fn create(stack: usize, p: Thunk) -> io::Result<rust_thread> {
227227

228228
#[cfg(any(target_os = "linux", target_os = "android"))]
229229
pub unsafe fn set_name(name: &str) {
230-
// pthread_setname_np() since glibc 2.12
231-
// availability autodetected via weak linkage
232-
type F = unsafe extern fn(libc::pthread_t, *const libc::c_char)
233-
-> libc::c_int;
230+
// pthread wrapper only appeared in glibc 2.12, so we use syscall directly.
234231
extern {
235-
#[linkage = "extern_weak"]
236-
static pthread_setname_np: *const ();
237-
}
238-
if !pthread_setname_np.is_null() {
239-
let cname = CString::new(name).unwrap();
240-
mem::transmute::<*const (), F>(pthread_setname_np)(pthread_self(),
241-
cname.as_ptr());
232+
fn prctl(option: libc::c_int, arg2: libc::c_ulong, arg3: libc::c_ulong,
233+
arg4: libc::c_ulong, arg5: libc::c_ulong) -> libc::c_int;
242234
}
235+
const PR_SET_NAME: libc::c_int = 15;
236+
let cname = CString::new(name).unwrap_or_else(|_| {
237+
panic!("thread name may not contain interior null bytes")
238+
});
239+
prctl(PR_SET_NAME, cname.as_ptr() as libc::c_ulong, 0, 0, 0);
243240
}
244241

245242
#[cfg(any(target_os = "freebsd",

0 commit comments

Comments
 (0)