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::*;
20
21// Manager module - core Manager structure and initialization
22
23use crate::manager::Metadata;
24
25use alloc::collections::BTreeMap;
26use synchronization::{
27    blocking_mutex::raw::CriticalSectionRawMutex, once_lock::OnceLock, rwlock::RwLock,
28};
29
30static MANAGER_INSTANCE: OnceLock<Manager> = OnceLock::new();
31
32pub fn initialize() -> &'static Manager {
33    MANAGER_INSTANCE.get_or_init(Manager::new)
34}
35
36pub fn get_instance() -> &'static Manager {
37    MANAGER_INSTANCE.try_get().expect("Manager not initialized")
38}
39
40pub(crate) struct Inner {
41    pub(crate) tasks: BTreeMap<TaskIdentifier, Metadata>,
42    pub(crate) identifiers: BTreeMap<usize, TaskIdentifier>,
43    pub(crate) spawners: BTreeMap<usize, ::embassy_executor::Spawner>,
44}
45
46unsafe impl Send for Manager {}
47
48/// A manager for tasks.
49pub struct Manager(pub(crate) RwLock<CriticalSectionRawMutex, Inner>);
50
51unsafe impl Sync for Manager {}
52
53impl Manager {
54    pub const ROOT_TASK_IDENTIFIER: TaskIdentifier = TaskIdentifier::new(0);
55
56    /// Create a new task manager instance,
57    /// create a root task and register current thread as the root task main thread.
58    pub(crate) fn new() -> Self {
59        Manager(RwLock::new(Inner {
60            tasks: BTreeMap::new(),
61            identifiers: BTreeMap::new(),
62            spawners: BTreeMap::new(),
63        }))
64    }
65}