diff --git a/src/library_pthread.js b/src/library_pthread.js index 64d276410d3fa..01e125f0e979b 100644 --- a/src/library_pthread.js +++ b/src/library_pthread.js @@ -188,11 +188,11 @@ var LibraryPThread = { // worker pool as an unused worker. worker.pthread_ptr = 0; -#if ENVIRONMENT_MAY_BE_NODE && PROXY_TO_PTHREAD +#if ENVIRONMENT_MAY_BE_NODE if (ENVIRONMENT_IS_NODE) { - // Once the proxied main thread has finished, mark it as weakly - // referenced so that its existence does not prevent Node.js from - // exiting. This has no effect if the worker is already weakly + // Once a pthread has finished and the worker becomes idle, mark it + // as weakly referenced so that its existence does not prevent Node.js + // from exiting. This has no effect if the worker is already weakly // referenced. worker.unref(); } @@ -526,9 +526,9 @@ var LibraryPThread = { else postMessage({ cmd: 'cleanupThread', thread }); }, - _emscripten_thread_set_strongref: (thread) => { + emscripten_thread_set_strongref: (thread) => { // Called when a thread needs to be strongly referenced. - // Currently only used for: + // Internally used for: // - keeping the "main" thread alive in PROXY_TO_PTHREAD mode; // - crashed threads that needs to propagate the uncaught exception // back to the main thread. diff --git a/src/library_sigs.js b/src/library_sigs.js index 24537748afd4c..0f9776c074dc1 100644 --- a/src/library_sigs.js +++ b/src/library_sigs.js @@ -333,7 +333,6 @@ sigs = { _emscripten_thread_cleanup__sig: 'vp', _emscripten_thread_exit_joinable__sig: 'vp', _emscripten_thread_mailbox_await__sig: 'vp', - _emscripten_thread_set_strongref__sig: 'vp', _emscripten_throw_longjmp__sig: 'v', _emval_as__sig: 'dppp', _emval_as_int64__sig: 'jpp', @@ -813,6 +812,7 @@ sigs = { emscripten_supports_offscreencanvas__sig: 'i', emscripten_terminate_all_wasm_workers__sig: 'v', emscripten_terminate_wasm_worker__sig: 'vi', + emscripten_thread_set_strongref__sig: 'vp', emscripten_throw_number__sig: 'vd', emscripten_throw_string__sig: 'vp', emscripten_trace_annotate_address_type__sig: 'vpp', diff --git a/system/include/emscripten/threading.h b/system/include/emscripten/threading.h index f4f68d288db42..932c9a90501d4 100644 --- a/system/include/emscripten/threading.h +++ b/system/include/emscripten/threading.h @@ -77,6 +77,15 @@ pthread_t emscripten_main_runtime_thread_id(void); // Asyncify builds. void emscripten_thread_sleep(double msecs); +// Marks the given thread as strongly referenced. This is used to prevent the +// Node.js application from exiting as long as there are strongly referenced +// threads still running. Normally you don't need to call this function, and +// the pthread behaviour will match native in that background threads won't +// keep runtime alive, but waiting for them via e.g. pthread_join will. +// However, this is useful for features like PROXY_TO_PTHREAD where we want to +// keep running as long as the detached pthread is. +void emscripten_thread_set_strongref(pthread_t thread); + // Sets the name of the given thread. Pass pthread_self() as the thread ID to // set the name of the calling thread. // The name parameter is a UTF-8 encoded string which is truncated to 32 bytes. diff --git a/system/lib/libc/crt1_proxy_main.c b/system/lib/libc/crt1_proxy_main.c index 2d178ca2e473a..e022164124e4c 100644 --- a/system/lib/libc/crt1_proxy_main.c +++ b/system/lib/libc/crt1_proxy_main.c @@ -13,8 +13,6 @@ #include #include -#include "threading_internal.h" - static int _main_argc; static char** _main_argv; @@ -54,7 +52,7 @@ EMSCRIPTEN_KEEPALIVE int _emscripten_proxy_main(int argc, char** argv) { if (rc == 0) { // Mark the thread as strongly referenced, so that Node.js doesn't exit // while the pthread is running. - _emscripten_thread_set_strongref(thread); + emscripten_thread_set_strongref(thread); } return rc; } diff --git a/system/lib/pthread/emscripten_yield.c b/system/lib/pthread/emscripten_yield.c index 70c20fec55fd9..71b200b0fecda 100644 --- a/system/lib/pthread/emscripten_yield.c +++ b/system/lib/pthread/emscripten_yield.c @@ -32,7 +32,7 @@ void _emscripten_yield(double now) { // Mark the crashed thread as strongly referenced so that Node.js doesn't // exit while the pthread is propagating the uncaught exception back to // the main thread. - _emscripten_thread_set_strongref(crashed_thread_id); + emscripten_thread_set_strongref(crashed_thread_id); // Return the event loop so we can handle the message from the crashed // thread. emscripten_exit_with_live_runtime(); diff --git a/system/lib/pthread/threading_internal.h b/system/lib/pthread/threading_internal.h index eae455944d578..de92a039aaeac 100644 --- a/system/lib/pthread/threading_internal.h +++ b/system/lib/pthread/threading_internal.h @@ -50,15 +50,6 @@ void _emscripten_thread_cleanup(pthread_t thread); hidden void* _emscripten_tls_init(void); hidden void _emscripten_tls_free(void); -// Marks the given thread as strongly referenced. This is used to prevent the -// Node.js application from exiting as long as there are strongly referenced -// threads still running. Normally you don't need to call this function, and -// the pthread behaviour will match native in that background threads won't -// keep runtime alive, but waiting for them via e.g. pthread_join will. -// However, this is useful for features like PROXY_TO_PTHREAD where we want to -// keep running as long as the detached pthread is. -void _emscripten_thread_set_strongref(pthread_t thread); - // Checks certain structural invariants. This allows us to detect when // already-freed threads are used in some APIs. Technically this is undefined // behaviour, but we have a couple of places where we add these checks so that diff --git a/test/other/codesize/test_codesize_minimal_pthreads.gzsize b/test/other/codesize/test_codesize_minimal_pthreads.gzsize index f3c2b2fc06522..f8f7dadb44f93 100644 --- a/test/other/codesize/test_codesize_minimal_pthreads.gzsize +++ b/test/other/codesize/test_codesize_minimal_pthreads.gzsize @@ -1 +1 @@ -4163 +4170 diff --git a/test/other/codesize/test_codesize_minimal_pthreads.imports b/test/other/codesize/test_codesize_minimal_pthreads.imports index 8fb7da4f6b7e7..6a0cdb9e13970 100644 --- a/test/other/codesize/test_codesize_minimal_pthreads.imports +++ b/test/other/codesize/test_codesize_minimal_pthreads.imports @@ -1,7 +1,7 @@ a (memory) b (emscripten_get_now) c (exit) -d (_emscripten_thread_set_strongref) +d (emscripten_thread_set_strongref) e (fd_write) f (emscripten_runtime_keepalive_check) g (emscripten_resize_heap) diff --git a/test/other/codesize/test_codesize_minimal_pthreads.sent b/test/other/codesize/test_codesize_minimal_pthreads.sent index 8fb7da4f6b7e7..6a0cdb9e13970 100644 --- a/test/other/codesize/test_codesize_minimal_pthreads.sent +++ b/test/other/codesize/test_codesize_minimal_pthreads.sent @@ -1,7 +1,7 @@ a (memory) b (emscripten_get_now) c (exit) -d (_emscripten_thread_set_strongref) +d (emscripten_thread_set_strongref) e (fd_write) f (emscripten_runtime_keepalive_check) g (emscripten_resize_heap)