aeth_task/
lib.rs

1//! Async-based task framework.
2//!
3//! The well-known tokio runtime is general async
4//! runtime, and it is good. In fact, we are using
5//! tokio as a component of the task framework.
6//! Howver, shear tokio is not well-suited for
7//! a UI applications:
8//!
9//! First of all, we want to prioritize UI
10//! rendering and response to user interactions,
11//! this means we shouldn't treat all tasks in
12//! the application equally. However, tokio views
13//! all tasks equally, there's no difference
14//! between a task in respond to user interaction
15//! and a task performing massive computation.
16//!
17//! What's worse, a UI application usually has a
18//! UI thread running an UI event poll, which is
19//! the place triggering rendering and interaction
20//! logic. Meantime, tokio is a work-stealing
21//! async runtime, which might steal the massive
22//! computation work to the UI thread if we are
23//! running tokio on the UI thread.
24//!
25//! Actually, separating thread is not enough,
26//! there's case when the UI thread and tokio
27//! thread running massive task are scheduled
28//! on the same CPU, which might also downgrade
29//! the performance of UI thread as it relies
30//! on timer interrupt to reclaim the CPU. We
31//! will also want to set the CPU affinities
32//! whenever it's possible.
33//!
34//! This is why the task framework comes in. We
35//! provides facilities for configuring thread
36//! model for UI applications, as well as
37//! specifying type of work, and spawning them
38//! on the correct threads.
39
40#[doc(hidden)]
41pub mod spawner;
42#[rustfmt::skip]
43pub use spawner::{
44    Handle, Type,
45    spawn_foreground,
46    dispatch_background,
47    dispatch_foreground,
48    drain_foreground_loopback,
49    run_foreground,
50    current_type,
51};
52
53pub mod foreground {
54    //! Task primitives in foreground thread flavour.
55    //!
56    //! This allows the function which is dedicated
57    //! to a foreground thread to directly write
58    //! `use aeth::task::foreground`, and then
59    //! `foreground::spawn` or `foreground::dispatch`.
60    pub use super::dispatch_background as dispatch;
61    pub use super::spawn_foreground as spawn;
62
63    /// Assert current thread is a foreground thread.
64    pub fn assert() {
65        assert!(
66            super::current_type() == super::Type::Foreground,
67            "Not running on a foreground thread.",
68        );
69    }
70}
71
72pub mod background {
73    //! Task primitives in background thread flavour.
74    //!
75    //! This allows the function which is dedicated
76    //! to background threads to directly write
77    //! `use aeth::task::background`, and then
78    //! `background::spawn` or `background::loopback`.
79    pub use super::dispatch_background as spawn;
80    pub use super::dispatch_foreground as loopback;
81
82    /// Assert current thread is a background thread.
83    pub fn assert() {
84        assert!(
85            super::current_type() == super::Type::Background,
86            "Not running on a background thread.",
87        );
88    }
89}
90
91pub mod framework;
92
93pub mod ready_poll;
94
95#[cfg(test)]
96mod test;