@@ -71,6 +71,12 @@ impl RawWaker {
7171/// pointer of a properly constructed [`RawWaker`] object from inside the
7272/// [`RawWaker`] implementation. Calling one of the contained functions using
7373/// any other `data` pointer will cause undefined behavior.
74+ ///
75+ /// These functions must all be thread-safe (even though [`RawWaker`] is
76+ /// <code>\![Send] + \![Sync]</code>)
77+ /// because [`Waker`] is <code>[Send] + [Sync]</code>, and thus wakers may be moved to
78+ /// arbitrary threads or invoked by `&` reference. For example, this means that if the
79+ /// `clone` and `drop` functions manage a reference count, they must do so atomically.
7480#[ stable( feature = "futures_api" , since = "1.36.0" ) ]
7581#[ derive( PartialEq , Copy , Clone , Debug ) ]
7682pub struct RawWakerVTable {
@@ -110,6 +116,12 @@ impl RawWakerVTable {
110116 /// Creates a new `RawWakerVTable` from the provided `clone`, `wake`,
111117 /// `wake_by_ref`, and `drop` functions.
112118 ///
119+ /// These functions must all be thread-safe (even though [`RawWaker`] is
120+ /// <code>\![Send] + \![Sync]</code>)
121+ /// because [`Waker`] is <code>[Send] + [Sync]</code>, and thus wakers may be moved to
122+ /// arbitrary threads or invoked by `&` reference. For example, this means that if the
123+ /// `clone` and `drop` functions manage a reference count, they must do so atomically.
124+ ///
113125 /// # `clone`
114126 ///
115127 /// This function will be called when the [`RawWaker`] gets cloned, e.g. when
@@ -157,9 +169,9 @@ impl RawWakerVTable {
157169 }
158170}
159171
160- /// The `Context` of an asynchronous task.
172+ /// The context of an asynchronous task.
161173///
162- /// Currently, `Context` only serves to provide access to a `&Waker`
174+ /// Currently, `Context` only serves to provide access to a [ `&Waker`](Waker)
163175/// which can be used to wake the current task.
164176#[ stable( feature = "futures_api" , since = "1.36.0" ) ]
165177pub struct Context < ' a > {
@@ -172,15 +184,15 @@ pub struct Context<'a> {
172184}
173185
174186impl < ' a > Context < ' a > {
175- /// Create a new `Context` from a `&Waker`.
187+ /// Create a new `Context` from a [ `&Waker`](Waker) .
176188 #[ stable( feature = "futures_api" , since = "1.36.0" ) ]
177189 #[ must_use]
178190 #[ inline]
179191 pub fn from_waker ( waker : & ' a Waker ) -> Self {
180192 Context { waker, _marker : PhantomData }
181193 }
182194
183- /// Returns a reference to the `Waker` for the current task.
195+ /// Returns a reference to the [ `Waker`] for the current task.
184196 #[ stable( feature = "futures_api" , since = "1.36.0" ) ]
185197 #[ must_use]
186198 #[ inline]
@@ -202,7 +214,18 @@ impl fmt::Debug for Context<'_> {
202214/// This handle encapsulates a [`RawWaker`] instance, which defines the
203215/// executor-specific wakeup behavior.
204216///
205- /// Implements [`Clone`], [`Send`], and [`Sync`].
217+ /// The typical life of a `Waker` is that it is constructed by an executor, wrapped in a
218+ /// [`Context`], then passed to [`Future::poll()`]. Then, if the future chooses to return
219+ /// [`Poll::Pending`], it must also store the waker somehow and call [`Waker::wake()`] when
220+ /// the future should be polled again.
221+ ///
222+ /// Implements [`Clone`], [`Send`], and [`Sync`]; therefore, a waker may be invoked
223+ /// from any thread, including ones not in any way managed by the executor. For example,
224+ /// this might be done to wake a future when a blocking function call completes on another
225+ /// thread.
226+ ///
227+ /// [`Future::poll()`]: core::future::Future::poll
228+ /// [`Poll::Pending`]: core::task::Poll::Pending
206229#[ repr( transparent) ]
207230#[ stable( feature = "futures_api" , since = "1.36.0" ) ]
208231pub struct Waker {
@@ -219,18 +242,21 @@ unsafe impl Sync for Waker {}
219242impl Waker {
220243 /// Wake up the task associated with this `Waker`.
221244 ///
222- /// As long as the runtime keeps running and the task is not finished, it is
223- /// guaranteed that each invocation of `wake` (or `wake_by_ref`) will be followed
224- /// by at least one `poll` of the task to which this `Waker` belongs. This makes
245+ /// As long as the executor keeps running and the task is not finished, it is
246+ /// guaranteed that each invocation of [`wake()`](Self::wake) (or
247+ /// [`wake_by_ref()`](Self::wake_by_ref)) will be followed by at least one
248+ /// [`poll()`] of the task to which this `Waker` belongs. This makes
225249 /// it possible to temporarily yield to other tasks while running potentially
226250 /// unbounded processing loops.
227251 ///
228252 /// Note that the above implies that multiple wake-ups may be coalesced into a
229- /// single `poll` invocation by the runtime.
253+ /// single [ `poll()`] invocation by the runtime.
230254 ///
231255 /// Also note that yielding to competing tasks is not guaranteed: it is the
232256 /// executor’s choice which task to run and the executor may choose to run the
233257 /// current task again.
258+ ///
259+ /// [`poll()`]: crate::future::Future::poll
234260 #[ inline]
235261 #[ stable( feature = "futures_api" , since = "1.36.0" ) ]
236262 pub fn wake ( self ) {
@@ -250,8 +276,8 @@ impl Waker {
250276
251277 /// Wake up the task associated with this `Waker` without consuming the `Waker`.
252278 ///
253- /// This is similar to `wake` , but may be slightly less efficient in the case
254- /// where an owned `Waker` is available. This method should be preferred to
279+ /// This is similar to [ `wake()`](Self::wake) , but may be slightly less efficient in
280+ /// the case where an owned `Waker` is available. This method should be preferred to
255281 /// calling `waker.clone().wake()`.
256282 #[ inline]
257283 #[ stable( feature = "futures_api" , since = "1.36.0" ) ]
@@ -263,7 +289,7 @@ impl Waker {
263289 unsafe { ( self . waker . vtable . wake_by_ref ) ( self . waker . data ) }
264290 }
265291
266- /// Returns `true` if this `Waker` and another `Waker` have awoken the same task.
292+ /// Returns `true` if this `Waker` and another `Waker` would awake the same task.
267293 ///
268294 /// This function works on a best-effort basis, and may return false even
269295 /// when the `Waker`s would awaken the same task. However, if this function
0 commit comments