1717int _emscripten_thread_supports_atomics_wait (void );
1818
1919typedef enum {
20+ // The test hasn't yet started
21+ TEST_NOT_STARTED ,
2022 // No wait support in audio worklets
2123 TEST_HAS_WAIT ,
2224 // Acquired in main, fail in process
@@ -40,26 +42,17 @@ typedef enum {
4042// Lock used in all the tests
4143emscripten_lock_t testLock = EMSCRIPTEN_LOCK_T_STATIC_INITIALIZER ;
4244// Which test is running (sometimes in the worklet, sometimes in the main thread)
43- _Atomic Test whichTest = TEST_HAS_WAIT ;
45+ _Atomic Test whichTest = TEST_NOT_STARTED ;
4446// Time at which the test starts taken in main()
4547double startTime = 0 ;
4648
4749bool ProcessAudio (int numInputs , const AudioSampleFrame * inputs , int numOutputs , AudioSampleFrame * outputs , int numParams , const AudioParamFrame * params , void * userData ) {
4850 assert (emscripten_current_thread_is_audio_worklet ());
4951
50- // Produce at few empty frames of audio before we start trying to interact
51- // with the with main thread.
52- // On chrome at least it appears the main thread completely blocks until
53- // a few frames have been produced. This means it may not be safe to interact
54- // with the main thread during initial frames?
55- // In my experiments it seems like 5 was the magic number that I needed to
56- // produce before the main thread could continue to run.
57- // See https://github.com/emscripten-core/emscripten/issues/24213
58- static int count = 0 ;
59- if (count ++ < 5 ) return true;
60-
6152 int result = 0 ;
6253 switch (whichTest ) {
54+ case TEST_NOT_STARTED :
55+ break ;
6356 case TEST_HAS_WAIT :
6457 // Should not have wait support here
6558 result = _emscripten_thread_supports_atomics_wait ();
@@ -80,6 +73,7 @@ bool ProcessAudio(int numInputs, const AudioSampleFrame *inputs, int numOutputs,
8073 emscripten_outf ("TEST_WAIT_ACQUIRE_FAIL: %d (expect: 0)" , result );
8174 assert (!result );
8275 whichTest = TEST_WAIT_ACQUIRE ;
76+ break ;
8377 case TEST_WAIT_ACQUIRE :
8478 // Will get unlocked in main thread, so should quickly acquire
8579 result = emscripten_lock_busyspin_wait_acquire (& testLock , 10000 );
@@ -130,6 +124,10 @@ bool MainLoop(double time, void* data) {
130124 assert (!emscripten_current_thread_is_audio_worklet ());
131125 static int didUnlock = false;
132126 switch (whichTest ) {
127+ case TEST_NOT_STARTED :
128+ emscripten_out ("Staring test (may need a button click)" );
129+ whichTest = TEST_HAS_WAIT ;
130+ break ;
133131 case TEST_WAIT_ACQUIRE :
134132 if (!didUnlock ) {
135133 emscripten_out ("main thread releasing lock" );
@@ -178,9 +176,9 @@ int main() {
178176
179177 startTime = emscripten_get_now ();
180178
181- emscripten_set_timeout_loop (MainLoop , 10 , NULL );
182179 EMSCRIPTEN_WEBAUDIO_T context = emscripten_create_audio_context (NULL );
183180 emscripten_start_wasm_audio_worklet_thread_async (context , wasmAudioWorkletStack , sizeof (wasmAudioWorkletStack ), WebAudioWorkletThreadInitialized , NULL );
181+ emscripten_set_timeout_loop (MainLoop , 10 , NULL );
184182
185183 emscripten_exit_with_live_runtime ();
186184}
0 commit comments