]> git.lizzy.rs Git - rust.git/blobdiff - src/libstd/future.rs
Remove spawning from task::Context
[rust.git] / src / libstd / future.rs
index cadb5c0ba5d0e019b5a1156512e5c97b1a466cf0..1cadbdc66c3986e76e0eb30914f1cd80c3e05c8b 100644 (file)
 
 use core::cell::Cell;
 use core::marker::Unpin;
-use core::mem::PinMut;
+use core::pin::Pin;
 use core::option::Option;
 use core::ptr::NonNull;
-use core::task::{self, Poll};
+use core::task::{LocalWaker, Poll};
 use core::ops::{Drop, Generator, GeneratorState};
 
 #[doc(inline)]
@@ -42,8 +42,8 @@ impl<T: Generator<Yield = ()>> !Unpin for GenFuture<T> {}
 #[unstable(feature = "gen_future", issue = "50547")]
 impl<T: Generator<Yield = ()>> Future for GenFuture<T> {
     type Output = T::Return;
-    fn poll(self: PinMut<Self>, cx: &mut task::Context) -> Poll<Self::Output> {
-        set_task_cx(cx, || match unsafe { PinMut::get_mut_unchecked(self).0.resume() } {
+    fn poll(self: Pin<&mut Self>, lw: &LocalWaker) -> Poll<Self::Output> {
+        set_task_waker(lw, || match unsafe { Pin::get_mut_unchecked(self).0.resume() } {
             GeneratorState::Yielded(()) => Poll::Pending,
             GeneratorState::Complete(x) => Poll::Ready(x),
         })
@@ -51,66 +51,61 @@ fn poll(self: PinMut<Self>, cx: &mut task::Context) -> Poll<Self::Output> {
 }
 
 thread_local! {
-    static TLS_CX: Cell<Option<NonNull<task::Context<'static>>>> = Cell::new(None);
+    static TLS_WAKER: Cell<Option<NonNull<LocalWaker>>> = Cell::new(None);
 }
 
-struct SetOnDrop(Option<NonNull<task::Context<'static>>>);
+struct SetOnDrop(Option<NonNull<LocalWaker>>);
 
 impl Drop for SetOnDrop {
     fn drop(&mut self) {
-        TLS_CX.with(|tls_cx| {
-            tls_cx.set(self.0.take());
+        TLS_WAKER.with(|tls_waker| {
+            tls_waker.set(self.0.take());
         });
     }
 }
 
 #[unstable(feature = "gen_future", issue = "50547")]
 /// Sets the thread-local task context used by async/await futures.
-pub fn set_task_cx<F, R>(cx: &mut task::Context, f: F) -> R
+pub fn set_task_waker<F, R>(lw: &LocalWaker, f: F) -> R
 where
     F: FnOnce() -> R
 {
-    let old_cx = TLS_CX.with(|tls_cx| {
-        tls_cx.replace(NonNull::new(
-            cx
-                as *mut task::Context
-                as *mut ()
-                as *mut task::Context<'static>
-        ))
+    let old_waker = TLS_WAKER.with(|tls_waker| {
+        tls_waker.replace(Some(NonNull::from(lw)))
     });
-    let _reset_cx = SetOnDrop(old_cx);
+    let _reset_waker = SetOnDrop(old_waker);
     f()
 }
 
 #[unstable(feature = "gen_future", issue = "50547")]
-/// Retrieves the thread-local task context used by async/await futures.
+/// Retrieves the thread-local task waker used by async/await futures.
 ///
-/// This function acquires exclusive access to the task context.
+/// This function acquires exclusive access to the task waker.
 ///
-/// Panics if no task has been set or if the task context has already been
-/// retrieved by a surrounding call to get_task_cx.
-pub fn get_task_cx<F, R>(f: F) -> R
+/// Panics if no waker has been set or if the waker has already been
+/// retrieved by a surrounding call to get_task_waker.
+pub fn get_task_waker<F, R>(f: F) -> R
 where
-    F: FnOnce(&mut task::Context) -> R
+    F: FnOnce(&LocalWaker) -> R
 {
-    let cx_ptr = TLS_CX.with(|tls_cx| {
-        // Clear the entry so that nested `with_get_cx` calls
+    let waker_ptr = TLS_WAKER.with(|tls_waker| {
+        // Clear the entry so that nested `get_task_waker` calls
         // will fail or set their own value.
-        tls_cx.replace(None)
+        tls_waker.replace(None)
     });
-    let _reset_cx = SetOnDrop(cx_ptr);
+    let _reset_waker = SetOnDrop(waker_ptr);
 
-    let mut cx_ptr = cx_ptr.expect(
-        "TLS task::Context not set. This is a rustc bug. \
+    let mut waker_ptr = waker_ptr.expect(
+        "TLS LocalWaker not set. This is a rustc bug. \
         Please file an issue on https://github.com/rust-lang/rust.");
-    unsafe { f(cx_ptr.as_mut()) }
+    unsafe { f(waker_ptr.as_mut()) }
 }
 
 #[unstable(feature = "gen_future", issue = "50547")]
-/// Polls a future in the current thread-local task context.
-pub fn poll_in_task_cx<F>(f: PinMut<F>) -> Poll<F::Output>
+/// Polls a future in the current thread-local task waker.
+pub fn poll_with_tls_waker<F>(f: Pin<&mut F>) -> Poll<F::Output>
 where
     F: Future
 {
-    get_task_cx(|cx| f.poll(cx))
+    get_task_waker(|lw| F::poll(f, lw))
 }