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
#[cfg(test)]
mod tests;

use std::cmp;
use std::fmt;
use std::sync::atomic::{AtomicUsize, Ordering};
use std::sync::{Arc, Condvar, Mutex};

/// Managment interface for the ring buffer.
pub trait RB<T: Clone + Copy + Default> {
    /// Resets the whole buffer to the default value of type `T`.
    /// The buffer is empty after this call.
    fn clear(&self);
    /// Creates a *producer* view inside the buffer.
    fn producer(&self) -> Producer<T>;
    /// Creates a *consumer* view inside the buffer.
    fn consumer(&self) -> Consumer<T>;
}

/// RbInspector provides non-modifying operations on the ring buffer.
pub trait RbInspector {
    /// Returns true if the buffer is empty.
    fn is_empty(&self) -> bool;
    /// Returns true if the buffer is full.
    fn is_full(&self) -> bool;
    /// Returns the total capacity of the ring buffer.
    /// This is the size with which the buffer was initialized.
    fn capacity(&self) -> usize;
    /// Returns the number of values that can be written until the buffer until it is full.
    fn slots_free(&self) -> usize;
    /// Returns the number of values from the buffer that are available to read.
    fn count(&self) -> usize;
}

/// Defines *write* methods for a producer view.
pub trait RbProducer<T> {
    /// Stores the given slice of data into the ring buffer.
    /// Returns the number of written elements or an error.
    ///
    /// Possible errors:
    ///
    /// - `RbError::Full`
    fn write(&self, &[T]) -> Result<usize>;
    /// Works analog to `write` but blocks until there are free slots in the ring buffer.
    /// The number of actual blocks written is returned in the `Option` value.
    ///
    /// Returns `None` if the given slice has zero length.
    fn write_blocking(&self, &[T]) -> Option<usize>;
}

/// Defines *read* methods for a consumer view.
pub trait RbConsumer<T> {
    /// Skips all pending values.
    /// Technically it sets the consumer's read pointer to the position
    /// of the producer's write pointer.
    ///
    /// Returns the number of skipped elements.
    ///
    /// Possible errors:
    ///
    /// - `RbError::Empty` no pending elements
    fn skip_pending(&self) -> Result<usize>;
    /// Skips `cnt` number of elements.
    ///
    /// Returns the number of skipped elements.
    ///
    /// Possible errors:
    ///
    /// - `RbError::Empty` no pending elements
    fn skip(&self, cnt: usize) -> Result<usize>;
    /// Fills the given slice with values or, if the buffer is empty, does not modify it.
    /// This method does not change the state of the buffer, this means that the read pointer
    /// isn't changed if you call `get`. Consecutive calls to this method are idempotent, i.e. they
    /// will fill the given slice with the same data.
    /// Using `get` can be beneficial to `read` when a successive call has failed and you want to
    /// try again with same data. You can use `skip` to move the read pointer i.e. mark the values
    /// as read after the call succeeded.
    ///
    /// Returns the number of written values or an error.
    ///
    /// Possible errors:
    ///
    /// - RbError::Empty
    fn get(&self, &mut [T]) -> Result<usize>;
    /// Fills the given slice with values or, if the buffer is empty, does not modify it.
    /// Returns the number of written values or an error.
    ///
    /// Possible errors:
    ///
    /// - RbError::Empty
    fn read(&self, &mut [T]) -> Result<usize>;
    /// Works analog to `read` but blocks until it can read elements to fill
    /// the given buffer slice.
    /// The number of blocks read is not necessarily equal to the length of the given buffer slice,
    /// the exact number is returned in the `Option` value.
    ///
    /// Returns `None` if the given slice has zero length.
    fn read_blocking(&self, &mut [T]) -> Option<usize>;
}

/// Ring buffer errors.
#[derive(Debug)]
pub enum RbError {
    Full,
    Empty,
}
impl fmt::Display for RbError {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        match self {
            &RbError::Full => write!(f, "No free slots in the buffer"),
            &RbError::Empty => write!(f, "Buffer is empty"),
        }
    }
}

/// Result type used inside the module.
pub type Result<T> = ::std::result::Result<T, RbError>;

struct Inspector {
    read_pos: Arc<AtomicUsize>,
    write_pos: Arc<AtomicUsize>,
    size: usize,
}

/// A *thread-safe* Single-Producer-Single-Consumer RingBuffer
///
/// - blocking and non-blocking IO
/// - mutually exclusive access for producer and consumer
/// - no use of `unsafe`
/// - never under- or overflows
///
/// ```
/// use std::thread;
/// use rb::*;
///
/// let rb = SpscRb::new(1024);
/// let (prod, cons) = (rb.producer(), rb.consumer());
/// thread::spawn(move || {
///     let gen = || {(-16..16+1).cycle().map(|x| x as f32/16.0)};
///     loop {
///         let data = gen().take(32).collect::<Vec<f32>>();
///         prod.write(&data).unwrap();
///     }
/// });
/// let mut data = Vec::with_capacity(1024);
/// let mut buf = [0.0f32; 256];
/// while data.len() < 1024 {
///     let cnt = cons.read_blocking(&mut buf).unwrap();
///     data.extend_from_slice(&buf[..cnt]);
/// }
/// ```
pub struct SpscRb<T> {
    buf: Arc<Mutex<Vec<T>>>,
    inspector: Arc<Inspector>,
    slots_free: Arc<Condvar>,
    data_available: Arc<Condvar>,
}

