use core::sync::atomic::Ordering::{Relaxed, Release};
use crate::cfg::atomic::AtomicBool;
use crate::relax::Relax;
pub trait Lock {
#[cfg(not(all(loom, test)))]
const LOCKED: Self;
#[cfg(not(all(loom, test)))]
const UNLOCKED: Self;
#[cfg(all(loom, test))]
fn locked() -> Self;
#[cfg(all(loom, test))]
fn unlocked() -> Self;
fn lock_wait_relaxed<W: Wait>(&self);
fn notify(&self);
}
pub trait Wait {
type LockRelax: Relax;
}
impl Lock for AtomicBool {
#[cfg(not(all(loom, test)))]
#[allow(clippy::declare_interior_mutable_const)]
const LOCKED: Self = Self::new(true);
#[cfg(not(all(loom, test)))]
#[allow(clippy::declare_interior_mutable_const)]
const UNLOCKED: Self = Self::new(false);
#[cfg(all(loom, test))]
#[cfg(not(tarpaulin_include))]
fn locked() -> Self {
Self::new(true)
}
#[cfg(all(loom, test))]
#[cfg(not(tarpaulin_include))]
fn unlocked() -> Self {
Self::new(false)
}
fn lock_wait_relaxed<W: Wait>(&self) {
let mut relax = W::LockRelax::new();
while self.load(Relaxed) {
relax.relax();
}
}
fn notify(&self) {
self.store(false, Release);
}
}