Skip to content

Commit 75046d1

Browse files
committed
Migrate to portable-atomic
1 parent 52da9d9 commit 75046d1

File tree

4 files changed

+16
-86
lines changed

4 files changed

+16
-86
lines changed

.github/workflows/doctests.yml

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -9,23 +9,13 @@ name: Documentation Tests
99
jobs:
1010
build:
1111
runs-on: ubuntu-latest
12-
strategy:
13-
matrix:
14-
include:
15-
- rust: stable
16-
features: thumbv6
17-
nodefault: "--no-default-features"
18-
- rust: stable
19-
features: ""
20-
nodefault: ""
21-
2212
steps:
2313
- uses: actions/checkout@v2
2414
- uses: actions-rs/toolchain@v1
2515
with:
2616
profile: minimal
27-
toolchain: ${{ matrix.rust }}
17+
toolchain: stable
2818
- uses: actions-rs/cargo@v1
2919
with:
3020
command: test
31-
args: ${{ matrix.nodefault }} --features=${{ matrix.features }} --manifest-path core/Cargo.toml
21+
args: --manifest-path core/Cargo.toml

.github/workflows/embedded-builds.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ jobs:
1515
- features: ""
1616
target: thumbv7em-none-eabihf
1717
rust: stable
18-
- feature: thumbv6
18+
- feature: portable-atomic/critical-section
1919
target: thumbv6m-none-eabi
2020
rust: stable
2121

