use core::cell::UnsafeCell;
pub struct Mutex<T> {
inner: UnsafeCell<T>,
}
impl<T> Mutex<T> {
pub const fn new(value: T) -> Self {
Mutex { inner: UnsafeCell::new(value) }
}
}
impl<T> Mutex<T> {
pub fn borrow<'cs>(&self, _ctxt: &'cs CriticalSection) -> &'cs T {
unsafe { &*self.inner.get() }
}
}
pub unsafe trait Nr {
fn nr(&self) -> u8;
}
unsafe impl<T> Sync for Mutex<T> {}
#[inline(always)]
pub fn disable() {
match () {
#[cfg(target_arch = "arm")]
() => unsafe {
asm!("cpsid i"
:
:
:
: "volatile");
},
#[cfg(not(target_arch = "arm"))]
() => {}
}
}
#[inline(always)]
pub fn enable() {
match () {
#[cfg(target_arch = "arm")]
() => unsafe {
asm!("cpsie i"
:
:
:
: "volatile");
},
#[cfg(not(target_arch = "arm"))]
() => {}
}
}
pub struct CriticalSection {
_0: (),
}
pub fn free<F, R>(f: F) -> R
where
F: FnOnce(&CriticalSection) -> R,
{
let primask = ::register::primask::read();
disable();
let r = f(&CriticalSection { _0: () });
if primask.is_active() {
enable();
}
r
}