Skip to content

Commit ef3562f

Browse files
committed
Cleanup wasm worker tests. NFC
- Use `emscripten_out` helper instead of EM_ASM(out(...)). - Remove tabs - Curly braces on the same line as keyword - Parameterize one test
1 parent afa356a commit ef3562f

29 files changed

+206
-291
lines changed

test/test_browser.py

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5011,9 +5011,13 @@ def test_wasm_worker_futex_wait(self, args):
50115011

50125012
# Tests Wasm Worker thread stack setup
50135013
@also_with_minimal_runtime
5014-
def test_wasm_worker_thread_stack(self):
5015-
for mode in (0, 1, 2):
5016-
self.btest('wasm_worker/thread_stack.c', expected='0', args=['-sWASM_WORKERS', f'-sSTACK_OVERFLOW_CHECK={mode}'])
5014+
@parameterized({
5015+
'0': (0,),
5016+
'1': (1,),
5017+
'2': (2,),
5018+
})
5019+
def test_wasm_worker_thread_stack(self, mode):
5020+
self.btest('wasm_worker/thread_stack.c', expected='0', args=['-sWASM_WORKERS', f'-sSTACK_OVERFLOW_CHECK={mode}'])
50175021

50185022
# Tests emscripten_malloc_wasm_worker() and emscripten_current_thread_is_wasm_worker() functions
50195023
@also_with_minimal_runtime

test/wasm_worker/c11__Thread_local.c

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,17 +5,15 @@
55

66
_Thread_local int __attribute__((aligned(64))) tls = 1;
77

