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
use std::{
pin::Pin,
task::{Context, Poll},
};
use crate::*;
use concurrent_queue::PopError;
use event_listener::EventListener;
use futures::{pin_mut, Future, FutureExt};
use tokio::task::yield_now;
impl<M> Channel<M> {
pub fn try_recv(&self, signaled_halt: &mut bool) -> Result<M, TryRecvError> {
if !(*signaled_halt) && self.inbox_should_halt() {
*signaled_halt = true;
Err(TryRecvError::Halted)
} else {
self.pop_msg().map_err(|e| match e {
PopError::Empty => TryRecvError::Empty,
PopError::Closed => TryRecvError::ClosedAndEmpty,
})
}
}
pub fn recv<'a>(
&'a self,
signaled_halt: &'a mut bool,
listener: &'a mut Option<EventListener>,
) -> Rcv<'a, M> {
Rcv {
channel: self,
signaled_halt,
listener,
}
}
fn poll_try_recv(
&self,
signaled_halt: &mut bool,
listener: &mut Option<EventListener>,
) -> Option<Result<M, RecvError>> {
match self.try_recv(signaled_halt) {
Ok(msg) => {
*listener = None;
Some(Ok(msg))
}
Err(signal) => match signal {
TryRecvError::Halted => {
*listener = None;
Some(Err(RecvError::Halted))
}
TryRecvError::ClosedAndEmpty => {
*listener = None;
Some(Err(RecvError::ClosedAndEmpty))
}
TryRecvError::Empty => None,
},
}
}
}
#[derive(Debug)]
pub struct Rcv<'a, M> {
channel: &'a Channel<M>,
signaled_halt: &'a mut bool,
listener: &'a mut Option<EventListener>,
}
impl<'a, M> Unpin for Rcv<'a, M> {}
impl<'a, M> Future for Rcv<'a, M> {
type Output = Result<M, RecvError>;
fn poll(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
let Self {
channel,
signaled_halt,
listener,
} = &mut *self;
if let Some(res) = channel.poll_try_recv(signaled_halt, listener) {
let fut = yield_now();
pin_mut!(fut);
let _ = fut.poll(cx);
return Poll::Ready(res);
}
loop {
if listener.is_none() {
**listener = Some(channel.get_recv_listener())
}
if let Some(res) = channel.poll_try_recv(signaled_halt, listener) {
return Poll::Ready(res);
}
match listener.as_mut().unwrap().poll_unpin(cx) {
Poll::Ready(()) => {
**listener = None;
if let Some(res) = channel.poll_try_recv(signaled_halt, listener) {
return Poll::Ready(res);
}
}
Poll::Pending => return Poll::Pending,
}
}
}
}
impl<'a, M> Drop for Rcv<'a, M> {
fn drop(&mut self) {
*self.listener = None;
}
}
#[cfg(test)]
mod test {
use std::{future::ready, sync::Arc, time::Duration};
use crate::*;
#[test]
fn try_recv() {
let channel = Channel::<()>::new(1, 1, Capacity::default());
channel.push_msg(()).unwrap();
channel.push_msg(()).unwrap();
assert!(channel.try_recv(&mut true).is_ok());
assert!(channel.try_recv(&mut false).is_ok());
assert_eq!(channel.try_recv(&mut true), Err(TryRecvError::Empty));
assert_eq!(channel.try_recv(&mut false), Err(TryRecvError::Empty));
}
#[test]
fn try_recv_closed() {
let channel = Channel::<()>::new(1, 1, Capacity::default());
channel.push_msg(()).unwrap();
channel.push_msg(()).unwrap();
channel.close();
assert!(channel.try_recv(&mut true).is_ok());
assert!(channel.try_recv(&mut false).is_ok());
assert_eq!(
channel.try_recv(&mut true),
Err(TryRecvError::ClosedAndEmpty)
);
assert_eq!(
channel.try_recv(&mut false),
Err(TryRecvError::ClosedAndEmpty)
);
}
#[test]
fn try_recv_halt() {
let channel = Channel::<()>::new(1, 1, Capacity::default());
channel.push_msg(()).unwrap();
channel.push_msg(()).unwrap();
channel.halt_some(1);
assert_eq!(channel.try_recv(&mut false), Err(TryRecvError::Halted));
assert!(channel.try_recv(&mut true).is_ok());
assert!(channel.try_recv(&mut false).is_ok());
assert_eq!(channel.try_recv(&mut true), Err(TryRecvError::Empty));
assert_eq!(channel.try_recv(&mut false), Err(TryRecvError::Empty));
}
#[tokio::test]
async fn recv_immedeate() {
let channel = Channel::<()>::new(1, 1, Capacity::default());
let mut listener = None;
channel.push_msg(()).unwrap();
channel.close();
assert_eq!(channel.recv(&mut false, &mut listener).await, Ok(()));
assert_eq!(
channel.recv(&mut false, &mut listener).await,
Err(RecvError::ClosedAndEmpty)
);
}
#[tokio::test]
async fn recv_delayed() {
let channel = Arc::new(Channel::<()>::new(1, 1, Capacity::default()));
let channel_clone = channel.clone();
let handle = tokio::task::spawn(async move {
let mut listener = None;
assert_eq!(channel_clone.recv(&mut false, &mut listener).await, Ok(()));
assert_eq!(
channel_clone.recv(&mut false, &mut listener).await,
Err(RecvError::ClosedAndEmpty)
);
});
tokio::time::sleep(Duration::from_millis(10)).await;
channel.push_msg(()).unwrap();
channel.close();
handle.await.unwrap();
}
#[tokio::test]
async fn dropping_recv_notifies_next() {
let channel = Arc::new(Channel::<()>::new(1, 1, Capacity::default()));
let channel_clone = channel.clone();
let handle = tokio::task::spawn(async move {
let mut listener = None;
let mut halt = false;
let mut recv1 = channel_clone.recv(&mut halt, &mut listener);
tokio::select! {
biased;
_ = &mut recv1 => {
panic!()
}
_ = ready(||()) => {
()
}
}
let mut listener = None;
let mut halt = false;
let recv2 = channel_clone.recv(&mut halt, &mut listener);
drop(recv1);
recv2.await.unwrap();
});
tokio::time::sleep(Duration::from_millis(10)).await;
channel.push_msg(()).unwrap();
channel.close();
handle.await.unwrap();
}
}