use critical_section::{CriticalSection, Mutex}; use portable_atomic::{AtomicBool, Ordering};
usecrate::unsync;
pub(crate) struct OnceCell<T> {
initialized: AtomicBool, // Use `unsync::OnceCell` internally since `Mutex` does not provide // interior mutability and to be able to re-use `get_or_try_init`.
value: Mutex<unsync::OnceCell<T>>,
}
// Why do we need `T: Send`? // Thread A creates a `OnceCell` and shares it with // scoped thread B, which fills the cell, which is // then destroyed by A. That is, destructor observes // a sent value. unsafeimpl<T: Sync + Send> Sync for OnceCell<T> {} unsafeimpl<T: Send> Send for OnceCell<T> {}
impl<T: RefUnwindSafe + UnwindSafe> RefUnwindSafe for OnceCell<T> {} impl<T: UnwindSafe> UnwindSafe for OnceCell<T> {}
/// Get the reference to the underlying value, without checking if the cell /// is initialized. /// /// # Safety /// /// Caller must ensure that the cell is in initialized state, and that /// the contents are acquired by (synchronized to) this thread. pub(crate) unsafefn get_unchecked(&self) -> &T {
debug_assert!(self.is_initialized()); // SAFETY: The caller ensures that the value is initialized and access synchronized. self.value.borrow(CriticalSection::new()).get().unwrap_unchecked()
}
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.