/rust/registry/src/index.crates.io-6f17d22bba15001f/tokio-1.46.1/src/util/wake.rs
Line | Count | Source (jump to first uncovered line) |
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 | 0 |
|
38 | 0 | let waker = unsafe { Waker::from_raw(RawWaker::new(ptr, waker_vtable::<W>())) }; |
39 | 0 |
|
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 | 34.3k | pub(crate) fn waker<W: Wake>(wake: Arc<W>) -> Waker { |
49 | 34.3k | unsafe { |
50 | 34.3k | Waker::from_raw(RawWaker::new( |
51 | 34.3k | Arc::into_raw(wake).cast(), |
52 | 34.3k | waker_vtable::<W>(), |
53 | 34.3k | )) |
54 | 34.3k | } |
55 | 34.3k | } |
56 | | |
57 | 2.42M | fn waker_vtable<W: Wake>() -> &'static RawWakerVTable { |
58 | 2.42M | &RawWakerVTable::new( |
59 | 2.42M | clone_arc_raw::<W>, |
60 | 2.42M | wake_arc_raw::<W>, |
61 | 2.42M | wake_by_ref_arc_raw::<W>, |
62 | 2.42M | drop_arc_raw::<W>, |
63 | 2.42M | ) |
64 | 2.42M | } tokio::util::wake::waker_vtable::<tokio::runtime::park::Inner> Line | Count | Source | 57 | 2.42M | fn waker_vtable<W: Wake>() -> &'static RawWakerVTable { | 58 | 2.42M | &RawWakerVTable::new( | 59 | 2.42M | clone_arc_raw::<W>, | 60 | 2.42M | wake_arc_raw::<W>, | 61 | 2.42M | wake_by_ref_arc_raw::<W>, | 62 | 2.42M | drop_arc_raw::<W>, | 63 | 2.42M | ) | 64 | 2.42M | } |
Unexecuted instantiation: tokio::util::wake::waker_vtable::<tokio::runtime::scheduler::current_thread::Handle> |
65 | | |
66 | 2.38M | unsafe fn clone_arc_raw<T: Wake>(data: *const ()) -> RawWaker { |
67 | 2.38M | Arc::<T>::increment_strong_count(data as *const T); |
68 | 2.38M | RawWaker::new(data, waker_vtable::<T>()) |
69 | 2.38M | } tokio::util::wake::clone_arc_raw::<tokio::runtime::park::Inner> Line | Count | Source | 66 | 2.38M | unsafe fn clone_arc_raw<T: Wake>(data: *const ()) -> RawWaker { | 67 | 2.38M | Arc::<T>::increment_strong_count(data as *const T); | 68 | 2.38M | RawWaker::new(data, waker_vtable::<T>()) | 69 | 2.38M | } |
Unexecuted instantiation: tokio::util::wake::clone_arc_raw::<tokio::runtime::scheduler::current_thread::Handle> |
70 | | |
71 | 2.17M | unsafe fn wake_arc_raw<T: Wake>(data: *const ()) { |
72 | 2.17M | let arc: Arc<T> = Arc::from_raw(data as *const T); |
73 | 2.17M | Wake::wake(arc); |
74 | 2.17M | } tokio::util::wake::wake_arc_raw::<tokio::runtime::park::Inner> Line | Count | Source | 71 | 2.17M | unsafe fn wake_arc_raw<T: Wake>(data: *const ()) { | 72 | 2.17M | let arc: Arc<T> = Arc::from_raw(data as *const T); | 73 | 2.17M | Wake::wake(arc); | 74 | 2.17M | } |
Unexecuted instantiation: tokio::util::wake::wake_arc_raw::<tokio::runtime::scheduler::current_thread::Handle> |
75 | | |
76 | | // used by `waker_ref` |
77 | 20.7k | unsafe fn wake_by_ref_arc_raw<T: Wake>(data: *const ()) { |
78 | 20.7k | // Retain Arc, but don't touch refcount by wrapping in ManuallyDrop |
79 | 20.7k | let arc = ManuallyDrop::new(Arc::<T>::from_raw(data.cast())); |
80 | 20.7k | Wake::wake_by_ref(&arc); |
81 | 20.7k | } tokio::util::wake::wake_by_ref_arc_raw::<tokio::runtime::park::Inner> Line | Count | Source | 77 | 20.7k | unsafe fn wake_by_ref_arc_raw<T: Wake>(data: *const ()) { | 78 | 20.7k | // Retain Arc, but don't touch refcount by wrapping in ManuallyDrop | 79 | 20.7k | let arc = ManuallyDrop::new(Arc::<T>::from_raw(data.cast())); | 80 | 20.7k | Wake::wake_by_ref(&arc); | 81 | 20.7k | } |
Unexecuted instantiation: tokio::util::wake::wake_by_ref_arc_raw::<tokio::runtime::scheduler::current_thread::Handle> |
82 | | |
83 | 240k | unsafe fn drop_arc_raw<T: Wake>(data: *const ()) { |
84 | 240k | drop(Arc::<T>::from_raw(data.cast())); |
85 | 240k | } tokio::util::wake::drop_arc_raw::<tokio::runtime::park::Inner> Line | Count | Source | 83 | 240k | unsafe fn drop_arc_raw<T: Wake>(data: *const ()) { | 84 | 240k | drop(Arc::<T>::from_raw(data.cast())); | 85 | 240k | } |
Unexecuted instantiation: tokio::util::wake::drop_arc_raw::<tokio::runtime::scheduler::current_thread::Handle> |