From ec4981ece86769c768d61a914093a7a39192522c Mon Sep 17 00:00:00 2001 From: Vojtech Kral Date: Tue, 27 Jan 2015 03:38:07 +0100 Subject: Thread native name setting, fix #10302 --- src/libstd/sys/unix/thread.rs | 40 ++++++++++++++++++++++++++++++++++++++++ src/libstd/sys/windows/thread.rs | 7 +++++++ 2 files changed, 47 insertions(+) (limited to 'src/libstd/sys') diff --git a/src/libstd/sys/unix/thread.rs b/src/libstd/sys/unix/thread.rs index ac51b68795f..3c34e1f9dd4 100644 --- a/src/libstd/sys/unix/thread.rs +++ b/src/libstd/sys/unix/thread.rs @@ -17,6 +17,7 @@ use ptr; use libc::consts::os::posix01::{PTHREAD_CREATE_JOINABLE, PTHREAD_STACK_MIN}; use libc; use thunk::Thunk; +use ffi::CString; use sys_common::stack::RED_ZONE; use sys_common::thread::*; @@ -206,6 +207,29 @@ pub unsafe fn create(stack: uint, p: Thunk) -> rust_thread { native } +#[cfg(any(target_os = "linux", target_os = "android"))] +pub unsafe fn set_name(name: &str) { + // Using prctl() rather than pthread_setname_np(), + // because pthread_setname_np() wasn't added until glibc 2.12 + // PR_SET_NAME since Linux 2.6.9 + let cname = CString::from_slice(name.as_bytes()); + prctl(15i32 /* = PR_SET_NAME */, cname.as_ptr() as u64, 0u64, 0u64, 0u64); +} + +#[cfg(any(target_os = "freebsd", target_os = "dragonfly"))] +pub unsafe fn set_name(name: &str) { + // pthread_set_name_np() since almost forever on all BSDs + let cname = CString::from_slice(name.as_bytes()); + pthread_set_name_np(pthread_self(), cname.as_ptr()); +} + +#[cfg(target_os = "macos")] +pub unsafe fn set_name(name: &str) { + // pthread_setname_np() since OS X 10.6 + let cname = CString::from_slice(name.as_bytes()); + pthread_setname_np(cname.as_ptr()); +} + pub unsafe fn join(native: rust_thread) { assert_eq!(pthread_join(native, ptr::null_mut()), 0); } @@ -246,6 +270,15 @@ fn min_stack_size(_: *const libc::pthread_attr_t) -> libc::size_t { PTHREAD_STACK_MIN } +#[cfg(any(target_os = "linux", target_os = "android"))] +extern { + fn prctl(option: libc::c_int, + arg2: libc::c_ulong, + arg3: libc::c_ulong, + arg4: libc::c_ulong, + arg5: libc::c_ulong) -> libc::c_int; +} + #[cfg(any(target_os = "linux"))] extern { pub fn pthread_self() -> libc::pthread_t; @@ -258,11 +291,18 @@ extern { stacksize: *mut libc::size_t) -> libc::c_int; } +#[cfg(any(target_os = "freebsd", target_os = "dragonfly"))] +extern { + pub fn pthread_self() -> libc::pthread_t; + fn pthread_set_name_np(tid: libc::pthread_t, name: *const c_char); +} + #[cfg(target_os = "macos")] extern { pub fn pthread_self() -> libc::pthread_t; pub fn pthread_get_stackaddr_np(thread: libc::pthread_t) -> *mut libc::c_void; pub fn pthread_get_stacksize_np(thread: libc::pthread_t) -> libc::size_t; + fn pthread_setname_np(name: *const c_char) -> libc::c_int; } extern { diff --git a/src/libstd/sys/windows/thread.rs b/src/libstd/sys/windows/thread.rs index 30707488b30..b6027ea2e7e 100644 --- a/src/libstd/sys/windows/thread.rs +++ b/src/libstd/sys/windows/thread.rs @@ -67,6 +67,13 @@ pub unsafe fn create(stack: uint, p: Thunk) -> rust_thread { return ret; } +pub unsafe fn set_name(name: &str) { + // Windows threads are nameless + // The names in MSVC debugger are obtained using a "magic" exception, + // which requires a use of C++ macros. + // See https://msdn.microsoft.com/en-us/library/xcb2z8hs.aspx +} + pub unsafe fn join(native: rust_thread) { use libc::consts::os::extra::INFINITE; WaitForSingleObject(native, INFINITE); -- cgit 1.4.1-3-g733a5 From c155208de42de5761231726e35614b4499b5a137 Mon Sep 17 00:00:00 2001 From: Vojtech Kral Date: Tue, 27 Jan 2015 16:00:26 +0100 Subject: Thread native name setting, fix #10302 --- src/libstd/sys/unix/thread.rs | 4 ++-- src/libstd/sys/windows/thread.rs | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) (limited to 'src/libstd/sys') diff --git a/src/libstd/sys/unix/thread.rs b/src/libstd/sys/unix/thread.rs index 3c34e1f9dd4..1fba6bedc7c 100644 --- a/src/libstd/sys/unix/thread.rs +++ b/src/libstd/sys/unix/thread.rs @@ -223,9 +223,9 @@ pub unsafe fn set_name(name: &str) { pthread_set_name_np(pthread_self(), cname.as_ptr()); } -#[cfg(target_os = "macos")] +#[cfg(any(target_os = "macos", target_os = "ios"))] pub unsafe fn set_name(name: &str) { - // pthread_setname_np() since OS X 10.6 + // pthread_setname_np() since OS X 10.6 and iOS 3.2 let cname = CString::from_slice(name.as_bytes()); pthread_setname_np(cname.as_ptr()); } diff --git a/src/libstd/sys/windows/thread.rs b/src/libstd/sys/windows/thread.rs index b6027ea2e7e..a94adcb3bc7 100644 --- a/src/libstd/sys/windows/thread.rs +++ b/src/libstd/sys/windows/thread.rs @@ -67,10 +67,10 @@ pub unsafe fn create(stack: uint, p: Thunk) -> rust_thread { return ret; } -pub unsafe fn set_name(name: &str) { +pub unsafe fn set_name(_name: &str) { // Windows threads are nameless // The names in MSVC debugger are obtained using a "magic" exception, - // which requires a use of C++ macros. + // which requires a use of MS C++ extensions. // See https://msdn.microsoft.com/en-us/library/xcb2z8hs.aspx } -- cgit 1.4.1-3-g733a5 From 33a3d6d88f76bfae770983ee50e36e23cc4c7655 Mon Sep 17 00:00:00 2001 From: Vojtech Kral Date: Wed, 28 Jan 2015 13:48:27 +0100 Subject: Thread native name setting, fix #10302 --- src/libstd/sys/common/thread_info.rs | 4 ++++ src/libstd/sys/unix/thread.rs | 29 ++++++++++++++--------------- src/libstd/thread.rs | 4 ---- 3 files changed, 18 insertions(+), 19 deletions(-) (limited to 'src/libstd/sys') diff --git a/src/libstd/sys/common/thread_info.rs b/src/libstd/sys/common/thread_info.rs index 92b936e74f6..7c9758ca924 100644 --- a/src/libstd/sys/common/thread_info.rs +++ b/src/libstd/sys/common/thread_info.rs @@ -56,6 +56,10 @@ pub fn stack_guard() -> uint { pub fn set(stack_bounds: (uint, uint), stack_guard: uint, thread: Thread) { THREAD_INFO.with(|c| assert!(c.borrow().is_none())); + match thread.name() { + Some(name) => unsafe { ::sys::thread::set_name(name.as_slice()); }, + None => {} + } THREAD_INFO.with(move |c| *c.borrow_mut() = Some(ThreadInfo{ stack_bounds: stack_bounds, stack_guard: stack_guard, diff --git a/src/libstd/sys/unix/thread.rs b/src/libstd/sys/unix/thread.rs index 1fba6bedc7c..59c2badaf62 100644 --- a/src/libstd/sys/unix/thread.rs +++ b/src/libstd/sys/unix/thread.rs @@ -209,11 +209,19 @@ pub unsafe fn create(stack: uint, p: Thunk) -> rust_thread { #[cfg(any(target_os = "linux", target_os = "android"))] pub unsafe fn set_name(name: &str) { - // Using prctl() rather than pthread_setname_np(), - // because pthread_setname_np() wasn't added until glibc 2.12 - // PR_SET_NAME since Linux 2.6.9 + // pthread_setname_np() since glibc 2.12 + // availability autodetected via weak linkage let cname = CString::from_slice(name.as_bytes()); - prctl(15i32 /* = PR_SET_NAME */, cname.as_ptr() as u64, 0u64, 0u64, 0u64); + type F = unsafe extern "C" fn(libc::pthread_t, *const libc::c_char) -> libc::c_int; + extern { + #[linkage = "extern_weak"] + static pthread_setname_np: *const (); + } + if !pthread_setname_np.is_null() { + unsafe { + mem::transmute::<*const (), F>(pthread_setname_np)(pthread_self(), cname.as_ptr()); + } + } } #[cfg(any(target_os = "freebsd", target_os = "dragonfly"))] @@ -270,15 +278,6 @@ fn min_stack_size(_: *const libc::pthread_attr_t) -> libc::size_t { PTHREAD_STACK_MIN } -#[cfg(any(target_os = "linux", target_os = "android"))] -extern { - fn prctl(option: libc::c_int, - arg2: libc::c_ulong, - arg3: libc::c_ulong, - arg4: libc::c_ulong, - arg5: libc::c_ulong) -> libc::c_int; -} - #[cfg(any(target_os = "linux"))] extern { pub fn pthread_self() -> libc::pthread_t; @@ -294,7 +293,7 @@ extern { #[cfg(any(target_os = "freebsd", target_os = "dragonfly"))] extern { pub fn pthread_self() -> libc::pthread_t; - fn pthread_set_name_np(tid: libc::pthread_t, name: *const c_char); + fn pthread_set_name_np(tid: libc::pthread_t, name: *const libc::c_char); } #[cfg(target_os = "macos")] @@ -302,7 +301,7 @@ extern { pub fn pthread_self() -> libc::pthread_t; pub fn pthread_get_stackaddr_np(thread: libc::pthread_t) -> *mut libc::c_void; pub fn pthread_get_stacksize_np(thread: libc::pthread_t) -> libc::size_t; - fn pthread_setname_np(name: *const c_char) -> libc::c_int; + fn pthread_setname_np(name: *const libc::c_char) -> libc::c_int; } extern { diff --git a/src/libstd/thread.rs b/src/libstd/thread.rs index 0cab7e69928..a86b82b8c62 100644 --- a/src/libstd/thread.rs +++ b/src/libstd/thread.rs @@ -275,10 +275,6 @@ impl Builder { unsafe { stack::record_os_managed_stack_bounds(my_stack_bottom, my_stack_top); } - match their_thread.name() { - Some(thename) => unsafe { imp::set_name(thename.as_slice()); }, - None => {} - } thread_info::set( (my_stack_bottom, my_stack_top), unsafe { imp::guard::current() }, -- cgit 1.4.1-3-g733a5 From 7e67eba180eb2efb09e1487020a9a160335e7926 Mon Sep 17 00:00:00 2001 From: Vojtech Kral Date: Wed, 28 Jan 2015 14:01:14 +0100 Subject: Thread native name setting, fix #10302 --- src/libstd/sys/unix/thread.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'src/libstd/sys') diff --git a/src/libstd/sys/unix/thread.rs b/src/libstd/sys/unix/thread.rs index 59c2badaf62..d8e281bf5ab 100644 --- a/src/libstd/sys/unix/thread.rs +++ b/src/libstd/sys/unix/thread.rs @@ -296,7 +296,7 @@ extern { fn pthread_set_name_np(tid: libc::pthread_t, name: *const libc::c_char); } -#[cfg(target_os = "macos")] +#[cfg(any(target_os = "macos", target_os = "ios"))] extern { pub fn pthread_self() -> libc::pthread_t; pub fn pthread_get_stackaddr_np(thread: libc::pthread_t) -> *mut libc::c_void; -- cgit 1.4.1-3-g733a5 From 9ee972ca32b1f91eb6880e1bc9c8bb5a4faf1f29 Mon Sep 17 00:00:00 2001 From: Vojtech Kral Date: Wed, 28 Jan 2015 16:52:53 +0100 Subject: Thread native name setting, fix #10302 --- src/libstd/sys/unix/thread.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'src/libstd/sys') diff --git a/src/libstd/sys/unix/thread.rs b/src/libstd/sys/unix/thread.rs index d8e281bf5ab..26a450b8599 100644 --- a/src/libstd/sys/unix/thread.rs +++ b/src/libstd/sys/unix/thread.rs @@ -278,7 +278,7 @@ fn min_stack_size(_: *const libc::pthread_attr_t) -> libc::size_t { PTHREAD_STACK_MIN } -#[cfg(any(target_os = "linux"))] +#[cfg(any(target_os = "linux", target_os = "android"))] extern { pub fn pthread_self() -> libc::pthread_t; pub fn pthread_getattr_np(native: libc::pthread_t, -- cgit 1.4.1-3-g733a5