Skip to content
Closed
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
12 changes: 6 additions & 6 deletions src/library_pthread.js
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
Expand Down Expand Up @@ -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.
Expand Down
2 changes: 1 addition & 1 deletion src/library_sigs.js
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down Expand Up @@ -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',
Expand Down
9 changes: 9 additions & 0 deletions system/include/emscripten/threading.h
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
4 changes: 1 addition & 3 deletions system/lib/libc/crt1_proxy_main.c
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,6 @@
#include <emscripten/threading.h>
#include <emscripten/eventloop.h>

#include "threading_internal.h"

static int _main_argc;
static char** _main_argv;

Expand Down Expand Up @@ -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;
}
2 changes: 1 addition & 1 deletion system/lib/pthread/emscripten_yield.c
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
9 changes: 0 additions & 9 deletions system/lib/pthread/threading_internal.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion test/other/codesize/test_codesize_minimal_pthreads.gzsize
Original file line number Diff line number Diff line change
@@ -1 +1 @@
4163
4170
2 changes: 1 addition & 1 deletion test/other/codesize/test_codesize_minimal_pthreads.imports
Original file line number Diff line number Diff line change
@@ -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)
Expand Down
2 changes: 1 addition & 1 deletion test/other/codesize/test_codesize_minimal_pthreads.sent
Original file line number Diff line number Diff line change
@@ -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)
Expand Down
Loading