Skip to content

Commit cdbd127

Browse files
authored
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`
2 parents 6f3e65c + e35d9fe commit cdbd127

File tree

1 file changed

+2
-2
lines changed

1 file changed

+2
-2
lines changed

std/src/sys/sync/once_box.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
use crate::mem::replace;
99
use crate::ptr::null_mut;
1010
use crate::sync::atomic::AtomicPtr;
11-
use crate::sync::atomic::Ordering::{AcqRel, Acquire, Relaxed};
11+
use crate::sync::atomic::Ordering::{Acquire, Relaxed, Release};
1212

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

0 commit comments

Comments
 (0)