diff options
| author | Kat Perez <[email protected]> | 2025-05-08 14:35:43 -0400 |
|---|---|---|
| committer | Kat Perez <[email protected]> | 2025-05-08 14:35:43 -0400 |
| commit | dfaab013ebaaa4a19c06f2eb00821712ff13cf7a (patch) | |
| tree | 9e32aee0654cc4d827d3458447998e90874ddadf /embassy-executor/src | |
| parent | e968c4763694d676cca6f1bd30949619dd12e962 (diff) | |
move SpawnerTraceExt back into Spawner
Diffstat (limited to 'embassy-executor/src')
| -rw-r--r-- | embassy-executor/src/raw/trace.rs | 42 | ||||
| -rw-r--r-- | embassy-executor/src/spawner.rs | 50 |
2 files changed, 50 insertions, 42 deletions
diff --git a/embassy-executor/src/raw/trace.rs b/embassy-executor/src/raw/trace.rs index f55033530..593f7b0ba 100644 --- a/embassy-executor/src/raw/trace.rs +++ b/embassy-executor/src/raw/trace.rs | |||
| @@ -89,48 +89,6 @@ use rtos_trace::TaskInfo; | |||
| 89 | use crate::raw::{SyncExecutor, TaskHeader, TaskRef}; | 89 | use crate::raw::{SyncExecutor, TaskHeader, TaskRef}; |
| 90 | use crate::spawner::{SpawnError, SpawnToken, Spawner}; | 90 | use crate::spawner::{SpawnError, SpawnToken, Spawner}; |
| 91 | 91 | ||
| 92 | /// Extension trait adding tracing capabilities to the Spawner | ||
| 93 | pub trait SpawnerTraceExt { | ||
| 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 SpawnerTraceExt 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 SpawnerTraceExt for Spawner { | ||
| 129 | fn spawn_named<S>(&self, _name: &'static str, token: SpawnToken<S>) -> Result<(), SpawnError> { | ||
| 130 | self.spawn(token) | ||
| 131 | } | ||
| 132 | } | ||
| 133 | |||
| 134 | /// Global task tracker instance | 92 | /// Global task tracker instance |
| 135 | /// | 93 | /// |
| 136 | /// This static provides access to the global task tracker which maintains | 94 | /// This static provides access to the global task tracker which maintains |
diff --git a/embassy-executor/src/spawner.rs b/embassy-executor/src/spawner.rs index 6b8db4f8f..bfb32ebcc 100644 --- a/embassy-executor/src/spawner.rs +++ b/embassy-executor/src/spawner.rs | |||
| @@ -6,6 +6,9 @@ use core::task::Poll; | |||
| 6 | 6 | ||
| 7 | use super::raw; | 7 | use super::raw; |
| 8 | 8 | ||
| 9 | #[cfg(feature = "trace")] | ||
| 10 | use crate::raw::trace::TaskRefTrace; | ||
| 11 | |||
| 9 | /// Token to spawn a newly-created task in an executor. | 12 | /// Token to spawn a newly-created task in an executor. |
| 10 | /// | 13 | /// |
| 11 | /// When calling a task function (like `#[embassy_executor::task] async fn my_task() { ... }`), the returned | 14 | /// When calling a task function (like `#[embassy_executor::task] async fn my_task() { ... }`), the returned |
| @@ -180,6 +183,53 @@ impl Spawner { | |||
| 180 | } | 183 | } |
| 181 | } | 184 | } |
| 182 | 185 | ||
| 186 | /// Extension trait adding tracing capabilities to the Spawner | ||
| 187 | /// | ||
| 188 | /// This trait provides an additional method to spawn tasks with an associated name, | ||
| 189 | /// which can be useful for debugging and tracing purposes. | ||
| 190 | pub trait SpawnerTraceExt { | ||
| 191 | /// Spawns a new task with a specified name. | ||
| 192 | /// | ||
| 193 | /// # Arguments | ||
| 194 | /// * `name` - Static string name to associate with the task | ||
| 195 | /// * `token` - Token representing the task to spawn | ||
| 196 | /// | ||
| 197 | /// # Returns | ||
| 198 | /// Result indicating whether the spawn was successful | ||
| 199 | fn spawn_named<S>(&self, name: &'static str, token: SpawnToken<S>) -> Result<(), SpawnError>; | ||
| 200 | } | ||
| 201 | |||
| 202 | /// Implementation of the SpawnerTraceExt trait for Spawner when trace is enabled | ||
| 203 | #[cfg(feature = "trace")] | ||
| 204 | impl SpawnerTraceExt for Spawner { | ||
| 205 | fn spawn_named<S>(&self, name: &'static str, token: SpawnToken<S>) -> Result<(), SpawnError> { | ||
| 206 | let task = token.raw_task; | ||
| 207 | core::mem::forget(token); | ||
| 208 | |||
| 209 | match task { | ||
| 210 | Some(task) => { | ||
| 211 | // Set the name and ID when trace is enabled | ||
| 212 | task.set_name(Some(name)); | ||
| 213 | let task_id = task.as_ptr() as u32; | ||
| 214 | task.set_id(task_id); | ||
| 215 | |||
| 216 | unsafe { self.executor.spawn(task) }; | ||
| 217 | Ok(()) | ||
| 218 | } | ||
| 219 | None => Err(SpawnError::Busy), | ||
| 220 | } | ||
| 221 | } | ||
| 222 | } | ||
| 223 | |||
| 224 | /// Implementation of the SpawnerTraceExt trait for Spawner when trace is disabled | ||
| 225 | #[cfg(not(feature = "trace"))] | ||
| 226 | impl SpawnerTraceExt for Spawner { | ||
| 227 | fn spawn_named<S>(&self, _name: &'static str, token: SpawnToken<S>) -> Result<(), SpawnError> { | ||
| 228 | // When trace is disabled, just forward to regular spawn and ignore the name | ||
| 229 | self.spawn(token) | ||
| 230 | } | ||
| 231 | } | ||
| 232 | |||
| 183 | /// Handle to spawn tasks into an executor from any thread. | 233 | /// Handle to spawn tasks into an executor from any thread. |
| 184 | /// | 234 | /// |
| 185 | /// This Spawner can be used from any thread (it is Send), but it can | 235 | /// This Spawner can be used from any thread (it is Send), but it can |
