mmtk::util::rust_util::atomic_box

Struct OnceOptionBox

source
pub struct OnceOptionBox<T> {
    inner: AtomicPtr<T>,
}
Expand description

A lazily initialized box. Similar to an Option<Box<T>>, but can be initialized atomically.

It is designed for implementing shared data. Therefore, methods with &self, namely OnceOptionBox::get and the OnceOptionBox::get_or_init methods, only return shared references to the content (&T). The user should use types that support multi-threaded accesses, such as mutexes or atomic types, if the inner type is supposed to be modified concurrently.

Once initialized, this object will own its content. The content is allocated in the heap, and will be dropped and deallocated when this instance is dropped.

§Comparison to existing data structures

std::sync::OnceLock also provides thread-safe lazily-initialized cells. But as its name suggests, it uses locks for synchronization, whereas OnceOptionBox is lock-free. OnceLock also has a field of std::sync::Once which increases the space overhead. OnceOptionBox only has one atomic pointer field and is more suitable for large arrays of lazily initialized elements.

Fields§

§inner: AtomicPtr<T>

Implementations§

source§

impl<T> OnceOptionBox<T>

source

pub fn new() -> OnceOptionBox<T>

Create an empty OnceOptionBox instance.

source

pub fn get(&self, order: Ordering) -> Option<&T>

Get a reference to the content of this box, or None if not yet initialized.

source

pub fn get_or_init( &self, order_load: Ordering, order_store: Ordering, init: impl FnOnce() -> T, ) -> &T

Get a reference to the content of this box. If not initialized, it will call init to initialize this box.

When multiple threads attempt to initialize this box concurrently, all threads may call their supplied init closure, but only one thread will successfully initialize this box to the return value of init. Other threads will drop their return values of init. All callers will return the reference to the value created by the successful thread.

Trait Implementations§

source§

impl<T> Drop for OnceOptionBox<T>

source§

fn drop(&mut self)

Executes the destructor for this type. Read more
source§

impl<T> Zeroable for OnceOptionBox<T>

§

fn zeroed() -> Self

Auto Trait Implementations§

§

impl<T> !Freeze for OnceOptionBox<T>

§

impl<T> RefUnwindSafe for OnceOptionBox<T>

§

impl<T> Send for OnceOptionBox<T>

§

impl<T> Sync for OnceOptionBox<T>

§

impl<T> Unpin for OnceOptionBox<T>

§

impl<T> UnwindSafe for OnceOptionBox<T>
where T: RefUnwindSafe,

Blanket Implementations§

source§

impl<T> Any for T
where T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

impl<T> Borrow<T> for T
where T: ?Sized,

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
§

impl<T> Downcast for T
where T: Any,

§

fn into_any(self: Box<T>) -> Box<dyn Any>

Converts Box<dyn Trait> (where Trait: Downcast) to Box<dyn Any>, which can then be downcast into Box<dyn ConcreteType> where ConcreteType implements Trait.
§

fn into_any_rc(self: Rc<T>) -> Rc<dyn Any>

Converts Rc<Trait> (where Trait: Downcast) to Rc<Any>, which can then be further downcast into Rc<ConcreteType> where ConcreteType implements Trait.
§

fn as_any(&self) -> &(dyn Any + 'static)

Converts &Trait (where Trait: Downcast) to &Any. This is needed since Rust cannot generate &Any’s vtable from &Trait’s.
§

fn as_any_mut(&mut self) -> &mut (dyn Any + 'static)

Converts &mut Trait (where Trait: Downcast) to &Any. This is needed since Rust cannot generate &mut Any’s vtable from &mut Trait’s.
§

impl<T> DowncastSend for T
where T: Any + Send,

§

fn into_any_send(self: Box<T>) -> Box<dyn Any + Send>

Converts Box<Trait> (where Trait: DowncastSend) to Box<dyn Any + Send>, which can then be downcast into Box<ConcreteType> where ConcreteType implements Trait.
§

impl<T> DowncastSync for T
where T: Any + Send + Sync,

§

fn into_any_sync(self: Box<T>) -> Box<dyn Any + Sync + Send>

Converts Box<Trait> (where Trait: DowncastSync) to Box<dyn Any + Send + Sync>, which can then be downcast into Box<ConcreteType> where ConcreteType implements Trait.
§

fn into_any_arc(self: Arc<T>) -> Arc<dyn Any + Sync + Send>

Converts Arc<Trait> (where Trait: DowncastSync) to Arc<Any>, which can then be downcast into Arc<ConcreteType> where ConcreteType implements Trait.
source§

impl<T> From<T> for T

source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

impl<T, U> Into<U> for T
where U: From<T>,

source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

source§

impl<T> IntoEither for T

source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts self into a Left variant of Either<Self, Self> if into_left is true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts self into a Left variant of Either<Self, Self> if into_left(&self) returns true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
§

impl<T> Pointable for T

§

const ALIGN: usize = _

The alignment of pointer.
§

type Init = T

The type for initializers.
§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. Read more
source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

source§

type Error = Infallible

The type returned in the event of a conversion error.
source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.