hle(host_posix): implement DuplicateHandle/OpenThread/ExitThread, InitOnceBeginInitialize/Complete, FLS destructors, and pooled 8 MiB threadpool workers #559

Open
opened 2026-11-28 04:35:48 +00:00 by Sheffy007 · 5 comments
Sheffy007 commented 2026-11-28 04:35:48 +00:00 (Migrated from github.com)
Contributor

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:

  1. Missing DuplicateHandle, GetThreadId, ExitThread, and stubbed OpenThread:
    • docs/native-host.md lists DuplicateHandle in the host_posix.rs handle table, but DuplicateHandle, GetThreadId, and ExitThread are unimplemented, and OpenThread unconditionally fails with ERROR_ACCESS_DENIED. Duplicating GetCurrentThread() (-2) or opening a thread by ID is standard Win32/CRT practice for thread inspection and synchronization.
  2. Missing InitOnceInitialize, InitOnceBeginInitialize, and InitOnceComplete:
    • system.rs exports InitOnceExecuteOnce (polling with 200 µs sleeps) but lacks InitOnceInitialize, InitOnceBeginInitialize, and InitOnceComplete, which MSVC C++ std::call_once and runtime initialization resolve from api-ms-win-core-synch-l1-2-0.dll / kernel32.dll. Now that WaitOnAddress and WakeByAddressAll are on main (#556), synchronous INIT_ONCE waiters can block and wake directly on the INIT_ONCE word alongside INIT_ONCE_CHECK_ONLY, INIT_ONCE_ASYNC, and INIT_ONCE_INIT_FAILED.
  3. FlsAlloc destructor callbacks are ignored (system.rs:1661, docs/native-host.md:148):
    • FlsAlloc currently drops lpCallback, so per-thread CRT / library allocations stored via FlsSetValue are never cleaned up at thread exit or FlsFree.
  4. Win32 threadpool (SubmitThreadpoolWork / SetThreadpoolTimer) spawns ephemeral 2 MiB std::thread::spawn threads:
    • Title threads require 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 with SIGSEGV / exit 139). Pooling reusable 8 MiB worker threads for SubmitThreadpoolWork and giving SetThreadpoolTimer workers MIN_STACK avoids per-submission pthread/TEB churn and 2 MiB stack overflows.
