diff options
| author | Peter Krull <[email protected]> | 2024-09-23 19:02:59 +0200 |
|---|---|---|
| committer | GitHub <[email protected]> | 2024-09-23 19:02:59 +0200 |
| commit | a2c473306f4a7c8e99add2546450ab3a7a97436e (patch) | |
| tree | 5522a708e492db7d4632dc0a56fe5057244f03f0 /embassy-executor/src/raw | |
| parent | e02a987bafd4f0fcf9d80e7c4f6e1504b8b02cec (diff) | |
| parent | 2935290a6222536d6341103f91bfd732165d3862 (diff) | |
Merge branch 'embassy-rs:main' into multi-signal
Diffstat (limited to 'embassy-executor/src/raw')
| -rw-r--r-- | embassy-executor/src/raw/mod.rs | 2 | ||||
| -rw-r--r-- | embassy-executor/src/raw/waker.rs | 38 |
2 files changed, 25 insertions, 15 deletions
diff --git a/embassy-executor/src/raw/mod.rs b/embassy-executor/src/raw/mod.rs index 3d5e3ab9f..d9ea5c005 100644 --- a/embassy-executor/src/raw/mod.rs +++ b/embassy-executor/src/raw/mod.rs | |||
| @@ -30,7 +30,7 @@ use core::ptr::NonNull; | |||
| 30 | use core::task::{Context, Poll}; | 30 | use core::task::{Context, Poll}; |
| 31 | 31 | ||
| 32 | #[cfg(feature = "integrated-timers")] | 32 | #[cfg(feature = "integrated-timers")] |
| 33 | use embassy_time_driver::{self, AlarmHandle}; | 33 | use embassy_time_driver::AlarmHandle; |
| 34 | #[cfg(feature = "rtos-trace")] | 34 | #[cfg(feature = "rtos-trace")] |
| 35 | use rtos_trace::trace; | 35 | use rtos_trace::trace; |
| 36 | 36 | ||
diff --git a/embassy-executor/src/raw/waker.rs b/embassy-executor/src/raw/waker.rs index 522853e34..8bb2cfd05 100644 --- a/embassy-executor/src/raw/waker.rs +++ b/embassy-executor/src/raw/waker.rs | |||
| @@ -1,4 +1,3 @@ | |||
| 1 | use core::mem; | ||
| 2 | use core::task::{RawWaker, RawWakerVTable, Waker}; | 1 | use core::task::{RawWaker, RawWakerVTable, Waker}; |
| 3 | 2 | ||
| 4 | use super::{wake_task, TaskHeader, TaskRef}; | 3 | use super::{wake_task, TaskHeader, TaskRef}; |
| @@ -33,20 +32,31 @@ pub(crate) unsafe fn from_task(p: TaskRef) -> Waker { | |||
| 33 | /// | 32 | /// |
| 34 | /// Panics if the waker is not created by the Embassy executor. | 33 | /// Panics if the waker is not created by the Embassy executor. |
| 35 | pub fn task_from_waker(waker: &Waker) -> TaskRef { | 34 | pub fn task_from_waker(waker: &Waker) -> TaskRef { |
| 36 | // safety: OK because WakerHack has the same layout as Waker. | 35 | let (vtable, data) = { |
| 37 | // This is not really guaranteed because the structs are `repr(Rust)`, it is | 36 | #[cfg(not(feature = "nightly"))] |
| 38 | // indeed the case in the current implementation. | 37 | { |
| 39 | // TODO use waker_getters when stable. https://github.com/rust-lang/rust/issues/96992 | 38 | struct WakerHack { |
| 40 | let hack: &WakerHack = unsafe { mem::transmute(waker) }; | 39 | data: *const (), |
| 41 | if hack.vtable != &VTABLE { | 40 | vtable: &'static RawWakerVTable, |
| 41 | } | ||
| 42 | |||
| 43 | // safety: OK because WakerHack has the same layout as Waker. | ||
| 44 | // This is not really guaranteed because the structs are `repr(Rust)`, it is | ||
| 45 | // indeed the case in the current implementation. | ||
| 46 | // TODO use waker_getters when stable. https://github.com/rust-lang/rust/issues/96992 | ||
| 47 | let hack: &WakerHack = unsafe { core::mem::transmute(waker) }; | ||
| 48 | (hack.vtable, hack.data) | ||
| 49 | } | ||
| 50 | |||
| 51 | #[cfg(feature = "nightly")] | ||
| 52 | { | ||
| 53 | (waker.vtable(), waker.data()) | ||
| 54 | } | ||
| 55 | }; | ||
| 56 | |||
| 57 | if vtable != &VTABLE { | ||
| 42 | panic!("Found waker not created by the Embassy executor. `embassy_time::Timer` only works with the Embassy executor.") | 58 | panic!("Found waker not created by the Embassy executor. `embassy_time::Timer` only works with the Embassy executor.") |
| 43 | } | 59 | } |
| 44 | |||
| 45 | // safety: our wakers are always created with `TaskRef::as_ptr` | 60 | // safety: our wakers are always created with `TaskRef::as_ptr` |
| 46 | unsafe { TaskRef::from_ptr(hack.data as *const TaskHeader) } | 61 | unsafe { TaskRef::from_ptr(data as *const TaskHeader) } |
| 47 | } | ||
| 48 | |||
| 49 | struct WakerHack { | ||
| 50 | data: *const (), | ||
| 51 | vtable: &'static RawWakerVTable, | ||
| 52 | } | 62 | } |
