hle(host_posix): implement DuplicateHandle, InitOnce*, FLS destructors, and pooled 8 MiB workers #564

Open
github-bot wants to merge 1 commit from github-bot/gh-560 into main AGit
Owner

@⁠Sheffy007 on GitHub · 2026-11-28 04:48 UTC · original
Sheffy007:feat/host-posix-threads-initonce-fls → main · +806 −76 · 3 files · 1 commits · open

Closes #563
Refs #460, #525

cc @⁠Olima34

Summary

  1. DuplicateHandle, OpenThread, GetThreadId, and ExitThread (host_posix.rs, host_posix/system.rs):
    • Implements DuplicateHandle (including DUPLICATE_CLOSE_SOURCE, DUPLICATE_SAME_ACCESS, and pseudo-handles GetCurrentProcess() / GetCurrentThread()), GetThreadId, ExitThread (via native::exit_title_thread), and OpenThread over tracked ThreadObject records (current_thread_object synthesizes a live record for title-main or host callback threads when duplicated or opened by ID).
  2. InitOnceInitialize, InitOnceBeginInitialize, and InitOnceComplete (host_posix/system.rs):
    • Implements synchronous INIT_ONCE (WaitOnAddress / WakeByAddressAll instead of 200 µs sleeps), INIT_ONCE_CHECK_ONLY, INIT_ONCE_INIT_FAILED retries, and INIT_ONCE_ASYNC alongside InitOnceExecuteOnce, and exports them across kernel32 / kernelbase / api-ms-win-core-*.
  3. FlsAlloc destructor callbacks (PFLS_CALLBACK_FUNCTION) (host_posix/system.rs, docs/native-host.md):
    • Tracks per-slot destructor callbacks and per-thread FLS vectors so FlsFree clears active slots across all live threads and invokes lpCallback(value) for non-null entries, and exiting threads run fls_thread_exit() before signaling their completion event (with a TLS Drop fallback for plain host threads).
  4. Reusable 8 MiB Win32 threadpool workers (SubmitThreadpoolWork / SetThreadpoolTimer):
    • Runs SubmitThreadpoolWork items on a reusable worker pool spawned with MIN_STACK (8 MiB) instead of spawning a short-lived 2 MiB std::thread::spawn per submission, and spawns SetThreadpoolTimer workers with MIN_STACK as well.

Verification

  • cargo test -p victory-hle --lib --target x86_64-apple-darwin host_posix --locked -- --test-threads=1 (183 passed)
  • cargo test -p victory-hle --test native_probe --target x86_64-apple-darwin --locked -- --test-threads=1 (17 passed)
  • cargo clippy --workspace --all-targets --locked --target x86_64-pc-windows-gnullvm -- -D warnings
  • cargo clippy --workspace --all-targets --all-features --locked --target x86_64-pc-windows-gnullvm -- -D warnings
  • cargo xtask policy
**@⁠Sheffy007** on GitHub · 2026-11-28 04:48 UTC · [original](https://github.com/LakshanSivakumar/Native-Victory-macOS/pull/560) `Sheffy007:feat/host-posix-threads-initonce-fls` → `main` · +806 −76 · 3 files · 1 commits · open Closes #563 Refs #460, #525 cc @⁠Olima34 ### Summary 1. **`DuplicateHandle`, `OpenThread`, `GetThreadId`, and `ExitThread` (`host_posix.rs`, `host_posix/system.rs`):** - Implements `DuplicateHandle` (including `DUPLICATE_CLOSE_SOURCE`, `DUPLICATE_SAME_ACCESS`, and pseudo-handles `GetCurrentProcess()` / `GetCurrentThread()`), `GetThreadId`, `ExitThread` (via `native::exit_title_thread`), and `OpenThread` over tracked `ThreadObject` records (`current_thread_object` synthesizes a live record for `title-main` or host callback threads when duplicated or opened by ID). 2. **`InitOnceInitialize`, `InitOnceBeginInitialize`, and `InitOnceComplete` (`host_posix/system.rs`):** - Implements synchronous `INIT_ONCE` (`WaitOnAddress` / `WakeByAddressAll` instead of `200 µs` sleeps), `INIT_ONCE_CHECK_ONLY`, `INIT_ONCE_INIT_FAILED` retries, and `INIT_ONCE_ASYNC` alongside `InitOnceExecuteOnce`, and exports them across `kernel32` / `kernelbase` / `api-ms-win-core-*`. 3. **`FlsAlloc` destructor callbacks (`PFLS_CALLBACK_FUNCTION`) (`host_posix/system.rs`, `docs/native-host.md`):** - Tracks per-slot destructor callbacks and per-thread FLS vectors so `FlsFree` clears active slots across all live threads and invokes `lpCallback(value)` for non-null entries, and exiting threads run `fls_thread_exit()` before signaling their completion event (with a TLS `Drop` fallback for plain host threads). 4. **Reusable 8 MiB Win32 threadpool workers (`SubmitThreadpoolWork` / `SetThreadpoolTimer`):** - Runs `SubmitThreadpoolWork` items on a reusable worker pool spawned with `MIN_STACK` (8 MiB) instead of spawning a short-lived 2 MiB `std::thread::spawn` per submission, and spawns `SetThreadpoolTimer` workers with `MIN_STACK` as well. ### Verification - `cargo test -p victory-hle --lib --target x86_64-apple-darwin host_posix --locked -- --test-threads=1` (183 passed) - `cargo test -p victory-hle --test native_probe --target x86_64-apple-darwin --locked -- --test-threads=1` (17 passed) - `cargo clippy --workspace --all-targets --locked --target x86_64-pc-windows-gnullvm -- -D warnings` - `cargo clippy --workspace --all-targets --all-features --locked --target x86_64-pc-windows-gnullvm -- -D warnings` - `cargo xtask policy`
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-28 04:49:38 +00:00
This pull request can be merged automatically.
This branch is out-of-date with the base branch
You are not authorized to merge this pull request.
View command line instructions

Checkout

From your project repository, check out a new branch and test the changes.
git fetch -u origin '+refs/pull/564/head:github-bot/gh-560'
git switch 'github-bot/gh-560'

Merge

Merge the changes and update on Forgejo.
git switch 'main'
git merge --no-ff 'github-bot/gh-560'
git switch 'github-bot/gh-560'
git rebase 'main'
git switch 'main'
git merge --ff-only 'github-bot/gh-560'
git switch 'github-bot/gh-560'
git rebase 'main'
git switch 'main'
git merge --no-ff 'github-bot/gh-560'
git switch 'main'
git merge --squash 'github-bot/gh-560'
git switch 'main'
git merge --ff-only 'github-bot/gh-560'
git switch 'main'
git merge 'github-bot/gh-560'
git push origin 'main'
This discussion has been locked. Commenting is limited to contributors.
No description provided.