XAsync pool: idle workers end, bursts stop at a cap, delayed callbacks share one timer thread (rest of #228) #466

Open
github-bot wants to merge 2 commits from github-bot/gh-466 into main AGit
Owner

@⁠illuminatedfellow on GitHub · 2026-11-27 13:40 UTC · original
illuminatedfellow:xasync-pool-cap → main · +688 −56 · 1 files · 2 commits · open

Part of #228 (item 3). Stacked on #244: this branch is #244's commit plus one more, so review the last commit, or wait for #244 to merge and the diff here shrinks to it.

What changes, in crates/victory-hle/src/modules/xasync.rs:

  • 3.1, idle workers never exit: a pool worker that waits 30 s with nothing to do now ends.
  • 3.2, a thread per delayed callback: delayed callbacks (XTaskQueueSubmitDelayedCallback, XAsyncSchedule with a delay) now wait on one timer thread and are delivered on the pool, instead of each starting a thread that sleeps out its delay.
  • 3.3, bursts start about one worker per task: the pool starts workers up to twice the core count (at least 4). Past that, tasks wait for a worker, unless no worker has taken a task for 50 ms. Then the workers may all be blocked on callbacks still queued, so the pool starts one more, and checks again 50 ms later. This is option (a) from #228, the soft cap with a starvation escape. The go-ahead there didn't pick between (a) and (b); I went with (a) because a callback that waits on another still never starves. If you'd rather have (b), the hard cap with a logged warning, say so and I'll switch it.

Termination still takes a queue's callbacks back out of the pool and cancels them: the pool keeps titles' callbacks apart from its own jobs.

Tests added:

  • idle_pool_workers_end
  • a_burst_starts_no_more_workers_than_the_cap
  • a_stalled_pool_grows_past_its_cap
  • delayed_callbacks_do_not_start_a_thread_each: fails on #244 alone (200 threads for 200 delayed callbacks), passes here.

Tested on Linux with xasync.rs built alone with a stub for xgameruntime, since victory-hle needs Windows to build: all 20 xasync tests pass, 60 runs in release, 20 in debug and 15 under CPU load without a failure; clippy --all-targets -D warnings is clean on Rust 1.97. Not built with the pinned 1.98.1 or on Windows; CI here will be the first run of that.

**@⁠illuminatedfellow** on GitHub · 2026-11-27 13:40 UTC · [original](https://github.com/avatarmanz/VIctory-7900XTX/pull/466) `illuminatedfellow:xasync-pool-cap` → `main` · +688 −56 · 1 files · 2 commits · open Part of #228 (item 3). Stacked on #244: this branch is #244's commit plus one more, so review the last commit, or wait for #244 to merge and the diff here shrinks to it. What changes, in `crates/victory-hle/src/modules/xasync.rs`: - **3.1, idle workers never exit:** a pool worker that waits 30 s with nothing to do now ends. - **3.2, a thread per delayed callback:** delayed callbacks (`XTaskQueueSubmitDelayedCallback`, `XAsyncSchedule` with a delay) now wait on one timer thread and are delivered on the pool, instead of each starting a thread that sleeps out its delay. - **3.3, bursts start about one worker per task:** the pool starts workers up to twice the core count (at least 4). Past that, tasks wait for a worker, unless no worker has taken a task for 50 ms. Then the workers may all be blocked on callbacks still queued, so the pool starts one more, and checks again 50 ms later. This is option (a) from #228, the soft cap with a starvation escape. The go-ahead there didn't pick between (a) and (b); I went with (a) because a callback that waits on another still never starves. If you'd rather have (b), the hard cap with a logged warning, say so and I'll switch it. Termination still takes a queue's callbacks back out of the pool and cancels them: the pool keeps titles' callbacks apart from its own jobs. Tests added: - `idle_pool_workers_end` - `a_burst_starts_no_more_workers_than_the_cap` - `a_stalled_pool_grows_past_its_cap` - `delayed_callbacks_do_not_start_a_thread_each`: fails on #244 alone (200 threads for 200 delayed callbacks), passes here. Tested on Linux with `xasync.rs` built alone with a stub for `xgameruntime`, since `victory-hle` needs Windows to build: all 20 xasync tests pass, 60 runs in release, 20 in debug and 15 under CPU load without a failure; `clippy --all-targets -D warnings` is clean on Rust 1.97. Not built with the pinned 1.98.1 or on Windows; CI here will be the first run of that.
A cancel or complete on one thread could release a call while its
provider's DoWork or Cancel still ran on another: the provider's cleanup,
which frees its context, ran under them. Each call now counts the provider
operations under way (begin, DoWork, cancel, get result). Release marks the
call and cleans up at once if none runs, otherwise the last one to return
does; no operation starts once the call is released, so cleanup runs once
and last.

XTaskQueueTerminate called its callback straight away and ignored `wait`.
Callbacks already running, or waiting in the shared thread pool, still ran
after it, and draining a port canceled the callbacks of every composite
queue sharing it. Tasks now carry the queue they were submitted to, which
counts them until they have run, live or canceled. Terminate sets its flag
under that count's lock (a delivery checks it there, so a task either
reaches its port before the drain or is refused), cancels only its own
queue's tasks from the ports, the pool and the delay list, and completes
when the count reaches zero: it waits there when `wait` is set, otherwise
the last callback to return calls the termination callback. A delayed
callback is counted from submission, so one whose delay ran out just
before termination is canceled before termination completes.

Waiting from one of the queue's own callbacks would wait for the caller,
where before `wait` was ignored. Each thread keeps a stack of the queues
whose callbacks it runs (an immediate port nests them); Terminate called
from inside one of its queue's callbacks does not wait, and the callback's
end completes the termination.

Tests: cleanup waits for DoWork under a concurrent cancel; Terminate, with
and without wait, completes only after a running callback returns; a
composite queue's termination leaves the other queue's callbacks queued;
a callback terminating its own queue with wait returns, and termination
completes after it.

Not addressed: worker threads are still uncapped, and complete_call still
runs an immediate cleanup under the call's lock.

Part of #228

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Stacked on #244, which fixes the two races in #228. This is item 3:

- 3.1: a pool worker idle for 30 s now ends. Before, every worker a burst
  started stayed for the life of the process.
- 3.2: delayed callbacks (XTaskQueueSubmitDelayedCallback, XAsyncSchedule
  with a delay) wait on one timer thread and are delivered on the pool.
  Before, each one started its own thread that slept out the delay.
- 3.3: a burst starts workers up to twice the core count (at least 4).
  Past that, tasks wait for a worker, unless no worker has taken a task
  for 50 ms: then they may all be blocked on callbacks still queued, so
  the pool starts one more (and checks again 50 ms later). This is option
  (a) from #228: a callback that waits on another still never starves.

Termination still takes a queue's callbacks back out of the pool: the
pool keeps them apart from its own jobs.

Tests: idle_pool_workers_end, a_burst_starts_no_more_workers_than_the_cap,
a_stalled_pool_grows_past_its_cap, delayed_callbacks_do_not_start_a_thread_each.
The last one fails on #244 alone (200 threads for 200 delayed callbacks).
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-27 13:53:58 +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/466/head:github-bot/gh-466'
git switch 'github-bot/gh-466'

Merge

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