Skip to content

Commit

Permalink
AcqRel -> Release for compare_exchange success ordering in race module
Browse files Browse the repository at this point in the history
Since the load during success for compare exchange is `0`/`null` and no
`Release` store of this value exists, using an `Aquire` load for this
case adds no additional synchronization.

Fixes matklad#220
  • Loading branch information
Imberflur committed Feb 14, 2023
1 parent 78ab172 commit 70216b4
Showing 1 changed file with 6 additions and 6 deletions.
12 changes: 6 additions & 6 deletions src/race.rs
Expand Up @@ -57,7 +57,7 @@ impl OnceNonZeroUsize {
#[inline]
pub fn set(&self, value: NonZeroUsize) -> Result<(), ()> {
let exchange =
self.inner.compare_exchange(0, value.get(), Ordering::AcqRel, Ordering::Acquire);
self.inner.compare_exchange(0, value.get(), Ordering::Release, Ordering::Acquire);
match exchange {
Ok(_) => Ok(()),
Err(_) => Err(()),
Expand Down Expand Up @@ -98,7 +98,7 @@ impl OnceNonZeroUsize {
None => {
let mut val = f()?.get();
let exchange =
self.inner.compare_exchange(0, val, Ordering::AcqRel, Ordering::Acquire);
self.inner.compare_exchange(0, val, Ordering::Release, Ordering::Acquire);
if let Err(old) = exchange {
val = old;
}
Expand Down Expand Up @@ -215,7 +215,7 @@ impl<'a, T> OnceRef<'a, T> {
pub fn set(&self, value: &'a T) -> Result<(), ()> {
let ptr = value as *const T as *mut T;
let exchange =
self.inner.compare_exchange(ptr::null_mut(), ptr, Ordering::AcqRel, Ordering::Acquire);
self.inner.compare_exchange(ptr::null_mut(), ptr, Ordering::Release, Ordering::Acquire);
match exchange {
Ok(_) => Ok(()),
Err(_) => Err(()),
Expand Down Expand Up @@ -258,7 +258,7 @@ impl<'a, T> OnceRef<'a, T> {
let exchange = self.inner.compare_exchange(
ptr::null_mut(),
ptr,
Ordering::AcqRel,
Ordering::Release,
Ordering::Acquire,
);
if let Err(old) = exchange {
Expand Down Expand Up @@ -348,7 +348,7 @@ mod once_box {
let exchange = self.inner.compare_exchange(
ptr::null_mut(),
ptr,
Ordering::AcqRel,
Ordering::Release,
Ordering::Acquire,
);
if let Err(_) = exchange {
Expand Down Expand Up @@ -394,7 +394,7 @@ mod once_box {
let exchange = self.inner.compare_exchange(
ptr::null_mut(),
ptr,
Ordering::AcqRel,
Ordering::Release,
Ordering::Acquire,
);
if let Err(old) = exchange {
Expand Down

0 comments on commit 70216b4

Please sign in to comment.