wasm_sync

Crates.iowasm_sync
lib.rswasm_sync
version0.1.2
sourcesrc
created_at2023-07-18 21:44:09.685527
updated_at2024-01-19 23:07:57.820218
descriptionSynchronization primitives for both web and native.
homepage
repositoryhttps://github.com/DouglasDwyer/wasm_sync
max_upload_size
id919776
size60,918
Douglas Dwyer (DouglasDwyer)

documentation

README

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);
Commit count: 2

cargo fmt