Skip to content

Synchronize with all calls to unpark in id-based thread parker #114591

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Aug 18, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 6 additions & 11 deletions library/std/src/sys_common/thread_parking/id.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,18 +56,14 @@ impl Parker {
self.init_tid();

// Changes NOTIFIED to EMPTY and EMPTY to PARKED.
let mut state = self.state.fetch_sub(1, Acquire).wrapping_sub(1);
if state == PARKED {
let state = self.state.fetch_sub(1, Acquire);
if state == EMPTY {
// Loop to guard against spurious wakeups.
while state == PARKED {
// The state must be reset with acquire ordering to ensure that all
// calls to `unpark` synchronize with this thread.
while self.state.compare_exchange(NOTIFIED, EMPTY, Acquire, Relaxed).is_err() {
park(self.state.as_ptr().addr());
state = self.state.load(Acquire);
}

// Since the state change has already been observed with acquire
// ordering, the state can be reset with a relaxed store instead
// of a swap.
self.state.store(EMPTY, Relaxed);
}
}

Expand All @@ -78,8 +74,7 @@ impl Parker {
if state == PARKED {
park_timeout(dur, self.state.as_ptr().addr());
// Swap to ensure that we observe all state changes with acquire
// ordering, even if the state has been changed after the timeout
// occurred.
// ordering.
self.state.swap(EMPTY, Acquire);
}
}
Expand Down