Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
110: Add critical-section 1.0 implementation, fix multicore unsoundness. r=almindor a=Dirbaio ~~Requires #109~~ This adds a [critical-section](https://github.com/rust-embedded/critical-section) implementation for single-core chips, based on disabling all interrupts. `interrupt::free` is is unsound on multicore systems because it only disables interrupts in the current core. For multicore chips, a chip-specific critical section implementationis needed instead. Unsoundness is fixed by not returning the `CriticalSection` token. This is a breaking change. This is the riscv equivalent of rust-embedded/cortex-m#447 and rust-embedded/cortex-m#448 Co-authored-by: Dario Nieuwenhuis <dirbaio@dirbaio.net>
- Loading branch information
Showing
7 changed files
with
78 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
use critical_section::{set_impl, Impl, RawRestoreState}; | ||
|
||
use crate::interrupt; | ||
use crate::register::mstatus; | ||
|
||
struct SingleHartCriticalSection; | ||
set_impl!(SingleHartCriticalSection); | ||
|
||
unsafe impl Impl for SingleHartCriticalSection { | ||
unsafe fn acquire() -> RawRestoreState { | ||
let was_active = mstatus::read().mie(); | ||
interrupt::disable(); | ||
was_active | ||
} | ||
|
||
unsafe fn release(was_active: RawRestoreState) { | ||
// Only re-enable interrupts if they were enabled before the critical section. | ||
if was_active { | ||
interrupt::enable() | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters