aboutsummaryrefslogtreecommitdiff
path: root/embassy-executor/src/raw/waker.rs
diff options
context:
space:
mode:
authorzjp <[email protected]>2024-06-13 07:33:40 +0800
committerzjp <[email protected]>2024-06-13 07:33:40 +0800
commitb780df5f76adaf68d041d13820c455d4bf7eed96 (patch)
tree5af13d73bb705cc33714c1cd57ad2d4a31705415 /embassy-executor/src/raw/waker.rs
parent6b9470be2c3551863387ea5395d605dd8a853132 (diff)
put cfg code inside task_from_waker function
Diffstat (limited to 'embassy-executor/src/raw/waker.rs')
-rw-r--r--embassy-executor/src/raw/waker.rs62
1 files changed, 26 insertions, 36 deletions
diff --git a/embassy-executor/src/raw/waker.rs b/embassy-executor/src/raw/waker.rs
index 421ec301e..75641c257 100644
--- a/embassy-executor/src/raw/waker.rs
+++ b/embassy-executor/src/raw/waker.rs
@@ -31,46 +31,36 @@ pub(crate) unsafe fn from_task(p: TaskRef) -> Waker {
31/// # Panics 31/// # Panics
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#[cfg(not(feature = "nightly"))]
35pub fn task_from_waker(waker: &Waker) -> TaskRef { 34pub fn task_from_waker(waker: &Waker) -> TaskRef {
36 // safety: OK because WakerHack has the same layout as Waker. 35 #[cfg(not(feature = "nightly"))]
37 // This is not really guaranteed because the structs are `repr(Rust)`, it is 36 {
38 // indeed the case in the current implementation. 37 struct WakerHack {
39 // TODO use waker_getters when stable. https://github.com/rust-lang/rust/issues/96992 38 data: *const (),
40 let hack: &WakerHack = unsafe { core::mem::transmute(waker) }; 39 vtable: &'static RawWakerVTable,
41 if hack.vtable != &VTABLE { 40 }
42 panic!("Found waker not created by the Embassy executor. `embassy_time::Timer` only works with the Embassy executor.")
43 }
44 41
45 // safety: our wakers are always created with `TaskRef::as_ptr` 42 // safety: OK because WakerHack has the same layout as Waker.
46 unsafe { TaskRef::from_ptr(hack.data as *const TaskHeader) } 43 // This is not really guaranteed because the structs are `repr(Rust)`, it is
47} 44 // indeed the case in the current implementation.
45 // TODO use waker_getters when stable. https://github.com/rust-lang/rust/issues/96992
46 let hack: &WakerHack = unsafe { core::mem::transmute(waker) };
47 if hack.vtable != &VTABLE {
48 panic!("Found waker not created by the Embassy executor. `embassy_time::Timer` only works with the Embassy executor.")
49 }
48 50
49#[cfg(not(feature = "nightly"))] 51 // safety: our wakers are always created with `TaskRef::as_ptr`
50struct WakerHack { 52 unsafe { TaskRef::from_ptr(hack.data as *const TaskHeader) }
51 data: *const (), 53 }
52 vtable: &'static RawWakerVTable,
53}
54 54
55/// Get a task pointer from a waker. 55 #[cfg(feature = "nightly")]
56/// 56 {
57/// This can be used as an optimization in wait queues to store task pointers 57 let raw_waker = waker.as_raw();
58/// (1 word) instead of full Wakers (2 words). This saves a bit of RAM and helps
59/// avoid dynamic dispatch.
60///
61/// You can use the returned task pointer to wake the task with [`wake_task`](super::wake_task).
62///
63/// # Panics
64///
65/// Panics if the waker is not created by the Embassy executor.
66#[cfg(feature = "nightly")]
67pub fn task_from_waker(waker: &Waker) -> TaskRef {
68 let raw_waker = waker.as_raw();
69 58
70 if raw_waker.vtable() != &VTABLE { 59 if raw_waker.vtable() != &VTABLE {
71 panic!("Found waker not created by the Embassy executor. `embassy_time::Timer` only works with the Embassy executor.") 60 panic!("Found waker not created by the Embassy executor. `embassy_time::Timer` only works with the Embassy executor.")
72 } 61 }
73 62
74 // safety: our wakers are always created with `TaskRef::as_ptr` 63 // safety: our wakers are always created with `TaskRef::as_ptr`
75 unsafe { TaskRef::from_ptr(raw_waker.data() as *const TaskHeader) } 64 unsafe { TaskRef::from_ptr(raw_waker.data() as *const TaskHeader) }
65 }
76} 66}