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
use std::collections::BTreeMap;
use std::sync::Arc;
use std::thread;
use std::mem;
use RunningTask;
use channel::{Exec, ExecParam};
use monitor::Monitor;
use time::{Duration, SteadyTime};
use futures::{task, Future, Stream, Async};
use futures::task::Task;
#[derive(Clone)]
pub struct TimeScheduler {
tasks: Arc<Monitor<BTreeMap<SteadyTime, ScheduledEvent>>>
}
impl TimeScheduler {
pub fn new() -> Self {
let scheduler = TimeScheduler {
tasks: Arc::new(Monitor::new(BTreeMap::new()))
};
let tasks = scheduler.tasks.clone();
thread::spawn(move || tasks.with_lock(|mut guard| {
loop {
if let Some(time) = guard.iter()
.next()
.map(|(time, _)| *time)
.filter(|time| *time <= SteadyTime::now()) {
guard.remove(&time).unwrap().run();
} else if let Some((time, delay)) = guard.iter()
.next()
.map(|(time, _)| *time)
.map(|time| (time, time - SteadyTime::now()))
.map(|(time, delay)| match delay.to_std() {
Ok(delay) => (time, delay),
Err(_) => (time, Duration::zero().to_std().unwrap())
}) {
if guard.wait_timeout(delay).timed_out() {
guard.remove(&time).unwrap().run();
}
} else {
guard.wait();
}
}
}));
scheduler
}
fn insert(&self, time: SteadyTime, event: ScheduledEvent) {
self.tasks.with_lock(|mut tasks| {
if let Some(to_insert) = match tasks.get_mut(&time) {
None => {
Some(event)
},
Some(&mut ScheduledEvent::Multiple(ref mut vec)) => {
vec.push(event);
None
},
Some(a) => {
let mut b = ScheduledEvent::Multiple(Vec::new());
mem::swap( a, &mut b);
if let &mut ScheduledEvent::Multiple(ref mut vec) = a {
vec.push(b);
} else {
unreachable!()
}
None
}
} {
tasks.insert(time, to_insert);
}
tasks.notify_all();
})
}
pub fn at(&self, moment: SteadyTime) -> FutureMoment {
FutureMoment {
scheduler: Some(self.clone()),
moment,
requested_notify: false,
}
}
pub fn after(&self, delay: Duration) -> FutureMoment {
FutureMoment {
scheduler: Some(self.clone()),
moment: SteadyTime::now() + delay,
requested_notify: false,
}
}
pub fn periodically(&self, start: SteadyTime, period: Duration) -> PeriodicMoments {
PeriodicMoments {
scheduler: self.clone(),
current: start,
period
}
}
pub fn run_at(
&self,
moment: SteadyTime,
future: impl Future<Item=(), Error=()> + Send + 'static,
exec: impl Exec + Send + 'static,
) {
self.insert(moment, ScheduledEvent::Submit(Box::new(SubmitExec {
task: RunningTask::new(future),
exec
})));
}
pub fn run_at_param<P: Send + 'static>(
&self,
moment: SteadyTime,
future: impl Future<Item=(), Error=()> + Send + 'static,
exec: impl ExecParam<Param=P> + Send + 'static,
param: P,
) {
self.insert(moment, ScheduledEvent::Submit(Box::new(SubmitExecParam {
task: RunningTask::new(future),
exec,
param
})))
}
pub fn run_after(
&self,
delay: Duration,
future: impl Future<Item=(), Error=()> + Send + 'static,
exec: impl Exec + Send + 'static,
) {
self.run_at(SteadyTime::now() + delay, future, exec);
}
pub fn run_after_param<P: Send + 'static>(
&self,
delay: Duration,
future: impl Future<Item=(), Error=()> + Send + 'static,
exec: impl ExecParam<Param=P> + Send + 'static,
param: P,
) {
self.run_at_param(SteadyTime::now() + delay, future, exec, param);
}
}
enum ScheduledEvent {
Notify(Task),
Submit(Box<dyn Submit>),
Multiple(Vec<ScheduledEvent>),
}
impl ScheduledEvent {
fn run(self) {
match self {
ScheduledEvent::Notify(task) => task.notify(),
ScheduledEvent::Submit(submit) => submit.submit(),
ScheduledEvent::Multiple(events) => events.into_iter()
.for_each(|event| event.run()),
};
}
}
trait Submit: Send + 'static {
fn submit(self: Box<Self>);
}
struct SubmitExec<E: Exec + Send + 'static> {
task: RunningTask,
exec: E,
}
impl<E: Exec + Send + 'static> Submit for SubmitExec<E> {
fn submit(self: Box<Self>) {
let unboxed = *self;
let SubmitExec {
task,
exec
} = unboxed;
exec.submit(task);
}
}
struct SubmitExecParam<E: ExecParam + Send + 'static> {
task: RunningTask,
exec: E,
param: E::Param,
}
impl<E: ExecParam + Send + 'static> Submit for SubmitExecParam<E>
where E::Param: Send + 'static {
fn submit(self: Box<Self>) {
let unboxed = *self;
let SubmitExecParam {
task,
exec,
param
} = unboxed;
exec.submit(task, param);
}
}
pub struct FutureMoment {
scheduler: Option<TimeScheduler>,
moment: SteadyTime,
requested_notify: bool,
}
impl Future for FutureMoment {
type Item = ();
type Error = ();
fn poll(&mut self) -> Result<Async<<Self as Future>::Item>, <Self as Future>::Error> {
if SteadyTime::now() >= self.moment {
Ok(Async::Ready(()))
} else {
if !self.requested_notify {
self.requested_notify = true;
if let Some(scheduler) = self.scheduler.take() {
scheduler.insert(self.moment, ScheduledEvent::Notify(task::current()));
}
}
Ok(Async::NotReady)
}
}
}
pub struct PeriodicMoments {
scheduler: TimeScheduler,
current: SteadyTime,
period: Duration,
}
impl Stream for PeriodicMoments {
type Item = ();
type Error = ();
fn poll(&mut self) -> Result<Async<Option<<Self as Stream>::Item>>, <Self as Stream>::Error> {
if self.current <= SteadyTime::now() {
self.current = self.current + self.period;
Ok(Async::Ready(Some(())))
} else {
self.scheduler.insert(self.current,ScheduledEvent::Notify(task::current()));
Ok(Async::NotReady)
}
}
}