-
Notifications
You must be signed in to change notification settings - Fork 13.3k
Add a numeric ID to threads #29448
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
Add a numeric ID to threads #29448
Changes from all commits
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 |
---|---|---|
|
@@ -35,7 +35,7 @@ unsafe impl Send for Thread {} | |
unsafe impl Sync for Thread {} | ||
|
||
impl Thread { | ||
pub unsafe fn new<'a>(stack: usize, p: Box<FnBox() + 'a>) | ||
pub unsafe fn new<'a>(stack: usize, p: Box<FnBox(u32) + 'a>) | ||
-> io::Result<Thread> { | ||
let p = box p; | ||
let mut native: libc::pthread_t = mem::zeroed(); | ||
|
@@ -71,7 +71,7 @@ impl Thread { | |
}; | ||
|
||
extern fn thread_start(main: *mut libc::c_void) -> *mut libc::c_void { | ||
unsafe { start_thread(main); } | ||
unsafe { start_thread(main, pthread_self() as u32); } | ||
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. This is wrong.
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. So should we use a typedef around what a thread ID is? 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. On Linux pthread_t is I'm starting to think that using the address of the Inner struct is way simpler and would totally work (it's what I used for #29447) if we want "some kind of ID" and don't care for the OS-provided ID. 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. On Windows what I want is some sort of OS provided ID. Either the thread 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. Nevermind, I'm fine with this just providing some sort of unique numerical ID that has no meaning other than to distinguish threads. I'll work on a PR to add OS specific APIs for getting the |
||
ptr::null_mut() | ||
} | ||
} | ||
|
@@ -162,6 +162,10 @@ impl Thread { | |
debug_assert_eq!(ret, 0); | ||
} | ||
} | ||
|
||
pub fn id(&self) -> u32 { | ||
self.id as u32 | ||
} | ||
} | ||
|
||
impl Drop for Thread { | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -49,7 +49,7 @@ impl Thread { | |
}; | ||
|
||
extern "system" fn thread_start(main: *mut libc::c_void) -> DWORD { | ||
unsafe { start_thread(main); } | ||
unsafe { start_thread(main, 42); } | ||
0 | ||
} | ||
} | ||
|
@@ -78,6 +78,10 @@ impl Thread { | |
c::Sleep(super::dur2timeout(dur)) | ||
} | ||
} | ||
|
||
pub fn id(&self) -> u32 { | ||
42 // TODO | ||
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. On Windows this is done either via |
||
} | ||
} | ||
|
||
pub mod guard { | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -252,17 +252,18 @@ impl Builder { | |
|
||
let stack_size = stack_size.unwrap_or(util::min_stack()); | ||
|
||
let my_thread = Thread::new(name); | ||
let their_thread = my_thread.clone(); | ||
let mut my_thread = Thread::new(name); | ||
let mut their_thread = my_thread.clone(); | ||
|
||
let my_packet : Arc<UnsafeCell<Option<Result<T>>>> | ||
= Arc::new(UnsafeCell::new(None)); | ||
let their_packet = my_packet.clone(); | ||
|
||
let main = move || { | ||
let main = move |id: u32| { | ||
if let Some(name) = their_thread.name() { | ||
imp::Thread::set_name(name); | ||
} | ||
their_thread.id = id; | ||
unsafe { | ||
thread_info::set(imp::guard::current(), their_thread); | ||
let mut output = None; | ||
|
@@ -276,10 +277,12 @@ impl Builder { | |
} | ||
}; | ||
|
||
let native = unsafe { | ||
try!(imp::Thread::new(stack_size, Box::new(main))) | ||
}; | ||
my_thread.id = native.id(); | ||
Ok(JoinHandle(JoinInner { | ||
native: unsafe { | ||
Some(try!(imp::Thread::new(stack_size, Box::new(main)))) | ||
}, | ||
native: Some(native), | ||
thread: my_thread, | ||
packet: Packet(my_packet), | ||
})) | ||
|
@@ -502,6 +505,7 @@ struct Inner { | |
/// A handle to a thread. | ||
pub struct Thread { | ||
inner: Arc<Inner>, | ||
id: u32, | ||
} | ||
|
||
impl Thread { | ||
|
@@ -512,7 +516,8 @@ impl Thread { | |
name: name, | ||
lock: Mutex::new(false), | ||
cvar: Condvar::new(), | ||
}) | ||
}), | ||
id: 0, | ||
} | ||
} | ||
|
||
|
@@ -533,12 +538,20 @@ impl Thread { | |
pub fn name(&self) -> Option<&str> { | ||
self.inner.name.as_ref().map(|s| &**s) | ||
} | ||
|
||
/// Gets the thread's unique ID. | ||
/// | ||
/// This ID is unique across running threads, and a thread that has stopped | ||
/// might see its ID reused. | ||
pub fn id(&self) -> u32 { | ||
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. If this PR is accepted, this will need to be marked |
||
self.id | ||
} | ||
} | ||
|
||
#[stable(feature = "rust1", since = "1.0.0")] | ||
impl fmt::Debug for Thread { | ||
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { | ||
fmt::Debug::fmt(&self.name(), f) | ||
fmt::Debug::fmt(&self.name(), f) // print ID? | ||
} | ||
} | ||
|
||
|
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.
I do not understand why’d you want to pass the thread id as an argument into the thread instead of e.g. providing a static method on
Thread
which callspthread_self
.