Skip to content

Commit

Permalink
Merge pull request #18 from taiki-e/compare_and_swap
Browse files Browse the repository at this point in the history
Replace deprecated compare_and_swap with compare_exchange
  • Loading branch information
taiki-e authored Dec 24, 2020
2 parents a652d3b + d4aa030 commit a0c600a
Showing 1 changed file with 6 additions and 3 deletions.
9 changes: 6 additions & 3 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -392,7 +392,10 @@ impl Event {
let new = Arc::into_raw(new) as *mut Inner;

// Attempt to replace the null-pointer with the new state pointer.
inner = self.inner.compare_and_swap(inner, new, Ordering::AcqRel);
inner = self
.inner
.compare_exchange(inner, new, Ordering::AcqRel, Ordering::Acquire)
.unwrap_or_else(|x| x);

// Check if the old pointer value was indeed null.
if inner.is_null() {
Expand Down Expand Up @@ -970,7 +973,7 @@ fn full_fence() {
// a `SeqCst` fence.
//
// 1. `atomic::fence(SeqCst)`, which compiles into a `mfence` instruction.
// 2. `_.compare_and_swap(_, _, SeqCst)`, which compiles into a `lock cmpxchg` instruction.
// 2. `_.compare_exchange(_, _, SeqCst, SeqCst)`, which compiles into a `lock cmpxchg` instruction.
//
// Both instructions have the effect of a full barrier, but empirical benchmarks have shown
// that the second one is sometimes a bit faster.
Expand All @@ -979,7 +982,7 @@ fn full_fence() {
// temporary atomic variable and compare-and-exchanging its value. No sane compiler to
// x86 platforms is going to optimize this away.
let a = AtomicUsize::new(0);
a.compare_and_swap(0, 1, Ordering::SeqCst);
let _ = a.compare_exchange(0, 1, Ordering::SeqCst, Ordering::SeqCst);
} else {
atomic::fence(Ordering::SeqCst);
}
Expand Down

0 comments on commit a0c600a

Please sign in to comment.