Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 0 additions & 2 deletions system/lib/pthread/emscripten_tls_init.c
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
13 changes: 6 additions & 7 deletions system/lib/pthread/pthread_create.c
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -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;
Expand All @@ -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) {
Expand Down Expand Up @@ -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.
Expand Down