Skip to content

Commit 2cdcb71

Browse files
committed
Avoid using __cxa_thread_atexit for pthread_cleanup_push.
These is no need to use `__cxa_thread_atexit` here, we can just call these functions during thread exit (which is what musl does). This also avoids references `__cxa_thread_atexit` which is normally a libc++abi symbol from C programs. Needed by (split out from) #14489.
1 parent be2fe9c commit 2cdcb71

5 files changed

+7
-12
lines changed

system/lib/pthread/emscripten_tls_init.c

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,6 @@
1919
// linker-generated symbol that loads static TLS data at the given location.
2020
extern void __wasm_init_tls(void *memory);
2121

22-
extern int __cxa_thread_atexit(void (*)(void *), void *, void *);
23-
2422
extern int __dso_handle;
2523

2624
void* emscripten_tls_init(void) {

system/lib/pthread/pthread_create.c

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@
1919

2020
// See musl's pthread_create.c
2121

22-
extern int __cxa_thread_atexit(void (*)(void *), void *, void *);
2322
extern int __pthread_create_js(struct pthread *thread, const pthread_attr_t *attr, void *(*start_routine) (void *), void *arg);
2423
extern void _emscripten_thread_init(int, int, int);
2524
extern void __pthread_exit_run_handlers();
@@ -32,7 +31,7 @@ static void dummy_0()
3231
}
3332
weak_alias(dummy_0, __pthread_tsd_run_dtors);
3433

35-
void __run_cleanup_handlers(void* _unused) {
34+
static void __run_cleanup_handlers() {
3635
pthread_t self = __pthread_self();
3736
while (self->cancelbuf) {
3837
void (*f)(void *) = self->cancelbuf->__f;
@@ -46,11 +45,6 @@ void __do_cleanup_push(struct __ptcb *cb) {
4645
struct pthread *self = __pthread_self();
4746
cb->__next = self->cancelbuf;
4847
self->cancelbuf = cb;
49-
static thread_local bool registered = false;
50-
if (!registered) {
51-
__cxa_thread_atexit(__run_cleanup_handlers, NULL, &__dso_handle);
52-
registered = true;
53-
}
5448
}
5549

5650
void __do_cleanup_pop(struct __ptcb *cb) {
@@ -128,6 +122,11 @@ void _emscripten_thread_exit(void* result) {
128122
self->cancelasync = PTHREAD_CANCEL_DEFERRED;
129123
self->result = result;
130124

125+
// Run any handlers registered with pthread_cleanup_push
126+
__run_cleanup_handlers();
127+
128+
// Run any JS thread exit handlers (for C++ programs this includes any
129+
// functions registered with __cxa_thread_atexit).
131130
__pthread_exit_run_handlers();
132131

133132
// Call into the musl function that runs destructors of all thread-specific data.

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

0 commit comments

Comments
 (0)