]> git.lizzy.rs Git - rust.git/blob - src/libcore/task/wake.rs
0759ff93ea85f0da49acb9f346068da1b0622a83
[rust.git] / src / libcore / task / wake.rs
1 #![stable(feature = "futures_api", since = "1.36.0")]
2
3 use crate::fmt;
4 use crate::marker::{PhantomData, Unpin};
5
6 /// A `RawWaker` allows the implementor of a task executor to create a [`Waker`]
7 /// which provides customized wakeup behavior.
8 ///
9 /// [vtable]: https://en.wikipedia.org/wiki/Virtual_method_table
10 ///
11 /// It consists of a data pointer and a [virtual function pointer table (vtable)][vtable] that
12 /// customizes the behavior of the `RawWaker`.
13 ///
14 /// [`Waker`]: struct.Waker.html
15 #[derive(PartialEq, Debug)]
16 #[stable(feature = "futures_api", since = "1.36.0")]
17 pub struct RawWaker {
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.
23     data: *const (),
24     /// Virtual function pointer table that customizes the behavior of this waker.
25     vtable: &'static RawWakerVTable,
26 }
27
28 impl RawWaker {
29     /// Creates a new `RawWaker` from the provided `data` pointer and `vtable`.
30     ///
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.
36     ///
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.
40     #[rustc_promotable]
41     #[stable(feature = "futures_api", since = "1.36.0")]
42     pub const fn new(data: *const (), vtable: &'static RawWakerVTable) -> RawWaker {
43         RawWaker { data, vtable }
44     }
45 }
46
47 /// A virtual function pointer table (vtable) that specifies the behavior
48 /// of a [`RawWaker`].
49 ///
50 /// The pointer passed to all functions inside the vtable is the `data` pointer
51 /// from the enclosing [`RawWaker`] object.
52 ///
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.
57 ///
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.
64     ///
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`].
69     ///
70     /// [`Waker`]: struct.Waker.html
71     /// [`RawWaker`]: struct.RawWaker.html
72     clone: unsafe fn(*const ()) -> RawWaker,
73
74     /// This function will be called when `wake` is called on the [`Waker`].
75     /// It must wake up the task associated with this [`RawWaker`].
76     ///
77     /// The implementation of this function must make sure to release any
78     /// resources that are associated with this instance of a [`RawWaker`] and
79     /// associated task.
80     ///
81     /// [`Waker`]: struct.Waker.html
82     /// [`RawWaker`]: struct.RawWaker.html
83     wake: unsafe fn(*const ()),
84
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`].
87     ///
88     /// This function is similar to `wake`, but must not consume the provided data
89     /// pointer.
90     ///
91     /// [`Waker`]: struct.Waker.html
92     /// [`RawWaker`]: struct.RawWaker.html
93     wake_by_ref: unsafe fn(*const ()),
94
95     /// This function gets called when a [`RawWaker`] gets dropped.
96     ///
97     /// The implementation of this function must make sure to release any
98     /// resources that are associated with this instance of a [`RawWaker`] and
99     /// associated task.
100     ///
101     /// [`RawWaker`]: struct.RawWaker.html
102     drop: unsafe fn(*const ()),
103 }
104
105 impl RawWakerVTable {
106     /// Creates a new `RawWakerVTable` from the provided `clone`, `wake`,
107     /// `wake_by_ref`, and `drop` functions.
108     ///
109     /// # `clone`
110     ///
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.
113     ///
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`].
118     ///
119     /// # `wake`
120     ///
121     /// This function will be called when `wake` is called on the [`Waker`].
122     /// It must wake up the task associated with this [`RawWaker`].
123     ///
124     /// The implementation of this function must make sure to release any
125     /// resources that are associated with this instance of a [`RawWaker`] and
126     /// associated task.
127     ///
128     /// # `wake_by_ref`
129     ///
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`].
132     ///
133     /// This function is similar to `wake`, but must not consume the provided data
134     /// pointer.
135     ///
136     /// # `drop`
137     ///
138     /// This function gets called when a [`RawWaker`] gets dropped.
139     ///
140     /// The implementation of this function must make sure to release any
141     /// resources that are associated with this instance of a [`RawWaker`] and
142     /// associated task.
143     ///
144     /// [`Waker`]: struct.Waker.html
145     /// [`RawWaker`]: struct.RawWaker.html
146     #[rustc_promotable]
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.
150     //
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]
154     pub const fn new(
155         clone: unsafe fn(*const ()) -> RawWaker,
156         wake: unsafe fn(*const ()),
157         wake_by_ref: unsafe fn(*const ()),
158         drop: unsafe fn(*const ()),
159     ) -> Self {
160         Self { clone, wake, wake_by_ref, drop }
161     }
162 }
163
164 /// The `Context` of an asynchronous task.
165 ///
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> {
170     waker: &'a Waker,
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
174     // covariant).
175     _marker: PhantomData<fn(&'a ()) -> &'a ()>,
176 }
177
178 impl<'a> Context<'a> {
179     /// Create a new `Context` from a `&Waker`.
180     #[stable(feature = "futures_api", since = "1.36.0")]
181     #[inline]
182     pub fn from_waker(waker: &'a Waker) -> Self {
183         Context { waker, _marker: PhantomData }
184     }
185
186     /// Returns a reference to the `Waker` for the current task.
187     #[stable(feature = "futures_api", since = "1.36.0")]
188     #[inline]
189     pub fn waker(&self) -> &'a Waker {
190         &self.waker
191     }
192 }
193
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()
198     }
199 }
200
201 /// A `Waker` is a handle for waking up a task by notifying its executor that it
202 /// is ready to be run.
203 ///
204 /// This handle encapsulates a [`RawWaker`] instance, which defines the
205 /// executor-specific wakeup behavior.
206 ///
207 /// Implements [`Clone`], [`Send`], and [`Sync`].
208 ///
209 /// [`RawWaker`]: struct.RawWaker.html
210 #[repr(transparent)]
211 #[stable(feature = "futures_api", since = "1.36.0")]
212 pub struct Waker {
213     waker: RawWaker,
214 }
215
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 {}
222
223 impl Waker {
224     /// Wake up the task associated with this `Waker`.
225     #[inline]
226     #[stable(feature = "futures_api", since = "1.36.0")]
227     pub fn wake(self) {
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;
232
233         // Don't call `drop` -- the waker will be consumed by `wake`.
234         crate::mem::forget(self);
235
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) };
240     }
241
242     /// Wake up the task associated with this `Waker` without consuming the `Waker`.
243     ///
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()`.
247     #[inline]
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.
252
253         // SAFETY: see `wake`
254         unsafe { (self.waker.vtable.wake_by_ref)(self.waker.data) }
255     }
256
257     /// Returns `true` if this `Waker` and another `Waker` have awoken the same task.
258     ///
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.
262     ///
263     /// This function is primarily used for optimization purposes.
264     #[inline]
265     #[stable(feature = "futures_api", since = "1.36.0")]
266     pub fn will_wake(&self, other: &Waker) -> bool {
267         self.waker == other.waker
268     }
269
270     /// Creates a new `Waker` from [`RawWaker`].
271     ///
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.
275     ///
276     /// [`RawWaker`]: struct.RawWaker.html
277     /// [`RawWakerVTable`]: struct.RawWakerVTable.html
278     #[inline]
279     #[stable(feature = "futures_api", since = "1.36.0")]
280     pub unsafe fn from_raw(waker: RawWaker) -> Waker {
281         Waker { waker }
282     }
283 }
284
285 #[stable(feature = "futures_api", since = "1.36.0")]
286 impl Clone for Waker {
287     #[inline]
288     fn clone(&self) -> Self {
289         Waker {
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) },
294         }
295     }
296 }
297
298 #[stable(feature = "futures_api", since = "1.36.0")]
299 impl Drop for Waker {
300     #[inline]
301     fn drop(&mut self) {
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) }
306     }
307 }
308
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)
316             .finish()
317     }
318 }