ruduino/src/interrupt.rs

36 lines
763 B
Rust
Raw Normal View History

//! Routines for managing interrupts.
use core::prelude::v1::*;
use core::marker::PhantomData;
/// Helper struct that automatically restores interrupts
/// on drop.
struct DisableInterrupts(PhantomData<()>);
/// Executes a closure, disabling interrupts until its completion.
///
/// Restores interrupts after the closure has completed
/// execution.
pub fn without_interrupts<F, T>(f: F) -> T
where F: FnOnce() -> T
{
let _disabled = DisableInterrupts::new();
f()
}
impl DisableInterrupts {
#[inline]
pub fn new() -> DisableInterrupts {
unsafe { llvm_asm!("CLI") }
DisableInterrupts(PhantomData)
}
}
impl Drop for DisableInterrupts {
#[inline]
fn drop(&mut self) {
unsafe { llvm_asm!("SEI") }
}
}