aboutsummaryrefslogtreecommitdiff
path: root/embassy-executor/src/spawner.rs
diff options
context:
space:
mode:
Diffstat (limited to 'embassy-executor/src/spawner.rs')
-rw-r--r--embassy-executor/src/spawner.rs50
1 files changed, 50 insertions, 0 deletions
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
7use super::raw; 7use super::raw;
8 8
9#[cfg(feature = "trace")]
10use 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.
190pub 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")]
204impl 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"))]
226impl 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