|
| 1 | +#![unstable(feature = "wake_trait", issue = "0")] |
| 2 | +//! Types and Traits for working with asynchronous tasks. |
| 3 | +use core::mem; |
| 4 | +use core::task::{Waker, RawWaker, RawWakerVTable}; |
| 5 | + |
| 6 | +use crate::sync::Arc; |
| 7 | + |
| 8 | +/// The implementation of waking a task on an executor. |
| 9 | +/// |
| 10 | +/// This trait can be used to create a [`Waker`]. An executor can define an |
| 11 | +/// implementation of this trait, and use that to construct a Waker to pass |
| 12 | +/// to the tasks that are executed on that executor. |
| 13 | +/// |
| 14 | +/// This trait is a memory-safe and ergonomic alternative to constructing a |
| 15 | +/// [`RawWaker`]. It supports the common executor design in which the data |
| 16 | +/// used to wake up a task is stored in an [`Arc`]. Some executors (especially |
| 17 | +/// those for embedded systems) cannot use this API, which is way [`RawWaker`] |
| 18 | +/// exists as an alternative for those systems. |
| 19 | +#[unstable(feature = "wake_trait", issue = "0")] |
| 20 | +pub trait Wake { |
| 21 | + /// Wake this task. |
| 22 | + #[unstable(feature = "wake_trait", issue = "0")] |
| 23 | + fn wake(self: Arc<Self>); |
| 24 | + |
| 25 | + /// Wake this task without consuming the waker. |
| 26 | + /// |
| 27 | + /// If an executor supports a cheaper way to wake without consuming the |
| 28 | + /// waker, it should override this method. By default, it clones the |
| 29 | + /// [`Arc`] and calls `wake` on the clone. |
| 30 | + #[unstable(feature = "wake_trait", issue = "0")] |
| 31 | + fn wake_by_ref(self: &Arc<Self>) { |
| 32 | + self.clone().wake(); |
| 33 | + } |
| 34 | +} |
| 35 | + |
| 36 | +#[unstable(feature = "wake_trait", issue = "0")] |
| 37 | +impl<W: Wake + Send + Sync + 'static> From<Arc<W>> for Waker { |
| 38 | + fn from(waker: Arc<W>) -> Waker { |
| 39 | + unsafe { |
| 40 | + Waker::from_raw(raw_waker(waker)) |
| 41 | + } |
| 42 | + } |
| 43 | +} |
| 44 | + |
| 45 | +#[unstable(feature = "wake_trait", issue = "0")] |
| 46 | +impl<W: Wake + Send + Sync + 'static> From<Arc<W>> for RawWaker { |
| 47 | + fn from(waker: Arc<W>) -> RawWaker { |
| 48 | + raw_waker(waker) |
| 49 | + } |
| 50 | +} |
| 51 | + |
| 52 | +// NB: This private function for constructing a RawWaker is used, rather than |
| 53 | +// inlining this into the `From<Arc<W>> for RawWaker` impl, to ensure that |
| 54 | +// the safety of `From<Arc<W>> for Waker` does not depend on the correct |
| 55 | +// trait dispatch - instead both impls call this function directly and |
| 56 | +// explicitly. |
| 57 | +#[inline(always)] |
| 58 | +fn raw_waker<W: Wake + Send + Sync + 'static>(waker: Arc<W>) -> RawWaker { |
| 59 | + |
| 60 | + // Increment the reference count of the arc to clone it. |
| 61 | + unsafe fn clone_waker<W: Wake + Send + Sync + 'static>(waker: *const ()) -> RawWaker { |
| 62 | + let waker: Arc<W> = Arc::from_raw(waker as *const W); |
| 63 | + mem::forget(waker.clone()); |
| 64 | + raw_waker(waker) |
| 65 | + } |
| 66 | + |
| 67 | + // Wake by value, moving the Arc into the Wake::wake function |
| 68 | + unsafe fn wake<W: Wake + Send + Sync + 'static>(waker: *const ()) { |
| 69 | + let waker: Arc<W> = Arc::from_raw(waker as *const W); |
| 70 | + Wake::wake(waker); |
| 71 | + } |
| 72 | + |
| 73 | + // Wake by reference, forgetting the Arc to avoid decrementing the reference count |
| 74 | + unsafe fn wake_by_ref<W: Wake + Send + Sync + 'static>(waker: *const ()) { |
| 75 | + let waker: Arc<W> = Arc::from_raw(waker as *const W); |
| 76 | + Wake::wake_by_ref(&waker); |
| 77 | + mem::forget(waker); |
| 78 | + } |
| 79 | + |
| 80 | + // Decrement the reference count of the Arc on drop |
| 81 | + unsafe fn drop_waker<W: Wake + Send + Sync + 'static>(waker: *const ()) { |
| 82 | + mem::drop(Arc::from_raw(waker as *const W)); |
| 83 | + } |
| 84 | + |
| 85 | + RawWaker::new(Arc::into_raw(waker) as *const (), &RawWakerVTable::new( |
| 86 | + clone_waker::<W>, |
| 87 | + wake::<W>, |
| 88 | + wake_by_ref::<W>, |
| 89 | + drop_waker::<W>, |
| 90 | + )) |
| 91 | +} |
0 commit comments