diff options
| author | Dario Nieuwenhuis <[email protected]> | 2025-07-09 01:18:04 +0200 |
|---|---|---|
| committer | diondokter <[email protected]> | 2025-08-29 13:22:59 +0200 |
| commit | da9cdf0c536ec4fa7bdfb649750c44f70ef1cd55 (patch) | |
| tree | a080b8663037d8d4af8fc3998360faa80c45fb02 /embassy-executor/src/spawner.rs | |
| parent | 2ba34ce2178d576f339f0b0dac70ac125f81cc5b (diff) | |
executor: add "task metadata" concept, make name a task metadata.
Diffstat (limited to 'embassy-executor/src/spawner.rs')
| -rw-r--r-- | embassy-executor/src/spawner.rs | 66 |
1 files changed, 13 insertions, 53 deletions
diff --git a/embassy-executor/src/spawner.rs b/embassy-executor/src/spawner.rs index 7550e8ea4..cd2113a28 100644 --- a/embassy-executor/src/spawner.rs +++ b/embassy-executor/src/spawner.rs | |||
| @@ -5,8 +5,7 @@ use core::sync::atomic::Ordering; | |||
| 5 | use core::task::Poll; | 5 | use core::task::Poll; |
| 6 | 6 | ||
| 7 | use super::raw; | 7 | use super::raw; |
| 8 | #[cfg(feature = "trace")] | 8 | use crate::Metadata; |
| 9 | use crate::raw::trace::TaskRefTrace; | ||
| 10 | 9 | ||
| 11 | /// Token to spawn a newly-created task in an executor. | 10 | /// Token to spawn a newly-created task in an executor. |
| 12 | /// | 11 | /// |
| @@ -36,6 +35,14 @@ impl<S> SpawnToken<S> { | |||
| 36 | } | 35 | } |
| 37 | } | 36 | } |
| 38 | 37 | ||
| 38 | /// Return a SpawnToken that represents a failed spawn. | ||
| 39 | pub fn new_failed() -> Self { | ||
| 40 | Self { | ||
| 41 | raw_task: None, | ||
| 42 | phantom: PhantomData, | ||
| 43 | } | ||
| 44 | } | ||
| 45 | |||
| 39 | /// Returns the task ID if available, otherwise 0 | 46 | /// Returns the task ID if available, otherwise 0 |
| 40 | /// This can be used in combination with rtos-trace to match task names with IDs | 47 | /// This can be used in combination with rtos-trace to match task names with IDs |
| 41 | pub fn id(&self) -> u32 { | 48 | pub fn id(&self) -> u32 { |
| @@ -45,12 +52,10 @@ impl<S> SpawnToken<S> { | |||
| 45 | } | 52 | } |
| 46 | } | 53 | } |
| 47 | 54 | ||
| 48 | /// Return a SpawnToken that represents a failed spawn. | 55 | /// Get the metadata for this task. You can use this to set metadata fields |
| 49 | pub fn new_failed() -> Self { | 56 | /// prior to spawning it. |
| 50 | Self { | 57 | pub fn metadata(&self) -> &Metadata { |
| 51 | raw_task: None, | 58 | self.raw_task.unwrap().metadata() |
| 52 | phantom: PhantomData, | ||
| 53 | } | ||
| 54 | } | 59 | } |
| 55 | } | 60 | } |
| 56 | 61 | ||
| @@ -198,51 +203,6 @@ impl Spawner { | |||
| 198 | } | 203 | } |
| 199 | } | 204 | } |
| 200 | 205 | ||
| 201 | /// Extension trait adding tracing capabilities to the Spawner | ||
| 202 | /// | ||
| 203 | /// This trait provides an additional method to spawn tasks with an associated name, | ||
| 204 | /// which can be useful for debugging and tracing purposes. | ||
| 205 | pub trait SpawnerTraceExt { | ||
| 206 | /// Spawns a new task with a specified name. | ||
| 207 | /// | ||
| 208 | /// # Arguments | ||
| 209 | /// * `name` - Static string name to associate with the task | ||
| 210 | /// * `token` - Token representing the task to spawn | ||
| 211 | /// | ||
| 212 | /// # Returns | ||
| 213 | /// Result indicating whether the spawn was successful | ||
| 214 | fn spawn_named<S>(&self, name: &'static str, token: SpawnToken<S>) -> Result<(), SpawnError>; | ||
| 215 | } | ||
| 216 | |||
| 217 | /// Implementation of the SpawnerTraceExt trait for Spawner when trace is enabled | ||
| 218 | #[cfg(feature = "trace")] | ||
| 219 | impl SpawnerTraceExt for Spawner { | ||
| 220 | fn spawn_named<S>(&self, name: &'static str, token: SpawnToken<S>) -> Result<(), SpawnError> { | ||
| 221 | let task = token.raw_task; | ||
| 222 | core::mem::forget(token); | ||
| 223 | |||
| 224 | match task { | ||
| 225 | Some(task) => { | ||
| 226 | // Set the name when trace is enabled | ||
| 227 | task.set_name(Some(name)); | ||
| 228 | |||
| 229 | unsafe { self.executor.spawn(task) }; | ||
| 230 | Ok(()) | ||
| 231 | } | ||
| 232 | None => Err(SpawnError::Busy), | ||
| 233 | } | ||
| 234 | } | ||
| 235 | } | ||
| 236 | |||
| 237 | /// Implementation of the SpawnerTraceExt trait for Spawner when trace is disabled | ||
| 238 | #[cfg(not(feature = "trace"))] | ||
| 239 | impl SpawnerTraceExt for Spawner { | ||
| 240 | fn spawn_named<S>(&self, _name: &'static str, token: SpawnToken<S>) -> Result<(), SpawnError> { | ||
| 241 | // When trace is disabled, just forward to regular spawn and ignore the name | ||
| 242 | self.spawn(token) | ||
| 243 | } | ||
| 244 | } | ||
| 245 | |||
| 246 | /// Handle to spawn tasks into an executor from any thread. | 206 | /// Handle to spawn tasks into an executor from any thread. |
| 247 | /// | 207 | /// |
| 248 | /// This Spawner can be used from any thread (it is Send), but it can | 208 | /// This Spawner can be used from any thread (it is Send), but it can |
