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
use alloc::boxed::Box;
use core::{
cell::UnsafeCell,
hint, ptr,
sync::atomic::{AtomicPtr, Ordering},
};
struct Node<T> {
next: AtomicPtr<Self>,
inner: Option<T>,
}
pub(crate) enum NodeData<T> {
Empty,
Data(T),
Inconsistent,
}
pub(crate) struct Queue<T> {
head: AtomicPtr<Node<T>>,
tail: UnsafeCell<*mut Node<T>>,
}
unsafe impl<T: Send> Send for Queue<T> {}
unsafe impl<T: Sync> Sync for Queue<T> {}
impl<T> Node<T> {
const fn new_null() -> *mut Self {
ptr::null_mut::<Self>()
}
unsafe fn new(value: Option<T>) -> *mut Self {
Box::into_raw(Box::new(Self {
next: AtomicPtr::new(ptr::null_mut()),
inner: value,
}))
}
}
impl<T> Queue<T> {
pub(crate) const fn new_null() -> Self {
let node = Node::new_null();
Queue {
head: AtomicPtr::new(node),
tail: UnsafeCell::new(node),
}
}
pub(crate) fn assume_init(&self) -> bool {
let tail = unsafe { *self.tail.get() };
if self.head.load(Ordering::Acquire) == tail {
if tail.is_null() {
let node = unsafe { Node::new(None) };
self.head.store(node, Ordering::Release);
unsafe {
*self.tail.get() = node;
}
}
}
true
}
pub(crate) fn new() -> Self {
let node = unsafe { Node::new(None) };
Queue {
head: AtomicPtr::new(node),
tail: UnsafeCell::new(node),
}
}
pub(crate) fn push(&self, value: T) {
unsafe {
let node = Node::new(Some(value));
let dest = self.head.swap(node, Ordering::AcqRel);
(*dest).next.store(node, Ordering::Release);
}
}
pub(crate) unsafe fn try_pop(&self) -> NodeData<T> {
let tail = *self.tail.get();
let next = (*tail).next.load(Ordering::Acquire);
if !next.is_null() {
*self.tail.get() = next;
assert!((*tail).inner.is_none());
assert!((*next).inner.is_some());
let ret = (*next).inner.take().unwrap();
drop(Box::from_raw(tail));
return NodeData::Data(ret);
}
if self.head.load(Ordering::Acquire) == tail {
NodeData::Empty
} else {
NodeData::Inconsistent
}
}
pub(crate) unsafe fn pop(&self) -> Option<T> {
loop {
match self.try_pop() {
NodeData::Empty => return None,
NodeData::Data(node) => return Some(node),
NodeData::Inconsistent => {
hint::spin_loop();
}
}
}
}
}
impl<T> Drop for Queue<T> {
fn drop(&mut self) {
unsafe {
let mut tail = *self.tail.get();
while !tail.is_null() {
let next = (*tail).next.load(Ordering::Relaxed);
drop(Box::from_raw(tail));
tail = next;
}
}
}
}
#[test]
fn test_queue() {
struct Se {
state: i32,
}
let que: Queue<Se> = Queue::new_null();
let b = que.assume_init();
assert!(b);
assert!(unsafe { que.pop() }.is_none());
let se = Se { state: 0 };
que.push(se);
let se = Se { state: 1 };
que.push(se);
let data = unsafe { que.pop() };
assert!(data.is_some());
assert_eq!(data.unwrap().state, 0);
let data = unsafe { que.pop() };
assert!(data.is_some());
assert_eq!(data.unwrap().state, 1);
}