Skip to content

Commit a36da8a

Browse files
committed
Pre-calculate magic numbers
1 parent b34ad1a commit a36da8a

File tree

1 file changed

+37
-15
lines changed

1 file changed

+37
-15
lines changed

test/webaudio/audioworklet_emscripten_locks.c

Lines changed: 37 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,16 @@
1414
#define AUDIO_STACK_SIZE 2048
1515

1616
// Define DISABLE_LOCKS to run the test without locking, which should statistically always fail
17-
//#define DISABLE_LOCKS
17+
#define DISABLE_LOCKS
18+
19+
// Number of times mainLoop() calculations get called
20+
#define MAINLOOP_CALCS 10000
21+
// Number of times MAINLOOP_CALCS are performed
22+
#define MAINLOOP_RUNS 200
23+
// Number of times process() calculations get called (called 3.75x more than mainLoop)
24+
#define PROCESS_CALCS 2667
25+
// Number of times PROCESS_CALCS are performed (3.75x more than mainLoop)
26+
#define PROCESS_RUNS 750
1827

1928
// Internal, found in 'system/lib/pthread/threading_internal.h' (and requires building with -pthread)
2029
int _emscripten_thread_supports_atomics_wait(void);
@@ -51,6 +60,11 @@ typedef struct {
5160
uint32_t val2;
5261
} Dummy;
5362

63+
// Container used to run the test
64+
Dummy testData;
65+
// Container to hold the expected value
66+
Dummy trueData;
67+
5468
// Start values
5569
void initDummy(Dummy* dummy) {
5670
dummy->val0 = 4;
@@ -83,8 +97,7 @@ void runCalcs(Dummy* dummy, int num) {
8397
}
8498

8599
void stopping() {
86-
emscripten_out("Expect: 0x305868D2, 0x2D45E948, 0x2B1B1ED4");
87-
emscripten_out("Ending test");
100+
emscripten_out("Test done");
88101
emscripten_destroy_audio_context(context);
89102
emscripten_force_exit(0);
90103
}
@@ -99,7 +112,7 @@ bool process(int numInputs, const AudioSampleFrame* inputs, int numOutputs, Audi
99112
case TEST_RUNNING:
100113
case TEST_DONE_MAIN:
101114
if (howManyProc-- > 0) {
102-
runCalcs((Dummy*) data, 267); // <-- process gets called 3.75x more than main
115+
runCalcs((Dummy*) data, PROCESS_CALCS);
103116
} else {
104117
if (whichTest == TEST_DONE_MAIN) {
105118
emscripten_outf("Worklet done after %dms (expect: > 2s)", (int) (emscripten_get_now() - startTime));
@@ -122,7 +135,7 @@ bool mainLoop(double time, void* data) {
122135
break;
123136
case TEST_RUNNING:
124137
if (howManyMain-- > 0) {
125-
runCalcs((Dummy*) data, 1000);
138+
runCalcs((Dummy*) data, MAINLOOP_CALCS);
126139
} else {
127140
emscripten_outf("Main thread done after %dms (expect: > 2s)", (int) (emscripten_get_now() - startTime));
128141
// Done here, so signal to process()
@@ -133,11 +146,11 @@ bool mainLoop(double time, void* data) {
133146
// Wait for process() to finish
134147
break;
135148
case TEST_DONE:
149+
emscripten_out("Multi-thread results:");
136150
printDummy((Dummy*) data);
137-
// 32-bit maths with locks *should* result in these:
138-
assert(((Dummy*) data)->val0 == 0x305868D2
139-
&& ((Dummy*) data)->val1 == 0x2D45E948
140-
&& ((Dummy*) data)->val2 == 0x2B1B1ED4);
151+
assert(((Dummy*) data)->val0 == trueData.val0
152+
&& ((Dummy*) data)->val1 == trueData.val1
153+
&& ((Dummy*) data)->val2 == trueData.val2);
141154
stopping();
142155
return false;
143156
}
@@ -149,8 +162,8 @@ EMSCRIPTEN_KEEPALIVE void startTest() {
149162
if (emscripten_audio_context_state(context) != AUDIO_CONTEXT_STATE_RUNNING) {
150163
emscripten_resume_audio_context_sync(context);
151164
}
152-
howManyMain = 200;
153-
howManyProc = 750; // <-- process gets called 3.75x more than main
165+
howManyMain = MAINLOOP_RUNS;
166+
howManyProc = PROCESS_RUNS;
154167
}
155168

156169
// HTML button to manually run the test
@@ -191,17 +204,26 @@ void initialised(EMSCRIPTEN_WEBAUDIO_T ctx, bool success, void* data) {
191204

192205
int main() {
193206
emscripten_lock_init(&testLock);
194-
Dummy* dummy = (Dummy*) malloc(sizeof(Dummy));
195-
initDummy(dummy);
207+
initDummy(&testData);
208+
initDummy(&trueData);
209+
// Canonical results, run in a single thread
210+
for (int n = MAINLOOP_RUNS; n > 0; n--) {
211+
runCalcs(&trueData, MAINLOOP_CALCS);
212+
}
213+
for (int n = PROCESS_RUNS; n > 0; n--) {
214+
runCalcs(&trueData, PROCESS_CALCS);
215+
}
216+
emscripten_out("Single-thread results:");
217+
printDummy(&trueData);
196218

197219
char* const workletStack = memalign(16, AUDIO_STACK_SIZE);
198220
assert(workletStack);
199221
// Audio processor callback setup
200222
context = emscripten_create_audio_context(NULL);
201223
assert(context);
202-
emscripten_start_wasm_audio_worklet_thread_async(context, workletStack, AUDIO_STACK_SIZE, initialised, dummy);
224+
emscripten_start_wasm_audio_worklet_thread_async(context, workletStack, AUDIO_STACK_SIZE, initialised, &testData);
203225

204-
emscripten_set_timeout_loop(mainLoop, 10, dummy);
226+
emscripten_set_timeout_loop(mainLoop, 10, &testData);
205227
addButton();
206228
startTest(); // <-- May need a manual click to start
207229

0 commit comments

Comments
 (0)