use core::sync::atomic::{AtomicU8, Ordering};
use super::RESULT_PENDING;
pub unsafe trait StateWord: Send + Sync {
fn pending() -> Self;
fn load(&self, order: Ordering) -> u8;
fn store(&self, value: u8, order: Ordering);
fn compare_exchange(
&self,
current: u8,
new: u8,
success: Ordering,
failure: Ordering,
) -> Result<u8, u8>;
fn get_mut(&mut self) -> &mut u8;
}
unsafe impl StateWord for AtomicU8 {
#[inline]
fn pending() -> Self {
Self::new(RESULT_PENDING)
}
#[inline]
fn load(&self, order: Ordering) -> u8 {
Self::load(self, order)
}
#[inline]
fn store(&self, value: u8, order: Ordering) {
Self::store(self, value, order);
}
#[inline]
fn compare_exchange(
&self,
current: u8,
new: u8,
success: Ordering,
failure: Ordering,
) -> Result<u8, u8> {
Self::compare_exchange(self, current, new, success, failure)
}
#[inline]
fn get_mut(&mut self) -> &mut u8 {
Self::get_mut(self)
}
}
unsafe impl StateWord for crate::cache::CacheAligned<AtomicU8> {
#[inline]
fn pending() -> Self {
Self::new(AtomicU8::new(RESULT_PENDING))
}
#[inline]
fn load(&self, order: Ordering) -> u8 {
self.0.load(order)
}
#[inline]
fn store(&self, value: u8, order: Ordering) {
self.0.store(value, order);
}
#[inline]
fn compare_exchange(
&self,
current: u8,
new: u8,
success: Ordering,
failure: Ordering,
) -> Result<u8, u8> {
self.0.compare_exchange(current, new, success, failure)
}
#[inline]
fn get_mut(&mut self) -> &mut u8 {
self.0.get_mut()
}
}