diff options
author | Carl Lerche <me@carllerche.com> | 2019-11-01 13:18:52 -0700 |
---|---|---|
committer | GitHub <noreply@github.com> | 2019-11-01 13:18:52 -0700 |
commit | d70c928d88dff9e3e8d673b8ee02bce131598550 (patch) | |
tree | 6b079db2f80bd61764203a32ffe48769d18c1386 /tokio/src/runtime/threadpool/mod.rs | |
parent | 742d89b0f333150f6a550ae7840235851f4eb069 (diff) |
runtime: merge multi & single threaded runtimes (#1716)
Simplify Tokio's runtime construct by combining both Runtime variants
into a single type. The execution style can be controlled by a
configuration setting on `Builder`.
The implication of this change is that there is no longer any way to
spawn `!Send` futures. This, however, is a temporary limitation. A
different strategy will be employed for supporting `!Send` futures.
Included in this patch is a rework of `task::JoinHandle` to support
using this type from both the thread-pool and current-thread executors.
Diffstat (limited to 'tokio/src/runtime/threadpool/mod.rs')
-rw-r--r-- | tokio/src/runtime/threadpool/mod.rs | 202 |
1 files changed, 0 insertions, 202 deletions
diff --git a/tokio/src/runtime/threadpool/mod.rs b/tokio/src/runtime/threadpool/mod.rs deleted file mode 100644 index dad3437b..00000000 --- a/tokio/src/runtime/threadpool/mod.rs +++ /dev/null @@ -1,202 +0,0 @@ -mod builder; -#[allow(unreachable_pub)] // https://github.com/rust-lang/rust/issues/57411 -pub use self::builder::Builder; - -mod spawner; -#[allow(unreachable_pub)] // https://github.com/rust-lang/rust/issues/57411 -pub use self::spawner::Spawner; - -#[allow(unreachable_pub)] // https://github.com/rust-lang/rust/issues/57411 -pub use crate::executor::thread_pool::JoinHandle; - -use crate::net::driver; -use crate::timer::timer; - -use crate::executor::thread_pool::ThreadPool; - -use std::future::Future; -use std::io; - -/// Handle to the Tokio runtime. -/// -/// The Tokio runtime includes a reactor as well as an executor for running -/// tasks. -/// -/// Instances of `Runtime` can be created using [`new`] or [`Builder`]. However, -/// most users will use [`tokio::run`], which uses a `Runtime` internally. -/// -/// See [module level][mod] documentation for more details. -/// -/// [mod]: index.html -/// [`new`]: #method.new -/// [`Builder`]: struct.Builder.html -/// [`tokio::run`]: fn.run.html -#[derive(Debug)] -pub struct Runtime { - inner: Option<Inner>, -} - -#[derive(Debug)] -struct Inner { - /// Task execution pool. - pool: ThreadPool, - - /// Reactor handles - reactor_handles: Vec<crate::net::driver::Handle>, - - /// Timer handles - timer_handles: Vec<timer::Handle>, -} - -// ===== impl Runtime ===== - -impl Runtime { - /// Create a new runtime instance with default configuration values. - /// - /// This results in a reactor, thread pool, and timer being initialized. The - /// thread pool will not spawn any worker threads until it needs to, i.e. - /// tasks are scheduled to run. - /// - /// Most users will not need to call this function directly, instead they - /// will use [`tokio::run`](fn.run.html). - /// - /// See [module level][mod] documentation for more details. - /// - /// # Examples - /// - /// Creating a new `Runtime` with default configuration values. - /// - /// ``` - /// use tokio::runtime::Runtime; - /// - /// let rt = Runtime::new() - /// .unwrap(); - /// - /// // Use the runtime... - /// ``` - /// - /// [mod]: index.html - pub fn new() -> io::Result<Self> { - Builder::new().build() - } - - /// Spawn a future onto the Tokio runtime. - /// - /// This spawns the given future onto the runtime's executor, usually a - /// thread pool. The thread pool is then responsible for polling the future - /// until it completes. - /// - /// See [module level][mod] documentation for more details. - /// - /// [mod]: index.html - /// - /// # Examples - /// - /// ``` - /// use tokio::runtime::Runtime; - /// - /// fn main() { - /// // Create the runtime - /// let rt = Runtime::new().unwrap(); - /// - /// // Spawn a future onto the runtime - /// rt.spawn(async { - /// println!("now running on a worker thread"); - /// }); - /// } - /// ``` - /// - /// # Panics - /// - /// This function panics if the spawn fails. Failure occurs if the executor - /// is currently at capacity and is unable to spawn a new future. - pub fn spawn<F>(&self, future: F) -> &Self - where - F: Future<Output = ()> + Send + 'static, - { - self.inner().pool.spawn(future); - self - } - - /// Run a future to completion on the Tokio runtime. - /// - /// This runs the given future on the runtime, blocking until it is - /// complete, and yielding its resolved result. Any tasks or timers which - /// the future spawns internally will be executed on the runtime. - /// - /// This method should not be called from an asynchronous context. - /// - /// # Panics - /// - /// This function panics if the executor is at capacity, if the provided - /// future panics, or if called within an asynchronous execution context. - pub fn block_on<F: Future>(&self, future: F) -> F::Output { - let _reactor = driver::set_default(&self.inner().reactor_handles[0]); - let _timer = timer::set_default(&self.inner().timer_handles[0]); - - self.inner().pool.block_on(future) - } - - /// Return a handle to the runtime's spawner. - /// - /// The returned handle can be used to spawn tasks that run on this runtime. - /// - /// # Examples - /// - /// ``` - /// use tokio::runtime::Runtime; - /// - /// let rt = Runtime::new() - /// .unwrap(); - /// - /// let spawner = rt.spawner(); - /// - /// spawner.spawn(async { println!("hello"); }); - /// ``` - pub fn spawner(&self) -> Spawner { - let inner = self.inner().pool.spawner().clone(); - Spawner::new(inner) - } - - /// Signals the runtime to shutdown immediately. - /// - /// Blocks the current thread until the shutdown operation has completed. - /// This function will forcibly shutdown the runtime, causing any - /// in-progress work to become canceled. - /// - /// The shutdown steps are: - /// - /// * Drain any scheduled work queues. - /// * Drop any futures that have not yet completed. - /// * Drop the reactor. - /// - /// Once the reactor has dropped, any outstanding I/O resources bound to - /// that reactor will no longer function. Calling any method on them will - /// result in an error. - /// - /// See [module level][mod] documentation for more details. - /// - /// # Examples - /// - /// ``` - /// use tokio::runtime::Runtime; - /// - /// let rt = Runtime::new() - /// .unwrap(); - /// - /// // Use the runtime... - /// - /// // Shutdown the runtime - /// rt.shutdown_now(); - /// ``` - /// - /// [mod]: index.html - #[allow(warnings)] - pub fn shutdown_now(mut self) { - self.inner.unwrap().pool.shutdown_now(); - } - - fn inner(&self) -> &Inner { - self.inner.as_ref().unwrap() - } -} |