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
// SPDX-License-Identifier: MIT OR Apache-2.0
//! A WebAssembly-safe condition variable implementation.
//!
//! This module provides a condition variable that works across native and WebAssembly targets,
//! automatically adapting its waiting strategy based on the runtime environment.
use crateSpinlock;
use Arc;
use ;
use Instant;
use Instant;
use thread;
use wasm_safe_thread as thread;
// Re-export for internal use within condvar submodules
pub use crateatomics_wait_supported;
static ASYNC_WAITER_ID_COUNTER: AtomicU64 = new;
/// A condition variable that works across native and WebAssembly targets.
///
/// A condition variable allows threads to wait for a particular condition to become true,
/// and to notify other threads when the condition changes. This implementation automatically
/// adapts to the platform:
///
/// - **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)
///
/// # Examples
///
/// ## Basic Usage
///
/// ```
/// # // std::thread::spawn panics on wasm32
/// # if cfg!(target_arch = "wasm32") { return; }
/// use wasm_safe_mutex::{Mutex, condvar::Condvar};
/// use std::sync::Arc;
/// # use std::thread;
///
/// let pair = Arc::new((Mutex::new(false), Condvar::new()));
/// let pair_clone = Arc::clone(&pair);
///
/// thread::spawn(move || {
/// let (mutex, condvar) = &*pair_clone;
/// let mut ready = mutex.lock_sync();
/// *ready = true;
/// drop(ready);
/// condvar.notify_one();
/// });
///
/// let (mutex, condvar) = &*pair;
/// let mut ready = mutex.lock_sync();
/// while !*ready {
/// ready = condvar.wait_sync(ready);
/// }
/// assert!(*ready);
/// ```
///
/// ## Producer-Consumer Pattern
///
/// ```
/// # // std::thread::spawn panics on wasm32
/// # if cfg!(target_arch = "wasm32") { return; }
/// use wasm_safe_mutex::{Mutex, condvar::Condvar};
/// use std::sync::Arc;
/// use std::collections::VecDeque;
/// # use std::thread;
///
/// let shared = Arc::new((Mutex::new(VecDeque::new()), Condvar::new()));
/// let producer = Arc::clone(&shared);
///
/// // Producer thread
/// thread::spawn(move || {
/// let (mutex, condvar) = &*producer;
/// for i in 0..5 {
/// let mut queue = mutex.lock_sync();
/// queue.push_back(i);
/// drop(queue);
/// condvar.notify_one();
/// }
/// });
///
/// // Consumer
/// let (mutex, condvar) = &*shared;
/// let mut collected = Vec::new();
/// for _ in 0..5 {
/// let mut queue = mutex.lock_sync();
/// while queue.is_empty() {
/// queue = condvar.wait_sync(queue);
/// }
/// if let Some(value) = queue.pop_front() {
/// collected.push(value);
/// }
/// }
/// assert_eq!(collected, vec![0, 1, 2, 3, 4]);
/// ```
/// A type indicating whether a timed wait on a condition variable returned
/// due to a time out or not.
;
unsafe
unsafe