Skip to content

Commit c0320dc

Browse files
authored
Rollup merge of #149481 - RalfJung:threadid-fallback, r=joboet
ThreadId generation fallback path: avoid spurious yields Fixes rust-lang/miri#4737 Alternative to #149476 Cc `@orlp` `@joboet`
2 parents 77178e9 + 1fccfa6 commit c0320dc

File tree

1 file changed

+4
-1
lines changed
  • library/std/src/thread

1 file changed

+4
-1
lines changed

library/std/src/thread/id.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,9 @@ impl ThreadId {
7070

7171
// Acquire lock.
7272
let mut spin = 0;
73-
while COUNTER_LOCKED.compare_exchange_weak(false, true, Ordering::Acquire, Ordering::Relaxed).is_err() {
73+
// Miri doesn't like it when we yield here as it interferes with deterministically
74+
// scheduling threads, so avoid `compare_exchange_weak` to avoid spurious yields.
75+
while COUNTER_LOCKED.swap(true, Ordering::Acquire, Ordering::Relaxed) {
7476
if spin <= 3 {
7577
for _ in 0..(1 << spin) {
7678
spin_loop();
@@ -80,6 +82,7 @@ impl ThreadId {
8082
}
8183
spin += 1;
8284
}
85+
// This was `false` before the swap, so we got the lock.
8386

8487
// SAFETY: we have an exclusive lock on the counter.
8588
unsafe {

0 commit comments

Comments
 (0)