impl<T: Clone + Copy + Default> SpscRb<T> {
    pub fn new(size: usize) -> Self {
        let (read_pos, write_pos) = (Arc::new(AtomicUsize::new(0)), Arc::new(AtomicUsize::new(0)));
        SpscRb {
            buf: Arc::new(Mutex::new(vec![T::default(); size + 1])),
            slots_free: Arc::new(Condvar::new()),
            data_available: Arc::new(Condvar::new()),
            // the additional element is used to distinct between empty and full state
            inspector: Arc::new(Inspector {
                read_pos: read_pos.clone(),
                write_pos: write_pos.clone(),
                size: size + 1,
            }),
        }
    }
}

impl<T: Clone + Copy + Default> RB<T> for SpscRb<T> {
    fn clear(&self) {
        let mut buf = self.buf.lock().unwrap();
        buf.iter_mut().map(|_| T::default()).count();
        self.inspector.read_pos.store(0, Ordering::Relaxed);
        self.inspector.write_pos.store(0, Ordering::Relaxed);
    }

    fn producer(&self) -> Producer<T> {
        Producer {
            buf: self.buf.clone(),
            inspector: self.inspector.clone(),
            slots_free: self.slots_free.clone(),
            data_available: self.data_available.clone(),
        }
    }

    fn consumer(&self) -> Consumer<T> {
        Consumer {
            buf: self.buf.clone(),
            inspector: self.inspector.clone(),
            slots_free: self.slots_free.clone(),
            data_available: self.data_available.clone(),
        }
    }
}

impl<T: Clone + Copy + Default> RbInspector for SpscRb<T> {
    fn is_empty(&self) -> bool {
        self.inspector.is_empty()
    }
    fn is_full(&self) -> bool {
        self.inspector.is_full()
    }
    fn capacity(&self) -> usize {
        self.inspector.capacity()
    }
    fn slots_free(&self) -> usize {
        self.inspector.slots_free()
    }
    fn count(&self) -> usize {
        self.inspector.count()
    }
}

impl RbInspector for Inspector {
    #[inline(always)]
    fn is_empty(&self) -> bool {
        self.slots_free() == self.capacity()
    }

    #[inline(always)]
    fn is_full(&self) -> bool {
        self.slots_free() == 0
    }

    #[inline(always)]
    fn capacity(&self) -> usize {
        self.size - 1
    }

    #[inline(always)]
    fn slots_free(&self) -> usize {
        let wr_pos = self.write_pos.load(Ordering::Relaxed);
        let re_pos = self.read_pos.load(Ordering::Relaxed);
        match wr_pos < re_pos {
            true => re_pos - wr_pos - 1,
            false => self.capacity() - wr_pos + re_pos,
        }
    }

    #[inline(always)]
    fn count(&self) -> usize {
        self.capacity() - self.slots_free()
    }
}

/// Producer view into the ring buffer.
pub struct Producer<T> {
    buf: Arc<Mutex<Vec<T>>>,
    inspector: Arc<Inspector>,
    slots_free: Arc<Condvar>,
    data_available: Arc<Condvar>,
}

/// Consumer view into the ring buffer.
pub struct Consumer<T> {
    buf: Arc<Mutex<Vec<T>>>,
    inspector: Arc<Inspector>,
    slots_free: Arc<Condvar>,
    data_available: Arc<Condvar>,
}

impl<T: Clone + Copy> RbProducer<T> for Producer<T> {
    fn write(&self, data: &[T]) -> Result<usize> {
        if data.len() == 0 {
            return Ok(0);
        }
        if self.inspector.is_full() {
            return Err(RbError::Full);
        }
        let cnt = cmp::min(data.len(), self.inspector.slots_free());
        let mut buf = self.buf.lock().unwrap();
        let buf_len = buf.len();
        let wr_pos = self.inspector.write_pos.load(Ordering::Relaxed);

        if (wr_pos + cnt) < buf_len {
            buf[wr_pos..wr_pos + cnt].copy_from_slice(&data[..cnt]);
        } else {
            let d = buf_len - wr_pos;
            buf[wr_pos..].copy_from_slice(&data[..d]);
            buf[..(cnt - d)].copy_from_slice(&data[d..cnt]);
        }
        self.inspector
            .write_pos
            .store((wr_pos + cnt) % buf_len, Ordering::Relaxed);

        self.data_available.notify_one();
        return Ok(cnt);
    }

