Skip to main content

ui/
tasks.rs

1// tasks -- async tasks on the UI thread (plat::task): spawned by App code, polled between
2// passes; a task that ran gets the app another pass, so App::frame sees what it changed
3//
4//   ui.spawn(fut) -> Task<T>    fut runs on the UI thread (it need not be Send); inside it the
5//                               blocking work goes to the pool (plat::task::spawn_blocking,
6//                               plat::aio, netio's async calls), sleep() waits for the task
7//                               clock.  App::frame polls the handle: task.take() (the result,
8//                               once), is_done(), cancel(); dropping the handle cancels it
9//   ui.spawner() -> Spawner     spawn from inside tasks (a clone moves into them)
10//   ui.tasks_live() -> n        tasks not finished yet
11//
12// Where they run: run_passes polls the woken tasks after the commands of each round (a poll
13// counts as a command: another round, at most the round limit; past it the next loop turn
14// builds the frame).  Wake-ups from other threads (pool jobs, Progress reports, channels) come
15// as E_WAKE (plat::wake) and are taken there.  The window loop waits no longer than the next
16// task timer and does not wait while tasks are ready; the browser / Android host gets the same
17// as its frame delay.  Headless runs put the task clock on the virtual clock (`wait` fires
18// sleep()s), and the scripts' `sync` waits until no task work is in flight (blocking jobs,
19// wake-ups not taken yet) -- so scripts with async work stay deterministic.
20
21use core::future::Future;
22
23use plat::task::{Spawner, Task};
24
25use super::Ui;
26
27impl Ui {
28    /// run fut on the UI thread between passes; App::frame polls the handle (dropping it
29    /// cancels the task, Task::detach lets it run on)
30    pub fn spawn<F: Future + 'static>(&self, fut: F) -> Task<F::Output> {
31        self.tasks.spawn(fut)
32    }
33
34    /// a handle that spawns UI tasks from inside tasks
35    pub fn spawner(&self) -> Spawner {
36        self.tasks.spawner()
37    }
38
39    /// tasks spawned and not finished
40    pub fn tasks_live(&self) -> usize {
41        self.tasks.live()
42    }
43
44    /// (runtime) poll the woken tasks once -> any ran (the frame may change)
45    pub(crate) fn tasks_pump(&mut self) -> bool {
46        self.tasks.run() > 0
47    }
48
49    /// (E_WAKE) take the wake-ups other threads sent
50    pub(crate) fn tasks_wake(&mut self) {
51        self.tasks.accept_remote();
52    }
53
54    /// tasks to poll now: woken, new, or a timer of theirs is due
55    pub(crate) fn tasks_ready(&self) -> bool {
56        self.tasks.has_ready() || plat::task::next_timer().is_some_and(|t| t <= plat::task::now_us())
57    }
58
59    /// task work in flight (the scripts' `sync`)
60    pub(crate) fn tasks_busy(&self) -> bool {
61        self.tasks_ready() || self.tasks.busy()
62    }
63
64    /// the next task timer on the task clock (window mode: plat::time_us)
65    pub(crate) fn tasks_deadline(&self) -> Option<u64> {
66        plat::task::next_timer()
67    }
68}