wasm_sync/lib.rs
1#![deny(warnings)]
2#![warn(missing_docs)]
3#![warn(clippy::missing_docs_in_private_items)]
4
5//! `wasm_sync` offers synchronization primitives that work in both browser and native contexts.
6//!
7//! In web browsers, use of atomic wait instructions on the main thread [causes an error](https://github.com/WebAssembly/threads/issues/106). 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.
8//!
9//! On native platforms, `wasm_sync` simply re-exports the standard library's synchronization primitives.
10//!
11//! ## Supported primitives
12//!
13//! - [`wasm_sync::Condvar`](crate::Condvar)
14//! - [`wasm_sync::Mutex`](crate::Mutex)
15//! - [`wasm_sync::RwLock`](crate::RwLock)
16//! - [`wasm_sync::Once`](crate::Once)
17//! - [`wasm_sync::OnceLock`](crate::OnceLock)
18//!
19//! ## Usage
20//!
21//! Instead of importing a standard library primitive, import the `wasm_sync` variant. For example:
22//!
23//! ```rust
24//! use std::sync::Arc;
25//! use std::thread;
26//! use wasm_sync::Mutex;
27//!
28//! let mutex = Arc::new(Mutex::new(0));
29//! let c_mutex = Arc::clone(&mutex);
30//!
31//! thread::spawn(move || {
32//! *c_mutex.lock().unwrap() = 10;
33//! }).join().expect("thread::spawn failed");
34//! assert_eq!(*mutex.lock().unwrap(), 10);
35//! ```
36//!
37//!
38
39/// Provides the ability to generate and compare type IDs in a `const` context.
40#[cfg_attr(all(target_arch = "wasm32", target_feature = "atomics"), path = "wasm.rs")]
41#[cfg_attr(not(all(target_arch = "wasm32", target_feature = "atomics")), path = "native.rs")]
42mod backend;
43
44pub use crate::backend::*;
45pub use crate::backend::Condvar;
46pub use crate::backend::Mutex;
47pub use crate::backend::RwLock;