From 9c7767de09ff308b55e201c21f994b03091c2cbf Mon Sep 17 00:00:00 2001 From: Kleis Auke Wolthuizen Date: Mon, 14 Nov 2022 14:31:55 +0100 Subject: [PATCH 1/4] Reproduce issue #13194 on the Wasm Workers API See: https://github.com/emscripten-core/emscripten/pull/18171#issuecomment-1312793642 --- test/core/test_stdio_locking.c | 47 +++++++++++++++++++++++++++++----- test/test_core.py | 1 + 2 files changed, 42 insertions(+), 6 deletions(-) diff --git a/test/core/test_stdio_locking.c b/test/core/test_stdio_locking.c index 538c157e36236..446f96c7271eb 100644 --- a/test/core/test_stdio_locking.c +++ b/test/core/test_stdio_locking.c @@ -9,12 +9,30 @@ * musl/src/stdio/fwrite.c */ #include -#include #include #include #include +#ifdef __EMSCRIPTEN_PTHREADS__ +#include + pthread_t thread[2]; +#elif defined(__EMSCRIPTEN_WASM_WORKERS__) +#include +#include + +emscripten_wasm_worker_t worker[2]; +#else +#error Expected to be compiled with either -sWASM_WORKERS or -pthread. +#endif + +#ifndef __wasm_atomics__ +#error Expected to be compiled with -matomics. +#endif + +#ifndef __wasm_bulk_memory__ +#error Expected to be compiled with -mbulk-memory. +#endif char *char_repeat(int n, char c) { char *dest = malloc(n + 1); @@ -23,23 +41,31 @@ char *char_repeat(int n, char c) { return dest; } -void *thread_main(void *arg) { +void thread_main() { char *msg = char_repeat(100, 'a'); for (int i = 0; i < 10; ++i) - printf("%s\n", msg); + printf("%s\n", msg); free(msg); - return 0; } +#ifdef __EMSCRIPTEN_WASM_WORKERS__ +void terminate_worker(void *userData) +{ + emscripten_terminate_all_wasm_workers(); + printf("main done\n"); +} +#endif + int main() { printf("in main\n"); +#ifdef __EMSCRIPTEN_PTHREADS__ void *thread_rtn; int rc; - rc = pthread_create(&thread[0], NULL, thread_main, NULL); + rc = pthread_create(&thread[0], NULL, (void* (*)(void*))thread_main, NULL); assert(rc == 0); - rc = pthread_create(&thread[1], NULL, thread_main, NULL); + rc = pthread_create(&thread[1], NULL, (void* (*)(void*))thread_main, NULL); assert(rc == 0); rc = pthread_join(thread[0], &thread_rtn); @@ -51,5 +77,14 @@ int main() { assert(thread_rtn == 0); printf("main done\n"); +#else + worker[0] = emscripten_malloc_wasm_worker(/*stack size: */1024); + worker[1] = emscripten_malloc_wasm_worker(/*stack size: */1024); + emscripten_wasm_worker_post_function_v(worker[0], (void (*))thread_main); + emscripten_wasm_worker_post_function_v(worker[1], (void (*))thread_main); + + // Terminate both workers after a small delay + emscripten_set_timeout(terminate_worker, 1000, 0); +#endif return 0; } diff --git a/test/test_core.py b/test/test_core.py index c517d88fb4552..a26fd397d1519 100644 --- a/test/test_core.py +++ b/test/test_core.py @@ -9242,6 +9242,7 @@ def test_emscripten_futexes(self): self.do_core_test('pthread/emscripten_futexes.c', cflags=['-pthread', '-Wno-nonnull']) @requires_pthreads + @also_with_wasm_workers def test_stdio_locking(self): self.set_setting('PTHREAD_POOL_SIZE', '2') self.do_core_test('test_stdio_locking.c') From e4e8ec909ced186bafdfec0076db114e9f28ef18 Mon Sep 17 00:00:00 2001 From: Kleis Auke Wolthuizen Date: Tue, 8 Oct 2024 18:48:10 +0200 Subject: [PATCH 2/4] Cleanup --- test/core/test_stdio_locking.c | 47 +++++++++++++++++++--------------- 1 file changed, 26 insertions(+), 21 deletions(-) diff --git a/test/core/test_stdio_locking.c b/test/core/test_stdio_locking.c index 446f96c7271eb..e7657a8847fb0 100644 --- a/test/core/test_stdio_locking.c +++ b/test/core/test_stdio_locking.c @@ -13,27 +13,40 @@ #include #include +#ifndef __wasm_atomics__ +#error Expected to be compiled with -matomics. +#endif + +#ifndef __wasm_bulk_memory__ +#error Expected to be compiled with -mbulk-memory. +#endif + #ifdef __EMSCRIPTEN_PTHREADS__ #include pthread_t thread[2]; + +void thread_func(void); + +void *thread_main(void *arg) { + thread_func(); + return 0; +} #elif defined(__EMSCRIPTEN_WASM_WORKERS__) #include #include emscripten_wasm_worker_t worker[2]; + +void terminate_worker(void *userData) +{ + emscripten_terminate_all_wasm_workers(); + printf("main done\n"); +} #else #error Expected to be compiled with either -sWASM_WORKERS or -pthread. #endif -#ifndef __wasm_atomics__ -#error Expected to be compiled with -matomics. -#endif - -#ifndef __wasm_bulk_memory__ -#error Expected to be compiled with -mbulk-memory. -#endif - char *char_repeat(int n, char c) { char *dest = malloc(n + 1); memset(dest, c, n); @@ -41,31 +54,23 @@ char *char_repeat(int n, char c) { return dest; } -void thread_main() { +void thread_func(void) { char *msg = char_repeat(100, 'a'); for (int i = 0; i < 10; ++i) printf("%s\n", msg); free(msg); } -#ifdef __EMSCRIPTEN_WASM_WORKERS__ -void terminate_worker(void *userData) -{ - emscripten_terminate_all_wasm_workers(); - printf("main done\n"); -} -#endif - int main() { printf("in main\n"); #ifdef __EMSCRIPTEN_PTHREADS__ void *thread_rtn; int rc; - rc = pthread_create(&thread[0], NULL, (void* (*)(void*))thread_main, NULL); + rc = pthread_create(&thread[0], NULL, thread_main, NULL); assert(rc == 0); - rc = pthread_create(&thread[1], NULL, (void* (*)(void*))thread_main, NULL); + rc = pthread_create(&thread[1], NULL, thread_main, NULL); assert(rc == 0); rc = pthread_join(thread[0], &thread_rtn); @@ -80,8 +85,8 @@ int main() { #else worker[0] = emscripten_malloc_wasm_worker(/*stack size: */1024); worker[1] = emscripten_malloc_wasm_worker(/*stack size: */1024); - emscripten_wasm_worker_post_function_v(worker[0], (void (*))thread_main); - emscripten_wasm_worker_post_function_v(worker[1], (void (*))thread_main); + emscripten_wasm_worker_post_function_v(worker[0], thread_func); + emscripten_wasm_worker_post_function_v(worker[1], thread_func); // Terminate both workers after a small delay emscripten_set_timeout(terminate_worker, 1000, 0); From f7af41c8fd1526eb991739b3acee634b047c3859 Mon Sep 17 00:00:00 2001 From: Kleis Auke Wolthuizen Date: Sat, 14 Mar 2026 11:25:50 +0100 Subject: [PATCH 3/4] Update after dc80f64 --- test/core/test_stdio_locking.c | 42 +++++++++++++++++----------------- 1 file changed, 21 insertions(+), 21 deletions(-) diff --git a/test/core/test_stdio_locking.c b/test/core/test_stdio_locking.c index e7657a8847fb0..29654e13c3d7a 100644 --- a/test/core/test_stdio_locking.c +++ b/test/core/test_stdio_locking.c @@ -21,18 +21,7 @@ #error Expected to be compiled with -mbulk-memory. #endif -#ifdef __EMSCRIPTEN_PTHREADS__ -#include - -pthread_t thread[2]; - -void thread_func(void); - -void *thread_main(void *arg) { - thread_func(); - return 0; -} -#elif defined(__EMSCRIPTEN_WASM_WORKERS__) +#ifdef __EMSCRIPTEN_WASM_WORKERS__ #include #include @@ -43,6 +32,17 @@ void terminate_worker(void *userData) emscripten_terminate_all_wasm_workers(); printf("main done\n"); } +#elif defined(__EMSCRIPTEN_PTHREADS__) +#include + +pthread_t thread[2]; + +void thread_func(void); + +void *thread_main(void *arg) { + thread_func(); + return 0; +} #else #error Expected to be compiled with either -sWASM_WORKERS or -pthread. #endif @@ -63,7 +63,15 @@ void thread_func(void) { int main() { printf("in main\n"); -#ifdef __EMSCRIPTEN_PTHREADS__ +#ifdef __EMSCRIPTEN_WASM_WORKERS__ + worker[0] = emscripten_malloc_wasm_worker(/*stack size: */1024); + worker[1] = emscripten_malloc_wasm_worker(/*stack size: */1024); + emscripten_wasm_worker_post_function_v(worker[0], thread_func); + emscripten_wasm_worker_post_function_v(worker[1], thread_func); + + // Terminate both workers after a small delay + emscripten_set_timeout(terminate_worker, 1000, 0); +#else void *thread_rtn; int rc; @@ -82,14 +90,6 @@ int main() { assert(thread_rtn == 0); printf("main done\n"); -#else - worker[0] = emscripten_malloc_wasm_worker(/*stack size: */1024); - worker[1] = emscripten_malloc_wasm_worker(/*stack size: */1024); - emscripten_wasm_worker_post_function_v(worker[0], thread_func); - emscripten_wasm_worker_post_function_v(worker[1], thread_func); - - // Terminate both workers after a small delay - emscripten_set_timeout(terminate_worker, 1000, 0); #endif return 0; } From ce2c6c614c096e473e05d525e1324eb6349aec97 Mon Sep 17 00:00:00 2001 From: Kleis Auke Wolthuizen Date: Sat, 14 Mar 2026 11:31:21 +0100 Subject: [PATCH 4/4] Appease clang-format --- test/core/test_stdio_locking.c | 21 ++++++++++----------- 1 file changed, 10 insertions(+), 11 deletions(-) diff --git a/test/core/test_stdio_locking.c b/test/core/test_stdio_locking.c index 29654e13c3d7a..e57edb494d40b 100644 --- a/test/core/test_stdio_locking.c +++ b/test/core/test_stdio_locking.c @@ -10,8 +10,8 @@ */ #include #include -#include #include +#include #ifndef __wasm_atomics__ #error Expected to be compiled with -matomics. @@ -22,13 +22,12 @@ #endif #ifdef __EMSCRIPTEN_WASM_WORKERS__ -#include #include +#include emscripten_wasm_worker_t worker[2]; -void terminate_worker(void *userData) -{ +void terminate_worker(void* userData) { emscripten_terminate_all_wasm_workers(); printf("main done\n"); } @@ -39,7 +38,7 @@ pthread_t thread[2]; void thread_func(void); -void *thread_main(void *arg) { +void* thread_main(void* arg) { thread_func(); return 0; } @@ -47,15 +46,15 @@ void *thread_main(void *arg) { #error Expected to be compiled with either -sWASM_WORKERS or -pthread. #endif -char *char_repeat(int n, char c) { - char *dest = malloc(n + 1); +char* char_repeat(int n, char c) { + char* dest = malloc(n + 1); memset(dest, c, n); dest[n] = '\0'; return dest; } void thread_func(void) { - char *msg = char_repeat(100, 'a'); + char* msg = char_repeat(100, 'a'); for (int i = 0; i < 10; ++i) printf("%s\n", msg); free(msg); @@ -64,15 +63,15 @@ void thread_func(void) { int main() { printf("in main\n"); #ifdef __EMSCRIPTEN_WASM_WORKERS__ - worker[0] = emscripten_malloc_wasm_worker(/*stack size: */1024); - worker[1] = emscripten_malloc_wasm_worker(/*stack size: */1024); + worker[0] = emscripten_malloc_wasm_worker(/*stack size: */ 1024); + worker[1] = emscripten_malloc_wasm_worker(/*stack size: */ 1024); emscripten_wasm_worker_post_function_v(worker[0], thread_func); emscripten_wasm_worker_post_function_v(worker[1], thread_func); // Terminate both workers after a small delay emscripten_set_timeout(terminate_worker, 1000, 0); #else - void *thread_rtn; + void* thread_rtn; int rc; rc = pthread_create(&thread[0], NULL, thread_main, NULL);