1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
// SPDX-License-Identifier: MIT OR Apache-2.0
use super::inner::RwLock;
use super::{LOCKED_WRITE, UNLOCKED};
use crate::NotAvailable;
use crate::guard::WriteGuard;
use std::sync::atomic::Ordering::{Acquire, Relaxed};
#[cfg(not(target_arch = "wasm32"))]
use std::thread;
#[cfg(target_arch = "wasm32")]
use wasm_safe_thread as thread;
impl<T> RwLock<T> {
/// Attempts to acquire a write lock without blocking.
///
/// Returns immediately with either a guard to the protected data or
/// a `NotAvailable` error if any readers or another writer currently
/// hold locks.
///
/// Write locks are exclusive - no other readers or writers can be active.
///
/// # Examples
///
/// ```
/// use wasm_safe_mutex::rwlock::RwLock;
/// use wasm_safe_mutex::NotAvailable;
///
/// let rwlock = RwLock::new(vec![1, 2, 3]);
///
/// match rwlock.try_lock_write() {
/// Ok(mut guard) => {
/// guard.push(4);
/// println!("Updated: {:?}", *guard);
/// }
/// Err(NotAvailable) => {
/// println!("Could not acquire write lock");
/// }
/// }
/// ```
///
/// ## Readers Block Writers
///
/// ```
/// use wasm_safe_mutex::rwlock::RwLock;
/// use wasm_safe_mutex::NotAvailable;
///
/// let rwlock = RwLock::new(0);
/// let _reader = rwlock.lock_sync_read();
///
/// // This will fail because a reader holds a lock
/// assert!(matches!(rwlock.try_lock_write(), Err(NotAvailable)));
/// ```
pub fn try_lock_write(&self) -> Result<WriteGuard<'_, T>, NotAvailable> {
match self
.data_lock
.compare_exchange(UNLOCKED, LOCKED_WRITE, Acquire, Relaxed)
{
Ok(_) => Ok(WriteGuard { mutex: self }),
Err(_) => Err(NotAvailable),
}
}
/// Acquires a write lock by spinning until it becomes available.
///
/// This method will continuously check if the lock is available in a tight
/// loop. The lock can only be acquired when no readers or writers are active.
///
/// While spinning ensures the lock is eventually acquired, it consumes CPU
/// cycles while waiting. Use this when blocking is not possible or when
/// you know the lock will be available soon.
///
/// # Examples
///
/// ```
/// use wasm_safe_mutex::rwlock::RwLock;
///
/// let rwlock = RwLock::new(String::from("hello"));
///
/// let mut guard = rwlock.lock_spin_write();
/// guard.push_str(", world!");
/// assert_eq!(&*guard, "hello, world!");
/// ```
pub fn lock_spin_write(&self) -> WriteGuard<'_, T> {
// Spin until we can acquire the lock
loop {
let r = self.try_lock_write();
match r {
Ok(r) => {
return r;
}
Err(_) => {
std::hint::spin_loop();
}
}
}
}
/// Acquires a write lock by blocking the current thread until it becomes available.
///
/// This method will put the thread to sleep if any readers or another writer
/// hold locks, allowing other threads to run. When all locks are released,
/// one waiting writer thread is woken up to acquire exclusive access.
///
/// # Platform Behavior
///
/// - **Native**: Uses thread parking for efficient blocking
/// - **WASM with `Atomics.wait`**: Blocks using `Atomics.wait`
/// - **WASM without `Atomics.wait`**: **Will panic** - use [`lock_sync_write`](Self::lock_sync_write) instead
///
/// This is a low-level primitive that unconditionally blocks. For adaptive
/// behavior that works everywhere (including browser main threads where
/// `Atomics.wait` is unavailable), use [`lock_sync_write`](Self::lock_sync_write).
///
/// # Examples
///
/// ```
/// # // std::thread::spawn panics on wasm32
/// # if cfg!(target_arch = "wasm32") { return; }
/// use wasm_safe_mutex::rwlock::RwLock;
/// use std::sync::Arc;
/// # use std::thread;
///
/// let rwlock = Arc::new(RwLock::new(0));
/// let rwlock_clone = Arc::clone(&rwlock);
///
/// thread::spawn(move || {
/// let mut guard = rwlock_clone.lock_block_write();
/// *guard = 42;
/// });
///
/// # thread::sleep(std::time::Duration::from_millis(10));
/// let guard = rwlock.lock_block_read();
/// assert_eq!(*guard, 42);
/// ```
pub fn lock_block_write(&self) -> WriteGuard<'_, T> {
loop {
let r =
self.waiting_sync_write_threads
.with_mut(|threads| match self.try_lock_write() {
Ok(guard) => Ok(guard),
Err(_) => {
let handle = thread::current();
threads.push(handle);
Err(NotAvailable)
}
});
match r {
Ok(guard) => return guard,
Err(NotAvailable) => thread::park(),
}
}
}
/// Asynchronously acquires a write lock.
///
/// This method returns a future that resolves to a write guard when the lock
/// becomes available. Unlike the blocking variants, this doesn't block
/// the async executor, allowing other tasks to run while waiting.
///
/// The write lock is exclusive - it waits until all readers and any other
/// writer release their locks.
///
/// # Examples
///
/// ```
/// # test_executors::spin_on(async {
/// use wasm_safe_mutex::rwlock::RwLock;
/// use std::collections::HashMap;
///
/// let rwlock = RwLock::new(HashMap::new());
///
/// let mut guard = rwlock.lock_async_write().await;
/// guard.insert("key", "value");
/// drop(guard);
///
/// let guard = rwlock.lock_async_read().await;
/// assert_eq!(guard.get("key"), Some(&"value"));
/// # });
/// ```
pub async fn lock_async_write(&self) -> WriteGuard<'_, T> {
loop {
let a = self.waiting_async_write_threads.with_mut(|senders| {
match self.try_lock_write() {
Ok(guard) => Ok(guard),
Err(NotAvailable) => {
// Create a new channel to signal when the lock is available
let (sender, receiver) = r#continue::continuation();
senders.push(sender);
Err(receiver)
}
}
});
match a {
Ok(guard) => return guard,
Err(receiver) => {
// Wait for the signal that the lock is available
receiver.await;
}
}
}
}
/// Automatically chooses the right write locking strategy for your platform.
///
/// This is the recommended method for acquiring write locks as it papers over
/// all platform differences:
/// - **Native**: Uses efficient thread parking
/// - **WASM with `Atomics.wait`**: Uses `Atomics.wait` for proper blocking
/// - **WASM without `Atomics.wait`**: Falls back to spinning (e.g., browser main thread)
///
/// The write lock provides exclusive access - no readers or other writers
/// can access the data while this lock is held.
///
/// # Examples
///
/// ```
/// use wasm_safe_mutex::rwlock::RwLock;
///
/// let rwlock = RwLock::new(vec![1, 2, 3]);
///
/// // Automatically uses the best strategy for the platform
/// let mut guard = rwlock.lock_sync_write();
/// guard.push(4);
/// assert_eq!(guard.len(), 4);
/// ```
///
/// ## Cross-Platform Modifications
///
/// ```
/// use wasm_safe_mutex::rwlock::RwLock;
/// use std::collections::HashMap;
///
/// fn update_config(rwlock: &RwLock<HashMap<String, i32>>) {
/// // Works on both native and WASM without changes
/// let mut guard = rwlock.lock_sync_write();
/// guard.insert("version".to_string(), 2);
/// }
///
/// let rwlock = RwLock::new(HashMap::new());
/// update_config(&rwlock);
///
/// let guard = rwlock.lock_sync_read();
/// assert_eq!(guard.get("version"), Some(&2));
/// ```
pub fn lock_sync_write(&self) -> WriteGuard<'_, T> {
#[cfg(not(target_arch = "wasm32"))]
{
self.lock_block_write()
}
#[cfg(target_arch = "wasm32")]
{
if crate::wasm_support::atomics_wait_supported() {
self.lock_block_write()
} else {
// Fallback to spin lock if Atomics.wait is not supported
self.lock_spin_write()
}
}
}
/// Accesses the data inside the RwLock synchronously with a mutable closure.
///
/// This method acquires a write lock, executes the provided closure with a mutable
/// reference to the protected data, and immediately releases the lock. This ensures
/// exclusive access during modifications.
///
/// # Examples
///
/// ```
/// use wasm_safe_mutex::rwlock::RwLock;
///
/// let rwlock = RwLock::new(vec![1, 2, 3]);
///
/// // Modify and return a value
/// let new_len = rwlock.with_mut_sync(|data| {
/// data.push(4);
/// data.len()
/// });
/// assert_eq!(new_len, 4);
/// ```
pub fn with_mut_sync<R, F: FnOnce(&mut T) -> R>(&self, f: F) -> R {
let mut guard = self.lock_sync_write();
f(&mut guard)
}
/// Accesses the data inside the RwLock asynchronously with a mutable closure.
///
/// This method asynchronously acquires a write lock, executes the provided closure
/// with a mutable reference to the protected data, and immediately releases the lock.
/// This ensures exclusive access for modifications without blocking the async executor.
///
/// # Examples
///
/// ```
/// # test_executors::spin_on(async {
/// use wasm_safe_mutex::rwlock::RwLock;
/// use std::collections::HashMap;
///
/// let rwlock = RwLock::new(HashMap::new());
///
/// // Add multiple entries in one async operation
/// rwlock.with_mut_async(|map| {
/// map.insert("async", 1);
/// map.insert("await", 2);
/// }).await;
///
/// let sum = rwlock.with_async(|map| {
/// map.values().sum::<i32>()
/// }).await;
/// assert_eq!(sum, 3);
/// # });
/// ```
pub async fn with_mut_async<R, F: FnOnce(&mut T) -> R>(&self, f: F) -> R {
let mut guard = self.lock_async_write().await;
f(&mut guard)
}
}