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

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