Skip to content

Commit 5684713

Browse files
committed
Add functions to take/release just producer or consumer
See jamesmunns#40 See jamesmunns#67
1 parent 7446101 commit 5684713

File tree

1 file changed

+18
-21
lines changed

1 file changed

+18
-21
lines changed

Diff for: core/src/bbbuffer.rs

+18-21
Original file line numberDiff line numberDiff line change
@@ -54,9 +54,6 @@ pub struct BBBuffer<const N: usize> {
5454
/// Is there an active write grant?
5555
write_in_progress: AtomicBool,
5656

57-
/// Have we already split?
58-
already_split: AtomicBool,
59-
6057
/// Whether we have split the producer and/or consumer parts off.
6158
///
6259
/// See the `BIT_PRODUCER` and `BIT_CONSUMER` bits which define what parts have been split off.
@@ -160,7 +157,7 @@ impl<'a, const N: usize> BBBuffer<N> {
160157
/// while splitting.
161158
pub fn try_take_producer(&'a self) -> Result<Producer<'a, N>> {
162159
// Set producer taken bit, error if already set
163-
if self.0.split_prod_cons.fetch_or(BIT_PRODUCER, AcqRel) & BIT_PRODUCER > 0 {
160+
if self.split_prod_cons.fetch_or(BIT_PRODUCER, AcqRel) & BIT_PRODUCER > 0 {
164161
return Err(Error::AlreadySplit);
165162
}
166163

@@ -169,7 +166,7 @@ impl<'a, const N: usize> BBBuffer<N> {
169166
// // Explicitly zero the data to avoid undefined behavior.
170167
// // This is required, because we hand out references to the buffers,
171168
// // which mean that creating them as references is technically UB for now
172-
// let mu_ptr = self.0.buf.get();
169+
// let mu_ptr = self.buf.get();
173170
// (*mu_ptr).as_mut_ptr().write_bytes(0u8, 1);
174171

175172
let nn1 = NonNull::new_unchecked(self as *const _ as *mut _);
@@ -194,7 +191,7 @@ impl<'a, const N: usize> BBBuffer<N> {
194191
/// while splitting.
195192
pub fn try_take_consumer(&'a self) -> Result<Consumer<'a, N>> {
196193
// Set producer taken bit, error if already set
197-
if self.0.split_prod_cons.fetch_or(BIT_CONSUMER, AcqRel) & BIT_CONSUMER > 0 {
194+
if self.split_prod_cons.fetch_or(BIT_CONSUMER, AcqRel) & BIT_CONSUMER > 0 {
198195
return Err(Error::AlreadySplit);
199196
}
200197

@@ -203,7 +200,7 @@ impl<'a, const N: usize> BBBuffer<N> {
203200
// // Explicitly zero the data to avoid undefined behavior.
204201
// // This is required, because we hand out references to the buffers,
205202
// // which mean that creating them as references is technically UB for now
206-
// let mu_ptr = self.0.buf.get();
203+
// let mu_ptr = self.buf.get();
207204
// (*mu_ptr).as_mut_ptr().write_bytes(0u8, 1);
208205

209206
let nn1 = NonNull::new_unchecked(self as *const _ as *mut _);
@@ -361,8 +358,8 @@ impl<'a, const N: usize> BBBuffer<N> {
361358
return Err(prod);
362359
}
363360

364-
let wr_in_progress = self.0.write_in_progress.load(Acquire);
365-
let rd_in_progress = self.0.read_in_progress.load(Acquire);
361+
let wr_in_progress = self.write_in_progress.load(Acquire);
362+
let rd_in_progress = self.read_in_progress.load(Acquire);
366363

367364
if wr_in_progress || rd_in_progress {
368365
// Can't release, active grant(s) in progress
@@ -373,13 +370,13 @@ impl<'a, const N: usize> BBBuffer<N> {
373370
drop(prod);
374371

375372
// Re-initialize the buffer (not totally needed, but nice to do)
376-
self.0.write.store(0, Release);
377-
self.0.read.store(0, Release);
378-
self.0.reserve.store(0, Release);
379-
self.0.last.store(0, Release);
373+
self.write.store(0, Release);
374+
self.read.store(0, Release);
375+
self.reserve.store(0, Release);
376+
self.last.store(0, Release);
380377

381378
// Mark the buffer as ready to retake producer
382-
self.0.split_prod_cons.fetch_and(!BIT_PRODUCER, Release);
379+
self.split_prod_cons.fetch_and(!BIT_PRODUCER, Release);
383380

384381
Ok(())
385382
}
@@ -432,8 +429,8 @@ impl<'a, const N: usize> BBBuffer<N> {
432429
return Err(cons);
433430
}
434431

435-
let wr_in_progress = self.0.write_in_progress.load(Acquire);
436-
let rd_in_progress = self.0.read_in_progress.load(Acquire);
432+
let wr_in_progress = self.write_in_progress.load(Acquire);
433+
let rd_in_progress = self.read_in_progress.load(Acquire);
437434

438435
if wr_in_progress || rd_in_progress {
439436
// Can't release, active grant(s) in progress
@@ -444,13 +441,13 @@ impl<'a, const N: usize> BBBuffer<N> {
444441
drop(cons);
445442

446443
// Re-initialize the buffer (not totally needed, but nice to do)
447-
self.0.write.store(0, Release);
448-
self.0.read.store(0, Release);
449-
self.0.reserve.store(0, Release);
450-
self.0.last.store(0, Release);
444+
self.write.store(0, Release);
445+
self.read.store(0, Release);
446+
self.reserve.store(0, Release);
447+
self.last.store(0, Release);
451448

452449
// Mark the buffer as ready to retake consumer
453-
self.0.split_prod_cons.fetch_and(!BIT_CONSUMER, Release);
450+
self.split_prod_cons.fetch_and(!BIT_CONSUMER, Release);
454451

455452
Ok(())
456453
}

0 commit comments

Comments
 (0)