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
use crate::{archetype::ArchetypeGeneration, schedule::ParallelSystemContainer, world::World};
use downcast_rs::{impl_downcast, Downcast};
pub trait ParallelSystemExecutor: Downcast + Send + Sync {
fn rebuild_cached_data(&mut self, systems: &[ParallelSystemContainer]);
fn run_systems(&mut self, systems: &mut [ParallelSystemContainer], world: &mut World);
}
impl_downcast!(ParallelSystemExecutor);
pub struct SingleThreadedExecutor {
archetype_generation: ArchetypeGeneration,
}
impl Default for SingleThreadedExecutor {
fn default() -> Self {
Self {
archetype_generation: ArchetypeGeneration::new(usize::MAX),
}
}
}
impl ParallelSystemExecutor for SingleThreadedExecutor {
fn rebuild_cached_data(&mut self, _: &[ParallelSystemContainer]) {}
fn run_systems(&mut self, systems: &mut [ParallelSystemContainer], world: &mut World) {
self.update_archetypes(systems, world);
for system in systems {
if system.should_run() {
system.system_mut().run((), world);
}
}
}
}
impl SingleThreadedExecutor {
fn update_archetypes(&mut self, systems: &mut [ParallelSystemContainer], world: &World) {
let archetypes = world.archetypes();
let old_generation = self.archetype_generation;
let new_generation = archetypes.generation();
if old_generation == new_generation {
return;
}
let archetype_index_range = if old_generation.value() == usize::MAX {
0..archetypes.len()
} else {
old_generation.value()..archetypes.len()
};
for archetype in archetypes.archetypes[archetype_index_range].iter() {
for container in systems.iter_mut() {
let system = container.system_mut();
system.new_archetype(archetype);
}
}
self.archetype_generation = new_generation;
}
}