2019-04-05 14:14:19 -07:00
|
|
|
|
#![stable(feature = "futures_api", since = "1.36.0")]
|
2018-05-30 18:23:10 -07:00
|
|
|
|
|
2023-11-30 18:11:02 -03:00
|
|
|
|
use crate::mem::transmute;
|
|
|
|
|
|
|
2019-04-15 11:23:21 +09:00
|
|
|
|
use crate::fmt;
|
2023-11-10 10:11:24 +08:00
|
|
|
|
use crate::marker::PhantomData;
|
2022-05-09 17:01:35 +01:00
|
|
|
|
use crate::ptr;
|
2019-01-29 19:02:42 -08:00
|
|
|
|
|
2019-02-03 12:59:51 -08:00
|
|
|
|
/// A `RawWaker` allows the implementor of a task executor to create a [`Waker`]
|
2019-01-29 19:02:42 -08:00
|
|
|
|
/// which provides customized wakeup behavior.
|
|
|
|
|
|
///
|
2019-02-03 12:30:34 -08:00
|
|
|
|
/// [vtable]: https://en.wikipedia.org/wiki/Virtual_method_table
|
|
|
|
|
|
///
|
2020-09-17 22:43:13 +02:00
|
|
|
|
/// It consists of a data pointer and a [virtual function pointer table (vtable)][vtable]
|
|
|
|
|
|
/// that customizes the behavior of the `RawWaker`.
|
2019-02-03 12:59:51 -08:00
|
|
|
|
#[derive(PartialEq, Debug)]
|
2019-04-05 14:14:19 -07:00
|
|
|
|
#[stable(feature = "futures_api", since = "1.36.0")]
|
2019-01-29 19:02:42 -08:00
|
|
|
|
pub struct RawWaker {
|
|
|
|
|
|
/// A data pointer, which can be used to store arbitrary data as required
|
|
|
|
|
|
/// by the executor. This could be e.g. a type-erased pointer to an `Arc`
|
|
|
|
|
|
/// that is associated with the task.
|
|
|
|
|
|
/// The value of this field gets passed to all functions that are part of
|
2019-02-03 12:30:34 -08:00
|
|
|
|
/// the vtable as the first parameter.
|
2019-02-06 22:56:33 -08:00
|
|
|
|
data: *const (),
|
2019-01-29 19:02:42 -08:00
|
|
|
|
/// Virtual function pointer table that customizes the behavior of this waker.
|
2019-02-06 22:56:33 -08:00
|
|
|
|
vtable: &'static RawWakerVTable,
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
impl RawWaker {
|
|
|
|
|
|
/// Creates a new `RawWaker` from the provided `data` pointer and `vtable`.
|
|
|
|
|
|
///
|
|
|
|
|
|
/// The `data` pointer can be used to store arbitrary data as required
|
|
|
|
|
|
/// by the executor. This could be e.g. a type-erased pointer to an `Arc`
|
|
|
|
|
|
/// that is associated with the task.
|
2019-07-15 10:03:53 -05:00
|
|
|
|
/// The value of this pointer will get passed to all functions that are part
|
2019-02-06 22:56:33 -08:00
|
|
|
|
/// of the `vtable` as the first parameter.
|
|
|
|
|
|
///
|
|
|
|
|
|
/// The `vtable` customizes the behavior of a `Waker` which gets created
|
|
|
|
|
|
/// from a `RawWaker`. For each operation on the `Waker`, the associated
|
|
|
|
|
|
/// function in the `vtable` of the underlying `RawWaker` will be called.
|
2020-07-26 00:00:00 +00:00
|
|
|
|
#[inline]
|
2019-03-11 16:56:00 -07:00
|
|
|
|
#[rustc_promotable]
|
2019-04-05 14:14:19 -07:00
|
|
|
|
#[stable(feature = "futures_api", since = "1.36.0")]
|
2019-12-18 12:00:59 -05:00
|
|
|
|
#[rustc_const_stable(feature = "futures_api", since = "1.36.0")]
|
2021-10-10 02:44:26 -04:00
|
|
|
|
#[must_use]
|
2019-02-06 22:56:33 -08:00
|
|
|
|
pub const fn new(data: *const (), vtable: &'static RawWakerVTable) -> RawWaker {
|
|
|
|
|
|
RawWaker { data, vtable }
|
|
|
|
|
|
}
|
2021-12-12 17:55:41 +08:00
|
|
|
|
|
|
|
|
|
|
/// Get the `data` pointer used to create this `RawWaker`.
|
|
|
|
|
|
#[inline]
|
|
|
|
|
|
#[must_use]
|
2023-12-12 14:38:13 +01:00
|
|
|
|
#[unstable(feature = "waker_getters", issue = "96992")]
|
2021-12-12 17:55:41 +08:00
|
|
|
|
pub fn data(&self) -> *const () {
|
|
|
|
|
|
self.data
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// Get the `vtable` pointer used to create this `RawWaker`.
|
|
|
|
|
|
#[inline]
|
|
|
|
|
|
#[must_use]
|
2023-12-12 14:38:13 +01:00
|
|
|
|
#[unstable(feature = "waker_getters", issue = "96992")]
|
2021-12-12 17:55:41 +08:00
|
|
|
|
pub fn vtable(&self) -> &'static RawWakerVTable {
|
|
|
|
|
|
self.vtable
|
|
|
|
|
|
}
|
2023-11-30 18:11:02 -03:00
|
|
|
|
|
|
|
|
|
|
#[unstable(feature = "noop_waker", issue = "98286")]
|
|
|
|
|
|
const NOOP: RawWaker = {
|
|
|
|
|
|
const VTABLE: RawWakerVTable = RawWakerVTable::new(
|
|
|
|
|
|
// Cloning just returns a new no-op raw waker
|
|
|
|
|
|
|_| RawWaker::NOOP,
|
|
|
|
|
|
// `wake` does nothing
|
|
|
|
|
|
|_| {},
|
|
|
|
|
|
// `wake_by_ref` does nothing
|
|
|
|
|
|
|_| {},
|
|
|
|
|
|
// Dropping does nothing as we don't allocate anything
|
|
|
|
|
|
|_| {},
|
|
|
|
|
|
);
|
|
|
|
|
|
RawWaker::new(ptr::null(), &VTABLE)
|
|
|
|
|
|
};
|
2019-01-29 19:02:42 -08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// A virtual function pointer table (vtable) that specifies the behavior
|
2019-02-03 12:30:34 -08:00
|
|
|
|
/// of a [`RawWaker`].
|
2019-01-29 19:02:42 -08:00
|
|
|
|
///
|
|
|
|
|
|
/// The pointer passed to all functions inside the vtable is the `data` pointer
|
2019-02-03 12:59:51 -08:00
|
|
|
|
/// from the enclosing [`RawWaker`] object.
|
2019-02-05 01:30:00 -08:00
|
|
|
|
///
|
2020-09-17 22:43:13 +02:00
|
|
|
|
/// The functions inside this struct are only intended to be called on the `data`
|
2019-02-05 01:30:00 -08:00
|
|
|
|
/// pointer of a properly constructed [`RawWaker`] object from inside the
|
|
|
|
|
|
/// [`RawWaker`] implementation. Calling one of the contained functions using
|
|
|
|
|
|
/// any other `data` pointer will cause undefined behavior.
|
2022-08-03 18:12:21 -07:00
|
|
|
|
///
|
|
|
|
|
|
/// These functions must all be thread-safe (even though [`RawWaker`] is
|
|
|
|
|
|
/// <code>\![Send] + \![Sync]</code>)
|
|
|
|
|
|
/// because [`Waker`] is <code>[Send] + [Sync]</code>, and thus wakers may be moved to
|
|
|
|
|
|
/// arbitrary threads or invoked by `&` reference. For example, this means that if the
|
|
|
|
|
|
/// `clone` and `drop` functions manage a reference count, they must do so atomically.
|
2019-04-05 14:14:19 -07:00
|
|
|
|
#[stable(feature = "futures_api", since = "1.36.0")]
|
2019-02-03 12:59:51 -08:00
|
|
|
|
#[derive(PartialEq, Copy, Clone, Debug)]
|
2019-01-29 19:02:42 -08:00
|
|
|
|
pub struct RawWakerVTable {
|
2019-02-03 12:59:51 -08:00
|
|
|
|
/// This function will be called when the [`RawWaker`] gets cloned, e.g. when
|
|
|
|
|
|
/// the [`Waker`] in which the [`RawWaker`] is stored gets cloned.
|
2019-01-29 19:02:42 -08:00
|
|
|
|
///
|
|
|
|
|
|
/// The implementation of this function must retain all resources that are
|
2019-02-03 12:59:51 -08:00
|
|
|
|
/// required for this additional instance of a [`RawWaker`] and associated
|
|
|
|
|
|
/// task. Calling `wake` on the resulting [`RawWaker`] should result in a wakeup
|
|
|
|
|
|
/// of the same task that would have been awoken by the original [`RawWaker`].
|
2019-03-11 16:56:00 -07:00
|
|
|
|
clone: unsafe fn(*const ()) -> RawWaker,
|
2019-01-29 19:02:42 -08:00
|
|
|
|
|
2019-02-03 12:59:51 -08:00
|
|
|
|
/// This function will be called when `wake` is called on the [`Waker`].
|
|
|
|
|
|
/// It must wake up the task associated with this [`RawWaker`].
|
2019-02-05 01:14:09 -08:00
|
|
|
|
///
|
2019-04-05 11:46:30 -07:00
|
|
|
|
/// The implementation of this function must make sure to release any
|
|
|
|
|
|
/// resources that are associated with this instance of a [`RawWaker`] and
|
|
|
|
|
|
/// associated task.
|
2019-03-11 16:56:00 -07:00
|
|
|
|
wake: unsafe fn(*const ()),
|
|
|
|
|
|
|
2019-04-05 11:46:30 -07:00
|
|
|
|
/// This function will be called when `wake_by_ref` is called on the [`Waker`].
|
|
|
|
|
|
/// It must wake up the task associated with this [`RawWaker`].
|
|
|
|
|
|
///
|
|
|
|
|
|
/// This function is similar to `wake`, but must not consume the provided data
|
|
|
|
|
|
/// pointer.
|
|
|
|
|
|
wake_by_ref: unsafe fn(*const ()),
|
|
|
|
|
|
|
2022-12-20 14:51:24 -08:00
|
|
|
|
/// This function gets called when a [`Waker`] gets dropped.
|
2019-03-11 16:56:00 -07:00
|
|
|
|
///
|
|
|
|
|
|
/// The implementation of this function must make sure to release any
|
|
|
|
|
|
/// resources that are associated with this instance of a [`RawWaker`] and
|
|
|
|
|
|
/// associated task.
|
|
|
|
|
|
drop: unsafe fn(*const ()),
|
|
|
|
|
|
}
|
2019-01-29 19:02:42 -08:00
|
|
|
|
|
2019-03-11 16:56:00 -07:00
|
|
|
|
impl RawWakerVTable {
|
2019-04-05 11:46:30 -07:00
|
|
|
|
/// Creates a new `RawWakerVTable` from the provided `clone`, `wake`,
|
|
|
|
|
|
/// `wake_by_ref`, and `drop` functions.
|
2019-03-11 16:56:00 -07:00
|
|
|
|
///
|
2022-08-03 18:12:21 -07:00
|
|
|
|
/// These functions must all be thread-safe (even though [`RawWaker`] is
|
|
|
|
|
|
/// <code>\![Send] + \![Sync]</code>)
|
|
|
|
|
|
/// because [`Waker`] is <code>[Send] + [Sync]</code>, and thus wakers may be moved to
|
|
|
|
|
|
/// arbitrary threads or invoked by `&` reference. For example, this means that if the
|
|
|
|
|
|
/// `clone` and `drop` functions manage a reference count, they must do so atomically.
|
|
|
|
|
|
///
|
2019-03-11 16:56:00 -07:00
|
|
|
|
/// # `clone`
|
|
|
|
|
|
///
|
|
|
|
|
|
/// This function will be called when the [`RawWaker`] gets cloned, e.g. when
|
|
|
|
|
|
/// the [`Waker`] in which the [`RawWaker`] is stored gets cloned.
|
|
|
|
|
|
///
|
|
|
|
|
|
/// The implementation of this function must retain all resources that are
|
|
|
|
|
|
/// required for this additional instance of a [`RawWaker`] and associated
|
|
|
|
|
|
/// task. Calling `wake` on the resulting [`RawWaker`] should result in a wakeup
|
|
|
|
|
|
/// of the same task that would have been awoken by the original [`RawWaker`].
|
|
|
|
|
|
///
|
|
|
|
|
|
/// # `wake`
|
|
|
|
|
|
///
|
|
|
|
|
|
/// This function will be called when `wake` is called on the [`Waker`].
|
|
|
|
|
|
/// It must wake up the task associated with this [`RawWaker`].
|
|
|
|
|
|
///
|
2019-04-05 11:46:30 -07:00
|
|
|
|
/// The implementation of this function must make sure to release any
|
|
|
|
|
|
/// resources that are associated with this instance of a [`RawWaker`] and
|
|
|
|
|
|
/// associated task.
|
|
|
|
|
|
///
|
|
|
|
|
|
/// # `wake_by_ref`
|
|
|
|
|
|
///
|
|
|
|
|
|
/// This function will be called when `wake_by_ref` is called on the [`Waker`].
|
|
|
|
|
|
/// It must wake up the task associated with this [`RawWaker`].
|
|
|
|
|
|
///
|
|
|
|
|
|
/// This function is similar to `wake`, but must not consume the provided data
|
2019-03-11 16:56:00 -07:00
|
|
|
|
/// pointer.
|
|
|
|
|
|
///
|
|
|
|
|
|
/// # `drop`
|
|
|
|
|
|
///
|
2022-12-20 14:51:24 -08:00
|
|
|
|
/// This function gets called when a [`Waker`] gets dropped.
|
2019-01-29 19:02:42 -08:00
|
|
|
|
///
|
|
|
|
|
|
/// The implementation of this function must make sure to release any
|
2019-02-03 12:59:51 -08:00
|
|
|
|
/// resources that are associated with this instance of a [`RawWaker`] and
|
2019-01-29 19:02:42 -08:00
|
|
|
|
/// associated task.
|
2019-03-11 16:56:00 -07:00
|
|
|
|
#[rustc_promotable]
|
2019-05-23 12:27:58 +02:00
|
|
|
|
#[stable(feature = "futures_api", since = "1.36.0")]
|
2019-12-18 12:00:59 -05:00
|
|
|
|
#[rustc_const_stable(feature = "futures_api", since = "1.36.0")]
|
2019-03-11 16:56:00 -07:00
|
|
|
|
pub const fn new(
|
|
|
|
|
|
clone: unsafe fn(*const ()) -> RawWaker,
|
|
|
|
|
|
wake: unsafe fn(*const ()),
|
2019-04-05 11:46:30 -07:00
|
|
|
|
wake_by_ref: unsafe fn(*const ()),
|
2019-03-11 16:56:00 -07:00
|
|
|
|
drop: unsafe fn(*const ()),
|
|
|
|
|
|
) -> Self {
|
2019-04-05 11:46:30 -07:00
|
|
|
|
Self { clone, wake, wake_by_ref, drop }
|
2019-03-11 16:56:00 -07:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2022-08-03 18:12:21 -07:00
|
|
|
|
/// The context of an asynchronous task.
|
2019-03-11 16:56:00 -07:00
|
|
|
|
///
|
2022-08-03 18:12:21 -07:00
|
|
|
|
/// Currently, `Context` only serves to provide access to a [`&Waker`](Waker)
|
2019-03-11 16:56:00 -07:00
|
|
|
|
/// which can be used to wake the current task.
|
2019-04-05 14:14:19 -07:00
|
|
|
|
#[stable(feature = "futures_api", since = "1.36.0")]
|
2023-01-25 09:48:32 -05:00
|
|
|
|
#[lang = "Context"]
|
2019-03-11 16:56:00 -07:00
|
|
|
|
pub struct Context<'a> {
|
2023-11-30 18:11:02 -03:00
|
|
|
|
waker: Option<&'a Waker>,
|
2023-11-30 19:05:12 -03:00
|
|
|
|
local_waker: &'a LocalWaker,
|
2019-03-11 16:56:00 -07:00
|
|
|
|
// Ensure we future-proof against variance changes by forcing
|
|
|
|
|
|
// the lifetime to be invariant (argument-position lifetimes
|
|
|
|
|
|
// are contravariant while return-position lifetimes are
|
|
|
|
|
|
// covariant).
|
|
|
|
|
|
_marker: PhantomData<fn(&'a ()) -> &'a ()>,
|
2022-04-12 13:54:36 -03:00
|
|
|
|
// Ensure `Context` is `!Send` and `!Sync` in order to allow
|
|
|
|
|
|
// for future `!Send` and / or `!Sync` fields.
|
|
|
|
|
|
_marker2: PhantomData<*mut ()>,
|
2019-03-11 16:56:00 -07:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
impl<'a> Context<'a> {
|
2022-08-03 22:07:50 -07:00
|
|
|
|
/// Create a new `Context` from a [`&Waker`](Waker).
|
2019-04-05 14:14:19 -07:00
|
|
|
|
#[stable(feature = "futures_api", since = "1.36.0")]
|
2022-09-19 15:07:12 +02:00
|
|
|
|
#[rustc_const_unstable(feature = "const_waker", issue = "102012")]
|
2021-10-10 18:22:40 -04:00
|
|
|
|
#[must_use]
|
2019-03-11 16:56:00 -07:00
|
|
|
|
#[inline]
|
2022-09-14 14:15:44 +02:00
|
|
|
|
pub const fn from_waker(waker: &'a Waker) -> Self {
|
2023-12-06 12:11:28 -03:00
|
|
|
|
ContextBuilder::from_waker(waker).build()
|
2019-03-11 16:56:00 -07:00
|
|
|
|
}
|
|
|
|
|
|
|
2022-08-03 18:12:21 -07:00
|
|
|
|
/// Returns a reference to the [`Waker`] for the current task.
|
2023-11-30 18:11:02 -03:00
|
|
|
|
///
|
|
|
|
|
|
/// Note that if the waker does not need to be sent across threads, it
|
|
|
|
|
|
/// is preferable to call `local_waker`, which is more portable and
|
|
|
|
|
|
/// potentially more efficient.
|
|
|
|
|
|
///
|
|
|
|
|
|
/// # Panics
|
|
|
|
|
|
/// This function will panic if no `Waker` was set on the context. This happens if
|
|
|
|
|
|
/// the executor does not support working with thread safe wakers. An alternative
|
2023-12-06 14:09:16 -03:00
|
|
|
|
/// may be to call [`.local_waker()`](Context::local_waker) instead. For a fallible
|
|
|
|
|
|
/// version of this function see [`.try_waker()`](Context::try_waker).
|
|
|
|
|
|
#[inline]
|
|
|
|
|
|
#[must_use]
|
2019-04-05 14:14:19 -07:00
|
|
|
|
#[stable(feature = "futures_api", since = "1.36.0")]
|
2022-09-19 15:07:12 +02:00
|
|
|
|
#[rustc_const_unstable(feature = "const_waker", issue = "102012")]
|
2022-09-14 14:15:44 +02:00
|
|
|
|
pub const fn waker(&self) -> &'a Waker {
|
2023-11-30 18:11:02 -03:00
|
|
|
|
&self
|
|
|
|
|
|
.waker
|
|
|
|
|
|
.expect("no waker was set on this context, consider calling `local_waker` instead.")
|
|
|
|
|
|
}
|
|
|
|
|
|
/// Returns a reference to the [`LocalWaker`] for the current task.
|
2023-12-06 14:09:16 -03:00
|
|
|
|
#[inline]
|
2023-12-14 23:15:01 -03:00
|
|
|
|
#[unstable(feature = "local_waker", issue = "118959")]
|
2023-12-06 14:09:16 -03:00
|
|
|
|
#[rustc_const_unstable(feature = "const_waker", issue = "102012")]
|
|
|
|
|
|
pub const fn local_waker(&self) -> &'a LocalWaker {
|
2023-11-30 19:05:12 -03:00
|
|
|
|
&self.local_waker
|
2019-03-11 16:56:00 -07:00
|
|
|
|
}
|
2023-12-06 14:09:16 -03:00
|
|
|
|
/// Returns a `Some(&Waker)` if a waker was defined on the `Context`,
|
|
|
|
|
|
/// otherwise it returns `None`.
|
|
|
|
|
|
#[inline]
|
|
|
|
|
|
#[rustc_const_unstable(feature = "const_waker", issue = "102012")]
|
2023-12-14 23:15:01 -03:00
|
|
|
|
#[unstable(feature = "local_waker", issue = "118959")]
|
2023-12-06 14:09:16 -03:00
|
|
|
|
pub const fn try_waker(&self) -> Option<&'a Waker> {
|
|
|
|
|
|
self.waker
|
|
|
|
|
|
}
|
2019-03-11 16:56:00 -07:00
|
|
|
|
}
|
|
|
|
|
|
|
2019-04-05 14:14:19 -07:00
|
|
|
|
#[stable(feature = "futures_api", since = "1.36.0")]
|
2019-03-11 16:56:00 -07:00
|
|
|
|
impl fmt::Debug for Context<'_> {
|
2019-04-19 01:37:12 +02:00
|
|
|
|
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
2019-03-11 16:56:00 -07:00
|
|
|
|
f.debug_struct("Context").field("waker", &self.waker).finish()
|
|
|
|
|
|
}
|
2019-01-29 19:02:42 -08:00
|
|
|
|
}
|
2018-06-08 16:45:27 -04:00
|
|
|
|
|
2023-11-30 18:11:02 -03:00
|
|
|
|
/// A Builder used to construct a `Context` instance
|
|
|
|
|
|
/// with support for `LocalWaker`.
|
|
|
|
|
|
///
|
|
|
|
|
|
/// # Examples
|
|
|
|
|
|
/// ```
|
|
|
|
|
|
/// #![feature(local_waker)]
|
|
|
|
|
|
/// #![feature(noop_waker)]
|
2023-12-06 14:09:16 -03:00
|
|
|
|
/// use std::task::{ContextBuilder, LocalWaker, Waker, Poll};
|
|
|
|
|
|
/// use std::future::Future;
|
|
|
|
|
|
///
|
2023-11-30 18:11:02 -03:00
|
|
|
|
/// let local_waker = LocalWaker::noop();
|
|
|
|
|
|
/// let waker = Waker::noop();
|
|
|
|
|
|
///
|
2023-12-06 14:09:16 -03:00
|
|
|
|
/// let mut cx = ContextBuilder::from_local_waker(&local_waker)
|
2023-11-30 18:11:02 -03:00
|
|
|
|
/// .waker(&waker)
|
|
|
|
|
|
/// .build();
|
2023-12-06 12:11:28 -03:00
|
|
|
|
///
|
2023-12-06 14:09:16 -03:00
|
|
|
|
/// let mut future = std::pin::pin!(async { 20 });
|
|
|
|
|
|
/// let poll = future.as_mut().poll(&mut cx);
|
|
|
|
|
|
/// assert_eq!(poll, Poll::Ready(20));
|
2023-12-06 12:11:28 -03:00
|
|
|
|
///
|
2023-11-30 18:11:02 -03:00
|
|
|
|
/// ```
|
2023-12-14 23:15:01 -03:00
|
|
|
|
#[unstable(feature = "local_waker", issue = "118959")]
|
2023-12-06 12:11:28 -03:00
|
|
|
|
#[derive(Debug)]
|
2023-11-30 18:11:02 -03:00
|
|
|
|
pub struct ContextBuilder<'a> {
|
|
|
|
|
|
waker: Option<&'a Waker>,
|
2023-12-06 12:11:28 -03:00
|
|
|
|
local_waker: &'a LocalWaker,
|
2023-11-30 18:11:02 -03:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
impl<'a> ContextBuilder<'a> {
|
2023-12-06 12:11:28 -03:00
|
|
|
|
/// Create a ContextBuilder from a Waker.
|
|
|
|
|
|
#[inline]
|
|
|
|
|
|
#[rustc_const_unstable(feature = "const_waker", issue = "102012")]
|
2023-12-14 23:15:01 -03:00
|
|
|
|
#[unstable(feature = "local_waker", issue = "118959")]
|
2023-12-06 12:11:28 -03:00
|
|
|
|
pub const fn from_waker(waker: &'a Waker) -> Self {
|
|
|
|
|
|
// SAFETY: LocalWaker is just Waker without thread safety
|
|
|
|
|
|
let local_waker = unsafe { transmute(waker) };
|
|
|
|
|
|
Self { waker: Some(waker), local_waker }
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// Create a ContextBuilder from a LocalWaker.
|
2023-11-30 18:11:02 -03:00
|
|
|
|
#[inline]
|
|
|
|
|
|
#[rustc_const_unstable(feature = "const_waker", issue = "102012")]
|
2023-12-14 23:15:01 -03:00
|
|
|
|
#[unstable(feature = "local_waker", issue = "118959")]
|
2023-12-06 12:11:28 -03:00
|
|
|
|
pub const fn from_local_waker(local_waker: &'a LocalWaker) -> Self {
|
|
|
|
|
|
Self { local_waker, waker: None }
|
2023-11-30 18:11:02 -03:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// This field is used to set the value of the waker on `Context`.
|
2023-12-06 12:11:28 -03:00
|
|
|
|
|
2023-11-30 18:11:02 -03:00
|
|
|
|
#[inline]
|
|
|
|
|
|
#[rustc_const_unstable(feature = "const_waker", issue = "102012")]
|
2023-12-14 23:15:01 -03:00
|
|
|
|
#[unstable(feature = "local_waker", issue = "118959")]
|
2023-11-30 18:11:02 -03:00
|
|
|
|
pub const fn waker(self, waker: &'a Waker) -> Self {
|
|
|
|
|
|
Self { waker: Some(waker), ..self }
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// This method is used to set the value for the local waker on `Context`.
|
|
|
|
|
|
#[inline]
|
2023-12-14 23:15:01 -03:00
|
|
|
|
#[unstable(feature = "local_waker", issue = "118959")]
|
2023-11-30 18:11:02 -03:00
|
|
|
|
#[rustc_const_unstable(feature = "const_waker", issue = "102012")]
|
|
|
|
|
|
pub const fn local_waker(self, local_waker: &'a LocalWaker) -> Self {
|
2023-12-06 12:11:28 -03:00
|
|
|
|
Self { local_waker, ..self }
|
2023-11-30 18:11:02 -03:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// Builds the `Context`.
|
|
|
|
|
|
#[inline]
|
2023-12-14 23:15:01 -03:00
|
|
|
|
#[unstable(feature = "local_waker", issue = "118959")]
|
2023-11-30 18:11:02 -03:00
|
|
|
|
#[rustc_const_unstable(feature = "const_waker", issue = "102012")]
|
|
|
|
|
|
pub const fn build(self) -> Context<'a> {
|
|
|
|
|
|
let ContextBuilder { waker, local_waker } = self;
|
|
|
|
|
|
Context { waker, local_waker, _marker: PhantomData, _marker2: PhantomData }
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2023-12-06 14:09:16 -03:00
|
|
|
|
/// Construct a `ContextBuilder`` from a `Context`. This is useful for
|
|
|
|
|
|
/// overriding values from a context.
|
|
|
|
|
|
///
|
|
|
|
|
|
/// # Examples
|
|
|
|
|
|
/// An example of a future that allows to set a Waker on Context if none was defined.
|
|
|
|
|
|
/// This can be used to await futures that require a `Waker` even if the runtime does not
|
|
|
|
|
|
/// support `Waker`.
|
|
|
|
|
|
/// ```rust
|
|
|
|
|
|
/// #![feature(noop_waker, local_waker)]
|
|
|
|
|
|
/// use std::task::{Waker, ContextBuilder};
|
|
|
|
|
|
/// use std::future::{poll_fn, Future};
|
|
|
|
|
|
/// use std::pin::pin;
|
2023-12-06 15:19:24 -03:00
|
|
|
|
///
|
2023-12-06 14:09:16 -03:00
|
|
|
|
/// async fn with_waker<F>(f: F, waker: &Waker) -> F::Output
|
|
|
|
|
|
/// where
|
|
|
|
|
|
/// F: Future
|
|
|
|
|
|
/// {
|
|
|
|
|
|
/// let mut f = pin!(f);
|
|
|
|
|
|
/// poll_fn(move |cx| {
|
|
|
|
|
|
/// let has_waker = cx.try_waker().is_some();
|
|
|
|
|
|
/// if has_waker {
|
|
|
|
|
|
/// return f.as_mut().poll(cx);
|
|
|
|
|
|
/// }
|
|
|
|
|
|
///
|
|
|
|
|
|
/// let mut cx = ContextBuilder::from(cx)
|
|
|
|
|
|
/// .waker(&waker)
|
|
|
|
|
|
/// .build();
|
|
|
|
|
|
/// f.as_mut().poll(&mut cx)
|
|
|
|
|
|
/// }).await
|
|
|
|
|
|
/// }
|
2023-12-06 15:19:24 -03:00
|
|
|
|
///
|
2023-12-06 14:09:16 -03:00
|
|
|
|
/// # async fn __() {
|
|
|
|
|
|
/// with_waker(async { /* ... */ }, &Waker::noop()).await;
|
|
|
|
|
|
/// # }
|
|
|
|
|
|
/// ```
|
2023-12-14 23:15:01 -03:00
|
|
|
|
#[unstable(feature = "local_waker", issue = "118959")]
|
2023-12-06 14:09:16 -03:00
|
|
|
|
impl<'a> From<&mut Context<'a>> for ContextBuilder<'a> {
|
|
|
|
|
|
#[inline]
|
|
|
|
|
|
fn from(value: &mut Context<'a>) -> Self {
|
|
|
|
|
|
let Context { waker, local_waker, .. } = *value;
|
|
|
|
|
|
ContextBuilder { waker, local_waker }
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2018-05-30 18:23:10 -07:00
|
|
|
|
/// A `Waker` is a handle for waking up a task by notifying its executor that it
|
|
|
|
|
|
/// is ready to be run.
|
|
|
|
|
|
///
|
2019-02-03 12:59:51 -08:00
|
|
|
|
/// This handle encapsulates a [`RawWaker`] instance, which defines the
|
2019-01-29 19:02:42 -08:00
|
|
|
|
/// executor-specific wakeup behavior.
|
|
|
|
|
|
///
|
2022-08-03 22:07:50 -07:00
|
|
|
|
/// The typical life of a `Waker` is that it is constructed by an executor, wrapped in a
|
2022-08-03 18:12:21 -07:00
|
|
|
|
/// [`Context`], then passed to [`Future::poll()`]. Then, if the future chooses to return
|
|
|
|
|
|
/// [`Poll::Pending`], it must also store the waker somehow and call [`Waker::wake()`] when
|
|
|
|
|
|
/// the future should be polled again.
|
|
|
|
|
|
///
|
|
|
|
|
|
/// Implements [`Clone`], [`Send`], and [`Sync`]; therefore, a waker may be invoked
|
|
|
|
|
|
/// from any thread, including ones not in any way managed by the executor. For example,
|
|
|
|
|
|
/// this might be done to wake a future when a blocking function call completes on another
|
2023-11-30 18:11:02 -03:00
|
|
|
|
/// thread. If the waker does not need to be moved across threads, it is better to use
|
|
|
|
|
|
/// [`LocalWaker`], which the executor may use to skip unnecessary memory synchronization.
|
2022-08-03 18:12:21 -07:00
|
|
|
|
///
|
2023-07-27 16:29:13 +01:00
|
|
|
|
/// Note that it is preferable to use `waker.clone_from(&new_waker)` instead
|
|
|
|
|
|
/// of `*waker = new_waker.clone()`, as the former will avoid cloning the waker
|
|
|
|
|
|
/// unnecessarily if the two wakers [wake the same task](Self::will_wake).
|
|
|
|
|
|
///
|
2022-08-03 18:12:21 -07:00
|
|
|
|
/// [`Future::poll()`]: core::future::Future::poll
|
|
|
|
|
|
/// [`Poll::Pending`]: core::task::Poll::Pending
|
2023-02-04 19:12:01 -08:00
|
|
|
|
#[cfg_attr(not(doc), repr(transparent))] // work around https://github.com/rust-lang/rust/issues/66401
|
2019-04-05 14:14:19 -07:00
|
|
|
|
#[stable(feature = "futures_api", since = "1.36.0")]
|
2018-05-30 18:23:10 -07:00
|
|
|
|
pub struct Waker {
|
2019-01-29 19:02:42 -08:00
|
|
|
|
waker: RawWaker,
|
2018-05-30 18:23:10 -07:00
|
|
|
|
}
|
|
|
|
|
|
|
2019-04-05 14:14:19 -07:00
|
|
|
|
#[stable(feature = "futures_api", since = "1.36.0")]
|
2018-06-29 19:33:16 -07:00
|
|
|
|
impl Unpin for Waker {}
|
2019-04-05 14:14:19 -07:00
|
|
|
|
#[stable(feature = "futures_api", since = "1.36.0")]
|
2018-05-30 18:23:10 -07:00
|
|
|
|
unsafe impl Send for Waker {}
|
2019-04-05 14:14:19 -07:00
|
|
|
|
#[stable(feature = "futures_api", since = "1.36.0")]
|
2018-05-30 18:23:10 -07:00
|
|
|
|
unsafe impl Sync for Waker {}
|
|
|
|
|
|
|
|
|
|
|
|
impl Waker {
|
|
|
|
|
|
/// Wake up the task associated with this `Waker`.
|
2022-02-13 17:08:30 +01:00
|
|
|
|
///
|
2022-08-03 18:12:21 -07:00
|
|
|
|
/// As long as the executor keeps running and the task is not finished, it is
|
|
|
|
|
|
/// guaranteed that each invocation of [`wake()`](Self::wake) (or
|
|
|
|
|
|
/// [`wake_by_ref()`](Self::wake_by_ref)) will be followed by at least one
|
2022-08-03 22:07:50 -07:00
|
|
|
|
/// [`poll()`] of the task to which this `Waker` belongs. This makes
|
2022-02-20 12:23:26 +01:00
|
|
|
|
/// it possible to temporarily yield to other tasks while running potentially
|
|
|
|
|
|
/// unbounded processing loops.
|
|
|
|
|
|
///
|
|
|
|
|
|
/// Note that the above implies that multiple wake-ups may be coalesced into a
|
2022-08-03 18:12:21 -07:00
|
|
|
|
/// single [`poll()`] invocation by the runtime.
|
2022-05-04 10:58:23 +02:00
|
|
|
|
///
|
|
|
|
|
|
/// Also note that yielding to competing tasks is not guaranteed: it is the
|
|
|
|
|
|
/// executor’s choice which task to run and the executor may choose to run the
|
|
|
|
|
|
/// current task again.
|
2022-08-03 18:12:21 -07:00
|
|
|
|
///
|
|
|
|
|
|
/// [`poll()`]: crate::future::Future::poll
|
2019-03-11 16:56:00 -07:00
|
|
|
|
#[inline]
|
2019-04-05 14:14:19 -07:00
|
|
|
|
#[stable(feature = "futures_api", since = "1.36.0")]
|
2019-04-05 11:46:30 -07:00
|
|
|
|
pub fn wake(self) {
|
2019-01-29 19:02:42 -08:00
|
|
|
|
// The actual wakeup call is delegated through a virtual function call
|
|
|
|
|
|
// to the implementation which is defined by the executor.
|
2019-04-05 11:46:30 -07:00
|
|
|
|
let wake = self.waker.vtable.wake;
|
|
|
|
|
|
let data = self.waker.data;
|
|
|
|
|
|
|
|
|
|
|
|
// Don't call `drop` -- the waker will be consumed by `wake`.
|
|
|
|
|
|
crate::mem::forget(self);
|
2019-02-03 12:59:51 -08:00
|
|
|
|
|
2019-04-05 11:49:46 -07:00
|
|
|
|
// SAFETY: This is safe because `Waker::from_raw` is the only way
|
2019-02-03 12:59:51 -08:00
|
|
|
|
// to initialize `wake` and `data` requiring the user to acknowledge
|
|
|
|
|
|
// that the contract of `RawWaker` is upheld.
|
2019-04-05 11:46:30 -07:00
|
|
|
|
unsafe { (wake)(data) };
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// Wake up the task associated with this `Waker` without consuming the `Waker`.
|
|
|
|
|
|
///
|
2022-08-03 18:12:21 -07:00
|
|
|
|
/// This is similar to [`wake()`](Self::wake), but may be slightly less efficient in
|
|
|
|
|
|
/// the case where an owned `Waker` is available. This method should be preferred to
|
2019-04-05 11:46:30 -07:00
|
|
|
|
/// calling `waker.clone().wake()`.
|
|
|
|
|
|
#[inline]
|
2019-04-05 14:14:19 -07:00
|
|
|
|
#[stable(feature = "futures_api", since = "1.36.0")]
|
2019-04-05 11:46:30 -07:00
|
|
|
|
pub fn wake_by_ref(&self) {
|
|
|
|
|
|
// The actual wakeup call is delegated through a virtual function call
|
|
|
|
|
|
// to the implementation which is defined by the executor.
|
|
|
|
|
|
|
|
|
|
|
|
// SAFETY: see `wake`
|
|
|
|
|
|
unsafe { (self.waker.vtable.wake_by_ref)(self.waker.data) }
|
2018-05-30 18:23:10 -07:00
|
|
|
|
}
|
|
|
|
|
|
|
2022-08-03 22:07:50 -07:00
|
|
|
|
/// Returns `true` if this `Waker` and another `Waker` would awake the same task.
|
2018-05-30 18:23:10 -07:00
|
|
|
|
///
|
|
|
|
|
|
/// This function works on a best-effort basis, and may return false even
|
|
|
|
|
|
/// when the `Waker`s would awaken the same task. However, if this function
|
2019-01-29 19:02:42 -08:00
|
|
|
|
/// returns `true`, it is guaranteed that the `Waker`s will awaken the same task.
|
2018-05-30 18:23:10 -07:00
|
|
|
|
///
|
2023-07-27 16:29:13 +01:00
|
|
|
|
/// This function is primarily used for optimization purposes — for example,
|
|
|
|
|
|
/// this type's [`clone_from`](Self::clone_from) implementation uses it to
|
|
|
|
|
|
/// avoid cloning the waker when they would wake the same task anyway.
|
2019-03-11 16:56:00 -07:00
|
|
|
|
#[inline]
|
2021-10-14 18:54:55 -04:00
|
|
|
|
#[must_use]
|
2019-04-05 14:14:19 -07:00
|
|
|
|
#[stable(feature = "futures_api", since = "1.36.0")]
|
2018-05-30 18:23:10 -07:00
|
|
|
|
pub fn will_wake(&self, other: &Waker) -> bool {
|
2019-01-29 19:02:42 -08:00
|
|
|
|
self.waker == other.waker
|
2018-05-30 18:23:10 -07:00
|
|
|
|
}
|
2018-10-01 15:16:06 -07:00
|
|
|
|
|
2019-02-03 12:59:51 -08:00
|
|
|
|
/// Creates a new `Waker` from [`RawWaker`].
|
2018-10-01 15:16:06 -07:00
|
|
|
|
///
|
2019-02-03 12:59:51 -08:00
|
|
|
|
/// The behavior of the returned `Waker` is undefined if the contract defined
|
2019-02-06 22:56:33 -08:00
|
|
|
|
/// in [`RawWaker`]'s and [`RawWakerVTable`]'s documentation is not upheld.
|
|
|
|
|
|
/// Therefore this method is unsafe.
|
2019-03-11 16:56:00 -07:00
|
|
|
|
#[inline]
|
2021-10-10 18:22:40 -04:00
|
|
|
|
#[must_use]
|
2019-04-05 14:14:19 -07:00
|
|
|
|
#[stable(feature = "futures_api", since = "1.36.0")]
|
2022-09-19 15:07:12 +02:00
|
|
|
|
#[rustc_const_unstable(feature = "const_waker", issue = "102012")]
|
2022-09-14 14:15:44 +02:00
|
|
|
|
pub const unsafe fn from_raw(waker: RawWaker) -> Waker {
|
2019-01-29 19:02:42 -08:00
|
|
|
|
Waker { waker }
|
2018-10-01 15:16:06 -07:00
|
|
|
|
}
|
2021-12-12 17:55:41 +08:00
|
|
|
|
|
2024-01-14 16:18:44 -08:00
|
|
|
|
/// Returns a reference to a `Waker` that does nothing when used.
|
2022-05-09 17:01:35 +01:00
|
|
|
|
///
|
|
|
|
|
|
/// This is mostly useful for writing tests that need a [`Context`] to poll
|
|
|
|
|
|
/// some futures, but are not expecting those futures to wake the waker or
|
|
|
|
|
|
/// do not need to do anything specific if it happens.
|
|
|
|
|
|
///
|
2024-01-14 16:18:44 -08:00
|
|
|
|
/// If an owned `Waker` is needed, `clone()` this one.
|
|
|
|
|
|
///
|
2022-05-09 17:01:35 +01:00
|
|
|
|
/// # Examples
|
|
|
|
|
|
///
|
|
|
|
|
|
/// ```
|
|
|
|
|
|
/// #![feature(noop_waker)]
|
|
|
|
|
|
///
|
|
|
|
|
|
/// use std::future::Future;
|
|
|
|
|
|
/// use std::task;
|
|
|
|
|
|
///
|
2024-01-14 16:18:44 -08:00
|
|
|
|
/// let mut cx = task::Context::from_waker(task::Waker::noop());
|
2022-05-09 17:01:35 +01:00
|
|
|
|
///
|
|
|
|
|
|
/// let mut future = Box::pin(async { 10 });
|
|
|
|
|
|
/// assert_eq!(future.as_mut().poll(&mut cx), task::Poll::Ready(10));
|
|
|
|
|
|
/// ```
|
|
|
|
|
|
#[inline]
|
|
|
|
|
|
#[must_use]
|
2022-06-20 11:58:10 +01:00
|
|
|
|
#[unstable(feature = "noop_waker", issue = "98286")]
|
2024-01-14 16:18:44 -08:00
|
|
|
|
pub const fn noop() -> &'static Waker {
|
2023-11-30 18:11:02 -03:00
|
|
|
|
const WAKER: &Waker = &Waker { waker: RawWaker::NOOP };
|
|
|
|
|
|
WAKER
|
2022-05-09 17:01:35 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
2021-12-12 17:55:41 +08:00
|
|
|
|
/// Get a reference to the underlying [`RawWaker`].
|
|
|
|
|
|
#[inline]
|
|
|
|
|
|
#[must_use]
|
2023-12-12 14:38:13 +01:00
|
|
|
|
#[unstable(feature = "waker_getters", issue = "96992")]
|
2021-12-12 17:55:41 +08:00
|
|
|
|
pub fn as_raw(&self) -> &RawWaker {
|
|
|
|
|
|
&self.waker
|
|
|
|
|
|
}
|
2018-05-30 18:23:10 -07:00
|
|
|
|
}
|
|
|
|
|
|
|
2019-04-05 14:14:19 -07:00
|
|
|
|
#[stable(feature = "futures_api", since = "1.36.0")]
|
2018-05-30 18:23:10 -07:00
|
|
|
|
impl Clone for Waker {
|
2019-03-11 16:56:00 -07:00
|
|
|
|
#[inline]
|
2018-05-30 18:23:10 -07:00
|
|
|
|
fn clone(&self) -> Self {
|
2019-01-29 19:02:42 -08:00
|
|
|
|
Waker {
|
2019-04-05 11:49:46 -07:00
|
|
|
|
// SAFETY: This is safe because `Waker::from_raw` is the only way
|
2019-02-03 12:59:51 -08:00
|
|
|
|
// to initialize `clone` and `data` requiring the user to acknowledge
|
|
|
|
|
|
// that the contract of [`RawWaker`] is upheld.
|
2019-01-29 19:02:42 -08:00
|
|
|
|
waker: unsafe { (self.waker.vtable.clone)(self.waker.data) },
|
2018-05-30 18:23:10 -07:00
|
|
|
|
}
|
|
|
|
|
|
}
|
2023-07-27 16:29:13 +01:00
|
|
|
|
|
|
|
|
|
|
#[inline]
|
|
|
|
|
|
fn clone_from(&mut self, source: &Self) {
|
|
|
|
|
|
if !self.will_wake(source) {
|
|
|
|
|
|
*self = source.clone();
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
2018-05-30 18:23:10 -07:00
|
|
|
|
}
|
|
|
|
|
|
|
2019-04-05 14:14:19 -07:00
|
|
|
|
#[stable(feature = "futures_api", since = "1.36.0")]
|
2018-05-30 18:23:10 -07:00
|
|
|
|
impl Drop for Waker {
|
2019-03-11 16:56:00 -07:00
|
|
|
|
#[inline]
|
2018-05-30 18:23:10 -07:00
|
|
|
|
fn drop(&mut self) {
|
2019-04-05 11:49:46 -07:00
|
|
|
|
// SAFETY: This is safe because `Waker::from_raw` is the only way
|
2019-02-03 12:59:51 -08:00
|
|
|
|
// to initialize `drop` and `data` requiring the user to acknowledge
|
|
|
|
|
|
// that the contract of `RawWaker` is upheld.
|
|
|
|
|
|
unsafe { (self.waker.vtable.drop)(self.waker.data) }
|
2018-05-30 18:23:10 -07:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2019-04-05 14:14:19 -07:00
|
|
|
|
#[stable(feature = "futures_api", since = "1.36.0")]
|
2019-01-29 19:02:42 -08:00
|
|
|
|
impl fmt::Debug for Waker {
|
2019-04-19 01:37:12 +02:00
|
|
|
|
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
2019-02-05 01:14:09 -08:00
|
|
|
|
let vtable_ptr = self.waker.vtable as *const RawWakerVTable;
|
2019-01-29 19:02:42 -08:00
|
|
|
|
f.debug_struct("Waker")
|
2019-02-05 01:14:09 -08:00
|
|
|
|
.field("data", &self.waker.data)
|
|
|
|
|
|
.field("vtable", &vtable_ptr)
|
2018-05-30 18:23:10 -07:00
|
|
|
|
.finish()
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
2023-11-30 18:11:02 -03:00
|
|
|
|
|
|
|
|
|
|
/// A `LocalWaker` is analogous to a [`Waker`], but it does not implement [`Send`] or [`Sync`].
|
|
|
|
|
|
/// This handle encapsulates a [`RawWaker`] instance, which defines the
|
|
|
|
|
|
/// executor-specific wakeup behavior.
|
|
|
|
|
|
///
|
|
|
|
|
|
/// Local wakers can be requested from a `Context` with the [`local_waker`] method.
|
|
|
|
|
|
///
|
|
|
|
|
|
/// The typical life of a `LocalWaker` is that it is constructed by an executor, wrapped in a
|
2023-12-06 12:11:28 -03:00
|
|
|
|
/// [`Context`] using [`ContextBuilder`], then passed to [`Future::poll()`]. Then, if the future chooses to return
|
|
|
|
|
|
/// [`Poll::Pending`], it must also store the waker somehow and call [`LocalWaker::wake()`] when
|
2023-11-30 18:11:02 -03:00
|
|
|
|
/// the future should be polled again.
|
|
|
|
|
|
///
|
|
|
|
|
|
/// Implements [`Clone`], but neither [`Send`] nor [`Sync`]; therefore, a local waker may
|
|
|
|
|
|
/// not be moved to other threads. In general, when deciding to use wakers or local wakers,
|
|
|
|
|
|
/// local wakers are preferable unless the waker needs to be sent across threads. This is because
|
|
|
|
|
|
/// wakers can incur in additional cost related to memory synchronization, and not all executors
|
|
|
|
|
|
/// may support wakers.
|
|
|
|
|
|
///
|
|
|
|
|
|
/// Note that it is preferable to use `local_waker.clone_from(&new_waker)` instead
|
|
|
|
|
|
/// of `*local_waker = new_waker.clone()`, as the former will avoid cloning the waker
|
|
|
|
|
|
/// unnecessarily if the two wakers [wake the same task](Self::will_wake).
|
|
|
|
|
|
///
|
|
|
|
|
|
/// # Examples
|
2023-12-06 12:11:28 -03:00
|
|
|
|
/// Usage of a local waker to implement a future
|
2023-11-30 18:11:02 -03:00
|
|
|
|
/// ```
|
|
|
|
|
|
/// #![feature(local_waker)]
|
|
|
|
|
|
/// use std::future::{Future, poll_fn};
|
|
|
|
|
|
/// use std::task::Poll;
|
|
|
|
|
|
///
|
|
|
|
|
|
/// // a future that returns pending once.
|
|
|
|
|
|
/// fn yield_now() -> impl Future<Output=()> + Unpin {
|
|
|
|
|
|
/// let mut yielded = false;
|
|
|
|
|
|
/// poll_fn(move |cx| {
|
|
|
|
|
|
/// if !yielded {
|
|
|
|
|
|
/// yielded = true;
|
|
|
|
|
|
/// cx.local_waker().wake_by_ref();
|
|
|
|
|
|
/// return Poll::Pending;
|
|
|
|
|
|
/// }
|
|
|
|
|
|
/// return Poll::Ready(())
|
|
|
|
|
|
/// })
|
|
|
|
|
|
/// }
|
2023-12-06 12:11:28 -03:00
|
|
|
|
///
|
2023-11-30 19:05:12 -03:00
|
|
|
|
/// # async fn __() {
|
2023-11-30 18:11:02 -03:00
|
|
|
|
/// yield_now().await;
|
2023-11-30 19:05:12 -03:00
|
|
|
|
/// # }
|
2023-11-30 18:11:02 -03:00
|
|
|
|
/// ```
|
2023-12-06 14:09:16 -03:00
|
|
|
|
///
|
2023-11-30 18:11:02 -03:00
|
|
|
|
/// [`Future::poll()`]: core::future::Future::poll
|
|
|
|
|
|
/// [`Poll::Pending`]: core::task::Poll::Pending
|
|
|
|
|
|
/// [`local_waker`]: core::task::Context::local_waker
|
2023-12-14 23:15:01 -03:00
|
|
|
|
#[unstable(feature = "local_waker", issue = "118959")]
|
2023-11-30 18:11:02 -03:00
|
|
|
|
#[repr(transparent)]
|
|
|
|
|
|
pub struct LocalWaker {
|
|
|
|
|
|
waker: RawWaker,
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2023-12-14 23:15:01 -03:00
|
|
|
|
#[unstable(feature = "local_waker", issue = "118959")]
|
2023-11-30 18:11:02 -03:00
|
|
|
|
impl Unpin for LocalWaker {}
|
|
|
|
|
|
|
|
|
|
|
|
impl LocalWaker {
|
|
|
|
|
|
/// Creates a new `LocalWaker` from [`RawWaker`].
|
|
|
|
|
|
///
|
2023-12-14 23:15:01 -03:00
|
|
|
|
/// The behavior of the returned `LocalWaker` is undefined if the contract defined
|
2023-11-30 18:11:02 -03:00
|
|
|
|
/// in [`RawWaker`]'s and [`RawWakerVTable`]'s documentation is not upheld.
|
|
|
|
|
|
/// Therefore this method is unsafe.
|
|
|
|
|
|
#[inline]
|
|
|
|
|
|
#[must_use]
|
|
|
|
|
|
#[stable(feature = "futures_api", since = "1.36.0")]
|
|
|
|
|
|
#[rustc_const_unstable(feature = "const_waker", issue = "102012")]
|
|
|
|
|
|
pub const unsafe fn from_raw(waker: RawWaker) -> LocalWaker {
|
|
|
|
|
|
Self { waker }
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// Wake up the task associated with this `LocalWaker`.
|
|
|
|
|
|
///
|
|
|
|
|
|
/// As long as the executor keeps running and the task is not finished, it is
|
|
|
|
|
|
/// guaranteed that each invocation of [`wake()`](Self::wake) (or
|
|
|
|
|
|
/// [`wake_by_ref()`](Self::wake_by_ref)) will be followed by at least one
|
2023-12-14 23:15:01 -03:00
|
|
|
|
/// [`poll()`] of the task to which this `LocalWaker` belongs. This makes
|
2023-11-30 18:11:02 -03:00
|
|
|
|
/// it possible to temporarily yield to other tasks while running potentially
|
|
|
|
|
|
/// unbounded processing loops.
|
|
|
|
|
|
///
|
|
|
|
|
|
/// Note that the above implies that multiple wake-ups may be coalesced into a
|
|
|
|
|
|
/// single [`poll()`] invocation by the runtime.
|
|
|
|
|
|
///
|
|
|
|
|
|
/// Also note that yielding to competing tasks is not guaranteed: it is the
|
|
|
|
|
|
/// executor’s choice which task to run and the executor may choose to run the
|
|
|
|
|
|
/// current task again.
|
|
|
|
|
|
///
|
|
|
|
|
|
/// [`poll()`]: crate::future::Future::poll
|
|
|
|
|
|
#[inline]
|
|
|
|
|
|
#[stable(feature = "futures_api", since = "1.36.0")]
|
|
|
|
|
|
pub fn wake(self) {
|
|
|
|
|
|
// The actual wakeup call is delegated through a virtual function call
|
|
|
|
|
|
// to the implementation which is defined by the executor.
|
|
|
|
|
|
let wake = self.waker.vtable.wake;
|
|
|
|
|
|
let data = self.waker.data;
|
|
|
|
|
|
|
|
|
|
|
|
// Don't call `drop` -- the waker will be consumed by `wake`.
|
|
|
|
|
|
crate::mem::forget(self);
|
|
|
|
|
|
|
|
|
|
|
|
// SAFETY: This is safe because `Waker::from_raw` is the only way
|
|
|
|
|
|
// to initialize `wake` and `data` requiring the user to acknowledge
|
|
|
|
|
|
// that the contract of `RawWaker` is upheld.
|
|
|
|
|
|
unsafe { (wake)(data) };
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// Creates a new `LocalWaker` that does nothing when `wake` is called.
|
|
|
|
|
|
///
|
|
|
|
|
|
/// This is mostly useful for writing tests that need a [`Context`] to poll
|
|
|
|
|
|
/// some futures, but are not expecting those futures to wake the waker or
|
|
|
|
|
|
/// do not need to do anything specific if it happens.
|
|
|
|
|
|
///
|
|
|
|
|
|
/// # Examples
|
|
|
|
|
|
///
|
|
|
|
|
|
/// ```
|
|
|
|
|
|
/// #![feature(local_waker)]
|
|
|
|
|
|
/// #![feature(noop_waker)]
|
|
|
|
|
|
///
|
|
|
|
|
|
/// use std::future::Future;
|
|
|
|
|
|
/// use std::task::{ContextBuilder, LocalWaker};
|
|
|
|
|
|
///
|
|
|
|
|
|
/// let mut cx = task::ContextBuilder::new()
|
|
|
|
|
|
/// .local_waker(LocalWaker::noop())
|
|
|
|
|
|
/// .build();
|
|
|
|
|
|
///
|
|
|
|
|
|
/// let mut future = Box::pin(async { 10 });
|
|
|
|
|
|
/// assert_eq!(future.as_mut().poll(&mut cx), task::Poll::Ready(10));
|
|
|
|
|
|
/// ```
|
|
|
|
|
|
#[inline]
|
|
|
|
|
|
#[must_use]
|
|
|
|
|
|
#[unstable(feature = "noop_waker", issue = "98286")]
|
|
|
|
|
|
pub const fn noop() -> &'static LocalWaker {
|
|
|
|
|
|
const WAKER: &LocalWaker = &LocalWaker { waker: RawWaker::NOOP };
|
|
|
|
|
|
WAKER
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// Get a reference to the underlying [`RawWaker`].
|
|
|
|
|
|
#[inline]
|
|
|
|
|
|
#[must_use]
|
|
|
|
|
|
#[unstable(feature = "waker_getters", issue = "87021")]
|
|
|
|
|
|
pub fn as_raw(&self) -> &RawWaker {
|
|
|
|
|
|
&self.waker
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// Returns `true` if this `LocalWaker` and another `LocalWaker` would awake the same task.
|
|
|
|
|
|
///
|
|
|
|
|
|
/// This function works on a best-effort basis, and may return false even
|
|
|
|
|
|
/// when the `Waker`s would awaken the same task. However, if this function
|
|
|
|
|
|
/// returns `true`, it is guaranteed that the `Waker`s will awaken the same task.
|
|
|
|
|
|
///
|
|
|
|
|
|
/// This function is primarily used for optimization purposes — for example,
|
|
|
|
|
|
/// this type's [`clone_from`](Self::clone_from) implementation uses it to
|
|
|
|
|
|
/// avoid cloning the waker when they would wake the same task anyway.
|
|
|
|
|
|
#[inline]
|
|
|
|
|
|
#[must_use]
|
|
|
|
|
|
#[stable(feature = "futures_api", since = "1.36.0")]
|
|
|
|
|
|
pub fn will_wake(&self, other: &LocalWaker) -> bool {
|
|
|
|
|
|
self.waker == other.waker
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// Wake up the task associated with this `LocalWaker` without consuming the `LocalWaker`.
|
|
|
|
|
|
///
|
|
|
|
|
|
/// This is similar to [`wake()`](Self::wake), but may be slightly less efficient in
|
|
|
|
|
|
/// the case where an owned `Waker` is available. This method should be preferred to
|
|
|
|
|
|
/// calling `waker.clone().wake()`.
|
|
|
|
|
|
#[inline]
|
|
|
|
|
|
#[stable(feature = "futures_api", since = "1.36.0")]
|
|
|
|
|
|
pub fn wake_by_ref(&self) {
|
|
|
|
|
|
// The actual wakeup call is delegated through a virtual function call
|
|
|
|
|
|
// to the implementation which is defined by the executor.
|
|
|
|
|
|
|
|
|
|
|
|
// SAFETY: see `wake`
|
|
|
|
|
|
unsafe { (self.waker.vtable.wake_by_ref)(self.waker.data) }
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
2023-12-14 23:15:01 -03:00
|
|
|
|
#[unstable(feature = "local_waker", issue = "118959")]
|
2023-11-30 18:11:02 -03:00
|
|
|
|
impl Clone for LocalWaker {
|
|
|
|
|
|
#[inline]
|
|
|
|
|
|
fn clone(&self) -> Self {
|
|
|
|
|
|
LocalWaker {
|
|
|
|
|
|
// SAFETY: This is safe because `Waker::from_raw` is the only way
|
|
|
|
|
|
// to initialize `clone` and `data` requiring the user to acknowledge
|
|
|
|
|
|
// that the contract of [`RawWaker`] is upheld.
|
|
|
|
|
|
waker: unsafe { (self.waker.vtable.clone)(self.waker.data) },
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
#[inline]
|
|
|
|
|
|
fn clone_from(&mut self, source: &Self) {
|
|
|
|
|
|
if !self.will_wake(source) {
|
|
|
|
|
|
*self = source.clone();
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
#[stable(feature = "futures_api", since = "1.36.0")]
|
|
|
|
|
|
impl fmt::Debug for LocalWaker {
|
|
|
|
|
|
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
|
|
|
|
|
let vtable_ptr = self.waker.vtable as *const RawWakerVTable;
|
|
|
|
|
|
f.debug_struct("LocalWaker")
|
|
|
|
|
|
.field("data", &self.waker.data)
|
|
|
|
|
|
.field("vtable", &vtable_ptr)
|
|
|
|
|
|
.finish()
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|