Skip to content

Commit 33a3d6d

Browse files
committed
Thread native name setting, fix #10302
1 parent c155208 commit 33a3d6d

File tree

3 files changed

+18
-19
lines changed

3 files changed

+18
-19
lines changed

src/libstd/sys/common/thread_info.rs

+4
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,10 @@ pub fn stack_guard() -> uint {
5656

5757
pub fn set(stack_bounds: (uint, uint), stack_guard: uint, thread: Thread) {
5858
THREAD_INFO.with(|c| assert!(c.borrow().is_none()));
59+
match thread.name() {
60+
Some(name) => unsafe { ::sys::thread::set_name(name.as_slice()); },
61+
None => {}
62+
}
5963
THREAD_INFO.with(move |c| *c.borrow_mut() = Some(ThreadInfo{
6064
stack_bounds: stack_bounds,
6165
stack_guard: stack_guard,

src/libstd/sys/unix/thread.rs

+14-15
Original file line numberDiff line numberDiff line change
@@ -209,11 +209,19 @@ pub unsafe fn create(stack: uint, p: Thunk) -> rust_thread {
209209

210210
#[cfg(any(target_os = "linux", target_os = "android"))]
211211
pub unsafe fn set_name(name: &str) {
212-
// Using prctl() rather than pthread_setname_np(),
213-
// because pthread_setname_np() wasn't added until glibc 2.12
214-
// PR_SET_NAME since Linux 2.6.9
212+
// pthread_setname_np() since glibc 2.12
213+
// availability autodetected via weak linkage
215214
let cname = CString::from_slice(name.as_bytes());
216-
prctl(15i32 /* = PR_SET_NAME */, cname.as_ptr() as u64, 0u64, 0u64, 0u64);
215+
type F = unsafe extern "C" fn(libc::pthread_t, *const libc::c_char) -> libc::c_int;
216+
extern {
217+
#[linkage = "extern_weak"]
218+
static pthread_setname_np: *const ();
219+
}
220+
if !pthread_setname_np.is_null() {
221+
unsafe {
222+
mem::transmute::<*const (), F>(pthread_setname_np)(pthread_self(), cname.as_ptr());
223+
}
224+
}
217225
}
218226

219227
#[cfg(any(target_os = "freebsd", target_os = "dragonfly"))]
@@ -270,15 +278,6 @@ fn min_stack_size(_: *const libc::pthread_attr_t) -> libc::size_t {
270278
PTHREAD_STACK_MIN
271279
}
272280

273-
#[cfg(any(target_os = "linux", target_os = "android"))]
274-
extern {
275-
fn prctl(option: libc::c_int,
276-
arg2: libc::c_ulong,
277-
arg3: libc::c_ulong,
278-
arg4: libc::c_ulong,
279-
arg5: libc::c_ulong) -> libc::c_int;
280-
}
281-
282281
#[cfg(any(target_os = "linux"))]
283282
extern {
284283
pub fn pthread_self() -> libc::pthread_t;
@@ -294,15 +293,15 @@ extern {
294293
#[cfg(any(target_os = "freebsd", target_os = "dragonfly"))]
295294
extern {
296295
pub fn pthread_self() -> libc::pthread_t;
297-
fn pthread_set_name_np(tid: libc::pthread_t, name: *const c_char);
296+
fn pthread_set_name_np(tid: libc::pthread_t, name: *const libc::c_char);
298297
}
299298

300299
#[cfg(target_os = "macos")]
301300
extern {
302301
pub fn pthread_self() -> libc::pthread_t;
303302
pub fn pthread_get_stackaddr_np(thread: libc::pthread_t) -> *mut libc::c_void;
304303
pub fn pthread_get_stacksize_np(thread: libc::pthread_t) -> libc::size_t;
305-
fn pthread_setname_np(name: *const c_char) -> libc::c_int;
304+
fn pthread_setname_np(name: *const libc::c_char) -> libc::c_int;
306305
}
307306

308307
extern {

src/libstd/thread.rs

-4
Original file line numberDiff line numberDiff line change
@@ -275,10 +275,6 @@ impl Builder {
275275
unsafe {
276276
stack::record_os_managed_stack_bounds(my_stack_bottom, my_stack_top);
277277
}
278-
match their_thread.name() {
279-
Some(thename) => unsafe { imp::set_name(thename.as_slice()); },
280-
None => {}
281-
}
282278
thread_info::set(
283279
(my_stack_bottom, my_stack_top),
284280
unsafe { imp::guard::current() },

0 commit comments

Comments
 (0)