Skip to content

Commit 7da469d

Browse files
authored
park():prohibit spurious wakeups in next park
should consume this notification, so prohibit spurious wakeups in next park
1 parent edae1cc commit 7da469d

File tree

1 file changed

+10
-2
lines changed

1 file changed

+10
-2
lines changed

src/libstd/thread/mod.rs

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -796,7 +796,11 @@ pub fn park() {
796796
let mut m = thread.inner.lock.lock().unwrap();
797797
match thread.inner.state.compare_exchange(EMPTY, PARKED, SeqCst, SeqCst) {
798798
Ok(_) => {}
799-
Err(NOTIFIED) => return, // notified after we locked
799+
Err(NOTIFIED) => {
800+
// should consume this notification, so prohibit spurious wakeups in next park...
801+
thread.inner.state.store(EMPTY, SeqCst);
802+
return;
803+
}, // notified after we locked
800804
Err(_) => panic!("inconsistent park state"),
801805
}
802806
loop {
@@ -882,7 +886,11 @@ pub fn park_timeout(dur: Duration) {
882886
let m = thread.inner.lock.lock().unwrap();
883887
match thread.inner.state.compare_exchange(EMPTY, PARKED, SeqCst, SeqCst) {
884888
Ok(_) => {}
885-
Err(NOTIFIED) => return, // notified after we locked
889+
Err(NOTIFIED) => {
890+
// should consume this notification, so prohibit spurious wakeups in next park...
891+
thread.inner.state.store(EMPTY, SeqCst);
892+
return;
893+
}, // notified after we locked
886894
Err(_) => panic!("inconsistent park_timeout state"),
887895
}
888896

0 commit comments

Comments
 (0)