diff options
| author | Dario Nieuwenhuis <[email protected]> | 2025-07-09 01:49:31 +0200 |
|---|---|---|
| committer | diondokter <[email protected]> | 2025-08-29 13:23:21 +0200 |
| commit | 8aec341f28a00012e1771d5c35d2647e11830755 (patch) | |
| tree | 28ec3bad05e5dcb6ec949493688111839bb6865b /embassy-executor/src | |
| parent | 34ff67cdbf25e278ff99bd4a05b6b8c6a30fa5d1 (diff) | |
executor: return error when creating the spawntoken, not when spawning.
Diffstat (limited to 'embassy-executor/src')
| -rw-r--r-- | embassy-executor/src/raw/mod.rs | 18 | ||||
| -rw-r--r-- | embassy-executor/src/spawner.rs | 65 |
2 files changed, 18 insertions, 65 deletions
diff --git a/embassy-executor/src/raw/mod.rs b/embassy-executor/src/raw/mod.rs index a7e65360d..bdaa32951 100644 --- a/embassy-executor/src/raw/mod.rs +++ b/embassy-executor/src/raw/mod.rs | |||
| @@ -41,7 +41,7 @@ use self::state::State; | |||
| 41 | use self::util::{SyncUnsafeCell, UninitCell}; | 41 | use self::util::{SyncUnsafeCell, UninitCell}; |
| 42 | pub use self::waker::task_from_waker; | 42 | pub use self::waker::task_from_waker; |
| 43 | use super::SpawnToken; | 43 | use super::SpawnToken; |
| 44 | use crate::Metadata; | 44 | use crate::{Metadata, SpawnError}; |
| 45 | 45 | ||
| 46 | #[no_mangle] | 46 | #[no_mangle] |
| 47 | extern "Rust" fn __embassy_time_queue_item_from_waker(waker: &Waker) -> &'static mut TimerQueueItem { | 47 | extern "Rust" fn __embassy_time_queue_item_from_waker(waker: &Waker) -> &'static mut TimerQueueItem { |
| @@ -220,11 +220,11 @@ impl<F: Future + 'static> TaskStorage<F> { | |||
| 220 | /// | 220 | /// |
| 221 | /// Once the task has finished running, you may spawn it again. It is allowed to spawn it | 221 | /// Once the task has finished running, you may spawn it again. It is allowed to spawn it |
| 222 | /// on a different executor. | 222 | /// on a different executor. |
| 223 | pub fn spawn(&'static self, future: impl FnOnce() -> F) -> SpawnToken<impl Sized> { | 223 | pub fn spawn(&'static self, future: impl FnOnce() -> F) -> Result<SpawnToken<impl Sized>, SpawnError> { |
| 224 | let task = AvailableTask::claim(self); | 224 | let task = AvailableTask::claim(self); |
| 225 | match task { | 225 | match task { |
| 226 | Some(task) => task.initialize(future), | 226 | Some(task) => Ok(task.initialize(future)), |
| 227 | None => SpawnToken::new_failed(), | 227 | None => Err(SpawnError::Busy), |
| 228 | } | 228 | } |
| 229 | } | 229 | } |
| 230 | 230 | ||
| @@ -353,10 +353,10 @@ impl<F: Future + 'static, const N: usize> TaskPool<F, N> { | |||
| 353 | } | 353 | } |
| 354 | } | 354 | } |
| 355 | 355 | ||
| 356 | fn spawn_impl<T>(&'static self, future: impl FnOnce() -> F) -> SpawnToken<T> { | 356 | fn spawn_impl<T>(&'static self, future: impl FnOnce() -> F) -> Result<SpawnToken<T>, SpawnError> { |
| 357 | match self.pool.iter().find_map(AvailableTask::claim) { | 357 | match self.pool.iter().find_map(AvailableTask::claim) { |
| 358 | Some(task) => task.initialize_impl::<T>(future), | 358 | Some(task) => Ok(task.initialize_impl::<T>(future)), |
| 359 | None => SpawnToken::new_failed(), | 359 | None => Err(SpawnError::Busy), |
| 360 | } | 360 | } |
| 361 | } | 361 | } |
| 362 | 362 | ||
| @@ -367,7 +367,7 @@ impl<F: Future + 'static, const N: usize> TaskPool<F, N> { | |||
| 367 | /// This will loop over the pool and spawn the task in the first storage that | 367 | /// This will loop over the pool and spawn the task in the first storage that |
| 368 | /// is currently free. If none is free, a "poisoned" SpawnToken is returned, | 368 | /// is currently free. If none is free, a "poisoned" SpawnToken is returned, |
| 369 | /// which will cause [`Spawner::spawn()`](super::Spawner::spawn) to return the error. | 369 | /// which will cause [`Spawner::spawn()`](super::Spawner::spawn) to return the error. |
| 370 | pub fn spawn(&'static self, future: impl FnOnce() -> F) -> SpawnToken<impl Sized> { | 370 | pub fn spawn(&'static self, future: impl FnOnce() -> F) -> Result<SpawnToken<impl Sized>, SpawnError> { |
| 371 | self.spawn_impl::<F>(future) | 371 | self.spawn_impl::<F>(future) |
| 372 | } | 372 | } |
| 373 | 373 | ||
| @@ -380,7 +380,7 @@ impl<F: Future + 'static, const N: usize> TaskPool<F, N> { | |||
| 380 | /// SAFETY: `future` must be a closure of the form `move || my_async_fn(args)`, where `my_async_fn` | 380 | /// SAFETY: `future` must be a closure of the form `move || my_async_fn(args)`, where `my_async_fn` |
| 381 | /// is an `async fn`, NOT a hand-written `Future`. | 381 | /// is an `async fn`, NOT a hand-written `Future`. |
| 382 | #[doc(hidden)] | 382 | #[doc(hidden)] |
| 383 | pub unsafe fn _spawn_async_fn<FutFn>(&'static self, future: FutFn) -> SpawnToken<impl Sized> | 383 | pub unsafe fn _spawn_async_fn<FutFn>(&'static self, future: FutFn) -> Result<SpawnToken<impl Sized>, SpawnError> |
| 384 | where | 384 | where |
| 385 | FutFn: FnOnce() -> F, | 385 | FutFn: FnOnce() -> F, |
| 386 | { | 386 | { |
diff --git a/embassy-executor/src/spawner.rs b/embassy-executor/src/spawner.rs index cd2113a28..83d896b76 100644 --- a/embassy-executor/src/spawner.rs +++ b/embassy-executor/src/spawner.rs | |||
| @@ -23,39 +23,28 @@ use crate::Metadata; | |||
| 23 | /// Once you've invoked a task function and obtained a SpawnToken, you *must* spawn it. | 23 | /// Once you've invoked a task function and obtained a SpawnToken, you *must* spawn it. |
| 24 | #[must_use = "Calling a task function does nothing on its own. You must spawn the returned SpawnToken, typically with Spawner::spawn()"] | 24 | #[must_use = "Calling a task function does nothing on its own. You must spawn the returned SpawnToken, typically with Spawner::spawn()"] |
| 25 | pub struct SpawnToken<S> { | 25 | pub struct SpawnToken<S> { |
| 26 | pub(crate) raw_task: Option<raw::TaskRef>, | 26 | pub(crate) raw_task: raw::TaskRef, |
| 27 | phantom: PhantomData<*mut S>, | 27 | phantom: PhantomData<*mut S>, |
| 28 | } | 28 | } |
| 29 | 29 | ||
| 30 | impl<S> SpawnToken<S> { | 30 | impl<S> SpawnToken<S> { |
| 31 | pub(crate) unsafe fn new(raw_task: raw::TaskRef) -> Self { | 31 | pub(crate) unsafe fn new(raw_task: raw::TaskRef) -> Self { |
| 32 | Self { | 32 | Self { |
| 33 | raw_task: Some(raw_task), | 33 | raw_task, |
| 34 | phantom: PhantomData, | 34 | phantom: PhantomData, |
| 35 | } | 35 | } |
| 36 | } | 36 | } |
| 37 | 37 | ||
| 38 | /// Return a SpawnToken that represents a failed spawn. | 38 | /// Returns the task ID. |
| 39 | pub fn new_failed() -> Self { | ||
| 40 | Self { | ||
| 41 | raw_task: None, | ||
| 42 | phantom: PhantomData, | ||
| 43 | } | ||
| 44 | } | ||
| 45 | |||
| 46 | /// Returns the task ID if available, otherwise 0 | ||
| 47 | /// This can be used in combination with rtos-trace to match task names with IDs | 39 | /// This can be used in combination with rtos-trace to match task names with IDs |
| 48 | pub fn id(&self) -> u32 { | 40 | pub fn id(&self) -> u32 { |
| 49 | match self.raw_task { | 41 | self.raw_task.id() |
| 50 | None => 0, | ||
| 51 | Some(t) => t.id(), | ||
| 52 | } | ||
| 53 | } | 42 | } |
| 54 | 43 | ||
| 55 | /// Get the metadata for this task. You can use this to set metadata fields | 44 | /// Get the metadata for this task. You can use this to set metadata fields |
| 56 | /// prior to spawning it. | 45 | /// prior to spawning it. |
| 57 | pub fn metadata(&self) -> &Metadata { | 46 | pub fn metadata(&self) -> &Metadata { |
| 58 | self.raw_task.unwrap().metadata() | 47 | self.raw_task.metadata() |
| 59 | } | 48 | } |
| 60 | } | 49 | } |
| 61 | 50 | ||
| @@ -164,30 +153,10 @@ impl Spawner { | |||
| 164 | /// Spawn a task into an executor. | 153 | /// Spawn a task into an executor. |
| 165 | /// | 154 | /// |
| 166 | /// You obtain the `token` by calling a task function (i.e. one marked with `#[embassy_executor::task]`). | 155 | /// You obtain the `token` by calling a task function (i.e. one marked with `#[embassy_executor::task]`). |
| 167 | pub fn spawn<S>(&self, token: SpawnToken<S>) -> Result<(), SpawnError> { | 156 | pub fn spawn<S>(&self, token: SpawnToken<S>) { |
| 168 | let task = token.raw_task; | 157 | let task = token.raw_task; |
| 169 | mem::forget(token); | 158 | mem::forget(token); |
| 170 | 159 | unsafe { self.executor.spawn(task) } | |
| 171 | match task { | ||
| 172 | Some(task) => { | ||
| 173 | unsafe { self.executor.spawn(task) }; | ||
| 174 | Ok(()) | ||
| 175 | } | ||
| 176 | None => Err(SpawnError::Busy), | ||
| 177 | } | ||
| 178 | } | ||
| 179 | |||
| 180 | // Used by the `embassy_executor_macros::main!` macro to throw an error when spawn | ||
| 181 | // fails. This is here to allow conditional use of `defmt::unwrap!` | ||
| 182 | // without introducing a `defmt` feature in the `embassy_executor_macros` package, | ||
| 183 | // which would require use of `-Z namespaced-features`. | ||
| 184 | /// Spawn a task into an executor, panicking on failure. | ||
| 185 | /// | ||
| 186 | /// # Panics | ||
| 187 | /// | ||
| 188 | /// Panics if the spawning fails. | ||
| 189 | pub fn must_spawn<S>(&self, token: SpawnToken<S>) { | ||
| 190 | unwrap!(self.spawn(token)); | ||
| 191 | } | 160 | } |
| 192 | 161 | ||
| 193 | /// Convert this Spawner to a SendSpawner. This allows you to send the | 162 | /// Convert this Spawner to a SendSpawner. This allows you to send the |
| @@ -245,25 +214,9 @@ impl SendSpawner { | |||
| 245 | /// Spawn a task into an executor. | 214 | /// Spawn a task into an executor. |
| 246 | /// | 215 | /// |
| 247 | /// You obtain the `token` by calling a task function (i.e. one marked with `#[embassy_executor::task]`). | 216 | /// You obtain the `token` by calling a task function (i.e. one marked with `#[embassy_executor::task]`). |
| 248 | pub fn spawn<S: Send>(&self, token: SpawnToken<S>) -> Result<(), SpawnError> { | 217 | pub fn spawn<S: Send>(&self, token: SpawnToken<S>) { |
| 249 | let header = token.raw_task; | 218 | let header = token.raw_task; |
| 250 | mem::forget(token); | 219 | mem::forget(token); |
| 251 | 220 | unsafe { self.executor.spawn(header) } | |
| 252 | match header { | ||
| 253 | Some(header) => { | ||
| 254 | unsafe { self.executor.spawn(header) }; | ||
| 255 | Ok(()) | ||
| 256 | } | ||
| 257 | None => Err(SpawnError::Busy), | ||
| 258 | } | ||
| 259 | } | ||
| 260 | |||
| 261 | /// Spawn a task into an executor, panicking on failure. | ||
| 262 | /// | ||
| 263 | /// # Panics | ||
| 264 | /// | ||
| 265 | /// Panics if the spawning fails. | ||
| 266 | pub fn must_spawn<S: Send>(&self, token: SpawnToken<S>) { | ||
| 267 | unwrap!(self.spawn(token)); | ||
| 268 | } | 221 | } |
| 269 | } | 222 | } |
