1 #![unstable(feature = "futures_api",
2 reason = "futures in libcore are unstable",
8 /// A `RawWaker` allows the implementor of a task executor to create a [`Waker`]
9 /// which provides customized wakeup behavior.
11 /// [vtable]: https://en.wikipedia.org/wiki/Virtual_method_table
13 /// It consists of a data pointer and a [virtual function pointer table (vtable)][vtable] that
14 /// customizes the behavior of the `RawWaker`.
15 #[derive(PartialEq, Debug)]
17 /// A data pointer, which can be used to store arbitrary data as required
18 /// by the executor. This could be e.g. a type-erased pointer to an `Arc`
19 /// that is associated with the task.
20 /// The value of this field gets passed to all functions that are part of
21 /// the vtable as the first parameter.
23 /// Virtual function pointer table that customizes the behavior of this waker.
24 vtable: &'static RawWakerVTable,
28 /// Creates a new `RawWaker` from the provided `data` pointer and `vtable`.
30 /// The `data` pointer can be used to store arbitrary data as required
31 /// by the executor. This could be e.g. a type-erased pointer to an `Arc`
32 /// that is associated with the task.
33 /// The value of this poiner will get passed to all functions that are part
34 /// of the `vtable` as the first parameter.
36 /// The `vtable` customizes the behavior of a `Waker` which gets created
37 /// from a `RawWaker`. For each operation on the `Waker`, the associated
38 /// function in the `vtable` of the underlying `RawWaker` will be called.
39 pub const fn new(data: *const (), vtable: &'static RawWakerVTable) -> RawWaker {
47 /// A virtual function pointer table (vtable) that specifies the behavior
48 /// of a [`RawWaker`].
50 /// The pointer passed to all functions inside the vtable is the `data` pointer
51 /// from the enclosing [`RawWaker`] object.
53 /// The functions inside this struct are only intended be called on the `data`
54 /// pointer of a properly constructed [`RawWaker`] object from inside the
55 /// [`RawWaker`] implementation. Calling one of the contained functions using
56 /// any other `data` pointer will cause undefined behavior.
57 #[derive(PartialEq, Copy, Clone, Debug)]
58 pub struct RawWakerVTable {
59 /// This function will be called when the [`RawWaker`] gets cloned, e.g. when
60 /// the [`Waker`] in which the [`RawWaker`] is stored gets cloned.
62 /// The implementation of this function must retain all resources that are
63 /// required for this additional instance of a [`RawWaker`] and associated
64 /// task. Calling `wake` on the resulting [`RawWaker`] should result in a wakeup
65 /// of the same task that would have been awoken by the original [`RawWaker`].
66 pub clone: unsafe fn(*const ()) -> RawWaker,
68 /// This function will be called when `wake` is called on the [`Waker`].
69 /// It must wake up the task associated with this [`RawWaker`].
71 /// The implemention of this function must not consume the provided data
73 pub wake: unsafe fn(*const ()),
75 /// This function gets called when a [`RawWaker`] gets dropped.
77 /// The implementation of this function must make sure to release any
78 /// resources that are associated with this instance of a [`RawWaker`] and
80 pub drop: unsafe fn(*const ()),
83 /// A `Waker` is a handle for waking up a task by notifying its executor that it
84 /// is ready to be run.
86 /// This handle encapsulates a [`RawWaker`] instance, which defines the
87 /// executor-specific wakeup behavior.
89 /// Implements [`Clone`], [`Send`], and [`Sync`].
95 impl Unpin for Waker {}
96 unsafe impl Send for Waker {}
97 unsafe impl Sync for Waker {}
100 /// Wake up the task associated with this `Waker`.
102 // The actual wakeup call is delegated through a virtual function call
103 // to the implementation which is defined by the executor.
105 // SAFETY: This is safe because `Waker::new_unchecked` is the only way
106 // to initialize `wake` and `data` requiring the user to acknowledge
107 // that the contract of `RawWaker` is upheld.
108 unsafe { (self.waker.vtable.wake)(self.waker.data) }
111 /// Returns whether or not this `Waker` and other `Waker` have awaken the same task.
113 /// This function works on a best-effort basis, and may return false even
114 /// when the `Waker`s would awaken the same task. However, if this function
115 /// returns `true`, it is guaranteed that the `Waker`s will awaken the same task.
117 /// This function is primarily used for optimization purposes.
118 pub fn will_wake(&self, other: &Waker) -> bool {
119 self.waker == other.waker
122 /// Creates a new `Waker` from [`RawWaker`].
124 /// The behavior of the returned `Waker` is undefined if the contract defined
125 /// in [`RawWaker`]'s and [`RawWakerVTable`]'s documentation is not upheld.
126 /// Therefore this method is unsafe.
127 pub unsafe fn new_unchecked(waker: RawWaker) -> Waker {
134 impl Clone for Waker {
135 fn clone(&self) -> Self {
137 // SAFETY: This is safe because `Waker::new_unchecked` is the only way
138 // to initialize `clone` and `data` requiring the user to acknowledge
139 // that the contract of [`RawWaker`] is upheld.
140 waker: unsafe { (self.waker.vtable.clone)(self.waker.data) },
145 impl Drop for Waker {
147 // SAFETY: This is safe because `Waker::new_unchecked` is the only way
148 // to initialize `drop` and `data` requiring the user to acknowledge
149 // that the contract of `RawWaker` is upheld.
150 unsafe { (self.waker.vtable.drop)(self.waker.data) }
154 impl fmt::Debug for Waker {
155 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
156 let vtable_ptr = self.waker.vtable as *const RawWakerVTable;
157 f.debug_struct("Waker")
158 .field("data", &self.waker.data)
159 .field("vtable", &vtable_ptr)