Skip to content

Thread native name setting #21678

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Jan 31, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions src/libstd/sys/common/thread_info.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
43 changes: 41 additions & 2 deletions src/libstd/sys/unix/thread.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::*;
Expand Down Expand Up @@ -206,6 +207,37 @@ 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) {
// pthread_setname_np() since glibc 2.12
// availability autodetected via weak linkage
let cname = CString::from_slice(name.as_bytes());
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"))]
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(any(target_os = "macos", target_os = "ios"))]
pub unsafe fn set_name(name: &str) {
// 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());
}

pub unsafe fn join(native: rust_thread) {
assert_eq!(pthread_join(native, ptr::null_mut()), 0);
}
Expand Down Expand Up @@ -246,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,
Expand All @@ -258,11 +290,18 @@ extern {
stacksize: *mut libc::size_t) -> libc::c_int;
}

#[cfg(target_os = "macos")]
#[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 libc::c_char);
}

#[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;
pub fn pthread_get_stacksize_np(thread: libc::pthread_t) -> libc::size_t;
fn pthread_setname_np(name: *const libc::c_char) -> libc::c_int;
}

extern {
Expand Down
7 changes: 7 additions & 0 deletions src/libstd/sys/windows/thread.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 MS C++ extensions.
// 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);
Expand Down