Skip to content

Commit 4a9b53d

Browse files
committed
Replace interrupt::free with critical_section::with.
1 parent 0d94e3b commit 4a9b53d

File tree

12 files changed

+71
-61
lines changed

12 files changed

+71
-61
lines changed

.github/workflows/ci.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,6 @@ jobs:
2929
toolchain: ${{ matrix.rust }}
3030
override: true
3131
- name: Run tests
32-
run: cargo test --all --exclude cortex-m-rt --exclude testsuite
32+
run: cargo test --all --exclude cortex-m-rt --exclude testsuite --features cortex-m/single-core-critical-section
3333

3434
# FIXME: test on macOS and Windows

.github/workflows/clippy.yml

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,14 @@
11
on:
22
push:
33
branches: [ staging, trying, master ]
4-
pull_request_target:
4+
pull_request:
55

66
name: Clippy check
77
jobs:
88
clippy:
99
runs-on: ubuntu-latest
1010
steps:
1111
- uses: actions/checkout@v3
12-
if: github.event_name == 'pull_request_target'
13-
with:
14-
ref: refs/pull/${{ github.event.number }}/head
15-
- uses: actions/checkout@v3
16-
if: github.event_name != 'pull_request_target'
1712
- uses: actions-rs/toolchain@v1
1813
with:
1914
profile: minimal
@@ -23,4 +18,4 @@ jobs:
2318
- uses: actions-rs/clippy-check@v1
2419
with:
2520
token: ${{ secrets.GITHUB_TOKEN }}
26-
args: --all
21+
args: --all --features cortex-m/single-core-critical-section

.github/workflows/rt-ci.yml

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -69,18 +69,18 @@ jobs:
6969
- name: Install all Rust targets
7070
run: rustup target install thumbv6m-none-eabi thumbv7m-none-eabi thumbv7em-none-eabi thumbv7em-none-eabihf thumbv8m.base-none-eabi thumbv8m.main-none-eabi thumbv8m.main-none-eabihf
7171
- name: Build examples for thumbv6m-none-eabi
72-
run: cargo build --target=thumbv6m-none-eabi --examples
72+
run: cargo build --target=thumbv6m-none-eabi --features cortex-m/single-core-critical-section --examples
7373
- name: Build examples for thumbv7m-none-eabi
74-
run: cargo build --target=thumbv7m-none-eabi --examples
74+
run: cargo build --target=thumbv7m-none-eabi --features cortex-m/single-core-critical-section --examples
7575
- name: Build examples for thumbv7em-none-eabi
76-
run: cargo build --target=thumbv7em-none-eabi --examples
76+
run: cargo build --target=thumbv7em-none-eabi --features cortex-m/single-core-critical-section --examples
7777
- name: Build examples for thumbv7em-none-eabihf
78-
run: cargo build --target=thumbv7em-none-eabihf --examples
78+
run: cargo build --target=thumbv7em-none-eabihf --features cortex-m/single-core-critical-section --examples
7979
- name: Build examples for thumbv8m.base-none-eabi
80-
run: cargo build --target=thumbv8m.base-none-eabi --examples
80+
run: cargo build --target=thumbv8m.base-none-eabi --features cortex-m/single-core-critical-section --examples
8181
- name: Build examples for thumbv8m.main-none-eabi
82-
run: cargo build --target=thumbv8m.main-none-eabi --examples
82+
run: cargo build --target=thumbv8m.main-none-eabi --features cortex-m/single-core-critical-section --examples
8383
- name: Build examples for thumbv8m.main-none-eabihf
84-
run: cargo build --target=thumbv8m.main-none-eabihf --examples
84+
run: cargo build --target=thumbv8m.main-none-eabihf --features cortex-m/single-core-critical-section --examples
8585
- name: Build crate for host OS
8686
run: cargo build

cortex-m-rt/ci/script.sh

