aboutsummaryrefslogtreecommitdiff
path: root/embassy-executor/src/executor/raw/waker.rs
diff options
context:
space:
mode:
authorDario Nieuwenhuis <[email protected]>2022-08-17 23:40:16 +0200
committerDario Nieuwenhuis <[email protected]>2022-08-18 01:22:30 +0200
commit5daa173ce4b153a532b4daa9e94c7a248231f25b (patch)
tree2ef0b4d6f9b1c02dac2589e7b57982c20cbc0e66 /embassy-executor/src/executor/raw/waker.rs
parent1c5b54a4823d596db730eb476c3ab78110557214 (diff)
Split embassy-time from embassy-executor.
Diffstat (limited to 'embassy-executor/src/executor/raw/waker.rs')
-rw-r--r--embassy-executor/src/executor/raw/waker.rs53
1 files changed, 0 insertions, 53 deletions
diff --git a/embassy-executor/src/executor/raw/waker.rs b/embassy-executor/src/executor/raw/waker.rs
deleted file mode 100644
index 6b9c03a62..000000000
--- a/embassy-executor/src/executor/raw/waker.rs
+++ /dev/null
@@ -1,53 +0,0 @@
1use core::mem;
2use core::ptr::NonNull;
3use core::task::{RawWaker, RawWakerVTable, Waker};
4
5use super::{wake_task, TaskHeader};
6
7const VTABLE: RawWakerVTable = RawWakerVTable::new(clone, wake, wake, drop);
8
9unsafe fn clone(p: *const ()) -> RawWaker {
10 RawWaker::new(p, &VTABLE)
11}
12
13unsafe fn wake(p: *const ()) {
14 wake_task(NonNull::new_unchecked(p as *mut TaskHeader))
15}
16
17unsafe fn drop(_: *const ()) {
18 // nop
19}
20
21pub(crate) unsafe fn from_task(p: NonNull<TaskHeader>) -> Waker {
22 Waker::from_raw(RawWaker::new(p.as_ptr() as _, &VTABLE))
23}
24
25/// Get a task pointer from a waker.
26///
27/// This can be used as an optimization in wait queues to store task pointers
28/// (1 word) instead of full Wakers (2 words). This saves a bit of RAM and helps
29/// avoid dynamic dispatch.
30///
31/// You can use the returned task pointer to wake the task with [`wake_task`](super::wake_task).
32///
33/// # Panics
34///
35/// Panics if the waker is not created by the Embassy executor.
36pub fn task_from_waker(waker: &Waker) -> NonNull<TaskHeader> {
37 // safety: OK because WakerHack has the same layout as Waker.
38 // This is not really guaranteed because the structs are `repr(Rust)`, it is
39 // indeed the case in the current implementation.
40 // TODO use waker_getters when stable. https://github.com/rust-lang/rust/issues/96992
41 let hack: &WakerHack = unsafe { mem::transmute(waker) };
42 if hack.vtable != &VTABLE {
43 panic!("Found waker not created by the Embassy executor. `embassy_executor::time::Timer` only works with the Embassy executor.")
44 }
45
46 // safety: we never create a waker with a null data pointer.
47 unsafe { NonNull::new_unchecked(hack.data as *mut TaskHeader) }
48}
49
50struct WakerHack {
51 data: *const (),
52 vtable: &'static RawWakerVTable,
53}