async_ecs/dispatcher/
task.rs

1use log::info;
2
3use super::{
4    LocalRun, LocalRunAsync, Receiver, Run, RunAsync, Sender, SharedWorld, ThreadRun,
5    ThreadRunAsync,
6};
7
8/// Long running task of a `System` that is executed in a separate thread.
9pub async fn execute_thread(
10    name: String,
11    mut run: ThreadRun,
12    sender: Sender,
13    receivers: Vec<Receiver>,
14    world: SharedWorld,
15) {
16    info!("System started: {}", &name);
17
18    execute_inner(run.as_mut(), sender, receivers, world).await;
19
20    info!("System finished: {}", &name);
21}
22
23/// Long running task of a `System` that is executed in the thread local context.
24pub async fn execute_local(
25    name: String,
26    mut run: LocalRun,
27    sender: Sender,
28    receivers: Vec<Receiver>,
29    world: SharedWorld,
30) {
31    info!("System started (local): {}", &name);
32
33    execute_inner(run.as_mut(), sender, receivers, world).await;
34
35    info!("System finished (local): {}", &name);
36}
37
38/// Long running task of a `System` that is executed in a separate thread.
39pub async fn execute_thread_async(
40    name: String,
41    mut run: ThreadRunAsync,
42    sender: Sender,
43    receivers: Vec<Receiver>,
44    world: SharedWorld,
45) {
46    info!("System started: {}", &name);
47
48    execute_inner_async(run.as_mut(), sender, receivers, world).await;
49
50    info!("System finished: {}", &name);
51}
52
53/// Long running task of a `System` that is executed in the thread local context.
54pub async fn execute_local_async(
55    name: String,
56    mut run: LocalRunAsync,
57    sender: Sender,
58    receivers: Vec<Receiver>,
59    world: SharedWorld,
60) {
61    info!("System started (local): {}", &name);
62
63    execute_inner_async(run.as_mut(), sender, receivers, world).await;
64
65    info!("System finished (local): {}", &name);
66}
67
68/// Actual tasks that is running the system.
69async fn execute_inner<R: for<'a> Run<'a> + ?Sized>(
70    run: &mut R,
71    sender: Sender,
72    mut receivers: Vec<Receiver>,
73    world: SharedWorld,
74) {
75    loop {
76        for receiver in &mut receivers {
77            match receiver.changed().await {
78                Ok(()) => (),
79                Err(_) => return,
80            }
81        }
82
83        run.run(&world);
84
85        match sender.send(()) {
86            Ok(()) => (),
87            Err(_) => return,
88        }
89    }
90}
91
92/// Actual tasks that is running the system.
93async fn execute_inner_async<R: for<'a> RunAsync<'a> + ?Sized>(
94    run: &mut R,
95    sender: Sender,
96    mut receivers: Vec<Receiver>,
97    world: SharedWorld,
98) {
99    loop {
100        for receiver in &mut receivers {
101            match receiver.changed().await {
102                Ok(()) => (),
103                Err(_) => return,
104            }
105        }
106
107        run.run(&world).await;
108
109        match sender.send(()) {
110            Ok(()) => (),
111            Err(_) => return,
112        }
113    }
114}