diff options
| author | Kat Perez <[email protected]> | 2025-05-08 11:20:22 -0400 |
|---|---|---|
| committer | Kat Perez <[email protected]> | 2025-05-08 11:29:44 -0400 |
| commit | 8a8deb704fdd58cecf463f033cd3c3d1cc3534c7 (patch) | |
| tree | 1ba3015e547b582cff15ecb7e92508a59c5ed6ef /embassy-executor/src | |
| parent | 56b5e35c60743d65aacee753d1db391c3cbeae16 (diff) | |
move spawn_named into trace.rs through TraceExt trait
Diffstat (limited to 'embassy-executor/src')
| -rw-r--r-- | embassy-executor/src/raw/trace.rs | 43 | ||||
| -rw-r--r-- | embassy-executor/src/spawner.rs | 37 |
2 files changed, 45 insertions, 35 deletions
diff --git a/embassy-executor/src/raw/trace.rs b/embassy-executor/src/raw/trace.rs index fec3a4834..eb960f721 100644 --- a/embassy-executor/src/raw/trace.rs +++ b/embassy-executor/src/raw/trace.rs | |||
| @@ -87,6 +87,49 @@ use core::sync::atomic::{AtomicUsize, Ordering}; | |||
| 87 | use rtos_trace::TaskInfo; | 87 | use rtos_trace::TaskInfo; |
| 88 | 88 | ||
| 89 | use crate::raw::{SyncExecutor, TaskHeader, TaskRef, TaskTracker}; | 89 | use crate::raw::{SyncExecutor, TaskHeader, TaskRef, TaskTracker}; |
| 90 | use crate::spawner::{SpawnError, SpawnToken, Spawner}; | ||
| 91 | |||
| 92 | /// Extension trait adding tracing capabilities to the Spawner | ||
| 93 | pub trait TraceExt { | ||
| 94 | /// Spawns a new task with a specified name. | ||
| 95 | /// | ||
| 96 | /// # Arguments | ||
| 97 | /// * `name` - Static string name to associate with the task | ||
| 98 | /// * `token` - Token representing the task to spawn | ||
| 99 | /// | ||
| 100 | /// # Returns | ||
| 101 | /// Result indicating whether the spawn was successful | ||
| 102 | fn spawn_named<S>(&self, name: &'static str, token: SpawnToken<S>) -> Result<(), SpawnError>; | ||
| 103 | } | ||
| 104 | |||
| 105 | #[cfg(feature = "trace")] | ||
| 106 | impl TraceExt for Spawner { | ||
| 107 | fn spawn_named<S>(&self, name: &'static str, token: SpawnToken<S>) -> Result<(), SpawnError> { | ||
| 108 | let task = token.raw_task; | ||
| 109 | core::mem::forget(token); | ||
| 110 | |||
| 111 | match task { | ||
| 112 | Some(task) => { | ||
| 113 | task.set_name(Some(name)); | ||
| 114 | let task_id = task.as_ptr() as u32; | ||
| 115 | task.set_id(task_id); | ||
| 116 | |||
| 117 | unsafe { self.executor.spawn(task) }; | ||
| 118 | Ok(()) | ||
| 119 | } | ||
| 120 | None => Err(SpawnError::Busy), | ||
| 121 | } | ||
| 122 | } | ||
| 123 | } | ||
| 124 | |||
| 125 | /// When trace is disabled, spawn_named falls back to regular spawn. | ||
| 126 | /// This maintains API compatibility while optimizing out the name parameter. | ||
| 127 | #[cfg(not(feature = "trace"))] | ||
| 128 | impl TraceExt for Spawner { | ||
| 129 | fn spawn_named<S>(&self, _name: &'static str, token: SpawnToken<S>) -> Result<(), SpawnError> { | ||
| 130 | self.spawn(token) | ||
| 131 | } | ||
| 132 | } | ||
| 90 | 133 | ||
| 91 | /// Global task tracker instance | 134 | /// Global task tracker instance |
| 92 | /// | 135 | /// |
diff --git a/embassy-executor/src/spawner.rs b/embassy-executor/src/spawner.rs index a0d246616..6b8db4f8f 100644 --- a/embassy-executor/src/spawner.rs +++ b/embassy-executor/src/spawner.rs | |||
| @@ -22,7 +22,7 @@ use super::raw; | |||
| 22 | /// Once you've invoked a task function and obtained a SpawnToken, you *must* spawn it. | 22 | /// Once you've invoked a task function and obtained a SpawnToken, you *must* spawn it. |
| 23 | #[must_use = "Calling a task function does nothing on its own. You must spawn the returned SpawnToken, typically with Spawner::spawn()"] | 23 | #[must_use = "Calling a task function does nothing on its own. You must spawn the returned SpawnToken, typically with Spawner::spawn()"] |
| 24 | pub struct SpawnToken<S> { | 24 | pub struct SpawnToken<S> { |
| 25 | raw_task: Option<raw::TaskRef>, | 25 | pub(crate) raw_task: Option<raw::TaskRef>, |
| 26 | phantom: PhantomData<*mut S>, | 26 | phantom: PhantomData<*mut S>, |
| 27 | } | 27 | } |
| 28 | 28 | ||
| @@ -103,7 +103,7 @@ impl core::error::Error for SpawnError {} | |||
| 103 | /// If you want to spawn tasks from another thread, use [SendSpawner]. | 103 | /// If you want to spawn tasks from another thread, use [SendSpawner]. |
| 104 | #[derive(Copy, Clone)] | 104 | #[derive(Copy, Clone)] |
| 105 | pub struct Spawner { | 105 | pub struct Spawner { |
| 106 | executor: &'static raw::Executor, | 106 | pub(crate) executor: &'static raw::Executor, |
| 107 | not_send: PhantomData<*mut ()>, | 107 | not_send: PhantomData<*mut ()>, |
| 108 | } | 108 | } |
| 109 | 109 | ||
| @@ -154,39 +154,6 @@ impl Spawner { | |||
| 154 | } | 154 | } |
| 155 | } | 155 | } |
| 156 | 156 | ||
| 157 | /// Spawns a new task with a specified name. | ||
| 158 | /// | ||
| 159 | /// # Arguments | ||
| 160 | /// * `name` - Static string name to associate with the task | ||
| 161 | /// * `token` - Token representing the task to spawn | ||
| 162 | /// | ||
| 163 | /// # Returns | ||
| 164 | /// Result indicating whether the spawn was successful | ||
| 165 | #[cfg(feature = "trace")] | ||
| 166 | pub fn spawn_named<S>(&self, name: &'static str, token: SpawnToken<S>) -> Result<(), SpawnError> { | ||
| 167 | let task = token.raw_task; | ||
| 168 | mem::forget(token); | ||
| 169 | |||
| 170 | match task { | ||
| 171 | Some(task) => { | ||
| 172 | task.set_name(Some(name)); | ||
| 173 | let task_id = task.as_ptr() as u32; | ||
| 174 | task.set_id(task_id); | ||
| 175 | |||
| 176 | unsafe { self.executor.spawn(task) }; | ||
| 177 | Ok(()) | ||
| 178 | } | ||
| 179 | None => Err(SpawnError::Busy), | ||
| 180 | } | ||
| 181 | } | ||
| 182 | |||
| 183 | /// When rtos-trace is disabled, spawn_named falls back to regular spawn. | ||
| 184 | /// This maintains API compatibility while optimizing out the name parameter. | ||
| 185 | #[cfg(not(feature = "trace"))] | ||
| 186 | pub fn spawn_named<S>(&self, _name: &'static str, token: SpawnToken<S>) -> Result<(), SpawnError> { | ||
| 187 | self.spawn(token) | ||
| 188 | } | ||
| 189 | |||
| 190 | // Used by the `embassy_executor_macros::main!` macro to throw an error when spawn | 157 | // Used by the `embassy_executor_macros::main!` macro to throw an error when spawn |
| 191 | // fails. This is here to allow conditional use of `defmt::unwrap!` | 158 | // fails. This is here to allow conditional use of `defmt::unwrap!` |
| 192 | // without introducing a `defmt` feature in the `embassy_executor_macros` package, | 159 | // without introducing a `defmt` feature in the `embassy_executor_macros` package, |
