Skip to content

Commit 1f479cf

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 1f479cf

5 files changed

+7
-11
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 & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ static void dummy_0()
3232
}
3333
weak_alias(dummy_0, __pthread_tsd_run_dtors);
3434

35-
void __run_cleanup_handlers(void* _unused) {
35+
static void __run_cleanup_handlers() {
3636
pthread_t self = __pthread_self();
3737
while (self->cancelbuf) {
3838
void (*f)(void *) = self->cancelbuf->__f;
@@ -46,11 +46,6 @@ void __do_cleanup_push(struct __ptcb *cb) {
4646
struct pthread *self = __pthread_self();
4747
cb->__next = self->cancelbuf;
4848
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-
}
5449
}
5550

5651
void __do_cleanup_pop(struct __ptcb *cb) {
@@ -128,6 +123,11 @@ void _emscripten_thread_exit(void* result) {
128123
self->cancelasync = PTHREAD_CANCEL_DEFERRED;
129124
self->result = result;
130125

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

133133
// 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)