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
mod receiver;
mod sender;
pub use self::{
receiver::Receiver,
sender::{SendError, Sender},
};
use crate::sync::spsc::{SpscInner, SpscInnerErr};
use alloc::sync::Arc;
use core::{
cell::UnsafeCell,
mem::{size_of, MaybeUninit},
sync::atomic::{AtomicUsize, Ordering},
task::Waker,
};
pub const MAX_CAPACITY: usize = 1 << size_of::<usize>() as u32 * 8 - OPTION_BITS;
#[allow(clippy::identity_op)]
const TX_WAKER_STORED: usize = 1 << 0;
const RX_WAKER_STORED: usize = 1 << 1;
const COMPLETE: usize = 1 << 2;
const OPTION_BITS: u32 = 3;
struct Inner<E> {
state: AtomicUsize,
err: UnsafeCell<Option<E>>,
rx_waker: UnsafeCell<MaybeUninit<Waker>>,
tx_waker: UnsafeCell<MaybeUninit<Waker>>,
}
#[inline]
pub fn channel<E>() -> (Sender<E>, Receiver<E>) {
let inner = Arc::new(Inner::new());
let sender = Sender::new(Arc::clone(&inner));
let receiver = Receiver::new(inner);
(sender, receiver)
}
unsafe impl<E: Send> Send for Inner<E> {}
unsafe impl<E: Send> Sync for Inner<E> {}
impl<E> Inner<E> {
#[inline]
fn new() -> Self {
Self {
state: AtomicUsize::new(0),
err: UnsafeCell::new(None),
rx_waker: UnsafeCell::new(MaybeUninit::zeroed()),
tx_waker: UnsafeCell::new(MaybeUninit::zeroed()),
}
}
}
impl<E> SpscInner<AtomicUsize, usize> for Inner<E> {
const COMPLETE: usize = COMPLETE;
const RX_WAKER_STORED: usize = RX_WAKER_STORED;
const TX_WAKER_STORED: usize = TX_WAKER_STORED;
const ZERO: usize = 0;
#[inline]
fn state_load(&self, order: Ordering) -> usize {
self.state.load(order)
}
#[inline]
fn compare_exchange_weak(
&self,
current: usize,
new: usize,
success: Ordering,
failure: Ordering,
) -> Result<usize, usize> {
self.state.compare_exchange_weak(current, new, success, failure)
}
#[inline]
unsafe fn rx_waker_mut(&self) -> &mut MaybeUninit<Waker> {
unsafe { &mut *self.rx_waker.get() }
}
#[inline]
unsafe fn tx_waker_mut(&self) -> &mut MaybeUninit<Waker> {
unsafe { &mut *self.tx_waker.get() }
}
}
impl<E> SpscInnerErr<AtomicUsize, usize> for Inner<E> {
type Error = E;
unsafe fn err_mut(&self) -> &mut Option<Self::Error> {
unsafe { &mut *self.err.get() }
}
}
#[cfg(test)]
mod tests {
use super::*;
use core::{
num::NonZeroUsize,
pin::Pin,
sync::atomic::AtomicUsize,
task::{Context, Poll, RawWaker, RawWakerVTable, Waker},
};
use futures::stream::Stream;
struct Counter(AtomicUsize);
impl Counter {
fn to_waker(&'static self) -> Waker {
unsafe fn clone(counter: *const ()) -> RawWaker {
RawWaker::new(counter, &VTABLE)
}
unsafe fn wake(counter: *const ()) {
unsafe { (*(counter as *const Counter)).0.fetch_add(1, Ordering::SeqCst) };
}
static VTABLE: RawWakerVTable = RawWakerVTable::new(clone, wake, wake, drop);
unsafe { Waker::from_raw(RawWaker::new(self as *const _ as *const (), &VTABLE)) }
}
}
#[test]
fn send_sync() {
static COUNTER: Counter = Counter(AtomicUsize::new(0));
let (mut tx, mut rx) = channel::<()>();
assert_eq!(tx.send(1).unwrap(), ());
drop(tx);
let waker = COUNTER.to_waker();
let mut cx = Context::from_waker(&waker);
assert_eq!(
Pin::new(&mut rx).poll_next(&mut cx),
Poll::Ready(Some(Ok(NonZeroUsize::new(1).unwrap())))
);
assert_eq!(Pin::new(&mut rx).poll_next(&mut cx), Poll::Ready(None));
assert_eq!(COUNTER.0.load(Ordering::SeqCst), 0);
}
#[test]
fn send_async() {
static COUNTER: Counter = Counter(AtomicUsize::new(0));
let (mut tx, mut rx) = channel::<()>();
let waker = COUNTER.to_waker();
let mut cx = Context::from_waker(&waker);
assert_eq!(Pin::new(&mut rx).poll_next(&mut cx), Poll::Pending);
assert_eq!(tx.send(1).unwrap(), ());
assert_eq!(
Pin::new(&mut rx).poll_next(&mut cx),
Poll::Ready(Some(Ok(NonZeroUsize::new(1).unwrap())))
);
assert_eq!(Pin::new(&mut rx).poll_next(&mut cx), Poll::Pending);
drop(tx);
assert_eq!(Pin::new(&mut rx).poll_next(&mut cx), Poll::Ready(None));
assert_eq!(COUNTER.0.load(Ordering::SeqCst), 2);
}
#[test]
fn send_err() {
static COUNTER: Counter = Counter(AtomicUsize::new(0));
let (tx, mut rx) = channel::<()>();
assert_eq!(tx.send_err(()).unwrap(), ());
let waker = COUNTER.to_waker();
let mut cx = Context::from_waker(&waker);
assert_eq!(Pin::new(&mut rx).poll_next(&mut cx), Poll::Ready(Some(Err(()))));
assert_eq!(Pin::new(&mut rx).poll_next(&mut cx), Poll::Ready(None));
assert_eq!(COUNTER.0.load(Ordering::SeqCst), 0);
}
#[test]
fn recv_many() {
static COUNTER: Counter = Counter(AtomicUsize::new(0));
let (mut tx, mut rx) = channel::<()>();
let waker = COUNTER.to_waker();
let mut cx = Context::from_waker(&waker);
assert_eq!(Pin::new(&mut rx).poll_next(&mut cx), Poll::Pending);
assert_eq!(tx.send(1).unwrap(), ());
assert_eq!(tx.send(1).unwrap(), ());
assert_eq!(tx.send(1).unwrap(), ());
assert_eq!(
Pin::new(&mut rx).poll_next(&mut cx),
Poll::Ready(Some(Ok(NonZeroUsize::new(3).unwrap())))
);
assert_eq!(Pin::new(&mut rx).poll_next(&mut cx), Poll::Pending);
drop(tx);
assert_eq!(Pin::new(&mut rx).poll_next(&mut cx), Poll::Ready(None));
assert_eq!(COUNTER.0.load(Ordering::SeqCst), 4);
}
}