Skip to content

Commit caff723

Browse files
joboetkawadakk
andcommitted
std: relax memory orderings in Parker
Co-authored-by: Tomoaki Kawada <kawada@kmckk.co.jp>
1 parent b9660de commit caff723

File tree

1 file changed

+7
-8
lines changed

1 file changed

+7
-8
lines changed

library/std/src/sys_common/thread_parker/wait_flag.rs

+7-8
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
2626
use crate::pin::Pin;
2727
use crate::sync::atomic::AtomicI8;
28-
use crate::sync::atomic::Ordering::{Relaxed, SeqCst};
28+
use crate::sync::atomic::Ordering::{Acquire, Relaxed, Release};
2929
use crate::sys::wait_flag::WaitFlag;
3030
use crate::time::Duration;
3131

@@ -47,7 +47,7 @@ impl Parker {
4747

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

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

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

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

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

9998
if state == PARKED {
10099
self.wait_flag.raise();

0 commit comments

Comments
 (0)