bbqueue 0.4.0-alpha4

A (WIP) SPSC, lockless, no_std, thread safe, queue, based on BipBuffers
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
//! A version of BBQueue built on Cortex-M critical sections.
//! This is useful on thumbv6 targets (Cortex-M0, Cortex-M0+)
//! if your platform does not support atomic compare and swaps.

pub use generic_array::typenum::consts;
use core::{
    cell::UnsafeCell,
    marker::PhantomData,
    mem::{size_of, MaybeUninit, forget},
    ops::{Deref, DerefMut},
    ptr::NonNull,
    slice::from_raw_parts,
    slice::from_raw_parts_mut,
};
use generic_array::{ArrayLength, GenericArray};
use cortex_m::interrupt::free;
use crate::{Error, Result};

/// A backing structure for a BBQueue. Can be used to create either
/// a BBQueue or a split Producer/Consumer pair
pub struct BBBuffer<N: ArrayLength<u8>> (
    // Underlying data storage
    #[doc(hidden)] pub ConstBBBuffer<GenericArray<u8, N>>,
);

// unsafe impl<N> Send for BBBuffer<N: ArrayLength<u8>> {}
unsafe impl<A> Sync for ConstBBBuffer<A> {}

impl<'a, N> BBBuffer<N>
where
    N: ArrayLength<u8>,
{
    /// NOTE: Takes a critical section while splitting
    pub fn try_split(&'a self) -> Result<(Producer<'a, N>, Consumer<'a, N>)> {
        free(|_cs| {
            if self.0.already_split {
                return Err(Error::AlreadySplit);
            } else {
                unsafe {
                    // Explicitly zero the data to avoid undefined behavior.
                    // This is required, because we hand out references to the buffers,
                    // which mean that creating them as references is technically UB for now
                    let mu_ptr = self.0.buf.get();
                    (*mu_ptr).as_mut_ptr().write_bytes(0u8, 1);

                    let nn1 = NonNull::new_unchecked(self as *const _ as *mut _);
                    let nn2 = NonNull::new_unchecked(self as *const _ as *mut _);

                    Ok((
                        Producer {
                            bbq: nn1,
                            pd: PhantomData,
                        },
                        Consumer {
                            bbq: nn2,
                            pd: PhantomData,
                        },
                    ))
                }
            }
        })
    }
}

/// `const-fn` version BBBuffer
pub struct ConstBBBuffer<A> {
    buf: UnsafeCell<MaybeUninit<A>>,

    /// Where the next byte will be written
    write: usize,

    /// Where the next byte will be read from
    read: usize,

    /// Used in the inverted case to mark the end of the
    /// readable streak. Otherwise will == unsafe { self.buf.as_mut().len() }.
    /// Writer is responsible for placing this at the correct
    /// place when entering an inverted condition, and Reader
    /// is responsible for moving it back to unsafe { self.buf.as_mut().len() }
    /// when exiting the inverted condition
    last: usize,

    /// Used by the Writer to remember what bytes are currently
    /// allowed to be written to, but are not yet ready to be
    /// read from
    reserve: usize,

    /// Is there an active read grant?
    read_in_progress: bool,

    /// Have we already split?
    already_split: bool,
}

impl<A> ConstBBBuffer<A> {
    pub const fn new() -> Self {
        Self {
            // This will not be initialized until we split the buffer
            buf: UnsafeCell::new(MaybeUninit::uninit()),

            /// Owned by the writer
            write: 0,

            /// Owned by the reader
            read: 0,

            /// Cooperatively owned
            last: size_of::<A>(),

            /// Owned by the Writer, "private"
            reserve: 0,

            /// Owned by the Reader, "private"
            read_in_progress: false,

            already_split: false,
        }
    }
}

pub struct Producer<'a, N>
where
    N: ArrayLength<u8>,
{
    bbq: NonNull<BBBuffer<N>>,
    pd: PhantomData<&'a ()>,
}

unsafe impl<'a, N> Send for Producer<'a, N>
where
    N: ArrayLength<u8>
{ }