core/Cargo.toml

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,8 @@ categories = [
1515
license = "MIT OR Apache-2.0"
1616

1717
[dependencies]
18-
cortex-m = { version = "0.6.0", optional = true }
19-
20-
[dependencies.defmt]
21-
version = "0.3.0"
22-
optional = true
18+
portable-atomic = { version = "1.5.1", default-features = false, features = ["require-cas"] }
19+
defmt = { version = "0.3.0", optional = true }
2320

2421
[package.metadata.docs.rs]
2522
all-features = true

core/src/bbbuffer.rs

Lines changed: 11 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,10 @@ use core::{
1111
ptr::NonNull,
1212
result::Result as CoreResult,
1313
slice::from_raw_parts_mut,
14-
sync::atomic::{
15-
AtomicBool, AtomicUsize,
16-
Ordering::{AcqRel, Acquire, Release},
17-
},
14+
sync::atomic::Ordering::{AcqRel, Acquire, Release},
1815
};
16+
use portable_atomic::{AtomicBool, AtomicUsize};
17+
1918
#[derive(Debug)]
2019
/// A backing structure for a BBQueue. Can be used to create either
2120
/// a BBQueue or a split Producer/Consumer pair
@@ -86,7 +85,7 @@ impl<'a, const N: usize> BBBuffer<N> {
8685
/// # }
8786
/// ```
8887
pub fn try_split(&'a self) -> Result<(Producer<'a, N>, Consumer<'a, N>)> {
89-
if atomic::swap(&self.already_split, true, AcqRel) {
88+
if self.already_split.swap(true, AcqRel) {
9089
return Err(Error::AlreadySplit);
9190
}
9291

@@ -352,7 +351,7 @@ impl<'a, const N: usize> Producer<'a, N> {
352351
pub fn grant_exact(&mut self, sz: usize) -> Result<GrantW<'a, N>> {
353352
let inner = unsafe { &self.bbq.as_ref() };
354353

355-
if atomic::swap(&inner.write_in_progress, true, AcqRel) {
354+
if inner.write_in_progress.swap(true, AcqRel) {
356355
return Err(Error::GrantInProgress);
357356
}
358357

@@ -450,7 +449,7 @@ impl<'a, const N: usize> Producer<'a, N> {
450449
pub fn grant_max_remaining(&mut self, mut sz: usize) -> Result<GrantW<'a, N>> {
451450
let inner = unsafe { &self.bbq.as_ref() };
452451

453-
if atomic::swap(&inner.write_in_progress, true, AcqRel) {
452+
if inner.write_in_progress.swap(true, AcqRel) {
454453
return Err(Error::GrantInProgress);
455454
}
456455

@@ -555,7 +554,7 @@ impl<'a, const N: usize> Consumer<'a, N> {
555554
pub fn read(&mut self) -> Result<GrantR<'a, N>> {
556555
let inner = unsafe { &self.bbq.as_ref() };
557556

558-
if atomic::swap(&inner.read_in_progress, true, AcqRel) {
557+
if inner.read_in_progress.swap(true, AcqRel) {
559558
return Err(Error::GrantInProgress);
560559
}
561560

@@ -607,7 +606,7 @@ impl<'a, const N: usize> Consumer<'a, N> {
607606
pub fn split_read(&mut self) -> Result<SplitGrantR<'a, N>> {
608607
let inner = unsafe { &self.bbq.as_ref() };
609608

610-
if atomic::swap(&inner.read_in_progress, true, AcqRel) {
609+
if inner.read_in_progress.swap(true, AcqRel) {
611610
return Err(Error::GrantInProgress);
612611
}
613612

@@ -814,7 +813,7 @@ impl<'a, const N: usize> GrantW<'a, N> {
814813
let used = min(len, used);
815814

816815
let write = inner.write.load(Acquire);
817-
atomic::fetch_sub(&inner.reserve, len - used, AcqRel);
816+
inner.reserve.fetch_sub(len - used, AcqRel);
818817

819818
let max = N;
820819
let last = inner.last.load(Acquire);
@@ -951,7 +950,7 @@ impl<'a, const N: usize> GrantR<'a, N> {
951950
debug_assert!(used <= self.buf.len());
952951

953952
// This should be fine, purely incrementing
954-
let _ = atomic::fetch_add(&inner.read, used, Release);
953+
let _ = inner.read.fetch_add(used, Release);
955954

956955
inner.read_in_progress.store(false, Release);
957956
}
@@ -1036,7 +1035,7 @@ impl<'a, const N: usize> SplitGrantR<'a, N> {
10361035

10371036
if used <= self.buf1.len() {
10381037
// This should be fine, purely incrementing
1039-
let _ = atomic::fetch_add(&inner.read, used, Release);
1038+
let _ = inner.read.fetch_add(used, Release);
10401039
} else {
10411040
// Also release parts of the second buffer
10421041
inner.read.store(used - self.buf1.len(), Release);
@@ -1101,59 +1100,3 @@ impl<'a, const N: usize> DerefMut for GrantR<'a, N> {
11011100
self.buf
11021101
}
11031102
}
1104-
1105-
#[cfg(feature = "thumbv6")]
1106-
mod atomic {
1107-
use core::sync::atomic::{
1108-
AtomicBool, AtomicUsize,
1109-
Ordering::{self, Acquire, Release},
1110-
};
1111-
use cortex_m::interrupt::free;
1112-
1113-
#[inline(always)]
1114-
pub fn fetch_add(atomic: &AtomicUsize, val: usize, _order: Ordering) -> usize {
1115-
free(|_| {
1116-
let prev = atomic.load(Acquire);
1117-
atomic.store(prev.wrapping_add(val), Release);
1118-
prev
1119-
})
1120-
}
1121-
1122-
#[inline(always)]
1123-
pub fn fetch_sub(atomic: &AtomicUsize, val: usize, _order: Ordering) -> usize {
1124-
free(|_| {
1125-
let prev = atomic.load(Acquire);
1126-
atomic.store(prev.wrapping_sub(val), Release);
1127-
prev
1128-
})
1129-
}
1130-
1131-
#[inline(always)]
1132-
pub fn swap(atomic: &AtomicBool, val: bool, _order: Ordering) -> bool {
1133-
free(|_| {
1134-
let prev = atomic.load(Acquire);
1135-
atomic.store(val, Release);
1136-
prev
1137-
})
1138-
}
1139-
}
1140-
1141-
#[cfg(not(feature = "thumbv6"))]
1142-
mod atomic {
1143-
use core::sync::atomic::{AtomicBool, AtomicUsize, Ordering};
1144-
1145-
#[inline(always)]
1146-
pub fn fetch_add(atomic: &AtomicUsize, val: usize, order: Ordering) -> usize {
1147-
atomic.fetch_add(val, order)
1148-
}
1149-
1150-
#[inline(always)]
1151-
pub fn fetch_sub(atomic: &AtomicUsize, val: usize, order: Ordering) -> usize {
1152-
atomic.fetch_sub(val, order)
1153-
}
1154-
1155-
#[inline(always)]
1156-
pub fn swap(atomic: &AtomicBool, val: bool, order: Ordering) -> bool {
1157-
atomic.swap(val, order)
1158-
}
1159-
}

0 commit comments

Comments
 (0)