mirror of
https://github.com/jart/cosmopolitan.git
synced 2025-04-18 21:04:40 +00:00
Introduce __cxa_thread_atexit()
This commit is contained in:
parent
a699cda5c8
commit
ee82f90bba
6 changed files with 98 additions and 0 deletions
|
@ -21,7 +21,10 @@ extern struct CxaAtexitBlocks __cxa_blocks;
|
||||||
|
|
||||||
void __cxa_lock(void);
|
void __cxa_lock(void);
|
||||||
void __cxa_unlock(void);
|
void __cxa_unlock(void);
|
||||||
|
void __cxa_thread_finalize(void);
|
||||||
void __cxa_printexits(FILE *, void *);
|
void __cxa_printexits(FILE *, void *);
|
||||||
|
int __cxa_thread_atexit(void *, void *, void *);
|
||||||
|
int __cxa_thread_atexit_impl(void *, void *, void *);
|
||||||
|
|
||||||
COSMOPOLITAN_C_END_
|
COSMOPOLITAN_C_END_
|
||||||
#endif /* !(__ASSEMBLER__ + __LINKER__ + 0) */
|
#endif /* !(__ASSEMBLER__ + __LINKER__ + 0) */
|
||||||
|
|
|
@ -20,6 +20,7 @@
|
||||||
#include "libc/intrin/asan.internal.h"
|
#include "libc/intrin/asan.internal.h"
|
||||||
#include "libc/intrin/bits.h"
|
#include "libc/intrin/bits.h"
|
||||||
#include "libc/intrin/cmpxchg.h"
|
#include "libc/intrin/cmpxchg.h"
|
||||||
|
#include "libc/intrin/cxaatexit.internal.h"
|
||||||
#include "libc/intrin/kprintf.h"
|
#include "libc/intrin/kprintf.h"
|
||||||
#include "libc/intrin/strace.internal.h"
|
#include "libc/intrin/strace.internal.h"
|
||||||
#include "libc/mem/mem.h"
|
#include "libc/mem/mem.h"
|
||||||
|
@ -94,6 +95,7 @@ dontasan void CheckForMemoryLeaks(void) {
|
||||||
exit(0);
|
exit(0);
|
||||||
}
|
}
|
||||||
_pthread_unwind(_pthread_self());
|
_pthread_unwind(_pthread_self());
|
||||||
|
__cxa_thread_finalize();
|
||||||
_pthread_unkey(__get_tls());
|
_pthread_unkey(__get_tls());
|
||||||
_pthread_ungarbage();
|
_pthread_ungarbage();
|
||||||
__cxa_finalize(0);
|
__cxa_finalize(0);
|
||||||
|
|
23
libc/thread/__cxa_thread_atexit.c
Normal file
23
libc/thread/__cxa_thread_atexit.c
Normal file
|
@ -0,0 +1,23 @@
|
||||||
|
/*-*- mode:c;indent-tabs-mode:nil;c-basic-offset:2;tab-width:8;coding:utf-8 -*-│
|
||||||
|
│vi: set net ft=c ts=2 sts=2 sw=2 fenc=utf-8 :vi│
|
||||||
|
╞══════════════════════════════════════════════════════════════════════════════╡
|
||||||
|
│ Copyright 2023 Justine Alexandra Roberts Tunney │
|
||||||
|
│ │
|
||||||
|
│ Permission to use, copy, modify, and/or distribute this software for │
|
||||||
|
│ any purpose with or without fee is hereby granted, provided that the │
|
||||||
|
│ above copyright notice and this permission notice appear in all copies. │
|
||||||
|
│ │
|
||||||
|
│ THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL │
|
||||||
|
│ WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED │
|
||||||
|
│ WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE │
|
||||||
|
│ AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL │
|
||||||
|
│ DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR │
|
||||||
|
│ PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER │
|
||||||
|
│ TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR │
|
||||||
|
│ PERFORMANCE OF THIS SOFTWARE. │
|
||||||
|
╚─────────────────────────────────────────────────────────────────────────────*/
|
||||||
|
#include "libc/intrin/cxaatexit.internal.h"
|
||||||
|
|
||||||
|
int __cxa_thread_atexit(void *fun, void *arg, void *dso_symbol) {
|
||||||
|
return __cxa_thread_atexit_impl(fun, arg, dso_symbol);
|
||||||
|
}
|
47
libc/thread/__cxa_thread_atexit_impl.c
Normal file
47
libc/thread/__cxa_thread_atexit_impl.c
Normal file
|
@ -0,0 +1,47 @@
|
||||||
|
/*-*- mode:c;indent-tabs-mode:nil;c-basic-offset:2;tab-width:8;coding:utf-8 -*-│
|
||||||
|
│vi: set net ft=c ts=2 sts=2 sw=2 fenc=utf-8 :vi│
|
||||||
|
╞══════════════════════════════════════════════════════════════════════════════╡
|
||||||
|
│ Copyright 2023 Justine Alexandra Roberts Tunney │
|
||||||
|
│ │
|
||||||
|
│ Permission to use, copy, modify, and/or distribute this software for │
|
||||||
|
│ any purpose with or without fee is hereby granted, provided that the │
|
||||||
|
│ above copyright notice and this permission notice appear in all copies. │
|
||||||
|
│ │
|
||||||
|
│ THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL │
|
||||||
|
│ WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED │
|
||||||
|
│ WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE │
|
||||||
|
│ AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL │
|
||||||
|
│ DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR │
|
||||||
|
│ PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER │
|
||||||
|
│ TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR │
|
||||||
|
│ PERFORMANCE OF THIS SOFTWARE. │
|
||||||
|
╚─────────────────────────────────────────────────────────────────────────────*/
|
||||||
|
#include "libc/intrin/cxaatexit.internal.h"
|
||||||
|
#include "libc/mem/mem.h"
|
||||||
|
|
||||||
|
struct Dtor {
|
||||||
|
void *fun;
|
||||||
|
void *arg;
|
||||||
|
struct Dtor *next;
|
||||||
|
};
|
||||||
|
|
||||||
|
static _Thread_local struct Dtor *__cxa_thread_atexit_list;
|
||||||
|
|
||||||
|
void __cxa_thread_finalize(void) {
|
||||||
|
struct Dtor *dtor;
|
||||||
|
while ((dtor = __cxa_thread_atexit_list)) {
|
||||||
|
__cxa_thread_atexit_list = dtor->next;
|
||||||
|
((void (*)(void *))dtor->fun)(dtor->arg);
|
||||||
|
free(dtor);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
int __cxa_thread_atexit_impl(void *fun, void *arg, void *dso_symbol) {
|
||||||
|
struct Dtor *dtor;
|
||||||
|
if (!(dtor = malloc(sizeof(struct Dtor)))) return -1;
|
||||||
|
dtor->fun = fun;
|
||||||
|
dtor->arg = arg;
|
||||||
|
dtor->next = __cxa_thread_atexit_list;
|
||||||
|
__cxa_thread_atexit_list = dtor;
|
||||||
|
return 0;
|
||||||
|
}
|
|
@ -20,6 +20,7 @@
|
||||||
#include "libc/atomic.h"
|
#include "libc/atomic.h"
|
||||||
#include "libc/dce.h"
|
#include "libc/dce.h"
|
||||||
#include "libc/intrin/atomic.h"
|
#include "libc/intrin/atomic.h"
|
||||||
|
#include "libc/intrin/cxaatexit.internal.h"
|
||||||
#include "libc/intrin/strace.internal.h"
|
#include "libc/intrin/strace.internal.h"
|
||||||
#include "libc/intrin/weaken.h"
|
#include "libc/intrin/weaken.h"
|
||||||
#include "libc/limits.h"
|
#include "libc/limits.h"
|
||||||
|
@ -110,6 +111,9 @@ wontreturn void pthread_exit(void *rc) {
|
||||||
|
|
||||||
// free resources
|
// free resources
|
||||||
_pthread_unwind(pt);
|
_pthread_unwind(pt);
|
||||||
|
if (_weaken(__cxa_thread_finalize)) {
|
||||||
|
_weaken(__cxa_thread_finalize)();
|
||||||
|
}
|
||||||
_pthread_unkey(tib);
|
_pthread_unkey(tib);
|
||||||
_pthread_ungarbage();
|
_pthread_ungarbage();
|
||||||
_pthread_decimate();
|
_pthread_decimate();
|
||||||
|
|
|
@ -16,6 +16,7 @@
|
||||||
│ TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR │
|
│ TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR │
|
||||||
│ PERFORMANCE OF THIS SOFTWARE. │
|
│ PERFORMANCE OF THIS SOFTWARE. │
|
||||||
╚─────────────────────────────────────────────────────────────────────────────*/
|
╚─────────────────────────────────────────────────────────────────────────────*/
|
||||||
|
#include "libc/intrin/cxaatexit.internal.h"
|
||||||
#include "libc/runtime/runtime.h"
|
#include "libc/runtime/runtime.h"
|
||||||
#include "libc/testlib/subprocess.h"
|
#include "libc/testlib/subprocess.h"
|
||||||
#include "libc/testlib/testlib.h"
|
#include "libc/testlib/testlib.h"
|
||||||
|
@ -66,3 +67,21 @@ TEST(pthread_exit, detachedOrphanedChild_runsAtexitHandlers) {
|
||||||
pthread_exit(0);
|
pthread_exit(0);
|
||||||
EXITS(CHILD);
|
EXITS(CHILD);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void OnMainThreadExit(void *arg) {
|
||||||
|
_Exit((long)arg);
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST(__cxa_thread_atexit, exit_wontInvokeThreadDestructors) {
|
||||||
|
SPAWN(fork);
|
||||||
|
__cxa_thread_atexit(OnMainThreadExit, (void *)123L, 0);
|
||||||
|
exit(0);
|
||||||
|
EXITS(0);
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST(__cxa_thread_atexit, pthread_exit_willInvokeThreadDestructors) {
|
||||||
|
SPAWN(fork);
|
||||||
|
__cxa_thread_atexit(OnMainThreadExit, (void *)123L, 0);
|
||||||
|
pthread_exit(0);
|
||||||
|
EXITS(123);
|
||||||
|
}
|
||||||
|
|
Loading…
Add table
Reference in a new issue