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
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
# Changelog

## 1.21.3

- Outline more initialization in `race`: [#284](https://github.com/matklad/once_cell/pull/284),
[#285](https://github.com/matklad/once_cell/pull/285).

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

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.2"
version = "1.21.3"
authors = ["Aleksey Kladov <aleksey.kladov@gmail.com>"]
license = "MIT OR Apache-2.0"
edition = "2021"
Expand Down
32 changes: 17 additions & 15 deletions src/race.rs
Original file line number Diff line number Diff line change
Expand Up @@ -266,10 +266,7 @@ impl<'a, T> OnceRef<'a, T> {
/// Returns `Ok(())` if the cell was empty and `Err(value)` if it was
/// full.
pub fn set(&self, value: &'a T) -> Result<(), ()> {
let ptr = <*const T>::cast_mut(value);
let exchange =
self.inner.compare_exchange(ptr::null_mut(), ptr, Ordering::Release, Ordering::Acquire);
match exchange {
match self.compare_exchange(value) {
Ok(_) => Ok(()),
Err(_) => Err(()),
}
Expand Down Expand Up @@ -312,19 +309,24 @@ impl<'a, T> OnceRef<'a, T> {
#[cold]
#[inline(never)]
fn init<E>(&self, f: impl FnOnce() -> Result<&'a T, E>) -> Result<&'a T, E> {
let value: &'a T = f()?;
let mut ptr = <*const T>::cast_mut(value);
let exchange = self.inner.compare_exchange(
ptr::null_mut(),
ptr,
Ordering::Release,
Ordering::Acquire,
);
if let Err(old) = exchange {
ptr = old;
let mut value: &'a T = f()?;
if let Err(old) = self.compare_exchange(value) {
value = unsafe { &*old };
}
Ok(value)
}

Ok(unsafe { &*ptr })
#[inline(always)]
fn compare_exchange(&self, value: &'a T) -> Result<(), *const T> {
self.inner
.compare_exchange(
ptr::null_mut(),
<*const T>::cast_mut(value),
Ordering::Release,
Ordering::Acquire,
)
.map(|_: *mut T| ())
.map_err(<*mut T>::cast_const)
}

/// ```compile_fail
Expand Down