Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions src/lib/libwasm_worker.js
Original file line number Diff line number Diff line change
Expand Up @@ -350,7 +350,11 @@ if (ENVIRONMENT_IS_WASM_WORKER
__do_set_thread_state: (tb) => {
___set_thread_state(
/*thread_ptr=*/0,
#if AUDIO_WORKLET
/*is_main_thread=*/!ENVIRONMENT_IS_WORKER && !ENVIRONMENT_IS_AUDIO_WORKLET,
#else
/*is_main_thread=*/!ENVIRONMENT_IS_WORKER,
#endif
/*is_runtime_thread=*/!ENVIRONMENT_IS_WASM_WORKER,
/*supports_wait=*/ENVIRONMENT_IS_WORKER && {{{ workerSupportsFutexWait() }}});
},
Expand Down
5 changes: 3 additions & 2 deletions system/lib/pthread/emscripten_futex_wait.c
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ extern void* _emscripten_main_thread_futex;
static int futex_wait_main_browser_thread(volatile void* addr,
uint32_t val,
double timeout, bool cancelable) {
DBG("futex_wait_main_browser_thread");
// Atomics.wait is not available in the main browser thread, so simulate it
// via busy spinning. Only the main browser thread is allowed to call into
// this function. It is not thread-safe to be called from any other thread.
Expand Down Expand Up @@ -154,14 +155,14 @@ static int _do_futex_wait(volatile void *addr, uint32_t val, double max_wait_ms)
bool cancelable = false;
#endif

DBG("emscripten_futex_wait ms=%f", max_wait_ms);

// For the main browser thread and audio worklets we can't use
// __builtin_wasm_memory_atomic_wait32 so we have busy wait instead.
if (!_emscripten_thread_supports_atomics_wait()) {
return futex_wait_main_browser_thread(addr, val, max_wait_ms, cancelable);
}

DBG("emscripten_futex_wait ms=%f", max_wait_ms);

bool is_runtime_thread = emscripten_is_main_runtime_thread();
if (is_runtime_thread) {
max_wait_ms = fmin(max_wait_ms, fmax(0, _emscripten_next_timer()));
Expand Down
5 changes: 4 additions & 1 deletion test/webaudio/audioworklet.c
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#include <emscripten/webaudio.h>
#include <emscripten/threading.h>
#include <assert.h>
#include <unistd.h>

Expand Down Expand Up @@ -38,12 +39,13 @@ bool ProcessAudio(int numInputs,
int numParams,
const AudioParamFrame* params,
void* userData) {
assert(!emscripten_is_main_browser_thread());
assert(emscripten_current_thread_is_audio_worklet());
#ifdef TEST_AND_EXIT
// Only running in the test harness, see main_thread_tls_access()
assert(testTlsVariable == lastTlsVariableValueInAudioThread);
++testTlsVariable;
lastTlsVariableValueInAudioThread = testTlsVariable;
assert(emscripten_current_thread_is_audio_worklet());
#endif

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

int main() {
assert(emscripten_is_main_browser_thread());
assert(!emscripten_current_thread_is_audio_worklet());

// Create an audio context
Expand Down
Loading