Skip to content

Commit 8e1067f

Browse files
committed
Extend hybrid pthread/wasm-workers support for Audio Worklets. NFC
Prior to this change calling pthread functions from an audio worklet context was undefined behaviour. Followup to #26777
1 parent eff7318 commit 8e1067f

6 files changed

Lines changed: 24 additions & 7 deletions

File tree

src/lib/libsigs.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -314,7 +314,8 @@ sigs = {
314314
_embind_register_value_object__sig: 'vpppppp',
315315
_embind_register_value_object_field__sig: 'vpppppppppp',
316316
_embind_register_void__sig: 'vpp',
317-
_emscripten_create_audio_worklet__sig: 'viipipp',
317+
_emscripten_create_audio_worklet__sig: 'viipipipp',
318+
318319
_emscripten_create_wasm_worker__sig: 'iipip',
319320
_emscripten_dlopen_js__sig: 'vpppp',
320321
_emscripten_dlsync_threads__sig: 'v',

src/lib/libwebaudio.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -170,7 +170,7 @@ var LibraryWebAudio = {
170170
_emscripten_create_audio_worklet__deps: [
171171
'$_emAudioDispatchProcessorCallback',
172172
'$stackAlloc', '$stackRestore', '$stackSave'],
173-
_emscripten_create_audio_worklet: (wwID, contextHandle, stackLowestAddress, stackSize, callback, userData) => {
173+
_emscripten_create_audio_worklet: (wwID, contextHandle, stackLowestAddress, stackSize, pthreadPtr, callback, userData) => {
174174

175175
#if ASSERTIONS || WEBAUDIO_DEBUG
176176
emAudioExpectContext(contextHandle, '_emscripten_create_audio_worklet');
@@ -256,6 +256,9 @@ var LibraryWebAudio = {
256256
wasmMemory,
257257
stackLowestAddress, // sb = stack base
258258
stackSize, // sz = stack size
259+
#if PTHREADS
260+
pthreadPtr,
261+
#endif
259262
});
260263
audioWorklet.port.onmessage = _emAudioDispatchProcessorCallback;
261264
{{{ makeDynCall('viip', 'callback') }}}(contextHandle, 1/*EM_TRUE*/, userData);

system/lib/libc/emscripten_internal.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,7 @@ void emscripten_fetch_free(unsigned int);
137137
// to perform the wasm worker creation.
138138
bool _emscripten_create_wasm_worker(emscripten_wasm_worker_t wwID, void *stackLowestAddress, uint32_t stackSize, void* pthreadPtr);
139139

140-
void _emscripten_create_audio_worklet(emscripten_wasm_worker_t wwID, EMSCRIPTEN_WEBAUDIO_T audioContext, void *stackLowestAddress, uint32_t stackSize, EmscriptenStartWebAudioWorkletCallback callback, void *userData2);
140+
void _emscripten_create_audio_worklet(emscripten_wasm_worker_t wwID, EMSCRIPTEN_WEBAUDIO_T audioContext, void *stackLowestAddress, uint32_t stackSize, void* pthreadPtr, EmscriptenStartWebAudioWorkletCallback callback, void *userData2);
141141

142142
void __resumeException(void* exn);
143143
void __cxa_call_unexpected(void* exn);

system/lib/pthread/threading_internal.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,12 @@ int _emscripten_thread_supports_atomics_wait(void);
120120

121121
pid_t _emscripten_get_next_tid();
122122

123+
// Initialize pthread data, at start of memory region pointed to `base`.
124+
// `size` is an in/out parameter representing the size of the `base` region
125+
// on input, and the size of new/adjusted region on output.
126+
// Return a new/adjusted memory base to be used to stack/tls data.
127+
void* _emscripten_init_pthread(void *base, size_t* size, pid_t tid);
128+
123129
// Wake the target thread in case it is blocked in emscripten_futex_wait.
124130
// Note: If threads directly use lower level APIs such
125131
// __builtin_wasm_memory_atomic_waitXX then they will not be woken by

system/lib/wasm_worker/audio_worklet.c

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,5 +13,12 @@
1313
// Simple wrapper function around the JS _emscripten_create_audio_worklet
1414
// function that adds the _emscripten_get_next_tid() as arg0
1515
void emscripten_start_wasm_audio_worklet_thread_async(EMSCRIPTEN_WEBAUDIO_T audioContext, void *stackLowestAddress, uint32_t stackSize, EmscriptenStartWebAudioWorkletCallback callback, void *userData2) {
16-
_emscripten_create_audio_worklet(_emscripten_get_next_tid(), audioContext, stackLowestAddress, stackSize, callback, userData2);
16+
emscripten_wasm_worker_t wwID = _emscripten_get_next_tid();
17+
void* pthreadPtr = stackLowestAddress;
18+
#ifdef __EMSCRIPTEN_PTHREADS__
19+
size_t stackSize_ = stackSize;
20+
stackLowestAddress = _emscripten_init_pthread(stackLowestAddress, &stackSize_, wwID);
21+
stackSize = stackSize_;
22+
#endif
23+
_emscripten_create_audio_worklet(wwID, audioContext, stackLowestAddress, stackSize, pthreadPtr, callback, userData2);
1724
}

system/lib/wasm_worker/library_wasm_worker.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ weak_alias(dummy, __pthread_tsd_size);
9494
* In either case these sections are all aligned to `max_alignment()`
9595
* which is the max alignment of any of the given chunks.
9696
*/
97-
static void* init_pthread_struct(void *stackPlusTLSAddress, pid_t tid, size_t* stackPlusTLSSize) {
97+
void* _emscripten_init_pthread(void *stackPlusTLSAddress, size_t* stackPlusTLSSize, pid_t tid) {
9898
// TODO: Remove duplication with pthread_create
9999
pthread_t self = pthread_self();
100100

@@ -143,7 +143,7 @@ static void* init_pthread_struct(void *stackPlusTLSAddress, pid_t tid, size_t* s
143143

144144
__tl_unlock();
145145

146-
dbg("init_pthread_struct: base=%#lx, end=%#lx, used=%zu "
146+
dbg("_emscripten_init_pthread: base=%#lx, end=%#lx, used=%zu "
147147
"stackold=%zu stacknew=%zu",
148148
base,
149149
base + *stackPlusTLSSize,
@@ -202,7 +202,7 @@ emscripten_wasm_worker_t emscripten_create_wasm_worker(void *stackPlusTLSAddress
202202
emscripten_wasm_worker_t wwID = _emscripten_get_next_tid();
203203
void* pthreadPtr = stackPlusTLSAddress;
204204
#ifdef __EMSCRIPTEN_PTHREADS__
205-
stackPlusTLSAddress = init_pthread_struct(stackPlusTLSAddress, wwID, &stackPlusTLSSize);
205+
stackPlusTLSAddress = _emscripten_init_pthread(stackPlusTLSAddress, &stackPlusTLSSize, wwID);
206206
#endif
207207
if (!_emscripten_create_wasm_worker(wwID, stackPlusTLSAddress, stackPlusTLSSize, pthreadPtr))
208208
return 0;

0 commit comments

Comments
 (0)