1 #![stable(feature = "futures_api", since = "1.36.0")]
4 use crate::marker::{PhantomData, Unpin};
6 /// A `RawWaker` allows the implementor of a task executor to create a [`Waker`]
7 /// which provides customized wakeup behavior.
9 /// [vtable]: https://en.wikipedia.org/wiki/Virtual_method_table
11 /// It consists of a data pointer and a [virtual function pointer table (vtable)][vtable] that
12 /// customizes the behavior of the `RawWaker`.
14 /// [`Waker`]: struct.Waker.html
15 #[derive(PartialEq, Debug)]
16 #[stable(feature = "futures_api", since = "1.36.0")]
18 /// A data pointer, which can be used to store arbitrary data as required
19 /// by the executor. This could be e.g. a type-erased pointer to an `Arc`
20 /// that is associated with the task.
21 /// The value of this field gets passed to all functions that are part of
22 /// the vtable as the first parameter.
24 /// Virtual function pointer table that customizes the behavior of this waker.
25 vtable: &'static RawWakerVTable,
29 /// Creates a new `RawWaker` from the provided `data` pointer and `vtable`.
31 /// The `data` pointer can be used to store arbitrary data as required
32 /// by the executor. This could be e.g. a type-erased pointer to an `Arc`
33 /// that is associated with the task.
34 /// The value of this pointer will get passed to all functions that are part
35 /// of the `vtable` as the first parameter.
37 /// The `vtable` customizes the behavior of a `Waker` which gets created
38 /// from a `RawWaker`. For each operation on the `Waker`, the associated
39 /// function in the `vtable` of the underlying `RawWaker` will be called.
41 #[stable(feature = "futures_api", since = "1.36.0")]
42 pub const fn new(data: *const (), vtable: &'static RawWakerVTable) -> RawWaker {
43 RawWaker { data, vtable }
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.
58 /// [`RawWaker`]: struct.RawWaker.html
59 #[stable(feature = "futures_api", since = "1.36.0")]
60 #[derive(PartialEq, Copy, Clone, Debug)]
61 pub struct RawWakerVTable {
62 /// This function will be called when the [`RawWaker`] gets cloned, e.g. when
63 /// the [`Waker`] in which the [`RawWaker`] is stored gets cloned.
65 /// The implementation of this function must retain all resources that are
66 /// required for this additional instance of a [`RawWaker`] and associated
67 /// task. Calling `wake` on the resulting [`RawWaker`] should result in a wakeup
68 /// of the same task that would have been awoken by the original [`RawWaker`].
70 /// [`Waker`]: struct.Waker.html
71 /// [`RawWaker`]: struct.RawWaker.html
72 clone: unsafe fn(*const ()) -> RawWaker,
74 /// This function will be called when `wake` is called on the [`Waker`].
75 /// It must wake up the task associated with this [`RawWaker`].
77 /// The implementation of this function must make sure to release any
78 /// resources that are associated with this instance of a [`RawWaker`] and
81 /// [`Waker`]: struct.Waker.html
82 /// [`RawWaker`]: struct.RawWaker.html
83 wake: unsafe fn(*const ()),
85 /// This function will be called when `wake_by_ref` is called on the [`Waker`].
86 /// It must wake up the task associated with this [`RawWaker`].
88 /// This function is similar to `wake`, but must not consume the provided data
91 /// [`Waker`]: struct.Waker.html
92 /// [`RawWaker`]: struct.RawWaker.html
93 wake_by_ref: unsafe fn(*const ()),
95 /// This function gets called when a [`RawWaker`] gets dropped.
97 /// The implementation of this function must make sure to release any
98 /// resources that are associated with this instance of a [`RawWaker`] and
101 /// [`RawWaker`]: struct.RawWaker.html
102 drop: unsafe fn(*const ()),
105 impl RawWakerVTable {
106 /// Creates a new `RawWakerVTable` from the provided `clone`, `wake`,
107 /// `wake_by_ref`, and `drop` functions.
111 /// This function will be called when the [`RawWaker`] gets cloned, e.g. when
112 /// the [`Waker`] in which the [`RawWaker`] is stored gets cloned.
114 /// The implementation of this function must retain all resources that are
115 /// required for this additional instance of a [`RawWaker`] and associated
116 /// task. Calling `wake` on the resulting [`RawWaker`] should result in a wakeup
117 /// of the same task that would have been awoken by the original [`RawWaker`].
121 /// This function will be called when `wake` is called on the [`Waker`].
122 /// It must wake up the task associated with this [`RawWaker`].
124 /// The implementation of this function must make sure to release any
125 /// resources that are associated with this instance of a [`RawWaker`] and
130 /// This function will be called when `wake_by_ref` is called on the [`Waker`].
131 /// It must wake up the task associated with this [`RawWaker`].
133 /// This function is similar to `wake`, but must not consume the provided data
138 /// This function gets called when a [`RawWaker`] gets dropped.
140 /// The implementation of this function must make sure to release any
141 /// resources that are associated with this instance of a [`RawWaker`] and
144 /// [`Waker`]: struct.Waker.html
145 /// [`RawWaker`]: struct.RawWaker.html
147 #[stable(feature = "futures_api", since = "1.36.0")]
148 // `rustc_allow_const_fn_ptr` is a hack that should not be used anywhere else
149 // without first consulting with T-Lang.
151 // FIXME: remove whenever we have a stable way to accept fn pointers from const fn
152 // (see https://github.com/rust-rfcs/const-eval/issues/19#issuecomment-472799062)
153 #[rustc_allow_const_fn_ptr]
155 clone: unsafe fn(*const ()) -> RawWaker,
156 wake: unsafe fn(*const ()),
157 wake_by_ref: unsafe fn(*const ()),
158 drop: unsafe fn(*const ()),
160 Self { clone, wake, wake_by_ref, drop }
164 /// The `Context` of an asynchronous task.
166 /// Currently, `Context` only serves to provide access to a `&Waker`
167 /// which can be used to wake the current task.
168 #[stable(feature = "futures_api", since = "1.36.0")]
169 pub struct Context<'a> {
171 // Ensure we future-proof against variance changes by forcing
172 // the lifetime to be invariant (argument-position lifetimes
173 // are contravariant while return-position lifetimes are
175 _marker: PhantomData<fn(&'a ()) -> &'a ()>,
178 impl<'a> Context<'a> {
179 /// Create a new `Context` from a `&Waker`.
180 #[stable(feature = "futures_api", since = "1.36.0")]
182 pub fn from_waker(waker: &'a Waker) -> Self {
183 Context { waker, _marker: PhantomData }
186 /// Returns a reference to the `Waker` for the current task.
187 #[stable(feature = "futures_api", since = "1.36.0")]
189 pub fn waker(&self) -> &'a Waker {
194 #[stable(feature = "futures_api", since = "1.36.0")]
195 impl fmt::Debug for Context<'_> {
196 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
197 f.debug_struct("Context").field("waker", &self.waker).finish()
201 /// A `Waker` is a handle for waking up a task by notifying its executor that it
202 /// is ready to be run.
204 /// This handle encapsulates a [`RawWaker`] instance, which defines the
205 /// executor-specific wakeup behavior.
207 /// Implements [`Clone`], [`Send`], and [`Sync`].
209 /// [`RawWaker`]: struct.RawWaker.html
211 #[stable(feature = "futures_api", since = "1.36.0")]
216 #[stable(feature = "futures_api", since = "1.36.0")]
217 impl Unpin for Waker {}
218 #[stable(feature = "futures_api", since = "1.36.0")]
219 unsafe impl Send for Waker {}
220 #[stable(feature = "futures_api", since = "1.36.0")]
221 unsafe impl Sync for Waker {}
224 /// Wake up the task associated with this `Waker`.
226 #[stable(feature = "futures_api", since = "1.36.0")]
228 // The actual wakeup call is delegated through a virtual function call
229 // to the implementation which is defined by the executor.
230 let wake = self.waker.vtable.wake;
231 let data = self.waker.data;
233 // Don't call `drop` -- the waker will be consumed by `wake`.
234 crate::mem::forget(self);
236 // SAFETY: This is safe because `Waker::from_raw` is the only way
237 // to initialize `wake` and `data` requiring the user to acknowledge
238 // that the contract of `RawWaker` is upheld.
239 unsafe { (wake)(data) };
242 /// Wake up the task associated with this `Waker` without consuming the `Waker`.
244 /// This is similar to `wake`, but may be slightly less efficient in the case
245 /// where an owned `Waker` is available. This method should be preferred to
246 /// calling `waker.clone().wake()`.
248 #[stable(feature = "futures_api", since = "1.36.0")]
249 pub fn wake_by_ref(&self) {
250 // The actual wakeup call is delegated through a virtual function call
251 // to the implementation which is defined by the executor.
253 // SAFETY: see `wake`
254 unsafe { (self.waker.vtable.wake_by_ref)(self.waker.data) }
257 /// Returns `true` if this `Waker` and another `Waker` have awoken the same task.
259 /// This function works on a best-effort basis, and may return false even
260 /// when the `Waker`s would awaken the same task. However, if this function
261 /// returns `true`, it is guaranteed that the `Waker`s will awaken the same task.
263 /// This function is primarily used for optimization purposes.
265 #[stable(feature = "futures_api", since = "1.36.0")]
266 pub fn will_wake(&self, other: &Waker) -> bool {
267 self.waker == other.waker
270 /// Creates a new `Waker` from [`RawWaker`].
272 /// The behavior of the returned `Waker` is undefined if the contract defined
273 /// in [`RawWaker`]'s and [`RawWakerVTable`]'s documentation is not upheld.
274 /// Therefore this method is unsafe.
276 /// [`RawWaker`]: struct.RawWaker.html
277 /// [`RawWakerVTable`]: struct.RawWakerVTable.html
279 #[stable(feature = "futures_api", since = "1.36.0")]
280 pub unsafe fn from_raw(waker: RawWaker) -> Waker {
285 #[stable(feature = "futures_api", since = "1.36.0")]
286 impl Clone for Waker {
288 fn clone(&self) -> Self {
290 // SAFETY: This is safe because `Waker::from_raw` is the only way
291 // to initialize `clone` and `data` requiring the user to acknowledge
292 // that the contract of [`RawWaker`] is upheld.
293 waker: unsafe { (self.waker.vtable.clone)(self.waker.data) },
298 #[stable(feature = "futures_api", since = "1.36.0")]
299 impl Drop for Waker {
302 // SAFETY: This is safe because `Waker::from_raw` is the only way
303 // to initialize `drop` and `data` requiring the user to acknowledge
304 // that the contract of `RawWaker` is upheld.
305 unsafe { (self.waker.vtable.drop)(self.waker.data) }
309 #[stable(feature = "futures_api", since = "1.36.0")]
310 impl fmt::Debug for Waker {
311 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
312 let vtable_ptr = self.waker.vtable as *const RawWakerVTable;
313 f.debug_struct("Waker")
314 .field("data", &self.waker.data)
315 .field("vtable", &vtable_ptr)