about summary refs log tree commit diff
path: root/library/std/src/sys_common/lazy_box.rs
diff options
context:
space:
mode:
authorbors <bors@rust-lang.org>2024-10-05 11:28:48 +0000
committerbors <bors@rust-lang.org>2024-10-05 11:28:48 +0000
commitf559d6188828b738ce7e7c2e4d99bf03111336d6 (patch)
tree7e80557eaf790511cc548e5c4a5212ad02aeb9c7 /library/std/src/sys_common/lazy_box.rs
parent5a4ee43c387110736440cecc375cb5aedc441dbc (diff)
parent72acacf60f4331f7229cd82f16c946d44c51f743 (diff)
downloadrust-f559d6188828b738ce7e7c2e4d99bf03111336d6.tar.gz
rust-f559d6188828b738ce7e7c2e4d99bf03111336d6.zip
Auto merge of #131288 - matthiaskrgr:rollup-h0t0v2h, r=matthiaskrgr
Rollup of 5 pull requests

Successful merges:

 - #130428 (Stabilize `const_slice_split_at_mut` and `const_slice_first_last_chunk`)
 - #131094 (std: replace `LazyBox` with `OnceBox`)
 - #131256 (move f16/f128 const fn under f16/f128 feature gate)
 - #131278 (remove outdated contribution direction)
 - #131286 (Miri subtree update)

r? `@ghost`
`@rustbot` modify labels: rollup
Diffstat (limited to 'library/std/src/sys_common/lazy_box.rs')
-rw-r--r--library/std/src/sys_common/lazy_box.rs88
1 files changed, 0 insertions, 88 deletions
diff --git a/library/std/src/sys_common/lazy_box.rs b/library/std/src/sys_common/lazy_box.rs
deleted file mode 100644
index b45b05f63ba..00000000000
--- a/library/std/src/sys_common/lazy_box.rs
+++ /dev/null
@@ -1,88 +0,0 @@
-#![allow(dead_code)] // Only used on some platforms.
-
-// This is used to wrap pthread {Mutex, Condvar, RwLock} in.
-
-use crate::marker::PhantomData;
-use crate::ops::{Deref, DerefMut};
-use crate::ptr::null_mut;
-use crate::sync::atomic::AtomicPtr;
-use crate::sync::atomic::Ordering::{AcqRel, Acquire};
-
-pub(crate) struct LazyBox<T: LazyInit> {
-    ptr: AtomicPtr<T>,
-    _phantom: PhantomData<T>,
-}
-
-pub(crate) trait LazyInit {
-    /// This is called before the box is allocated, to provide the value to
-    /// move into the new box.
-    ///
-    /// It might be called more than once per LazyBox, as multiple threads
-    /// might race to initialize it concurrently, each constructing and initializing
-    /// their own box. All but one of them will be passed to `cancel_init` right after.
-    fn init() -> Box<Self>;
-
-    /// Any surplus boxes from `init()` that lost the initialization race
-    /// are passed to this function for disposal.
-    ///
-    /// The default implementation calls destroy().
-    fn cancel_init(x: Box<Self>) {
-        Self::destroy(x);
-    }
-
-    /// This is called to destroy a used box.
-    ///
-    /// The default implementation just drops it.
-    fn destroy(_: Box<Self>) {}
-}
-
-impl<T: LazyInit> LazyBox<T> {
-    #[inline]
-    pub const fn new() -> Self {
-        Self { ptr: AtomicPtr::new(null_mut()), _phantom: PhantomData }
-    }
-
-    #[inline]
-    fn get_pointer(&self) -> *mut T {
-        let ptr = self.ptr.load(Acquire);
-        if ptr.is_null() { self.initialize() } else { ptr }
-    }
-
-    #[cold]
-    fn initialize(&self) -> *mut T {
-        let new_ptr = Box::into_raw(T::init());
-        match self.ptr.compare_exchange(null_mut(), new_ptr, AcqRel, Acquire) {
-            Ok(_) => new_ptr,
-            Err(ptr) => {
-                // Lost the race to another thread.
-                // Drop the box we created, and use the one from the other thread instead.
-                T::cancel_init(unsafe { Box::from_raw(new_ptr) });
-                ptr
-            }
-        }
-    }
-}
-
-impl<T: LazyInit> Deref for LazyBox<T> {
-    type Target = T;
-    #[inline]
-    fn deref(&self) -> &T {
-        unsafe { &*self.get_pointer() }
-    }
-}
-
-impl<T: LazyInit> DerefMut for LazyBox<T> {
-    #[inline]
-    fn deref_mut(&mut self) -> &mut T {
-        unsafe { &mut *self.get_pointer() }
-    }
-}
-
-impl<T: LazyInit> Drop for LazyBox<T> {
-    fn drop(&mut self) {
-        let ptr = *self.ptr.get_mut();
-        if !ptr.is_null() {
-            T::destroy(unsafe { Box::from_raw(ptr) });
-        }
-    }
-}