include!("seq_lock_common.rs");
use core::sync::atomic::AtomicU64;
pub(super) type State = u64;
pub(super) struct SeqLock {
state: AtomicU64,
}
impl SeqLock {
#[inline]
pub(super) const fn new() -> Self {
Self { state: AtomicU64::new(0) }
}
#[inline]
pub(super) fn optimistic_read(&self) -> Option<State> {
let state = self.state.load(Ordering::Acquire);
if state == LOCKED { None } else { Some(state) }
}
#[inline]
pub(super) fn validate_read(&self, stamp: State) -> bool {
crate::fence(Ordering::Acquire);
self.state.load(Ordering::Relaxed) == stamp
}
}
impl SeqLockWriteGuard<'_> {
#[inline]
fn next_stamp(&self) -> State {
self.state.wrapping_add(2)
}
}