mulch/
bipqueue.rs

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
//! A `BipQueue` is a SPSC lockless queue based on bi-partite circular buffers (commonly referred
//! to as Bip-Buffers). It is inspired by James Munns' `bbqueue` crate, but implemented from
//! scratch as a learning exercise for me.
//!
//! Useful resources:
//!    - [The `bbqueue` crate](https://github.com/jamesmunns/bbqueue/)
//!    - [This blog post](https://ferrous-systems.com/blog/lock-free-ring-buffer/)
//!    - [This article](https://www.codeproject.com/articles/3479/the-bip-buffer-the-circular-buffer-with-a-twist)

use core::{
    cell::UnsafeCell,
    mem::MaybeUninit,
    ops::{Deref, DerefMut},
    ptr::NonNull,
    slice,
    sync::atomic::{AtomicBool, AtomicUsize, Ordering},
};

pub struct BipQueue<const N: usize> {
    storage: UnsafeCell<MaybeUninit<[u8; N]>>,
    read: AtomicUsize,
    write: AtomicUsize,
    /// Used to mark a region as reserved for writing, but before its grant is committed.
    reserve: AtomicUsize,
    /// In the two-region scenario, this marks the end of the upper region. Data after this point
    /// is invalid and should not be read, and the reader should instead start again from the
    /// bigging of the buffer. If there is only one active region, this should be set to the size
    /// of the buffer.
    watermark: AtomicUsize,

    read_granted: AtomicBool,
    write_granted: AtomicBool,
}

unsafe impl<const N: usize> Send for BipQueue<N> {}
unsafe impl<const N: usize> Sync for BipQueue<N> {}

impl<const N: usize> BipQueue<N> {
    pub const fn new() -> BipQueue<N> {
        BipQueue {
            storage: UnsafeCell::new(MaybeUninit::uninit()),
            read: AtomicUsize::new(0),
            write: AtomicUsize::new(0),
            reserve: AtomicUsize::new(0),
            watermark: AtomicUsize::new(N),

            read_granted: AtomicBool::new(false),
            write_granted: AtomicBool::new(false),
        }
    }

    pub fn grant(&self, length: usize) -> Result<WriteGrant<'_, N>, Error> {
        if self.write_granted.swap(true, Ordering::AcqRel) {
            return Err(Error::AlreadyGranted);
        }

        let read = self.read.load(Ordering::Acquire);
        let write = self.write.load(Ordering::Acquire);

        let start = if write < read {
            /*
             * There are already two active regions. Check if there is still space available -
             * write must never catch up with read.
             */
            if (write + length) < read {
                write
            } else {
                self.write_granted.store(false, Ordering::Release);
                return Err(Error::NotEnoughSpace);
            }
        } else {
            /*
             * There is only one active region. See if we can fit it on the end, otherwise go back
             * round to the beginning.
             */
            if (write + length) <= N {
                write
            } else if length < read {
                /*
                 * There's space to create a second active region at the front of the buffer. We
                 * must make sure here not to let `write == read`, or we won't be able to tell how
                 * many regions we have active.
                 */
                0
            } else {
                self.write_granted.store(false, Ordering::Release);
                return Err(Error::NotEnoughSpace);
            }
        };

        self.reserve.store(start + length, Ordering::Release);

        /*
         * Create a slice of the granted part of the buffer. Casting through the `MaybeUninit` is
         * safe because it's `repr(transparent)`.
         */
        let grant_buffer =
            unsafe { slice::from_raw_parts_mut(self.storage.get().cast::<u8>().add(start), length) };
        Ok(WriteGrant {
            buffer: grant_buffer,
            queue: unsafe { NonNull::new_unchecked(self as *const Self as *mut Self) },
        })
    }

    pub fn read(&self) -> Result<ReadGrant<'_, N>, Error> {
        if self.read_granted.swap(true, Ordering::AcqRel) {
            return Err(Error::AlreadyGranted);
        }

        let read = self.read.load(Ordering::Acquire);
        let write = self.write.load(Ordering::Acquire);
        let watermark = self.watermark.load(Ordering::Acquire);

        if (write < read) && (read == watermark) {
            /*
             * We have two active regions, and we've finished the second one (read up to the
             * watermark). We move read to the front of the buffer.
             */
            self.read.store(0, Ordering::Release);
        }

        let length = if write < read {
            // Two active regions - read til the watermark
            watermark - read
        } else {
            // One active region - read til the point we've written up to
            write - read
        };

        if length == 0 {
            self.read_granted.store(false, Ordering::Release);
            return Err(Error::NoBytes);
        }

        /*
         * Create a slice of the readable part of the buffer. Casting through `MaybeUninit` is safe
         * because it's `repr(transparent)`.
         */
        let grant_buffer = unsafe { slice::from_raw_parts(self.storage.get().cast::<u8>().add(read), length) };
        Ok(ReadGrant {
            buffer: grant_buffer,
            queue: unsafe { NonNull::new_unchecked(self as *const Self as *mut Self) },
        })
    }
}

