relax a memory order in once_box

This commit is contained in:
Slanterns 2024-10-16 00:22:05 +08:00
parent f79fae3069
commit 937d13b8ef
No known key found for this signature in database
GPG Key ID: 1AA37EE7FAAE433A

View File

@ -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 @@ pub fn take(&mut self) -> Option<Box<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.