-
Notifications
You must be signed in to change notification settings - Fork 13.4k
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
Thread native name setting #21678
Changes from 1 commit
ec4981e
c155208
33a3d6d
7e67eba
9ee972c
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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")] | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think iOS is (somewhat) supported too; does it use There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @huonw It would make sense since the two OSes share the same posix base, but I can't find a direct confirmation... There is a There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Found it: over here it says There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @vojtechkral @huonw yep, iOS is similar to OS X here, please change it to |
||
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 { | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -275,6 +275,10 @@ 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 => {} | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Could this be folded into |
||
thread_info::set( | ||
(my_stack_bottom, my_stack_top), | ||
unsafe { imp::guard::current() }, | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can this use the
linkage = "extern_weak"
trick instead?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@alexcrichton Sure, which function though?
prctl()
orpthread_setname_np()
?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
For
pthread_setname_np
, but largely only because it looks like it's only defined on newer glibc implementations. This is a bit of a nicety, however, so we probably won't lose too much if it's not available.