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 poiner 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 {
50 /// A virtual function pointer table (vtable) that specifies the behavior
51 /// of a [`RawWaker`].
53 /// The pointer passed to all functions inside the vtable is the `data` pointer
54 /// from the enclosing [`RawWaker`] object.
56 /// The functions inside this struct are only intended be called on the `data`
57 /// pointer of a properly constructed [`RawWaker`] object from inside the
58 /// [`RawWaker`] implementation. Calling one of the contained functions using
59 /// any other `data` pointer will cause undefined behavior.
61 /// [`RawWaker`]: struct.RawWaker.html
62 #[stable(feature = "futures_api", since = "1.36.0")]
63 #[derive(PartialEq, Copy, Clone, Debug)]
64 pub struct RawWakerVTable {
65 /// This function will be called when the [`RawWaker`] gets cloned, e.g. when
66 /// the [`Waker`] in which the [`RawWaker`] is stored gets cloned.
68 /// The implementation of this function must retain all resources that are
69 /// required for this additional instance of a [`RawWaker`] and associated
70 /// task. Calling `wake` on the resulting [`RawWaker`] should result in a wakeup
71 /// of the same task that would have been awoken by the original [`RawWaker`].
73 /// [`Waker`]: struct.Waker.html
74 /// [`RawWaker`]: struct.RawWaker.html
75 clone: unsafe fn(*const ()) -> RawWaker,
77 /// This function will be called when `wake` is called on the [`Waker`].
78 /// It must wake up the task associated with this [`RawWaker`].
80 /// The implementation of this function must make sure to release any
81 /// resources that are associated with this instance of a [`RawWaker`] and
84 /// [`Waker`]: struct.Waker.html
85 /// [`RawWaker`]: struct.RawWaker.html
86 wake: unsafe fn(*const ()),
88 /// This function will be called when `wake_by_ref` is called on the [`Waker`].
89 /// It must wake up the task associated with this [`RawWaker`].
91 /// This function is similar to `wake`, but must not consume the provided data
94 /// [`Waker`]: struct.Waker.html
95 /// [`RawWaker`]: struct.RawWaker.html
96 wake_by_ref: unsafe fn(*const ()),
98 /// This function gets called when a [`RawWaker`] gets dropped.
100 /// The implementation of this function must make sure to release any
101 /// resources that are associated with this instance of a [`RawWaker`] and
104 /// [`RawWaker`]: struct.RawWaker.html
105 drop: unsafe fn(*const ()),
108 impl RawWakerVTable {
109 /// Creates a new `RawWakerVTable` from the provided `clone`, `wake`,
110 /// `wake_by_ref`, and `drop` functions.
114 /// This function will be called when the [`RawWaker`] gets cloned, e.g. when
115 /// the [`Waker`] in which the [`RawWaker`] is stored gets cloned.
117 /// The implementation of this function must retain all resources that are
118 /// required for this additional instance of a [`RawWaker`] and associated
119 /// task. Calling `wake` on the resulting [`RawWaker`] should result in a wakeup
120 /// of the same task that would have been awoken by the original [`RawWaker`].
124 /// This function will be called when `wake` is called on the [`Waker`].
125 /// It must wake up the task associated with this [`RawWaker`].
127 /// The implementation of this function must make sure to release any
128 /// resources that are associated with this instance of a [`RawWaker`] and
133 /// This function will be called when `wake_by_ref` is called on the [`Waker`].
134 /// It must wake up the task associated with this [`RawWaker`].
136 /// This function is similar to `wake`, but must not consume the provided data
141 /// This function gets called when a [`RawWaker`] gets dropped.
143 /// The implementation of this function must make sure to release any
144 /// resources that are associated with this instance of a [`RawWaker`] and
147 /// [`Waker`]: struct.Waker.html
148 /// [`RawWaker`]: struct.RawWaker.html
150 #[cfg_attr(stage0, unstable(feature = "futures_api_const_fn_ptr", issue = "50547"))]
151 #[cfg_attr(not(stage0), stable(feature = "futures_api", since = "1.36.0"))]
152 // `rustc_allow_const_fn_ptr` is a hack that should not be used anywhere else
153 // without first consulting with T-Lang.
155 // FIXME: remove whenever we have a stable way to accept fn pointers from const fn
156 // (see https://github.com/rust-rfcs/const-eval/issues/19#issuecomment-472799062)
157 #[cfg_attr(not(stage0), rustc_allow_const_fn_ptr)]
159 clone: unsafe fn(*const ()) -> RawWaker,
160 wake: unsafe fn(*const ()),
161 wake_by_ref: unsafe fn(*const ()),
162 drop: unsafe fn(*const ()),
173 /// The `Context` of an asynchronous task.
175 /// Currently, `Context` only serves to provide access to a `&Waker`
176 /// which can be used to wake the current task.
177 #[stable(feature = "futures_api", since = "1.36.0")]
178 pub struct Context<'a> {
180 // Ensure we future-proof against variance changes by forcing
181 // the lifetime to be invariant (argument-position lifetimes
182 // are contravariant while return-position lifetimes are
184 _marker: PhantomData<fn(&'a ()) -> &'a ()>,
187 impl<'a> Context<'a> {
188 /// Create a new `Context` from a `&Waker`.
189 #[stable(feature = "futures_api", since = "1.36.0")]
191 pub fn from_waker(waker: &'a Waker) -> Self {
194 _marker: PhantomData,
198 /// Returns a reference to the `Waker` for the current task.
199 #[stable(feature = "futures_api", since = "1.36.0")]
201 pub fn waker(&self) -> &'a Waker {
206 #[stable(feature = "futures_api", since = "1.36.0")]
207 impl fmt::Debug for Context<'_> {
208 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
209 f.debug_struct("Context")
210 .field("waker", &self.waker)
215 /// A `Waker` is a handle for waking up a task by notifying its executor that it
216 /// is ready to be run.
218 /// This handle encapsulates a [`RawWaker`] instance, which defines the
219 /// executor-specific wakeup behavior.
221 /// Implements [`Clone`], [`Send`], and [`Sync`].
223 /// [`RawWaker`]: struct.RawWaker.html
225 #[stable(feature = "futures_api", since = "1.36.0")]
230 #[stable(feature = "futures_api", since = "1.36.0")]
231 impl Unpin for Waker {}
232 #[stable(feature = "futures_api", since = "1.36.0")]
233 unsafe impl Send for Waker {}
234 #[stable(feature = "futures_api", since = "1.36.0")]
235 unsafe impl Sync for Waker {}
238 /// Wake up the task associated with this `Waker`.
240 #[stable(feature = "futures_api", since = "1.36.0")]
242 // The actual wakeup call is delegated through a virtual function call
243 // to the implementation which is defined by the executor.
244 let wake = self.waker.vtable.wake;
245 let data = self.waker.data;
247 // Don't call `drop` -- the waker will be consumed by `wake`.
248 crate::mem::forget(self);
250 // SAFETY: This is safe because `Waker::from_raw` is the only way
251 // to initialize `wake` and `data` requiring the user to acknowledge
252 // that the contract of `RawWaker` is upheld.
253 unsafe { (wake)(data) };
256 /// Wake up the task associated with this `Waker` without consuming the `Waker`.
258 /// This is similar to `wake`, but may be slightly less efficient in the case
259 /// where an owned `Waker` is available. This method should be preferred to
260 /// calling `waker.clone().wake()`.
262 #[stable(feature = "futures_api", since = "1.36.0")]
263 pub fn wake_by_ref(&self) {
264 // The actual wakeup call is delegated through a virtual function call
265 // to the implementation which is defined by the executor.
267 // SAFETY: see `wake`
268 unsafe { (self.waker.vtable.wake_by_ref)(self.waker.data) }
271 /// Returns `true` if this `Waker` and another `Waker` have awoken the same task.
273 /// This function works on a best-effort basis, and may return false even
274 /// when the `Waker`s would awaken the same task. However, if this function
275 /// returns `true`, it is guaranteed that the `Waker`s will awaken the same task.
277 /// This function is primarily used for optimization purposes.
279 #[stable(feature = "futures_api", since = "1.36.0")]
280 pub fn will_wake(&self, other: &Waker) -> bool {
281 self.waker == other.waker
284 /// Creates a new `Waker` from [`RawWaker`].
286 /// The behavior of the returned `Waker` is undefined if the contract defined
287 /// in [`RawWaker`]'s and [`RawWakerVTable`]'s documentation is not upheld.
288 /// Therefore this method is unsafe.
290 /// [`RawWaker`]: struct.RawWaker.html
291 /// [`RawWakerVTable`]: struct.RawWakerVTable.html
293 #[stable(feature = "futures_api", since = "1.36.0")]
294 pub unsafe fn from_raw(waker: RawWaker) -> Waker {
301 #[stable(feature = "futures_api", since = "1.36.0")]
302 impl Clone for Waker {
304 fn clone(&self) -> Self {
306 // SAFETY: This is safe because `Waker::from_raw` is the only way
307 // to initialize `clone` and `data` requiring the user to acknowledge
308 // that the contract of [`RawWaker`] is upheld.
309 waker: unsafe { (self.waker.vtable.clone)(self.waker.data) },
314 #[stable(feature = "futures_api", since = "1.36.0")]
315 impl Drop for Waker {
318 // SAFETY: This is safe because `Waker::from_raw` is the only way
319 // to initialize `drop` and `data` requiring the user to acknowledge
320 // that the contract of `RawWaker` is upheld.
321 unsafe { (self.waker.vtable.drop)(self.waker.data) }
325 #[stable(feature = "futures_api", since = "1.36.0")]
326 impl fmt::Debug for Waker {
327 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
328 let vtable_ptr = self.waker.vtable as *const RawWakerVTable;
329 f.debug_struct("Waker")
330 .field("data", &self.waker.data)
331 .field("vtable", &vtable_ptr)