Skip to content

Commit

Permalink
std: relax memory orderings in Parker
Browse files Browse the repository at this point in the history
Co-authored-by: Tomoaki Kawada <kawada@kmckk.co.jp>
  • Loading branch information
joboet and kawadakk committed Jun 15, 2022
1 parent b9660de commit caff723
Showing 1 changed file with 7 additions and 8 deletions.
15 changes: 7 additions & 8 deletions library/std/src/sys_common/thread_parker/wait_flag.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
use crate::pin::Pin;
use crate::sync::atomic::AtomicI8;
use crate::sync::atomic::Ordering::{Relaxed, SeqCst};
use crate::sync::atomic::Ordering::{Acquire, Relaxed, Release};
use crate::sys::wait_flag::WaitFlag;
use crate::time::Duration;

Expand All @@ -47,7 +47,7 @@ impl Parker {

// This implementation doesn't require `unsafe` and `Pin`, but other implementations do.
pub unsafe fn park(self: Pin<&Self>) {
match self.state.fetch_sub(1, SeqCst) {
match self.state.fetch_sub(1, Acquire) {
// NOTIFIED => EMPTY
NOTIFIED => return,
// EMPTY => PARKED
Expand All @@ -59,7 +59,7 @@ impl Parker {
loop {
self.wait_flag.wait();

match self.state.compare_exchange(NOTIFIED, EMPTY, SeqCst, Relaxed) {
match self.state.compare_exchange(NOTIFIED, EMPTY, Acquire, Relaxed) {
Ok(_) => return,
Err(PARKED) => (),
Err(_) => panic!("inconsistent park state"),
Expand All @@ -69,7 +69,7 @@ impl Parker {

// This implementation doesn't require `unsafe` and `Pin`, but other implementations do.
pub unsafe fn park_timeout(self: Pin<&Self>, dur: Duration) {
match self.state.fetch_sub(1, SeqCst) {
match self.state.fetch_sub(1, Acquire) {
NOTIFIED => return,
EMPTY => (),
_ => panic!("inconsistent park state"),
Expand All @@ -83,9 +83,8 @@ impl Parker {
// is protected against this by looping until the token is actually given, but
// here we cannot easily tell.

// Use `swap` to provide acquire ordering (not strictly necessary, but all other
// implementations do).
match self.state.swap(EMPTY, SeqCst) {
// Use `swap` to provide acquire ordering.
match self.state.swap(EMPTY, Acquire) {
NOTIFIED => (),
PARKED => (),
_ => panic!("inconsistent park state"),
Expand All @@ -94,7 +93,7 @@ impl Parker {

// This implementation doesn't require `Pin`, but other implementations do.
pub fn unpark(self: Pin<&Self>) {
let state = self.state.swap(NOTIFIED, SeqCst);
let state = self.state.swap(NOTIFIED, Release);

if state == PARKED {
self.wait_flag.raise();
Expand Down

0 comments on commit caff723

Please sign in to comment.