8-
void main_thread_func()
9-
{
8+
void main_thread_func() {
109
assert(!emscripten_current_thread_is_wasm_worker());
1110
EM_ASM(out($0), tls);
1211
#ifdef REPORT_RESULT
1312
REPORT_RESULT(tls);
1413
#endif
1514
}
1615

17-
void worker_main()
18-
{
16+
void worker_main() {
1917
assert(emscripten_current_thread_is_wasm_worker());
2018
assert(((intptr_t)&tls % 64) == 0);
2119
assert(tls != 42);
@@ -27,8 +25,7 @@ void worker_main()
2725

2826
char stack[1024];
2927

30-
int main()
31-
{
28+
int main() {
3229
EM_ASM(out($0), tls);
3330
assert(((intptr_t)&tls % 64) == 0);
3431
assert(!emscripten_current_thread_is_wasm_worker());

test/wasm_worker/cancel_all_wait_asyncs.c

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,23 +9,20 @@ volatile int32_t addr = 1;
99

1010
bool testSucceeded = 1;
1111

12-
void asyncWaitFinishedShouldNotBeCalled(int32_t *ptr, uint32_t val, ATOMICS_WAIT_RESULT_T waitResult, void *userData)
13-
{
12+
void asyncWaitFinishedShouldNotBeCalled(int32_t *ptr, uint32_t val, ATOMICS_WAIT_RESULT_T waitResult, void *userData) {
1413
emscripten_console_log("asyncWaitFinishedShouldNotBeCalled");
1514
testSucceeded = 0;
1615
assert(0); // We should not reach here
1716
}
1817

19-
void asyncWaitFinishedShouldBeCalled(int32_t *ptr, uint32_t val, ATOMICS_WAIT_RESULT_T waitResult, void *userData)
20-
{
18+
void asyncWaitFinishedShouldBeCalled(int32_t *ptr, uint32_t val, ATOMICS_WAIT_RESULT_T waitResult, void *userData) {
2119
emscripten_console_log("asyncWaitFinishedShouldBeCalled");
2220
#ifdef REPORT_RESULT
2321
REPORT_RESULT(testSucceeded);
2422
#endif
2523
}
2624

27-
int main()
28-
{
25+
int main() {
2926
emscripten_console_log("Async waiting on address should give a wait token");
3027
ATOMICS_WAIT_TOKEN_T ret = emscripten_atomic_wait_async((int32_t*)&addr, 1, asyncWaitFinishedShouldNotBeCalled, (void*)42, EMSCRIPTEN_WAIT_ASYNC_INFINITY);
3128
assert(EMSCRIPTEN_IS_VALID_WAIT_TOKEN(ret));

test/wasm_worker/cancel_all_wait_asyncs_at_address.c

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,23 +9,20 @@ volatile int32_t addr = 1;
99

1010
bool testSucceeded = 1;
1111

12-
void asyncWaitFinishedShouldNotBeCalled(int32_t *ptr, uint32_t val, ATOMICS_WAIT_RESULT_T waitResult, void *userData)
13-
{
12+
void asyncWaitFinishedShouldNotBeCalled(int32_t *ptr, uint32_t val, ATOMICS_WAIT_RESULT_T waitResult, void *userData) {
1413
emscripten_console_log("asyncWaitFinishedShouldNotBeCalled");
1514
testSucceeded = 0;
1615
assert(0); // We should not reach here
1716
}
1817

19-
void asyncWaitFinishedShouldBeCalled(int32_t *ptr, uint32_t val, ATOMICS_WAIT_RESULT_T waitResult, void *userData)
20-
{
18+
void asyncWaitFinishedShouldBeCalled(int32_t *ptr, uint32_t val, ATOMICS_WAIT_RESULT_T waitResult, void *userData) {
2119
emscripten_console_log("asyncWaitFinishedShouldBeCalled");
2220
#ifdef REPORT_RESULT
2321
REPORT_RESULT(testSucceeded);
2422
#endif
2523
}
2624

27-
int main()
28-
{
25+
int main() {
2926
emscripten_console_log("Async waiting on address should give a wait token");
3027
ATOMICS_WAIT_TOKEN_T ret = emscripten_atomic_wait_async((int32_t*)&addr, 1, asyncWaitFinishedShouldNotBeCalled, (void*)42, EMSCRIPTEN_WAIT_ASYNC_INFINITY);
3128
assert(EMSCRIPTEN_IS_VALID_WAIT_TOKEN(ret));

test/wasm_worker/cancel_wait_async.c

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,23 +9,20 @@ volatile int32_t addr = 1;
99

1010
bool testSucceeded = 1;
1111

12-
void asyncWaitFinishedShouldNotBeCalled(int32_t *ptr, uint32_t val, ATOMICS_WAIT_RESULT_T waitResult, void *userData)
13-
{
12+
void asyncWaitFinishedShouldNotBeCalled(int32_t *ptr, uint32_t val, ATOMICS_WAIT_RESULT_T waitResult, void *userData) {
1413
emscripten_console_log("asyncWaitFinishedShouldNotBeCalled");
1514
testSucceeded = 0;
1615
assert(0); // We should not reach here
1716
}
1817

19-
void asyncWaitFinishedShouldBeCalled(int32_t *ptr, uint32_t val, ATOMICS_WAIT_RESULT_T waitResult, void *userData)
20-
{
18+
void asyncWaitFinishedShouldBeCalled(int32_t *ptr, uint32_t val, ATOMICS_WAIT_RESULT_T waitResult, void *userData) {
2119
emscripten_console_log("asyncWaitFinishedShouldBeCalled");
2220
#ifdef REPORT_RESULT
2321
REPORT_RESULT(testSucceeded);
2422
#endif
2523
}
2624

27-
int main()
28-
{
25+
int main() {
2926
emscripten_console_log("Async waiting on address should give a wait token");
3027
ATOMICS_WAIT_TOKEN_T ret = emscripten_atomic_wait_async((int32_t*)&addr, 1, asyncWaitFinishedShouldNotBeCalled, (void*)42, EMSCRIPTEN_WAIT_ASYNC_INFINITY);
3128
assert(EMSCRIPTEN_IS_VALID_WAIT_TOKEN(ret));

test/wasm_worker/gcc___Thread.c

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,17 +5,15 @@
55

66
__thread int tls = 1;
77

8-
void main_thread_func()
9-
{
8+
void main_thread_func() {
109
assert(!emscripten_current_thread_is_wasm_worker());
1110
EM_ASM(out($0), tls);
1211
#ifdef REPORT_RESULT
1312
REPORT_RESULT(tls);
1413
#endif
1514
}
1615

17-
void worker_main()
18-
{
16+
void worker_main() {
1917
assert(emscripten_current_thread_is_wasm_worker());
2018
assert(tls != 42);
2119
assert(tls != 0);
@@ -26,8 +24,7 @@ void worker_main()
2624

2725
char stack[1024];
2826

29-
int main()
30-
{
27+
int main() {
3128
EM_ASM(out($0), tls);
3229
assert(!emscripten_current_thread_is_wasm_worker());
3330
tls = 42;

test/wasm_worker/hardware_concurrency_is_lock_free.c

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,7 @@
44

55
// Test emscripten_navigator_hardware_concurrency() and emscripten_atomics_is_lock_free() functions
66

7-
void test()
8-
{
7+
void test() {
98
// Assume that test suite does have navigator.hardwareConcurrency.
109
assert(emscripten_navigator_hardware_concurrency() >= 2);
1110
assert(emscripten_atomics_is_lock_free(1));
@@ -17,8 +16,7 @@ void test()
1716
assert(!emscripten_atomics_is_lock_free(31));
1817
}
1918

20-
void worker_main()
21-
{
19+
void worker_main() {
2220
test();
2321
#ifdef REPORT_RESULT
2422
REPORT_RESULT(0);
@@ -27,8 +25,7 @@ void worker_main()
2725

2826
char stack[1024];
2927

30-
int main()
31-
{
28+
int main() {
3229
test();
3330
emscripten_wasm_worker_t worker = emscripten_create_wasm_worker(stack, sizeof(stack));
3431
emscripten_wasm_worker_post_function_v(worker, worker_main);

test/wasm_worker/hello_wasm_worker.c

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,16 +5,14 @@
55

66
// This is the code example in site/source/docs/api_reference/wasm_workers.rst
77

8-
void run_in_worker()
9-
{
8+
void run_in_worker() {
109
emscripten_console_log("Hello from wasm worker!\n");
1110
#ifdef REPORT_RESULT
1211
REPORT_RESULT(0);
1312
#endif
1413
}
1514

16-
int main()
17-
{
15+
int main() {
1816
emscripten_wasm_worker_t worker = emscripten_malloc_wasm_worker(/*stack size: */1024);
1917
assert(worker);
2018
emscripten_wasm_worker_post_function_v(worker, run_in_worker);

test/wasm_worker/lock_async_acquire.c

Lines changed: 21 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -17,36 +17,32 @@ bool testFinished = false;
1717
int numTimesMainThreadAcquiredLock = 0;
1818
int 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

6056
void 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
}

test/wasm_worker/lock_busyspin_wait_acquire.c

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -8,31 +8,37 @@
88

99
emscripten_lock_t lock = EMSCRIPTEN_LOCK_T_STATIC_INITIALIZER;
1010

11-
void test()
12-
{
13-
bool success = emscripten_lock_busyspin_wait_acquire(&lock, 0); // Expect no contention on free lock.
11+
void test() {
12+
// Expect no contention on free lock.
13+
bool success = emscripten_lock_busyspin_wait_acquire(&lock, 0);
1414
assert(success == true);
1515

1616
double t0 = emscripten_performance_now();
17-
success = emscripten_lock_busyspin_wait_acquire(&lock, 0); // We already have the lock, and emscripten_lock is not recursive, so this should fail.
17+
// We already have the lock, and emscripten_lock is not recursive, so this
18+
// should fail.
19+
success = emscripten_lock_busyspin_wait_acquire(&lock, 0);
1820
double t1 = emscripten_performance_now();
1921
assert(!success);
20-
assert(t1 - t0 < 25); // Shouldn't have taken too much time to try the lock.
22+
// Shouldn't have taken too much time to try the lock.
23+
assert(t1 - t0 < 25);
2124

2225
success = emscripten_lock_try_acquire(&lock);
2326
assert(!success); // We already have the lock.
2427

2528
t0 = emscripten_performance_now();
26-
success = emscripten_lock_busyspin_wait_acquire(&lock, 1000.0); // We already have the lock, and emscripten_lock is not recursive, so this should fail.
29+
// We already have the lock, and emscripten_lock is not recursive, so this
30+
// should fail.
31+
success = emscripten_lock_busyspin_wait_acquire(&lock, 1000.0);
2732
t1 = emscripten_performance_now();
2833
assert(!success);
29-
assert(t1 - t0 >= 900); // We should have waited for the requested duration for the lock.. apply some slack since timing can have some noise.
34+
// We should have waited for the requested duration for the lock.. apply some
35+
// slack since timing can have some noise.
36+
assert(t1 - t0 >= 900);
3037

3138
emscripten_lock_release(&lock);
3239
}
3340

34-
void worker_main()
35-
{
41+
void worker_main() {
3642
test();
3743
#ifdef REPORT_RESULT
3844
REPORT_RESULT(0);
@@ -41,8 +47,7 @@ void worker_main()
4147

4248
char stack[1024];
4349

44-
int main()
45-
{
50+
int main() {
4651
test();
4752

4853
emscripten_wasm_worker_t worker = emscripten_create_wasm_worker(stack, sizeof(stack));

0 commit comments

Comments
 (0)