Skip to content

Commit 4a37dd9

Browse files
committed
Implement atomic fetch_or and fetch_and for thumbv6
1 parent 59a040e commit 4a37dd9

File tree

1 file changed

+35
-10
lines changed

1 file changed

+35
-10
lines changed

core/src/bbbuffer.rs

Lines changed: 35 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -71,10 +71,7 @@ where
7171
/// ```
7272
pub fn try_split(&'a self) -> Result<(Producer<'a, N>, Consumer<'a, N>)> {
7373
// Set producer/consumer taken bit, error and reset if one was already set
74-
let prev = self
75-
.0
76-
.split_prod_cons
77-
.fetch_or(BIT_PRODUCER | BIT_CONSUMER, AcqRel);
74+
let prev = atomic::fetch_or(&self.0.split_prod_cons, BIT_PRODUCER | BIT_CONSUMER, AcqRel);
7875
if prev > 0 {
7976
self.0.split_prod_cons.store(prev, Release);
8077
return Err(Error::AlreadySplit);
@@ -133,7 +130,7 @@ where
133130
/// while splitting.
134131
pub fn try_take_producer(&'a self) -> Result<Producer<'a, N>> {
135132
// Set producer taken bit, error if already set
136-
if self.0.split_prod_cons.fetch_or(BIT_PRODUCER, AcqRel) & BIT_PRODUCER > 0 {
133+
if atomic::fetch_or(&self.0.split_prod_cons, BIT_PRODUCER, AcqRel) & BIT_PRODUCER > 0 {
137134
return Err(Error::AlreadySplit);
138135
}
139136

@@ -167,7 +164,7 @@ where
167164
/// while splitting.
168165
pub fn try_take_consumer(&'a self) -> Result<Consumer<'a, N>> {
169166
// Set producer taken bit, error if already set
170-
if self.0.split_prod_cons.fetch_or(BIT_CONSUMER, AcqRel) & BIT_CONSUMER > 0 {
167+
if atomic::fetch_or(&self.0.split_prod_cons, BIT_CONSUMER, AcqRel) & BIT_CONSUMER > 0 {
171168
return Err(Error::AlreadySplit);
172169
}
173170

@@ -348,7 +345,7 @@ where
348345
self.0.last.store(0, Release);
349346

350347
// Mark the buffer as ready to retake producer
351-
self.0.split_prod_cons.fetch_and(!BIT_PRODUCER, Release);
348+
atomic::fetch_and(&self.0.split_prod_cons, !BIT_PRODUCER, Release);
352349

353350
Ok(())
354351
}
@@ -419,7 +416,7 @@ where
419416
self.0.last.store(0, Release);
420417

421418
// Mark the buffer as ready to retake consumer
422-
self.0.split_prod_cons.fetch_and(!BIT_CONSUMER, Release);
419+
atomic::fetch_and(&self.0.split_prod_cons, !BIT_CONSUMER, Release);
423420

424421
Ok(())
425422
}
@@ -1437,7 +1434,7 @@ where
14371434
#[cfg(feature = "thumbv6")]
14381435
mod atomic {
14391436
use core::sync::atomic::{
1440-
AtomicBool, AtomicUsize,
1437+
AtomicBool, AtomicU8, AtomicUsize,
14411438
Ordering::{self, Acquire, Release},
14421439
};
14431440
use cortex_m::interrupt::free;
@@ -1460,6 +1457,24 @@ mod atomic {
14601457
})
14611458
}
14621459

1460+
#[inline(always)]
1461+
pub fn fetch_and(atomic: &AtomicU8, val: u8, _order: Ordering) -> u8 {
1462+
free(|_| {
1463+
let prev = atomic.load(Acquire);
1464+
atomic.store(prev & val, Release);
1465+
prev
1466+
})
1467+
}
1468+
1469+
#[inline(always)]
1470+
pub fn fetch_or(atomic: &AtomicU8, val: u8, _order: Ordering) -> u8 {
1471+
free(|_| {
1472+
let prev = atomic.load(Acquire);
1473+
atomic.store(prev | val, Release);
1474+
prev
1475+
})
1476+
}
1477+
14631478
#[inline(always)]
14641479
pub fn swap(atomic: &AtomicBool, val: bool, _order: Ordering) -> bool {
14651480
free(|_| {
@@ -1472,7 +1487,7 @@ mod atomic {
14721487

14731488
#[cfg(not(feature = "thumbv6"))]
14741489
mod atomic {
1475-
use core::sync::atomic::{AtomicBool, AtomicUsize, Ordering};
1490+
use core::sync::atomic::{AtomicBool, AtomicU8, AtomicUsize, Ordering};
14761491

14771492
#[inline(always)]
14781493
pub fn fetch_add(atomic: &AtomicUsize, val: usize, order: Ordering) -> usize {
@@ -1484,6 +1499,16 @@ mod atomic {
14841499
atomic.fetch_sub(val, order)
14851500
}
14861501

1502+
#[inline(always)]
1503+
pub fn fetch_and(atomic: &AtomicU8, val: u8, order: Ordering) -> u8 {
1504+
atomic.fetch_and(val, order)
1505+
}
1506+
1507+
#[inline(always)]
1508+
pub fn fetch_or(atomic: &AtomicU8, val: u8, order: Ordering) -> u8 {
1509+
atomic.fetch_or(val, order)
1510+
}
1511+
14871512
#[inline(always)]
14881513
pub fn swap(atomic: &AtomicBool, val: bool, order: Ordering) -> bool {
14891514
atomic.swap(val, order)

0 commit comments

Comments
 (0)