Skip to content

Commit

Permalink
Add threading on the Web
Browse files Browse the repository at this point in the history
Use the `wasm_thread` library to enable real `spawn_blocking` on the
Web in `linera_base::task`.
  • Loading branch information
Twey committed Sep 19, 2024
1 parent c84b75c commit cd11041
Show file tree
Hide file tree
Showing 8 changed files with 27 additions and 10 deletions.
13 changes: 13 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,7 @@ wasm-bindgen-futures = "0.4.42"
wasm-bindgen-test = "0.3.42"
wasm-encoder = "0.24.1"
wasm-instrument = "0.4.0"
wasm_thread = "0.3.0"
wasmer = { package = "linera-wasmer", version = "4.3.6-linera.3", default-features = false }
wasmer-compiler-singlepass = { package = "linera-wasmer-compiler-singlepass", version = "4.3.6-linera.3" }
wasmparser = "0.101.1"
Expand Down
2 changes: 2 additions & 0 deletions linera-base/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ web = [
"tracing-web",
"wasmtimer",
"wasm-bindgen-futures",
"wasm_thread",
"web-time",
]

Expand Down Expand Up @@ -57,6 +58,7 @@ tokio = { workspace = true, features = ["time"] }
tracing.workspace = true
tracing-subscriber = { workspace = true, features = ["json", "fmt", "ansi"] }
wasm-bindgen-futures = { workspace = true, optional = true }
wasm_thread = { workspace = true, optional = true }
wasmtimer = { workspace = true, optional = true }
web-time = { workspace = true, optional = true }

Expand Down
6 changes: 5 additions & 1 deletion linera-base/src/task.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,11 @@ mod implementation {
pub fn spawn_blocking<R: Send + 'static, F: FnOnce() -> R + Send + 'static>(
task: F,
) -> BlockingFuture<R> {
spawn(async { task() })
let (send, recv) = oneshot::channel();
wasm_thread::spawn(move || {
let _ = send.send(task());
});
recv
}
}

Expand Down
2 changes: 1 addition & 1 deletion linera-execution/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ wasmer = { workspace = true, optional = true, features = ["sys-default", "single

[target.'cfg(target_arch = "wasm32")'.dependencies]
tokio = { workspace = true, features = ["rt"] }
wasmer = { workspace = true, optional = true, features = ["js-default"] }
wasmer = { workspace = true, optional = true, features = ["js-default", "js-serializable-module"] }

[dev-dependencies]
anyhow.workspace = true
Expand Down
4 changes: 0 additions & 4 deletions linera-storage/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,6 @@ version.workspace = true

[features]
test = [
"tokio/rt",
"tokio/test-util",
"tokio/time",
"linera-execution/test",
"linera-views/test",
]
Expand Down Expand Up @@ -45,7 +42,6 @@ linera-execution.workspace = true
linera-views.workspace = true
prometheus.workspace = true
serde.workspace = true
tokio = { workspace = true, features = ["macros"] }

[dev-dependencies]
anyhow.workspace = true
Expand Down
2 changes: 1 addition & 1 deletion linera-storage/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ fn main() {
cfg_aliases::cfg_aliases! {
with_testing: { any(test, feature = "test") },
with_metrics: { all(not(target_arch = "wasm32"), feature = "metrics") },
with_wasmer: { all(not(target_arch = "wasm32"), feature = "wasmer") },
with_wasmer: { all(any(feature = "web", not(target_arch = "wasm32")), feature = "wasmer") },
with_wasmtime: { all(not(target_arch = "wasm32"), feature = "wasmtime") },
with_wasm_runtime: { any(with_wasmer, with_wasmtime) },
};
Expand Down
7 changes: 4 additions & 3 deletions linera-storage/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,7 @@ pub trait Storage: Sized {
let mut tasks = Vec::new();
for key in keys {
let client = self.clone();
tasks.push(tokio::task::spawn(async move {
tasks.push(linera_base::task::spawn(async move {
client.read_certificate(key).await
}));
}
Expand Down Expand Up @@ -295,7 +295,7 @@ pub trait Storage: Sized {
.into_inner_contract_bytecode()
.expect("Contract Bytecode Blob is of the wrong Blob type!");
let contract_bytecode =
tokio::task::spawn_blocking(move || compressed_contract_bytecode.decompress())
linera_base::task::spawn_blocking(move || compressed_contract_bytecode.decompress())
.await??;
Ok(Arc::new(
WasmContractModule::new(contract_bytecode, wasm_runtime).await?,
Expand Down Expand Up @@ -333,7 +333,8 @@ pub trait Storage: Sized {
.into_inner_service_bytecode()
.expect("Service Bytecode Blob is of the wrong Blob type!");
let service_bytecode =
tokio::task::spawn_blocking(move || compressed_service_bytecode.decompress()).await??;
linera_base::task::spawn_blocking(move || compressed_service_bytecode.decompress())
.await??;
Ok(Arc::new(
WasmServiceModule::new(service_bytecode, wasm_runtime).await?,
))
Expand Down

0 comments on commit cd11041

Please sign in to comment.