Skip to content

Commit 0fb0306

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 1f479cf commit 0fb0306

File tree

5 files changed

+11
-36
lines changed

5 files changed

+11
-36
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 supported 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/library_pthread.c

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,7 @@
66
*/
77

88
#define _GNU_SOURCE
9-
#include "../internal/libc.h"
10-
#include "../internal/pthread_impl.h"
9+
1110
#include <assert.h>
1211
#include <dirent.h>
1312
#include <errno.h>
@@ -34,6 +33,9 @@
3433
#include <emscripten.h>
3534
#include <emscripten/threading.h>
3635

36+
#include "libc.h"
37+
#include "pthread_impl.h"
38+
3739
// Extra pthread_attr_t field:
3840
#define _a_transferredcanvases __u.__s[9]
3941

system/lib/pthread/pthread_create.c

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@
2222
extern int __cxa_thread_atexit(void (*)(void *), void *, void *);
2323
extern int __pthread_create_js(struct pthread *thread, const pthread_attr_t *attr, void *(*start_routine) (void *), void *arg);
2424
extern void _emscripten_thread_init(int, int, int);
25-
extern void __pthread_exit_run_handlers();
2625
extern void __pthread_detached_exit();
2726
extern void* _emscripten_tls_base();
2827
extern int8_t __dso_handle;
@@ -126,10 +125,6 @@ void _emscripten_thread_exit(void* result) {
126125
// Run any handlers registered with pthread_cleanup_push
127126
__run_cleanup_handlers();
128127

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

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)