Lines changed: 17 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,13 @@ main() {
77

88
cargo check --target "$TARGET" --features device
99

10+
# A `critical_section` implementation is always needed.
11+
needed_features=cortex-m/single-core-critical-section
12+
1013
if [ "$TARGET" = x86_64-unknown-linux-gnu ] && [ "$TRAVIS_RUST_VERSION" = stable ]; then
1114
( cd macros && cargo check && cargo test )
1215

13-
cargo test --features device --test compiletest
16+
cargo test --features "device,${needed_features}" --test compiletest
1417
fi
1518

1619
local examples=(
@@ -43,35 +46,35 @@ main() {
4346
if [ "$TARGET" != x86_64-unknown-linux-gnu ]; then
4447
# Only test on stable and nightly, not MSRV.
4548
if [ "$TRAVIS_RUST_VERSION" = stable ] || [ "$TRAVIS_RUST_VERSION" = nightly ]; then
46-
RUSTDOCFLAGS="-Cpanic=abort" cargo test --doc
49+
RUSTDOCFLAGS="-Cpanic=abort" cargo test --features "${needed_features}" --doc
4750
fi
4851

4952
for linker in "${linkers[@]}"; do
5053
for ex in "${examples[@]}"; do
51-
cargo rustc --target "$TARGET" --example "$ex" -- $linker
52-
cargo rustc --target "$TARGET" --example "$ex" --release -- $linker
54+
cargo rustc --target "$TARGET" --example "$ex" --features "${needed_features}" -- $linker
55+
cargo rustc --target "$TARGET" --example "$ex" --features "${needed_features}" --release -- $linker
5356
done
5457
for ex in "${fail_examples[@]}"; do
55-
! cargo rustc --target "$TARGET" --example "$ex" -- $linker
56-
! cargo rustc --target "$TARGET" --example "$ex" --release -- $linker
58+
! cargo rustc --target "$TARGET" --example "$ex" --features "${needed_features}" -- $linker
59+
! cargo rustc --target "$TARGET" --example "$ex" --features "${needed_features}" --release -- $linker
5760
done
58-
cargo rustc --target "$TARGET" --example device --features device -- $linker
59-
cargo rustc --target "$TARGET" --example device --features device --release -- $linker
61+
cargo rustc --target "$TARGET" --example device --features "device,${needed_features}" -- $linker
62+
cargo rustc --target "$TARGET" --example device --features "device,${needed_features}" --release -- $linker
6063

61-
cargo rustc --target "$TARGET" --example minimal --features set-sp -- $linker
62-
cargo rustc --target "$TARGET" --example minimal --features set-sp --release -- $linker
63-
cargo rustc --target "$TARGET" --example minimal --features set-vtor -- $linker
64-
cargo rustc --target "$TARGET" --example minimal --features set-vtor --release -- $linker
64+
cargo rustc --target "$TARGET" --example minimal --features "set-sp,${needed_features}" -- $linker
65+
cargo rustc --target "$TARGET" --example minimal --features "set-sp,${needed_features}" --release -- $linker
66+
cargo rustc --target "$TARGET" --example minimal --features "set-vtor,${needed_features}" -- $linker
67+
cargo rustc --target "$TARGET" --example minimal --features "set-vtor,${needed_features}" --release -- $linker
6568
done
6669
fi
6770

6871
case $TARGET in
6972
thumbv6m-none-eabi|thumbv7m-none-eabi)
7073
for linker in "${linkers[@]}"; do
7174
env RUSTFLAGS="$linker -C link-arg=-Tlink.x" cargo run \
72-
--target "$TARGET" --example qemu | grep "x = 42"
75+
--target "$TARGET" --features "${needed_features}" --example qemu | grep "x = 42"
7376
env RUSTFLAGS="$linker -C link-arg=-Tlink.x" cargo run \
74-
--target "$TARGET" --example qemu --release | grep "x = 42"
77+
--target "$TARGET" --features "${needed_features}" --example qemu --release | grep "x = 42"
7578
done
7679

7780
;;

cortex-m-semihosting/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,3 +21,4 @@ no-semihosting = []
2121

2222
[dependencies]
2323
cortex-m = { path = "..", version = ">= 0.5.8, < 0.8" }
24+
critical-section = "0.2"

cortex-m-semihosting/src/export.rs

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,12 @@
22
33
use core::fmt::{self, Write};
44

5-
use cortex_m::interrupt;
6-
75
use crate::hio::{self, HostStream};
86

97
static mut HSTDOUT: Option<HostStream> = None;
108

119
pub fn hstdout_str(s: &str) {
12-
let _result = interrupt::free(|| unsafe {
10+
let _result = critical_section::with(|_| unsafe {
1311
if HSTDOUT.is_none() {
1412
HSTDOUT = Some(hio::hstdout()?);
1513
}
@@ -19,7 +17,7 @@ pub fn hstdout_str(s: &str) {
1917
}
2018

2119
pub fn hstdout_fmt(args: fmt::Arguments) {
22-
let _result = interrupt::free(|| unsafe {
20+
let _result = critical_section::with(|_| unsafe {
2321
if HSTDOUT.is_none() {
2422
HSTDOUT = Some(hio::hstdout()?);
2523
}
@@ -31,7 +29,7 @@ pub fn hstdout_fmt(args: fmt::Arguments) {
3129
static mut HSTDERR: Option<HostStream> = None;
3230

3331
pub fn hstderr_str(s: &str) {
34-
let _result = interrupt::free(|| unsafe {
32+
let _result = critical_section::with(|_| unsafe {
3533
if HSTDERR.is_none() {
3634
HSTDERR = Some(hio::hstderr()?);
3735
}
@@ -41,7 +39,7 @@ pub fn hstderr_str(s: &str) {
4139
}
4240

4341
pub fn hstderr_fmt(args: fmt::Arguments) {
44-
let _result = interrupt::free(|| unsafe {
42+
let _result = critical_section::with(|_| unsafe {
4543
if HSTDERR.is_none() {
4644
HSTDERR = Some(hio::hstderr()?);
4745
}

src/critical_section.rs

Lines changed: 23 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,32 @@
1-
use crate::interrupt;
2-
use crate::register::primask::{self, Primask};
1+
#[cfg(all(cortex_m, feature = "single-core-critical-section"))]
2+
mod single_core_critical_section {
3+
use crate::interrupt;
4+
use crate::register::primask::{self, Primask};
35

4-
struct CriticalSection;
5-
critical_section::custom_impl!(CriticalSection);
6+
struct CriticalSection;
7+
critical_section::custom_impl!(CriticalSection);
68

7-
const TOKEN_IGNORE: u8 = 0;
8-
const TOKEN_REENABLE: u8 = 1;
9+
const TOKEN_IGNORE: u8 = 0;
10+
const TOKEN_REENABLE: u8 = 1;
911

10-
unsafe impl critical_section::Impl for CriticalSection {
11-
unsafe fn acquire() -> u8 {
12-
match primask::read() {
13-
Primask::Active => {
14-
interrupt::disable();
15-
TOKEN_REENABLE
12+
unsafe impl critical_section::Impl for CriticalSection {
13+
unsafe fn acquire() -> u8 {
14+
match primask::read() {
15+
Primask::Active => {
16+
interrupt::disable();
17+
TOKEN_REENABLE
18+
}
19+
Primask::Inactive => TOKEN_IGNORE,
1620
}
17-
Primask::Inactive => TOKEN_IGNORE,
1821
}
19-
}
2022

21-
unsafe fn release(token: u8) {
22-
// Only re-enable interrupts if they were enabled before the critical section.
23-
if token == TOKEN_REENABLE {
24-
interrupt::enable()
23+
unsafe fn release(token: u8) {
24+
// Only re-enable interrupts if they were enabled before the critical section.
25+
if token == TOKEN_REENABLE {
26+
interrupt::enable()
27+
}
2528
}
2629
}
2730
}
31+
32+
pub use critical_section::with;

src/lib.rs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,8 +49,11 @@ mod macros;
4949
pub mod asm;
5050
#[cfg(armv8m)]
5151
pub mod cmse;
52-
#[cfg(all(cortex_m, feature = "single-core-critical-section"))]
53-
mod critical_section;
52+
// This is only public so the `singleton` macro does not require depending on
53+
// the `critical-section` crate separately.
54+
#[doc(hidden)]
55+
#[cfg(feature = "critical-section")]
56+
pub mod critical_section;
5457
pub mod delay;
5558
pub mod interrupt;
5659
#[cfg(all(not(armv6m), not(armv8m_base)))]

src/macros.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ macro_rules! iprintln {
6262
#[macro_export]
6363
macro_rules! singleton {
6464
($name:ident: $ty:ty = $expr:expr) => {
65-
$crate::interrupt::free(|| {
65+
$crate::critical_section::with(|_| {
6666
// this is a tuple of a MaybeUninit and a bool because using an Option here is
6767
// problematic: Due to niche-optimization, an Option could end up producing a non-zero
6868
// initializer value which would move the entire static from `.bss` into `.data`...

src/peripheral/mod.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,6 @@
5757
//!
5858
//! - ARMv7-M Architecture Reference Manual (Issue E.b) - Chapter B3
5959
60-
use crate::interrupt;
6160
use core::marker::PhantomData;
6261
use core::ops;
6362

@@ -164,7 +163,7 @@ impl Peripherals {
164163
/// Returns all the core peripherals *once*
165164
#[inline]
166165
pub fn take() -> Option<Self> {
167-
interrupt::free(|| {
166+
critical_section::with(|_| {
168167
if unsafe { TAKEN } {
169168
None
170169
} else {

src/peripheral/sau.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
//!
88
//! For reference please check the section B8.3 of the Armv8-M Architecture Reference Manual.
99
10-
use crate::interrupt;
1110
use crate::peripheral::SAU;
1211
use bitfield::bitfield;
1312
use volatile_register::{RO, RW};
@@ -162,7 +161,7 @@ impl SAU {
162161
/// This function is executed under a critical section to prevent having inconsistent results.
163162
#[inline]
164163
pub fn set_region(&mut self, region_number: u8, region: SauRegion) -> Result<(), SauError> {
165-
interrupt::free(|| {
164+
critical_section::with(|_| {
166165
let base_address = region.base_address;
167166
let limit_address = region.limit_address;
168167
let attribute = region.attribute;
@@ -215,7 +214,7 @@ impl SAU {
215214
/// This function is executed under a critical section to prevent having inconsistent results.
216215
#[inline]
217216
pub fn get_region(&mut self, region_number: u8) -> Result<SauRegion, SauError> {
218-
interrupt::free(|| {
217+
critical_section::with(|_| {
219218
if region_number >= self.region_numbers() {
220219
Err(SauError::RegionNumberTooBig)
221220
} else {

xtask/tests/ci.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,13 @@ fn build(package: &str, target: &str, features: &[&str]) {
3232
cargo.args(&["--features", *feat]);
3333
}
3434

35+
// A `critical_section` implementation is always needed.
36+
if package == "cortex-m" {
37+
cargo.args(&["--features", "single-core-critical-section"]);
38+
} else if package == "cortex-m-semihosting" || package == "panic-semihosting" {
39+
cargo.args(&["--features", "cortex-m/single-core-critical-section"]);
40+
}
41+
3542
// Cargo features don't work right when invoked from the workspace root, so change to the
3643
// package's directory when necessary.
3744
if package != "cortex-m" {

0 commit comments

Comments
 (0)