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
pub use super::{
NoRecv,
RecvErr::{self, *},
};
use owned_alloc::OwnedAlloc;
use ptr::check_null_align;
use std::{
fmt,
ptr::{null_mut, NonNull},
sync::atomic::{AtomicPtr, Ordering::*},
};
/// Creates an asynchronous lock-free Single-Producer-Single-Consumer (SPSC)
/// channel.
pub fn create<T>() -> (Sender<T>, Receiver<T>) {
check_null_align::<Node<T>>();
// A single empty node shared between two ends.
let alloc = OwnedAlloc::new(Node {
message: None,
next: AtomicPtr::new(null_mut()),
});
let nnptr = alloc.into_raw();
(Sender { back: nnptr }, Receiver { front: nnptr })
}
/// The `Sender` handle of a SPSC channel. Created by [`create`] function.
pub struct Sender<T> {
back: NonNull<Node<T>>,
}
impl<T> Sender<T> {
/// Sends a message and if the receiver disconnected, an error is returned.
pub fn send(&mut self, message: T) -> Result<(), NoRecv<T>> {
// First we create a node for our message.
let alloc = OwnedAlloc::new(Node {
message: Some(message),
next: AtomicPtr::new(null_mut()),
});
let nnptr = alloc.into_raw();
// This dereferral is safe because the queue will always have at least
// one node. Our back is a single node. In any case, back will always be
// present. Also, we only put valid pointers allocated via `OwnedAlloc`.
let res = unsafe {
// First we try to publish the new node through the back's next
// field. The receiver will see our changes because at some point it
// will reach our current back.
//
// We compare to null because, when disconnecting, the receiver will
// mark the lower bit of the pointer. In order words, it will be
// null | 1. We do not need to publish the new node if we receiver
// disconnected.
self.back.as_ref().next.compare_exchange(
null_mut(),
nnptr.as_ptr(),
Release,
Relaxed,
)
};
if res.is_ok() {
// If we succeeded, let's update our back so we respect the rule of
// having a single node in the back.
self.back = nnptr;
Ok(())
} else {
// If we failed, the receiver disconnected and marked the bit.
let mut alloc = unsafe { OwnedAlloc::from_raw(nnptr) };
let message = alloc.message.take().unwrap();
Err(NoRecv { message })
}
}
/// Tests if the [`Receiver`] is still connected. There are no guarantees
/// that [`send`](Sender::send) will succeed if this method returns `true`
/// because the [`Receiver`] may disconnect meanwhile.
pub fn is_connected(&self) -> bool {
// Safe because we always have at least one node, which is only dropped
// in the last side to disconnect's drop.
let back = unsafe { self.back.as_ref() };
back.next.load(Relaxed).is_null()
}
}
impl<T> Drop for Sender<T> {
fn drop(&mut self) {
// This dereferral is safe because the queue will always have at least
// one node. Also, we only put nodes allocated from `OwnedAlloc`.
let res = unsafe {
// Let's try to mark next's bit so that receiver will see we
// disconnected, if it hasn't disconnected by itself. It is ok to
// just swap, since we have only two possible values (null and
// null | 1) and we everyone will be setting to the same value
// (null | 1).
self.back
.as_ref()
.next
.swap((null_mut::<Node<T>>() as usize | 1) as *mut _, Relaxed)
};
// If the previously stored value was not null, receiver has already
// disconnected. It is safe to drop because we are the only ones that
// have a pointer to the node.
if !res.is_null() {
unsafe { OwnedAlloc::from_raw(self.back) };
}
}
}
unsafe impl<T> Send for Sender<T> where T: Send {}
unsafe impl<T> Sync for Sender<T> where T: Send {}
impl<T> fmt::Debug for Sender<T> {
fn fmt(&self, fmtr: &mut fmt::Formatter) -> fmt::Result {
fmtr.write_str("spsc::Sender")
}
}
/// The [`Receiver`] handle of a SPSC channel. Created by [`create`] function.
pub struct Receiver<T> {
front: NonNull<Node<T>>,
}
impl<T> Receiver<T> {
/// Tries to receive a message. If no message is available,
/// [`Err`]`(`[`RecvErr::NoMessage`]`)` is returned. If the sender
/// disconnected, [`Err`]`(`[`RecvErr::NoSender`]`)` is returned.
pub fn recv(&mut self) -> Result<T, RecvErr> {
loop {
// This dereferral is safe because we only put nodes allocated from
// `OwnedAlloc`.
let node = unsafe { &mut *self.front.as_ptr() };
// We will try to replace the current node with this.
let next = node.next.load(Acquire);
// First we remove a node logically.
match node.message.take() {
Some(message) => {
let cleared = (next as usize & !1) as *mut _;
// But only if we have a new node. Otherwise we will not
// remove the only node of the queue. Also, let's clear the
// bit flag so null pointers are not misused.
if let Some(nnptr) = NonNull::new(cleared) {
// This is safe because the node was allocated with
// `OwnedAlloc` and we have the only pointer to it (back
// is something else).
unsafe { OwnedAlloc::from_raw(self.front) };
self.front = nnptr;
}
break Ok(message);
},
None => {
if next as usize & 1 == 0 {
// Lower bit clean. Let's try to remove the next.
match NonNull::new(next) {
Some(nnptr) => {
// This is safe because the node was allocated
// with `OwnedAlloc` and we have the only
// pointer to it (back is something else since
// it has a single node).
unsafe { OwnedAlloc::from_raw(self.front) };
self.front = nnptr;
},
// If the next is null, we have no message and we
// will not remove this list's single node.
None => break Err(RecvErr::NoMessage),
}
} else {
// If the sender marked the lower bit of the pointer, it
// has disconnected.
break Err(RecvErr::NoSender);
}
},
}
}
}
/// Tests if the [`Sender`] is still connected. There are no guarantees
/// that [`recv`](Receiver::recv) will succeed if this method returns `true`
/// because the [`Receiver`] may disconnect meanwhile. This method may
/// also return `true` if the [`Sender`] disconnected but there are
/// messages pending in the buffer.
pub fn is_connected(&self) -> bool {
// Safe because we always have at least one node, which is only dropped
// in the last side to disconnect's drop.
let front = unsafe { self.front.as_ref() };
front.message.is_some() || front.next.load(Relaxed) as usize & 1 == 0
}
}
impl<T> Drop for Receiver<T> {
fn drop(&mut self) {
loop {
// This dereferral is safe because we only put nodes allocated from
// `OwnedAlloc`.
let next = unsafe {
// Let's try to mark next's bit so that sender will see we
// disconnected, if it hasn't disconnected by itself. It is ok
// to just swap, since we have only two possible
// values (null and null | 1) and we everyone
// will be setting to the same value (null | 1).
self.front.as_ref().next.swap(
(null_mut::<Node<T>>() as usize | 1) as *mut _,
Acquire,
)
};
// Then we check for null (success of our swap).
let next_nnptr = match NonNull::new(next) {
Some(nnptr) => nnptr,
// If it was null, no other action is required. We should not
// deallocate it because the sender still sees it through back.
None => break,
};
// It is safe to drop because we are the only ones that
// have a pointer to the node.
unsafe { OwnedAlloc::from_raw(self.front) };
// if next is marked, it is actually null | 1, but we can deallocate
// it because the sender already disconnected.
if next as usize & 1 == 1 {
break;
}
// Update the front just like in pop.
self.front = next_nnptr;
}
}
}
unsafe impl<T> Send for Receiver<T> where T: Send {}
unsafe impl<T> Sync for Receiver<T> where T: Send {}
impl<T> fmt::Debug for Receiver<T> {
fn fmt(&self, fmtr: &mut fmt::Formatter) -> fmt::Result {
fmtr.write_str("spsc::Receiver")
}
}
#[repr(align(/* at least */ 2))]
struct Node<T> {
message: Option<T>,
// lower bit is 1 for "disconnected" and 0 for "connected"
next: AtomicPtr<Node<T>>,
}
#[cfg(test)]
mod test {
use channel::spsc;
use std::thread;
#[test]
fn correct_sequence() {
const MSGS: usize = 512;
let (mut sender, mut receiver) = spsc::create::<usize>();
let thread = thread::spawn(move || {
for i in 0 .. MSGS {
loop {
match receiver.recv() {
Ok(j) => {
assert_eq!(i, j);
break;
},
Err(spsc::NoMessage) => (),
_ => unreachable!(),
}
}
}
});
for i in 0 .. MSGS {
sender.send(i).unwrap();
}
thread.join().unwrap();
}
}