@@ -17,36 +17,32 @@ bool testFinished = false;
1717int numTimesMainThreadAcquiredLock = 0 ;
1818int numTimesWasmWorkerAcquiredLock = 0 ;
1919
20- void work ()
21- {
22- // emscripten_console_log("work");
20+ void work () {
21+ // emscripten_console_log("work");
2322 volatile int x = sharedState0 ;
2423 volatile int y = sharedState1 ;
2524 assert (x == y + 1 || y == x + 1 );
2625
27- if (emscripten_current_thread_is_wasm_worker ())
26+ if (emscripten_current_thread_is_wasm_worker ()) {
2827 ++ numTimesWasmWorkerAcquiredLock ;
29- else
28+ } else {
3029 ++ numTimesMainThreadAcquiredLock ;
30+ }
3131
32- if (x < y )
33- {
32+ if (x < y ) {
3433 x = y + 1 ;
35- if (emscripten_current_thread_is_wasm_worker ())
34+ if (emscripten_current_thread_is_wasm_worker ()) {
3635 emscripten_wasm_worker_sleep (/*nsecs=*/ (rand ()%100000 ));
36+ }
3737 sharedState0 = x ;
38- }
39- else
40- {
38+ } else {
4139 y = x + 1 ;
4240 if (emscripten_current_thread_is_wasm_worker ())
4341 emscripten_wasm_worker_sleep (/*nsecs=*/ (rand ()%100000 ));
4442 sharedState1 = y ;
4543
46- if (y > 100 && numTimesMainThreadAcquiredLock && numTimesWasmWorkerAcquiredLock )
47- {
48- if (!testFinished )
49- {
44+ if (y > 100 && numTimesMainThreadAcquiredLock && numTimesWasmWorkerAcquiredLock ) {
45+ if (!testFinished ) {
5046 emscripten_console_log ("test finished" );
5147#ifdef REPORT_RESULT
5248 REPORT_RESULT (0 );
@@ -59,33 +55,29 @@ void work()
5955
6056void schedule_work (void * userData );
6157
62- void lock_async_acquired (volatile void * addr , uint32_t val , ATOMICS_WAIT_RESULT_T waitResult , void * userData )
63- {
64- // emscripten_console_log("async lock acquired");
58+ void lock_async_acquired (volatile void * addr , uint32_t val , ATOMICS_WAIT_RESULT_T waitResult , void * userData ) {
59+ // emscripten_console_log("async lock acquired");
6560 assert (addr == & lock );
6661 assert (val == 0 || val == 1 );
6762 assert (waitResult == ATOMICS_WAIT_OK );
6863 assert (userData == (void * )42 );
6964 work ();
7065 emscripten_lock_release (& lock );
7166
72- if (!testFinished )
67+ if (!testFinished ) {
7368 emscripten_set_timeout (schedule_work , 10 , 0 );
69+ }
7470}
7571
76- void schedule_work (void * userData )
77- {
78- if (emscripten_current_thread_is_wasm_worker () && emscripten_random () > 0.5 )
79- {
72+ void schedule_work (void * userData ) {
73+ if (emscripten_current_thread_is_wasm_worker () && emscripten_random () > 0.5 ) {
8074 emscripten_lock_waitinf_acquire (& lock );
81- // emscripten_console_log("sync lock acquired");
75+ // emscripten_console_log("sync lock acquired");
8276 work ();
8377 emscripten_lock_release (& lock );
8478 if (!testFinished )
8579 emscripten_set_timeout (schedule_work , 0 , 0 );
86- }
87- else
88- {
80+ } else {
8981 emscripten_lock_async_acquire (& lock , lock_async_acquired , (void * )42 , EMSCRIPTEN_WAIT_ASYNC_INFINITY );
9082 }
9183}
@@ -94,11 +86,9 @@ void start_worker(int arg) {
9486 schedule_work (0 );
9587}
9688
97- int main ()
98- {
89+ int main () {
9990#define NUM_THREADS 10
100- for (int i = 0 ; i < NUM_THREADS ; ++ i )
101- {
91+ for (int i = 0 ; i < NUM_THREADS ; ++ i ) {
10292 emscripten_wasm_worker_t worker = emscripten_malloc_wasm_worker (1024 );
10393 emscripten_wasm_worker_post_function_vi (worker , start_worker , 0 );
10494 }
0 commit comments