From bb99d33fb02a716654c3fd84361c799f838a7d6a Mon Sep 17 00:00:00 2001 From: Abutalib Aghayev Date: Thu, 27 Oct 2022 15:55:51 -0400 Subject: [PATCH] rt: add a method to determine if the runtime is multi-threaded. --- tokio/src/runtime/handle.rs | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) diff --git a/tokio/src/runtime/handle.rs b/tokio/src/runtime/handle.rs index 91974252610..07150927f8f 100644 --- a/tokio/src/runtime/handle.rs +++ b/tokio/src/runtime/handle.rs @@ -101,6 +101,33 @@ impl Handle { } } + /// Returns true if the current `Runtime` is multi-threaded and false + /// otherwise. + /// + /// ``` + /// use tokio::runtime::Handle; + /// + /// #[tokio::main(flavor = "current_thread")] + /// async fn main() { + /// assert!(!Handle::current().is_multi_threaded()); + /// } + /// ``` + /// + /// ``` + /// use tokio::runtime::Handle; + /// + /// #[tokio::main(flavor = "multi_thread", worker_threads = 4)] + /// async fn main() { + /// assert!(Handle::current().is_multi_threaded()); + /// } + /// ``` + pub fn is_multi_threaded(&self) -> bool { + match self.inner { + scheduler::Handle::CurrentThread(_) => false, + scheduler::Handle::MultiThread(_) => true, + } + } + /// Returns a Handle view over the currently running Runtime /// /// Returns an error if no Runtime has been started