Skip to content

Replace deprecated compare_and_swap with compare_exchange #3

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
Dec 24, 2020
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
22 changes: 12 additions & 10 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
use std::cell::UnsafeCell;
use std::fmt;
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;

/// A synchronization primitive for task wakeup.
Expand Down Expand Up @@ -261,7 +261,11 @@ impl AtomicWaker {
/// }
/// ```
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 => {
unsafe {
// Locked acquired, update the waker cell
Expand All @@ -277,8 +281,9 @@ impl AtomicWaker {
// nothing to acquire, only release. In case of concurrent
// wakers, we need to acquire their releases, so success needs
// to do both.
let res = self.state.compare_exchange(
REGISTERING, WAITING, AcqRel, Acquire);
let res = self
.state
.compare_exchange(REGISTERING, WAITING, AcqRel, Acquire);

match res {
Ok(_) => {
Expand Down Expand Up @@ -342,9 +347,7 @@ impl AtomicWaker {
//
// We just want to maintain memory safety. It is ok to drop the
// call to `register`.
debug_assert!(
state == REGISTERING ||
state == REGISTERING | WAKING);
debug_assert!(state == REGISTERING || state == REGISTERING | WAKING);
}
}
}
Expand Down Expand Up @@ -389,9 +392,8 @@ impl AtomicWaker {
// not.
//
debug_assert!(
state == REGISTERING ||
state == REGISTERING | WAKING ||
state == WAKING);
state == REGISTERING || state == REGISTERING | WAKING || state == WAKING
);
None
}
}
Expand Down