use core::marker::PhantomData;
#[cfg(not(test))]
use {crate::runtime::enter_irq_guard, crate::runtime::task_runtime};
use crate::runtime::{IrqGuardSource, cpu::IrqGuardToken};
pub(crate) struct IrqScope {
token: IrqGuardToken,
_not_send: PhantomData<*mut ()>,
}
impl IrqScope {
pub(crate) fn enter() -> Self {
Self::enter_with_source(IrqGuardSource::ExplicitScope)
}
pub(super) fn enter_ticket_lock(source: IrqGuardSource) -> Self {
Self::enter_with_source(source)
}
fn enter_with_source(source: IrqGuardSource) -> Self {
#[cfg(test)]
let token = {
let _ = source;
IrqGuardToken::NONE
};
#[cfg(not(test))]
let token = enter_irq_guard(source);
Self {
token,
_not_send: PhantomData,
}
}
}
impl Drop for IrqScope {
fn drop(&mut self) {
if self.token.is_none() {
return;
}
#[cfg(test)]
unreachable!("unit-test IRQ scopes never own runtime tokens");
#[cfg(not(test))]
unsafe {
task_runtime::irq_guard_exit(self.token)
};
}
}