Skip to content

Commit b0d2d03

Browse files
bors[bot]Dirbaio
andauthored
Merge #451
451: Small critical-section-related fixes. r=adamgreig a=Dirbaio See individual commit messages. Co-authored-by: Dario Nieuwenhuis <[email protected]>
2 parents 0e53054 + 777f07a commit b0d2d03

File tree

3 files changed

+40
-26
lines changed

3 files changed

+40
-26
lines changed

src/critical_section.rs

Lines changed: 15 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,22 @@
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-
let was_active = primask::read().is_active();
14-
interrupt::disable();
15-
was_active
16-
}
9+
unsafe impl Impl for SingleCoreCriticalSection {
10+
unsafe fn acquire() -> RawRestoreState {
11+
let was_active = primask::read().is_active();
12+
interrupt::disable();
13+
was_active
14+
}
1715

18-
unsafe fn release(was_active: RawRestoreState) {
19-
// Only re-enable interrupts if they were enabled before the critical section.
20-
if was_active {
21-
interrupt::enable()
22-
}
16+
unsafe fn release(was_active: RawRestoreState) {
17+
// Only re-enable interrupts if they were enabled before the critical section.
18+
if was_active {
19+
interrupt::enable()
2320
}
2421
}
2522
}
26-
27-
pub use critical_section::with;

src/lib.rs

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,16 @@
99
//!
1010
//! # Optional features
1111
//!
12+
//! ## `critical-section-single-core`
13+
//!
14+
//! This feature enables a [`critical-section`](https://github.com/rust-embedded/critical-section)
15+
//! implementation suitable for single-core targets, based on disabling interrupts globally.
16+
//!
17+
//! It is **unsound** to enable it on multi-core targets or for code running in unprivileged mode,
18+
//! and may cause functional problems in systems where some interrupts must be not be disabled
19+
//! or critical sections are managed as part of an RTOS. In these cases, you should use
20+
//! a target-specific implementation instead, typically provided by a HAL or RTOS crate.
21+
//!
1222
//! ## `cm7-r0p1`
1323
//!
1424
//! This feature enables workarounds for errata found on Cortex-M7 chips with revision r0p1. Some
@@ -49,10 +59,6 @@ mod macros;
4959
pub mod asm;
5060
#[cfg(armv8m)]
5161
pub mod cmse;
52-
// This is only public so the `singleton` macro does not require depending on
53-
// the `critical-section` crate separately.
54-
#[doc(hidden)]
55-
pub mod critical_section;
5662
pub mod delay;
5763
pub mod interrupt;
5864
#[cfg(all(not(armv6m), not(armv8m_base)))]
@@ -61,3 +67,13 @@ pub mod peripheral;
6167
pub mod register;
6268

6369
pub use crate::peripheral::Peripherals;
70+
71+
#[cfg(all(cortex_m, feature = "critical-section-single-core"))]
72+
mod critical_section;
73+
74+
/// Used to reexport items for use in macros. Do not use directly.
75+
/// Not covered by semver guarantees.
76+
#[doc(hidden)]
77+
pub mod _export {
78+
pub use critical_section;
79+
}

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
/// the debugger and is not referencable from other code. See example below.
@@ -62,7 +65,7 @@ macro_rules! iprintln {
6265
#[macro_export]
6366
macro_rules! singleton {
6467
($name:ident: $ty:ty = $expr:expr) => {
65-
$crate::critical_section::with(|_| {
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)