use crate::{disable_mask, set_mask, MASK_ALL};
use core::ops::Deref;
pub struct CriticalSection<'cs> {
inner: bare_metal::CriticalSection<'cs>,
mask: u32,
}
impl<'cs> Default for CriticalSection<'cs> {
fn default() -> Self {
Self::new()
}
}
impl<'cs> CriticalSection<'cs> {
pub fn new() -> Self {
let mask = disable_mask(MASK_ALL);
Self {
inner: unsafe { bare_metal::CriticalSection::new() },
mask,
}
}
}
impl<'cs> Deref for CriticalSection<'cs> {
type Target = bare_metal::CriticalSection<'cs>;
fn deref(&self) -> &Self::Target {
&self.inner
}
}
impl<'cs> Drop for CriticalSection<'cs> {
fn drop(&mut self) {
unsafe { set_mask(self.mask) };
}
}