1 #![stable(feature = "futures_api", since = "1.36.0")]
4 use crate::marker
::{PhantomData, Unpin}
;
7 /// A `RawWaker` allows the implementor of a task executor to create a [`Waker`]
8 /// which provides customized wakeup behavior.
10 /// [vtable]: https://en.wikipedia.org/wiki/Virtual_method_table
12 /// It consists of a data pointer and a [virtual function pointer table (vtable)][vtable]
13 /// that customizes the behavior of the `RawWaker`.
14 #[derive(PartialEq, Debug)]
15 #[stable(feature = "futures_api", since = "1.36.0")]
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 pointer 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.
41 #[stable(feature = "futures_api", since = "1.36.0")]
42 #[rustc_const_stable(feature = "futures_api", since = "1.36.0")]
44 pub const fn new(data
: *const (), vtable
: &'
static RawWakerVTable
) -> RawWaker
{
45 RawWaker { data, vtable }
48 /// Get the `data` pointer used to create this `RawWaker`.
51 #[unstable(feature = "waker_getters", issue = "87021")]
52 pub fn data(&self) -> *const () {
56 /// Get the `vtable` pointer used to create this `RawWaker`.
59 #[unstable(feature = "waker_getters", issue = "87021")]
60 pub fn vtable(&self) -> &'
static RawWakerVTable
{
65 /// A virtual function pointer table (vtable) that specifies the behavior
66 /// of a [`RawWaker`].
68 /// The pointer passed to all functions inside the vtable is the `data` pointer
69 /// from the enclosing [`RawWaker`] object.
71 /// The functions inside this struct are only intended to be called on the `data`
72 /// pointer of a properly constructed [`RawWaker`] object from inside the
73 /// [`RawWaker`] implementation. Calling one of the contained functions using
74 /// any other `data` pointer will cause undefined behavior.
76 /// These functions must all be thread-safe (even though [`RawWaker`] is
77 /// <code>\![Send] + \![Sync]</code>)
78 /// because [`Waker`] is <code>[Send] + [Sync]</code>, and thus wakers may be moved to
79 /// arbitrary threads or invoked by `&` reference. For example, this means that if the
80 /// `clone` and `drop` functions manage a reference count, they must do so atomically.
81 #[stable(feature = "futures_api", since = "1.36.0")]
82 #[derive(PartialEq, Copy, Clone, Debug)]
83 pub struct RawWakerVTable
{
84 /// This function will be called when the [`RawWaker`] gets cloned, e.g. when
85 /// the [`Waker`] in which the [`RawWaker`] is stored gets cloned.
87 /// The implementation of this function must retain all resources that are
88 /// required for this additional instance of a [`RawWaker`] and associated
89 /// task. Calling `wake` on the resulting [`RawWaker`] should result in a wakeup
90 /// of the same task that would have been awoken by the original [`RawWaker`].
91 clone
: unsafe fn(*const ()) -> RawWaker
,
93 /// This function will be called when `wake` is called on the [`Waker`].
94 /// It must wake up the task associated with this [`RawWaker`].
96 /// The implementation of this function must make sure to release any
97 /// resources that are associated with this instance of a [`RawWaker`] and
99 wake
: unsafe fn(*const ()),
101 /// This function will be called when `wake_by_ref` is called on the [`Waker`].
102 /// It must wake up the task associated with this [`RawWaker`].
104 /// This function is similar to `wake`, but must not consume the provided data
106 wake_by_ref
: unsafe fn(*const ()),
108 /// This function gets called when a [`Waker`] gets dropped.
110 /// The implementation of this function must make sure to release any
111 /// resources that are associated with this instance of a [`RawWaker`] and
113 drop
: unsafe fn(*const ()),
116 impl RawWakerVTable
{
117 /// Creates a new `RawWakerVTable` from the provided `clone`, `wake`,
118 /// `wake_by_ref`, and `drop` functions.
120 /// These functions must all be thread-safe (even though [`RawWaker`] is
121 /// <code>\![Send] + \![Sync]</code>)
122 /// because [`Waker`] is <code>[Send] + [Sync]</code>, and thus wakers may be moved to
123 /// arbitrary threads or invoked by `&` reference. For example, this means that if the
124 /// `clone` and `drop` functions manage a reference count, they must do so atomically.
128 /// This function will be called when the [`RawWaker`] gets cloned, e.g. when
129 /// the [`Waker`] in which the [`RawWaker`] is stored gets cloned.
131 /// The implementation of this function must retain all resources that are
132 /// required for this additional instance of a [`RawWaker`] and associated
133 /// task. Calling `wake` on the resulting [`RawWaker`] should result in a wakeup
134 /// of the same task that would have been awoken by the original [`RawWaker`].
138 /// This function will be called when `wake` is called on the [`Waker`].
139 /// It must wake up the task associated with this [`RawWaker`].
141 /// The implementation of this function must make sure to release any
142 /// resources that are associated with this instance of a [`RawWaker`] and
147 /// This function will be called when `wake_by_ref` is called on the [`Waker`].
148 /// It must wake up the task associated with this [`RawWaker`].
150 /// This function is similar to `wake`, but must not consume the provided data
155 /// This function gets called when a [`Waker`] gets dropped.
157 /// The implementation of this function must make sure to release any
158 /// resources that are associated with this instance of a [`RawWaker`] and
161 #[stable(feature = "futures_api", since = "1.36.0")]
162 #[rustc_const_stable(feature = "futures_api", since = "1.36.0")]
164 clone
: unsafe fn(*const ()) -> RawWaker
,
165 wake
: unsafe fn(*const ()),
166 wake_by_ref
: unsafe fn(*const ()),
167 drop
: unsafe fn(*const ()),
169 Self { clone, wake, wake_by_ref, drop }
173 /// The context of an asynchronous task.
175 /// Currently, `Context` only serves to provide access to a [`&Waker`](Waker)
176 /// which can be used to wake the current task.
177 #[stable(feature = "futures_api", since = "1.36.0")]
179 pub struct Context
<'a
> {
181 // Ensure we future-proof against variance changes by forcing
182 // the lifetime to be invariant (argument-position lifetimes
183 // are contravariant while return-position lifetimes are
185 _marker
: PhantomData
<fn(&'
a ()) -> &'
a ()>,
186 // Ensure `Context` is `!Send` and `!Sync` in order to allow
187 // for future `!Send` and / or `!Sync` fields.
188 _marker2
: PhantomData
<*mut ()>,
191 impl<'a
> Context
<'a
> {
192 /// Create a new `Context` from a [`&Waker`](Waker).
193 #[stable(feature = "futures_api", since = "1.36.0")]
194 #[rustc_const_unstable(feature = "const_waker", issue = "102012")]
197 pub const fn from_waker(waker
: &'a Waker
) -> Self {
198 Context { waker, _marker: PhantomData, _marker2: PhantomData }
201 /// Returns a reference to the [`Waker`] for the current task.
202 #[stable(feature = "futures_api", since = "1.36.0")]
203 #[rustc_const_unstable(feature = "const_waker", issue = "102012")]
206 pub const fn waker(&self) -> &'a Waker
{
211 #[stable(feature = "futures_api", since = "1.36.0")]
212 impl fmt
::Debug
for Context
<'_
> {
213 fn fmt(&self, f
: &mut fmt
::Formatter
<'_
>) -> fmt
::Result
{
214 f
.debug_struct("Context").field("waker", &self.waker
).finish()
218 /// A `Waker` is a handle for waking up a task by notifying its executor that it
219 /// is ready to be run.
221 /// This handle encapsulates a [`RawWaker`] instance, which defines the
222 /// executor-specific wakeup behavior.
224 /// The typical life of a `Waker` is that it is constructed by an executor, wrapped in a
225 /// [`Context`], then passed to [`Future::poll()`]. Then, if the future chooses to return
226 /// [`Poll::Pending`], it must also store the waker somehow and call [`Waker::wake()`] when
227 /// the future should be polled again.
229 /// Implements [`Clone`], [`Send`], and [`Sync`]; therefore, a waker may be invoked
230 /// from any thread, including ones not in any way managed by the executor. For example,
231 /// this might be done to wake a future when a blocking function call completes on another
234 /// [`Future::poll()`]: core::future::Future::poll
235 /// [`Poll::Pending`]: core::task::Poll::Pending
236 #[cfg_attr(not(doc), repr(transparent))] // work around https://github.com/rust-lang/rust/issues/66401
237 #[stable(feature = "futures_api", since = "1.36.0")]
242 #[stable(feature = "futures_api", since = "1.36.0")]
243 impl Unpin
for Waker {}
244 #[stable(feature = "futures_api", since = "1.36.0")]
245 unsafe impl Send
for Waker {}
246 #[stable(feature = "futures_api", since = "1.36.0")]
247 unsafe impl Sync
for Waker {}
250 /// Wake up the task associated with this `Waker`.
252 /// As long as the executor keeps running and the task is not finished, it is
253 /// guaranteed that each invocation of [`wake()`](Self::wake) (or
254 /// [`wake_by_ref()`](Self::wake_by_ref)) will be followed by at least one
255 /// [`poll()`] of the task to which this `Waker` belongs. This makes
256 /// it possible to temporarily yield to other tasks while running potentially
257 /// unbounded processing loops.
259 /// Note that the above implies that multiple wake-ups may be coalesced into a
260 /// single [`poll()`] invocation by the runtime.
262 /// Also note that yielding to competing tasks is not guaranteed: it is the
263 /// executor’s choice which task to run and the executor may choose to run the
264 /// current task again.
266 /// [`poll()`]: crate::future::Future::poll
268 #[stable(feature = "futures_api", since = "1.36.0")]
270 // The actual wakeup call is delegated through a virtual function call
271 // to the implementation which is defined by the executor.
272 let wake
= self.waker
.vtable
.wake
;
273 let data
= self.waker
.data
;
275 // Don't call `drop` -- the waker will be consumed by `wake`.
276 crate::mem
::forget(self);
278 // SAFETY: This is safe because `Waker::from_raw` is the only way
279 // to initialize `wake` and `data` requiring the user to acknowledge
280 // that the contract of `RawWaker` is upheld.
281 unsafe { (wake)(data) }
;
284 /// Wake up the task associated with this `Waker` without consuming the `Waker`.
286 /// This is similar to [`wake()`](Self::wake), but may be slightly less efficient in
287 /// the case where an owned `Waker` is available. This method should be preferred to
288 /// calling `waker.clone().wake()`.
290 #[stable(feature = "futures_api", since = "1.36.0")]
291 pub fn wake_by_ref(&self) {
292 // The actual wakeup call is delegated through a virtual function call
293 // to the implementation which is defined by the executor.
295 // SAFETY: see `wake`
296 unsafe { (self.waker.vtable.wake_by_ref)(self.waker.data) }
299 /// Returns `true` if this `Waker` and another `Waker` would awake the same task.
301 /// This function works on a best-effort basis, and may return false even
302 /// when the `Waker`s would awaken the same task. However, if this function
303 /// returns `true`, it is guaranteed that the `Waker`s will awaken the same task.
305 /// This function is primarily used for optimization purposes.
308 #[stable(feature = "futures_api", since = "1.36.0")]
309 pub fn will_wake(&self, other
: &Waker
) -> bool
{
310 self.waker
== other
.waker
313 /// Creates a new `Waker` from [`RawWaker`].
315 /// The behavior of the returned `Waker` is undefined if the contract defined
316 /// in [`RawWaker`]'s and [`RawWakerVTable`]'s documentation is not upheld.
317 /// Therefore this method is unsafe.
320 #[stable(feature = "futures_api", since = "1.36.0")]
321 #[rustc_const_unstable(feature = "const_waker", issue = "102012")]
322 pub const unsafe fn from_raw(waker
: RawWaker
) -> Waker
{
326 /// Creates a new `Waker` that does nothing when `wake` is called.
328 /// This is mostly useful for writing tests that need a [`Context`] to poll
329 /// some futures, but are not expecting those futures to wake the waker or
330 /// do not need to do anything specific if it happens.
335 /// #![feature(noop_waker)]
337 /// use std::future::Future;
340 /// let waker = task::Waker::noop();
341 /// let mut cx = task::Context::from_waker(&waker);
343 /// let mut future = Box::pin(async { 10 });
344 /// assert_eq!(future.as_mut().poll(&mut cx), task::Poll::Ready(10));
348 #[unstable(feature = "noop_waker", issue = "98286")]
349 pub const fn noop() -> Waker
{
350 const VTABLE
: RawWakerVTable
= RawWakerVTable
::new(
351 // Cloning just returns a new no-op raw waker
353 // `wake` does nothing
355 // `wake_by_ref` does nothing
357 // Dropping does nothing as we don't allocate anything
360 const RAW
: RawWaker
= RawWaker
::new(ptr
::null(), &VTABLE
);
365 /// Get a reference to the underlying [`RawWaker`].
368 #[unstable(feature = "waker_getters", issue = "87021")]
369 pub fn as_raw(&self) -> &RawWaker
{
374 #[stable(feature = "futures_api", since = "1.36.0")]
375 impl Clone
for Waker
{
377 fn clone(&self) -> Self {
379 // SAFETY: This is safe because `Waker::from_raw` is the only way
380 // to initialize `clone` and `data` requiring the user to acknowledge
381 // that the contract of [`RawWaker`] is upheld.
382 waker
: unsafe { (self.waker.vtable.clone)(self.waker.data) }
,
387 #[stable(feature = "futures_api", since = "1.36.0")]
388 impl Drop
for Waker
{
391 // SAFETY: This is safe because `Waker::from_raw` is the only way
392 // to initialize `drop` and `data` requiring the user to acknowledge
393 // that the contract of `RawWaker` is upheld.
394 unsafe { (self.waker.vtable.drop)(self.waker.data) }
398 #[stable(feature = "futures_api", since = "1.36.0")]
399 impl fmt
::Debug
for Waker
{
400 fn fmt(&self, f
: &mut fmt
::Formatter
<'_
>) -> fmt
::Result
{
401 let vtable_ptr
= self.waker
.vtable
as *const RawWakerVTable
;
402 f
.debug_struct("Waker")
403 .field("data", &self.waker
.data
)
404 .field("vtable", &vtable_ptr
)