diff options
Diffstat (limited to 'embassy-executor/src')
| -rw-r--r-- | embassy-executor/src/lib.rs | 51 | ||||
| -rw-r--r-- | embassy-executor/src/raw/trace.rs | 12 | ||||
| -rw-r--r-- | embassy-executor/src/spawner.rs | 18 |
3 files changed, 77 insertions, 4 deletions
diff --git a/embassy-executor/src/lib.rs b/embassy-executor/src/lib.rs index dfe420bab..e174a0594 100644 --- a/embassy-executor/src/lib.rs +++ b/embassy-executor/src/lib.rs | |||
| @@ -65,8 +65,17 @@ pub mod _export { | |||
| 65 | 65 | ||
| 66 | use crate::raw::TaskPool; | 66 | use crate::raw::TaskPool; |
| 67 | 67 | ||
| 68 | trait TaskReturnValue {} | ||
| 69 | impl TaskReturnValue for () {} | ||
| 70 | impl TaskReturnValue for Never {} | ||
| 71 | |||
| 72 | #[diagnostic::on_unimplemented( | ||
| 73 | message = "task futures must resolve to `()` or `!`", | ||
| 74 | note = "use `async fn` or change the return type to `impl Future<Output = ()>`" | ||
| 75 | )] | ||
| 76 | #[allow(private_bounds)] | ||
| 68 | pub trait TaskFn<Args>: Copy { | 77 | pub trait TaskFn<Args>: Copy { |
| 69 | type Fut: Future + 'static; | 78 | type Fut: Future<Output: TaskReturnValue> + 'static; |
| 70 | } | 79 | } |
| 71 | 80 | ||
| 72 | macro_rules! task_fn_impl { | 81 | macro_rules! task_fn_impl { |
| @@ -74,7 +83,7 @@ pub mod _export { | |||
| 74 | impl<F, Fut, $($Tn,)*> TaskFn<($($Tn,)*)> for F | 83 | impl<F, Fut, $($Tn,)*> TaskFn<($($Tn,)*)> for F |
| 75 | where | 84 | where |
| 76 | F: Copy + FnOnce($($Tn,)*) -> Fut, | 85 | F: Copy + FnOnce($($Tn,)*) -> Fut, |
| 77 | Fut: Future + 'static, | 86 | Fut: Future<Output: TaskReturnValue> + 'static, |
| 78 | { | 87 | { |
| 79 | type Fut = Fut; | 88 | type Fut = Fut; |
| 80 | } | 89 | } |
| @@ -205,4 +214,42 @@ pub mod _export { | |||
| 205 | Align268435456: 268435456, | 214 | Align268435456: 268435456, |
| 206 | Align536870912: 536870912, | 215 | Align536870912: 536870912, |
| 207 | ); | 216 | ); |
| 217 | |||
| 218 | #[allow(dead_code)] | ||
| 219 | trait HasOutput { | ||
| 220 | type Output; | ||
| 221 | } | ||
| 222 | |||
| 223 | impl<O> HasOutput for fn() -> O { | ||
| 224 | type Output = O; | ||
| 225 | } | ||
| 226 | |||
| 227 | #[allow(dead_code)] | ||
| 228 | type Never = <fn() -> ! as HasOutput>::Output; | ||
| 229 | } | ||
| 230 | |||
| 231 | /// Implementation details for embassy macros. | ||
| 232 | /// Do not use. Used for macros and HALs only. Not covered by semver guarantees. | ||
| 233 | #[doc(hidden)] | ||
| 234 | #[cfg(feature = "nightly")] | ||
| 235 | pub mod _export { | ||
| 236 | #[diagnostic::on_unimplemented( | ||
| 237 | message = "task futures must resolve to `()` or `!`", | ||
| 238 | note = "use `async fn` or change the return type to `impl Future<Output = ()>`" | ||
| 239 | )] | ||
| 240 | pub trait TaskReturnValue {} | ||
| 241 | impl TaskReturnValue for () {} | ||
| 242 | impl TaskReturnValue for Never {} | ||
| 243 | |||
| 244 | #[allow(dead_code)] | ||
| 245 | trait HasOutput { | ||
| 246 | type Output; | ||
| 247 | } | ||
| 248 | |||
| 249 | impl<O> HasOutput for fn() -> O { | ||
| 250 | type Output = O; | ||
| 251 | } | ||
| 252 | |||
| 253 | #[allow(dead_code)] | ||
| 254 | type Never = <fn() -> ! as HasOutput>::Output; | ||
| 208 | } | 255 | } |
diff --git a/embassy-executor/src/raw/trace.rs b/embassy-executor/src/raw/trace.rs index 6c9cfda25..aa27ab37e 100644 --- a/embassy-executor/src/raw/trace.rs +++ b/embassy-executor/src/raw/trace.rs | |||
| @@ -283,7 +283,17 @@ pub(crate) fn task_new(executor: &SyncExecutor, task: &TaskRef) { | |||
| 283 | } | 283 | } |
| 284 | 284 | ||
| 285 | #[cfg(feature = "rtos-trace")] | 285 | #[cfg(feature = "rtos-trace")] |
| 286 | rtos_trace::trace::task_new(task.as_ptr() as u32); | 286 | { |
| 287 | rtos_trace::trace::task_new(task.as_ptr() as u32); | ||
| 288 | let name = task.name().unwrap_or("unnamed task\0"); | ||
| 289 | let info = rtos_trace::TaskInfo { | ||
| 290 | name, | ||
| 291 | priority: 0, | ||
| 292 | stack_base: 0, | ||
| 293 | stack_size: 0, | ||
| 294 | }; | ||
| 295 | rtos_trace::trace::task_send_info(task.id(), info); | ||
| 296 | } | ||
| 287 | 297 | ||
| 288 | #[cfg(feature = "rtos-trace")] | 298 | #[cfg(feature = "rtos-trace")] |
| 289 | TASK_TRACKER.add(*task); | 299 | TASK_TRACKER.add(*task); |
diff --git a/embassy-executor/src/spawner.rs b/embassy-executor/src/spawner.rs index 522d97db3..2909d19a0 100644 --- a/embassy-executor/src/spawner.rs +++ b/embassy-executor/src/spawner.rs | |||
| @@ -122,10 +122,26 @@ impl Spawner { | |||
| 122 | /// This function is `async` just to get access to the current async | 122 | /// This function is `async` just to get access to the current async |
| 123 | /// context. It returns instantly, it does not block/yield. | 123 | /// context. It returns instantly, it does not block/yield. |
| 124 | /// | 124 | /// |
| 125 | /// Using this method is discouraged due to it being unsafe. Consider the following | ||
| 126 | /// alternatives instead: | ||
| 127 | /// | ||
| 128 | /// - Pass the initial `Spawner` as an argument to tasks. Note that it's `Copy`, so you can | ||
| 129 | /// make as many copies of it as you want. | ||
| 130 | /// - Use `SendSpawner::for_current_executor()` instead, which is safe but can only be used | ||
| 131 | /// if task arguments are `Send`. | ||
| 132 | /// | ||
| 133 | /// The only case where using this method is absolutely required is obtaining the `Spawner` | ||
| 134 | /// for an `InterruptExecutor`. | ||
| 135 | /// | ||
| 136 | /// # Safety | ||
| 137 | /// | ||
| 138 | /// You must only execute this with an async `Context` created by the Embassy executor. | ||
| 139 | /// You must not execute it with manually-created `Context`s. | ||
| 140 | /// | ||
| 125 | /// # Panics | 141 | /// # Panics |
| 126 | /// | 142 | /// |
| 127 | /// Panics if the current executor is not an Embassy executor. | 143 | /// Panics if the current executor is not an Embassy executor. |
| 128 | pub fn for_current_executor() -> impl Future<Output = Self> { | 144 | pub unsafe fn for_current_executor() -> impl Future<Output = Self> { |
| 129 | poll_fn(|cx| { | 145 | poll_fn(|cx| { |
| 130 | let task = raw::task_from_waker(cx.waker()); | 146 | let task = raw::task_from_waker(cx.waker()); |
| 131 | let executor = unsafe { | 147 | let executor = unsafe { |
