/rust/registry/src/index.crates.io-1949cf8c6b5b557f/tokio-1.48.0/src/util/wake.rs
Line | Count | Source |
1 | | use crate::loom::sync::Arc; |
2 | | |
3 | | use std::mem::ManuallyDrop; |
4 | | use std::task::{RawWaker, RawWakerVTable, Waker}; |
5 | | |
6 | | /// Simplified waking interface based on Arcs. |
7 | | pub(crate) trait Wake: Send + Sync + Sized + 'static { |
8 | | /// Wake by value. |
9 | | fn wake(arc_self: Arc<Self>); |
10 | | |
11 | | /// Wake by reference. |
12 | | fn wake_by_ref(arc_self: &Arc<Self>); |
13 | | } |
14 | | |
15 | | cfg_rt! { |
16 | | use std::marker::PhantomData; |
17 | | use std::ops::Deref; |
18 | | |
19 | | /// A `Waker` that is only valid for a given lifetime. |
20 | | #[derive(Debug)] |
21 | | pub(crate) struct WakerRef<'a> { |
22 | | waker: ManuallyDrop<Waker>, |
23 | | _p: PhantomData<&'a ()>, |
24 | | } |
25 | | |
26 | | impl Deref for WakerRef<'_> { |
27 | | type Target = Waker; |
28 | | |
29 | 0 | fn deref(&self) -> &Waker { |
30 | 0 | &self.waker |
31 | 0 | } |
32 | | } |
33 | | |
34 | | /// Creates a reference to a `Waker` from a reference to `Arc<impl Wake>`. |
35 | 0 | pub(crate) fn waker_ref<W: Wake>(wake: &Arc<W>) -> WakerRef<'_> { |
36 | 0 | let ptr = Arc::as_ptr(wake).cast::<()>(); |
37 | | |
38 | 0 | let waker = unsafe { Waker::from_raw(RawWaker::new(ptr, waker_vtable::<W>())) }; |
39 | | |
40 | 0 | WakerRef { |
41 | 0 | waker: ManuallyDrop::new(waker), |
42 | 0 | _p: PhantomData, |
43 | 0 | } |
44 | 0 | } |
45 | | } |
46 | | |
47 | | /// Creates a waker from a `Arc<impl Wake>`. |
48 | 0 | pub(crate) fn waker<W: Wake>(wake: Arc<W>) -> Waker { |
49 | | unsafe { |
50 | 0 | Waker::from_raw(RawWaker::new( |
51 | 0 | Arc::into_raw(wake).cast(), |
52 | 0 | waker_vtable::<W>(), |
53 | | )) |
54 | | } |
55 | 0 | } |
56 | | |
57 | 0 | fn waker_vtable<W: Wake>() -> &'static RawWakerVTable { |
58 | 0 | &RawWakerVTable::new( |
59 | 0 | clone_arc_raw::<W>, |
60 | 0 | wake_arc_raw::<W>, |
61 | 0 | wake_by_ref_arc_raw::<W>, |
62 | 0 | drop_arc_raw::<W>, |
63 | 0 | ) |
64 | 0 | } Unexecuted instantiation: tokio::util::wake::waker_vtable::<tokio::runtime::park::Inner> Unexecuted instantiation: tokio::util::wake::waker_vtable::<tokio::runtime::scheduler::current_thread::Handle> |
65 | | |
66 | 0 | unsafe fn clone_arc_raw<T: Wake>(data: *const ()) -> RawWaker { |
67 | 0 | Arc::<T>::increment_strong_count(data as *const T); |
68 | 0 | RawWaker::new(data, waker_vtable::<T>()) |
69 | 0 | } Unexecuted instantiation: tokio::util::wake::clone_arc_raw::<tokio::runtime::park::Inner> Unexecuted instantiation: tokio::util::wake::clone_arc_raw::<tokio::runtime::scheduler::current_thread::Handle> |
70 | | |
71 | 0 | unsafe fn wake_arc_raw<T: Wake>(data: *const ()) { |
72 | 0 | let arc: Arc<T> = Arc::from_raw(data as *const T); |
73 | 0 | Wake::wake(arc); |
74 | 0 | } Unexecuted instantiation: tokio::util::wake::wake_arc_raw::<tokio::runtime::park::Inner> Unexecuted instantiation: tokio::util::wake::wake_arc_raw::<tokio::runtime::scheduler::current_thread::Handle> |
75 | | |
76 | | // used by `waker_ref` |
77 | 0 | unsafe fn wake_by_ref_arc_raw<T: Wake>(data: *const ()) { |
78 | | // Retain Arc, but don't touch refcount by wrapping in ManuallyDrop |
79 | 0 | let arc = ManuallyDrop::new(Arc::<T>::from_raw(data.cast())); |
80 | 0 | Wake::wake_by_ref(&arc); |
81 | 0 | } Unexecuted instantiation: tokio::util::wake::wake_by_ref_arc_raw::<tokio::runtime::park::Inner> Unexecuted instantiation: tokio::util::wake::wake_by_ref_arc_raw::<tokio::runtime::scheduler::current_thread::Handle> |
82 | | |
83 | 0 | unsafe fn drop_arc_raw<T: Wake>(data: *const ()) { |
84 | 0 | drop(Arc::<T>::from_raw(data.cast())); |
85 | 0 | } Unexecuted instantiation: tokio::util::wake::drop_arc_raw::<tokio::runtime::park::Inner> Unexecuted instantiation: tokio::util::wake::drop_arc_raw::<tokio::runtime::scheduler::current_thread::Handle> |