    fn write_blocking(&self, data: &[T]) -> Option<usize> {
        if data.len() == 0 {
            return None;
        }
        let guard = self.buf.lock().unwrap();
        let mut buf = if self.inspector.is_full() {
            self.slots_free.wait(guard).unwrap()
        } else {
            guard
        };
        let buf_len = buf.len();
        let data_len = data.len();
        let wr_pos = self.inspector.write_pos.load(Ordering::Relaxed);
        let cnt = cmp::min(data_len, self.inspector.slots_free());

        if (wr_pos + cnt) < buf_len {
            buf[wr_pos..wr_pos + cnt].copy_from_slice(&data[..cnt]);
        } else {
            let d = buf_len - wr_pos;
            buf[wr_pos..].copy_from_slice(&data[..d]);
            buf[..(cnt - d)].copy_from_slice(&data[d..cnt]);
        }
        self.inspector
            .write_pos
            .store((wr_pos + cnt) % buf_len, Ordering::Relaxed);

        self.data_available.notify_one();
        return Some(cnt);
    }
}

impl<T: Clone + Copy> RbConsumer<T> for Consumer<T> {
    fn skip_pending(&self) -> Result<usize> {
        if self.inspector.is_empty() {
            Err(RbError::Empty)
        } else {
            // TODO check Order value
            let write_pos = self.inspector.write_pos.load(Ordering::Relaxed);
            let count = self.inspector.count();
            self.inspector.read_pos.store(write_pos, Ordering::Relaxed);
            Ok(count)
        }
    }

    fn skip(&self, cnt: usize) -> Result<usize> {
        if self.inspector.is_empty() {
            Err(RbError::Empty)
        } else {
            let count = cmp::min(cnt, self.inspector.count());
            let prev_read_pos = self.inspector.read_pos.load(Ordering::Relaxed);
            self.inspector.read_pos.store(
                (prev_read_pos + count) % self.inspector.capacity(),
                Ordering::Relaxed,
            );
            Ok(count)
        }
    }

    fn get(&self, data: &mut [T]) -> Result<usize> {
        if data.len() == 0 {
            return Ok(0);
        }
        if self.inspector.is_empty() {
            return Err(RbError::Empty);
        }
        let cnt = cmp::min(data.len(), self.inspector.count());
        let buf = self.buf.lock().unwrap();
        let buf_len = buf.len();
        let re_pos = self.inspector.read_pos.load(Ordering::Relaxed);

        if (re_pos + cnt) < buf_len {
            data[..cnt].copy_from_slice(&buf[re_pos..re_pos + cnt]);
        } else {
            let d = buf_len - re_pos;
            data[..d].copy_from_slice(&buf[re_pos..]);
            data[d..cnt].copy_from_slice(&buf[..(cnt - d)]);
        }

        Ok(cnt)
    }

    fn read(&self, data: &mut [T]) -> Result<usize> {
        if data.len() == 0 {
            return Ok(0);
        }
        if self.inspector.is_empty() {
            return Err(RbError::Empty);
        }
        let cnt = cmp::min(data.len(), self.inspector.count());
        let buf = self.buf.lock().unwrap();
        let buf_len = buf.len();
        let re_pos = self.inspector.read_pos.load(Ordering::Relaxed);

        if (re_pos + cnt) < buf_len {
            data[..cnt].copy_from_slice(&buf[re_pos..re_pos + cnt]);
        } else {
            let d = buf_len - re_pos;
            data[..d].copy_from_slice(&buf[re_pos..]);
            data[d..cnt].copy_from_slice(&buf[..(cnt - d)]);
        }

        // TODO: Notify all? empty->slots_free
        self.inspector
            .read_pos
            .store((re_pos + cnt) % buf_len, Ordering::Relaxed);
        self.slots_free.notify_one();
        Ok(cnt)
    }

    fn read_blocking(&self, data: &mut [T]) -> Option<usize> {
        if data.len() == 0 {
            return None;
        }
        let guard = self.buf.lock().unwrap();
        let buf = if self.inspector.is_empty() {
            self.data_available.wait(guard).unwrap()
        } else {
            guard
        };
        let buf_len = buf.len();
        let cnt = cmp::min(data.len(), self.inspector.count());
        let re_pos = self.inspector.read_pos.load(Ordering::Relaxed);

        if (re_pos + cnt) < buf_len {
            data[..cnt].copy_from_slice(&buf[re_pos..re_pos + cnt]);
        } else {
            let d = buf_len - re_pos;
            data[..d].copy_from_slice(&buf[re_pos..]);
            data[d..cnt].copy_from_slice(&buf[..(cnt - d)]);
        }

        self.inspector
            .read_pos
            .store((re_pos + cnt) % buf_len, Ordering::Relaxed);
        self.slots_free.notify_one();
        Some(cnt)
    }
}