Skip to content

Commit 8a15868

Browse files
committed
rollup merge of #23640: nagisa/thread-less-weak
This is more portable as far as linux is concerned.
2 parents 5e06ebb + d29d554 commit 8a15868

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
@@ -229,19 +229,16 @@ pub unsafe fn create(stack: usize, p: Thunk) -> io::Result<rust_thread> {
229229

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

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

0 commit comments

Comments
 (0)