Fix a race in thread wakeup (#459)

This commit is contained in:
Stjepan Glavina
2018-07-03 16:28:50 -07:00
committed by Carl Lerche
parent dbefa67058
commit 7fb579c667
3 changed files with 16 additions and 20 deletions
+7 -5
View File
@@ -133,8 +133,8 @@ impl Inner {
None => self.condvar.wait(m).unwrap(),
};
// Transition back to idle. If the state has transitions dto `NOTIFY`,
// this will consume that notification
// Transition back to idle. If the state has transitioned to `NOTIFY`,
// this will consume that notification.
self.state.store(IDLE, SeqCst);
// Explicitly drop the mutex guard. There is no real point in doing it
@@ -155,10 +155,12 @@ impl Inner {
// The other half is sleeping, this requires a lock
let _m = self.mutex.lock().unwrap();
// Transition from SLEEP -> NOTIFY
match self.state.compare_and_swap(SLEEP, NOTIFY, SeqCst) {
// Transition to NOTIFY
match self.state.swap(NOTIFY, SeqCst) {
SLEEP => {}
_ => return,
NOTIFY => return,
IDLE => return,
_ => unreachable!(),
}
// Wakeup the sleeper