Skip to content

Commit b1210da

Browse files
authored
Move emscripten_futex declaration into threading_primitives.h. NFC (#26436)
Also, document that the low level wait function don't work everywhere. This is step towards #26375.
1 parent 7279a01 commit b1210da

3 files changed

Lines changed: 22 additions & 15 deletions

File tree

system/include/emscripten/atomic.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -197,6 +197,10 @@ _EM_INLINE void emscripten_atomic_fence(void) {
197197
// NOTE: This function takes in the wait value in int64_t nanosecond units. Pass
198198
// in maxWaitNanoseconds = -1 (or ATOMICS_WAIT_DURATION_INFINITE) to wait
199199
// infinitely long.
200+
// NOTE: This function is thin wrapper around the Wasm atomic.wait instruction
201+
// and therefore cannot be used on the main browser thread, or in Audio Worklets.
202+
// If you need a wait primitive that works everywhere you can use
203+
// `emscripten_futex_wait`.
200204
_EM_INLINE ATOMICS_WAIT_RESULT_T emscripten_atomic_wait_u32(void /*uint32_t*/* _Nonnull addr, uint32_t expectedValue, int64_t maxWaitNanoseconds) {
201205
return __builtin_wasm_memory_atomic_wait32((int32_t*)addr, expectedValue, maxWaitNanoseconds);
202206
}
@@ -208,6 +212,10 @@ _EM_INLINE ATOMICS_WAIT_RESULT_T emscripten_atomic_wait_u32(void /*uint32_t*/* _
208212
// NOTE: This function takes in the wait value in int64_t nanosecond units. Pass
209213
// in maxWaitNanoseconds = -1 (or ATOMICS_WAIT_DURATION_INFINITE) to wait
210214
// infinitely long.
215+
// NOTE: This function is thin wrapper around the Wasm atomic.wait instruction
216+
// and therefore cannot be used on the main browser thread, or in Audio Worklets.
217+
// If you need a wait primitive that works everywhere you can use
218+
// `emscripten_futex_wait`.
211219
_EM_INLINE ATOMICS_WAIT_RESULT_T emscripten_atomic_wait_u64(void /*uint64_t*/* _Nonnull addr, uint64_t expectedValue, int64_t maxWaitNanoseconds) {
212220
return __builtin_wasm_memory_atomic_wait64((int64_t*)addr, expectedValue, maxWaitNanoseconds);
213221
}

system/include/emscripten/threading.h

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -29,16 +29,6 @@ bool emscripten_has_threading_support(void);
2929
// Returns the number of logical cores on the system.
3030
int emscripten_num_logical_cores(void);
3131

32-
// If the given memory address contains value val, puts the calling thread to
33-
// sleep waiting for that address to be notified.
34-
// Returns -EINVAL if addr is null.
35-
int emscripten_futex_wait(volatile void/*uint32_t*/ * _Nonnull addr, uint32_t val, double maxWaitMilliseconds);
36-
37-
// Wakes the given number of threads waiting on a location. Pass count ==
38-
// INT_MAX to wake all waiters on that location.
39-
// Returns -EINVAL if addr is null.
40-
int emscripten_futex_wake(volatile void/*uint32_t*/ * _Nonnull addr, int count);
41-
4232
// Returns true if the current thread is the thread that hosts the Emscripten
4333
// runtime.
4434
// Returns false on pthreads and Wasm Workers.

system/include/emscripten/threading_primitives.h

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,6 @@
1313
extern "C" {
1414
#endif
1515

16-
// Similar to emscripten_async_wait_callback_t but with a volatile first
17-
// argument.
18-
typedef void (*emscripten_async_wait_volatile_callback_t)(volatile void* address, uint32_t value, ATOMICS_WAIT_RESULT_T waitResult, void* userData);
19-
2016
#define emscripten_lock_t volatile uint32_t
2117

2218
// Use with syntax "emscripten_lock_t l = EMSCRIPTEN_LOCK_T_STATIC_INITIALIZER;"
@@ -34,7 +30,6 @@ void emscripten_lock_init(emscripten_lock_t * _Nonnull lock);
3430
// NOTE: This function can be only called in a Worker, and not on the main
3531
// browser thread, because the main browser thread cannot synchronously
3632
// sleep to wait for locks.
37-
3833
bool emscripten_lock_wait_acquire(emscripten_lock_t * _Nonnull lock, int64_t maxWaitNanoseconds);
3934

4035
// Similar to emscripten_lock_wait_acquire(), but instead of waiting for at most
@@ -74,6 +69,10 @@ bool emscripten_lock_busyspin_wait_acquire(emscripten_lock_t * _Nonnull lock, do
7469
// acquire without contention from other threads.
7570
void emscripten_lock_busyspin_waitinf_acquire(emscripten_lock_t * _Nonnull lock);
7671

72+
// Similar to emscripten_async_wait_callback_t but with a volatile first
73+
// argument.
74+
typedef void (*emscripten_async_wait_volatile_callback_t)(volatile void* address, uint32_t value, ATOMICS_WAIT_RESULT_T waitResult, void* userData);
75+
7776
// Registers an *asynchronous* lock acquire operation. The calling thread will
7877
// asynchronously try to obtain the given lock after the calling thread yields
7978
// back to the event loop. If the attempt is successful within
@@ -187,6 +186,16 @@ ATOMICS_WAIT_TOKEN_T emscripten_condvar_wait_async(emscripten_condvar_t * _Nonnu
187186
// ("broadcast" operation).
188187
void emscripten_condvar_signal(emscripten_condvar_t * _Nonnull condvar, int64_t numWaitersToSignal);
189188

189+
// If the given memory address contains value val, puts the calling thread to
190+
// sleep waiting for that address to be notified.
191+
// Returns -EINVAL if addr is null.
192+
int emscripten_futex_wait(volatile void/*uint32_t*/ * _Nonnull addr, uint32_t val, double maxWaitMilliseconds);
193+
194+
// Wakes the given number of threads waiting on a location. Pass count ==
195+
// INT_MAX to wake all waiters on that location.
196+
// Returns -EINVAL if addr is null.
197+
int emscripten_futex_wake(volatile void/*uint32_t*/ * _Nonnull addr, int count);
198+
190199
#ifdef __cplusplus
191200
}
192201
#endif

0 commit comments

Comments
 (0)