diff --git a/crates/core/Cargo.toml b/crates/core/Cargo.toml index 2eafc004a..e4aefc016 100644 --- a/crates/core/Cargo.toml +++ b/crates/core/Cargo.toml @@ -83,7 +83,7 @@ serde_json = { workspace = true } serde_yaml = "0.9.22" svgr-rs = "0.1.3" thiserror = "1.0.43" -tokio = { version = "1", features = ["rt", "sync"] } +tokio = { version = "1", features = ["rt-multi-thread", "sync"] } tokio-tungstenite = "0.19.0" toml = "0.7.6" tracing = "0.1.37" diff --git a/crates/mako/src/dev.rs b/crates/mako/src/dev.rs index 881505753..080030499 100644 --- a/crates/mako/src/dev.rs +++ b/crates/mako/src/dev.rs @@ -12,10 +12,11 @@ use mako_core::notify_debouncer_full::new_debouncer; use mako_core::tokio::sync::broadcast; use mako_core::tracing::debug; use mako_core::tungstenite::Message; -use mako_core::{hyper, hyper_staticfile, hyper_tungstenite, tokio}; +use mako_core::{hyper, hyper_staticfile, hyper_tungstenite}; use crate::compiler::{Compiler, Context}; use crate::plugin::{PluginGenerateEndParams, PluginGenerateStats}; +use crate::tokio_runtime; use crate::watch::Watcher; pub struct DevServer { @@ -93,7 +94,7 @@ impl DevServer { debug!("new websocket connection"); let (response, websocket) = hyper_tungstenite::upgrade(req, None).unwrap(); let txws = txws.clone(); - tokio::spawn(async move { + tokio_runtime::spawn(async move { let receiver = txws.subscribe(); Self::handle_websocket(websocket, receiver).await.unwrap(); }); @@ -138,7 +139,7 @@ impl DevServer { ) -> Result<()> { let websocket = websocket.await?; let (mut sender, mut ws_recv) = websocket.split(); - let task = tokio::spawn(async move { + let task = tokio_runtime::spawn(async move { loop { if let Ok(msg) = receiver.recv().await { if sender diff --git a/crates/mako/src/lib.rs b/crates/mako/src/lib.rs index c5ecea0bc..3381f1ade 100644 --- a/crates/mako/src/lib.rs +++ b/crates/mako/src/lib.rs @@ -38,6 +38,7 @@ mod targets; #[cfg(test)] mod test_helper; mod thread_pool; +mod tokio_runtime; mod transform; mod transform_in_generate; mod transformers; diff --git a/crates/mako/src/main.rs b/crates/mako/src/main.rs index b600e5985..04bbe0da8 100644 --- a/crates/mako/src/main.rs +++ b/crates/mako/src/main.rs @@ -6,7 +6,6 @@ use std::sync::Arc; use mako_core::anyhow::{anyhow, Result}; use mako_core::clap::Parser; -use mako_core::tokio; #[cfg(feature = "profile")] use mako_core::tokio::sync::Notify; use mako_core::tracing::debug; @@ -52,6 +51,7 @@ mod targets; #[cfg(test)] mod test_helper; mod thread_pool; +mod tokio_runtime; mod transform; mod transform_in_generate; mod transformers; @@ -76,10 +76,7 @@ static GLOBAL: tikv_jemallocator::Jemalloc = tikv_jemallocator::Jemalloc; fn main() -> Result<()> { let fut = async { run().await }; - tokio::runtime::Builder::new_current_thread() - .build() - .expect("Failed to create tokio runtime.") - .block_on(fut) + tokio_runtime::block_on(fut) } async fn run() -> Result<()> { @@ -128,7 +125,7 @@ async fn run() -> Result<()> { let notify = Arc::new(Notify::new()); let to_be_notify = notify.clone(); - tokio::spawn(async move { + tokio_runtime::spawn(async move { let compiler = compiler.clone(); to_be_notify.notified().await; diff --git a/crates/mako/src/plugins/copy.rs b/crates/mako/src/plugins/copy.rs index 8cae626d0..6a9a5253f 100644 --- a/crates/mako/src/plugins/copy.rs +++ b/crates/mako/src/plugins/copy.rs @@ -2,23 +2,24 @@ use std::path::Path; use std::sync::Arc; use mako_core::anyhow::Result; +use mako_core::fs_extra; use mako_core::glob::glob; use mako_core::notify::event::{CreateKind, DataChange, ModifyKind, RenameMode}; use mako_core::notify::{EventKind, RecommendedWatcher, RecursiveMode, Watcher}; use mako_core::tokio::sync::mpsc::channel; use mako_core::tracing::debug; -use mako_core::{fs_extra, tokio}; use crate::compiler::Context; use crate::plugin::Plugin; use crate::stats::StatsJsonMap; +use crate::tokio_runtime; pub struct CopyPlugin {} impl CopyPlugin { fn watch(context: &Arc) { let context = context.clone(); - tokio::spawn(async move { + tokio_runtime::spawn(async move { let (tx, mut rx) = channel(2); let mut watcher = RecommendedWatcher::new( move |res| { diff --git a/crates/mako/src/tokio_runtime.rs b/crates/mako/src/tokio_runtime.rs new file mode 100644 index 000000000..9426c6482 --- /dev/null +++ b/crates/mako/src/tokio_runtime.rs @@ -0,0 +1,26 @@ +use std::future::Future; + +use mako_core::lazy_static::lazy_static; +use mako_core::tokio; + +lazy_static! { + static ref TOKIO_RUNTIME: tokio::runtime::Runtime = tokio::runtime::Builder::new_multi_thread() + .enable_io() + .worker_threads(2) + .thread_name("tokio-worker") + .build() + .expect("failed to create tokio runtime."); +} + +pub fn spawn(future: F) -> tokio::task::JoinHandle +where + F: Future + Send + 'static, + F::Output: Send + 'static, +{ + TOKIO_RUNTIME.spawn(future) +} + +#[allow(dead_code)] +pub fn block_on(future: F) -> F::Output { + TOKIO_RUNTIME.block_on(future) +}