2023-01-02 16:00:42 -05:00
|
|
|
// Copyright 2018-2023 the Deno authors. All rights reserved. MIT license.
|
2020-12-13 13:45:53 -05:00
|
|
|
|
2023-05-14 17:40:01 -04:00
|
|
|
use deno_core::task::MaskFutureAsSend;
|
|
|
|
|
2020-12-13 13:45:53 -05:00
|
|
|
pub fn create_basic_runtime() -> tokio::runtime::Runtime {
|
2021-01-12 02:50:02 -05:00
|
|
|
tokio::runtime::Builder::new_current_thread()
|
2020-12-13 13:45:53 -05:00
|
|
|
.enable_io()
|
|
|
|
.enable_time()
|
|
|
|
// 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.
|
2021-01-12 02:50:02 -05:00
|
|
|
.max_blocking_threads(32)
|
2020-12-13 13:45:53 -05:00
|
|
|
.build()
|
|
|
|
.unwrap()
|
|
|
|
}
|
2021-10-21 07:05:43 -04:00
|
|
|
|
2023-05-14 17:40:01 -04:00
|
|
|
pub fn create_and_run_current_thread<F, R>(future: F) -> R
|
2021-10-21 07:05:43 -04:00
|
|
|
where
|
2023-05-14 17:40:01 -04:00
|
|
|
F: std::future::Future<Output = R> + 'static,
|
|
|
|
R: Send + 'static,
|
2021-10-21 07:05:43 -04:00
|
|
|
{
|
|
|
|
let rt = create_basic_runtime();
|
2023-05-14 17:40:01 -04:00
|
|
|
// SAFETY: this this is guaranteed to be running on a current-thread executor
|
|
|
|
let future = unsafe { MaskFutureAsSend::new(future) };
|
|
|
|
let join_handle = rt.spawn(future);
|
|
|
|
rt.block_on(join_handle).unwrap().into_inner()
|
2021-10-21 07:05:43 -04:00
|
|
|
}
|