// Copyright 2018-2024 the Deno authors. All rights reserved. MIT license.
use std::fmt::Debug;
use std::str::FromStr;
use deno_core::unsync::MaskFutureAsSend;
#[cfg(tokio_unstable)]
use tokio_metrics::RuntimeMonitor;
/// Default configuration for tokio. In the future, this method may have different defaults
/// depending on the platform and/or CPU layout.
const fn tokio_configuration() -> (u32, u32, usize) {
(61, 31, 1024)
}
fn tokio_env(name: &'static str, default: T) -> T
where
::Err: Debug,
{
match std::env::var(name) {
Ok(value) => value.parse().unwrap(),
Err(_) => default,
}
}
pub fn create_basic_runtime() -> tokio::runtime::Runtime {
let (event_interval, global_queue_interval, max_io_events_per_tick) =
tokio_configuration();
tokio::runtime::Builder::new_current_thread()
.enable_io()
.enable_time()
.event_interval(tokio_env("DENO_TOKIO_EVENT_INTERVAL", event_interval))
.global_queue_interval(tokio_env(
"DENO_TOKIO_GLOBAL_QUEUE_INTERVAL",
global_queue_interval,
))
.max_io_events_per_tick(tokio_env(
"DENO_TOKIO_MAX_IO_EVENTS_PER_TICK",
max_io_events_per_tick,
))
// This limits the number of threads for blocking operations (like for
// synchronous fs ops) or CPU bound tasks like when we run dprint in
// parallel for deno fmt.
// The default value is 512, which is an unhelpfully large thread pool. We
// don't ever want to have more than a couple dozen threads.
.max_blocking_threads(if cfg!(windows) {
// on windows, tokio uses blocking tasks for child process IO, make sure
// we have enough available threads for other tasks to run
4 * std::thread::available_parallelism()
.map(|n| n.get())
.unwrap_or(8)
} else {
32
})
.build()
.unwrap()
}
#[inline(always)]
fn create_and_run_current_thread_inner(
future: F,
metrics_enabled: bool,
) -> R
where
F: std::future::Future