Skip to content

Commit

Permalink
rt: add a method to determine if the flavor of the runtime.
Browse files Browse the repository at this point in the history
  • Loading branch information
agayev committed Oct 29, 2022
1 parent 32d68fe commit d07b842
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 1 deletion.
28 changes: 27 additions & 1 deletion tokio/src/runtime/handle.rs
@@ -1,4 +1,4 @@
use crate::runtime::scheduler;
use crate::runtime::{scheduler, Flavor};

/// Handle to the runtime.
///
Expand Down Expand Up @@ -101,6 +101,32 @@ impl Handle {
}
}

/// Returns the flavor of the current `Runtime`.
///
/// ```
/// use tokio::runtime::{Handle, Flavor};
///
/// #[tokio::main(flavor = "current_thread")]
/// async fn main() {
/// assert_eq!(Flavor::CurrentThread, Handle::current().get_flavor());
/// }
/// ```
///
/// ```
/// use tokio::runtime::{Handle, Flavor};
///
/// #[tokio::main(flavor = "multi_thread", worker_threads = 4)]
/// async fn main() {
/// assert_eq!(Flavor::MultiThread, Handle::current().get_flavor());
/// }
/// ```
pub fn get_flavor(&self) -> Flavor {
match self.inner {
scheduler::Handle::CurrentThread(_) => Flavor::CurrentThread,
scheduler::Handle::MultiThread(_) => Flavor::MultiThread,
}
}

/// Returns a Handle view over the currently running Runtime
///
/// Returns an error if no Runtime has been started
Expand Down
9 changes: 9 additions & 0 deletions tokio/src/runtime/mod.rs
Expand Up @@ -312,6 +312,15 @@ cfg_rt! {
blocking_pool: BlockingPool,
}

/// The flavor of the `Runtime`.
#[derive(Debug, PartialEq)]
pub enum Flavor {
/// The flavor that executes all tasks on the current thread.
CurrentThread,
/// The flavor that executes tasks across multiple threads.
MultiThread,
}

/// The runtime scheduler is either a multi-thread or a current-thread executor.
#[derive(Debug)]
enum Scheduler {
Expand Down

0 comments on commit d07b842

Please sign in to comment.