Skip to content

Commit

Permalink
Unrolled build for rust-lang#131746
Browse files Browse the repository at this point in the history
Rollup merge of rust-lang#131746 - slanterns:once_box_order, r=joboet

Relax a memory order in `once_box`

per rust-lang#131094 (comment).

In the successful path we don't need `Acquire` since we don't care if the store in `f()` happened in other threads has become visible to the current thread. We'll use our own results instead and just using `Release` to ensure other threads can see our store to `Box` when they fail the `compare_exchange` will suffice.

Also took https://marabos.nl/atomics/memory-ordering.html#example-lazy-initialization-with-indirection as a reference.

`@rustbot` label: +T-libs

r? `@ibraheemdev`
  • Loading branch information
rust-timer authored Oct 16, 2024
2 parents d829780 + 937d13b commit 2d4415d
Showing 1 changed file with 2 additions and 2 deletions.
4 changes: 2 additions & 2 deletions library/std/src/sys/sync/once_box.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
use crate::mem::replace;
use crate::ptr::null_mut;
use crate::sync::atomic::AtomicPtr;
use crate::sync::atomic::Ordering::{AcqRel, Acquire, Relaxed};
use crate::sync::atomic::Ordering::{Acquire, Relaxed, Release};

pub(crate) struct OnceBox<T> {
ptr: AtomicPtr<T>,
Expand Down Expand Up @@ -60,7 +60,7 @@ impl<T> OnceBox<T> {
#[cold]
fn initialize(&self, f: impl FnOnce() -> Box<T>) -> &T {
let new_ptr = Box::into_raw(f());
match self.ptr.compare_exchange(null_mut(), new_ptr, AcqRel, Acquire) {
match self.ptr.compare_exchange(null_mut(), new_ptr, Release, Acquire) {
Ok(_) => unsafe { &*new_ptr },
Err(ptr) => {
// Lost the race to another thread.
Expand Down

0 comments on commit 2d4415d

Please sign in to comment.