diff options
| author | Urgau <3616612+Urgau@users.noreply.github.com> | 2024-10-16 12:03:42 +0200 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2024-10-16 12:03:42 +0200 |
| commit | 66dc09f3dacce171389b0861a320aaeb6f95f659 (patch) | |
| tree | 3726fe0ef8d8b83c79d9526923e8c10d426fdde8 /library/std/src | |
| parent | f7af3aa7dcef25ed66cc3b290df6e52dc4a3595d (diff) | |
| parent | 937d13b8efc43f3f2e93f81360565d82f243ce15 (diff) | |
Rollup merge of #131746 - slanterns:once_box_order, r=joboet
Relax a memory order in `once_box` per https://github.com/rust-lang/rust/pull/131094#discussion_r1788536445. 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`
Diffstat (limited to 'library/std/src')
| -rw-r--r-- | library/std/src/sys/sync/once_box.rs | 4 |
1 files changed, 2 insertions, 2 deletions
diff --git a/library/std/src/sys/sync/once_box.rs b/library/std/src/sys/sync/once_box.rs index 1422b5a1721..9d24db2245a 100644 --- a/library/std/src/sys/sync/once_box.rs +++ b/library/std/src/sys/sync/once_box.rs @@ -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>, @@ -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. |
