Skip to content

Commit 0fe6e3d

Browse files
bors[bot]taiki-e
andauthored
Merge #617
617: Replace deprecated compare_and_swap with compare_exchange r=jeehoonkang a=taiki-e `compare_and_swap` is deprecated in 1.50. (rust-lang/rust#79261) This patch replaces the uses of `compare_and_swap` with `compare_exchange`. See also the document about `compare_and_swap` -> `compare_exchange(_weak)` migration: https://doc.rust-lang.org/nightly/core/sync/atomic/struct.AtomicUsize.html#migrating-to-compare_exchange-and-compare_exchange_weak Co-authored-by: Taiki Endo <[email protected]>
2 parents dd8c9fc + 8b64014 commit 0fe6e3d

File tree

5 files changed

+42
-18
lines changed

5 files changed

+42
-18
lines changed

crossbeam-channel/src/flavors/list.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -231,8 +231,8 @@ impl<T> Channel<T> {
231231
if self
232232
.tail
233233
.block
234-
.compare_and_swap(block, new, Ordering::Release)
235-
== block
234+
.compare_exchange(block, new, Ordering::Release, Ordering::Relaxed)
235+
.is_ok()
236236
{
237237
self.head.block.store(new, Ordering::Release);
238238
block = new;

crossbeam-epoch/src/epoch.rs

Lines changed: 24 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -102,13 +102,31 @@ impl AtomicEpoch {
102102

103103
/// Stores a value into the atomic epoch if the current value is the same as `current`.
104104
///
105-
/// The return value is always the previous value. If it is equal to `current`, then the value
106-
/// is updated.
105+
/// The return value is a result indicating whether the new value was written and containing
106+
/// the previous value. On success this value is guaranteed to be equal to `current`.
107107
///
108-
/// The `Ordering` argument describes the memory ordering of this operation.
108+
/// `compare_exchange` takes two `Ordering` arguments to describe the memory
109+
/// ordering of this operation. `success` describes the required ordering for the
110+
/// read-modify-write operation that takes place if the comparison with `current` succeeds.
111+
/// `failure` describes the required ordering for the load operation that takes place when
112+
/// the comparison fails. Using `Acquire` as success ordering makes the store part
113+
/// of this operation `Relaxed`, and using `Release` makes the successful load
114+
/// `Relaxed`. The failure ordering can only be `SeqCst`, `Acquire` or `Relaxed`
115+
/// and must be equivalent to or weaker than the success ordering.
109116
#[inline]
110-
pub fn compare_and_swap(&self, current: Epoch, new: Epoch, ord: Ordering) -> Epoch {
111-
let data = self.data.compare_and_swap(current.data, new.data, ord);
112-
Epoch { data }
117+
pub fn compare_exchange(
118+
&self,
119+
current: Epoch,
120+
new: Epoch,
121+
success: Ordering,
122+
failure: Ordering,
123+
) -> Result<Epoch, Epoch> {
124+
match self
125+
.data
126+
.compare_exchange(current.data, new.data, success, failure)
127+
{
128+
Ok(data) => Ok(Epoch { data }),
129+
Err(data) => Err(Epoch { data }),
130+
}
113131
}
114132
}

crossbeam-epoch/src/internal.rs

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -376,7 +376,10 @@ pub struct Local {
376376
// https://github.com/crossbeam-rs/crossbeam/issues/551
377377
#[test]
378378
fn local_size() {
379-
assert!(core::mem::size_of::<Local>() <= 2048, "An allocation of `Local` should be <= 2048 bytes.");
379+
assert!(
380+
core::mem::size_of::<Local>() <= 2048,
381+
"An allocation of `Local` should be <= 2048 bytes."
382+
);
380383
}
381384

382385
impl Local {
@@ -468,7 +471,7 @@ impl Local {
468471
// a `SeqCst` fence.
469472
//
470473
// 1. `atomic::fence(SeqCst)`, which compiles into a `mfence` instruction.
471-
// 2. `_.compare_and_swap(_, _, SeqCst)`, which compiles into a `lock cmpxchg`
474+
// 2. `_.compare_exchange(_, _, SeqCst, SeqCst)`, which compiles into a `lock cmpxchg`
472475
// instruction.
473476
//
474477
// Both instructions have the effect of a full barrier, but benchmarks have shown
@@ -478,10 +481,13 @@ impl Local {
478481
// works fine. Using inline assembly would be a viable (and correct) alternative,
479482
// but alas, that is not possible on stable Rust.
480483
let current = Epoch::starting();
481-
let previous = self
482-
.epoch
483-
.compare_and_swap(current, new_epoch, Ordering::SeqCst);
484-
debug_assert_eq!(current, previous, "participant was expected to be unpinned");
484+
let res = self.epoch.compare_exchange(
485+
current,
486+
new_epoch,
487+
Ordering::SeqCst,
488+
Ordering::SeqCst,
489+
);
490+
debug_assert!(res.is_ok(), "participant was expected to be unpinned");
485491
// We add a compiler fence to make it less likely for LLVM to do something wrong
486492
// here. Formally, this is not enough to get rid of data races; practically,
487493
// it should go a long way.

crossbeam-queue/src/seg_queue.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -213,8 +213,8 @@ impl<T> SegQueue<T> {
213213
if self
214214
.tail
215215
.block
216-
.compare_and_swap(block, new, Ordering::Release)
217-
== block
216+
.compare_exchange(block, new, Ordering::Release, Ordering::Relaxed)
217+
.is_ok()
218218
{
219219
self.head.block.store(new, Ordering::Release);
220220
block = new;

crossbeam-utils/src/backoff.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ const YIELD_LIMIT: u32 = 10;
2727
/// let backoff = Backoff::new();
2828
/// loop {
2929
/// let val = a.load(SeqCst);
30-
/// if a.compare_and_swap(val, val.wrapping_mul(b), SeqCst) == val {
30+
/// if a.compare_exchange(val, val.wrapping_mul(b), SeqCst, SeqCst).is_ok() {
3131
/// return val;
3232
/// }
3333
/// backoff.spin();
@@ -131,7 +131,7 @@ impl Backoff {
131131
/// let backoff = Backoff::new();
132132
/// loop {
133133
/// let val = a.load(SeqCst);
134-
/// if a.compare_and_swap(val, val.wrapping_mul(b), SeqCst) == val {
134+
/// if a.compare_exchange(val, val.wrapping_mul(b), SeqCst, SeqCst).is_ok() {
135135
/// return val;
136136
/// }
137137
/// backoff.spin();

0 commit comments

Comments
 (0)