#![cfg_attr(not(feature = "std"), no_std)]
#![doc = include_str!("../README.md")]
mod mutex;
#[cfg(feature = "std")]
mod std;
use core::marker::PhantomData;
pub use self::mutex::Mutex;
#[derive(Clone, Copy, Debug)]
pub struct CriticalSection<'cs> {
_private: PhantomData<&'cs ()>,
_not_send_sync: PhantomData<*mut ()>,
}
impl<'cs> CriticalSection<'cs> {
#[inline(always)]
pub unsafe fn new() -> Self {
CriticalSection {
_private: PhantomData,
_not_send_sync: 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-bool", feature = "restore-state-usize"),
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-u8", feature = "restore-state-usize"),
all(feature = "restore-state-u16", feature = "restore-state-u32"),
all(feature = "restore-state-u16", feature = "restore-state-u64"),
all(feature = "restore-state-u16", feature = "restore-state-usize"),
all(feature = "restore-state-u32", feature = "restore-state-u64"),
all(feature = "restore-state-u32", feature = "restore-state-usize"),
all(feature = "restore-state-u64", feature = "restore-state-usize"),
))]
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, restore-state-usize");
#[cfg(not(any(
feature = "restore-state-bool",
feature = "restore-state-u8",
feature = "restore-state-u16",
feature = "restore-state-u32",
feature = "restore-state-u64",
feature = "restore-state-usize"
)))]
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;
#[cfg(feature = "restore-state-usize")]
type RawRestoreStateInner = usize;
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",
feature = "restore-state-usize"
)))]
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);
#[cfg(feature = "restore-state-usize")]
return Self(0);
}
}
#[inline(always)]
pub unsafe fn acquire() -> RestoreState {
extern "Rust" {
fn _critical_section_1_0_acquire() -> RawRestoreState;
}
#[allow(clippy::unit_arg)]
RestoreState(_critical_section_1_0_acquire())
}
#[inline(always)]
pub unsafe fn release(restore_state: RestoreState) {
extern "Rust" {
fn _critical_section_1_0_release(restore_state: RawRestoreState);
}
#[allow(clippy::unit_arg)]
_critical_section_1_0_release(restore_state.0)
}
#[inline]
pub fn with<R>(f: impl FnOnce(CriticalSection) -> R) -> R {
struct Guard {
state: RestoreState,
}
impl Drop for Guard {
#[inline(always)]
fn drop(&mut self) {
unsafe { release(self.state) }
}
}
let state = unsafe { acquire() };
let _guard = Guard { state };
unsafe { f(CriticalSection::new()) }
}
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)
}
};
}