]> git.lizzy.rs Git - rust.git/blob - src/libcore/task/wake.rs
Remove spawning from task::Context
[rust.git] / src / libcore / task / wake.rs
1 // Copyright 2018 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution and at
3 // http://rust-lang.org/COPYRIGHT.
4 //
5 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8 // option. This file may not be copied, modified, or distributed
9 // except according to those terms.
10
11 #![unstable(feature = "futures_api",
12             reason = "futures in libcore are unstable",
13             issue = "50547")]
14
15 use {fmt, mem};
16 use marker::Unpin;
17 use ptr::NonNull;
18
19 /// A `Waker` is a handle for waking up a task by notifying its executor that it
20 /// is ready to be run.
21 ///
22 /// This handle contains a trait object pointing to an instance of the `UnsafeWake`
23 /// trait, allowing notifications to get routed through it.
24 #[repr(transparent)]
25 pub struct Waker {
26     inner: NonNull<dyn UnsafeWake>,
27 }
28
29 impl Unpin for Waker {}
30 unsafe impl Send for Waker {}
31 unsafe impl Sync for Waker {}
32
33 impl Waker {
34     /// Constructs a new `Waker` directly.
35     ///
36     /// Note that most code will not need to call this. Implementers of the
37     /// `UnsafeWake` trait will typically provide a wrapper that calls this
38     /// but you otherwise shouldn't call it directly.
39     ///
40     /// If you're working with the standard library then it's recommended to
41     /// use the `Waker::from` function instead which works with the safe
42     /// `Arc` type and the safe `Wake` trait.
43     #[inline]
44     pub unsafe fn new(inner: NonNull<dyn UnsafeWake>) -> Self {
45         Waker { inner }
46     }
47
48     /// Wake up the task associated with this `Waker`.
49     #[inline]
50     pub fn wake(&self) {
51         unsafe { self.inner.as_ref().wake() }
52     }
53
54     /// Returns whether or not this `Waker` and `other` awaken the same task.
55     ///
56     /// This function works on a best-effort basis, and may return false even
57     /// when the `Waker`s would awaken the same task. However, if this function
58     /// returns true, it is guaranteed that the `Waker`s will awaken the same
59     /// task.
60     ///
61     /// This function is primarily used for optimization purposes.
62     #[inline]
63     pub fn will_wake(&self, other: &Waker) -> bool {
64         self.inner == other.inner
65     }
66 }
67
68 impl Clone for Waker {
69     #[inline]
70     fn clone(&self) -> Self {
71         unsafe {
72             self.inner.as_ref().clone_raw()
73         }
74     }
75 }
76
77 impl fmt::Debug for Waker {
78     fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
79         f.debug_struct("Waker")
80             .finish()
81     }
82 }
83
84 impl Drop for Waker {
85     #[inline]
86     fn drop(&mut self) {
87         unsafe {
88             self.inner.as_ref().drop_raw()
89         }
90     }
91 }
92
93 /// A `LocalWaker` is a handle for waking up a task by notifying its executor that it
94 /// is ready to be run.
95 ///
96 /// This is similar to the `Waker` type, but cannot be sent across threads.
97 /// Task executors can use this type to implement more optimized singlethreaded wakeup
98 /// behavior.
99 #[repr(transparent)]
100 pub struct LocalWaker {
101     inner: NonNull<dyn UnsafeWake>,
102 }
103
104 impl Unpin for LocalWaker {}
105 impl !Send for LocalWaker {}
106 impl !Sync for LocalWaker {}
107
108 impl LocalWaker {
109     /// Constructs a new `LocalWaker` directly.
110     ///
111     /// Note that most code will not need to call this. Implementers of the
112     /// `UnsafeWake` trait will typically provide a wrapper that calls this
113     /// but you otherwise shouldn't call it directly.
114     ///
115     /// If you're working with the standard library then it's recommended to
116     /// use the `local_waker_from_nonlocal` or `local_waker` to convert a `Waker`
117     /// into a `LocalWaker`.
118     ///
119     /// For this function to be used safely, it must be sound to call `inner.wake_local()`
120     /// on the current thread.
121     #[inline]
122     pub unsafe fn new(inner: NonNull<dyn UnsafeWake>) -> Self {
123         LocalWaker { inner }
124     }
125
126     /// Converts this `LocalWaker` into a `Waker`.
127     ///
128     /// `Waker` is nearly identical to `LocalWaker`, but is threadsafe
129     /// (implements `Send` and `Sync`).
130     #[inline]
131     pub fn into_waker(self) -> Waker {
132         self.into()
133     }
134
135     /// Wake up the task associated with this `LocalWaker`.
136     #[inline]
137     pub fn wake(&self) {
138         unsafe { self.inner.as_ref().wake_local() }
139     }
140
141     /// Returns whether or not this `LocalWaker` and `other` `LocalWaker` awaken the same task.
142     ///
143     /// This function works on a best-effort basis, and may return false even
144     /// when the `LocalWaker`s would awaken the same task. However, if this function
145     /// returns true, it is guaranteed that the `LocalWaker`s will awaken the same
146     /// task.
147     ///
148     /// This function is primarily used for optimization purposes.
149     #[inline]
150     pub fn will_wake(&self, other: &LocalWaker) -> bool {
151         self.inner == other.inner
152     }
153
154     /// Returns whether or not this `LocalWaker` and `other` `Waker` awaken the same task.
155     ///
156     /// This function works on a best-effort basis, and may return false even
157     /// when the `Waker`s would awaken the same task. However, if this function
158     /// returns true, it is guaranteed that the `LocalWaker`s will awaken the same
159     /// task.
160     ///
161     /// This function is primarily used for optimization purposes.
162     #[inline]
163     pub fn will_wake_nonlocal(&self, other: &Waker) -> bool {
164         self.inner == other.inner
165     }
166 }
167
168 impl From<LocalWaker> for Waker {
169     #[inline]
170     fn from(local_waker: LocalWaker) -> Self {
171         let inner = local_waker.inner;
172         mem::forget(local_waker);
173         Waker { inner }
174     }
175 }
176
177 impl Clone for LocalWaker {
178     #[inline]
179     fn clone(&self) -> Self {
180         let waker = unsafe { self.inner.as_ref().clone_raw() };
181         let inner = waker.inner;
182         mem::forget(waker);
183         LocalWaker { inner }
184     }
185 }
186
187 impl fmt::Debug for LocalWaker {
188     fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
189         f.debug_struct("Waker")
190             .finish()
191     }
192 }
193
194 impl Drop for LocalWaker {
195     #[inline]
196     fn drop(&mut self) {
197         unsafe {
198             self.inner.as_ref().drop_raw()
199         }
200     }
201 }
202
203 /// An unsafe trait for implementing custom memory management for a `Waker` or `LocalWaker`.
204 ///
205 /// A `Waker` conceptually is a cloneable trait object for `Wake`, and is
206 /// most often essentially just `Arc<dyn Wake>`. However, in some contexts
207 /// (particularly `no_std`), it's desirable to avoid `Arc` in favor of some
208 /// custom memory management strategy. This trait is designed to allow for such
209 /// customization.
210 ///
211 /// When using `std`, a default implementation of the `UnsafeWake` trait is provided for
212 /// `Arc<T>` where `T: Wake`.
213 pub unsafe trait UnsafeWake: Send + Sync {
214     /// Creates a clone of this `UnsafeWake` and stores it behind a `Waker`.
215     ///
216     /// This function will create a new uniquely owned handle that under the
217     /// hood references the same notification instance. In other words calls
218     /// to `wake` on the returned handle should be equivalent to calls to
219     /// `wake` on this handle.
220     ///
221     /// # Unsafety
222     ///
223     /// This function is unsafe to call because it's asserting the `UnsafeWake`
224     /// value is in a consistent state, i.e. hasn't been dropped.
225     unsafe fn clone_raw(&self) -> Waker;
226
227     /// Drops this instance of `UnsafeWake`, deallocating resources
228     /// associated with it.
229     ///
230     /// FIXME(cramertj)
231     /// This method is intended to have a signature such as:
232     ///
233     /// ```ignore (not-a-doctest)
234     /// fn drop_raw(self: *mut Self);
235     /// ```
236     ///
237     /// Unfortunately in Rust today that signature is not object safe.
238     /// Nevertheless it's recommended to implement this function *as if* that
239     /// were its signature. As such it is not safe to call on an invalid
240     /// pointer, nor is the validity of the pointer guaranteed after this
241     /// function returns.
242     ///
243     /// # Unsafety
244     ///
245     /// This function is unsafe to call because it's asserting the `UnsafeWake`
246     /// value is in a consistent state, i.e. hasn't been dropped.
247     unsafe fn drop_raw(&self);
248
249     /// Indicates that the associated task is ready to make progress and should
250     /// be `poll`ed.
251     ///
252     /// Executors generally maintain a queue of "ready" tasks; `wake` should place
253     /// the associated task onto this queue.
254     ///
255     /// # Panics
256     ///
257     /// Implementations should avoid panicking, but clients should also be prepared
258     /// for panics.
259     ///
260     /// # Unsafety
261     ///
262     /// This function is unsafe to call because it's asserting the `UnsafeWake`
263     /// value is in a consistent state, i.e. hasn't been dropped.
264     unsafe fn wake(&self);
265
266     /// Indicates that the associated task is ready to make progress and should
267     /// be `poll`ed. This function is the same as `wake`, but can only be called
268     /// from the thread that this `UnsafeWake` is "local" to. This allows for
269     /// implementors to provide specialized wakeup behavior specific to the current
270     /// thread. This function is called by `LocalWaker::wake`.
271     ///
272     /// Executors generally maintain a queue of "ready" tasks; `wake_local` should place
273     /// the associated task onto this queue.
274     ///
275     /// # Panics
276     ///
277     /// Implementations should avoid panicking, but clients should also be prepared
278     /// for panics.
279     ///
280     /// # Unsafety
281     ///
282     /// This function is unsafe to call because it's asserting the `UnsafeWake`
283     /// value is in a consistent state, i.e. hasn't been dropped, and that the
284     /// `UnsafeWake` hasn't moved from the thread on which it was created.
285     unsafe fn wake_local(&self) {
286         self.wake()
287     }
288 }