Merge pull request #3 from taiki-e/compare_and_swap
Replace deprecated compare_and_swap with compare_exchange
This commit is contained in:
commit
1c275b99da
22
src/lib.rs
22
src/lib.rs
|
@ -3,7 +3,7 @@
|
||||||
use std::cell::UnsafeCell;
|
use std::cell::UnsafeCell;
|
||||||
use std::fmt;
|
use std::fmt;
|
||||||
use std::sync::atomic::AtomicUsize;
|
use std::sync::atomic::AtomicUsize;
|
||||||
use std::sync::atomic::Ordering::{Acquire, Release, AcqRel};
|
use std::sync::atomic::Ordering::{AcqRel, Acquire, Release};
|
||||||
use std::task::Waker;
|
use std::task::Waker;
|
||||||
|
|
||||||
/// A synchronization primitive for task wakeup.
|
/// A synchronization primitive for task wakeup.
|
||||||
|
@ -261,7 +261,11 @@ impl AtomicWaker {
|
||||||
/// }
|
/// }
|
||||||
/// ```
|
/// ```
|
||||||
pub fn register(&self, waker: &Waker) {
|
pub fn register(&self, waker: &Waker) {
|
||||||
match self.state.compare_and_swap(WAITING, REGISTERING, Acquire) {
|
match self
|
||||||
|
.state
|
||||||
|
.compare_exchange(WAITING, REGISTERING, Acquire, Acquire)
|
||||||
|
.unwrap_or_else(|x| x)
|
||||||
|
{
|
||||||
WAITING => {
|
WAITING => {
|
||||||
unsafe {
|
unsafe {
|
||||||
// Locked acquired, update the waker cell
|
// Locked acquired, update the waker cell
|
||||||
|
@ -277,8 +281,9 @@ impl AtomicWaker {
|
||||||
// nothing to acquire, only release. In case of concurrent
|
// nothing to acquire, only release. In case of concurrent
|
||||||
// wakers, we need to acquire their releases, so success needs
|
// wakers, we need to acquire their releases, so success needs
|
||||||
// to do both.
|
// to do both.
|
||||||
let res = self.state.compare_exchange(
|
let res = self
|
||||||
REGISTERING, WAITING, AcqRel, Acquire);
|
.state
|
||||||
|
.compare_exchange(REGISTERING, WAITING, AcqRel, Acquire);
|
||||||
|
|
||||||
match res {
|
match res {
|
||||||
Ok(_) => {
|
Ok(_) => {
|
||||||
|
@ -342,9 +347,7 @@ impl AtomicWaker {
|
||||||
//
|
//
|
||||||
// We just want to maintain memory safety. It is ok to drop the
|
// We just want to maintain memory safety. It is ok to drop the
|
||||||
// call to `register`.
|
// call to `register`.
|
||||||
debug_assert!(
|
debug_assert!(state == REGISTERING || state == REGISTERING | WAKING);
|
||||||
state == REGISTERING ||
|
|
||||||
state == REGISTERING | WAKING);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -389,9 +392,8 @@ impl AtomicWaker {
|
||||||
// not.
|
// not.
|
||||||
//
|
//
|
||||||
debug_assert!(
|
debug_assert!(
|
||||||
state == REGISTERING ||
|
state == REGISTERING || state == REGISTERING | WAKING || state == WAKING
|
||||||
state == REGISTERING | WAKING ||
|
);
|
||||||
state == WAKING);
|
|
||||||
None
|
None
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue