Skip to content

Commit 0578e9d

Browse files
committed
[test] Fix flakiness in test_pthread_wait32/64_notify
Avoid racing between the main thread and the worker by only triggering the main thread to notify once the workers done printing all its messages.
1 parent 1311f22 commit 0578e9d

File tree

3 files changed

+8
-8
lines changed

3 files changed

+8
-8
lines changed

test/wasm_worker/wait32_notify.c

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,8 @@ void run_test() {
3333
assert(ret == ATOMICS_WAIT_TIMED_OUT);
3434

3535
emscripten_out("Waiting for infinitely long should return 'ok'");
36-
ret = emscripten_atomic_wait_u32((int32_t*)&addr, 1, /*timeout=*/-1);
36+
emscripten_atomic_store_u32((void*)&addr, 3);
37+
ret = emscripten_atomic_wait_u32((int32_t*)&addr, 3, /*timeout=*/-1);
3738
assert(ret == ATOMICS_WAIT_OK);
3839

3940
emscripten_out("Test finished");
@@ -43,8 +44,6 @@ void run_test() {
4344
// This test run in both wasm workers and pthreads mode
4445
#ifdef __EMSCRIPTEN_WASM_WORKERS__
4546

46-
char stack[1024];
47-
4847
void worker_main() {
4948
run_test();
5049

@@ -66,15 +65,14 @@ void* thread_main(void* arg) {
6665

6766

6867
EM_BOOL main_loop(double time, void *userData) {
69-
if (addr == 1) {
68+
if (addr == 3) {
7069
// Burn one second to make sure worker finishes its test.
7170
emscripten_out("main: seen worker running");
7271
double t0 = emscripten_performance_now();
7372
while(emscripten_performance_now() < t0 + 1000);
7473

7574
// Wake the waiter
7675
emscripten_out("main: waking worker");
77-
addr = 2;
7876
emscripten_atomic_notify((int32_t*)&addr, 1);
7977

8078
#ifndef __EMSCRIPTEN_WASM_WORKERS__
@@ -90,6 +88,7 @@ int main() {
9088
emscripten_out("main: creating worker");
9189

9290
#ifdef __EMSCRIPTEN_WASM_WORKERS__
91+
static char stack[1024];
9392
emscripten_wasm_worker_t worker = emscripten_create_wasm_worker(stack, sizeof(stack));
9493
emscripten_wasm_worker_post_function_v(worker, worker_main);
9594
#else

test/wasm_worker/wait64_notify.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,8 @@ void run_test() {
3434
assert(ret == ATOMICS_WAIT_TIMED_OUT);
3535

3636
emscripten_out("Waiting for infinitely long should return 'ok'");
37-
ret = emscripten_atomic_wait_u64((int64_t*)&addr, 0x100000000ull, /*timeout=*/-1);
37+
emscripten_atomic_store_u64((void*)&addr, 0x300000000ull);
38+
ret = emscripten_atomic_wait_u64((int64_t*)&addr, 0x300000000ull, /*timeout=*/-1);
3839
assert(ret == ATOMICS_WAIT_OK);
3940

4041
emscripten_out("Test finished");
@@ -65,15 +66,14 @@ void* thread_main(void* arg) {
6566
#endif
6667

6768
EM_BOOL main_loop(double time, void *userData) {
68-
if (addr == 0x100000000ull) {
69+
if (addr == 0x300000000ull) {
6970
// Burn one second to make sure worker finishes its test.
7071
emscripten_out("main: seen worker running");
7172
double t0 = emscripten_performance_now();
7273
while(emscripten_performance_now() < t0 + 1000);
7374

7475
// Wake the waiter
7576
emscripten_out("main: waking worker");
76-
addr = 0x200000000ull;
7777
emscripten_atomic_notify((int32_t*)&addr, 1);
7878

7979
return EM_FALSE;

test/wasm_worker/wait64_notify.out

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,3 +8,4 @@ Waiting for >0 nanoseconds should return 'timed-out'
88
Waiting for infinitely long should return 'ok'
99
main: seen worker running
1010
main: waking worker
11+
Test finished

0 commit comments

Comments
 (0)