Join GitHub today
GitHub is home to over 31 million developers working together to host and review code, manage projects, and build software together.
Sign upAdd a numeric ID to threads #29448
Conversation
remram44
added some commits
Oct 28, 2015
rust-highfive
assigned
brson
Oct 29, 2015
This comment has been minimized.
This comment has been minimized.
|
r? @brson (rust_highfive has picked a reviewer for you, use r? to override) |
remram44
added some commits
Oct 28, 2015
huonw
added
I-nominated
T-libs
labels
Oct 29, 2015
huonw
reviewed
Oct 29, 2015
| /// | ||
| /// This ID is unique across running threads, and a thread that has stopped | ||
| /// might see its ID reused. | ||
| pub fn id(&self) -> u32 { |
This comment has been minimized.
This comment has been minimized.
huonw
Oct 29, 2015
Member
If this PR is accepted, this will need to be marked #[unstable] and have a tracking issue opened. (See catch_panic in this module for an example.)
retep998
reviewed
Oct 29, 2015
| @@ -78,6 +78,10 @@ impl Thread { | |||
| c::Sleep(super::dur2timeout(dur)) | |||
| } | |||
| } | |||
|
|
|||
| pub fn id(&self) -> u32 { | |||
| 42 // TODO | |||
This comment has been minimized.
This comment has been minimized.
nagisa
reviewed
Oct 29, 2015
| @@ -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); } | |||
This comment has been minimized.
This comment has been minimized.
nagisa
Oct 29, 2015
Contributor
This is wrong.
POSIX.1 allows an implementation wide freedom in choosing the type used to represent a thread ID; for example, representation using either an arithmetic type or a structure is permitted.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
remram44
Oct 29, 2015
Author
Contributor
On Linux pthread_t is unsigned long, thus u64 on amd64. There is no portable way to get a numeric ID from pthreads (pthread_getthreadid_np is not portable).
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.
This comment has been minimized.
This comment has been minimized.
retep998
Oct 29, 2015
Member
On Windows what I want is some sort of OS provided ID. Either the thread HANDLE or the thread ID which is a DWORD. Anything else would be useless to me as I wouldn't be able to use it in OS functions to manipulate that thread like queuing APCs to it.
This comment has been minimized.
This comment has been minimized.
retep998
Oct 29, 2015
Member
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 HANDLE of a thread.
nagisa
reviewed
Oct 29, 2015
| // Next, set up our stack overflow handler which may get triggered if we run | ||
| // out of stack. | ||
| let _handler = stack_overflow::Handler::new(); | ||
|
|
||
| // Finally, let's run some code. | ||
| Box::from_raw(main as *mut Box<FnBox()>)() | ||
| Box::from_raw(main as *mut Box<FnBox(u32)>)(id) |
This comment has been minimized.
This comment has been minimized.
nagisa
Oct 29, 2015
Contributor
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 calls pthread_self.
This comment has been minimized.
This comment has been minimized.
|
I think that there's a few pieces that may want to be tweaked here:
I'm also not sure that we want to use the exact same ID as the underlying OS. As @nagisa points out we can't rely on |
This comment has been minimized.
This comment has been minimized.
Getting the ID of the current thread is trivial, especially on Windows, so there shouldn't be any reason for |
This comment has been minimized.
This comment has been minimized.
|
|
This comment has been minimized.
This comment has been minimized.
|
Haven't looked at this in detail, but because the Rust thread type outlives the system thread, it's possible for comparison operations to produce incorrect results if the system reuses the thread id. |
This comment has been minimized.
This comment has been minimized.
|
The libs team discussed this in triage today, and in addition to @brson's comment we also had thinking along the lines of:
|
alexcrichton
removed
the
I-nominated
label
Nov 12, 2015
This comment has been minimized.
This comment has been minimized.
|
Closing due to inactivity, but feel free to resubmit with the comments on this and #29447 addressed! |
remram44 commentedOct 29, 2015
See #21507
Because we only get an ID once the thread has been spawned, and at that point the Inner struct is referenced from both the thread (which is gonna store it in the threadlocal thread_info) and the parent (which returns it), it was much easier to add it to Thread than to Inner. It is a single u32 so I'm not sure the memory usage is an issue, but it is a little awkward to have id in Thread and name in Inner?
Let me know.