Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Reproduce issue #13194 on the Wasm Workers API #18201

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .circleci/config.yml
Original file line number Diff line number Diff line change
@@ -516,7 +516,7 @@ jobs:
asan.test_asyncify_longjmp
asan.test_pthread_run_on_main_thread
lsan.test_dylink_dso_needed
lsan.test_stdio_locking
lsan.test_stdio_locking_pthread
lsan.test_dlfcn_basic
lsan.test_pthread_create
lsan.test_pthread_exit_main_stub
48 changes: 44 additions & 4 deletions test/core/test_stdio_locking.c
Original file line number Diff line number Diff line change
@@ -9,30 +9,61 @@
* musl/src/stdio/fwrite.c
*/
#include <assert.h>
#include <pthread.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>

#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.h>

pthread_t thread[2];

void thread_func(void);

void *thread_main(void *arg) {
thread_func();
return 0;
}
#elif defined(__EMSCRIPTEN_WASM_WORKERS__)
#include <emscripten/wasm_worker.h>
#include <emscripten/eventloop.h>

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

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) {
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");
#ifdef __EMSCRIPTEN_PTHREADS__
void *thread_rtn;
int rc;

@@ -51,5 +82,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], 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;
}
8 changes: 7 additions & 1 deletion test/test_core.py
Original file line number Diff line number Diff line change
@@ -9264,10 +9264,16 @@ def test_emscripten_futexes(self):
self.do_run_in_out_file_test('core/pthread/emscripten_futexes.c')

@node_pthreads
def test_stdio_locking(self):
def test_stdio_locking_pthread(self):
self.set_setting('PTHREAD_POOL_SIZE', '2')
self.do_run_in_out_file_test('core/test_stdio_locking.c')

# Cannot use @node_pthreads here, since that links with -pthread
def test_stdio_locking_wasm_workers(self):
self.set_setting('WASM_WORKERS')
self.node_args += shared.node_pthread_flags(self.require_node())
self.do_run_in_out_file_test('core/test_stdio_locking.c')

@with_dylink_reversed
@node_pthreads
def test_pthread_dylink_basics(self):
Loading
Oops, something went wrong.