17
17
int _emscripten_thread_supports_atomics_wait (void );
18
18
19
19
typedef enum {
20
+ // The test hasn't yet started
21
+ TEST_NOT_STARTED ,
20
22
// No wait support in audio worklets
21
23
TEST_HAS_WAIT ,
22
24
// Acquired in main, fail in process
@@ -40,26 +42,17 @@ typedef enum {
40
42
// Lock used in all the tests
41
43
emscripten_lock_t testLock = EMSCRIPTEN_LOCK_T_STATIC_INITIALIZER ;
42
44
// 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 ;
44
46
// Time at which the test starts taken in main()
45
47
double startTime = 0 ;
46
48
47
49
bool ProcessAudio (int numInputs , const AudioSampleFrame * inputs , int numOutputs , AudioSampleFrame * outputs , int numParams , const AudioParamFrame * params , void * userData ) {
48
50
assert (emscripten_current_thread_is_audio_worklet ());
49
51
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
-
61
52
int result = 0 ;
62
53
switch (whichTest ) {
54
+ case TEST_NOT_STARTED :
55
+ break ;
63
56
case TEST_HAS_WAIT :
64
57
// Should not have wait support here
65
58
result = _emscripten_thread_supports_atomics_wait ();
@@ -80,6 +73,7 @@ bool ProcessAudio(int numInputs, const AudioSampleFrame *inputs, int numOutputs,
80
73
emscripten_outf ("TEST_WAIT_ACQUIRE_FAIL: %d (expect: 0)" , result );
81
74
assert (!result );
82
75
whichTest = TEST_WAIT_ACQUIRE ;
76
+ break ;
83
77
case TEST_WAIT_ACQUIRE :
84
78
// Will get unlocked in main thread, so should quickly acquire
85
79
result = emscripten_lock_busyspin_wait_acquire (& testLock , 10000 );
@@ -130,6 +124,10 @@ bool MainLoop(double time, void* data) {
130
124
assert (!emscripten_current_thread_is_audio_worklet ());
131
125
static int didUnlock = false;
132
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 ;
133
131
case TEST_WAIT_ACQUIRE :
134
132
if (!didUnlock ) {
135
133
emscripten_out ("main thread releasing lock" );
@@ -178,9 +176,9 @@ int main() {
178
176
179
177
startTime = emscripten_get_now ();
180
178
181
- emscripten_set_timeout_loop (MainLoop , 10 , NULL );
182
179
EMSCRIPTEN_WEBAUDIO_T context = emscripten_create_audio_context (NULL );
183
180
emscripten_start_wasm_audio_worklet_thread_async (context , wasmAudioWorkletStack , sizeof (wasmAudioWorkletStack ), WebAudioWorkletThreadInitialized , NULL );
181
+ emscripten_set_timeout_loop (MainLoop , 10 , NULL );
184
182
185
183
emscripten_exit_with_live_runtime ();
186
184
}
0 commit comments