hle(host_posix): implement DuplicateHandle/OpenThread/ExitThread, InitOnceBeginInitialize/Complete, FLS destructors, and pooled 8 MiB threadpool workers #559
Loading…
Reference in a new issue
No description provided.
Delete branch "%!s()"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
Imported from LakshanSivakumar/Native-Victory-macOS. Originally posted by Sheffy007 on 2026-11-28T04:35:48Z.
Refs #456, #521.
Several Win32 thread, handle, one-time-init, and FLS primitives on the native POSIX host (
crates/victory-hle/src/host_posix.rs,crates/victory-hle/src/host_posix/system.rs) are currently missing or incomplete:DuplicateHandle,GetThreadId,ExitThread, and stubbedOpenThread:docs/native-host.mdlistsDuplicateHandlein thehost_posix.rshandle table, butDuplicateHandle,GetThreadId, andExitThreadare unimplemented, andOpenThreadunconditionally fails withERROR_ACCESS_DENIED. DuplicatingGetCurrentThread()(-2) or opening a thread by ID is standard Win32/CRT practice for thread inspection and synchronization.InitOnceInitialize,InitOnceBeginInitialize, andInitOnceComplete:system.rsexportsInitOnceExecuteOnce(polling with200 µssleeps) but lacksInitOnceInitialize,InitOnceBeginInitialize, andInitOnceComplete, which MSVC C++std::call_onceand runtime initialization resolve fromapi-ms-win-core-synch-l1-2-0.dll/kernel32.dll. Now thatWaitOnAddressandWakeByAddressAllare onmain(#556), synchronousINIT_ONCEwaiters can block and wake directly on theINIT_ONCEword alongsideINIT_ONCE_CHECK_ONLY,INIT_ONCE_ASYNC, andINIT_ONCE_INIT_FAILED.FlsAllocdestructor callbacks are ignored (system.rs:1661,docs/native-host.md:148):FlsAlloccurrently dropslpCallback, so per-thread CRT / library allocations stored viaFlsSetValueare never cleaned up at thread exit orFlsFree.SubmitThreadpoolWork/SetThreadpoolTimer) spawns ephemeral 2 MiBstd::thread::spawnthreads:MIN_STACK = 8 MiB(host_posix.rs:64) because title frames and guessed-draw Metal pipeline builds overflow smaller stacks (which under Rosetta faults into the guard page and can terminate withSIGSEGV/ exit 139). Pooling reusable 8 MiB worker threads forSubmitThreadpoolWorkand givingSetThreadpoolTimerworkersMIN_STACKavoids per-submission pthread/TEB churn and 2 MiB stack overflows.Imported from LakshanSivakumar/Native-Victory-macOS. Originally posted by Sheffy007 on 2026-11-28T04:35:50Z.
/claim
Imported from LakshanSivakumar/Native-Victory-macOS. Originally posted by github-actions[bot] on 2026-11-28T04:36:04Z.
Claimed by @Sheffy007. Open a draft pull request with
Refs #559in its description within 24 hours, or the claim expires.Imported from LakshanSivakumar/Native-Victory-macOS. Originally posted by AnastasisKon on 2026-11-28T15:16:00Z.
Heads up rather than a claim, since this is yours: while working my own fork earlier today I landed two pieces of this that overlap your branch
feat/host-posix-threads-initonce-fls, and I would rather you know now than find it in review.InitOnceBeginInitialize/InitOnceCompleteover the wordInitOnceExecuteOncealready uses (0 new, 1 running, context with bit 1 set done).INIT_ONCE_CHECK_ONLYreports a run under way asERROR_IO_PENDING; a complete without a claim, or a second one, isERROR_INVALID_PARAMETER;INIT_ONCE_INIT_FAILEDhands the word back;INIT_ONCE_ASYNCis refused withERROR_CALL_NOT_IMPLEMENTEDbecause it hands back the slot the winner's context goes in and this host has nowhere to keep it. Commita4ada0d0, with two tests and adocs/native-host.mdrow.OpenThread(was a stub answeringERROR_ACCESS_DENIED) andGetThreadId. A title thread records its OS id when it starts, in aTHREADStable holding the object weakly; an id whose thread has ended isERROR_INVALID_PARAMETER. Commite4122142, three tests, PR #31 in my fork.DuplicateHandlewas already there (handle #23). Both commits are onAnastasisKon/Native-Victory-macOSmain, so take them as they are or cherry-pick; nothing of yours needs rewriting for them. The parts of this issue nobody has touched:ExitThread, the FLS destructor callbacks, and the pooled 8 MiB threadpool workers.Imported from LakshanSivakumar/Native-Victory-macOS. Originally posted by AnastasisKon on 2026-11-28T15:45:17Z.
Two of the four items on this list are done in my fork's
main(AnastasisKon/Native-Victory-macOS), each with gates green:ExitThread(PR #36 there): served fromhost_posix, leaving the entry frame the loader recorded for the thread (bridge::exit_title_thread, the primitive_endthreadexalready used), so the thread'sDLL_THREAD_DETACHnotices run and it ends with the code it passed. A foreign host thread of ours running a title callback has no such frame, so a newbridge::in_title_thread()says so and the call answersERROR_CALL_NOT_IMPLEMENTEDinstead of aborting the process onexit_title_thread's precondition.probe_threadsnow starts a thread whose routine callsExitThread(7)and stores a flag after it: the flag must stay clear andGetExitCodeThreadmust say 7.victory_platform::threadthatspawn_title_thread_withruns beforeDLL_THREAD_DETACH(armed by the firstFlsAllocthat is given a callback, four rounds when a destructor sets its slot again), and for the thread that frees the slot. The probe checks both paths.DuplicateHandleandOpenThread/GetThreadIdhad already landed;InitOnceBeginInitialize/InitOnceCompletetoo. Item 4 (pooled 8 MiB threadpool workers) is still open —MIN_STACKis 8 MiB for title threads, and the pool's workers are still per-submission.Imported from LakshanSivakumar/Native-Victory-macOS. Originally posted by AnastasisKon on 2026-11-28T16:07:30Z.
Item 4 of this list (the thread pool's ephemeral 2 MiB workers) is done now too, in my fork's
main(AnastasisKon/Native-Victory-macOS, PR #41 there), with gates green:SubmitThreadpoolWork, and a timer that is already due, now queue onto a pool of workers that keeps its stack and its TEB for the next item —MIN_STACK(8 MiB) each, starting a worker when none is waiting, up to 12. A timer that has to wait for its deadline, a repeating timer and every armed wait keep a thread of their own with the same stack, so a long deadline cannot hold a worker the title's work items need; the cancel tickets and theWaitFor...Callbacksaccounting are unchanged.The probe submits one item through the real imports and checks from title code that it ran exactly once and that the stack it ran on is 8 MiB (
gs:[8]minusgs:[0x10]). I checked that assertion is a real one by shrinking the pool's stack to 2 MiB: the probe test fails on exactly that bit.That closes the four items:
DuplicateHandle/OpenThread/GetThreadId,InitOnceBeginInitialize/InitOnceComplete, FLS destructors, and the pooled workers;ExitThreadcame with them.