1use super::*;
3
4mod lifecycle;
6mod metadata;
7mod metrics;
8mod properties;
9mod registration;
10mod relationships;
11mod signals;
12mod spawner;
13mod utilities;
14
15#[cfg(test)]
16mod tests;
17
18pub(crate) use metadata::Metadata;
21pub use metadata::TaskAuthority;
22pub use spawner::*;
23
24#[cfg(any(test, feature = "test_harness"))]
27use alloc::boxed::Box;
28use alloc::collections::BTreeMap;
29use synchronization::{blocking_mutex::raw::CriticalSectionRawMutex, rwlock::RwLock};
30
31pub struct TaskSlot(embassy_executor::raw::TaskStorage<lifecycle::TaskFuture>);
34
35impl Default for TaskSlot {
36 fn default() -> Self {
37 Self::new()
38 }
39}
40
41impl TaskSlot {
42 pub const fn new() -> Self {
43 Self(embassy_executor::raw::TaskStorage::new())
44 }
45}
46
47pub(crate) struct Inner {
48 next_generation: u64,
49 root_established: bool,
50 pub(crate) tasks: BTreeMap<TaskIdentifier, Metadata>,
51 pub(crate) identifiers: BTreeMap<usize, TaskIdentifier>,
52 slots: alloc::vec::Vec<&'static embassy_executor::raw::TaskStorage<lifecycle::TaskFuture>>,
53 static_slots: &'static [TaskSlot],
54 #[cfg(feature = "metrics")]
56 counters: alloc::vec::Vec<&'static crate::metrics::TaskCounters>,
57 maximum_tasks: usize,
58}
59
60pub struct Manager(
62 pub(crate) RwLock<CriticalSectionRawMutex, Inner>,
63 synchronization::blocking_mutex::Mutex<
64 CriticalSectionRawMutex,
65 core::cell::RefCell<BTreeMap<usize, spawner::SpawnerState>>,
66 >,
67);
68
69#[cfg(any(test, feature = "test_harness"))]
70static TEST_MANAGER: synchronization::once_lock::OnceLock<&'static Manager> =
71 synchronization::once_lock::OnceLock::new();
72
73#[cfg(any(test, feature = "test_harness"))]
74static TEST_MANAGER_LOCK: std::sync::Mutex<()> = std::sync::Mutex::new(());
75
76#[cfg(any(test, feature = "test_harness"))]
77pub fn test_lock() -> &'static std::sync::Mutex<()> {
78 &TEST_MANAGER_LOCK
79}
80
81#[cfg(any(test, feature = "test_harness"))]
82#[doc(hidden)]
83pub fn test_manager() -> &'static Manager {
84 TEST_MANAGER.get_or_init(|| Box::leak(Box::new(Manager::new())))
85}
86
87#[cfg(any(test, feature = "test_harness"))]
88#[doc(hidden)]
89pub fn reset_test_manager() -> &'static Manager {
90 let manager = test_manager();
91 let mut inner = embassy_futures::block_on(manager.0.write());
92 inner.tasks.clear();
93 inner.root_established = false;
94 inner.identifiers.clear();
95 manager.1.lock(|spawners| spawners.borrow_mut().clear());
96 drop(inner);
97 manager
98}
99
100#[cfg(any(test, feature = "test_harness"))]
101pub fn initialize() -> &'static Manager {
102 test_manager()
103}
104
105#[cfg(any(test, feature = "test_harness"))]
106pub fn get_instance() -> &'static Manager {
107 test_manager()
108}
109
110impl Default for Manager {
111 fn default() -> Self {
112 Self::new()
113 }
114}
115
116impl Manager {
117 pub const ROOT_TASK_IDENTIFIER: TaskIdentifier = TaskIdentifier::new(0);
118
119 pub const fn new() -> Self {
122 Self::with_maximum_tasks(256)
123 }
124
125 pub const fn with_maximum_tasks(maximum_tasks: usize) -> Self {
127 Self::with_slots(maximum_tasks, &[])
128 }
129
130 pub const fn with_static_slots(slots: &'static [TaskSlot]) -> Self {
134 Self::with_slots(slots.len(), slots)
135 }
136
137 const fn with_slots(maximum_tasks: usize, static_slots: &'static [TaskSlot]) -> Self {
138 Manager(
139 RwLock::new(Inner {
140 next_generation: 0,
141 root_established: false,
142 tasks: BTreeMap::new(),
143 identifiers: BTreeMap::new(),
144 slots: alloc::vec::Vec::new(),
145 static_slots,
146 #[cfg(feature = "metrics")]
147 counters: alloc::vec::Vec::new(),
148 maximum_tasks,
149 }),
150 synchronization::blocking_mutex::Mutex::new(core::cell::RefCell::new(BTreeMap::new())),
151 )
152 }
153}