-
Notifications
You must be signed in to change notification settings - Fork 5
/
buffer.rs
168 lines (145 loc) · 5 KB
/
buffer.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
use alloc::boxed::Box;
use crossbeam_queue::ArrayQueue;
use crossbeam_utils::atomic::AtomicCell;
use derivative::Derivative;
#[derive(Derivative)]
#[derivative(Debug)]
#[allow(clippy::large_enum_variant)]
pub(super) enum RingBuffer<T> {
Atomic(#[derivative(Debug = "ignore")] AtomicCell<Option<T>>),
Boxed(#[derivative(Debug = "ignore")] AtomicCell<Option<Box<T>>>),
Queue(#[derivative(Debug = "ignore")] ArrayQueue<T>),
}
impl<T> RingBuffer<T> {
pub(super) fn new(capacity: usize) -> Self {
assert!(capacity > 0, "capacity must be non-zero");
if capacity == 1 && AtomicCell::<Option<T>>::is_lock_free() {
RingBuffer::Atomic(AtomicCell::new(None))
} else if capacity == 1 {
debug_assert!(AtomicCell::<Option<Box<T>>>::is_lock_free());
RingBuffer::Boxed(AtomicCell::new(None))
} else {
RingBuffer::Queue(ArrayQueue::new(capacity))
}
}
#[cfg(test)]
pub(super) fn capacity(&self) -> usize {
match self {
RingBuffer::Atomic(_) => 1,
RingBuffer::Boxed(_) => 1,
RingBuffer::Queue(q) => q.capacity(),
}
}
pub(super) fn push(&self, mut value: T) {
match self {
RingBuffer::Atomic(c) => {
c.store(Some(value));
}
RingBuffer::Boxed(b) => {
b.store(Some(Box::new(value)));
}
RingBuffer::Queue(q) => {
while let Err(v) = q.push(value) {
self.pop();
value = v;
}
}
}
}
pub(super) fn pop(&self) -> Option<T> {
match self {
RingBuffer::Atomic(c) => c.take(),
RingBuffer::Boxed(b) => Some(*b.take()?),
RingBuffer::Queue(q) => q.pop(),
}
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::{RingReceiver, RingSender};
use alloc::{sync::Arc, vec::Vec};
use core::{cmp::max, mem::discriminant};
use futures::{future::try_join, prelude::*, stream::repeat};
use proptest::collection::size_range;
use test_strategy::proptest;
use tokio::{runtime, task::spawn_blocking};
#[should_panic]
#[proptest]
fn new_panics_if_capacity_is_zero() {
RingBuffer::<()>::new(0);
}
#[proptest]
fn new_uses_atomic_cell_when_capacity_is_one() {
assert_eq!(
discriminant(&RingBuffer::<[char; 1]>::new(1)),
discriminant(&RingBuffer::Atomic(Default::default()))
);
assert_eq!(
discriminant(&RingBuffer::<[char; 1]>::new(2)),
discriminant(&RingBuffer::Queue(ArrayQueue::new(2)))
);
assert_eq!(
discriminant(&RingBuffer::<[char; 4]>::new(1)),
discriminant(&RingBuffer::Boxed(Default::default()))
);
assert_eq!(
discriminant(&RingBuffer::<[char; 4]>::new(2)),
discriminant(&RingBuffer::Queue(ArrayQueue::new(2)))
);
assert_eq!(
discriminant(&RingBuffer::<RingSender<()>>::new(1)),
discriminant(&RingBuffer::Atomic(Default::default()))
);
assert_eq!(
discriminant(&RingBuffer::<RingReceiver<()>>::new(1)),
discriminant(&RingBuffer::Atomic(Default::default()))
);
}
#[proptest]
fn capacity_returns_the_maximum_buffer_size(#[strategy(1..=10usize)] capacity: usize) {
assert_eq!(RingBuffer::<[char; 1]>::new(capacity).capacity(), capacity);
assert_eq!(RingBuffer::<[char; 4]>::new(capacity).capacity(), capacity);
}
#[proptest]
fn oldest_items_are_overwritten_on_overflow(
#[any(size_range(1..=10).lift())] items: Vec<char>,
#[strategy(1..=10usize)] capacity: usize,
) {
let buffer = RingBuffer::new(capacity);
for &item in &items {
buffer.push(item);
}
for &item in items.iter().skip(max(items.len(), capacity) - capacity) {
assert_eq!(buffer.pop(), Some(item));
}
for _ in items.len()..max(items.len(), capacity) {
assert_eq!(buffer.pop(), None);
}
}
#[cfg(not(miri))] // https://github.com/rust-lang/miri/issues/1388
#[proptest]
fn buffer_is_thread_safe(
#[strategy(1..=10usize)] m: usize,
#[strategy(1..=10usize)] n: usize,
#[strategy(1..=10usize)] capacity: usize,
) {
let rt = runtime::Builder::new_multi_thread().build()?;
let buffer = Arc::new(RingBuffer::new(capacity));
rt.block_on(try_join(
repeat(buffer.clone())
.enumerate()
.take(m)
.map(Ok)
.try_for_each_concurrent(None, |(item, b)| spawn_blocking(move || b.push(item))),
repeat(buffer)
.take(n)
.map(Ok)
.try_for_each_concurrent(None, |b| {
spawn_blocking(move || {
b.pop();
})
}),
))?;
}
}