Skip to content

Commit 90ed244

Browse files
committed
Auto merge of #21678 - vojtechkral:threads-native-names, r=
Fixes #10302 I really am not sure I'm doing this right, so here goes nothing... Also testing this isn't easy. I don't have any other *nix boxes besides a Linux one. Test code: ```rust use std::thread; use std::io::timer::sleep; use std::time::duration::Duration; fn make_thread<'a>(i: i64) -> thread::JoinGuard<'a, ()> { thread::Builder::new().name(format!("MyThread{}", i).to_string()).scoped(move || { println!("Start: {}", i); sleep(Duration::seconds(i)); println!("End: {}", i); }) } fn main() { let mut guards = vec![make_thread(3)]; for i in 4i64..16 { guards.push(make_thread(i)); } } ``` GDB output on my machine: ``` (gdb) info threads Id Target Id Frame 15 Thread 0x7fdfbb35f700 (LWP 23575) "MyThread3" 0x00007fdfbbe35a8d in nanosleep () from /usr/lib/libpthread.so.0 14 Thread 0x7fdfba7ff700 (LWP 23576) "MyThread4" 0x00007fdfbbe35a8d in nanosleep () from /usr/lib/libpthread.so.0 13 Thread 0x7fdfba5fe700 (LWP 23577) "MyThread5" 0x00007fdfbbe35a8d in nanosleep () from /usr/lib/libpthread.so.0 12 Thread 0x7fdfba3fd700 (LWP 23578) "MyThread6" 0x00007fdfbbe35a8d in nanosleep () from /usr/lib/libpthread.so.0 11 Thread 0x7fdfb8dfe700 (LWP 23580) "MyThread4" 0x00007fdfbb746193 in select () from /usr/lib/libc.so.6 10 Thread 0x7fdfb8fff700 (LWP 23579) "MyThread7" 0x00007fdfbbe35a8d in nanosleep () from /usr/lib/libpthread.so.0 9 Thread 0x7fdfb8bfd700 (LWP 23581) "MyThread8" 0x00007fdfbbe35a8d in nanosleep () from /usr/lib/libpthread.so.0 8 Thread 0x7fdfb3fff700 (LWP 23582) "MyThread9" 0x00007fdfbbe35a8d in nanosleep () from /usr/lib/libpthread.so.0 7 Thread 0x7fdfb3dfe700 (LWP 23583) "MyThread10" 0x00007fdfbbe35a8d in nanosleep () from /usr/lib/libpthread.so.0 6 Thread 0x7fdfb3bfd700 (LWP 23584) "MyThread11" 0x00007fdfbbe35a8d in nanosleep () from /usr/lib/libpthread.so.0 5 Thread 0x7fdfb2bff700 (LWP 23585) "MyThread12" 0x00007fdfbbe35a8d in nanosleep () from /usr/lib/libpthread.so.0 4 Thread 0x7fdfb29fe700 (LWP 23586) "MyThread13" 0x00007fdfbbe35a8d in nanosleep () from /usr/lib/libpthread.so.0 3 Thread 0x7fdfb27fd700 (LWP 23587) "MyThread14" 0x00007fdfbbe35a8d in nanosleep () from /usr/lib/libpthread.so.0 2 Thread 0x7fdfb1bff700 (LWP 23588) "MyThread15" 0x00007fdfbbe35a8d in nanosleep () from /usr/lib/libpthread.so.0 * 1 Thread 0x7fdfbc411800 (LWP 23574) "threads" 0x00007fdfbbe2e505 in pthread_join () from /usr/lib/libpthread.so.0 ``` (I'm not sure why one of the threads is duplicated, but it does that without my patch too...)
2 parents a45e117 + 7e67eba commit 90ed244

File tree

3 files changed

+51
-1
lines changed

3 files changed

+51
-1
lines changed

src/libstd/sys/common/thread_info.rs

Lines changed: 4 additions & 0 deletions
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

Lines changed: 40 additions & 1 deletion
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,37 @@ 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+
// pthread_setname_np() since glibc 2.12
213+
// availability autodetected via weak linkage
214+
let cname = CString::from_slice(name.as_bytes());
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+
}
225+
}
226+
227+
#[cfg(any(target_os = "freebsd", target_os = "dragonfly"))]
228+
pub unsafe fn set_name(name: &str) {
229+
// pthread_set_name_np() since almost forever on all BSDs
230+
let cname = CString::from_slice(name.as_bytes());
231+
pthread_set_name_np(pthread_self(), cname.as_ptr());
232+
}
233+
234+
#[cfg(any(target_os = "macos", target_os = "ios"))]
235+
pub unsafe fn set_name(name: &str) {
236+
// pthread_setname_np() since OS X 10.6 and iOS 3.2
237+
let cname = CString::from_slice(name.as_bytes());
238+
pthread_setname_np(cname.as_ptr());
239+
}
240+
209241
pub unsafe fn join(native: rust_thread) {
210242
assert_eq!(pthread_join(native, ptr::null_mut()), 0);
211243
}
@@ -258,11 +290,18 @@ extern {
258290
stacksize: *mut libc::size_t) -> libc::c_int;
259291
}
260292

261-
#[cfg(target_os = "macos")]
293+
#[cfg(any(target_os = "freebsd", target_os = "dragonfly"))]
294+
extern {
295+
pub fn pthread_self() -> libc::pthread_t;
296+
fn pthread_set_name_np(tid: libc::pthread_t, name: *const libc::c_char);
297+
}
298+
299+
#[cfg(any(target_os = "macos", target_os = "ios"))]
262300
extern {
263301
pub fn pthread_self() -> libc::pthread_t;
264302
pub fn pthread_get_stackaddr_np(thread: libc::pthread_t) -> *mut libc::c_void;
265303
pub fn pthread_get_stacksize_np(thread: libc::pthread_t) -> libc::size_t;
304+
fn pthread_setname_np(name: *const libc::c_char) -> libc::c_int;
266305
}
267306

268307
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 MS C++ extensions.
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);

0 commit comments

Comments
 (0)