Skip to content

Commit 39e8328

Browse files
authored
Replace deprecated compare_and_swap with compare_exchange (rust-lang#2297)
1 parent 5016a29 commit 39e8328

File tree

3 files changed

+39
-32
lines changed

3 files changed

+39
-32
lines changed

futures-channel/src/mpsc/mod.rs

Lines changed: 20 additions & 22 deletions
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

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -256,7 +256,11 @@ impl AtomicWaker {
256256
/// }
257257
/// ```
258258
pub fn register(&self, waker: &Waker) {
259-
match self.state.compare_and_swap(WAITING, REGISTERING, Acquire) {
259+
match self
260+
.state
261+
.compare_exchange(WAITING, REGISTERING, Acquire, Acquire)
262+
.unwrap_or_else(|x| x)
263+
{
260264
WAITING => {
261265
unsafe {
262266
// Locked acquired, update the waker cell

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

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -204,7 +204,12 @@ where
204204

205205
inner.record_waker(&mut this.waker_key, cx);
206206

207-
match inner.notifier.state.compare_and_swap(IDLE, POLLING, SeqCst) {
207+
match inner
208+
.notifier
209+
.state
210+
.compare_exchange(IDLE, POLLING, SeqCst, SeqCst)
211+
.unwrap_or_else(|x| x)
212+
{
208213
IDLE => {
209214
// Lock acquired, fall through
210215
}
@@ -249,14 +254,14 @@ where
249254

250255
match future.poll(&mut cx) {
251256
Poll::Pending => {
252-
match inner.notifier.state.compare_and_swap(POLLING, IDLE, SeqCst) {
253-
POLLING => {
254-
// Success
255-
drop(_reset);
256-
this.inner = Some(inner);
257-
return Poll::Pending;
258-
}
259-
_ => unreachable!(),
257+
if inner.notifier.state.compare_exchange(POLLING, IDLE, SeqCst, SeqCst).is_ok()
258+
{
259+
// Success
260+
drop(_reset);
261+
this.inner = Some(inner);
262+
return Poll::Pending;
263+
} else {
264+
unreachable!()
260265
}
261266
}
262267
Poll::Ready(output) => output,

0 commit comments

Comments
 (0)