mirror of
https://github.com/tangerineturtle1/NX2navi.git
synced 2026-11-17 15:49:57 -04:00
Give back what a host thread keeps for itself when it ends.
Every guest thread is a host thread, and the game makes and ends them all through play. A thread that slept through plat_wait_ns kept a waitable timer, one that read or wrote a file an event for each, and one that decoded zstd through the host a decoder context of about 100 KiB; nothing closed or freed them when the thread ended, and a detached thread's own record stayed allocated. They are now given back when the thread's function returns: the handles in win32.c thread_main, the decoder context through plat_thread_end_hook, and the record by whichever of the thread and its detach comes last. tests/thread_end_check.py runs 416 such threads and compares the process's handle count before and after. tests/sync_wake_check.c gets the two stand-ins kernel.c has needed since the service wait log, so that check builds and passes again. The Linux file only gains the hook call and was not compiled here. Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
This commit is contained in:
@@ -159,3 +159,20 @@ int plat_addr_str(const PlatAddr *a, char *out, size_t cap) {
|
||||
memcpy(out, s, (size_t)n + 1);
|
||||
return n;
|
||||
}
|
||||
|
||||
// ---- what a thread keeps for itself (platform.h plat_thread_end_hook)
|
||||
static void (*end_hooks[PLAT_THREAD_END_HOOKS])(void);
|
||||
static volatile int32_t n_end_hooks;
|
||||
|
||||
void plat_thread_end_hook(void (*fn)(void)) {
|
||||
int32_t i = __atomic_fetch_add(&n_end_hooks, 1, __ATOMIC_ACQ_REL);
|
||||
if (i < PLAT_THREAD_END_HOOKS) __atomic_store_n(&end_hooks[i], fn, __ATOMIC_RELEASE);
|
||||
}
|
||||
|
||||
void plat_thread_end_run(void) {
|
||||
int32_t n = __atomic_load_n(&n_end_hooks, __ATOMIC_ACQUIRE);
|
||||
for (int32_t i = 0; i < n && i < PLAT_THREAD_END_HOOKS; i++) {
|
||||
void (*fn)(void) = __atomic_load_n(&end_hooks[i], __ATOMIC_ACQUIRE);
|
||||
if (fn) fn();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -114,6 +114,7 @@ static void *thread_main(void *p) {
|
||||
__atomic_store_n(&t->tid, (int32_t)syscall(SYS_gettid), __ATOMIC_RELEASE);
|
||||
if (t->name[0]) pthread_setname_np(pthread_self(), t->name);
|
||||
t->fn(t->arg);
|
||||
plat_thread_end_run(); // what the other files keep per thread (platform.h plat_thread_end_hook)
|
||||
return NULL;
|
||||
}
|
||||
|
||||
|
||||
@@ -28,6 +28,11 @@ void plat_thread_join(PlatThread *t); // waits for fn to return, then frees
|
||||
// Waits at most ns for fn to return: 1 when it did (t is freed), 0 when it still runs (t stays valid).
|
||||
int plat_thread_join_ns(PlatThread *t, uint64_t ns);
|
||||
void plat_thread_detach(PlatThread *t); // frees t; the thread runs on
|
||||
// fn runs on every thread these calls make, when its function has returned and before the thread ends: for what a
|
||||
// thread keeps for itself alone (a decoder context). At most PLAT_THREAD_END_HOOKS, each added once.
|
||||
#define PLAT_THREAD_END_HOOKS 4
|
||||
void plat_thread_end_hook(void (*fn)(void));
|
||||
void plat_thread_end_run(void); // the platform files' thread start functions call it
|
||||
// Restricts t (NULL: the calling thread) to the logical processors in mask (bit n = processor n of the first group).
|
||||
// Returns the previous mask, 0 on failure.
|
||||
uint64_t plat_thread_affinity(PlatThread *t, uint64_t mask);
|
||||
|
||||
@@ -31,11 +31,23 @@ struct PlatThread {
|
||||
HANDLE h;
|
||||
PlatThreadFn fn;
|
||||
void *arg;
|
||||
volatile int32_t state; // 0 running and owned by its maker, 1 detached, 2 its function has returned
|
||||
};
|
||||
|
||||
// What a thread made here keeps for itself is given back when its function returns: the handles below (the sleep
|
||||
// timer of plat_wait_ns, the events of plat_file_pread and plat_file_pwrite), what the other files keep per thread
|
||||
// (plat_thread_end_hook) and, for a detached thread, its PlatThread. The game makes and ends threads all through
|
||||
// play, each a host thread: nothing closed them, so every ended thread that had slept, read or written left up
|
||||
// to three kernel handles behind for the rest of the run. A guest thread ends by returning here too (kernel.c:
|
||||
// ExitThread jumps back to the thread's start).
|
||||
static void thread_handles_close(void);
|
||||
|
||||
static DWORD WINAPI thread_main(LPVOID p) {
|
||||
PlatThread *t = p;
|
||||
t->fn(t->arg);
|
||||
plat_thread_end_run();
|
||||
thread_handles_close();
|
||||
if (plat_xchg32(&t->state, 2) == 1) free(t); // detached while it ran: nobody else frees it
|
||||
return 0;
|
||||
}
|
||||
|
||||
@@ -73,10 +85,12 @@ int plat_thread_join_ns(PlatThread *t, uint64_t ns) {
|
||||
return 1;
|
||||
}
|
||||
|
||||
// The thread keeps using t->fn and t->arg, so the struct stays allocated (a few bytes per detached thread).
|
||||
// The thread still reads t->fn and t->arg when it starts, so the struct goes when the thread's function has
|
||||
// returned (thread_main), or here when it already has.
|
||||
void plat_thread_detach(PlatThread *t) {
|
||||
CloseHandle(t->h);
|
||||
t->h = NULL;
|
||||
if (plat_xchg32(&t->state, 1) == 2) free(t);
|
||||
}
|
||||
|
||||
int plat_cpu_cores(uint64_t *all, uint64_t *first) {
|
||||
@@ -474,13 +488,22 @@ uint64_t plat_now_ns(void) {
|
||||
return plat_scaled(qpc_ns, (uint64_t)t.QuadPart);
|
||||
}
|
||||
|
||||
// A high-resolution waitable timer per sleeping thread (Windows 10 1803+; one handle per thread, kept until exit)
|
||||
// A high-resolution waitable timer per sleeping thread (Windows 10 1803+; one handle per thread, closed when the
|
||||
// thread's function returns, thread_handles_close)
|
||||
// wakes within about 0.1-0.5 ms; the last PLAT_SPIN_NS are spent yielding so the deadline is met closely.
|
||||
// Without that timer kind, Sleep(1) granularity applies.
|
||||
#define PLAT_SPIN_NS 300000u
|
||||
|
||||
static __thread HANDLE sleep_timer;
|
||||
static __thread int sleep_timer_tried;
|
||||
static __thread HANDLE pread_ev, pwrite_ev; // plat_file_pread, plat_file_pwrite: the event of their overlapped call
|
||||
|
||||
static void thread_handles_close(void) {
|
||||
if (sleep_timer) CloseHandle(sleep_timer);
|
||||
if (pread_ev) CloseHandle(pread_ev);
|
||||
if (pwrite_ev) CloseHandle(pwrite_ev);
|
||||
sleep_timer = pread_ev = pwrite_ev = NULL, sleep_timer_tried = 0;
|
||||
}
|
||||
|
||||
void plat_wait_ns(uint64_t ns) {
|
||||
if (!sleep_timer_tried) {
|
||||
@@ -1302,8 +1325,8 @@ void plat_file_close(PlatFile *f) {
|
||||
}
|
||||
|
||||
int64_t plat_file_pread(PlatFile *f, void *buf, uint32_t n, uint64_t off) {
|
||||
static __thread HANDLE ev;
|
||||
if (!ev && !(ev = CreateEventW(NULL, TRUE, FALSE, NULL))) return -1;
|
||||
HANDLE ev = pread_ev;
|
||||
if (!ev && !(ev = pread_ev = CreateEventW(NULL, TRUE, FALSE, NULL))) return -1;
|
||||
OVERLAPPED ov = {.Offset = (DWORD)off, .OffsetHigh = (DWORD)(off >> 32), .hEvent = ev};
|
||||
DWORD got = 0;
|
||||
if (ReadFile((HANDLE)f, buf, n, &got, &ov)) return got;
|
||||
@@ -1561,8 +1584,8 @@ PlatFile *plat_fs_open(const char *path, int write, int *err) {
|
||||
PlatFile *plat_fs_create(const char *path, int *err) { return fs_open(path, GENERIC_WRITE, 0, CREATE_NEW, err); }
|
||||
|
||||
int64_t plat_file_pwrite(PlatFile *f, const void *buf, uint32_t n, uint64_t off) {
|
||||
static __thread HANDLE ev;
|
||||
if (!ev && !(ev = CreateEventW(NULL, TRUE, FALSE, NULL))) return -1;
|
||||
HANDLE ev = pwrite_ev;
|
||||
if (!ev && !(ev = pwrite_ev = CreateEventW(NULL, TRUE, FALSE, NULL))) return -1;
|
||||
OVERLAPPED ov = {.Offset = (DWORD)off, .OffsetHigh = (DWORD)(off >> 32), .hEvent = ev};
|
||||
DWORD put = 0;
|
||||
if (WriteFile((HANDLE)f, buf, n, &put, &ov)) return put;
|
||||
|
||||
Reference in New Issue
Block a user