#![no_std]
#![doc = include_str!("../README.md")]
mod mutex;
use core::marker::PhantomData;
pub use self::mutex::Mutex;
#[derive(Clone, Copy, Debug)]
pub struct CriticalSection<'cs> {
_0: PhantomData<&'cs ()>,
}
impl<'cs> CriticalSection<'cs> {
#[inline(always)]
pub unsafe fn new() -> Self {
CriticalSection { _0: PhantomData }
}
}
#[cfg(any(
all(feature = "restore-state-none", feature = "restore-state-bool"),
all(feature = "restore-state-none", feature = "restore-state-u8"),
all(feature = "restore-state-none", feature = "restore-state-u16"),
all(feature = "restore-state-none", feature = "restore-state-u32"),
all(feature = "restore-state-none", feature = "restore-state-u64"),
all(feature = "restore-state-bool", feature = "restore-state-u8"),
all(feature = "restore-state-bool", feature = "restore-state-u16"),
all(feature = "restore-state-bool", feature = "restore-state-u32"),
all(feature = "restore-state-bool", feature = "restore-state-u64"),
all(feature = "restore-state-u8", feature = "restore-state-u16"),
all(feature = "restore-state-u8", feature = "restore-state-u32"),
all(feature = "restore-state-u8", feature = "restore-state-u64"),
all(feature = "restore-state-u16", feature = "restore-state-u32"),
all(feature = "restore-state-u16", feature = "restore-state-u64"),
all(feature = "restore-state-u32", feature = "restore-state-u64"),
))]
compile_error!("You must set at most one of these Cargo features: restore-state-none, restore-state-bool, restore-state-u8, restore-state-u16, restore-state-u32, restore-state-u64");
#[cfg(not(any(
feature = "restore-state-bool",
feature = "restore-state-u8",
feature = "restore-state-u16",
feature = "restore-state-u32",
feature = "restore-state-u64"
)))]
type RawRestoreStateInner = ();
#[cfg(feature = "restore-state-bool")]
type RawRestoreStateInner = bool;
#[cfg(feature = "restore-state-u8")]
type RawRestoreStateInner = u8;
#[cfg(feature = "restore-state-u16")]
type RawRestoreStateInner = u16;
#[cfg(feature = "restore-state-u32")]
type RawRestoreStateInner = u32;
#[cfg(feature = "restore-state-u64")]
type RawRestoreStateInner = u64;
pub type RawRestoreState = RawRestoreStateInner;
#[derive(Clone, Copy, Debug)]
pub struct RestoreState(RawRestoreState);
impl RestoreState {
pub const fn invalid() -> Self {
#[cfg(not(any(
feature = "restore-state-bool",
feature = "restore-state-u8",
feature = "restore-state-u16",
feature = "restore-state-u32",
feature = "restore-state-u64"
)))]
return Self(());
#[cfg(feature = "restore-state-bool")]
return Self(false);
#[cfg(feature = "restore-state-u8")]
return Self(0);
#[cfg(feature = "restore-state-u16")]
return Self(0);
#[cfg(feature = "restore-state-u32")]
return Self(0);
#[cfg(feature = "restore-state-u64")]
return Self(0);
}
}
#[inline]
pub unsafe fn acquire() -> RestoreState {
extern "Rust" {
fn _critical_section_1_0_acquire() -> RawRestoreState;
}
RestoreState(_critical_section_1_0_acquire())
}
#[inline]
pub unsafe fn release(restore_state: RestoreState) {
extern "Rust" {
fn _critical_section_1_0_release(restore_state: RawRestoreState);
}
_critical_section_1_0_release(restore_state.0)
}
#[inline]
pub fn with<R>(f: impl FnOnce(CriticalSection) -> R) -> R {
unsafe {
let restore_state = acquire();
let r = f(CriticalSection::new());
release(restore_state);
r
}
}
pub unsafe trait Impl {
unsafe fn acquire() -> RawRestoreState;
unsafe fn release(restore_state: RawRestoreState);
}
#[macro_export]
macro_rules! set_impl {
($t: ty) => {
#[no_mangle]
unsafe fn _critical_section_1_0_acquire() -> $crate::RawRestoreState {
<$t as $crate::Impl>::acquire()
}
#[no_mangle]
unsafe fn _critical_section_1_0_release(restore_state: $crate::RawRestoreState) {
<$t as $crate::Impl>::release(restore_state)
}
};
}