-
Notifications
You must be signed in to change notification settings - Fork 12.7k
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
Stabilize ThreadId::as_u64
#110738
Stabilize ThreadId::as_u64
#110738
Conversation
r? @m-ou-se (rustbot has picked a reviewer for you, use r? to override) |
Hey! It looks like you've submitted a new PR for the library teams! If this PR contains changes to any Examples of
|
This comment has been minimized.
This comment has been minimized.
I think that we shouldn't apply a permutation to the output, and just return the sequential thread ID directly. This is the least surprising behavior. @rfcbot fcp merge |
Team member @Amanieu has proposed to merge this. The next step is review by the rest of the tagged team members: No concerns currently listed. Once a majority of reviewers approve (and at most 2 approvals are outstanding), this will enter its final comment period. If you spot a major issue that hasn't been raised at any point in this process, please speak up! See this document for info about what commands tagged team members can give me. |
I think the FCP is for the wrong team.
I'll argue that it's only surprising if you have an expectation that is inconsistent with what the API actually promises and that's all the more reason to change it. But we should discuss that on the other PR or the next libs meeting. |
@rfcbot fcp cancel |
@Amanieu proposal cancelled. |
@rfcbot fcp merge |
Team member @Amanieu has proposed to merge this. The next step is review by the rest of the tagged team members: Concerns:
Once a majority of reviewers approve (and at most 2 approvals are outstanding), this will enter its final comment period. If you spot a major issue that hasn't been raised at any point in this process, please speak up! See this document for info about what commands tagged team members can give me. |
The documentation for
So the first sentence says that "it is essentially an opaque ID" and the second sentence says "the returned value is entirely opaque." This, to me, leaves me confused. To say something is "essentially an opaque ID" leaves a little room for ambiguity. I think the docs should also call out why someone might want to use this routine. If one already has a @rfcbot concern docs |
I suppose this is perhaps not a stabilization concern, but I think it would be good to at least have a discussion of permute versus non-permute before stabilizing. Just to make sure I am understanding things correctly, I think there are two choices on the table?
It seems to me like we should do the latter if we can. If we do the former, it seems inevitable that folks will rely on it implicitly. And then we'll be pragmatically locked into that choice. So to me, and the reason why it's important to discuss this during stabilization, is that if we're going to go with the former then it seems like we should just be realistic and add that as part of our API promise. @rfcbot concern thread-id-generation |
I think it's fine to guarantee the IDs are generated in order. We already guarantee the IDs will never be recycled, so we need to generate our own IDs anyway. |
We briefly discussed this in today's libs-api meeting. We didn't reach a consensus on whether to permutate/randomize the IDs or to guarantee that they are sequentual, but we did think we should come to a conclusion on that before stabilizing this. |
Concerns about guaranteeing ThreadID assignment by incrementing an atomic integer
Concerns about using an u64: Assuming a hypothetical supercomputer with 131072 cores / compute units, 5GHz clock rate and a lightweight thread/task creation primitive that takes 100 instructions it's a ~6.5THz thread spawn rate -> 2^42 steps per second x 2^25 seconds per year -> more than 2^64 steps per year. |
Ok so there are two issues being debated. Is The other is if we should use randomness or permutations or something along those lines. I'm not sure I understand the threat model here. Concretely, what are the acual issues we're guarding against? We can't guarentee that the main thread is numbered |
Users relying on implementation details instead of the contract. E.g. implementing "if main thread" as
This isn't specific to non-rust threads. As soon as you have more than 1 thread and they're not synchronized you already have no global ordering anymore which means guaranteeing anything about ordering becomes nonsense.
Transmute is unsafe and has loud warnings on it though. |
From the zulip discussion: We need a finer breakdown of the use-cases. It seems possible that there are at least two non-overlapping ones that can be served by having separate APIs. A) ensure that a value is only be used on the thread that created it (e.g. due to TLS access). If the value can live longer than the thread then preventing ID reuse is important. Checking Neither the original PR (#67566, CC @Mark-Simulacrum ) nor the tracking issue are particularly illuminating. Perhaps a |
A use case that doesn't seem covered here is annotating log/tracing messages with a numeric ID that uniquely identifies the thread that the log/trace call was made on. An example where this is useful is constructing a per-thread hierarchical overview of tracing spans to show how much time was spent in different portions of a program. Currently the only sound way to get some kind of unique ID for logging purposes is to use the |
That doesn't answer the question of whether it has to be A) unique among the currently running threads or B) unique for the program lifetime. If a library is ok with printing the OS TID (as many libraries in other languages do) that would be A) while |
For the use cases I was considering, the |
Are there any RFCs for exposing the native thread ID (oh god, as I was writing this I realised that "thread ID" is an incredibly dubious concept on Unix)? As mentioned in #115746 it would be useful to have some means of mapping between Rust ThreadIds and native thread IDs that will show up in debuggers |
Stabilization proposal
This PR proposes to stabilize the following API:
Tracking issue: #67939
Implementation History
NonZeroU64
in Return NonZeroU64 from ThreadId::as_u64. #70240Design Considerations
The big question has been whether
ThreadId
should manage it's own internal counter or expose operating system IDs directly. #84083 added the guarantee thatThreadId
is unique for the lifetime of the process, which makes managing IDs ourselves necessary. Note that this also makes returning au64
necessary vs.usize
.The IDs being managed internally makes it trivial to guarantee that they are non-zero. This niche makes it possible to store an
Option<ThreadId>
as anAtomicU64
.Additionally, #110725 proposes applying a permutation sequence to avoid users relying on any internal order. The value is already documented as opaque, so it's not clear whether this is a stabilization concern.
Blocked on an FCP for stabilization.