diff --git a/test/core/test_stdio_locking.c b/test/core/test_stdio_locking.c index 538c157e36236..e57edb494d40b 100644 --- a/test/core/test_stdio_locking.c +++ b/test/core/test_stdio_locking.c @@ -9,31 +9,69 @@ * musl/src/stdio/fwrite.c */ #include -#include #include -#include #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_WASM_WORKERS__ +#include +#include + +emscripten_wasm_worker_t worker[2]; + +void terminate_worker(void* userData) { + emscripten_terminate_all_wasm_workers(); + printf("main done\n"); +} +#elif defined(__EMSCRIPTEN_PTHREADS__) +#include pthread_t thread[2]; -char *char_repeat(int n, char c) { - char *dest = malloc(n + 1); +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 + +char* char_repeat(int n, char c) { + char* dest = malloc(n + 1); memset(dest, c, n); dest[n] = '\0'; return dest; } -void *thread_main(void *arg) { - char *msg = char_repeat(100, 'a'); +void thread_func(void) { + 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; } int main() { printf("in main\n"); - void *thread_rtn; +#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; rc = pthread_create(&thread[0], NULL, thread_main, NULL); @@ -51,5 +89,6 @@ int main() { assert(thread_rtn == 0); printf("main done\n"); +#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')