Skip to content

Commit

Permalink
Make lock_waitinf_acquire test more robust to run
Browse files Browse the repository at this point in the history
  • Loading branch information
juj committed Nov 15, 2021
1 parent 27a8ae8 commit f40fc54
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 6 deletions.
4 changes: 4 additions & 0 deletions system/include/emscripten/wasm_worker.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,10 @@ void emscripten_terminate_all_wasm_workers(void);
// so avoid calling it in hot paths.
EM_BOOL emscripten_current_thread_is_wasm_worker(void);

// Returns a unique ID that identifies the calling Wasm Worker. Similar to pthread_self().
// The main browser thread will return 0 as the ID. First Wasm Worker will return 1, and so on.
uint32_t emscripten_wasm_worker_self_id(void);

// postMessage()s a function call over to the given Wasm Worker.
// Note that if the Wasm Worker runs in an infinite loop, it will not process the postMessage
// queue to dispatch the function call, until the infinite loop is broken and execution is returned
Expand Down
4 changes: 2 additions & 2 deletions tests/test_browser.py
Original file line number Diff line number Diff line change
Expand Up @@ -5096,7 +5096,7 @@ def test_system(self):
self.btest_exit(test_file('system.c'))

# Tests emscripten_create_wasm_worker() and emscripten_current_thread_is_wasm_worker() functions
def test_hello_wasm_worker(self):
def test_wasm_worker_hello(self):
self.btest(path_from_root('tests', 'wasm_worker', 'hello_wasm_worker.c'),
expected='0',
args=['-s', 'WASM_WORKERS=1', '-s', 'MINIMAL_RUNTIME=1'])
Expand Down Expand Up @@ -5183,7 +5183,7 @@ def test_wasm_worker_cancel_all_wait_asyncs_at_address(self):
# Tests emscripten_lock_init(), emscripten_lock_waitinf_acquire() and emscripten_lock_release()
def test_wasm_worker_lock_waitinf(self):
self.btest(path_from_root('tests', 'wasm_worker', 'lock_waitinf_acquire.c'),
expected='10001',
expected='4000',
args=['-s', 'WASM_WORKERS=1', '-s', 'MINIMAL_RUNTIME=1'])

# Tests emscripten_lock_wait_acquire() and emscripten_lock_try_acquire() in Worker.
Expand Down
13 changes: 9 additions & 4 deletions tests/wasm_worker/lock_waitinf_acquire.c
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#include <emscripten.h>
#include <emscripten/wasm_worker.h>
#include <emscripten/em_math.h>
#include <emscripten/threading.h>
#include <stdlib.h>
#include <assert.h>
Expand All @@ -16,14 +17,16 @@ volatile int numWorkersAlive = 0;

void test_ended()
{
EM_ASM(console.log(`Worker ${$0} last thread to finish. Reporting test end with sharedState0=${$1}, sharedState1=${$2}`), emscripten_wasm_worker_self_id(), sharedState0, sharedState1);
assert(sharedState0 == sharedState1 + 1 || sharedState1 == sharedState0 + 1);
#ifdef REPORT_RESULT
REPORT_RESULT(sharedState0 > sharedState1 ? sharedState0 : sharedState1);
REPORT_RESULT(sharedState0);
#endif
}

void worker_main()
{
EM_ASM(console.log(`Worker ${$0} running...`), emscripten_wasm_worker_self_id());
// Create contention on the lock from each thread, and stress the shared state
// in a racy way that would show a breakage if the lock is not watertight.
for(int i = 0; i < 1000; ++i)
Expand All @@ -35,18 +38,20 @@ void worker_main()
if (x < y)
{
x = y + 1;
emscripten_wasm_worker_sleep(/*nsecs=*/(rand()%100000));
emscripten_wasm_worker_sleep(/*nsecs=*/((uint64_t)(emscripten_math_random()*1000)));
sharedState0 = x;
}
else
{
y = x + 1;
emscripten_wasm_worker_sleep(/*nsecs=*/(rand()%100000));
emscripten_wasm_worker_sleep(/*nsecs=*/((uint64_t)(emscripten_math_random()*1000)));
sharedState1 = y;
}
emscripten_lock_release(&lock);
}

EM_ASM(console.log(`Worker ${$0} finished.`), emscripten_wasm_worker_self_id());

// Are we the last thread to finish? If so, test has ended.
uint32_t v = emscripten_atomic_sub_u32((void*)&numWorkersAlive, 1);
if (v == 1)
Expand All @@ -59,7 +64,7 @@ int main()
{
emscripten_lock_init(&lock);

#define NUM_THREADS 10
#define NUM_THREADS 4
numWorkersAlive = NUM_THREADS;
for(int i = 0; i < NUM_THREADS; ++i)
{
Expand Down

0 comments on commit f40fc54

Please sign in to comment.