Make POSIX threads improvements

- Ensure SIGTHR isn't blocked in newly created threads
- Use TIB rather than thread_local for thread atexits
- Make POSIX thread keys atomic within thread
- Don't bother logging prctl() to --strace
- Log thread destructor names to --strace
This commit is contained in:
Justine Tunney 2024-06-30 15:38:59 -07:00
parent 387310c659
commit 76957983cf
No known key found for this signature in database
GPG key ID: BE714B4575D6E328
11 changed files with 57 additions and 71 deletions

View file

@ -17,6 +17,7 @@
PERFORMANCE OF THIS SOFTWARE.
*/
#include "libc/intrin/cxaatexit.internal.h"
#include "libc/intrin/strace.internal.h"
#include "libc/intrin/weaken.h"
#include "libc/mem/mem.h"
#include "libc/nexgen32e/gc.internal.h"
@ -30,8 +31,6 @@ struct Dtor {
struct Dtor *next;
};
static _Thread_local struct Dtor *__cxa_thread_atexit_list;
static void _pthread_unwind(struct CosmoTib *tib) {
struct PosixThread *pt;
struct _pthread_cleanup_buffer *cb;
@ -42,24 +41,24 @@ static void _pthread_unwind(struct CosmoTib *tib) {
}
}
static void __cxa_thread_unkey(struct CosmoTib *tib) {
void *val;
int i, j, gotsome;
pthread_key_dtor dtor;
for (j = 0; j < PTHREAD_DESTRUCTOR_ITERATIONS; ++j) {
for (gotsome = i = 0; i < PTHREAD_KEYS_MAX; ++i) {
if ((val = tib->tib_keys[i]) &&
(dtor = atomic_load_explicit(_pthread_key_dtor + i,
memory_order_relaxed)) &&
dtor != (pthread_key_dtor)-1) {
gotsome = 1;
tib->tib_keys[i] = 0;
static void _pthread_unkey(struct CosmoTib *tib) {
for (int j = 0; j < PTHREAD_DESTRUCTOR_ITERATIONS; ++j) {
bool gotsome = false;
for (int i = 0; i < PTHREAD_KEYS_MAX; ++i) {
void *val;
pthread_key_dtor dtor;
if ((dtor = atomic_load_explicit(&_pthread_key_dtor[i],
memory_order_acquire)) &&
dtor != (pthread_key_dtor)-1 &&
(val = atomic_exchange_explicit(&tib->tib_keys[i], 0,
memory_order_relaxed))) {
STRACE("_pthread_unkey(%t, %p)", dtor, val);
gotsome = true;
dtor(val);
}
}
if (!gotsome) {
if (!gotsome)
break;
}
}
}
@ -67,9 +66,8 @@ static void _pthread_ungarbage(struct CosmoTib *tib) {
struct Garbages *g;
while ((g = tib->tib_garbages)) {
tib->tib_garbages = 0;
while (g->i--) {
while (g->i--)
((void (*)(intptr_t))g->p[g->i].fn)(g->p[g->i].arg);
}
_weaken(free)(g->p);
_weaken(free)(g);
}
@ -82,10 +80,11 @@ void __cxa_thread_finalize(void) {
_pthread_unwind(tib);
if (tib->tib_nsync)
_weaken(nsync_waiter_destroy)(tib->tib_nsync);
__cxa_thread_unkey(tib);
_pthread_unkey(tib);
_pthread_ungarbage(tib);
while ((dtor = __cxa_thread_atexit_list)) {
__cxa_thread_atexit_list = dtor->next;
while ((dtor = tib->tib_atexit)) {
STRACE("__cxa_finalize(%t, %p)", dtor->fun, dtor->arg);
tib->tib_atexit = dtor->next;
((void (*)(void *))dtor->fun)(dtor->arg);
_weaken(free)(dtor);
}
@ -97,9 +96,11 @@ int __cxa_thread_atexit_impl(void *fun, void *arg, void *dso_symbol) {
return -1;
if (!(dtor = _weaken(malloc)(sizeof(struct Dtor))))
return -1;
struct CosmoTib *tib;
tib = __get_tls();
dtor->fun = fun;
dtor->arg = arg;
dtor->next = __cxa_thread_atexit_list;
__cxa_thread_atexit_list = dtor;
dtor->next = tib->tib_atexit;
tib->tib_atexit = dtor;
return 0;
}