Skip to content

Commit ec4981e

Browse files
committed
Thread native name setting, fix #10302
1 parent a637365 commit ec4981e

File tree

3 files changed

+51
-0
lines changed

3 files changed

+51
-0
lines changed

src/libstd/sys/unix/thread.rs

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ use ptr;
1717
use libc::consts::os::posix01::{PTHREAD_CREATE_JOINABLE, PTHREAD_STACK_MIN};
1818
use libc;
1919
use thunk::Thunk;
20+
use ffi::CString;
2021

2122
use sys_common::stack::RED_ZONE;
2223
use sys_common::thread::*;
@@ -206,6 +207,29 @@ pub unsafe fn create(stack: uint, p: Thunk) -> rust_thread {
206207
native
207208
}
208209

210+
#[cfg(any(target_os = "linux", target_os = "android"))]
211+
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
215+
let cname = CString::from_slice(name.as_bytes());
216+
prctl(15i32 /* = PR_SET_NAME */, cname.as_ptr() as u64, 0u64, 0u64, 0u64);
217+
}
218+
219+
#[cfg(any(target_os = "freebsd", target_os = "dragonfly"))]
220+
pub unsafe fn set_name(name: &str) {
221+
// pthread_set_name_np() since almost forever on all BSDs
222+
let cname = CString::from_slice(name.as_bytes());
223+
pthread_set_name_np(pthread_self(), cname.as_ptr());
224+
}
225+
226+
#[cfg(target_os = "macos")]
227+
pub unsafe fn set_name(name: &str) {
228+
// pthread_setname_np() since OS X 10.6
229+
let cname = CString::from_slice(name.as_bytes());
230+
pthread_setname_np(cname.as_ptr());
231+
}
232+
209233
pub unsafe fn join(native: rust_thread) {
210234
assert_eq!(pthread_join(native, ptr::null_mut()), 0);
211235
}
@@ -246,6 +270,15 @@ fn min_stack_size(_: *const libc::pthread_attr_t) -> libc::size_t {
246270
PTHREAD_STACK_MIN
247271
}
248272

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+
249282
#[cfg(any(target_os = "linux"))]
250283
extern {
251284
pub fn pthread_self() -> libc::pthread_t;
@@ -258,11 +291,18 @@ extern {
258291
stacksize: *mut libc::size_t) -> libc::c_int;
259292
}
260293

294+
#[cfg(any(target_os = "freebsd", target_os = "dragonfly"))]
295+
extern {
296+
pub fn pthread_self() -> libc::pthread_t;
297+
fn pthread_set_name_np(tid: libc::pthread_t, name: *const c_char);
298+
}
299+
261300
#[cfg(target_os = "macos")]
262301
extern {
263302
pub fn pthread_self() -> libc::pthread_t;
264303
pub fn pthread_get_stackaddr_np(thread: libc::pthread_t) -> *mut libc::c_void;
265304
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;
266306
}
267307

268308
extern {

src/libstd/sys/windows/thread.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,13 @@ pub unsafe fn create(stack: uint, p: Thunk) -> rust_thread {
6767
return ret;
6868
}
6969

70+
pub unsafe fn set_name(name: &str) {
71+
// Windows threads are nameless
72+
// The names in MSVC debugger are obtained using a "magic" exception,
73+
// which requires a use of C++ macros.
74+
// See https://msdn.microsoft.com/en-us/library/xcb2z8hs.aspx
75+
}
76+
7077
pub unsafe fn join(native: rust_thread) {
7178
use libc::consts::os::extra::INFINITE;
7279
WaitForSingleObject(native, INFINITE);

src/libstd/thread.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -275,6 +275,10 @@ 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+
}
278282
thread_info::set(
279283
(my_stack_bottom, my_stack_top),
280284
unsafe { imp::guard::current() },

0 commit comments

Comments
 (0)