Skip to content

Commit

Permalink
interrupt: Defer mask until just before branch
Browse files Browse the repository at this point in the history
This does not change the code generation, but in the actual generated
code the mask is deferred until just before the branch, like this.
Since there has been some misleading discussion about this in the past,
we will use code that more closely matches the generated code.
  • Loading branch information
taiki-e committed Oct 19, 2022
1 parent 160d715 commit 3bda363
Showing 1 changed file with 8 additions and 5 deletions.
13 changes: 8 additions & 5 deletions src/imp/interrupt/armv6m.rs
Original file line number Diff line number Diff line change
@@ -1,16 +1,19 @@
// Adapted from https://github.com/rust-embedded/cortex-m.
//
// Generated asm:
// - armv6-m https://godbolt.org/z/oMjd4h3Tr

#[cfg(not(portable_atomic_no_asm))]
use core::arch::asm;

pub(super) use core::sync::atomic;

#[derive(Clone, Copy)]
pub(super) struct WasEnabled(bool);
pub(super) struct State(u32);

/// Disables interrupts and returns the previous interrupt state.
#[inline]
pub(super) fn disable() -> WasEnabled {
pub(super) fn disable() -> State {
let r: u32;
// SAFETY: reading the priority mask register and disabling interrupts are safe.
// (see module-level comments of interrupt/mod.rs on the safety of using privileged instructions)
Expand All @@ -23,13 +26,13 @@ pub(super) fn disable() -> WasEnabled {
options(nostack, preserves_flags),
);
}
WasEnabled(r & 0x1 == 0)
State(r)
}

/// Restores the previous interrupt state.
#[inline]
pub(super) unsafe fn restore(WasEnabled(was_enabled): WasEnabled) {
if was_enabled {
pub(super) unsafe fn restore(State(r): State) {
if r & 0x1 == 0 {
// SAFETY: the caller must guarantee that the state was retrieved by the previous `disable`,
// and we've checked that interrupts were enabled before disabling interrupts.
unsafe {
Expand Down

0 comments on commit 3bda363

Please sign in to comment.