mirror of
https://github.com/tokio-rs/tokio.git
synced 2025-10-01 12:20:39 +00:00
rt: add Handle::runtime_flavor
(#5138)
This commit is contained in:
parent
15b362d2dd
commit
fe1843c0e0
@ -1,4 +1,4 @@
|
|||||||
use crate::runtime::scheduler;
|
use crate::runtime::{scheduler, RuntimeFlavor};
|
||||||
|
|
||||||
/// Handle to the runtime.
|
/// Handle to the runtime.
|
||||||
///
|
///
|
||||||
@ -276,6 +276,35 @@ impl Handle {
|
|||||||
let future = crate::util::trace::task(future, "task", _name, id.as_u64());
|
let future = crate::util::trace::task(future, "task", _name, id.as_u64());
|
||||||
self.inner.spawn(future, id)
|
self.inner.spawn(future, id)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Returns the flavor of the current `Runtime`.
|
||||||
|
///
|
||||||
|
/// # Examples
|
||||||
|
///
|
||||||
|
/// ```
|
||||||
|
/// use tokio::runtime::{Handle, RuntimeFlavor};
|
||||||
|
///
|
||||||
|
/// #[tokio::main(flavor = "current_thread")]
|
||||||
|
/// async fn main() {
|
||||||
|
/// assert_eq!(RuntimeFlavor::CurrentThread, Handle::current().runtime_flavor());
|
||||||
|
/// }
|
||||||
|
/// ```
|
||||||
|
///
|
||||||
|
/// ```
|
||||||
|
/// use tokio::runtime::{Handle, RuntimeFlavor};
|
||||||
|
///
|
||||||
|
/// #[tokio::main(flavor = "multi_thread", worker_threads = 4)]
|
||||||
|
/// async fn main() {
|
||||||
|
/// assert_eq!(RuntimeFlavor::MultiThread, Handle::current().runtime_flavor());
|
||||||
|
/// }
|
||||||
|
/// ```
|
||||||
|
pub fn runtime_flavor(&self) -> RuntimeFlavor {
|
||||||
|
match self.inner {
|
||||||
|
scheduler::Handle::CurrentThread(_) => RuntimeFlavor::CurrentThread,
|
||||||
|
#[cfg(all(feature = "rt-multi-thread", not(tokio_wasi)))]
|
||||||
|
scheduler::Handle::MultiThread(_) => RuntimeFlavor::MultiThread,
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
cfg_metrics! {
|
cfg_metrics! {
|
||||||
|
@ -310,6 +310,18 @@ cfg_rt! {
|
|||||||
blocking_pool: BlockingPool,
|
blocking_pool: BlockingPool,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// The flavor of a `Runtime`.
|
||||||
|
///
|
||||||
|
/// This is the return type for [`Handle::runtime_flavor`](crate::runtime::Handle::runtime_flavor()).
|
||||||
|
#[derive(Debug, PartialEq, Eq)]
|
||||||
|
#[non_exhaustive]
|
||||||
|
pub enum RuntimeFlavor {
|
||||||
|
/// 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.
|
/// The runtime scheduler is either a multi-thread or a current-thread executor.
|
||||||
#[derive(Debug)]
|
#[derive(Debug)]
|
||||||
enum Scheduler {
|
enum Scheduler {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user