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
//! # Series
//! The prototype a schedule clones its runs from, and the
//! wrapper each run is spawned as
//!
//! Runs of a schedule can overlap, so each gets its own slot
//! and publishes back into the series slot
use crate::modules::input::{Token, token};
use crate::{
executor::{self, Executor},
futures::task::{
Nothing, Task,
sealed::{self, Step},
},
};
use std::time::Duration;
/// A task a schedule can make more of
///
/// Only the manager thread ever calls `launch`
pub(crate) trait SeriesTask: Send {
/// Spawns one run of the series
///
/// ## Returns
/// Whether a run is on its way
fn launch(&self, series: usize, priority: u8, timeout: Option<Duration>) -> bool;
}
impl<F> SeriesTask for F
where
F: Task + Clone,
{
#[inline(always)]
fn launch(&self, series: usize, priority: u8, timeout: Option<Duration>) -> bool {
// Held until the run is dropped, so the series slot can't be
// freed and reused while a run could still publish into it
Executor::add_listener(series);
executor::spawn_run(
SeriesRun {
series,
inner: self.clone(),
},
priority,
timeout,
series,
)
}
}
/// One run of a series, which publishes its output into the
/// series slot
pub(crate) struct SeriesRun<F>
where
F: Task,
{
/// The slot this run publishes into, which it holds a listener
/// on
series: usize,
/// This run's copy of the task
inner: F,
}
impl<F> Drop for SeriesRun<F>
where
F: Task,
{
/// Gives the run's claim on the series back, however the run
/// ended
fn drop(&mut self) {
Executor::drop_listener(self.series);
}
}
impl<F> sealed::Sealed for SeriesRun<F> where F: Task {}
impl<F> Task for SeriesRun<F>
where
F: Task,
{
type Output = ();
type Input = Nothing;
/// Runs this copy and publishes the result into the series
#[inline(always)]
fn execute(&self, _token: Token, reactor_id: i32, task_id: usize) -> Self::Output {
executor::publish(
self.series,
self.inner.execute(token(), reactor_id, task_id),
);
}
#[inline(always)]
fn prepare(&mut self, _token: Token) {
self.inner.prepare(token());
}
/// Whatever the task inside says
#[inline(always)]
fn blocking(&self, _token: Token) -> bool {
self.inner.blocking(token())
}
/// Steps this copy, so a run that waits on a socket parks like
/// any other, and publishes once it is done
#[inline(always)]
fn step(&mut self, _token: Token, reactor_id: i32, task_id: usize) -> Step<Self::Output> {
match self.inner.step(token(), reactor_id, task_id) {
Step::Done(out) => {
executor::publish(self.series, out);
Step::Done(())
}
Step::Park(park) => Step::Park(park),
}
}
}