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
use crate::owned_alloc::OwnedAlloc;
use crate::{
incin::Pause,
ptr::{bypass_null, check_null_align},
removable::Removable,
};
use core::{
fmt,
iter::FromIterator,
ptr::{null_mut, NonNull},
sync::atomic::{AtomicPtr, Ordering::*},
};
/// A lock-free general-purpouse queue. FIFO semanthics are fully respected.
/// It can be used as multi-producer and multi-consumer channel.
pub struct Queue<T> {
front: AtomicPtr<Node<T>>,
back: AtomicPtr<Node<T>>,
incin: SharedIncin<T>,
}
impl<T> Queue<T> {
/// Creates a new empty queue.
pub fn new() -> Self {
check_null_align::<Node<T>>();
Self::with_incin(SharedIncin::new())
}
/// Creates an empty queue using the passed shared incinerator.
pub fn with_incin(incin: SharedIncin<T>) -> Self {
let node = Node::new(Removable::empty());
let sentinel = OwnedAlloc::new(node).into_raw().as_ptr();
Self {
front: AtomicPtr::new(sentinel),
back: AtomicPtr::new(sentinel),
incin,
}
}
/// Returns the shared incinerator used by this [`Queue`].
pub fn incin(&self) -> SharedIncin<T> {
self.incin.clone()
}
/// Creates an iterator over `T`s, based on [`pop`](Queue::pop) operation of
/// the [`Queue`].
pub fn pop_iter(&self) -> PopIter<'_, T> {
PopIter { queue: self }
}
/// Pushes a value into the back of the queue. This operation is also
/// wait-free.
pub fn push(&self, item: T) {
// Pretty simple: create a node from the item.
let node = Node::new(Removable::new(item));
let alloc = OwnedAlloc::new(node);
let node_ptr = alloc.into_raw().as_ptr();
// Swap with the previously stored back.
let prev_back = self.back.swap(node_ptr, AcqRel);
unsafe {
// Updates the previous back's next field to our newly allocated
// node. This may delay the visibility of the insertion.
(*prev_back).next.store(node_ptr, Release);
}
}
/// Takes a value from the front of the queue, if it is avaible.
pub fn pop(&self) -> Option<T> {
// Pausing because of ABA problem involving remotion from linked lists.
let pause = self.incin.get_unchecked().pause();
let mut front_nnptr = unsafe {
// The pointer stored in front and back must never be null. The
// queue always have at least one node. Front and back are
// always connected.
bypass_null(self.front.load(Relaxed))
};
loop {
// This dereferral is safe because we paused the incinerator and
// only delete nodes via incinerator.
//
// We first remove the node logically.
match unsafe { front_nnptr.as_ref().item.take(AcqRel) } {
Some(val) => {
// Safe to call because we passed a pointer from the front
// which was loaded during the very same pause we are
// passing.
unsafe { self.try_clear_first(front_nnptr, &pause) };
break Some(val);
}
// Safe to call because we passed a pointer from the front
// which was loaded during the very same pause we are
// passing.
None => unsafe {
front_nnptr = self.try_clear_first(front_nnptr, &pause)?;
},
}
}
}
/// Pushes elements from the given iterable. Acts just like
/// [`Extend::extend`] but does not require mutability.
pub fn extend<I>(&self, iterable: I)
where
I: IntoIterator<Item = T>,
{
for elem in iterable {
self.push(elem);
}
}
// Returns an `Option` so we can use the try operator (?) with the function.
// This function is unsafe because passing the wrong pointer will lead to
// undefined behavior. The pointer must have been loaded from the front
// during the passed pause.
unsafe fn try_clear_first(
&self,
expected: NonNull<Node<T>>,
pause: &Pause<OwnedAlloc<Node<T>>>,
) -> Option<NonNull<Node<T>>> {
let next = expected.as_ref().next.load(Acquire);
// If this is the only node, we will not remove it. We want front and
// back to share the same node rather than having to set both to null
// when the queue is empty.
NonNull::new(next).map(|next_nnptr| {
let ptr = expected.as_ptr();
// We are not oblied to succeed. This is just cleanup and some other
// thread might do it.
match self.front.compare_exchange(ptr, next, Relaxed, Relaxed) {
Ok(_) => {
// Only deleting nodes via incinerator due to ABA problem
// and use-after-frees.
pause.add_to_incin(OwnedAlloc::from_raw(expected));
next_nnptr
}
Err(found) => {
// Safe to by-pass the check since we only store non-null
// pointers on the front.
bypass_null(found)
}
}
})
}
}
impl<T> Default for Queue<T> {
fn default() -> Self {
Self::new()
}
}
impl<T> Drop for Queue<T> {
fn drop(&mut self) {
let front = self.front.get_mut();
while let Some(nnptr) = NonNull::new(*front) {
// This is safe because we only store pointers allocated via
// `OwnedAlloc`. Also, we have exclusive access to this pointer.
let mut node = unsafe { OwnedAlloc::from_raw(nnptr) };
*front = *node.next.get_mut();
}
}
}
impl<T> FromIterator<T> for Queue<T> {
fn from_iter<I>(iterable: I) -> Self
where
I: IntoIterator<Item = T>,
{
let this = Self::new();
this.extend(iterable);
this
}
}
impl<T> Extend<T> for Queue<T> {
fn extend<I>(&mut self, iterable: I)
where
I: IntoIterator<Item = T>,
{
(*self).extend(iterable)
}
}
impl<T> Iterator for Queue<T> {
type Item = T;
fn next(&mut self) -> Option<T> {
let front = self.front.get_mut();
// Safe to by-pass it because the queue always have at least one node.
let mut front_node = unsafe { NonNull::new_unchecked(*front) };
loop {
// Safe because we allocated everything properly.
let (item, next) = unsafe {
let node_ref = front_node.as_mut();
(node_ref.item.replace(None), *node_ref.next.get_mut())
};
match (item, NonNull::new(next)) {
(Some(item), maybe_next) => {
if let Some(next) = maybe_next {
// Ok to drop it like this because we have exclusive
// reference to the queue.
unsafe { OwnedAlloc::from_raw(front_node) };
*front = next.as_ptr();
}
break Some(item);
}
(None, None) => break None,
(None, Some(next)) => {
// Ok to drop it like this because we have exclusive
// reference to the queue.
unsafe { OwnedAlloc::from_raw(front_node) };
*front = next.as_ptr();
front_node = next;
}
}
}
}
}
impl<T> fmt::Debug for Queue<T> {
fn fmt(&self, fmtr: &mut fmt::Formatter) -> fmt::Result {
write!(
fmtr,
"Queue {{ front: {:?}, back: {:?}, incin: {:?} }}",
self.front, self.back, self.incin
)
}
}
unsafe impl<T> Send for Queue<T> where T: Send {}
unsafe impl<T> Sync for Queue<T> where T: Send {}
/// An iterator based on [`pop`](Queue::pop) operation of the [`Queue`].
pub struct PopIter<'queue, T>
where
T: 'queue,
{
queue: &'queue Queue<T>,
}
impl<'queue, T> Iterator for PopIter<'queue, T> {
type Item = T;
fn next(&mut self) -> Option<Self::Item> {
self.queue.pop()
}
}
impl<'queue, T> fmt::Debug for PopIter<'queue, T> {
fn fmt(&self, fmtr: &mut fmt::Formatter) -> fmt::Result {
write!(fmtr, "PopIter {{ queue: {:?} }}", self.queue)
}
}
make_shared_incin! {
{ "[`Queue`]" }
pub SharedIncin<T> of OwnedAlloc<Node<T>>
}
impl<T> fmt::Debug for SharedIncin<T> {
fn fmt(&self, fmtr: &mut fmt::Formatter) -> fmt::Result {
write!(fmtr, "SharedIncin {{ inner: {:?} }}", self.inner)
}
}
#[repr(align(/* at least */ 2))]
struct Node<T> {
item: Removable<T>,
next: AtomicPtr<Node<T>>,
}
impl<T> Node<T> {
fn new(item: Removable<T>) -> Self {
Self {
item,
next: AtomicPtr::new(null_mut()),
}
}
}
// Testing the safety of `unsafe` in this module is done with random operations
// via fuzzing
#[cfg(test)]
mod test {
use super::*;
use alloc::sync::Arc;
use alloc::vec::Vec;
use core::sync::atomic::AtomicUsize;
#[test]
fn on_empty_first_pop_is_none() {
let queue = Queue::<usize>::new();
assert!(queue.pop().is_none());
}
#[test]
fn on_empty_last_pop_is_none() {
let queue = Queue::new();
queue.push(3);
queue.push(1234);
queue.pop();
queue.pop();
assert!(queue.pop().is_none());
}
#[test]
fn order() {
let queue = Queue::new();
queue.push(3);
queue.push(5);
queue.push(6);
assert_eq!(queue.pop(), Some(3));
assert_eq!(queue.pop(), Some(5));
assert_eq!(queue.pop(), Some(6));
}
#[test]
fn queue_iter() {
let mut queue = Queue::new();
queue.push(3);
queue.push(5);
queue.push(6);
assert_eq!(queue.next(), Some(3));
assert_eq!(queue.next(), Some(5));
assert_eq!(queue.next(), Some(6));
assert_eq!(queue.next(), None);
}
#[cfg(feature = "std")]
#[test]
fn no_data_corruption() {
use std::thread;
const NTHREAD: usize = 20;
const NITER: usize = 800;
const NMOD: usize = 55;
let queue = Arc::new(Queue::new());
let mut handles = Vec::with_capacity(NTHREAD);
let removed = Arc::new(AtomicUsize::new(0));
for i in 0..NTHREAD {
let removed = removed.clone();
let queue = queue.clone();
handles.push(thread::spawn(move || {
for j in 0..NITER {
let val = (i * NITER) + j;
queue.push(val);
if (val + 1) % NMOD == 0 {
if let Some(val) = queue.pop() {
removed.fetch_add(1, Relaxed);
assert!(val < NITER * NTHREAD);
}
}
}
}));
}
for handle in handles {
handle.join().expect("thread failed");
}
let expected = NITER * NTHREAD - removed.load(Relaxed);
let mut res = 0;
while let Some(val) = queue.pop() {
assert!(val < NITER * NTHREAD);
res += 1;
}
assert_eq!(res, expected);
}
}