diff options
| author | Dániel Buga <[email protected]> | 2025-12-18 09:57:52 +0100 |
|---|---|---|
| committer | Dániel Buga <[email protected]> | 2025-12-18 09:57:52 +0100 |
| commit | e62adea1f93f8eaba2f9da5ebe90e5f23a480101 (patch) | |
| tree | 2fd8c45cd22a522798b1655cba19af257d5d232b | |
| parent | e2d9adc56481e97fa490c8c3cc05bdad0dbe22e4 (diff) | |
executor: Add fallible from_waker getter
| -rw-r--r-- | embassy-executor/CHANGELOG.md | 1 | ||||
| -rw-r--r-- | embassy-executor/src/raw/mod.rs | 6 | ||||
| -rw-r--r-- | embassy-executor/src/raw/waker.rs | 13 | ||||
| -rw-r--r-- | embassy-executor/src/raw/waker_turbo.rs | 4 |
4 files changed, 20 insertions, 4 deletions
diff --git a/embassy-executor/CHANGELOG.md b/embassy-executor/CHANGELOG.md index 8f1db7de7..cc4027fca 100644 --- a/embassy-executor/CHANGELOG.md +++ b/embassy-executor/CHANGELOG.md | |||
| @@ -16,6 +16,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 | |||
| 16 | - Migrate `cortex-ar` to `aarch32-cpu`. The feature name `arch-cortex-ar` remains the same and | 16 | - Migrate `cortex-ar` to `aarch32-cpu`. The feature name `arch-cortex-ar` remains the same and |
| 17 | legacy ARM architectures are not supported. | 17 | legacy ARM architectures are not supported. |
| 18 | - Added `run_until` to `arch-std` variant of `Executor`. | 18 | - Added `run_until` to `arch-std` variant of `Executor`. |
| 19 | - Added `__try_embassy_time_queue_item_from_waker` | ||
| 19 | 20 | ||
| 20 | ## 0.9.1 - 2025-08-31 | 21 | ## 0.9.1 - 2025-08-31 |
| 21 | 22 | ||
diff --git a/embassy-executor/src/raw/mod.rs b/embassy-executor/src/raw/mod.rs index ab845ed3b..2b7560de6 100644 --- a/embassy-executor/src/raw/mod.rs +++ b/embassy-executor/src/raw/mod.rs | |||
| @@ -49,6 +49,7 @@ use self::run_queue::{RunQueue, RunQueueItem}; | |||
| 49 | use self::state::State; | 49 | use self::state::State; |
| 50 | use self::util::{SyncUnsafeCell, UninitCell}; | 50 | use self::util::{SyncUnsafeCell, UninitCell}; |
| 51 | pub use self::waker::task_from_waker; | 51 | pub use self::waker::task_from_waker; |
| 52 | use self::waker::try_task_from_waker; | ||
| 52 | use super::SpawnToken; | 53 | use super::SpawnToken; |
| 53 | use crate::{Metadata, SpawnError}; | 54 | use crate::{Metadata, SpawnError}; |
| 54 | 55 | ||
| @@ -57,6 +58,11 @@ extern "Rust" fn __embassy_time_queue_item_from_waker(waker: &Waker) -> &'static | |||
| 57 | unsafe { task_from_waker(waker).timer_queue_item() } | 58 | unsafe { task_from_waker(waker).timer_queue_item() } |
| 58 | } | 59 | } |
| 59 | 60 | ||
| 61 | #[unsafe(no_mangle)] | ||
| 62 | extern "Rust" fn __try_embassy_time_queue_item_from_waker(waker: &Waker) -> Option<&'static mut TimerQueueItem> { | ||
| 63 | unsafe { try_task_from_waker(waker).map(|task| task.timer_queue_item()) } | ||
| 64 | } | ||
| 65 | |||
| 60 | /// Raw task header for use in task pointers. | 66 | /// Raw task header for use in task pointers. |
| 61 | /// | 67 | /// |
| 62 | /// A task can be in one of the following states: | 68 | /// A task can be in one of the following states: |
diff --git a/embassy-executor/src/raw/waker.rs b/embassy-executor/src/raw/waker.rs index 2706f0fdf..8416f9f93 100644 --- a/embassy-executor/src/raw/waker.rs +++ b/embassy-executor/src/raw/waker.rs | |||
| @@ -32,13 +32,18 @@ pub(crate) unsafe fn from_task(p: TaskRef) -> Waker { | |||
| 32 | /// | 32 | /// |
| 33 | /// Panics if the waker is not created by the Embassy executor. | 33 | /// Panics if the waker is not created by the Embassy executor. |
| 34 | pub fn task_from_waker(waker: &Waker) -> TaskRef { | 34 | pub fn task_from_waker(waker: &Waker) -> TaskRef { |
| 35 | unwrap!( | ||
| 36 | try_task_from_waker(waker), | ||
| 37 | "Found waker not created by the Embassy executor. Unless the generic timer queue is enabled, `embassy_time::Timer` only works with the Embassy executor." | ||
| 38 | ) | ||
| 39 | } | ||
| 40 | |||
| 41 | pub(crate) fn try_task_from_waker(waker: &Waker) -> Option<TaskRef> { | ||
| 35 | // make sure to compare vtable addresses. Doing `==` on the references | 42 | // make sure to compare vtable addresses. Doing `==` on the references |
| 36 | // will compare the contents, which is slower. | 43 | // will compare the contents, which is slower. |
| 37 | if waker.vtable() as *const _ != &VTABLE as *const _ { | 44 | if waker.vtable() as *const _ != &VTABLE as *const _ { |
| 38 | panic!( | 45 | return None; |
| 39 | "Found waker not created by the Embassy executor. `embassy_time::Timer` only works with the Embassy executor." | ||
| 40 | ) | ||
| 41 | } | 46 | } |
| 42 | // safety: our wakers are always created with `TaskRef::as_ptr` | 47 | // safety: our wakers are always created with `TaskRef::as_ptr` |
| 43 | unsafe { TaskRef::from_ptr(waker.data() as *const TaskHeader) } | 48 | Some(unsafe { TaskRef::from_ptr(waker.data() as *const TaskHeader) }) |
| 44 | } | 49 | } |
diff --git a/embassy-executor/src/raw/waker_turbo.rs b/embassy-executor/src/raw/waker_turbo.rs index 919bcc61a..ee33e7633 100644 --- a/embassy-executor/src/raw/waker_turbo.rs +++ b/embassy-executor/src/raw/waker_turbo.rs | |||
| @@ -25,6 +25,10 @@ pub fn task_from_waker(waker: &Waker) -> TaskRef { | |||
| 25 | unsafe { TaskRef::from_ptr(ptr as *const TaskHeader) } | 25 | unsafe { TaskRef::from_ptr(ptr as *const TaskHeader) } |
| 26 | } | 26 | } |
| 27 | 27 | ||
| 28 | pub(crate) fn try_task_from_waker(waker: &Waker) -> Option<TaskRef> { | ||
| 29 | Some(task_from_waker(waker)) | ||
| 30 | } | ||
| 31 | |||
| 28 | #[inline(never)] | 32 | #[inline(never)] |
| 29 | #[unsafe(no_mangle)] | 33 | #[unsafe(no_mangle)] |
| 30 | fn _turbo_wake(ptr: NonNull<()>) { | 34 | fn _turbo_wake(ptr: NonNull<()>) { |
