use core::{
cell::UnsafeCell,
ptr::{self, NonNull},
};
use super::{Node, Stack};
pub struct AtomicPtr<N> {
inner: UnsafeCell<Option<NonNull<N>>>,
}
impl<N> AtomicPtr<N> {
#[inline]
pub const fn null() -> Self {
Self {
inner: UnsafeCell::new(None),
}
}
}
pub struct NonNullPtr<N> {
inner: NonNull<N>,
}
impl<N> NonNullPtr<N> {
#[inline]
pub fn as_ptr(&self) -> *mut N {
self.inner.as_ptr().cast()
}
#[inline]
pub fn from_static_mut_ref(ref_: &'static mut N) -> Self {
Self {
inner: NonNull::from(ref_),
}
}
#[inline]
pub unsafe fn from_ptr_unchecked(ptr: *mut N) -> Self {
debug_assert!(!ptr.is_null(), "Pointer must be non-null");
Self {
inner: NonNull::new_unchecked(ptr),
}
}
}
impl<N> Clone for NonNullPtr<N> {
fn clone(&self) -> Self {
Self { inner: self.inner }
}
}
impl<N> Copy for NonNullPtr<N> {}
pub unsafe fn push<T>(stack: &Stack<T>, mut node: NonNullPtr<Node<T>>) {
let top_addr = ptr::addr_of!(stack.top) as *mut usize;
loop {
let top = arch::load_link(top_addr);
(&mut *Node::next_mut(node.inner.as_mut()))
.inner
.get()
.write(NonNull::new(top as *mut _));
if arch::store_conditional(node.inner.as_ptr() as usize, top_addr).is_ok() {
break;
}
}
}
pub fn try_pop<T>(stack: &Stack<T>) -> Option<NonNullPtr<Node<T>>> {
unsafe {
let top_addr = ptr::addr_of!(stack.top) as *mut usize;
loop {
let top = arch::load_link(top_addr);
if let Some(top) = NonNull::new(top as *mut Node<T>) {
let next = &*Node::next(top.as_ptr());
if arch::store_conditional(
next.inner
.get()
.read()
.map(|non_null| non_null.as_ptr() as usize)
.unwrap_or_default(),
top_addr,
)
.is_ok()
{
break Some(NonNullPtr { inner: top });
}
} else {
arch::clear_load_link();
break None;
}
}
}
}
#[cfg(arm_llsc)]
mod arch {
use core::arch::asm;
#[inline(always)]
pub fn clear_load_link() {
unsafe { asm!("clrex", options(nomem, nostack)) }
}
#[inline(always)]
pub unsafe fn load_link(addr: *const usize) -> usize {
let value;
asm!("ldrex {}, [{}]", out(reg) value, in(reg) addr, options(nostack));
value
}
#[inline(always)]
pub unsafe fn store_conditional(value: usize, addr: *mut usize) -> Result<(), ()> {
let outcome: usize;
asm!("strex {}, {}, [{}]", out(reg) outcome, in(reg) value, in(reg) addr, options(nostack));
if outcome == 0 {
Ok(())
} else {
Err(())
}
}
}