use core::{hint, sync::atomic::Ordering};
use crate::{
cell::{Block, Unblock},
state::{AtomicOnceState, BlockedState, OnceState::WouldBlock},
POISON_PANIC_MSG,
};
use self::internal::Spin;
pub type Lazy<T, F = fn() -> T> = crate::lazy::Lazy<T, Spin, F>;
pub type OnceCell<T> = crate::cell::OnceCell<T, Spin>;
pub type Once = OnceCell<()>;
mod internal {
#[derive(Copy, Clone, Debug, Eq, Ord, PartialEq, PartialOrd)]
pub struct Spin;
}
impl Unblock for Spin {
#[inline(always)]
unsafe fn on_unblock(_: BlockedState) {}
}
unsafe impl Block for Spin {
#[inline]
fn block(state: &AtomicOnceState) {
while let WouldBlock(_) = state.load(Ordering::Acquire).expect(POISON_PANIC_MSG) {
hint::spin_loop();
}
}
}
#[cfg(test)]
mod tests {
generate_tests_non_blocking!();
generate_tests!();
}