5
5
#include <stdlib.h>
6
6
#include <assert.h>
7
7
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
+ //
9
10
// - emscripten_thread_supports_atomics_wait()
10
11
// - emscripten_lock_init()
11
12
// - emscripten_lock_try_acquire()
@@ -35,8 +36,11 @@ typedef enum {
35
36
TEST_DONE
36
37
} Test ;
37
38
39
+ // Lock used in all the tests
38
40
emscripten_lock_t testLock = EMSCRIPTEN_LOCK_T_STATIC_INITIALIZER ;
41
+ // Which test is running (sometimes in the worklet, sometimes in the main thread)
39
42
_Atomic Test whichTest = TEST_HAS_WAIT ;
43
+ // Time at which the test starts taken in main()
40
44
double startTime = 0 ;
41
45
42
46
bool 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,
63
67
assert (!result );
64
68
whichTest = TEST_WAIT_ACQUIRE ;
65
69
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
67
71
result = emscripten_lock_busyspin_wait_acquire (& testLock , 100 );
68
72
printf ("TEST_WAIT_ACQUIRE: %d\n" , result );
69
73
assert (result );
@@ -78,16 +82,22 @@ bool ProcessAudio(int numInputs, const AudioSampleFrame *inputs, int numOutputs,
78
82
whichTest = TEST_WAIT_INFINTE_1 ;
79
83
break ;
80
84
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 ;
82
91
case TEST_GET_NOW :
83
92
result = (int ) (emscripten_get_now () - startTime );
84
93
printf ("TEST_GET_NOW: %d\n" , result );
85
- assert (result );
94
+ assert (result > 0 );
86
95
whichTest = TEST_DONE ;
96
+ case TEST_DONE :
97
+ return false;
87
98
default :
88
99
break ;
89
100
}
90
-
91
101
return true;
92
102
}
93
103
@@ -103,17 +113,19 @@ EM_JS(void, InitHtmlUi, (EMSCRIPTEN_WEBAUDIO_T audioContext), {
103
113
});
104
114
105
115
bool MainLoop (double time , void * data ) {
106
- int result = 0 ;
107
116
switch (whichTest ) {
108
117
case TEST_WAIT_ACQUIRE :
109
118
// Release here to acquire in process
110
119
emscripten_lock_release (& testLock );
111
120
break ;
112
121
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 ;
114
124
emscripten_lock_busyspin_waitinf_acquire (& testLock );
115
- whichTest = TEST_DONE ;
125
+ printf ( "TEST_WAIT_INFINTE (from main)\n" ) ;
116
126
break ;
127
+ case TEST_DONE :
128
+ return false;
117
129
default :
118
130
break ;
119
131
}
@@ -133,7 +145,7 @@ void WebAudioWorkletThreadInitialized(EMSCRIPTEN_WEBAUDIO_T audioContext, bool s
133
145
emscripten_create_wasm_audio_worklet_processor_async (audioContext , & opts , AudioWorkletProcessorCreated , NULL );
134
146
}
135
147
136
- uint8_t wasmAudioWorkletStack [4096 ];
148
+ uint8_t wasmAudioWorkletStack [2048 ];
137
149
138
150
int main () {
139
151
// Main thread init and acquire (work passes to the processor)
0 commit comments