impl<'a, N> Producer<'a, N>
where
    N: ArrayLength<u8>,
{
    /// Request a writable, contiguous section of memory of exactly
    /// `sz` bytes. If the buffer size requested is not available,
    /// an error will be returned.
    ///
    /// NOTE: Takes a critical section while determining the grant.
    /// The critical section is only active for the duration of
    /// this function call.
    pub fn grant(&mut self, sz: usize) -> Result<GrantW<N>> {
        free(|_cs| {
            let inner = unsafe { &mut self.bbq.as_mut().0 };

            // Writer component. Must never write to `read`,
            // be careful writing to `load`
            let write = inner.write;

            if inner.reserve != write {
                // GRANT IN PROCESS, do not allow further grants
                // until the current one has been completed
                return Err(Error::GrantInProgress);
            }

            let read = inner.read;
            let max = N::to_usize();
            let already_inverted = write < read;

            let start = if already_inverted {
                if (write + sz) < read {
                    // Inverted, room is still available
                    write
                } else {
                    // Inverted, no room is available
                    return Err(Error::InsufficientSize);
                }
            } else {
                if write + sz <= max {
                    // Non inverted condition
                    write
                } else {
                    // Not inverted, but need to go inverted

                    // NOTE: We check sz < read, NOT <=, because
                    // write must never == read in an inverted condition, since
                    // we will then not be able to tell if we are inverted or not
                    if sz < read {
                        // Invertible situation
                        0
                    } else {
                        // Not invertible, no space
                        return Err(Error::InsufficientSize);
                    }
                }
            };

            // Safe write, only viewed by this task
            inner.reserve = start + sz;

            let c = unsafe { (*inner.buf.get()).as_mut_ptr().cast::<u8>() };
            let d =
                unsafe { from_raw_parts_mut(c.offset(start as isize), sz) };

            Ok(GrantW { buf: d, bbq: self.bbq })
        })
    }

    //     /// Request a writable, contiguous section of memory of up to
    //     /// `sz` bytes. If a buffer of size `sz` is not available, but
    //     /// some space (0 < available < sz) is available, then a grant
    //     /// will be given for the remaining size. If no space is available
    //     /// for writing, an error will be returned
    //     fn grant_max(&mut self, mut sz: usize) -> Result<GrantW> {
    //         // Writer component. Must never write to `read`,
    //         // be careful writing to `load`

    //         let write = self.0.write.load(Relaxed);

    //         if self.0.reserve.load(Relaxed) != write {
    //             // GRANT IN PROCESS, do not allow further grants
    //             // until the current one has been completed
    //             return Err(Error::GrantInProgress);
    //         }

    //         let read = self.0.read.load(Acquire);
    //         let max = unsafe { (*self.0.buf.as_mut_ptr()).as_mut().len() };

    //         let already_inverted = write < read;

    //         let start = if already_inverted {
    //             // In inverted case, read is always > write
    //             let remain = read - write - 1;

    //             if remain != 0 {
    //                 sz = min(remain, sz);
    //                 write
    //             } else {
    //                 // Inverted, no room is available
    //                 return Err(Error::InsufficientSize);
    //             }
    //         } else {
    //             if write != max {
    //                 // Some (or all) room remaining in un-inverted case
    //                 sz = min(max - write, sz);
    //                 write
    //             } else {
    //                 // Not inverted, but need to go inverted

    //                 // NOTE: We check read > 1, NOT read >= 1, because
    //                 // write must never == read in an inverted condition, since
    //                 // we will then not be able to tell if we are inverted or not
    //                 if read > 1 {
    //                     sz = min(read - 1, sz);
    //                     0
    //                 } else {
    //                     // Not invertible, no space
    //                     return Err(Error::InsufficientSize);
    //                 }
    //             }
    //         };

    //         // Safe write, only viewed by this task
    //         self.0.reserve.store(start + sz, Relaxed);

    //         let c = unsafe { (*self.0.buf.as_mut_ptr()).as_mut().as_mut_ptr() };
    //         let d = unsafe { from_raw_parts_mut(c, max) };

    //         Ok(GrantW {
    //             buf: &mut d[start..self.0.reserve.load(Relaxed)],
    //         })
    //     }
}

pub struct Consumer<'a, N>
where
    N: ArrayLength<u8>,
{
    bbq: NonNull<BBBuffer<N>>,
    pd: PhantomData<&'a ()>,
}

unsafe impl<'a, N> Send for Consumer<'a, N>
where
    N: ArrayLength<u8>
{ }

impl<'a, N> Consumer<'a, N>
where
    N: ArrayLength<u8>,
{
    /// Obtains a contiguous slice of committed bytes. This slice may not
    /// contain ALL available bytes, if the writer has wrapped around. The
    /// remaining bytes will be available after all readable bytes are
    /// released
    ///
    /// NOTE: Takes a critical section while determining the read grant.
    /// The critical section is only active for the duration of
    /// this function call.
    pub fn read(&mut self) -> Result<GrantR<N>> {
        free(|_cs| {
            let inner = unsafe { &mut self.bbq.as_mut().0 };

            if inner.read_in_progress {
                return Err(Error::GrantInProgress);
            }

            let write = inner.write;
            let last = inner.last;
            let mut read = inner.read;

            // Resolve the inverted case or end of read
            if (read == last) && (write < read) {
                read = 0;
                // This has some room for error, the other thread reads this
                // Impact to Grant:
                //   Grant checks if read < write to see if inverted. If not inverted, but
                //     no space left, Grant will initiate an inversion, but will not trigger it
                // Impact to Commit:
                //   Commit does not check read, but if Grant has started an inversion,
                //   grant could move Last to the prior write position
                // MOVING READ BACKWARDS!
                inner.read = 0;
            }

            let sz = if write < read {
                // Inverted, only believe last
                last
            } else {
                // Not inverted, only believe write
                write
            } - read;

            if sz == 0 {
                return Err(Error::InsufficientSize);
            }

            inner.read_in_progress = true;

            let c = unsafe { (*inner.buf.get()).as_ptr().cast::<u8>() };
            let d = unsafe { from_raw_parts(c.offset(read as isize), sz) };

            Ok(GrantR { buf: d, bbq: self.bbq })
        })
    }
}

