/
buffer.rs
348 lines (302 loc) · 11.2 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
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
use alloc::boxed::Box;
use core::sync::atomic::{self, AtomicUsize, Ordering};
use core::{cell::UnsafeCell, mem::MaybeUninit};
use crossbeam_utils::{atomic::AtomicCell, Backoff, CachePadded};
use derivative::Derivative;
struct Slot<T> {
// If the stamp equals the tail, this node will be next written to.
// If it equals head + 1, this node will be next read from.
stamp: AtomicUsize,
value: UnsafeCell<MaybeUninit<T>>,
}
impl<T> Slot<T> {
fn new(stamp: usize) -> Self {
Slot {
stamp: AtomicUsize::new(stamp),
value: UnsafeCell::new(MaybeUninit::uninit()),
}
}
}
pub struct AtomicQueue<T> {
head: CachePadded<AtomicUsize>,
tail: CachePadded<AtomicUsize>,
buffer: Box<[CachePadded<Slot<T>>]>,
one_lap: usize,
}
unsafe impl<T: Send> Sync for AtomicQueue<T> {}
unsafe impl<T: Send> Send for AtomicQueue<T> {}
impl<T> AtomicQueue<T> {
fn new(capacity: usize) -> AtomicQueue<T> {
AtomicQueue {
buffer: (0..capacity).map(Slot::new).map(CachePadded::new).collect(),
head: Default::default(),
tail: Default::default(),
one_lap: (capacity + 1).next_power_of_two(),
}
}
#[inline]
fn capacity(&self) -> usize {
self.buffer.len()
}
#[inline]
fn get(&self, cursor: usize) -> &Slot<T> {
let index = cursor & (self.one_lap - 1);
debug_assert!(index < self.capacity());
unsafe { self.buffer.get_unchecked(index) }
}
#[inline]
fn advance(&self, cursor: usize) -> usize {
let index = cursor & (self.one_lap - 1);
let lap = cursor & !(self.one_lap - 1);
if index + 1 < self.capacity() {
// Same lap, incremented index.
// Set to `{ lap: lap, index: index + 1 }`.
cursor + 1
} else {
// One lap forward, index wraps around to zero.
// Set to `{ lap: lap.wrapping_add(1), index: 0 }`.
lap.wrapping_add(self.one_lap)
}
}
fn push_or_swap(&self, value: T) -> Option<T> {
let backoff = Backoff::new();
let mut tail = self.tail.load(Ordering::Relaxed);
loop {
let new_tail = self.advance(tail);
let slot = self.get(tail);
let stamp = slot.stamp.load(Ordering::Acquire);
// If the tail and the stamp match, we may attempt to push.
if stamp == tail {
// Try moving the tail.
match self.tail.compare_exchange_weak(
tail,
new_tail,
Ordering::SeqCst,
Ordering::Relaxed,
) {
Ok(_) => {
// Write the value into the slot.
unsafe { slot.value.get().write(MaybeUninit::new(value)) };
slot.stamp.store(tail + 1, Ordering::Release);
return None;
}
Err(t) => {
tail = t;
backoff.spin();
}
}
} else if stamp.wrapping_add(self.one_lap) == tail + 1 {
atomic::fence(Ordering::SeqCst);
let head = self.head.load(Ordering::Relaxed);
// If the head lags one lap behind the tail as well, the queue is full.
if head.wrapping_add(self.one_lap) == tail {
let new_head = new_tail.wrapping_sub(self.one_lap);
// Try moving the head.
if self
.head
.compare_exchange_weak(head, new_head, Ordering::SeqCst, Ordering::Relaxed)
.is_ok()
{
// Move the tail.
debug_assert_eq!(self.tail.load(Ordering::SeqCst), tail);
self.tail.store(new_tail, Ordering::SeqCst);
// Replace the value in the slot.
let new = MaybeUninit::new(value);
let old = unsafe { slot.value.get().replace(new).assume_init() };
slot.stamp.store(tail + 1, Ordering::Release);
return Some(old);
}
}
backoff.spin();
tail = self.tail.load(Ordering::Relaxed);
} else {
backoff.snooze();
tail = self.tail.load(Ordering::Relaxed);
}
}
}
fn pop(&self) -> Option<T> {
let backoff = Backoff::new();
let mut head = self.head.load(Ordering::Relaxed);
loop {
let slot = self.get(head);
let stamp = slot.stamp.load(Ordering::Acquire);
// If the the stamp is ahead of the head by 1, we may attempt to pop.
if stamp == head + 1 {
let new_head = self.advance(head);
match self.head.compare_exchange_weak(
head,
new_head,
Ordering::SeqCst,
Ordering::Relaxed,
) {
Ok(_) => {
// Read the value from the slot.
let msg = unsafe { slot.value.get().read().assume_init() };
slot.stamp
.store(head.wrapping_add(self.one_lap), Ordering::Release);
return Some(msg);
}
Err(h) => {
head = h;
backoff.spin();
}
}
} else if stamp == head {
atomic::fence(Ordering::SeqCst);
let tail = self.tail.load(Ordering::Relaxed);
// If the tail equals the head, that means the channel is empty.
if tail == head {
return None;
}
backoff.spin();
head = self.head.load(Ordering::Relaxed);
} else {
backoff.snooze();
head = self.head.load(Ordering::Relaxed);
}
}
}
}
impl<T> Drop for AtomicQueue<T> {
fn drop(&mut self) {
let mut cursor = self.head.load(Ordering::Relaxed);
let end = self.tail.load(Ordering::Relaxed);
// Loop over all slots that hold a message and drop them.
while cursor != end {
let slot = self.get(cursor);
unsafe { (&mut *slot.value.get()).as_mut_ptr().drop_in_place() };
cursor = self.advance(cursor);
}
}
}
#[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")] AtomicQueue<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(AtomicQueue::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, value: T) -> Option<T> {
match self {
RingBuffer::Atomic(c) => c.swap(Some(value)),
RingBuffer::Boxed(b) => Some(*b.swap(Some(Box::new(value)))?),
RingBuffer::Queue(q) => q.push_or_swap(value),
}
}
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::{collections::BinaryHeap, sync::Arc, vec::Vec};
use core::{iter, mem::discriminant};
use futures::future::try_join_all;
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(AtomicQueue::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(AtomicQueue::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(
#[strategy(1..=10usize)] capacity: usize,
#[any(size_range(#capacity..=10).lift())] items: Vec<char>,
) {
let buffer = RingBuffer::new(capacity);
for &item in &items[..capacity] {
assert_eq!(buffer.push(item), None);
}
for (i, &item) in (0..(items.len() - capacity)).zip(&items[capacity..]) {
assert_eq!(buffer.push(item), Some(items[i]));
}
for &item in &items[(items.len() - capacity)..] {
assert_eq!(buffer.pop(), Some(item));
}
}
#[proptest]
fn buffer_is_linearizable(
#[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));
let items = rt.block_on(async {
try_join_all(iter::repeat(buffer).enumerate().take(n).map(|(i, b)| {
spawn_blocking(move || match b.push(i) {
None => b.pop(),
item => item,
})
}))
.await
})?;
let sorted = items
.into_iter()
.flatten()
.collect::<BinaryHeap<_>>()
.into_sorted_vec();
assert_eq!(sorted, (0..n).collect::<Vec<_>>());
}
}