Skip to content

Commit 0d980eb

Browse files
authored
Fix emscripten_is_main_runtime_thread() under in audio worklet (#26818)
This function was incorrectly returning `true` in audio worklets because `ENVIRONMENT_IS_WORKER` is not defined in audio worklets.
1 parent 2a8234f commit 0d980eb

3 files changed

Lines changed: 11 additions & 3 deletions

File tree

src/lib/libwasm_worker.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -350,7 +350,11 @@ if (ENVIRONMENT_IS_WASM_WORKER
350350
__do_set_thread_state: (tb) => {
351351
___set_thread_state(
352352
/*thread_ptr=*/0,
353+
#if AUDIO_WORKLET
354+
/*is_main_thread=*/!ENVIRONMENT_IS_WORKER && !ENVIRONMENT_IS_AUDIO_WORKLET,
355+
#else
353356
/*is_main_thread=*/!ENVIRONMENT_IS_WORKER,
357+
#endif
354358
/*is_runtime_thread=*/!ENVIRONMENT_IS_WASM_WORKER,
355359
/*supports_wait=*/ENVIRONMENT_IS_WORKER && {{{ workerSupportsFutexWait() }}});
356360
},

system/lib/pthread/emscripten_futex_wait.c

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ extern void* _emscripten_main_thread_futex;
3232
static int futex_wait_main_browser_thread(volatile void* addr,
3333
uint32_t val,
3434
double timeout, bool cancelable) {
35+
DBG("futex_wait_main_browser_thread");
3536
// Atomics.wait is not available in the main browser thread, so simulate it
3637
// via busy spinning. Only the main browser thread is allowed to call into
3738
// this function. It is not thread-safe to be called from any other thread.
@@ -154,14 +155,14 @@ static int _do_futex_wait(volatile void *addr, uint32_t val, double max_wait_ms)
154155
bool cancelable = false;
155156
#endif
156157

158+
DBG("emscripten_futex_wait ms=%f", max_wait_ms);
159+
157160
// For the main browser thread and audio worklets we can't use
158161
// __builtin_wasm_memory_atomic_wait32 so we have busy wait instead.
159162
if (!_emscripten_thread_supports_atomics_wait()) {
160163
return futex_wait_main_browser_thread(addr, val, max_wait_ms, cancelable);
161164
}
162165

163-
DBG("emscripten_futex_wait ms=%f", max_wait_ms);
164-
165166
bool is_runtime_thread = emscripten_is_main_runtime_thread();
166167
if (is_runtime_thread) {
167168
max_wait_ms = fmin(max_wait_ms, fmax(0, _emscripten_next_timer()));

test/webaudio/audioworklet.c

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
#include <emscripten/webaudio.h>
2+
#include <emscripten/threading.h>
23
#include <assert.h>
34
#include <unistd.h>
45

@@ -38,12 +39,13 @@ bool ProcessAudio(int numInputs,
3839
int numParams,
3940
const AudioParamFrame* params,
4041
void* userData) {
42+
assert(!emscripten_is_main_browser_thread());
43+
assert(emscripten_current_thread_is_audio_worklet());
4144
#ifdef TEST_AND_EXIT
4245
// Only running in the test harness, see main_thread_tls_access()
4346
assert(testTlsVariable == lastTlsVariableValueInAudioThread);
4447
++testTlsVariable;
4548
lastTlsVariableValueInAudioThread = testTlsVariable;
46-
assert(emscripten_current_thread_is_audio_worklet());
4749
#endif
4850

4951
// Verify that getentropy fails gracefully (i.e. returns non-zero) when called
@@ -146,6 +148,7 @@ void WebAudioWorkletThreadInitialized(EMSCRIPTEN_WEBAUDIO_T audioContext, bool s
146148
uint8_t wasmAudioWorkletStack[4096];
147149

148150
int main() {
151+
assert(emscripten_is_main_browser_thread());
149152
assert(!emscripten_current_thread_is_audio_worklet());
150153

151154
// Create an audio context

0 commit comments

Comments
 (0)