From dde8cec3e17e971717329238f794d818638f44c5 Mon Sep 17 00:00:00 2001 From: Luke Curley Date: Sat, 27 Dec 2025 22:00:25 +1100 Subject: [PATCH 1/2] Switch to parking_lot and add a `spawn_named` method. --- web-async/Cargo.toml | 1 + web-async/src/lock.rs | 19 +++++++++---------- web-async/src/spawn.rs | 20 ++++++++++++++++++++ 3 files changed, 30 insertions(+), 10 deletions(-) diff --git a/web-async/Cargo.toml b/web-async/Cargo.toml index bdc5e3c..0533b59 100644 --- a/web-async/Cargo.toml +++ b/web-async/Cargo.toml @@ -22,3 +22,4 @@ wasm-bindgen-futures = "0.4" [target.'cfg(not(target_arch = "wasm32"))'.dependencies] tokio = { version = "1", features = ["rt"] } +parking_lot = "0.12" diff --git a/web-async/src/lock.rs b/web-async/src/lock.rs index b864d18..85db989 100644 --- a/web-async/src/lock.rs +++ b/web-async/src/lock.rs @@ -3,9 +3,10 @@ use std::ops::{Deref, DerefMut}; // It's a cosmetic wrapper around Arc> on native platforms. // On WASM, it uses Rc> instead. +// On native, uses parking_lot::Mutex which has better performance and deadlock detection. pub struct Lock { #[cfg(any(not(target_arch = "wasm32"), target_os = "wasi"))] - inner: std::sync::Arc>, + inner: std::sync::Arc>, #[cfg(all(target_arch = "wasm32", not(target_os = "wasi")))] inner: std::rc::Rc>, @@ -15,13 +16,13 @@ pub struct Lock { impl Lock { pub fn new(value: T) -> Self { Self { - inner: std::sync::Arc::new(std::sync::Mutex::new(value)), + inner: std::sync::Arc::new(parking_lot::Mutex::new(value)), } } } impl Lock { - pub fn lock(&self) -> LockGuard { + pub fn lock(&self) -> LockGuard<'_, T> { LockGuard::new(&self.inner) } @@ -55,7 +56,7 @@ impl Clone for Lock { pub struct LockGuard<'a, T> { #[cfg(any(not(target_arch = "wasm32"), target_os = "wasi"))] - inner: std::sync::MutexGuard<'a, T>, + inner: parking_lot::MutexGuard<'a, T>, #[cfg(all(target_arch = "wasm32", not(target_os = "wasi")))] inner: std::cell::RefMut<'a, T>, @@ -63,10 +64,8 @@ pub struct LockGuard<'a, T> { #[cfg(any(not(target_arch = "wasm32"), target_os = "wasi"))] impl<'a, T> LockGuard<'a, T> { - fn new(inner: &'a std::sync::Arc>) -> Self { - Self { - inner: inner.lock().unwrap(), - } + fn new(inner: &'a std::sync::Arc>) -> Self { + Self { inner: inner.lock() } } } @@ -101,7 +100,7 @@ impl fmt::Debug for LockGuard<'_, T> { pub struct LockWeak { #[cfg(any(not(target_arch = "wasm32"), target_os = "wasi"))] - inner: std::sync::Weak>, + inner: std::sync::Weak>, #[cfg(all(target_arch = "wasm32", not(target_os = "wasi")))] inner: std::rc::Weak>, @@ -109,7 +108,7 @@ pub struct LockWeak { #[cfg(any(not(target_arch = "wasm32"), target_os = "wasi"))] impl LockWeak { - fn new(inner: &std::sync::Arc>) -> Self { + fn new(inner: &std::sync::Arc>) -> Self { Self { inner: std::sync::Arc::downgrade(inner), } diff --git a/web-async/src/spawn.rs b/web-async/src/spawn.rs index 5e1db1f..cef6b26 100644 --- a/web-async/src/spawn.rs +++ b/web-async/src/spawn.rs @@ -6,15 +6,35 @@ use std::future::Future; // TODO: use a send feature and make this runtime agnostic? #[cfg(any(not(target_arch = "wasm32"), target_os = "wasi"))] +#[track_caller] pub fn spawn + Send + 'static>(f: F) { #[cfg(feature = "tracing")] let f = tracing::Instrument::in_current_span(f); tokio::task::spawn(f); } +#[cfg(any(not(target_arch = "wasm32"), target_os = "wasi"))] +#[track_caller] +pub fn spawn_named + Send + 'static>(name: &str, f: F) { + #[cfg(feature = "tracing")] + let f = tracing::Instrument::in_current_span(f); + tokio::task::Builder::new().name(name).spawn(f).ok(); +} + #[cfg(all(target_arch = "wasm32", not(target_os = "wasi")))] +#[track_caller] pub fn spawn + 'static>(f: F) { #[cfg(feature = "tracing")] let f = tracing::Instrument::in_current_span(f); wasm_bindgen_futures::spawn_local(f); } + +#[cfg(all(target_arch = "wasm32", not(target_os = "wasi")))] +#[track_caller] +pub fn spawn_named + 'static>(name: &str, f: F) { + // WASM doesn't support task names, just spawn normally + #[cfg(feature = "tracing")] + let f = tracing::Instrument::in_current_span(f); + let _ = name; // Suppress unused warning + wasm_bindgen_futures::spawn_local(f); +} From 48417c6890526698287430889c71f1650dd840a3 Mon Sep 17 00:00:00 2001 From: Luke Curley Date: Sat, 27 Dec 2025 22:03:16 +1100 Subject: [PATCH 2/2] just fix --- web-async/Cargo.toml | 12 +++---- web-codecs/Cargo.toml | 74 +++++++++++++++++++++--------------------- web-streams/Cargo.toml | 10 +++--- 3 files changed, 48 insertions(+), 48 deletions(-) diff --git a/web-async/Cargo.toml b/web-async/Cargo.toml index 0533b59..938074f 100644 --- a/web-async/Cargo.toml +++ b/web-async/Cargo.toml @@ -11,15 +11,15 @@ edition = "2021" keywords = ["wasm", "async", "futures"] categories = ["wasm"] -[dependencies] -tracing = { version = "0.1", optional = true } - [features] tracing = ["dep:tracing"] -[target.'cfg(target_arch = "wasm32")'.dependencies] -wasm-bindgen-futures = "0.4" +[dependencies] +tracing = { version = "0.1", optional = true } [target.'cfg(not(target_arch = "wasm32"))'.dependencies] -tokio = { version = "1", features = ["rt"] } parking_lot = "0.12" +tokio = { version = "1", features = ["rt"] } + +[target.'cfg(target_arch = "wasm32")'.dependencies] +wasm-bindgen-futures = "0.4" diff --git a/web-codecs/Cargo.toml b/web-codecs/Cargo.toml index f24ac77..916fc9d 100644 --- a/web-codecs/Cargo.toml +++ b/web-codecs/Cargo.toml @@ -11,6 +11,10 @@ edition = "2021" categories = ["wasm", "multimedia", "web-programming", "api-bindings"] rust-version = "1.85" +[package.metadata.docs.rs] +rustdoc-args = ["--cfg", "web_sys_unstable_apis"] +rustc-args = ["--cfg", "web_sys_unstable_apis"] + [dependencies] bytemuck = "1.22" bytes = "1" @@ -21,44 +25,40 @@ tokio = { version = "1", features = ["sync", "macros"] } wasm-bindgen = "0.2" wasm-bindgen-futures = "0.4" -[package.metadata.docs.rs] -rustdoc-args = ["--cfg", "web_sys_unstable_apis"] -rustc-args = ["--cfg", "web_sys_unstable_apis"] - [dependencies.web-sys] version = "0.3.77" features = [ - "VideoDecoder", - "VideoDecoderInit", - "VideoDecoderConfig", - "VideoFrame", - "VideoColorSpace", - "VideoColorSpaceInit", - "EncodedVideoChunk", - "EncodedVideoChunkInit", - "EncodedVideoChunkType", - "HardwareAcceleration", - "VideoMatrixCoefficients", - "VideoColorPrimaries", - "VideoTransferCharacteristics", - "VideoEncoder", - "VideoEncoderInit", - "VideoEncoderConfig", - "VideoEncoderEncodeOptions", - "LatencyMode", - "AlphaOption", - "EncodedAudioChunk", - "EncodedAudioChunkInit", - "EncodedAudioChunkType", - "AudioData", - "AudioDecoder", - "AudioDecoderInit", - "AudioDecoderConfig", - "AudioEncoder", - "AudioEncoderInit", - "AudioEncoderConfig", - "AudioSampleFormat", - "AudioDataCopyToOptions", - "AudioDataInit", - "console", + "VideoDecoder", + "VideoDecoderInit", + "VideoDecoderConfig", + "VideoFrame", + "VideoColorSpace", + "VideoColorSpaceInit", + "EncodedVideoChunk", + "EncodedVideoChunkInit", + "EncodedVideoChunkType", + "HardwareAcceleration", + "VideoMatrixCoefficients", + "VideoColorPrimaries", + "VideoTransferCharacteristics", + "VideoEncoder", + "VideoEncoderInit", + "VideoEncoderConfig", + "VideoEncoderEncodeOptions", + "LatencyMode", + "AlphaOption", + "EncodedAudioChunk", + "EncodedAudioChunkInit", + "EncodedAudioChunkType", + "AudioData", + "AudioDecoder", + "AudioDecoderInit", + "AudioDecoderConfig", + "AudioEncoder", + "AudioEncoderInit", + "AudioEncoderConfig", + "AudioSampleFormat", + "AudioDataCopyToOptions", + "AudioDataInit", + "console", ] diff --git a/web-streams/Cargo.toml b/web-streams/Cargo.toml index df90198..05f6328 100644 --- a/web-streams/Cargo.toml +++ b/web-streams/Cargo.toml @@ -18,9 +18,9 @@ wasm-bindgen-futures = "0.4" [dependencies.web-sys] version = "0.3.77" features = [ - "ReadableStream", - "ReadableStreamDefaultReader", - "ReadableStreamReadResult", - "WritableStream", - "WritableStreamDefaultWriter", + "ReadableStream", + "ReadableStreamDefaultReader", + "ReadableStreamReadResult", + "WritableStream", + "WritableStreamDefaultWriter", ]