Skip to content

Commit 2f1b94b

Browse files
committed
Reproduce issue #13194 on the Wasm Workers API
See: #18171 (comment)
1 parent 4db36c1 commit 2f1b94b

File tree

3 files changed

+49
-8
lines changed

3 files changed

+49
-8
lines changed

.circleci/config.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -516,7 +516,7 @@ jobs:
516516
asan.test_asyncify_longjmp
517517
asan.test_pthread_run_on_main_thread
518518
lsan.test_dylink_dso_needed
519-
lsan.test_stdio_locking
519+
lsan.test_stdio_locking_pthread
520520
lsan.test_dlfcn_basic
521521
lsan.test_pthread_create
522522
lsan.test_pthread_exit_main_stub

test/core/test_stdio_locking.c

Lines changed: 41 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,30 @@
99
* musl/src/stdio/fwrite.c
1010
*/
1111
#include <assert.h>
12-
#include <pthread.h>
1312
#include <stdio.h>
1413
#include <string.h>
1514
#include <stdlib.h>
1615

16+
#ifdef __EMSCRIPTEN_PTHREADS__
17+
#include <pthread.h>
18+
1719
pthread_t thread[2];
20+
#elif defined(__EMSCRIPTEN_WASM_WORKERS__)
21+
#include <emscripten/wasm_worker.h>
22+
#include <emscripten/eventloop.h>
23+
24+
emscripten_wasm_worker_t worker[2];
25+
#else
26+
#error Expected to be compiled with either -sWASM_WORKERS or -pthread.
27+
#endif
28+
29+
#ifndef __wasm_atomics__
30+
#error Expected to be compiled with -matomics.
31+
#endif
32+
33+
#ifndef __wasm_bulk_memory__
34+
#error Expected to be compiled with -mbulk-memory.
35+
#endif
1836

1937
char *char_repeat(int n, char c) {
2038
char *dest = malloc(n + 1);
@@ -23,23 +41,31 @@ char *char_repeat(int n, char c) {
2341
return dest;
2442
}
2543

26-
void *thread_main(void *arg) {
44+
void thread_main() {
2745
char *msg = char_repeat(100, 'a');
2846
for (int i = 0; i < 10; ++i)
29-
printf("%s\n", msg);
47+
printf("%s\n", msg);
3048
free(msg);
31-
return 0;
3249
}
3350

51+
#ifdef __EMSCRIPTEN_WASM_WORKERS__
52+
void terminate_worker(void *userData)
53+
{
54+
emscripten_terminate_all_wasm_workers();
55+
printf("main done\n");
56+
}
57+
#endif
58+
3459
int main() {
3560
printf("in main\n");
61+
#ifdef __EMSCRIPTEN_PTHREADS__
3662
void *thread_rtn;
3763
int rc;
3864

39-
rc = pthread_create(&thread[0], NULL, thread_main, NULL);
65+
rc = pthread_create(&thread[0], NULL, (void* (*)(void*))thread_main, NULL);
4066
assert(rc == 0);
4167

42-
rc = pthread_create(&thread[1], NULL, thread_main, NULL);
68+
rc = pthread_create(&thread[1], NULL, (void* (*)(void*))thread_main, NULL);
4369
assert(rc == 0);
4470

4571
rc = pthread_join(thread[0], &thread_rtn);
@@ -51,5 +77,14 @@ int main() {
5177
assert(thread_rtn == 0);
5278

5379
printf("main done\n");
80+
#else
81+
worker[0] = emscripten_malloc_wasm_worker(/*stack size: */1024);
82+
worker[1] = emscripten_malloc_wasm_worker(/*stack size: */1024);
83+
emscripten_wasm_worker_post_function_v(worker[0], (void (*))thread_main);
84+
emscripten_wasm_worker_post_function_v(worker[1], (void (*))thread_main);
85+
86+
// Terminate both workers after a small delay
87+
emscripten_set_timeout(terminate_worker, 1000, 0);
88+
#endif
5489
return 0;
5590
}

test/test_core.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9264,10 +9264,16 @@ def test_emscripten_futexes(self):
92649264
self.do_run_in_out_file_test('core/pthread/emscripten_futexes.c')
92659265

92669266
@node_pthreads
9267-
def test_stdio_locking(self):
9267+
def test_stdio_locking_pthread(self):
92689268
self.set_setting('PTHREAD_POOL_SIZE', '2')
92699269
self.do_run_in_out_file_test('core/test_stdio_locking.c')
92709270

9271+
# Cannot use @node_pthreads here, since that links with -pthread
9272+
def test_stdio_locking_wasm_workers(self):
9273+
self.set_setting('WASM_WORKERS')
9274+
self.node_args += shared.node_pthread_flags(self.require_node())
9275+
self.do_run_in_out_file_test('core/test_stdio_locking.c')
9276+
92719277
@with_dylink_reversed
92729278
@node_pthreads
92739279
def test_pthread_dylink_basics(self):

0 commit comments

Comments
 (0)