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
pub use self::builder::DispatcherBuilder;
#[cfg(not(target_os = "emscripten"))]
pub use self::async::AsyncDispatcher;
use smallvec::SmallVec;
use res::Resources;
use system::RunNow;
use self::stage::Stage;
#[cfg(not(target_os = "emscripten"))]
mod async;
mod builder;
mod stage;
pub struct Dispatcher<'a, 'b> {
stages: Vec<Stage<'a>>,
thread_local: ThreadLocal<'b>,
#[cfg(not(target_os = "emscripten"))]
thread_pool: ::std::sync::Arc<::rayon::ThreadPool>,
}
impl<'a, 'b> Dispatcher<'a, 'b> {
pub fn dispatch(&mut self, res: &mut Resources) {
#[cfg(not(target_os = "emscripten"))]
self.dispatch_par(res);
#[cfg(target_os = "emscripten")]
self.dispatch_seq(res);
self.dispatch_thread_local(res);
}
#[cfg(not(target_os = "emscripten"))]
pub fn dispatch_par(&mut self, res: &mut Resources) {
let stages = &mut self.stages;
self.thread_pool
.install(move || for stage in stages {
stage.execute(res);
});
}
pub fn dispatch_seq(&mut self, res: &mut Resources) {
for stage in &mut self.stages {
stage.execute_seq(res);
}
}
pub fn dispatch_thread_local(&mut self, res: &mut Resources) {
for sys in &mut self.thread_local {
sys.run_now(res);
}
}
}
#[derive(Clone, Copy, Debug, Eq, Hash, Ord, PartialEq, PartialOrd)]
pub struct SystemId(pub usize);
type SystemExecSend<'b> = Box<for<'a> RunNow<'a> + Send + 'b>;
type ThreadLocal<'a> = SmallVec<[Box<for<'b> RunNow<'b> + 'a>; 4]>;
#[cfg(test)]
mod tests {
use res::*;
use system::*;
use super::*;
struct Res(i32);
struct Dummy(i32);
impl<'a> System<'a> for Dummy {
type SystemData = FetchMut<'a, Res>;
fn run(&mut self, mut data: Self::SystemData) {
if self.0 == 4 {
assert_eq!(data.0, 6);
} else if self.0 == 5 {
assert_eq!(data.0, 10);
}
data.0 += self.0;
}
}
struct Panic;
impl<'a> System<'a> for Panic {
type SystemData = ();
fn run(&mut self, _: Self::SystemData) {
panic!("Propagated panic");
}
}
fn new_builder() -> DispatcherBuilder<'static, 'static> {
DispatcherBuilder::new()
.add(Dummy(0), "0", &[])
.add(Dummy(1), "1", &[])
.add(Dummy(2), "2", &[])
.add(Dummy(3), "3", &["1"])
.add_barrier()
.add(Dummy(4), "4", &[])
.add(Dummy(5), "5", &["4"])
}
fn new_resources() -> Resources {
let mut res = Resources::new();
res.add(Res(0));
res
}
#[test]
#[should_panic(expected = "Propagated panic")]
fn dispatcher_panics() {
DispatcherBuilder::new()
.add(Panic, "p", &[])
.build()
.dispatch(&mut new_resources())
}
#[test]
fn stages() {
let mut d = new_builder().build();
d.dispatch(&mut new_resources());
}
#[test]
fn stages_async() {
let mut d = new_builder().build_async(new_resources());
d.dispatch();
}
}