Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
# Changelog

## 1.21.2
- Relax success ordering from AcqRel to Release in `race`: [#278](https://github.com/matklad/once_cell/pull/278).

## 1.21.1
- Reduce MSRV to 1.65: [#277](https://github.com/matklad/once_cell/pull/277).

Expand Down
2 changes: 1 addition & 1 deletion Cargo.lock.msrv

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "once_cell"
version = "1.21.1"
version = "1.21.2"
authors = ["Aleksey Kladov <aleksey.kladov@gmail.com>"]
license = "MIT OR Apache-2.0"
edition = "2021"
Expand Down
20 changes: 14 additions & 6 deletions src/race.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,14 @@
//! `Acquire` and `Release` have very little performance overhead on most
//! architectures versus `Relaxed`.

// The "atomic orderings" section of the documentation above promises
// "happens-before" semantics. This drives the choice of orderings in the uses
// of `compare_exchange` below. On success, the value was zero/null, so there
// was nothing to acquire (there is never any `Ordering::Release` store of 0).
// On failure, the value was nonzero, so it was initialized previously (perhaps
// on another thread) using `Ordering::Release`, so we must use
// `Ordering::Acquire` to ensure that store "happens-before" this load.

#[cfg(not(feature = "portable-atomic"))]
use core::sync::atomic;
#[cfg(feature = "portable-atomic")]
Expand Down Expand Up @@ -98,7 +106,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 @@ -144,7 +152,7 @@ impl OnceNonZeroUsize {
#[inline(never)]
fn init<E>(&self, f: impl FnOnce() -> Result<NonZeroUsize, E>) -> Result<NonZeroUsize, E> {
let mut val = f()?.get();
let exchange = self.inner.compare_exchange(0, val, Ordering::AcqRel, Ordering::Acquire);
let exchange = self.inner.compare_exchange(0, val, Ordering::Release, Ordering::Acquire);
if let Err(old) = exchange {
val = old;
}
Expand Down Expand Up @@ -258,7 +266,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 @@ -301,7 +309,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 @@ -396,7 +404,7 @@ mod once_box {
let exchange = self.inner.compare_exchange(
ptr::null_mut(),
ptr,
Ordering::AcqRel,
Ordering::Release,
Ordering::Acquire,
);
if exchange.is_err() {
Expand Down Expand Up @@ -442,7 +450,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