2 releases

0.1.2 Jan 19, 2024
0.1.1 Jan 19, 2024
0.1.0 Jul 18, 2023

#469 in Concurrency

Download history 1675/week @ 2024-07-23 1788/week @ 2024-07-30 1351/week @ 2024-08-06 841/week @ 2024-08-13 984/week @ 2024-08-20 1070/week @ 2024-08-27 1131/week @ 2024-09-03 1362/week @ 2024-09-10 1444/week @ 2024-09-17 2120/week @ 2024-09-24 1594/week @ 2024-10-01 2174/week @ 2024-10-08 1698/week @ 2024-10-15 1509/week @ 2024-10-22 1332/week @ 2024-10-29 1297/week @ 2024-11-05

6,226 downloads per month
Used in 18 crates (7 directly)

MIT/Apache

61KB
575 lines

wasm_sync

Crates.io Docs.rs

wasm_sync offers synchronization primitives that work in both browser and native contexts.

In web browsers, use of atomic wait instructions on the main thread causes an error. This prevents the use of standard library synchronization primitives within web contexts. wasm_sync solves this problem by busy-spinning on the main thread. Other threads, like dedicated web workers, still use atomic wait instructions.

On native platforms, wasm_sync simply re-exports the standard library's synchronization primitives.

Supported primitives

  • wasm_sync::Condvar
  • wasm_sync::Mutex
  • wasm_sync::RwLock
  • wasm_sync::Once
  • wasm_sync::OnceLock

Usage

Instead of importing a standard library primitive, import the wasm_sync variant. For example:

use std::sync::Arc;
use std::thread;
use wasm_sync::Mutex;

let mutex = Arc::new(Mutex::new(0));
let c_mutex = Arc::clone(&mutex);

thread::spawn(move || {
    *c_mutex.lock().unwrap() = 10;
}).join().expect("thread::spawn failed");
assert_eq!(*mutex.lock().unwrap(), 10);

Dependencies

~0–2.3MB
~43K SLoC