pub struct WriteGrant<'a, const N: usize> {
    pub buffer: &'a mut [u8],
    queue: NonNull<BipQueue<N>>,
}

impl<'a, const N: usize> WriteGrant<'a, N> {
    pub fn commit(self, written: usize) {
        let written = usize::min(written, self.buffer.len());

        let queue = unsafe { self.queue.as_ref() };
        let write = queue.write.load(Ordering::Acquire);

        // If less than the entire region was written into, reduce the reserved area.
        queue.reserve.fetch_sub(self.buffer.len() - written, Ordering::AcqRel);

        let watermark = queue.watermark.load(Ordering::Acquire);
        let new_write = queue.reserve.load(Ordering::Acquire);

        if (new_write < write) && (write != N) {
            /*
             * This write is creating a second active region, leaving bytes at the end of the
             * buffer invalid. Mark the watermark here to prevent a read from reading those bytes.
             */
            queue.watermark.store(write, Ordering::Release);
        } else if new_write > watermark {
            /*
             * We're going to write past the previous watermark. This means the lower active region
             * has cleared the higher, so we can push the watermark back to the end of the buffer.
             * A read will stop at the new `write` marker.
             */
            queue.watermark.store(N, Ordering::Release);
        }

        queue.write.store(new_write, Ordering::Release);
        queue.write_granted.store(false, Ordering::Release);
    }
}

impl<'a, const N: usize> Deref for WriteGrant<'a, N> {
    type Target = [u8];

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

impl<'a, const N: usize> DerefMut for WriteGrant<'a, N> {
    fn deref_mut(&mut self) -> &mut Self::Target {
        self.buffer
    }
}

impl<'a, const N: usize> Drop for WriteGrant<'a, N> {
    fn drop(&mut self) {
        let queue = unsafe { self.queue.as_ref() };
        queue.write_granted.store(false, Ordering::Release);
    }
}

unsafe impl<'a, const N: usize> Send for WriteGrant<'a, N> {}

pub struct ReadGrant<'a, const N: usize> {
    pub buffer: &'a [u8],
    queue: NonNull<BipQueue<N>>,
}

impl<'a, const N: usize> ReadGrant<'a, N> {
    pub fn release(self, read: usize) {
        let read = usize::min(read, self.buffer.len());
        let queue = unsafe { self.queue.as_ref() };

        queue.read.fetch_add(read, Ordering::Release);
        queue.read_granted.store(false, Ordering::Release);
    }
}

impl<'a, const N: usize> Deref for ReadGrant<'a, N> {
    type Target = [u8];

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

impl<'a, const N: usize> Drop for ReadGrant<'a, N> {
    fn drop(&mut self) {
        let queue = unsafe { self.queue.as_ref() };
        queue.read_granted.store(false, Ordering::Release);
    }
}

unsafe impl<'a, const N: usize> Send for ReadGrant<'a, N> {}

#[derive(Clone, Copy, PartialEq, Eq, Debug)]
pub enum Error {
    AlreadyGranted,
    NotEnoughSpace,
    NoBytes,
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    pub fn basic() {
        let queue: BipQueue<16> = BipQueue::new();

        {
            let write = queue.grant(4).unwrap();
            write.buffer.copy_from_slice(&[1, 2, 3, 4]);
            write.commit(4);
        }
        {
            let write = queue.grant(6).unwrap();
            write.buffer.copy_from_slice(&[5, 6, 7, 8, 9, 10]);
            write.commit(6);
        }
        {
            let read = queue.read().unwrap();
            assert_eq!(read.buffer.len(), 10);
            assert_eq!(read.buffer[0..2], [1, 2]);
            read.release(2);
        }
        {
            let write = queue.grant(1).unwrap();
            write.buffer.copy_from_slice(&[11]);
            write.commit(1);
        }
        {
            let read = queue.read().unwrap();
            assert_eq!(read.buffer.len(), 9);
            assert_eq!(read.buffer, [3, 4, 5, 6, 7, 8, 9, 10, 11]);
            read.release(9);
        }
    }
}