use std::cell::Cell;
use std::marker::PhantomData;
use std::sync::atomic::{AtomicBool, Ordering};
static mut IS_LOCKED: AtomicBool = AtomicBool::new(false);
pub struct SpiceLock {
_x: PhantomData<Cell<()>>,
}
impl SpiceLock {
pub fn try_acquire() -> Result<Self, &'static str> {
let was_unlocked = unsafe {
IS_LOCKED
.compare_exchange(false, true, Ordering::Acquire, Ordering::Relaxed)
.is_ok()
};
if was_unlocked {
Ok(Self { _x: PhantomData })
} else {
Err("Cannot acquire SPICE lock: Already locked.")
}
}
}
impl Drop for SpiceLock {
fn drop(&mut self) {
unsafe {
IS_LOCKED.store(false, Ordering::Release);
}
}
}