Skip to content

Commit 501eb51

Browse files
committed
Improved symmetry
The test function is called approx. 200'000x from each thread.
1 parent 154ffde commit 501eb51

File tree

1 file changed

+14
-7
lines changed

1 file changed

+14
-7
lines changed

test/webaudio/audioworklet_emscripten_locks.c

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,9 @@
1818
// This needs to be big enough for a stereo output (1024 with a 128 frame) + working stack
1919
#define AUDIO_STACK_SIZE 2048
2020

21+
// Define DISABLE_LOCKS to run the test without locking, which should statistically always fail
22+
//#define DISABLE_LOCKS
23+
2124
// Internal, found in 'system/lib/pthread/threading_internal.h' (and requires building with -pthread)
2225
int _emscripten_thread_supports_atomics_wait(void);
2326

@@ -65,10 +68,13 @@ void printDummy(Dummy* dummy) {
6568
}
6669

6770
// Run a simple calculation that will only be stable *if* all values are atomically updated
71+
// (Currently called approx. 200'000x from each thread)
6872
void runCalcs(Dummy* dummy, int num) {
6973
for (int n = 0; n < num; n++) {
74+
#ifndef DISABLE_LOCKS
7075
int have = emscripten_lock_busyspin_wait_acquire(&testLock, 10);
7176
assert(have);
77+
#endif
7278
dummy->val0 += dummy->val1 * dummy->val2;
7379
dummy->val1 += dummy->val2 * dummy->val0;
7480
dummy->val2 += dummy->val0 * dummy->val1;
@@ -80,7 +86,7 @@ void runCalcs(Dummy* dummy, int num) {
8086
}
8187

8288
void stopping() {
83-
emscripten_out("Expect: 949807601, 1303780836, 243502614");
89+
emscripten_out("Expect: 811100370, 759556424, 723197652");
8490
emscripten_out("Ending test");
8591
emscripten_destroy_audio_context(context);
8692
emscripten_force_exit(0);
@@ -96,16 +102,16 @@ bool process(int numInputs, const AudioSampleFrame* inputs, int numOutputs, Audi
96102
case TEST_RUNNING:
97103
case TEST_DONE_MAIN:
98104
if (howManyProc-- > 0) {
99-
runCalcs((Dummy*) data, 250);
105+
runCalcs((Dummy*) data, 267); // <-- process gets called 3.75x more than main
100106
} else {
101107
if (whichTest == TEST_DONE_MAIN) {
108+
emscripten_outf("Worklet done after %dms (expect: > 2s)", (int) (emscripten_get_now() - startTime));
102109
// Both loops are finished
103110
whichTest = TEST_DONE;
104111
}
105112
}
106113
break;
107114
case TEST_DONE:
108-
emscripten_outf("Took %dms (expect: > 0)", (int) (emscripten_get_now() - startTime));
109115
return false;
110116
}
111117
return true;
@@ -121,6 +127,7 @@ bool mainLoop(double time, void* data) {
121127
if (howManyMain-- > 0) {
122128
runCalcs((Dummy*) data, 1000);
123129
} else {
130+
emscripten_outf("Main thread done after %dms (expect: > 2s)", (int) (emscripten_get_now() - startTime));
124131
// Done here, so signal to process()
125132
whichTest = TEST_DONE_MAIN;
126133
}
@@ -131,9 +138,9 @@ bool mainLoop(double time, void* data) {
131138
case TEST_DONE:
132139
printDummy((Dummy*) data);
133140
// 32-bit maths with locks *should* result in these:
134-
assert(((Dummy*) data)->val0 == 949807601
135-
&& ((Dummy*) data)->val1 == 1303780836
136-
&& ((Dummy*) data)->val2 == 243502614);
141+
assert(((Dummy*) data)->val0 == 811100370
142+
&& ((Dummy*) data)->val1 == 759556424
143+
&& ((Dummy*) data)->val2 == 723197652);
137144
stopping();
138145
return false;
139146
}
@@ -146,7 +153,7 @@ KEEP_IN_MODULE void startTest() {
146153
emscripten_resume_audio_context_sync(context);
147154
}
148155
howManyMain = 200;
149-
howManyProc = 200;
156+
howManyProc = 750; // <-- process gets called 3.75x more than main
150157
}
151158

152159
// HTML button to manually run the test

0 commit comments

Comments
 (0)