// Private impls, used by Queue or Producer/Consumer
impl<N> BBBuffer<N>
where
    N: ArrayLength<u8>,
{
    /// Returns the size of the backing storage.
    ///
    /// This is the maximum number of bytes that can be stored in this queue.
    pub fn capacity(&self) -> usize {
        N::to_usize()
    }
}

impl<N> BBBuffer<N>
where
    N: ArrayLength<u8>,
{
    pub fn new() -> Self {
        Self(
            ConstBBBuffer::new(),
        )
    }
}

/// A structure representing a contiguous region of memory that
/// may be written to, and potentially "committed" to the queue
#[derive(Debug, PartialEq)]
pub struct GrantW<'a, N>
where
    N: ArrayLength<u8>
{
    buf: &'a mut [u8],
    bbq: NonNull<BBBuffer<N>>
}

/// A structure representing a contiguous region of memory that
/// may be read from, and potentially "released" (or cleared)
/// from the queue
#[derive(Debug, PartialEq)]
pub struct GrantR<'a, N>
where
    N: ArrayLength<u8>
{
    buf: &'a [u8],
    bbq: NonNull<BBBuffer<N>>
}

impl<'a, N> GrantW<'a, N>
where
    N: ArrayLength<u8>
{
    /// Finalizes a writable grant given by `grant()` or `grant_max()`.
    /// This makes the data available to be read via `read()`.
    ///
    /// If `used` is larger than the given grant, this function will panic.
    ///
    /// NOTE: Takes a critical section while commiting the grant.
    /// The critical section is only active for the duration of
    /// this function call.
    pub fn commit(mut self, used: usize) {
        self.commit_inner(used);
        forget(self);
    }

    #[inline(always)]
    fn commit_inner(&mut self, used: usize) {
        free(|_cs| {
            let inner = unsafe { &mut self.bbq.as_mut().0 };

            // Writer component. Must never write to READ,
            // be careful writing to LAST

            // Verify we are not committing more than the given
            // grant
            let len = self.buf.len();
            assert!(len >= used);

            let write = inner.write;
            inner.reserve -= len - used;

            let max = N::to_usize();
            let last = inner.last;

            // Inversion case, we have begun writing
            if (inner.reserve < write) && (write != max) {
                inner.last = write;
            } else if write > last {
                inner.last = max;
            }

            // Write must be updated AFTER last, otherwise read could think it was
            // time to invert early!
            inner.write = inner.reserve;
        })
    }
}

impl<'a, N> GrantR<'a, N>
where
    N: ArrayLength<u8>
{
    /// Release a sequence of bytes from the buffer, allowing the space
    /// to be used by later writes
    ///
    /// If `used` is larger than the given grant, this function will panic.
    ///
    /// NOTE: Takes a critical section while releasing the read grant.
    /// The critical section is only active for the duration of
    /// this function call.
    pub fn release(mut self, used: usize) {
        self.release_inner(used);
        forget(self);
    }

    #[inline(always)]
    fn release_inner(&mut self, used: usize) {
        free(|_cs| {
            let inner = unsafe { &mut self.bbq.as_mut().0 };

            assert!(used <= self.buf.len());

            // This should be fine, purely incrementing
            inner.read += used;

            inner.read_in_progress = false;
        })
    }
}

impl<'a, N> Drop for GrantW<'a, N>
where
    N: ArrayLength<u8>,
{
    fn drop(&mut self) {
        self.commit_inner(0)
    }
}

impl<'a, N> Drop for GrantR<'a, N>
where
    N: ArrayLength<u8>,
{
    fn drop(&mut self) {
        self.release_inner(0)
    }
}

impl<'a, N> Deref for GrantW<'a, N>
where
    N: ArrayLength<u8>,
{
    type Target = [u8];

    fn deref(&self) -> &Self::Target {
        self.buf
    }
}

impl<'a, N> DerefMut for GrantW<'a, N>
where
    N: ArrayLength<u8>,
{
    fn deref_mut(&mut self) -> &mut [u8] {
        self.buf
    }
}

impl<'a, N> Deref for GrantR<'a, N>
where
    N: ArrayLength<u8>,
{
    type Target = [u8];

    fn deref(&self) -> &Self::Target {
        self.buf
    }
}