Skip to content

Commit 8eb2804

Browse files
authored
Fix race condition in browser.test_audioworklet_worker (#26839)
Fix race condition in browser.test_audioworklet_worker where worker might miss the wakeup signal that audio worklet has posted. Update documentation of `emscripten_futex_wait()` for the different return values the function can have.
1 parent 1d7aa91 commit 8eb2804

2 files changed

Lines changed: 13 additions & 12 deletions

File tree

system/include/emscripten/threading_primitives.h

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -189,11 +189,16 @@ void emscripten_condvar_signal(emscripten_condvar_t * _Nonnull condvar, uint32_t
189189
// If the given memory address contains value val, puts the calling thread to
190190
// sleep waiting for that address to be notified. Like the linux futex syscall
191191
// this function returns negative errno values on failure.
192-
// Returns -EINVAL if addr is null.
193-
// Returns -ETIMEOUT if the maxWaitMilliseconds timeout was exceeded.
194-
// Returns -EINTR if the operation was interrupted (e.g. a timer fired, or an
195-
// async signal was received).
196-
// Returns 0 on success (i.e. another thread signaled this address)
192+
// Pass maxWaitMilliseconds = INFINITY (or __builtin_inf()) to sleep indefinitely.
193+
// Returns:
194+
// * negative value -EINVAL if addr is null.
195+
// * negative value -ETIMEDOUT if the maxWaitMilliseconds timeout was exceeded.
196+
// * negative value -EINTR if the operation was interrupted (e.g. a timer fired, or an
197+
// async signal was received).
198+
// * negative value -EWOULDBLOCK if the value of the memory address 'addr' was
199+
// not equal to 'val' to begin with.
200+
// * negative value -ECANCELED if the calling thread has been canceled.
201+
// * the value 0 on success (i.e. another thread signaled this address)
197202
int emscripten_futex_wait(volatile void/*uint32_t*/ * _Nonnull addr, uint32_t val, double maxWaitMilliseconds);
198203

199204
// Wakes the given number of threads waiting on a location. Pass count ==

test/webaudio/audioworklet_worker.c

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -21,13 +21,9 @@ void do_exit() {
2121
void run_in_worker() {
2222
double start = emscripten_performance_now();
2323
emscripten_outf("run_in_worker");
24-
while (emscripten_futex_wait(&workletToWorkerFlag, 0, 30000) == 0) {
25-
if (workletToWorkerFlag == true) {
26-
emscripten_outf("Test success (waited %.fms)", emscripten_performance_now() - start);
27-
emscripten_wasm_worker_post_function_v(EMSCRIPTEN_WASM_WORKER_ID_PARENT, &do_exit);
28-
break;
29-
}
30-
}
24+
emscripten_futex_wait(&workletToWorkerFlag, 0, __builtin_inf());
25+
emscripten_outf("Test success (waited %.fms)", emscripten_performance_now() - start);
26+
emscripten_wasm_worker_post_function_v(EMSCRIPTEN_WASM_WORKER_ID_PARENT, &do_exit);
3127
}
3228

3329
// This event will fire on the audio worklet thread.

0 commit comments

Comments
 (0)