From 83d97d4b2e05430442989b7bc26c980ca7e2855b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E4=BC=8A=E6=AC=A7?= Date: Mon, 11 Dec 2023 20:36:10 +0800 Subject: [PATCH] Add WASI support by tokio_wasi (#11) * Try to add WASI target support. * Enable tokio_wasi by wasi feature. * Add the impl for wasi environment. * Use target_os instead of feature. * Remove unused tokio_wasi module, use offical tokio instead. * Use same original `tokio`. * Pin tokio's version. --- Cargo.toml | 21 +++++++++++++-------- src/lib.rs | 4 ++-- src/rt_tokio/mod.rs | 10 +++++++++- 3 files changed, 24 insertions(+), 11 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 8d5676f..d9177a1 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -12,22 +12,27 @@ repository = "https://github.com/futursolo/prokio" rust-version = "1.60.0" [dependencies] -futures = { version = "0.3", default-features = false, features = ["std", "async-await"] } +futures = { version = "0.3", default-features = false, features = [ + "std", + "async-await", +] } pin-project = "1.0.11" pinned = "0.1.0" +once_cell = "1" + +[target.'cfg(all(target_arch = "wasm32", not(target_os = "wasi")))'.dependencies] +wasm-bindgen-futures = { version = "0.4" } +gloo = { version = "0.11" } -[target.'cfg(target_arch = "wasm32")'.dependencies] -wasm-bindgen-futures = "0.4" -gloo = "0.8" +[target.'cfg(any(not(target_arch = "wasm32"), target_os = "wasi"))'.dependencies] +tokio = { version = "1.35", features = ["rt", "time"] } +tokio-stream = { version = "0.1.14", features = ["time"] } [target.'cfg(not(target_arch = "wasm32"))'.dependencies] num_cpus = "1.13" -once_cell = "1" -tokio = { version = "1.21.1", features = ["rt", "time"] } -tokio-stream = { version = "0.1", features = ["time"] } [target.'cfg(not(target_arch = "wasm32"))'.dev-dependencies] -tokio = { version = "1.19", features = ["full"] } +tokio = { version = "1", features = ["full"] } [features] default = [] diff --git a/src/lib.rs b/src/lib.rs index 1c4fd12..025cc98 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -56,10 +56,10 @@ pub mod fmt; pub mod pinned; pub mod time; -#[cfg(target_arch = "wasm32")] +#[cfg(all(target_arch = "wasm32", not(target_os = "wasi")))] #[path = "rt_wasm_bindgen/mod.rs"] mod imp; -#[cfg(not(target_arch = "wasm32"))] +#[cfg(any(not(target_arch = "wasm32"), target_os = "wasi"))] #[path = "rt_tokio/mod.rs"] mod imp; diff --git a/src/rt_tokio/mod.rs b/src/rt_tokio/mod.rs index 4b3929a..6bc06d2 100644 --- a/src/rt_tokio/mod.rs +++ b/src/rt_tokio/mod.rs @@ -14,7 +14,15 @@ use local_worker::LocalWorker; pub(crate) fn get_default_runtime_size() -> usize { // We use num_cpus as std::thread::available_parallelism() does not take // system resource constraint (e.g.: cgroups) into consideration. - num_cpus::get() + #[cfg(not(target_os = "wasi"))] + { + num_cpus::get() + } + // WASI does not support multi-threading at this moment. + #[cfg(target_os = "wasi")] + { + 1 + } } #[inline(always)]