Perform some code cleanup

This commit is contained in:
Justine Tunney 2022-06-23 10:21:07 -07:00
parent 0dd9629562
commit a4601a24d3
63 changed files with 350 additions and 1643 deletions

View file

@ -29,6 +29,7 @@
#include "libc/sysv/consts/clone.h"
#include "libc/sysv/consts/map.h"
#include "libc/sysv/consts/prot.h"
#include "libc/thread/internal.h"
#include "libc/thread/thread.h"
STATIC_YOINK("_main_thread_ctor");
@ -77,6 +78,7 @@ static int cthread_start(void *arg) {
exitcode = (void *)rc.dx;
}
td->exitcode = exitcode;
_pthread_key_destruct(td->key);
if (atomic_load(&td->state) & cthread_detached) {
// we're still using the stack
// thus we can't munmap it yet

View file

@ -54,7 +54,6 @@ int cthread_join(cthread_t td, void **exitcode) {
} else {
if (~atomic_fetch_add(&td->state, cthread_joining) & cthread_finished) {
while ((x = atomic_load(&td->tid))) {
// FUTEX_WAIT_PRIVATE makes it hang
cthread_memory_wait32(&td->tid, x, 0);
}
}

View file

@ -30,7 +30,7 @@ StartOver:
x = _pthread_key_usage[i];
while (x) {
j = bsrl(x);
if ((dtor = _pthread_key_dtor[i * 64 + j]) && (value = key[i * 64 + j])) {
if ((value = key[i * 64 + j]) && (dtor = _pthread_key_dtor[i * 64 + j])) {
key[i * 64 + j] = 0;
dtor(value);
goto StartOver;

View file

@ -18,53 +18,25 @@
*/
#include "libc/bits/atomic.h"
#include "libc/calls/calls.h"
#include "libc/calls/struct/timespec.h"
#include "libc/dce.h"
#include "libc/sysv/consts/clock.h"
#include "libc/sysv/consts/futex.h"
#include "libc/thread/freebsd.internal.h"
#include "libc/errno.h"
#include "libc/intrin/futex.internal.h"
#include "libc/thread/thread.h"
int cthread_memory_wait32(int* addr, int val, const struct timespec* timeout) {
size_t size;
struct _umtx_time *put, ut;
if (IsLinux() || IsOpenbsd()) {
return sys_futex(addr, FUTEX_WAIT, val, timeout, 0);
#if 0
} else if (IsFreebsd()) {
if (!timeout) {
put = 0;
size = 0;
} else {
ut._flags = 0;
ut._clockid = CLOCK_REALTIME;
ut._timeout = *timeout;
put = &ut;
size = sizeof(ut);
}
return _umtx_op(addr, UMTX_OP_MUTEX_WAIT, 0, &size, put);
#endif
return _futex_wait(addr, val, timeout);
} else {
unsigned tries;
for (tries = 1; atomic_load(addr) == val; ++tries) {
if (tries & 7) {
__builtin_ia32_pause();
} else {
sched_yield();
}
}
return 0;
return sched_yield();
}
}
int cthread_memory_wake32(int* addr, int n) {
if (IsLinux() || IsOpenbsd()) {
return sys_futex(addr, FUTEX_WAKE, n, 0, 0);
#if 0
} else if (IsFreebsd()) {
return _umtx_op(addr, UMTX_OP_MUTEX_WAKE, n, 0, 0);
#endif
return _futex_wake(addr, n);
} else {
return 0;
}
return -1;
}

View file

@ -41,6 +41,7 @@ void cthread_zombies_add(cthread_t td) {
void cthread_zombies_reap(void) {
struct Zombie *z;
// TODO(jart): Is this right? Update to not use malloc/free?
while ((z = atomic_load(&cthread_zombies)) && !atomic_load(&z->td->tid)) {
if (atomic_compare_exchange_weak(&cthread_zombies, &z, z->next)) {
munmap(z->td->alloc.bottom, z->td->alloc.top - z->td->alloc.bottom);