From 1b00f0b9fa92daa489510b8718ce56130420795f Mon Sep 17 00:00:00 2001 From: Taylor Cramer Date: Tue, 18 Sep 2018 17:50:32 -0700 Subject: Remove spawning from task::Context --- src/libstd/future.rs | 59 ++++++++++++++++++++++++---------------------------- src/libstd/macros.rs | 2 +- src/libstd/panic.rs | 6 +++--- 3 files changed, 31 insertions(+), 36 deletions(-) (limited to 'src/libstd') diff --git a/src/libstd/future.rs b/src/libstd/future.rs index 262646738cf..1cadbdc66c3 100644 --- a/src/libstd/future.rs +++ b/src/libstd/future.rs @@ -15,7 +15,7 @@ use core::marker::Unpin; 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> !Unpin for GenFuture {} #[unstable(feature = "gen_future", issue = "50547")] impl> Future for GenFuture { type Output = T::Return; - fn poll(self: Pin<&mut Self>, cx: &mut task::Context) -> Poll { - set_task_cx(cx, || match unsafe { Pin::get_mut_unchecked(self).0.resume() } { + fn poll(self: Pin<&mut Self>, lw: &LocalWaker) -> Poll { + 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 @@ impl> Future for GenFuture { } thread_local! { - static TLS_CX: Cell>>> = Cell::new(None); + static TLS_WAKER: Cell>> = Cell::new(None); } -struct SetOnDrop(Option>>); +struct SetOnDrop(Option>); 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(cx: &mut task::Context, f: F) -> R +pub fn set_task_waker(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: 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: 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: Pin<&mut F>) -> Poll +/// Polls a future in the current thread-local task waker. +pub fn poll_with_tls_waker(f: Pin<&mut F>) -> Poll where F: Future { - get_task_cx(|cx| F::poll(f, cx)) + get_task_waker(|lw| F::poll(f, lw)) } diff --git a/src/libstd/macros.rs b/src/libstd/macros.rs index e60ef46e738..06056d6ed20 100644 --- a/src/libstd/macros.rs +++ b/src/libstd/macros.rs @@ -229,7 +229,7 @@ macro_rules! await { let mut pinned = $e; loop { if let $crate::task::Poll::Ready(x) = - $crate::future::poll_in_task_cx(unsafe { + $crate::future::poll_with_tls_waker(unsafe { $crate::pin::Pin::new_unchecked(&mut pinned) }) { diff --git a/src/libstd/panic.rs b/src/libstd/panic.rs index bd7a92e9b3f..48a9b2f4a93 100644 --- a/src/libstd/panic.rs +++ b/src/libstd/panic.rs @@ -22,7 +22,7 @@ use panicking; use ptr::{Unique, NonNull}; use rc::Rc; use sync::{Arc, Mutex, RwLock, atomic}; -use task::{self, Poll}; +use task::{LocalWaker, Poll}; use thread::Result; #[stable(feature = "panic_hooks", since = "1.10.0")] @@ -327,9 +327,9 @@ impl fmt::Debug for AssertUnwindSafe { impl<'a, F: Future> Future for AssertUnwindSafe { type Output = F::Output; - fn poll(self: Pin<&mut Self>, cx: &mut task::Context) -> Poll { + fn poll(self: Pin<&mut Self>, lw: &LocalWaker) -> Poll { let pinned_field = unsafe { Pin::map_unchecked_mut(self, |x| &mut x.0) }; - F::poll(pinned_field, cx) + F::poll(pinned_field, lw) } } -- cgit 1.4.1-3-g733a5