From f2c63e3646773f5dbf09986f71d8abda86b0a015 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E4=BC=8A=E6=AC=A7?= Date: Fri, 17 Nov 2023 15:47:52 +0800 Subject: [PATCH 1/7] Try to add WASI target support. --- Cargo.toml | 13 +++++++++++-- src/lib.rs | 4 ++-- src/rt_tokio/mod.rs | 10 +++++++++- 3 files changed, 22 insertions(+), 5 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 8d5676f..331b9cf 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -12,14 +12,22 @@ 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" -[target.'cfg(target_arch = "wasm32")'.dependencies] +[target.'cfg(all(target_arch = "wasm32", not(feature = "wasi")))'.dependencies] wasm-bindgen-futures = "0.4" gloo = "0.8" +[target.'cfg(all(target_arch = "wasm32", feature = "wasi"))'.dependencies] +once_cell = "1" +tokio_wasi = { version = "1.25", features = ["rt", "time"] } +tokio-stream_wasi = { version = "0.1", features = ["time"] } + [target.'cfg(not(target_arch = "wasm32"))'.dependencies] num_cpus = "1.13" once_cell = "1" @@ -31,6 +39,7 @@ tokio = { version = "1.19", features = ["full"] } [features] default = [] +wasi = [] [package.metadata.docs.rs] all-features = true diff --git a/src/lib.rs b/src/lib.rs index 1c4fd12..b22a905 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(feature = "wasi")))] #[path = "rt_wasm_bindgen/mod.rs"] mod imp; -#[cfg(not(target_arch = "wasm32"))] +#[cfg(any(not(target_arch = "wasm32"), feature = "wasi"))] #[path = "rt_tokio/mod.rs"] mod imp; diff --git a/src/rt_tokio/mod.rs b/src/rt_tokio/mod.rs index 4b3929a..9d7a594 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(feature = "wasi"))] + { + num_cpus::get() + } + // WASI does not support multi-threading at this moment. + #[cfg(feature = "wasi")] + { + 1 + } } #[inline(always)] From f8493f38a4cd99a6f2bebcd4b48186dd043c3fbf Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E4=BC=8A=E6=AC=A7?= Date: Sat, 18 Nov 2023 00:57:54 +0800 Subject: [PATCH 2/7] Enable tokio_wasi by wasi feature. --- Cargo.toml | 19 +++++++++---------- 1 file changed, 9 insertions(+), 10 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 331b9cf..9641b68 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -19,27 +19,26 @@ futures = { version = "0.3", default-features = false, features = [ pin-project = "1.0.11" pinned = "0.1.0" -[target.'cfg(all(target_arch = "wasm32", not(feature = "wasi")))'.dependencies] -wasm-bindgen-futures = "0.4" -gloo = "0.8" +[target.'cfg(target_arch = "wasm32")'.dependencies] +wasm-bindgen-futures = { version = "0.4" } +gloo = { version = "0.8" } -[target.'cfg(all(target_arch = "wasm32", feature = "wasi"))'.dependencies] -once_cell = "1" -tokio_wasi = { version = "1.25", features = ["rt", "time"] } -tokio-stream_wasi = { version = "0.1", features = ["time"] } +once_cell = { version = "1", optional = true } +tokio_wasi = { version = "1", features = ["rt", "time"], optional = true } +tokio-stream_wasi = { version = "0.1", features = ["time"], optional = true } [target.'cfg(not(target_arch = "wasm32"))'.dependencies] num_cpus = "1.13" once_cell = "1" -tokio = { version = "1.21.1", features = ["rt", "time"] } +tokio = { version = "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 = [] -wasi = [] +wasi = ["dep:once_cell", "dep:tokio_wasi", "dep:tokio-stream_wasi"] [package.metadata.docs.rs] all-features = true From 395c868933dd84543e11af93011a158a37a10606 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E4=BC=8A=E6=AC=A7?= Date: Sun, 19 Nov 2023 21:05:56 +0800 Subject: [PATCH 3/7] Add the impl for wasi environment. --- Cargo.toml | 5 ++- src/lib.rs | 5 ++- src/rt_tokio_wasi/mod.rs | 71 +++++++++++++++++++++++++++++++++++++++ src/rt_tokio_wasi/time.rs | 14 ++++++++ 4 files changed, 91 insertions(+), 4 deletions(-) create mode 100644 src/rt_tokio_wasi/mod.rs create mode 100644 src/rt_tokio_wasi/time.rs diff --git a/Cargo.toml b/Cargo.toml index 9641b68..fdda949 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -18,18 +18,17 @@ futures = { version = "0.3", default-features = false, features = [ ] } pin-project = "1.0.11" pinned = "0.1.0" +once_cell = "1" [target.'cfg(target_arch = "wasm32")'.dependencies] wasm-bindgen-futures = { version = "0.4" } gloo = { version = "0.8" } -once_cell = { version = "1", optional = true } tokio_wasi = { version = "1", features = ["rt", "time"], optional = true } tokio-stream_wasi = { version = "0.1", features = ["time"], optional = true } [target.'cfg(not(target_arch = "wasm32"))'.dependencies] num_cpus = "1.13" -once_cell = "1" tokio = { version = "1", features = ["rt", "time"] } tokio-stream = { version = "0.1", features = ["time"] } @@ -38,7 +37,7 @@ tokio = { version = "1", features = ["full"] } [features] default = [] -wasi = ["dep:once_cell", "dep:tokio_wasi", "dep:tokio-stream_wasi"] +wasi = ["dep:tokio_wasi", "dep:tokio-stream_wasi"] [package.metadata.docs.rs] all-features = true diff --git a/src/lib.rs b/src/lib.rs index b22a905..5435c78 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -59,7 +59,10 @@ pub mod time; #[cfg(all(target_arch = "wasm32", not(feature = "wasi")))] #[path = "rt_wasm_bindgen/mod.rs"] mod imp; -#[cfg(any(not(target_arch = "wasm32"), feature = "wasi"))] +#[cfg(all(target_arch = "wasm32", feature = "wasi"))] +#[path = "rt_tokio_wasi/mod.rs"] +mod imp; +#[cfg(not(target_arch = "wasm32"))] #[path = "rt_tokio/mod.rs"] mod imp; diff --git a/src/rt_tokio_wasi/mod.rs b/src/rt_tokio_wasi/mod.rs new file mode 100644 index 0000000..bb85a13 --- /dev/null +++ b/src/rt_tokio_wasi/mod.rs @@ -0,0 +1,71 @@ +use std::future::Future; +use std::io; +use std::marker::PhantomData; + +use once_cell::sync::Lazy; + +static RUNTIME: Lazy = Lazy::new(|| { + tokio::runtime::Builder::new_current_thread() + .enable_all() + .build() + .unwrap() +}); + +pub(crate) mod time; + +pub(crate) fn get_default_runtime_size() -> usize { + 0 +} + +#[inline(always)] +pub(super) fn spawn_local(f: F) +where + F: Future + 'static, +{ + RUNTIME.block_on(async { f.await }) +} + +#[derive(Debug, Clone, Default)] +pub(crate) struct Runtime {} + +impl Runtime { + pub fn new(_size: usize) -> io::Result { + Ok(Self {}) + } + + pub fn spawn_pinned(&self, create_task: F) + where + F: FnOnce() -> Fut, + F: Send + 'static, + Fut: Future + 'static, + { + RUNTIME.block_on(async { create_task().await }) + } +} + +#[derive(Debug, Clone)] +pub(crate) struct LocalHandle { + // This type is not send or sync. + _marker: PhantomData<*const ()>, +} + +impl LocalHandle { + pub fn try_current() -> Option { + Some(Self { + _marker: PhantomData, + }) + } + + pub fn current() -> Self { + Self { + _marker: PhantomData, + } + } + + pub fn spawn_local(&self, f: F) + where + F: Future + 'static, + { + RUNTIME.block_on(async { f.await }) + } +} diff --git a/src/rt_tokio_wasi/time.rs b/src/rt_tokio_wasi/time.rs new file mode 100644 index 0000000..bcab607 --- /dev/null +++ b/src/rt_tokio_wasi/time.rs @@ -0,0 +1,14 @@ +use std::future::Future; +use std::time::Duration; + +use futures::stream::{Stream, StreamExt}; +use tokio_stream::wrappers::IntervalStream; + +#[inline(always)] +pub(crate) fn sleep(dur: Duration) -> impl Future { + tokio::time::sleep(dur) +} + +pub(crate) fn interval(dur: Duration) -> impl Stream { + IntervalStream::new(tokio::time::interval(dur)).then(|_| async {}) +} From 3a13390ed1af566740a1c5703a2c153214596377 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E4=BC=8A=E6=AC=A7?= Date: Thu, 30 Nov 2023 21:07:47 +0800 Subject: [PATCH 4/7] Use target_os instead of feature. --- Cargo.toml | 10 +++++----- src/lib.rs | 4 ++-- src/rt_tokio/mod.rs | 4 ++-- 3 files changed, 9 insertions(+), 9 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index fdda949..2136fe5 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -20,12 +20,13 @@ pin-project = "1.0.11" pinned = "0.1.0" once_cell = "1" -[target.'cfg(target_arch = "wasm32")'.dependencies] +[target.'cfg(all(target_arch = "wasm32", not(target_os = "wasi")))'.dependencies] wasm-bindgen-futures = { version = "0.4" } -gloo = { version = "0.8" } +gloo = { version = "0.10" } -tokio_wasi = { version = "1", features = ["rt", "time"], optional = true } -tokio-stream_wasi = { version = "0.1", features = ["time"], optional = true } +[target.'cfg(target_os = "wasi")'.dependencies] +tokio_wasi = { version = "1", features = ["rt", "time"] } +tokio-stream_wasi = { version = "0.1", features = ["time"] } [target.'cfg(not(target_arch = "wasm32"))'.dependencies] num_cpus = "1.13" @@ -37,7 +38,6 @@ tokio = { version = "1", features = ["full"] } [features] default = [] -wasi = ["dep:tokio_wasi", "dep:tokio-stream_wasi"] [package.metadata.docs.rs] all-features = true diff --git a/src/lib.rs b/src/lib.rs index 5435c78..a1f153b 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -56,10 +56,10 @@ pub mod fmt; pub mod pinned; pub mod time; -#[cfg(all(target_arch = "wasm32", not(feature = "wasi")))] +#[cfg(all(target_arch = "wasm32", not(target_os = "wasi")))] #[path = "rt_wasm_bindgen/mod.rs"] mod imp; -#[cfg(all(target_arch = "wasm32", feature = "wasi"))] +#[cfg(all(target_arch = "wasm32", target_os = "wasi"))] #[path = "rt_tokio_wasi/mod.rs"] mod imp; #[cfg(not(target_arch = "wasm32"))] diff --git a/src/rt_tokio/mod.rs b/src/rt_tokio/mod.rs index 9d7a594..6bc06d2 100644 --- a/src/rt_tokio/mod.rs +++ b/src/rt_tokio/mod.rs @@ -14,12 +14,12 @@ 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. - #[cfg(not(feature = "wasi"))] + #[cfg(not(target_os = "wasi"))] { num_cpus::get() } // WASI does not support multi-threading at this moment. - #[cfg(feature = "wasi")] + #[cfg(target_os = "wasi")] { 1 } From 97113c2fa40484881b3eb76674273362ebd0ec8e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E4=BC=8A=E6=AC=A7?= Date: Sat, 9 Dec 2023 01:38:22 +0800 Subject: [PATCH 5/7] Remove unused tokio_wasi module, use offical tokio instead. --- Cargo.toml | 2 +- src/lib.rs | 5 +-- src/rt_tokio_wasi/mod.rs | 71 --------------------------------------- src/rt_tokio_wasi/time.rs | 14 -------- 4 files changed, 2 insertions(+), 90 deletions(-) delete mode 100644 src/rt_tokio_wasi/mod.rs delete mode 100644 src/rt_tokio_wasi/time.rs diff --git a/Cargo.toml b/Cargo.toml index 2136fe5..c942cc9 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -22,7 +22,7 @@ once_cell = "1" [target.'cfg(all(target_arch = "wasm32", not(target_os = "wasi")))'.dependencies] wasm-bindgen-futures = { version = "0.4" } -gloo = { version = "0.10" } +gloo = { version = "0.11" } [target.'cfg(target_os = "wasi")'.dependencies] tokio_wasi = { version = "1", features = ["rt", "time"] } diff --git a/src/lib.rs b/src/lib.rs index a1f153b..025cc98 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -59,10 +59,7 @@ pub mod time; #[cfg(all(target_arch = "wasm32", not(target_os = "wasi")))] #[path = "rt_wasm_bindgen/mod.rs"] mod imp; -#[cfg(all(target_arch = "wasm32", target_os = "wasi"))] -#[path = "rt_tokio_wasi/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_wasi/mod.rs b/src/rt_tokio_wasi/mod.rs deleted file mode 100644 index bb85a13..0000000 --- a/src/rt_tokio_wasi/mod.rs +++ /dev/null @@ -1,71 +0,0 @@ -use std::future::Future; -use std::io; -use std::marker::PhantomData; - -use once_cell::sync::Lazy; - -static RUNTIME: Lazy = Lazy::new(|| { - tokio::runtime::Builder::new_current_thread() - .enable_all() - .build() - .unwrap() -}); - -pub(crate) mod time; - -pub(crate) fn get_default_runtime_size() -> usize { - 0 -} - -#[inline(always)] -pub(super) fn spawn_local(f: F) -where - F: Future + 'static, -{ - RUNTIME.block_on(async { f.await }) -} - -#[derive(Debug, Clone, Default)] -pub(crate) struct Runtime {} - -impl Runtime { - pub fn new(_size: usize) -> io::Result { - Ok(Self {}) - } - - pub fn spawn_pinned(&self, create_task: F) - where - F: FnOnce() -> Fut, - F: Send + 'static, - Fut: Future + 'static, - { - RUNTIME.block_on(async { create_task().await }) - } -} - -#[derive(Debug, Clone)] -pub(crate) struct LocalHandle { - // This type is not send or sync. - _marker: PhantomData<*const ()>, -} - -impl LocalHandle { - pub fn try_current() -> Option { - Some(Self { - _marker: PhantomData, - }) - } - - pub fn current() -> Self { - Self { - _marker: PhantomData, - } - } - - pub fn spawn_local(&self, f: F) - where - F: Future + 'static, - { - RUNTIME.block_on(async { f.await }) - } -} diff --git a/src/rt_tokio_wasi/time.rs b/src/rt_tokio_wasi/time.rs deleted file mode 100644 index bcab607..0000000 --- a/src/rt_tokio_wasi/time.rs +++ /dev/null @@ -1,14 +0,0 @@ -use std::future::Future; -use std::time::Duration; - -use futures::stream::{Stream, StreamExt}; -use tokio_stream::wrappers::IntervalStream; - -#[inline(always)] -pub(crate) fn sleep(dur: Duration) -> impl Future { - tokio::time::sleep(dur) -} - -pub(crate) fn interval(dur: Duration) -> impl Stream { - IntervalStream::new(tokio::time::interval(dur)).then(|_| async {}) -} From f6ff913ff9fb9d5a179011e8b53ab1fdb704e273 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E4=BC=8A=E6=AC=A7?= Date: Sun, 10 Dec 2023 14:59:57 +0800 Subject: [PATCH 6/7] Use same original `tokio`. --- Cargo.toml | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index c942cc9..c3917d7 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -24,14 +24,12 @@ once_cell = "1" wasm-bindgen-futures = { version = "0.4" } gloo = { version = "0.11" } -[target.'cfg(target_os = "wasi")'.dependencies] -tokio_wasi = { version = "1", features = ["rt", "time"] } -tokio-stream_wasi = { version = "0.1", features = ["time"] } +[target.'cfg(any(not(target_arch = "wasm32"), target_os = "wasi"))'.dependencies] +tokio = { version = "1", features = ["rt", "time"] } +tokio-stream = { version = "0.1", features = ["time"] } [target.'cfg(not(target_arch = "wasm32"))'.dependencies] num_cpus = "1.13" -tokio = { version = "1", features = ["rt", "time"] } -tokio-stream = { version = "0.1", features = ["time"] } [target.'cfg(not(target_arch = "wasm32"))'.dev-dependencies] tokio = { version = "1", features = ["full"] } From 5ba912c82fcbb0db040c43db38ce824e4fa7c7b8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E4=BC=8A=E6=AC=A7?= Date: Sun, 10 Dec 2023 21:35:18 +0800 Subject: [PATCH 7/7] Pin tokio's version. --- Cargo.toml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index c3917d7..d9177a1 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -25,8 +25,8 @@ wasm-bindgen-futures = { version = "0.4" } gloo = { version = "0.11" } [target.'cfg(any(not(target_arch = "wasm32"), target_os = "wasi"))'.dependencies] -tokio = { version = "1", features = ["rt", "time"] } -tokio-stream = { version = "0.1", features = ["time"] } +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"