diff options
Diffstat (limited to 'embassy-executor/src/spawner.rs')
| -rw-r--r-- | embassy-executor/src/spawner.rs | 65 |
1 files changed, 9 insertions, 56 deletions
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 | } |
