-
Notifications
You must be signed in to change notification settings - Fork 14.1k
std: use a mutex for ThreadId generation if available
#149476
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
base: main
Are you sure you want to change the base?
Conversation
|
rustbot has assigned @Mark-Simulacrum. Use |
| #[cold] | ||
| fn exhausted() -> ! { | ||
| panic!("failed to generate unique thread ID: bitspace exhausted") | ||
| rtabort!("failed to generate unique thread ID: bitspace exhausted") |
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.
Panicking may allocate, so it shouldn't be used here either.
|
This violates the contract we established that the global allocator may call So if there's a platform without 64-bit atomics but which has an allocating |
No it won't, the |
|
@joboet Actually, re-reading the code, neither If we were to adopt #147342 I think we might be able to get rid of the desperate path altogether. |
The problem is that not all threads are spawned by Rust – and on foreign threads, it's the first call to |
|
I hadn't considered foreign threads, you're right. |
|
The job Click to see the possible cause of the failure (guessed by this bot) |
|
This still invokes |
| } | ||
| } | ||
|
|
||
| /// Generates a new unique thread 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.
| /// Generates a new unique thread ID. | |
| /// Generates a new unique thread ID. Might use the global allocator. |
ThreadId generation fallback path: avoid spurious yields Fixes rust-lang/miri#4737 Alternative to rust-lang#149476 Cc `@orlp` `@joboet`
ThreadId generation fallback path: avoid spurious yields Fixes rust-lang/miri#4737 Alternative to #149476 Cc `@orlp` `@joboet`
Fixes rust-lang/miri#4737
#144465 changed
ThreadId::newto use a spinlock on platforms without 64-bit atomics. This introduces subtle scheduling changes such as the one described in rust-lang/miri#4737 and isn't ideal from a performance perspective either. The necessity of avoidingMutexis not always given however – it only needs to be avoided if the thread handle is lazily initialised. For normal Rust threads however,stdgenerates the thread id inside the spawning logic, where allocation is expected. Thus this PR renames the currentThreadId::newtodesperate_newand reintroduces a versionThreadId::newthat employs aMutexto synchronise access to the inner spin-lock.CC @RalfJung @orlp