Skip to content

Commit 4774538

Browse files
committed
make Miri's scheduler proper round-robin
1 parent 5a1b09e commit 4774538

File tree

2 files changed

+51
-8
lines changed

2 files changed

+51
-8
lines changed

src/thread.rs

+18-8
Original file line numberDiff line numberDiff line change
@@ -518,16 +518,26 @@ impl<'mir, 'tcx: 'mir> ThreadManager<'mir, 'tcx> {
518518
return Ok(SchedulingAction::ExecuteTimeoutCallback);
519519
}
520520
// No callbacks scheduled, pick a regular thread to execute.
521-
// We need to pick a new thread for execution.
522-
for (id, thread) in self.threads.iter_enumerated() {
521+
// The active thread blocked or yielded. So we go search for another enabled thread.
522+
// Curcially, we start searching at the current active thread ID, rather than at 0, since we
523+
// want to avoid always scheduling threads 0 and 1 without ever making progress in thread 2.
524+
//
525+
// `skip(N)` means we start iterating at thread N, so we skip 1 more to start just *after*
526+
// the active thread. Then after that we look at `take(N)`, i.e., the threads *before* the
527+
// active thread.
528+
let threads = self
529+
.threads
530+
.iter_enumerated()
531+
.skip(self.active_thread.index() + 1)
532+
.chain(self.threads.iter_enumerated().take(self.active_thread.index()));
533+
for (id, thread) in threads {
534+
debug_assert_ne!(self.active_thread, id);
523535
if thread.state == ThreadState::Enabled {
524-
if !self.yield_active_thread || id != self.active_thread {
525-
self.active_thread = id;
526-
if let Some(data_race) = data_race {
527-
data_race.thread_set_active(self.active_thread);
528-
}
529-
break;
536+
self.active_thread = id;
537+
if let Some(data_race) = data_race {
538+
data_race.thread_set_active(self.active_thread);
530539
}
540+
break;
531541
}
532542
}
533543
self.yield_active_thread = false;

tests/pass/concurrency/spin_loops.rs

+33
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
// ignore-windows: Concurrency on Windows is not supported yet.
2+
3+
use std::thread;
4+
use std::sync::atomic::{AtomicUsize, Ordering};
5+
6+
static FLAG: AtomicUsize = AtomicUsize::new(0);
7+
8+
// When a thread yields, Miri's scheduler used to pick the thread with the lowest ID
9+
// that can run. IDs are assigned in thread creation order.
10+
// This means we could make 2 threads infinitely ping-pong with each other while
11+
// really there is a 3rd thread that we should schedule to make progress.
12+
13+
fn main() {
14+
let waiter1 = thread::spawn(|| {
15+
while FLAG.load(Ordering::Acquire) == 0 {
16+
// spin and wait
17+
thread::yield_now();
18+
}
19+
});
20+
let waiter2 = thread::spawn(|| {
21+
while FLAG.load(Ordering::Acquire) == 0 {
22+
// spin and wait
23+
thread::yield_now();
24+
}
25+
});
26+
let progress = thread::spawn(|| {
27+
FLAG.store(1, Ordering::Release);
28+
});
29+
// The first `join` blocks the main thread and thus takes it out of the equation.
30+
waiter1.join().unwrap();
31+
waiter2.join().unwrap();
32+
progress.join().unwrap();
33+
}

0 commit comments

Comments
 (0)