Skip to content

Commit b8342cd

Browse files
bors[bot]Dirbaio
authored andcommitted
Merge #451
451: Small critical-section-related fixes. r=adamgreig a=Dirbaio See individual commit messages. Co-authored-by: Dario Nieuwenhuis <[email protected]>
1 parent c00f89b commit b8342cd

File tree

3 files changed

+33
-24
lines changed

3 files changed

+33
-24
lines changed

cortex-m/src/critical_section.rs

Lines changed: 18 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,24 @@
1-
#[cfg(all(cortex_m, feature = "critical-section-single-core"))]
2-
mod single_core_critical_section {
3-
use critical_section::{set_impl, Impl, RawRestoreState};
1+
use critical_section::{set_impl, Impl, RawRestoreState};
42

5-
use crate::interrupt;
6-
use crate::register::primask;
3+
use crate::interrupt;
4+
use crate::register::primask;
75

8-
struct SingleCoreCriticalSection;
9-
set_impl!(SingleCoreCriticalSection);
6+
struct SingleCoreCriticalSection;
7+
set_impl!(SingleCoreCriticalSection);
108

11-
unsafe impl Impl for SingleCoreCriticalSection {
12-
unsafe fn acquire() -> RawRestoreState {
13-
// Backup previous state of PRIMASK register. We access the entire register directly as a
14-
// u32 instead of using the primask::read() function to minimize the number of processor
15-
// cycles during which interrupts are disabled.
16-
let restore_state = primask::read_raw();
17-
// NOTE: Fence guarantees are provided by interrupt::disable(), which performs a `compiler_fence(SeqCst)`.
18-
interrupt::disable();
19-
restore_state
20-
}
9+
unsafe impl Impl for SingleCoreCriticalSection {
10+
unsafe fn acquire() -> RawRestoreState {
11+
// Backup previous state of PRIMASK register. We access the entire register directly as a
12+
// u32 instead of using the primask::read() function to minimize the number of processor
13+
// cycles during which interrupts are disabled.
14+
let restore_state = primask::read_raw();
15+
// NOTE: Fence guarantees are provided by interrupt::disable(), which performs a `compiler_fence(SeqCst)`.
16+
interrupt::disable();
17+
restore_state
18+
}
2119

22-
unsafe fn release(restore_state: RawRestoreState) {
23-
// NOTE: Fence guarantees are provided by primask::write_raw(), which performs a `compiler_fence(SeqCst)`.
24-
primask::write_raw(restore_state);
25-
}
20+
unsafe fn release(restore_state: RawRestoreState) {
21+
// NOTE: Fence guarantees are provided by primask::write_raw(), which performs a `compiler_fence(SeqCst)`.
22+
primask::write_raw(restore_state);
2623
}
2724
}

cortex-m/src/lib.rs

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,6 @@ mod macros;
105105
pub mod asm;
106106
#[cfg(armv8m)]
107107
pub mod cmse;
108-
mod critical_section;
109108
pub mod delay;
110109
pub mod interrupt;
111110
#[cfg(all(not(armv6m), not(armv8m_base)))]
@@ -115,3 +114,13 @@ pub mod prelude;
115114
pub mod register;
116115

117116
pub use crate::peripheral::Peripherals;
117+
118+
#[cfg(all(cortex_m, feature = "critical-section-single-core"))]
119+
mod critical_section;
120+
121+
/// Used to reexport items for use in macros. Do not use directly.
122+
/// Not covered by semver guarantees.
123+
#[doc(hidden)]
124+
pub mod _export {
125+
pub use critical_section;
126+
}

cortex-m/src/macros.rs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,10 @@ macro_rules! iprintln {
3131
/// at most once in the whole lifetime of the program.
3232
///
3333
/// # Notes
34-
/// This macro is unsound on multi core systems.
34+
///
35+
/// This macro requires a `critical-section` implementation to be set. For most single core systems,
36+
/// you can enable the `critical-section-single-core` feature for this crate. For other systems, you
37+
/// have to provide one from elsewhere, typically your chip's HAL crate.
3538
///
3639
/// For debuggability, you can set an explicit name for a singleton. This name only shows up the
3740
/// debugger and is not referenceable from other code. See example below.
@@ -62,7 +65,7 @@ macro_rules! iprintln {
6265
#[macro_export]
6366
macro_rules! singleton {
6467
($(#[$meta:meta])* $name:ident: $ty:ty = $expr:expr) => {
65-
$crate::interrupt::free(|_| {
68+
$crate::_export::critical_section::with(|_| {
6669
// this is a tuple of a MaybeUninit and a bool because using an Option here is
6770
// problematic: Due to niche-optimization, an Option could end up producing a non-zero
6871
// initializer value which would move the entire static from `.bss` into `.data`...

0 commit comments

Comments
 (0)