55#include <stdlib.h>
66#include <assert.h>
77
8- // Tests that these audio worklet compatible functions work, details in comments below.
8+ // Tests that these audio worklet compatible functions work, details in comments below:
9+ //
910// - emscripten_thread_supports_atomics_wait()
1011// - emscripten_lock_init()
1112// - emscripten_lock_try_acquire()
@@ -35,8 +36,11 @@ typedef enum {
3536 TEST_DONE
3637} Test ;
3738
39+ // Lock used in all the tests
3840emscripten_lock_t testLock = EMSCRIPTEN_LOCK_T_STATIC_INITIALIZER ;
41+ // Which test is running (sometimes in the worklet, sometimes in the main thread)
3942_Atomic Test whichTest = TEST_HAS_WAIT ;
43+ // Time at which the test starts taken in main()
4044double startTime = 0 ;
4145
4246bool ProcessAudio (int numInputs , const AudioSampleFrame * inputs , int numOutputs , AudioSampleFrame * outputs , int numParams , const AudioParamFrame * params , void * userData ) {
@@ -63,7 +67,7 @@ bool ProcessAudio(int numInputs, const AudioSampleFrame *inputs, int numOutputs,
6367 assert (!result );
6468 whichTest = TEST_WAIT_ACQUIRE ;
6569 case TEST_WAIT_ACQUIRE :
66- // Will get unlocked in main, so should quickly acquire
70+ // Will get unlocked in main thread , so should quickly acquire
6771 result = emscripten_lock_busyspin_wait_acquire (& testLock , 100 );
6872 printf ("TEST_WAIT_ACQUIRE: %d\n" , result );
6973 assert (result );
@@ -78,16 +82,22 @@ bool ProcessAudio(int numInputs, const AudioSampleFrame *inputs, int numOutputs,
7882 whichTest = TEST_WAIT_INFINTE_1 ;
7983 break ;
8084 case TEST_WAIT_INFINTE_1 :
81- // Still locked when we enter here
85+ // Still locked when we enter here but move on in the main thread
86+ break ;
87+ case TEST_WAIT_INFINTE_2 :
88+ emscripten_lock_release (& testLock );
89+ whichTest = TEST_GET_NOW ;
90+ break ;
8291 case TEST_GET_NOW :
8392 result = (int ) (emscripten_get_now () - startTime );
8493 printf ("TEST_GET_NOW: %d\n" , result );
85- assert (result );
94+ assert (result > 0 );
8695 whichTest = TEST_DONE ;
96+ case TEST_DONE :
97+ return false;
8798 default :
8899 break ;
89100 }
90-
91101 return true;
92102}
93103
@@ -103,17 +113,19 @@ EM_JS(void, InitHtmlUi, (EMSCRIPTEN_WEBAUDIO_T audioContext), {
103113});
104114
105115bool MainLoop (double time , void * data ) {
106- int result = 0 ;
107116 switch (whichTest ) {
108117 case TEST_WAIT_ACQUIRE :
109118 // Release here to acquire in process
110119 emscripten_lock_release (& testLock );
111120 break ;
112121 case TEST_WAIT_INFINTE_1 :
113- // Spin here until released in process
122+ // Spin here until released in process (but don't change test until we know this case ran)
123+ whichTest = TEST_WAIT_INFINTE_2 ;
114124 emscripten_lock_busyspin_waitinf_acquire (& testLock );
115- whichTest = TEST_DONE ;
125+ printf ( "TEST_WAIT_INFINTE (from main)\n" ) ;
116126 break ;
127+ case TEST_DONE :
128+ return false;
117129 default :
118130 break ;
119131 }
@@ -133,7 +145,7 @@ void WebAudioWorkletThreadInitialized(EMSCRIPTEN_WEBAUDIO_T audioContext, bool s
133145 emscripten_create_wasm_audio_worklet_processor_async (audioContext , & opts , AudioWorkletProcessorCreated , NULL );
134146}
135147
136- uint8_t wasmAudioWorkletStack [4096 ];
148+ uint8_t wasmAudioWorkletStack [2048 ];
137149
138150int main () {
139151 // Main thread init and acquire (work passes to the processor)
0 commit comments