mirror of
https://github.com/tangerineturtle1/NX2navi.git
synced 2026-11-18 13:09: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:
@@ -113,10 +113,19 @@ static ZSTD_DDict *dict_for(uint64_t guest, uint64_t content, uint64_t size) {
|
||||
return dd;
|
||||
}
|
||||
|
||||
// A thread's one-shot decoder context (host_decode), freed when the thread ends (plat_thread_end_hook): the game
|
||||
// makes and ends threads all through play, and each that had decoded left its context behind, about 100 KiB.
|
||||
static __thread ZSTD_DCtx *dctx;
|
||||
static volatile int32_t dctx_hooked;
|
||||
|
||||
static void dctx_thread_end(void) {
|
||||
if (dctx) ZSTD_freeDCtx(dctx);
|
||||
dctx = NULL;
|
||||
}
|
||||
|
||||
// Decodes on this thread's host context. Returns libzstd's result; *ok 0 when the inputs cannot be taken.
|
||||
static size_t host_decode(uint64_t dst, uint64_t cap, uint64_t src, uint64_t size, uint64_t dict, uint64_t dict_size,
|
||||
uint64_t ddict, int *ok) {
|
||||
static __thread ZSTD_DCtx *dctx;
|
||||
*ok = 0;
|
||||
if ((cap && !dst) || !src || src + size > RT_ADDRESS_LIMIT) return 0; // dst: decode() checks a guest one
|
||||
ZSTD_DDict *dd = NULL;
|
||||
@@ -139,7 +148,10 @@ static size_t host_decode(uint64_t dst, uint64_t cap, uint64_t src, uint64_t siz
|
||||
} else if (dict && dict_size) {
|
||||
if (!(dd = dict_for(0, dict, dict_size))) return 0;
|
||||
}
|
||||
if (!dctx && !(dctx = ZSTD_createDCtx())) return 0;
|
||||
if (!dctx) {
|
||||
if (!plat_xchg32(&dctx_hooked, 1)) plat_thread_end_hook(dctx_thread_end);
|
||||
if (!(dctx = ZSTD_createDCtx())) return 0;
|
||||
}
|
||||
*ok = 1;
|
||||
return dd ? ZSTD_decompress_usingDDict(dctx, gp(dst), (size_t)cap, gp(src), (size_t)size, dd)
|
||||
: ZSTD_decompressDCtx(dctx, gp(dst), (size_t)cap, gp(src), (size_t)size);
|
||||
|
||||
Reference in New Issue
Block a user