use core::cell::{RefCell, RefMut};
use core::sync::atomic::{AtomicBool, Ordering};
pub struct UPSafeCell<T> {
inner: RefCell<T>,
}
unsafe impl<T> Sync for UPSafeCell<T> {}
impl<T> UPSafeCell<T> {
pub unsafe fn new(value: T) -> Self {
Self {
inner: RefCell::new(value),
}
}
pub fn exclusive_access(&self) -> RefMut<'_, T> {
self.inner.borrow_mut()
}
}
pub struct RawExclusiveLock(AtomicBool);
unsafe impl lock_api::RawMutex for RawExclusiveLock {
const INIT: Self = Self(AtomicBool::new(false));
type GuardMarker = lock_api::GuardNoSend;
fn lock(&self) {
assert_eq!(self.0.load(Ordering::Relaxed), false);
self.0.store(true, Ordering::Relaxed);
}
fn try_lock(&self) -> bool {
self.0
.compare_exchange(false, true, Ordering::Relaxed, Ordering::Relaxed)
.is_ok()
}
unsafe fn unlock(&self) {
self.0.store(false, Ordering::Relaxed);
}
}