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
use std::{future::Future, pin::Pin, sync::Arc};
use futures_util::{
stream::{FuturesUnordered, Stream},
task::{waker_ref, ArcWake, Context, LocalFutureObj, Poll as FutPoll},
};
use crate::{
sources::{
channel::{channel, Channel, Event, Sender},
ping::{make_ping, Ping, PingSource},
EventSource,
},
Poll, Readiness, Token,
};
pub struct Executor<T> {
futures: FuturesUnordered<LocalFutureObj<'static, T>>,
new_futures: Channel<LocalFutureObj<'static, T>>,
ready_futures: PingSource,
waker: Arc<ExecWaker>,
}
#[derive(Clone)]
pub struct Scheduler<T> {
sender: Sender<LocalFutureObj<'static, T>>,
}
impl<T> Scheduler<T> {
pub fn schedule<Fut: 'static>(&self, future: Fut) -> Result<(), ()>
where
Fut: Future<Output = T>,
{
let obj = LocalFutureObj::new(Box::new(future));
self.sender.send(obj).map_err(|_| ())
}
}
struct ExecWaker {
ping: Ping,
}
impl ArcWake for ExecWaker {
fn wake_by_ref(arc_self: &Arc<ExecWaker>) {
arc_self.ping.ping();
}
}
pub fn executor<T>() -> std::io::Result<(Executor<T>, Scheduler<T>)> {
let (ping, ready_futures) = make_ping()?;
let (sender, new_futures) = channel();
Ok((
Executor {
futures: FuturesUnordered::new(),
new_futures,
ready_futures,
waker: Arc::new(ExecWaker { ping }),
},
Scheduler { sender },
))
}
const TOK_NEW: u32 = 1;
const TOK_READY: u32 = 1;
impl<T> EventSource for Executor<T> {
type Event = T;
type Metadata = ();
type Ret = ();
fn process_events<F>(
&mut self,
readiness: Readiness,
token: Token,
mut callback: F,
) -> std::io::Result<()>
where
F: FnMut(T, &mut ()),
{
if token.sub_id == TOK_NEW {
let futures = &mut self.futures;
self.new_futures
.process_events(readiness, token, |evt, _| {
if let Event::Msg(fut) = evt {
futures.push(fut);
}
})?;
}
if token.sub_id == TOK_READY {
self.ready_futures
.process_events(readiness, token, |(), _| {})?;
}
let waker = waker_ref(&self.waker);
let mut cx = Context::from_waker(&waker);
while let FutPoll::Ready(Some(ret)) = Pin::new(&mut self.futures).poll_next(&mut cx) {
callback(ret, &mut ());
}
Ok(())
}
fn register(&mut self, poll: &mut Poll, token: Token) -> std::io::Result<()> {
self.new_futures.register(
poll,
Token {
sub_id: TOK_NEW,
..token
},
)?;
self.ready_futures.register(
poll,
Token {
sub_id: TOK_READY,
..token
},
)?;
Ok(())
}
fn reregister(&mut self, poll: &mut Poll, token: Token) -> std::io::Result<()> {
self.new_futures.reregister(
poll,
Token {
sub_id: TOK_NEW,
..token
},
)?;
self.ready_futures.reregister(
poll,
Token {
sub_id: TOK_READY,
..token
},
)?;
Ok(())
}
fn unregister(&mut self, poll: &mut Poll) -> std::io::Result<()> {
self.new_futures.unregister(poll)?;
self.ready_futures.unregister(poll)?;
Ok(())
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn ready() {
let mut event_loop = crate::EventLoop::<u32>::try_new().unwrap();
let handle = event_loop.handle();
let (exec, sched) = executor::<u32>().unwrap();
handle
.insert_source(exec, move |ret, &mut (), got| {
*got = ret;
})
.map_err(Into::<std::io::Error>::into)
.unwrap();
let mut got = 0;
let fut = async { 42 };
event_loop
.dispatch(Some(::std::time::Duration::from_millis(0)), &mut got)
.unwrap();
assert_eq!(got, 0);
sched.schedule(fut).unwrap();
event_loop
.dispatch(Some(::std::time::Duration::from_millis(0)), &mut got)
.unwrap();
assert_eq!(got, 42);
}
}