Skip to content

Commit 5559680

Browse files
authored
Replace deprecated compare_and_swap with compare_exchange (#2297)
1 parent 8a04b51 commit 5559680

File tree

3 files changed

+43
-32
lines changed

3 files changed

+43
-32
lines changed

futures-channel/src/mpsc/mod.rs

+20-22
Original file line numberDiff line numberDiff line change
@@ -923,17 +923,16 @@ impl<T> Clone for UnboundedSenderInner<T> {
923923
debug_assert!(curr < MAX_BUFFER);
924924

925925
let next = curr + 1;
926-
let actual = self.inner.num_senders.compare_and_swap(curr, next, SeqCst);
927-
928-
// The ABA problem doesn't matter here. We only care that the
929-
// number of senders never exceeds the maximum.
930-
if actual == curr {
931-
return Self {
932-
inner: self.inner.clone(),
933-
};
926+
match self.inner.num_senders.compare_exchange(curr, next, SeqCst, SeqCst) {
927+
Ok(_) => {
928+
// The ABA problem doesn't matter here. We only care that the
929+
// number of senders never exceeds the maximum.
930+
return Self {
931+
inner: self.inner.clone(),
932+
};
933+
}
934+
Err(actual) => curr = actual,
934935
}
935-
936-
curr = actual;
937936
}
938937
}
939938
}
@@ -954,19 +953,18 @@ impl<T> Clone for BoundedSenderInner<T> {
954953
debug_assert!(curr < self.inner.max_senders());
955954

956955
let next = curr + 1;
957-
let actual = self.inner.num_senders.compare_and_swap(curr, next, SeqCst);
958-
959-
// The ABA problem doesn't matter here. We only care that the
960-
// number of senders never exceeds the maximum.
961-
if actual == curr {
962-
return Self {
963-
inner: self.inner.clone(),
964-
sender_task: Arc::new(Mutex::new(SenderTask::new())),
965-
maybe_parked: false,
966-
};
956+
match self.inner.num_senders.compare_exchange(curr, next, SeqCst, SeqCst) {
957+
Ok(_) => {
958+
// The ABA problem doesn't matter here. We only care that the
959+
// number of senders never exceeds the maximum.
960+
return Self {
961+
inner: self.inner.clone(),
962+
sender_task: Arc::new(Mutex::new(SenderTask::new())),
963+
maybe_parked: false,
964+
};
965+
}
966+
Err(actual) => curr = actual,
967967
}
968-
969-
curr = actual;
970968
}
971969
}
972970
}

futures-core/src/task/__internal/atomic_waker.rs

+5-1
Original file line numberDiff line numberDiff line change
@@ -259,7 +259,11 @@ impl AtomicWaker {
259259
/// }
260260
/// ```
261261
pub fn register(&self, waker: &Waker) {
262-
match self.state.compare_and_swap(WAITING, REGISTERING, Acquire) {
262+
match self
263+
.state
264+
.compare_exchange(WAITING, REGISTERING, Acquire, Acquire)
265+
.unwrap_or_else(|x| x)
266+
{
263267
WAITING => {
264268
unsafe {
265269
// Locked acquired, update the waker cell

futures-util/src/future/future/shared.rs

+18-9
Original file line numberDiff line numberDiff line change
@@ -210,7 +210,12 @@ where
210210

211211
inner.record_waker(&mut this.waker_key, cx);
212212

213-
match inner.notifier.state.compare_and_swap(IDLE, POLLING, SeqCst) {
213+
match inner
214+
.notifier
215+
.state
216+
.compare_exchange(IDLE, POLLING, SeqCst, SeqCst)
217+
.unwrap_or_else(|x| x)
218+
{
214219
IDLE => {
215220
// Lock acquired, fall through
216221
}
@@ -255,14 +260,18 @@ where
255260

256261
match future.poll(&mut cx) {
257262
Poll::Pending => {
258-
match inner.notifier.state.compare_and_swap(POLLING, IDLE, SeqCst) {
259-
POLLING => {
260-
// Success
261-
drop(_reset);
262-
this.inner = Some(inner);
263-
return Poll::Pending;
264-
}
265-
_ => unreachable!(),
263+
if inner
264+
.notifier
265+
.state
266+
.compare_exchange(POLLING, IDLE, SeqCst, SeqCst)
267+
.is_ok()
268+
{
269+
// Success
270+
drop(_reset);
271+
this.inner = Some(inner);
272+
return Poll::Pending;
273+
} else {
274+
unreachable!()
266275
}
267276
}
268277
Poll::Ready(output) => output,

0 commit comments

Comments
 (0)