task/manager/
mod.rs

1// - Dependencies
2use super::*;
3
4// - Submodules
5mod lifecycle;
6mod metadata;
7mod properties;
8mod registration;
9mod relationships;
10mod signals;
11mod spawner;
12mod utilities;
13
14#[cfg(test)]
15mod tests;
16
17// - Re-exports
18
19pub(crate) use metadata::*;
20pub use spawner::*;
21
22// Manager module - core Manager structure and initialization
23
24use crate::manager::Metadata;
25
26use alloc::collections::BTreeMap;
27use synchronization::{
28    blocking_mutex::raw::CriticalSectionRawMutex, once_lock::OnceLock, rwlock::RwLock,
29};
30
31static MANAGER_INSTANCE: OnceLock<Manager> = OnceLock::new();
32
33pub fn initialize() -> &'static Manager {
34    MANAGER_INSTANCE.get_or_init(Manager::new)
35}
36
37pub fn get_instance() -> &'static Manager {
38    MANAGER_INSTANCE.try_get().expect("Manager not initialized")
39}
40
41pub(crate) struct Inner {
42    pub(crate) tasks: BTreeMap<TaskIdentifier, Metadata>,
43    pub(crate) identifiers: BTreeMap<usize, TaskIdentifier>,
44    pub(crate) spawners: BTreeMap<usize, ::embassy_executor::Spawner>,
45}
46
47unsafe impl Send for Manager {}
48
49/// A manager for tasks.
50pub struct Manager(pub(crate) RwLock<CriticalSectionRawMutex, Inner>);
51
52unsafe impl Sync for Manager {}
53
54impl Manager {
55    pub const ROOT_TASK_IDENTIFIER: TaskIdentifier = TaskIdentifier::new(0);
56
57    /// Create a new task manager instance,
58    /// create a root task and register current thread as the root task main thread.
59    pub(crate) fn new() -> Self {
60        Manager(RwLock::new(Inner {
61            tasks: BTreeMap::new(),
62            identifiers: BTreeMap::new(),
63            spawners: BTreeMap::new(),
64        }))
65    }
66}