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
//! Simple oneshot Channel implementation based on the one described in the book
//! of Mara Bos, but adapted to work as a signaling mechanism. Sending will
//! remain non-blocking and just overwrite the previous message.
use core::cell::UnsafeCell;
use core::future::poll_fn;
use core::mem::MaybeUninit;
use core::task::Poll;
use core::task::Waker;
struct ChannelState {
is_ready: bool, // We always stay in the same task/thread -> no atomic needed here
waker_recv: Option<Waker>,
waker_send: Option<Waker>,
}
pub struct Channel<T> {
message: UnsafeCell<MaybeUninit<T>>,
state: UnsafeCell<ChannelState>,
}
impl<T> Channel<T> {
pub fn new() -> Self {
Self {
message: UnsafeCell::new(MaybeUninit::uninit()),
state: UnsafeCell::new(ChannelState {
is_ready: false,
waker_recv: None,
waker_send: None,
}),
}
}
pub fn split(&mut self) -> (Sender<'_, T>, Receiver<'_, T>) {
*self = Self::new(); // Drop previous channel to reset state. We have exclusive access here
(Sender { channel: self }, Receiver { channel: self })
}
}
impl<T> Default for Channel<T> {
fn default() -> Self {
Self::new()
}
}
pub struct Sender<'a, T> {
channel: &'a Channel<T>,
}
impl<T> Sender<'_, T> {
/// Sends a message across the channel. Sending multiple messages before the
/// Receiver can read them, results in overwriting the previous messages.
/// Only the last one will be actually sent. This method returns whether or
/// not the previous message was overwritten
pub fn send(&self, message: T) -> bool {
// If the channel is ready, make the message drop
// Safety: The state is only accessed inside a function body and never across an
// await point. No concurrent access here (same task)
let state = unsafe { &mut *self.channel.state.get() };
let did_replace = if state.is_ready {
unsafe {
// Drop previous message
// Safety: This is ok, as we are the only ones with access to the
// Sender and there is no concurrent access from the reader
let maybe_uninit = &mut *self.channel.message.get();
core::ptr::drop_in_place(maybe_uninit.as_mut_ptr());
// Store the new message
maybe_uninit.as_mut_ptr().write(message);
// The channel is already set to be ready -> keep it this way
}
// Wake the Receiver task
if let Some(waker) = state.waker_recv.take() {
waker.wake()
}
// Signal that the channel has replaced something
true
} else {
// The channel is not yet ready -> store the message and make it ready
// Safety: We are the only one with access to the Sender and no concurrent
// access with the Receiver possible
unsafe {
let maybe_uninit = &mut *self.channel.message.get();
maybe_uninit.as_mut_ptr().write(message);
}
// Signal that the channel was empty before
false
};
// Wake the Receiver task
state.is_ready = true;
if let Some(waker) = state.waker_recv.take() {
waker.wake()
}
// Did we replace the inner message or not
did_replace
}
/// Check if there is an item in the channel
pub fn has_item(&self) -> bool {
let state = unsafe { &mut *self.channel.state.get() };
state.is_ready
}
/// Wait before sending
pub async fn send_async(&self, message: T) {
poll_fn(|cx| {
if self.has_item() {
// Replace waker if necessary
// Safety: we are the only ones that have access to the state at this moment
let state = unsafe { &mut *self.channel.state.get() };
let new_waker = cx.waker();
state.waker_send = match state.waker_send.take() {
Some(mut waker) => {
if new_waker.will_wake(&waker) {
waker.clone_from(new_waker);
Some(waker)
} else {
// We have a different waker now, wake the previous one before replacing
// it
waker.wake();
Some(new_waker.clone())
}
}
None => Some(new_waker.clone()),
};
Poll::Pending
} else {
Poll::Ready(())
}
})
.await;
self.send(message);
}
}
pub struct Receiver<'a, T> {
channel: &'a Channel<T>,
}
impl<T> Receiver<'_, T> {
pub async fn receive(&self) -> T {
poll_fn(|cx| {
// Safety: We only access the state in the bounds of this call and never across
// an await point
let state = unsafe { &mut *self.channel.state.get() };
if !state.is_ready {
// Not yet ready, store/replace the context
match &mut state.waker_recv {
Some(waker) => waker.clone_from(cx.waker()),
waker @ None => *waker = Some(cx.waker().clone()),
}
Poll::Pending
} else {
// Safety: We have a message, and exclusive access to the channel as there is no
// concurrent access possible
let message = unsafe {
let maybe_uninit = &mut *self.channel.message.get();
maybe_uninit.assume_init_read()
};
// Reset the state, such that we can send again
state.is_ready = false;
// Wake if possible the waker for sending
if let Some(waker) = state.waker_send.take() {
waker.wake();
}
Poll::Ready(message)
}
})
.await
}
/// Check if there is an item in the channel
#[allow(dead_code)]
pub fn has_item(&self) -> bool {
let state = unsafe { &mut *self.channel.state.get() };
state.is_ready
}
}
#[cfg(test)]
mod tests {
use pollster::FutureExt as _;
use crate::sync::yield_now;
use crate::sync::{join::join, yield_now::yield_now};
use super::Channel;
#[test]
pub fn test_channel_no_concurrency() {
async {
let mut channel = Channel::new();
let (send, recv) = channel.split();
send.send(1);
assert_eq!(recv.receive().await, 1);
}
.block_on();
}
#[test]
pub fn test_channel_join_concurrency() {
async {
let mut channel = Channel::new();
let (send, recv) = channel.split();
join(
async {
for i in 0..10 {
send.send(i);
yield_now().await;
}
},
async {
for i in 0..10 {
assert_eq!(recv.receive().await, i);
}
},
)
.await;
}
.block_on();
}
#[test]
/// Check with Miri whether or not drop is called correctly. If true, then
/// all heap allocation should be deallocated correctly
pub fn test_drop_by_leaking() {
async {
let mut channel = Channel::new();
let (send, recv) = channel.split();
send.send(Box::new(0));
send.send(Box::new(1));
send.send(Box::new(2));
assert_eq!(*recv.receive().await, 2);
}
.block_on()
}
#[test]
pub fn test_multiple_receivers() {
async {
let mut channel = Channel::new();
let (send, recv) = channel.split();
for _ in 0..10 {
join(
join(
async {
assert_eq!(recv.receive().await, 0);
},
async {
assert_eq!(recv.receive().await, 1);
},
),
async {
for _ in 0..10 {
yield_now::yield_now().await;
}
send.send(0);
yield_now::yield_now().await;
send.send(1);
},
)
.await;
}
}
.block_on()
}
#[test]
pub fn test_multiple_channels_at_once() {
async {
let mut channel1 = Channel::new();
let (tx1, rx1) = channel1.split();
let mut channel2 = Channel::new();
let (tx2, rx2) = channel2.split();
join(
async {
tx1.send_async(0).await;
tx2.send_async(0).await;
tx1.send_async(1).await;
tx2.send_async(1).await;
},
async {
for _ in 0..10 {
yield_now::yield_now().await;
}
join(
async {
assert_eq!(rx1.receive().await, 0);
assert_eq!(rx2.receive().await, 0);
},
async {
assert_eq!(rx1.receive().await, 1);
assert_eq!(rx2.receive().await, 1);
},
)
.await;
},
)
.await;
}
.block_on()
}
}