From baac32a32871cba9063830976cc32f2c1d12abeb Mon Sep 17 00:00:00 2001 From: Sam Clegg Date: Mon, 4 Oct 2021 08:42:06 -0700 Subject: [PATCH] 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. --- system/lib/pthread/emscripten_tls_init.c | 2 -- system/lib/pthread/pthread_create.c | 13 ++++++------- 2 files changed, 6 insertions(+), 9 deletions(-) diff --git a/system/lib/pthread/emscripten_tls_init.c b/system/lib/pthread/emscripten_tls_init.c index dbbeba33635de..26cad5c0b69d4 100644 --- a/system/lib/pthread/emscripten_tls_init.c +++ b/system/lib/pthread/emscripten_tls_init.c @@ -19,8 +19,6 @@ // linker-generated symbol that loads static TLS data at the given location. extern void __wasm_init_tls(void *memory); -extern int __cxa_thread_atexit(void (*)(void *), void *, void *); - extern int __dso_handle; void* emscripten_tls_init(void) { diff --git a/system/lib/pthread/pthread_create.c b/system/lib/pthread/pthread_create.c index 415c2099c1d30..bc50466d86b9d 100644 --- a/system/lib/pthread/pthread_create.c +++ b/system/lib/pthread/pthread_create.c @@ -19,7 +19,6 @@ // See musl's pthread_create.c -extern int __cxa_thread_atexit(void (*)(void *), void *, void *); extern int __pthread_create_js(struct pthread *thread, const pthread_attr_t *attr, void *(*start_routine) (void *), void *arg); extern void _emscripten_thread_init(int, int, int); extern void __pthread_exit_run_handlers(); @@ -32,7 +31,7 @@ static void dummy_0() } weak_alias(dummy_0, __pthread_tsd_run_dtors); -void __run_cleanup_handlers(void* _unused) { +static void __run_cleanup_handlers() { pthread_t self = __pthread_self(); while (self->cancelbuf) { void (*f)(void *) = self->cancelbuf->__f; @@ -46,11 +45,6 @@ void __do_cleanup_push(struct __ptcb *cb) { struct pthread *self = __pthread_self(); cb->__next = self->cancelbuf; self->cancelbuf = cb; - static thread_local bool registered = false; - if (!registered) { - __cxa_thread_atexit(__run_cleanup_handlers, NULL, &__dso_handle); - registered = true; - } } void __do_cleanup_pop(struct __ptcb *cb) { @@ -128,6 +122,11 @@ void _emscripten_thread_exit(void* result) { self->cancelasync = PTHREAD_CANCEL_DEFERRED; self->result = result; + // Run any handlers registered with pthread_cleanup_push + __run_cleanup_handlers(); + + // Run any JS thread exit handlers (for C++ programs this includes any + // functions registered with __cxa_thread_atexit). __pthread_exit_run_handlers(); // Call into the musl function that runs destructors of all thread-specific data.