diff --git a/test/test_browser.py b/test/test_browser.py index 20ed7d8d6ac18..452752064a92e 100644 --- a/test/test_browser.py +++ b/test/test_browser.py @@ -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 diff --git a/test/test_other.py b/test/test_other.py index 270fdc748a36b..89d2ac1846653 100644 --- a/test/test_other.py +++ b/test/test_other.py @@ -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']) diff --git a/test/wasm_worker/wasm_worker_and_pthread.c b/test/wasm_worker/wasm_worker_and_pthread.c index 27c5cee3ba2bc..1482d3a271ad5 100644 --- a/test/wasm_worker/wasm_worker_and_pthread.c +++ b/test/wasm_worker/wasm_worker_and_pthread.c @@ -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()); @@ -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);