Skip to content

Commit 2393eda

Browse files
committed
Move work off the main thread
1 parent 4dee288 commit 2393eda

File tree

1 file changed

+43
-31
lines changed

1 file changed

+43
-31
lines changed

test/webaudio/audioworklet_emscripten_locks.c

Lines changed: 43 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,12 @@ _Atomic Test whichTest = TEST_NOT_STARTED;
4646
// Time at which the test starts taken in main()
4747
double startTime = 0;
4848

49+
void do_exit() {
50+
emscripten_out("Test success");
51+
emscripten_terminate_all_wasm_workers();
52+
emscripten_force_exit(0);
53+
}
54+
4955
bool ProcessAudio(int numInputs, const AudioSampleFrame *inputs, int numOutputs, AudioSampleFrame *outputs, int numParams, const AudioParamFrame *params, void *userData) {
5056
assert(emscripten_current_thread_is_audio_worklet());
5157

@@ -120,37 +126,39 @@ EM_JS(void, InitHtmlUi, (EMSCRIPTEN_WEBAUDIO_T audioContext), {
120126
};
121127
});
122128

123-
bool MainLoop(double time, void* data) {
129+
void WorkerLoop() {
124130
assert(!emscripten_current_thread_is_audio_worklet());
125-
static int didUnlock = false;
126-
switch (whichTest) {
127-
case TEST_NOT_STARTED:
128-
emscripten_out("Staring test (may need a button click)");
129-
whichTest = TEST_HAS_WAIT;
130-
break;
131-
case TEST_WAIT_ACQUIRE:
132-
if (!didUnlock) {
133-
emscripten_out("main thread releasing lock");
134-
// Release here to acquire in process
135-
emscripten_lock_release(&testLock);
136-
didUnlock = true;
137-
}
138-
break;
139-
case TEST_WAIT_INFINTE_1:
140-
// Spin here until released in process (but don't change test until we know this case ran)
141-
whichTest = TEST_WAIT_INFINTE_2;
142-
emscripten_lock_busyspin_waitinf_acquire(&testLock);
143-
emscripten_out("TEST_WAIT_INFINTE (from main)");
144-
break;
145-
case TEST_DONE:
146-
// Finished, exit from the main thread
147-
emscripten_out("Test success");
148-
emscripten_force_exit(0);
149-
return false;
150-
default:
151-
break;
131+
int didUnlock = false;
132+
while (true) {
133+
switch (whichTest) {
134+
case TEST_NOT_STARTED:
135+
emscripten_out("Staring test (may need a button click)");
136+
whichTest = TEST_HAS_WAIT;
137+
break;
138+
case TEST_WAIT_ACQUIRE:
139+
if (!didUnlock) {
140+
emscripten_out("main thread releasing lock");
141+
// Release here to acquire in process
142+
emscripten_lock_release(&testLock);
143+
didUnlock = true;
144+
}
145+
break;
146+
case TEST_WAIT_INFINTE_1:
147+
// Spin here until released in process (but don't change test until we know this case ran)
148+
whichTest = TEST_WAIT_INFINTE_2;
149+
emscripten_lock_busyspin_waitinf_acquire(&testLock);
150+
emscripten_out("TEST_WAIT_INFINTE (from main)");
151+
break;
152+
case TEST_DONE:
153+
// Finished, exit from the main thread (and return out of this loop)
154+
emscripten_wasm_worker_post_function_v(EMSCRIPTEN_WASM_WORKER_ID_PARENT, &do_exit);
155+
return;
156+
default:
157+
break;
158+
}
159+
// Repeat every 16ms (except when TEST_DONE)
160+
emscripten_wasm_worker_sleep(16 * 1000000ULL);
152161
}
153-
return true;
154162
}
155163

156164
void AudioWorkletProcessorCreated(EMSCRIPTEN_WEBAUDIO_T audioContext, bool success, void *userData) {
@@ -169,16 +177,20 @@ void WebAudioWorkletThreadInitialized(EMSCRIPTEN_WEBAUDIO_T audioContext, bool s
169177
uint8_t wasmAudioWorkletStack[2048];
170178

171179
int main() {
172-
// Main thread init and acquire (work passes to the processor)
180+
// Main thread init and acquire (work passes to the audio processor)
173181
emscripten_lock_init(&testLock);
174182
int hasLock = emscripten_lock_busyspin_wait_acquire(&testLock, 0);
175183
assert(hasLock);
176184

177185
startTime = emscripten_get_now();
178186

187+
// Audio processor callback setup
179188
EMSCRIPTEN_WEBAUDIO_T context = emscripten_create_audio_context(NULL);
180189
emscripten_start_wasm_audio_worklet_thread_async(context, wasmAudioWorkletStack, sizeof(wasmAudioWorkletStack), WebAudioWorkletThreadInitialized, NULL);
181-
emscripten_set_timeout_loop(MainLoop, 10, NULL);
190+
191+
// Worker thread setup
192+
emscripten_wasm_worker_t worker = emscripten_malloc_wasm_worker(1024);
193+
emscripten_wasm_worker_post_function_v(worker, WorkerLoop);
182194

183195
emscripten_exit_with_live_runtime();
184196
}

0 commit comments

Comments
 (0)