Skip to content

Commit

Permalink
std: fix deadlock in Parker
Browse files Browse the repository at this point in the history
  • Loading branch information
joboet committed May 19, 2022
1 parent fd76552 commit 3b6ae15
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 8 deletions.
13 changes: 9 additions & 4 deletions library/std/src/sys/itron/wait_flag.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,13 +42,18 @@ impl WaitFlag {
}

/// Wait for the wait flag to be raised or the timeout to occur.
pub fn wait_timeout(&self, dur: Duration) {
///
/// Returns whether the flag was raised (`true`) or the operation timed out (`false`).
pub fn wait_timeout(&self, dur: Duration) -> bool {
let mut token = MaybeUninit::uninit();
let er = with_tmos(dur, |tmout| unsafe {
let res = with_tmos(dur, |tmout| unsafe {
abi::twai_flg(self.flag, RAISED, abi::TWF_ORW, token.as_mut_ptr(), tmout)
});
if er != abi::E_OK && er != abi::E_TMOUT {
fail(er, &"twai_flg");

match res {
abi::E_OK => true,
abi::E_TMOUT => false,
error => fail(error, &"twai_flg"),
}
}

Expand Down
8 changes: 4 additions & 4 deletions library/std/src/sys_common/thread_parker/wait_flag.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
//!
//! Some operating systems provide low-level parking primitives like wait counts,
//! event flags or semaphores which are not susceptible to race conditions (meaning
//! the wakeup can occure before the wait operation). To implement the `std` thread
//! the wakeup can occur before the wait operation). To implement the `std` thread
//! parker on top of these primitives, we only have to ensure that parking is fast
//! when the thread token is available, the atomic ordering guarantees are maintained
//! and spurious wakeups are minimized.
Expand Down Expand Up @@ -73,10 +73,10 @@ impl Parker {
_ => panic!("inconsistent park state"),
}

self.wait_flag.wait_timeout(dur);
let wakeup = self.wait_flag.wait_timeout(dur);
let state = self.state.swap(EMPTY, SeqCst);
if state == NOTIFIED {
// The token was made available after the timeout occurred, but before
if state == NOTIFIED && !wakeup {
// The token was made available after the wait timed out, but before
// we reset the state, so we need to reset the wait flag to avoid
// spurious wakeups. This wait has no timeout, but we know it will
// return quickly, as the unparking thread will definitely raise the
Expand Down

0 comments on commit 3b6ae15

Please sign in to comment.