std: relax memory orderings in Parker
Co-authored-by: Tomoaki Kawada <kawada@kmckk.co.jp>
This commit is contained in:
parent
b9660de664
commit
caff72361f
@ -25,7 +25,7 @@
|
|||||||
|
|
||||||
use crate::pin::Pin;
|
use crate::pin::Pin;
|
||||||
use crate::sync::atomic::AtomicI8;
|
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::sys::wait_flag::WaitFlag;
|
||||||
use crate::time::Duration;
|
use crate::time::Duration;
|
||||||
|
|
||||||
@ -47,7 +47,7 @@ impl Parker {
|
|||||||
|
|
||||||
// This implementation doesn't require `unsafe` and `Pin`, but other implementations do.
|
// This implementation doesn't require `unsafe` and `Pin`, but other implementations do.
|
||||||
pub unsafe fn park(self: Pin<&Self>) {
|
pub unsafe fn park(self: Pin<&Self>) {
|
||||||
match self.state.fetch_sub(1, SeqCst) {
|
match self.state.fetch_sub(1, Acquire) {
|
||||||
// NOTIFIED => EMPTY
|
// NOTIFIED => EMPTY
|
||||||
NOTIFIED => return,
|
NOTIFIED => return,
|
||||||
// EMPTY => PARKED
|
// EMPTY => PARKED
|
||||||
@ -59,7 +59,7 @@ impl Parker {
|
|||||||
loop {
|
loop {
|
||||||
self.wait_flag.wait();
|
self.wait_flag.wait();
|
||||||
|
|
||||||
match self.state.compare_exchange(NOTIFIED, EMPTY, SeqCst, Relaxed) {
|
match self.state.compare_exchange(NOTIFIED, EMPTY, Acquire, Relaxed) {
|
||||||
Ok(_) => return,
|
Ok(_) => return,
|
||||||
Err(PARKED) => (),
|
Err(PARKED) => (),
|
||||||
Err(_) => panic!("inconsistent park state"),
|
Err(_) => panic!("inconsistent park state"),
|
||||||
@ -69,7 +69,7 @@ impl Parker {
|
|||||||
|
|
||||||
// This implementation doesn't require `unsafe` and `Pin`, but other implementations do.
|
// This implementation doesn't require `unsafe` and `Pin`, but other implementations do.
|
||||||
pub unsafe fn park_timeout(self: Pin<&Self>, dur: Duration) {
|
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,
|
NOTIFIED => return,
|
||||||
EMPTY => (),
|
EMPTY => (),
|
||||||
_ => panic!("inconsistent park state"),
|
_ => panic!("inconsistent park state"),
|
||||||
@ -83,9 +83,8 @@ impl Parker {
|
|||||||
// is protected against this by looping until the token is actually given, but
|
// is protected against this by looping until the token is actually given, but
|
||||||
// here we cannot easily tell.
|
// here we cannot easily tell.
|
||||||
|
|
||||||
// Use `swap` to provide acquire ordering (not strictly necessary, but all other
|
// Use `swap` to provide acquire ordering.
|
||||||
// implementations do).
|
match self.state.swap(EMPTY, Acquire) {
|
||||||
match self.state.swap(EMPTY, SeqCst) {
|
|
||||||
NOTIFIED => (),
|
NOTIFIED => (),
|
||||||
PARKED => (),
|
PARKED => (),
|
||||||
_ => panic!("inconsistent park state"),
|
_ => panic!("inconsistent park state"),
|
||||||
@ -94,7 +93,7 @@ impl Parker {
|
|||||||
|
|
||||||
// This implementation doesn't require `Pin`, but other implementations do.
|
// This implementation doesn't require `Pin`, but other implementations do.
|
||||||
pub fn unpark(self: Pin<&Self>) {
|
pub fn unpark(self: Pin<&Self>) {
|
||||||
let state = self.state.swap(NOTIFIED, SeqCst);
|
let state = self.state.swap(NOTIFIED, Release);
|
||||||
|
|
||||||
if state == PARKED {
|
if state == PARKED {
|
||||||
self.wait_flag.raise();
|
self.wait_flag.raise();
|
||||||
|
Loading…
x
Reference in New Issue
Block a user