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/libcore/task/context.rs | 98 --------------------------------------------- src/libcore/task/mod.rs | 6 --- src/libcore/task/spawn.rs | 93 ------------------------------------------ src/libcore/task/wake.rs | 9 +++++ 4 files changed, 9 insertions(+), 197 deletions(-) delete mode 100644 src/libcore/task/context.rs delete mode 100644 src/libcore/task/spawn.rs (limited to 'src/libcore/task') diff --git a/src/libcore/task/context.rs b/src/libcore/task/context.rs deleted file mode 100644 index 5a29c8528ef..00000000000 --- a/src/libcore/task/context.rs +++ /dev/null @@ -1,98 +0,0 @@ -// Copyright 2018 The Rust Project Developers. See the COPYRIGHT -// file at the top-level directory of this distribution and at -// http://rust-lang.org/COPYRIGHT. -// -// Licensed under the Apache License, Version 2.0 or the MIT license -// , at your -// option. This file may not be copied, modified, or distributed -// except according to those terms. - -#![unstable(feature = "futures_api", - reason = "futures in libcore are unstable", - issue = "50547")] - -use fmt; -use super::{Spawn, Waker, LocalWaker}; - -/// Information about the currently-running task. -/// -/// Contexts are always tied to the stack, since they are set up specifically -/// when performing a single `poll` step on a task. -pub struct Context<'a> { - local_waker: &'a LocalWaker, - spawner: &'a mut dyn Spawn, -} - -impl<'a> fmt::Debug for Context<'a> { - fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { - f.debug_struct("Context") - .finish() - } -} - -impl<'a> Context<'a> { - /// Create a new task `Context` with the provided `local_waker`, `waker`, - /// and `spawner`. - #[inline] - pub fn new( - local_waker: &'a LocalWaker, - spawner: &'a mut dyn Spawn, - ) -> Context<'a> { - Context { local_waker, spawner } - } - - /// Get the `LocalWaker` associated with the current task. - #[inline] - pub fn local_waker(&self) -> &'a LocalWaker { - self.local_waker - } - - /// Get the `Waker` associated with the current task. - #[inline] - pub fn waker(&self) -> &'a Waker { - unsafe { &*(self.local_waker as *const LocalWaker as *const Waker) } - } - - /// Get the spawner associated with this task. - /// - /// This method is useful primarily if you want to explicitly handle - /// spawn failures. - #[inline] - pub fn spawner(&mut self) -> &mut dyn Spawn { - self.spawner - } - - /// Produce a context like the current one, but using the given waker - /// instead. - /// - /// This advanced method is primarily used when building "internal - /// schedulers" within a task, where you want to provide some customized - /// wakeup logic. - #[inline] - pub fn with_waker<'b>( - &'b mut self, - local_waker: &'b LocalWaker, - ) -> Context<'b> { - Context { - local_waker, - spawner: self.spawner, - } - } - - /// Produce a context like the current one, but using the given spawner - /// instead. - /// - /// This advanced method is primarily used when building "internal - /// schedulers" within a task. - #[inline] - pub fn with_spawner<'b, Sp: Spawn>( - &'b mut self, - spawner: &'b mut Sp, - ) -> Context<'b> { - Context { - local_waker: self.local_waker, - spawner, - } - } -} diff --git a/src/libcore/task/mod.rs b/src/libcore/task/mod.rs index f51e5f7ce0e..95c9cca292f 100644 --- a/src/libcore/task/mod.rs +++ b/src/libcore/task/mod.rs @@ -14,12 +14,6 @@ //! Types and Traits for working with asynchronous tasks. -mod context; -pub use self::context::Context; - -mod spawn; -pub use self::spawn::{Spawn, SpawnErrorKind, SpawnObjError, SpawnLocalObjError}; - mod poll; pub use self::poll::Poll; diff --git a/src/libcore/task/spawn.rs b/src/libcore/task/spawn.rs deleted file mode 100644 index 58ee85d232b..00000000000 --- a/src/libcore/task/spawn.rs +++ /dev/null @@ -1,93 +0,0 @@ -// Copyright 2018 The Rust Project Developers. See the COPYRIGHT -// file at the top-level directory of this distribution and at -// http://rust-lang.org/COPYRIGHT. -// -// Licensed under the Apache License, Version 2.0 or the MIT license -// , at your -// option. This file may not be copied, modified, or distributed -// except according to those terms. - -#![unstable(feature = "futures_api", - reason = "futures in libcore are unstable", - issue = "50547")] - -use fmt; -use future::{FutureObj, LocalFutureObj}; - -/// Spawns tasks that poll futures to completion onto its associated task -/// executor. -/// -/// The term "task" refers to a kind of lightweight "thread". Task executors -/// are responsible for scheduling the execution of tasks on operating system -/// threads. -pub trait Spawn { - /// Spawns a new task with the given future. The future will be polled until - /// completion. - /// - /// # Errors - /// - /// The executor may be unable to spawn tasks, either because it has - /// been shut down or is resource-constrained. - fn spawn_obj( - &mut self, - future: FutureObj<'static, ()>, - ) -> Result<(), SpawnObjError>; - - /// Determines whether the executor is able to spawn new tasks. - /// - /// # Returns - /// - /// An `Ok` return means the executor is *likely* (but not guaranteed) - /// to accept a subsequent spawn attempt. Likewise, an `Err` return - /// means that `spawn` is likely, but not guaranteed, to yield an error. - #[inline] - fn status(&self) -> Result<(), SpawnErrorKind> { - Ok(()) - } -} - -/// Provides the reason that an executor was unable to spawn. -pub struct SpawnErrorKind { - _hidden: (), -} - -impl fmt::Debug for SpawnErrorKind { - fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { - f.debug_tuple("SpawnErrorKind") - .field(&"shutdown") - .finish() - } -} - -impl SpawnErrorKind { - /// Spawning is failing because the executor has been shut down. - pub fn shutdown() -> SpawnErrorKind { - SpawnErrorKind { _hidden: () } - } - - /// Check whether this error is the `shutdown` error. - pub fn is_shutdown(&self) -> bool { - true - } -} - -/// The result of a failed spawn -#[derive(Debug)] -pub struct SpawnObjError { - /// The kind of error - pub kind: SpawnErrorKind, - - /// The future for which spawning inside a task was attempted - pub future: FutureObj<'static, ()>, -} - -/// The result of a failed spawn -#[derive(Debug)] -pub struct SpawnLocalObjError { - /// The kind of error - pub kind: SpawnErrorKind, - - /// The future for which spawning inside a task was attempted - pub future: LocalFutureObj<'static, ()>, -} diff --git a/src/libcore/task/wake.rs b/src/libcore/task/wake.rs index d770536ef42..651db6356ba 100644 --- a/src/libcore/task/wake.rs +++ b/src/libcore/task/wake.rs @@ -123,6 +123,15 @@ impl LocalWaker { LocalWaker { inner } } + /// Converts this `LocalWaker` into a `Waker`. + /// + /// `Waker` is nearly identical to `LocalWaker`, but is threadsafe + /// (implements `Send` and `Sync`). + #[inline] + pub fn into_waker(self) -> Waker { + self.into() + } + /// Wake up the task associated with this `LocalWaker`. #[inline] pub fn wake(&self) { -- cgit 1.4.1-3-g733a5