use core::{cell::UnsafeCell, mem::MaybeUninit};
#[cfg_attr(not(arm_llsc), path = "treiber/cas.rs")]
#[cfg_attr(arm_llsc, path = "treiber/llsc.rs")]
mod impl_;
pub use impl_::{AtomicPtr, NonNullPtr};
pub struct Stack<T> {
top: AtomicPtr<Node<T>>,
}
impl<T> Stack<T> {
pub const fn new() -> Self {
Self {
top: AtomicPtr::null(),
}
}
pub unsafe fn push(&self, node: NonNullPtr<Node<T>>) {
impl_::push(self, node);
}
pub fn try_pop(&self) -> Option<NonNullPtr<Node<T>>> {
impl_::try_pop(self)
}
}
pub struct Node<T> {
pub next: AtomicPtr<Self>,
pub data: UnsafeCell<MaybeUninit<T>>,
}
impl<T> Node<T> {
pub const fn unlinked() -> Self {
Self {
next: AtomicPtr::null(),
data: UnsafeCell::new(MaybeUninit::uninit()),
}
}
unsafe fn next(this: *const Self) -> *const AtomicPtr<Self> {
&raw const (*this).next
}
#[cfg_attr(not(arm_llsc), expect(unused))]
unsafe fn next_mut(this: *mut Self) -> *mut AtomicPtr<Self> {
&raw mut (*this).next
}
}
#[cfg(test)]
mod tests {
use core::mem;
use super::*;
#[test]
fn node_is_never_zero_sized() {
struct Zst;
assert_ne!(mem::size_of::<Node<Zst>>(), 0);
}
}