Skip to content
Merged
Show file tree
Hide file tree
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 test/test_browser.py
Original file line number Diff line number Diff line change
Expand Up @@ -5078,7 +5078,7 @@ def test_wasm_worker_malloc(self):
# Tests Wasm Worker+pthreads simultaneously
@also_with_minimal_runtime
def test_wasm_worker_and_pthreads(self):
self.btest('wasm_worker/wasm_worker_and_pthread.c', expected='0', cflags=['-sWASM_WORKERS', '-pthread', '-sPTHREAD_POOL_SIZE=1'])
self.btest_exit('wasm_worker/wasm_worker_and_pthread.c', cflags=['-sWASM_WORKERS', '-pthread', '-sPTHREAD_POOL_SIZE=1'])

# Tests emscripten_wasm_worker_self_id() function
@also_with_minimal_runtime
Expand Down
4 changes: 4 additions & 0 deletions test/test_other.py
Original file line number Diff line number Diff line change
Expand Up @@ -13582,6 +13582,10 @@ def test_wasm_worker_preprocessor_flags(self):
def test_wasm_worker_pthread_api_usage(self):
self.assert_fail([EMCC, test_file('wasm_worker/wasm_worker_pthread_api_usage.c'), '-sWASM_WORKERS'], 'undefined symbol: pthread_mutex_lock')

@also_with_minimal_runtime
def test_wasm_worker_and_pthread(self):
self.do_runf('wasm_worker/wasm_worker_and_pthread.c', 'done\n', cflags=['-sWASM_WORKERS', '-pthread'])

@also_with_minimal_runtime
def test_wasm_worker_cxx_init(self):
self.do_run_in_out_file_test('wasm_worker/wasm_worker_cxx_init.cpp', cflags=['-sWASM_WORKERS'])
Expand Down
26 changes: 20 additions & 6 deletions test/wasm_worker/wasm_worker_and_pthread.c
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,21 @@ EM_JS(int, am_i_wasm_worker, (), {
return ENVIRONMENT_IS_WASM_WORKER;
});

void do_exit() {
emscripten_out("done");
emscripten_terminate_all_wasm_workers();
emscripten_force_exit(0);
}

void *thread_main(void *arg) {
pthread_tid = gettid();
pthread_ptr = __builtin_thread_pointer();
emscripten_outf("hello from pthread! (tid=%d) (ptr=%p)", pthread_tid, pthread_ptr);
emscripten_outf("thread_main: gettid=%d", pthread_tid);
emscripten_outf("thread_main: __builtin_thread_pointer=%p", pthread_ptr);
emscripten_outf("thread_main: pthread_self=%p", pthread_self());
assert(pthread_tid && pthread_tid > main_tid);
assert(pthread_ptr && pthread_ptr != main_ptr);
assert(pthread_self() != 0);
assert(am_i_pthread());
assert(!am_i_wasm_worker());
assert(!emscripten_current_thread_is_wasm_worker());
Expand All @@ -39,23 +48,28 @@ void *thread_main(void *arg) {
void worker_main() {
worker_tid = gettid();
worker_ptr = __builtin_thread_pointer();
emscripten_outf("hello from wasm worker! (tid=%d) (ptr=%p)", worker_tid, worker_ptr);
emscripten_outf("worker_main: gettid=%d", worker_tid);
emscripten_outf("worker_main: __builtin_thread_pointer=%p", worker_ptr);
emscripten_outf("worker_main: pthread_self=%p", pthread_self());
assert(worker_tid && worker_tid > pthread_tid);
assert(worker_ptr && worker_ptr != pthread_ptr);
// Currently pthead_self return NULL when called from a wasm worker.
// See https://github.com/emscripten-core/emscripten/issues/26631
assert(pthread_self() == 0);
assert(!am_i_pthread());
assert(am_i_wasm_worker());
assert(emscripten_current_thread_is_wasm_worker());
assert(emscripten_wasm_worker_self_id() != 0);

#ifdef REPORT_RESULT
REPORT_RESULT(0);
#endif
emscripten_wasm_worker_post_function_v(EMSCRIPTEN_WASM_WORKER_ID_PARENT, do_exit);
}

int main() {
main_tid = gettid();
main_ptr = __builtin_thread_pointer();
emscripten_outf("in main (tid=%d) (ptr=%p)", main_tid, main_ptr);
emscripten_outf("main: gettid=%d", main_tid);
emscripten_outf("main: __builtin_thread_pointer=%p", main_ptr);
emscripten_outf("main: pthread_self=%p", pthread_self());
assert(main_tid > 0);
pthread_t thread;
pthread_create(&thread, NULL, thread_main, NULL);
Expand Down
Loading