Skip to content

Commit 52573f7

Browse files
authored
Merge pull request #7 from taiki-e/compare_and_swap
Replace deprecated compare_and_swap with compare_exchange
2 parents b5bf0b2 + cefe5ef commit 52573f7

File tree

3 files changed

+15
-8
lines changed

3 files changed

+15
-8
lines changed

src/lib.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -451,7 +451,7 @@ fn full_fence() {
451451
// a `SeqCst` fence.
452452
//
453453
// 1. `atomic::fence(SeqCst)`, which compiles into a `mfence` instruction.
454-
// 2. `_.compare_and_swap(_, _, SeqCst)`, which compiles into a `lock cmpxchg` instruction.
454+
// 2. `_.compare_exchange(_, _, SeqCst, SeqCst)`, which compiles into a `lock cmpxchg` instruction.
455455
//
456456
// Both instructions have the effect of a full barrier, but empirical benchmarks have shown
457457
// that the second one is sometimes a bit faster.
@@ -460,7 +460,7 @@ fn full_fence() {
460460
// temporary atomic variable and compare-and-exchanging its value. No sane compiler to
461461
// x86 platforms is going to optimize this away.
462462
let a = AtomicUsize::new(0);
463-
a.compare_and_swap(0, 1, Ordering::SeqCst);
463+
let _ = a.compare_exchange(0, 1, Ordering::SeqCst, Ordering::SeqCst);
464464
} else {
465465
atomic::fence(Ordering::SeqCst);
466466
}

src/single.rs

+11-4
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,8 @@ impl<T> Single<T> {
2929
// Lock and fill the slot.
3030
let state = self
3131
.state
32-
.compare_and_swap(0, LOCKED | PUSHED, Ordering::SeqCst);
32+
.compare_exchange(0, LOCKED | PUSHED, Ordering::SeqCst, Ordering::SeqCst)
33+
.unwrap_or_else(|x| x);
3334

3435
if state == 0 {
3536
// Write the value and unlock.
@@ -48,9 +49,15 @@ impl<T> Single<T> {
4849
let mut state = PUSHED;
4950
loop {
5051
// Lock and empty the slot.
51-
let prev =
52-
self.state
53-
.compare_and_swap(state, (state | LOCKED) & !PUSHED, Ordering::SeqCst);
52+
let prev = self
53+
.state
54+
.compare_exchange(
55+
state,
56+
(state | LOCKED) & !PUSHED,
57+
Ordering::SeqCst,
58+
Ordering::SeqCst,
59+
)
60+
.unwrap_or_else(|x| x);
5461

5562
if prev == state {
5663
// Read the value and unlock.

src/unbounded.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -172,8 +172,8 @@ impl<T> Unbounded<T> {
172172
if self
173173
.tail
174174
.block
175-
.compare_and_swap(block, new, Ordering::Release)
176-
== block
175+
.compare_exchange(block, new, Ordering::Release, Ordering::Relaxed)
176+
.is_ok()
177177
{
178178
self.head.block.store(new, Ordering::Release);
179179
block = new;

0 commit comments

Comments
 (0)