Skip to content

Commit

Permalink
Add optional backend for critical-section (#191)
Browse files Browse the repository at this point in the history
This is similar to the single-threaded bare metal implementations in
`cortex-m` and `riscv`.

In theory there could be a race condition in `acquire` if an interrupt
occurs between the `read` and `write`, and the interrupt disables `IME`.
But it's probably not sensible to have an interrupt disable interrupts,
so I don't think it's necessarily to complicate things with inline
assembly using `swp`.
  • Loading branch information
ids1024 authored Mar 4, 2024
1 parent c2ea385 commit cd4f2db
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 0 deletions.
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ track_caller = []
voladdress = "1.3.0"
bitfrob = "1"
bracer = "0.1.2"
critical-section = { version = "1.1.2", features = ["restore-state-bool"], optional = true }

[profile.dev]
opt-level = 3
Expand Down
18 changes: 18 additions & 0 deletions src/critical_section.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
use critical_section::{set_impl, Impl, RawRestoreState};

use crate::mmio::IME;

struct GbaCriticalSection;
set_impl!(GbaCriticalSection);

unsafe impl Impl for GbaCriticalSection {
unsafe fn acquire() -> RawRestoreState {
let restore = IME.read();
IME.write(false);
restore
}

unsafe fn release(restore: RawRestoreState) {
IME.write(restore);
}
}
2 changes: 2 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,8 @@ mod macros;
pub mod asm_runtime;
pub mod bios;
pub mod builtin_art;
#[cfg(feature = "critical-section")]
mod critical_section;
pub mod dma;
pub mod fixed;
pub mod gba_cell;
Expand Down

0 comments on commit cd4f2db

Please sign in to comment.