| 1 |
use core::{ |
| 2 |
marker::PhantomData, |
| 3 |
num::{NonZeroU32, NonZeroU64}, |
| 4 |
ptr::NonNull, |
| 5 |
sync::atomic::{AtomicU64, Ordering}, |
| 6 |
}; |
| 7 |
|
| 8 |
use super::{Node, Stack}; |
| 9 |
|
| 10 |
pub struct AtomicPtr<N> |
| 11 |
where |
| 12 |
N: Node, |
| 13 |
{ |
| 14 |
inner: AtomicU64, |
| 15 |
_marker: PhantomData<*mut N>, |
| 16 |
} |
| 17 |
|
| 18 |
impl<N> AtomicPtr<N> |
| 19 |
where |
| 20 |
N: Node, |
| 21 |
{ |
| 22 |
pub const fn null() -> Self { |
| 23 |
Self { |
| 24 |
inner: AtomicU64::new(0), |
| 25 |
_marker: PhantomData, |
| 26 |
} |
| 27 |
} |
| 28 |
|
| 29 |
fn compare_and_exchange_weak( |
| 30 |
&self, |
| 31 |
current: Option<NonNullPtr<N>>, |
| 32 |
new: Option<NonNullPtr<N>>, |
| 33 |
success: Ordering, |
| 34 |
failure: Ordering, |
| 35 |
) -> Result<(), Option<NonNullPtr<N>>> { |
| 36 |
self.inner |
| 37 |
.compare_exchange_weak( |
| 38 |
current |
| 39 |
.map(|pointer| pointer.into_u64()) |
| 40 |
.unwrap_or_default(), |
| 41 |
new.map(|pointer| pointer.into_u64()).unwrap_or_default(), |
| 42 |
success, |
| 43 |
failure, |
| 44 |
) |
| 45 |
.map(drop) |
| 46 |
.map_err(NonNullPtr::from_u64) |
| 47 |
} |
| 48 |
|
| 49 |
fn load(&self, order: Ordering) -> Option<NonNullPtr<N>> { |
| 50 |
NonZeroU64::new(self.inner.load(order)).map(|inner| NonNullPtr { |
| 51 |
inner, |
| 52 |
_marker: PhantomData, |
| 53 |
}) |
| 54 |
} |
| 55 |
|
| 56 |
fn store(&self, value: Option<NonNullPtr<N>>, order: Ordering) { |
| 57 |
self.inner.store( |
| 58 |
value.map(|pointer| pointer.into_u64()).unwrap_or_default(), |
| 59 |
order, |
| 60 |
) |
| 61 |
} |
| 62 |
} |
| 63 |
|
| 64 |
pub struct NonNullPtr<N> |
| 65 |
where |
| 66 |
N: Node, |
| 67 |
{ |
| 68 |
inner: NonZeroU64, |
| 69 |
_marker: PhantomData<*mut N>, |
| 70 |
} |
| 71 |
|
| 72 |
impl<N> Clone for NonNullPtr<N> |
| 73 |
where |
| 74 |
N: Node, |
| 75 |
{ |
| 76 |
fn clone(&self) -> Self { |
| 77 |
*self |
| 78 |
} |
| 79 |
} |
| 80 |
|
| 81 |
impl<N> Copy for NonNullPtr<N> where N: Node {} |
| 82 |
|
| 83 |
impl<N> NonNullPtr<N> |
| 84 |
where |
| 85 |
N: Node, |
| 86 |
{ |
| 87 |
pub fn as_ptr(&self) -> *mut N { |
| 88 |
self.inner.get() as *mut N |
| 89 |
} |
| 90 |
|
| 91 |
pub fn from_static_mut_ref(ref_: &'static mut N) -> NonNullPtr<N> { |
| 92 |
let non_null = NonNull::from(ref_); |
| 93 |
Self::from_non_null(non_null) |
| 94 |
} |
| 95 |
|
| 96 |
fn from_non_null(ptr: NonNull<N>) -> Self { |
| 97 |
let address = ptr.as_ptr() as u32; |
| 98 |
let tag = initial_tag().get(); |
| 99 |
|
| 100 |
let value = (u64::from(tag) << 32) | u64::from(address); |
| 101 |
|
| 102 |
Self { |
| 103 |
inner: unsafe { NonZeroU64::new_unchecked(value) }, |
| 104 |
_marker: PhantomData, |
| 105 |
} |
| 106 |
} |
| 107 |
|
| 108 |
fn from_u64(value: u64) -> Option<Self> { |
| 109 |
NonZeroU64::new(value).map(|inner| Self { |
| 110 |
inner, |
| 111 |
_marker: PhantomData, |
| 112 |
}) |
| 113 |
} |
| 114 |
|
| 115 |
fn non_null(&self) -> NonNull<N> { |
| 116 |
unsafe { NonNull::new_unchecked(self.inner.get() as *mut N) } |
| 117 |
} |
| 118 |
|
| 119 |
fn tag(&self) -> NonZeroU32 { |
| 120 |
unsafe { NonZeroU32::new_unchecked((self.inner.get() >> 32) as u32) } |
| 121 |
} |
| 122 |
|
| 123 |
fn into_u64(self) -> u64 { |
| 124 |
self.inner.get() |
| 125 |
} |
| 126 |
|
| 127 |
fn increase_tag(&mut self) { |
| 128 |
let address = self.as_ptr() as u32; |
| 129 |
|
| 130 |
let new_tag = self |
| 131 |
.tag() |
| 132 |
.get() |
| 133 |
.checked_add(1) |
| 134 |
.map(|val| unsafe { NonZeroU32::new_unchecked(val) }) |
| 135 |
.unwrap_or_else(initial_tag) |
| 136 |
.get(); |
| 137 |
|
| 138 |
let value = (u64::from(new_tag) << 32) | u64::from(address); |
| 139 |
|
| 140 |
self.inner = unsafe { NonZeroU64::new_unchecked(value) }; |
| 141 |
} |
| 142 |
} |
| 143 |
|
| 144 |
fn initial_tag() -> NonZeroU32 { |
| 145 |
unsafe { NonZeroU32::new_unchecked(1) } |
| 146 |
} |
| 147 |
|
| 148 |
pub unsafe fn push<N>(stack: &Stack<N>, new_top: NonNullPtr<N>) |
| 149 |
where |
| 150 |
N: Node, |
| 151 |
{ |
| 152 |
let mut top = stack.top.load(Ordering::Relaxed); |
| 153 |
|
| 154 |
loop { |
| 155 |
new_top |
| 156 |
.non_null() |
| 157 |
.as_ref() |
| 158 |
.next() |
| 159 |
.store(top, Ordering::Relaxed); |
| 160 |
|
| 161 |
if let Err(p) = stack.top.compare_and_exchange_weak( |
| 162 |
top, |
| 163 |
Some(new_top), |
| 164 |
Ordering::Release, |
| 165 |
Ordering::Relaxed, |
| 166 |
) { |
| 167 |
top = p; |
| 168 |
} else { |
| 169 |
return; |
| 170 |
} |
| 171 |
} |
| 172 |
} |
| 173 |
|
| 174 |
pub fn try_pop<N>(stack: &Stack<N>) -> Option<NonNullPtr<N>> |
| 175 |
where |
| 176 |
N: Node, |
| 177 |
{ |
| 178 |
loop { |
| 179 |
if let Some(mut top) = stack.top.load(Ordering::Acquire) { |
| 180 |
let next = unsafe { top.non_null().as_ref().next().load(Ordering::Relaxed) }; |
| 181 |
|
| 182 |
if stack |
| 183 |
.top |
| 184 |
.compare_and_exchange_weak(Some(top), next, Ordering::Release, Ordering::Relaxed) |
| 185 |
.is_ok() |
| 186 |
{ |
| 187 |
top.increase_tag(); |
| 188 |
|
| 189 |
return Some(top); |
| 190 |
} |
| 191 |
} else { |
| 192 |
|
| 193 |
return None; |
| 194 |
} |
| 195 |
} |
| 196 |
} |