critical_section.rs 669 B

12345678910111213141516171819202122
  1. use critical_section::{set_impl, Impl, RawRestoreState};
  2. use crate::interrupt;
  3. use crate::register::mstatus;
  4. struct SingleHartCriticalSection;
  5. set_impl!(SingleHartCriticalSection);
  6. unsafe impl Impl for SingleHartCriticalSection {
  7. unsafe fn acquire() -> RawRestoreState {
  8. let mut mstatus: usize;
  9. core::arch::asm!("csrrci {}, 0x300, 0b100", out(reg) mstatus);
  10. core::mem::transmute::<_, mstatus::Mstatus>(mstatus).mie()
  11. }
  12. unsafe fn release(was_active: RawRestoreState) {
  13. // Only re-enable interrupts if they were enabled before the critical section.
  14. if was_active {
  15. interrupt::enable()
  16. }
  17. }
  18. }