<!-- greyfinch-sync:LakshanSivakumar/Native-Victory-macOS:issue:6659227469 --> Imported from [LakshanSivakumar/Native-Victory-macOS](https://github.com/LakshanSivakumar/Native-Victory-macOS/issues/559). Originally posted by [Sheffy007](https://github.com/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: 1. **Missing `DuplicateHandle`, `GetThreadId`, `ExitThread`, and stubbed `OpenThread`:** - `docs/native-host.md` lists `DuplicateHandle` in the `host_posix.rs` handle table, but `DuplicateHandle`, `GetThreadId`, and `ExitThread` are unimplemented, and `OpenThread` unconditionally fails with `ERROR_ACCESS_DENIED`. Duplicating `GetCurrentThread()` (`-2`) or opening a thread by ID is standard Win32/CRT practice for thread inspection and synchronization. 2. **Missing `InitOnceInitialize`, `InitOnceBeginInitialize`, and `InitOnceComplete`:** - `system.rs` exports `InitOnceExecuteOnce` (polling with `200 µs` sleeps) but lacks `InitOnceInitialize`, `InitOnceBeginInitialize`, and `InitOnceComplete`, which MSVC C++ `std::call_once` and runtime initialization resolve from `api-ms-win-core-synch-l1-2-0.dll` / `kernel32.dll`. Now that `WaitOnAddress` and `WakeByAddressAll` are on `main` (#556), synchronous `INIT_ONCE` waiters can block and wake directly on the `INIT_ONCE` word alongside `INIT_ONCE_CHECK_ONLY`, `INIT_ONCE_ASYNC`, and `INIT_ONCE_INIT_FAILED`. 3. **`FlsAlloc` destructor callbacks are ignored (`system.rs:1661`, `docs/native-host.md:148`):** - `FlsAlloc` currently drops `lpCallback`, so per-thread CRT / library allocations stored via `FlsSetValue` are never cleaned up at thread exit or `FlsFree`. 4. **Win32 threadpool (`SubmitThreadpoolWork` / `SetThreadpoolTimer`) spawns ephemeral 2 MiB `std::thread::spawn` threads:** - Title threads require `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 with `SIGSEGV` / exit 139). Pooling reusable 8 MiB worker threads for `SubmitThreadpoolWork` and giving `SetThreadpoolTimer` workers `MIN_STACK` avoids per-submission pthread/TEB churn and 2 MiB stack overflows.
Sheffy007 commented 2026-11-28 04:35:50 +00:00 (Migrated from github.com)
Author
Contributor

Imported from LakshanSivakumar/Native-Victory-macOS. Originally posted by Sheffy007 on 2026-11-28T04:35:50Z.


/claim

<!-- greyfinch-sync:LakshanSivakumar/Native-Victory-macOS:comment:7234139378 --> Imported from [LakshanSivakumar/Native-Victory-macOS](https://github.com/LakshanSivakumar/Native-Victory-macOS/issues/559#issuecomment-7234139378). Originally posted by [Sheffy007](https://github.com/Sheffy007) on 2026-11-28T04:35:50Z. --- /claim
github-actions[bot] commented 2026-11-28 04:36:04 +00:00 (Migrated from github.com)
Author
Contributor

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 #559 in its description within 24 hours, or the claim expires.

<!-- greyfinch-sync:LakshanSivakumar/Native-Victory-macOS:comment:7234141884 --> Imported from [LakshanSivakumar/Native-Victory-macOS](https://github.com/LakshanSivakumar/Native-Victory-macOS/issues/559#issuecomment-7234141884). Originally posted by [github-actions[bot]](https://github.com/apps/github-actions) on 2026-11-28T04:36:04Z. --- Claimed by @Sheffy007. Open a draft pull request with `Refs #559` in its description within 24 hours, or the claim expires.
AnastasisKon commented 2026-11-28 15:16:00 +00:00 (Migrated from github.com)
Author
Contributor

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 / InitOnceComplete over the word InitOnceExecuteOnce already uses (0 new, 1 running, context with bit 1 set done). INIT_ONCE_CHECK_ONLY reports a run under way as ERROR_IO_PENDING; a complete without a claim, or a second one, is ERROR_INVALID_PARAMETER; INIT_ONCE_INIT_FAILED hands the word back; INIT_ONCE_ASYNC is refused with ERROR_CALL_NOT_IMPLEMENTED because it hands back the slot the winner's context goes in and this host has nowhere to keep it. Commit a4ada0d0, with two tests and a docs/native-host.md row.
  • OpenThread (was a stub answering ERROR_ACCESS_DENIED) and GetThreadId. A title thread records its OS id when it starts, in a THREADS table holding the object weakly; an id whose thread has ended is ERROR_INVALID_PARAMETER. Commit e4122142, three tests, PR #31 in my fork.

DuplicateHandle was already there (handle #23). Both commits are on AnastasisKon/Native-Victory-macOS main, 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.

<!-- greyfinch-sync:LakshanSivakumar/Native-Victory-macOS:comment:7241333595 --> Imported from [LakshanSivakumar/Native-Victory-macOS](https://github.com/LakshanSivakumar/Native-Victory-macOS/issues/559#issuecomment-7241333595). Originally posted by [AnastasisKon](https://github.com/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` / `InitOnceComplete` over the word `InitOnceExecuteOnce` already uses (0 new, 1 running, context with bit 1 set done). `INIT_ONCE_CHECK_ONLY` reports a run under way as `ERROR_IO_PENDING`; a complete without a claim, or a second one, is `ERROR_INVALID_PARAMETER`; `INIT_ONCE_INIT_FAILED` hands the word back; `INIT_ONCE_ASYNC` is refused with `ERROR_CALL_NOT_IMPLEMENTED` because it hands back the slot the winner's context goes in and this host has nowhere to keep it. Commit a4ada0d0, with two tests and a `docs/native-host.md` row. - `OpenThread` (was a stub answering `ERROR_ACCESS_DENIED`) and `GetThreadId`. A title thread records its OS id when it starts, in a `THREADS` table holding the object weakly; an id whose thread has ended is `ERROR_INVALID_PARAMETER`. Commit e4122142, three tests, PR #31 in my fork. `DuplicateHandle` was already there (handle #23). Both commits are on `AnastasisKon/Native-Victory-macOS` main, 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.
AnastasisKon commented 2026-11-28 15:45:17 +00:00 (Migrated from github.com)
Author
Contributor

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 from host_posix, leaving the entry frame the loader recorded for the thread (bridge::exit_title_thread, the primitive _endthreadex already used), so the thread's DLL_THREAD_DETACH notices 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 new bridge::in_title_thread() says so and the call answers ERROR_CALL_NOT_IMPLEMENTED instead of aborting the process on exit_title_thread's precondition. probe_threads now starts a thread whose routine calls ExitThread(7) and stores a flag after it: the flag must stay clear and GetExitCodeThread must say 7.
  • FLS destructors (PR #39 there): the callback is kept per slot and runs with the value the calling thread holds — at the thread's end through a new exit-hook registry in victory_platform::thread that spawn_title_thread_with runs before DLL_THREAD_DETACH (armed by the first FlsAlloc that 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.

DuplicateHandle and OpenThread/GetThreadId had already landed; InitOnceBeginInitialize/InitOnceComplete too. Item 4 (pooled 8 MiB threadpool workers) is still open — MIN_STACK is 8 MiB for title threads, and the pool's workers are still per-submission.

<!-- greyfinch-sync:LakshanSivakumar/Native-Victory-macOS:comment:7241708893 --> Imported from [LakshanSivakumar/Native-Victory-macOS](https://github.com/LakshanSivakumar/Native-Victory-macOS/issues/559#issuecomment-7241708893). Originally posted by [AnastasisKon](https://github.com/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 from `host_posix`, leaving the entry frame the loader recorded for the thread (`bridge::exit_title_thread`, the primitive `_endthreadex` already used), so the thread's `DLL_THREAD_DETACH` notices 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 new `bridge::in_title_thread()` says so and the call answers `ERROR_CALL_NOT_IMPLEMENTED` instead of aborting the process on `exit_title_thread`'s precondition. `probe_threads` now starts a thread whose routine calls `ExitThread(7)` and stores a flag after it: the flag must stay clear and `GetExitCodeThread` must say 7. - **FLS destructors** (PR #39 there): the callback is kept per slot and runs with the value the calling thread holds — at the thread's end through a new exit-hook registry in `victory_platform::thread` that `spawn_title_thread_with` runs before `DLL_THREAD_DETACH` (armed by the first `FlsAlloc` that 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. `DuplicateHandle` and `OpenThread`/`GetThreadId` had already landed; `InitOnceBeginInitialize`/`InitOnceComplete` too. Item 4 (pooled 8 MiB threadpool workers) is still open — `MIN_STACK` is 8 MiB for title threads, and the pool's workers are still per-submission.
AnastasisKon commented 2026-11-28 16:07:30 +00:00 (Migrated from github.com)
Author
Contributor

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 the WaitFor...Callbacks accounting 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] minus gs:[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; ExitThread came with them.

<!-- greyfinch-sync:LakshanSivakumar/Native-Victory-macOS:comment:7241992341 --> Imported from [LakshanSivakumar/Native-Victory-macOS](https://github.com/LakshanSivakumar/Native-Victory-macOS/issues/559#issuecomment-7241992341). Originally posted by [AnastasisKon](https://github.com/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 the `WaitFor...Callbacks` accounting 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]` minus `gs:[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; `ExitThread` came with them.
Sign in to join this conversation.
No labels
No milestone
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Reference
LakshanSivakumar/Native-Victory-macOS#559
No description provided.