From c84ebdc8e2287cba087318803fe72bb798f10da8 Mon Sep 17 00:00:00 2001 From: Sam Clegg Date: Wed, 7 Feb 2024 17:30:17 -0800 Subject: [PATCH] Fix deadlock in test_pthread_run_on_main_thread Fixes: #18210 --- .../pthread/test_pthread_run_on_main_thread.c | 22 ++++---- .../test_pthread_run_on_main_thread.out | 54 +++++++++++++++++++ test/test_browser.py | 1 - test/test_core.py | 4 ++ 4 files changed, 69 insertions(+), 12 deletions(-) create mode 100644 test/pthread/test_pthread_run_on_main_thread.out diff --git a/test/pthread/test_pthread_run_on_main_thread.c b/test/pthread/test_pthread_run_on_main_thread.c index 3657b10c9057..2c5370834fa7 100644 --- a/test/pthread/test_pthread_run_on_main_thread.c +++ b/test/pthread/test_pthread_run_on_main_thread.c @@ -11,35 +11,35 @@ int v_called = 0; void v() { assert(emscripten_is_main_runtime_thread()); - printf("Hello!\n"); + emscripten_outf("Hello!"); v_called = 1; } int vi_called = 0; void vi(int param0) { assert(emscripten_is_main_runtime_thread()); - printf("Hello %d!\n", param0); + emscripten_outf("Hello %d!", param0); vi_called = 1; } int vii_called = 0; void vii(int param0, int param1) { assert(emscripten_is_main_runtime_thread()); - printf("Hello %d %d!\n", param0, param1); + emscripten_outf("Hello %d %d!", param0, param1); vii_called = 1; } int viii_called = 0; void viii(int param0, int param1, int param2) { assert(emscripten_is_main_runtime_thread()); - printf("Hello %d %d %d!\n", param0, param1, param2); + emscripten_outf("Hello %d %d %d!", param0, param1, param2); viii_called = 1; } int i_called = 0; int i() { assert(emscripten_is_main_runtime_thread()); - printf("Hello i!\n"); + emscripten_outf("Hello i!"); i_called = 1; return 84; } @@ -47,7 +47,7 @@ int i() { int ii_called = 0; int ii(int param0) { assert(emscripten_is_main_runtime_thread()); - printf("Hello ii %d!\n", param0); + emscripten_outf("Hello ii %d!", param0); ii_called = 1; return 85; } @@ -55,7 +55,7 @@ int ii(int param0) { int iii_called = 0; int iii(int param0, int param1) { assert(emscripten_is_main_runtime_thread()); - printf("Hello iii %d %d!\n", param0, param1); + emscripten_outf("Hello iii %d %d!", param0, param1); iii_called = 1; return 86; } @@ -63,13 +63,13 @@ int iii(int param0, int param1) { int iiii_called = 0; int iiii(int param0, int param1, int param2) { assert(emscripten_is_main_runtime_thread()); - printf("Hello iiii %d %d %d!\n", param0, param1, param2); + emscripten_outf("Hello iiii %d %d %d!", param0, param1, param2); iiii_called = 1; return 87; } void test_sync() { - printf("Testing sync proxied runs:\n"); + emscripten_outf("Testing sync proxied runs:"); int ret; v_called = 0; emscripten_sync_run_in_main_runtime_thread(EM_FUNC_SIG_V, v); assert(v_called == 1); vi_called = 0; emscripten_sync_run_in_main_runtime_thread(EM_FUNC_SIG_VI, vi, 42); assert(vi_called == 1); @@ -82,7 +82,7 @@ void test_sync() { } void test_async() { - printf("Testing async proxied runs:\n"); + emscripten_outf("Testing async proxied runs:"); emscripten_async_run_in_main_runtime_thread(EM_FUNC_SIG_V, v); emscripten_async_run_in_main_runtime_thread(EM_FUNC_SIG_VI, vi, 42); emscripten_async_run_in_main_runtime_thread(EM_FUNC_SIG_VII, vii, 42, 43); @@ -94,7 +94,7 @@ void test_async() { } void test_async_waitable() { - printf("Testing waitable async proxied runs:\n"); + emscripten_outf("Testing waitable async proxied runs:"); em_queued_call *c1 = emscripten_async_waitable_run_in_main_runtime_thread(EM_FUNC_SIG_V, v); em_queued_call *c2 = emscripten_async_waitable_run_in_main_runtime_thread(EM_FUNC_SIG_VI, vi, 42); em_queued_call *c3 = emscripten_async_waitable_run_in_main_runtime_thread(EM_FUNC_SIG_VII, vii, 42, 43); diff --git a/test/pthread/test_pthread_run_on_main_thread.out b/test/pthread/test_pthread_run_on_main_thread.out new file mode 100644 index 000000000000..0ef12b8aada5 --- /dev/null +++ b/test/pthread/test_pthread_run_on_main_thread.out @@ -0,0 +1,54 @@ +Testing sync proxied runs: +Hello! +Hello 42! +Hello 42 43! +Hello 42 43 44! +Hello i! +Hello ii 42! +Hello iii 42 43! +Hello iiii 42 43 44! +Testing waitable async proxied runs: +Hello! +Hello 42! +Hello 42 43! +Hello 42 43 44! +Hello i! +Hello ii 42! +Hello iii 42 43! +Hello iiii 42 43 44! +Testing sync proxied runs: +Hello! +Hello 42! +Hello 42 43! +Hello 42 43 44! +Hello i! +Hello ii 42! +Hello iii 42 43! +Hello iiii 42 43 44! +Testing async proxied runs: +Testing waitable async proxied runs: +Hello! +Hello 42! +Hello 42 43! +Hello 42 43 44! +Hello i! +Hello ii 42! +Hello iii 42 43! +Hello iiii 42 43 44! +Hello! +Hello 42! +Hello 42 43! +Hello 42 43 44! +Hello i! +Hello ii 42! +Hello iii 42 43! +Hello iiii 42 43 44! +Testing async proxied runs: +Hello! +Hello 42! +Hello 42 43! +Hello 42 43 44! +Hello i! +Hello ii 42! +Hello iii 42 43! +Hello iiii 42 43 44! diff --git a/test/test_browser.py b/test/test_browser.py index f05433615523..040ad9d12339 100644 --- a/test/test_browser.py +++ b/test/test_browser.py @@ -4249,7 +4249,6 @@ def test_pthread_gauge_available_memory(self, args): # Test that the proxying operations of user code from pthreads to main thread # work - @disabled('https://github.com/emscripten-core/emscripten/issues/18210') @requires_threads def test_pthread_run_on_main_thread(self): self.btest_exit('pthread/test_pthread_run_on_main_thread.c', args=['-O3', '-pthread', '-sPTHREAD_POOL_SIZE']) diff --git a/test/test_core.py b/test/test_core.py index 52f9cc2f565c..fef4b291f49c 100644 --- a/test/test_core.py +++ b/test/test_core.py @@ -2639,6 +2639,10 @@ def test_pthread_wait_async(self): self.set_setting('PROXY_TO_PTHREAD') self.do_run_in_out_file_test('atomic/test_wait_async.c') + @node_pthreads + def test_pthread_run_on_main_thread(self): + self.do_run_in_out_file_test('pthread/test_pthread_run_on_main_thread.c') + def test_tcgetattr(self): self.do_runf('termios/test_tcgetattr.c', 'success')