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

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