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:
chrg6
2026-11-12 15:18:25 -05:00
parent 823e0cdcd4
commit 222a025c52
9 changed files with 212 additions and 9 deletions

View File

@@ -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();
}
}