diff options
author | Marc-Antoine Perennou <Marc-Antoine@Perennou.com> | 2020-11-05 13:00:13 +0100 |
---|---|---|
committer | GitHub <noreply@github.com> | 2020-11-05 13:00:13 +0100 |
commit | 0b3918bce956567cccc617213a56c339a5a21d6f (patch) | |
tree | fbbb4f81f135282e9f4f9b39c0ad79200cb05e32 /tokio/src/runtime/mod.rs | |
parent | e309da0beeb48a0c2ecd0f15eb600f871ec98e19 (diff) |
rt: bring back a public Handle type (#3076)
Signed-off-by: Marc-Antoine Perennou <Marc-Antoine@Perennou.com>
Co-authored-by: Alice Ryhl <alice@ryhl.io>
Co-authored-by: Carl Lerche <me@carllerche.com>
Diffstat (limited to 'tokio/src/runtime/mod.rs')
-rw-r--r-- | tokio/src/runtime/mod.rs | 46 |
1 files changed, 24 insertions, 22 deletions
diff --git a/tokio/src/runtime/mod.rs b/tokio/src/runtime/mod.rs index f85344db..d7f068ec 100644 --- a/tokio/src/runtime/mod.rs +++ b/tokio/src/runtime/mod.rs @@ -198,7 +198,7 @@ cfg_rt! { use self::enter::enter; mod handle; - use handle::Handle; + pub use handle::{EnterGuard, Handle}; mod spawner; use self::spawner::Spawner; @@ -272,16 +272,6 @@ cfg_rt! { blocking_pool: BlockingPool, } - /// Runtime context guard. - /// - /// Returned by [`Runtime::enter`], the context guard exits the runtime - /// context on drop. - #[derive(Debug)] - pub struct EnterGuard<'a> { - rt: &'a Runtime, - guard: context::EnterGuard, - } - /// The runtime executor is either a thread-pool or a current-thread executor. #[derive(Debug)] enum Kind { @@ -332,6 +322,27 @@ cfg_rt! { Builder::new_multi_thread().enable_all().build() } + /// Return a handle to the runtime's spawner. + /// + /// The returned handle can be used to spawn tasks that run on this runtime, and can + /// be cloned to allow moving the `Handle` to other threads. + /// + /// # Examples + /// + /// ``` + /// use tokio::runtime::Runtime; + /// + /// let rt = Runtime::new() + /// .unwrap(); + /// + /// let handle = rt.handle(); + /// + /// // Use the handle... + /// ``` + pub fn handle(&self) -> &Handle { + &self.handle + } + /// Spawn a future onto the Tokio runtime. /// /// This spawns the given future onto the runtime's executor, usually a @@ -363,13 +374,7 @@ cfg_rt! { F: Future + Send + 'static, F::Output: Send + 'static, { - #[cfg(feature = "tracing")] - let future = crate::util::trace::task(future, "task"); - match &self.kind { - #[cfg(feature = "rt-multi-thread")] - Kind::ThreadPool(exec) => exec.spawn(future), - Kind::CurrentThread(exec) => exec.spawn(future), - } + self.handle.spawn(future) } /// Run the provided function on an executor dedicated to blocking operations. @@ -481,10 +486,7 @@ cfg_rt! { /// } /// ``` pub fn enter(&self) -> EnterGuard<'_> { - EnterGuard { - rt: self, - guard: context::enter(self.handle.clone()), - } + self.handle.enter() } /// Shutdown the runtime, waiting for at most `duration` for all spawned |