Skip to content

Commit 272673b

Browse files
committed
Move __cxa_thread_atexit to native code
Rather than using a separate JS array use pthread TLS key which get cleaned during `__pthread_tsd_run_dtors`. Followup to #14484 and #14464 which both move more of the cleanup handling for threads and processes onto the native side.
1 parent f908904 commit 272673b

7 files changed

+8
-37
lines changed

src/library_pthread.js

Lines changed: 0 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,6 @@ var LibraryPThread = {
5252
},
5353
// Maps pthread_t to pthread info objects
5454
pthreads: {},
55-
threadExitHandlers: [], // An array of C functions to run when this thread exits.
5655

5756
#if PTHREADS_PROFILING
5857
createProfilerBlock: function(pthreadPtr) {
@@ -985,39 +984,12 @@ var LibraryPThread = {
985984
return 0;
986985
},
987986

988-
__pthread_exit_run_handlers__deps: ['exit'],
989-
__pthread_exit_run_handlers: function(status) {
990-
// Called from pthread_exit, either when called explicitly called
991-
// by programmer, or implicitly when leaving the thread main function.
992-
//
993-
// Note: in theory we would like to return any offscreen canvases back to
994-
// the main thread, but if we ever fetched a rendering context for them that
995-
// would not be valid, so we don't try.
996-
997-
#if PTHREADS_DEBUG
998-
var tb = _pthread_self();
999-
assert(tb);
1000-
err('Pthread 0x' + tb.toString(16) + ' exited.');
1001-
#endif
1002-
1003-
while (PThread.threadExitHandlers.length > 0) {
1004-
PThread.threadExitHandlers.pop()();
1005-
}
1006-
},
1007-
1008987
__pthread_detached_exit: function() {
1009988
// Called at the end of pthread_exit (which occurs also when leaving the
1010989
// thread main function) if an only if the thread is in a detached state.
1011990
postMessage({ 'cmd': 'detachedExit' });
1012991
},
1013992

1014-
__cxa_thread_atexit__sig: 'vii',
1015-
__cxa_thread_atexit: function(routine, arg) {
1016-
PThread.threadExitHandlers.push(function() { {{{ makeDynCall('vi', 'routine') }}}(arg) });
1017-
},
1018-
__cxa_thread_atexit_impl: '__cxa_thread_atexit',
1019-
1020-
1021993
// Returns 0 on success, or one of the values -ETIMEDOUT, -EWOULDBLOCK or -EINVAL on error.
1022994
emscripten_futex_wait__deps: ['emscripten_main_thread_process_queued_calls'],
1023995
emscripten_futex_wait: function(addr, val, timeout) {

system/lib/libcxxabi/src/cxa_thread_atexit.cpp

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -112,9 +112,14 @@ extern "C" {
112112
#ifdef HAVE___CXA_THREAD_ATEXIT_IMPL
113113
return __cxa_thread_atexit_impl(dtor, obj, dso_symbol);
114114
#else
115+
#ifndef __EMSCRIPTEN__
116+
// Emscripten doesn't fully support weak undefined symbols yet
117+
// https://github.com/emscripten-core/emscripten/issues/12819
115118
if (__cxa_thread_atexit_impl) {
116119
return __cxa_thread_atexit_impl(dtor, obj, dso_symbol);
117-
} else {
120+
} else
121+
#endif
122+
{
118123
// Initialize the dtors std::__libcpp_tls_key (uses __cxa_guard_*() for
119124
// one-time initialization and __cxa_atexit() for destruction)
120125
static DtorsManager manager;

system/lib/pthread/pthread_create.c

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@
2121

2222
extern int __pthread_create_js(struct pthread *thread, const pthread_attr_t *attr, void *(*start_routine) (void *), void *arg);
2323
extern void _emscripten_thread_init(int, int, int);
24-
extern void __pthread_exit_run_handlers();
2524
extern void __pthread_detached_exit();
2625
extern void* _emscripten_tls_base();
2726
extern int8_t __dso_handle;
@@ -125,10 +124,6 @@ void _emscripten_thread_exit(void* result) {
125124
// Run any handlers registered with pthread_cleanup_push
126125
__run_cleanup_handlers();
127126

128-
// Run any JS thread exit handlers (for C++ programs this includes any
129-
// functions registered with __cxa_thread_atexit).
130-
__pthread_exit_run_handlers();
131-
132127
// Call into the musl function that runs destructors of all thread-specific data.
133128
__pthread_tsd_run_dtors();
134129

tests/other/metadce/minimal_main_Oz_USE_PTHREADS_PROXY_TO_PTHREAD.exports

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ L
1313
M
1414
N
1515
O
16-
P
16+
u
1717
v
1818
w
1919
x

tests/other/metadce/minimal_main_Oz_USE_PTHREADS_PROXY_TO_PTHREAD.imports

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,4 +18,3 @@ a.q
1818
a.r
1919
a.s
2020
a.t
21-
a.u

tests/other/metadce/minimal_main_Oz_USE_PTHREADS_PROXY_TO_PTHREAD.sent

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,4 +18,3 @@ q
1818
r
1919
s
2020
t
21-
u

tools/system_libs.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -986,6 +986,7 @@ def get_files(self):
986986
'cxa_guard.cpp',
987987
'cxa_handlers.cpp',
988988
'cxa_virtual.cpp',
989+
'cxa_thread_atexit.cpp',
989990
'fallback_malloc.cpp',
990991
'stdlib_new_delete.cpp',
991992
'stdlib_exception.cpp',

0 commit comments

Comments
 (0)