fix(libSceAudioOut): raise the output thread priority on Windows #6922

Closed
WallisSkill wants to merge 1 commit from upstream-pr/6922 into main
WallisSkill commented 2026-10-16 18:52:50 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by WallisSkill on 2026-10-16T18:52:50Z.


What

fix(libSceAudioOut): raise the output thread priority on Windows.

Rebased on current main (82f0c9aec). main already serializes output per port with g_outputMutexes (bd9d10b4c), so this PR no longer adds its own per-port locks and keeps main's structure. Thanks to @blawesom for pointing this out. The rebase was redone from main, not taken from the handoff branch.

What remains is the priority change:

  • The thread that calls sceAudioOutOutput or sceAudioOutOutputs runs at THREAD_PRIORITY_HIGHEST on Windows. It is set once per thread, after the handle checks and before the port locks are taken.
  • If SetThreadPriority fails, the call throws with the Windows error code instead of continuing silently.
  • Linux and macOS are unchanged.

Tested

Windows 11 Pro 26200, MinGW GCC 15.2.0.

  • AudioOut.cpp compiles on current main with the project's flags. tools/check_conventions.py --base origin/main passes.
  • tools/pr_overlap.py: the only overlap is #6879 (WASM). It touches the same include block and line 114 of AudioOut.cpp, but not the same functions.
  • Cat Quest III (PPSA15065): tested with the earlier version of this PR, per-port locks plus this priority raise. With warm caches, no audible stutter. I have not run a title on the rebased version, which leaves out the locks that main now has.
  • Not run: Linux; a separate measurement of the priority raise alone.

Checklist

  • Based on current main; no other open PR implements the same functions
  • One topic per PR; follow-ups go in a new PR
  • No comments in code except technical debt
  • Unimplemented paths throw (NotImplemented_nid_no_patch); silent stubs are listed under Silent stubs in their library's technical debt file
  • No notes, investigation .md files or images added to the repository (attach them to this PR)
  • New third-party code is a submodule built from source
  • Generic behaviour, not specific to one title
  • Depends on: none
  • Fixes: none
  • Unblocks: none (audio stutter in PPSA15065 #5888; not a stop)
  • GPUs tested: n/a (audio only)
  • AI-assisted: yes (Claude Code); I reviewed and tested every change

🤖 Generated with Claude Code

<!-- greyfinch-sync:boykopovar/AnyPS5:issue:4892899811 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/6922). Originally posted by [WallisSkill](https://github.com/WallisSkill) on 2026-10-16T18:52:50Z. --- ### What **`fix(libSceAudioOut)`: raise the output thread priority on Windows.** Rebased on current `main` (82f0c9aec). `main` already serializes output per port with `g_outputMutexes` (bd9d10b4c), so this PR no longer adds its own per-port locks and keeps `main`'s structure. Thanks to @blawesom for pointing this out. The rebase was redone from `main`, not taken from the handoff branch. What remains is the priority change: - The thread that calls `sceAudioOutOutput` or `sceAudioOutOutputs` runs at `THREAD_PRIORITY_HIGHEST` on Windows. It is set once per thread, after the handle checks and before the port locks are taken. - If `SetThreadPriority` fails, the call throws with the Windows error code instead of continuing silently. - Linux and macOS are unchanged. ### Tested Windows 11 Pro 26200, MinGW GCC 15.2.0. - `AudioOut.cpp` compiles on current `main` with the project's flags. `tools/check_conventions.py --base origin/main` passes. - `tools/pr_overlap.py`: the only overlap is #6879 (WASM). It touches the same include block and line 114 of `AudioOut.cpp`, but not the same functions. - **Cat Quest III (PPSA15065):** tested with the earlier version of this PR, per-port locks plus this priority raise. With warm caches, no audible stutter. I have not run a title on the rebased version, which leaves out the locks that `main` now has. - Not run: Linux; a separate measurement of the priority raise alone. ### Checklist <!-- Rules behind each item: https://github.com/boykopovar/AnyPS5/blob/main/CONTRIBUTING.md --> - [x] Based on current `main`; no other open PR implements the same functions - [x] One topic per PR; follow-ups go in a new PR - [x] No comments in code except [technical debt](https://github.com/boykopovar/AnyPS5/blob/main/docs/dev/CONVENTIONS.md) - [x] Unimplemented paths throw (`NotImplemented_nid_no_patch`); silent stubs are listed under Silent stubs in their library's [technical debt](https://github.com/boykopovar/AnyPS5/tree/main/docs/dev/debt) file - [x] No notes, investigation `.md` files or images added to the repository (attach them to this PR) - [x] New third-party code is a submodule built from source - [x] Generic behaviour, not specific to one title - [x] Depends on: none - [x] Fixes: none - [x] Unblocks: none (audio stutter in PPSA15065 #5888; not a stop) - [x] GPUs tested: n/a (audio only) - [x] AI-assisted: yes (Claude Code); I reviewed and tested every change 🤖 Generated with [Claude Code](https://claude.com/claude-code)
github-actions[bot] commented 2026-10-16 19:16:29 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by github-actions[bot] on 2026-10-16T19:16:29Z.


Some comparisons could not be completed because a pull request conflicts with the base branch. Review these shared files manually before concluding that there is no overlap.

Pull request Shared files not compared
PR 5965 core/libs/prx/libSceAudioOut/src/AudioOut.cpp
PR 6879 core/libs/prx/libSceAudioOut/src/AudioOut.cpp
<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6222910672 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/6922#issuecomment-6222910672). Originally posted by [github-actions[bot]](https://github.com/apps/github-actions) on 2026-10-16T19:16:29Z. --- <!-- pr-overlap --> Some comparisons could not be completed because a pull request conflicts with the base branch. Review these shared files manually before concluding that there is no overlap. | Pull request | Shared files not compared | |---|---| | [PR 5965](https://www.github.com/boykopovar/AnyPS5/pull/5965) | `core/libs/prx/libSceAudioOut/src/AudioOut.cpp` | | [PR 6879](https://www.github.com/boykopovar/AnyPS5/pull/6879) | `core/libs/prx/libSceAudioOut/src/AudioOut.cpp` |
blawesom commented 2026-10-17 08:52:26 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by blawesom on 2026-10-17T08:52:26Z.


Rebase handoff, with a heads-up — main landed the same per-port output serialization independently (g_outputMutexes), so this PR's remaining delta after the rebase is the output thread priority raise (raiseOutputThreadPriority before the port locks — three call sites) plus stylistic guard differences; the merged resolution keeps main's structure and layers your priority calls onto it. If the priority raise alone is the intent, the PR may want retitling/reducing to that; the serialization itself is already on main.

Verified on Linux (CachyOS, kernel 7.2.9, GCC 16.2.1, Ninja, Release, BUILD_TESTING=ON, AMD RX 9070 XT / RADV):

  • builds; tools/check_conventions.py --base origin/main passes
  • full suite: 1335/1336 — the only failure is the known main regression (agc_driver_tessellation segfault, #4727), not this PR

To pick it up:

git fetch https://github.com/blawesom/AnyPS5 rebase/6922
git checkout <your branch>
git merge --ff-only FETCH_HEAD

Head is 3d926f36c.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6231968410 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/6922#issuecomment-6231968410). Originally posted by [blawesom](https://github.com/blawesom) on 2026-10-17T08:52:26Z. --- **Rebase handoff, with a heads-up** — `main` landed the same per-port output serialization independently (`g_outputMutexes`), so this PR's remaining delta after the rebase is the **output thread priority raise** (`raiseOutputThreadPriority` before the port locks — three call sites) plus stylistic guard differences; the merged resolution keeps `main`'s structure and layers your priority calls onto it. If the priority raise alone is the intent, the PR may want retitling/reducing to that; the serialization itself is already on `main`. Verified on Linux (CachyOS, kernel 7.2.9, GCC 16.2.1, Ninja, Release, `BUILD_TESTING=ON`, AMD RX 9070 XT / RADV): - builds; `tools/check_conventions.py --base origin/main` passes - **full suite: 1335/1336** — the only failure is the known `main` regression (`agc_driver_tessellation` segfault, #4727), not this PR **To pick it up:** ``` git fetch https://github.com/blawesom/AnyPS5 rebase/6922 git checkout <your branch> git merge --ff-only FETCH_HEAD ``` Head is `3d926f36c`.
WallisSkill commented 2026-10-18 17:51:15 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by WallisSkill on 2026-10-18T17:51:15Z.


@blawesom thanks for the rebase handoff and the Linux run.

I took your suggestion and narrowed the PR. It is rebased on main (base 82f0c9aec) and keeps only the Windows output thread priority raise, since main's g_outputMutexes already serializes output per port. The title and description now match.

What remains: raiseOutputThreadPriority() sets THREAD_PRIORITY_HIGHEST once per thread (a thread_local flag) in sceAudioOutOutput and sceAudioOutOutputs, before the port locks are taken. If SetThreadPriority fails, it throws with GetLastError(). Linux and macOS are unchanged.

Tested: AudioOut.cpp builds with MinGW GCC 15.2.0, and tools/check_conventions.py --base origin/main passes. The earlier version (per-port locks plus the priority raise) played Cat Quest III (PPSA15065) without audible stutter. The rebased priority-only version has not been run on a title yet, and the priority change has not been measured on its own.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6253151370 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/6922#issuecomment-6253151370). Originally posted by [WallisSkill](https://github.com/WallisSkill) on 2026-10-18T17:51:15Z. --- @blawesom thanks for the rebase handoff and the Linux run. I took your suggestion and narrowed the PR. It is rebased on main (base 82f0c9aec) and keeps only the Windows output thread priority raise, since main's `g_outputMutexes` already serializes output per port. The title and description now match. What remains: `raiseOutputThreadPriority()` sets `THREAD_PRIORITY_HIGHEST` once per thread (a `thread_local` flag) in `sceAudioOutOutput` and `sceAudioOutOutputs`, before the port locks are taken. If `SetThreadPriority` fails, it throws with `GetLastError()`. Linux and macOS are unchanged. Tested: `AudioOut.cpp` builds with MinGW GCC 15.2.0, and `tools/check_conventions.py --base origin/main` passes. The earlier version (per-port locks plus the priority raise) played Cat Quest III (PPSA15065) without audible stutter. The rebased priority-only version has not been run on a title yet, and the priority change has not been measured on its own.
adriadam10 commented 2026-10-19 11:14:58 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by adriadam10 on 2026-10-19T11:14:58Z.

Review: CHANGES_REQUESTED


Thanks for narrowing this down. A few points before it can merge:

  • AudioOut.cpp:135-144: main already has a thread-priority mechanism (core/libs/prx/libSceAgcDriver/Execution/src/ThreadPriority.cpp, APS5_THREAD_PRIORITY, off by default, logs what it set). This change raises a guest-called thread to THREAD_PRIORITY_HIGHEST unconditionally, bypassing that opt-in, with no way to turn it off and no log. Please either route it through the same mode/Raise path or explain why audio output should differ.
  • AudioOut.cpp:140: if SetThreadPriority fails (restricted token, policy), the call throws and the title loses audio. For a performance hint a failure should not kill the output; if you keep throwing, say why that is right here.
  • Evidence: the PR body says the stutter fix was tested with per-port locks plus the priority raise, and the rebased version (priority only) was not run on a title. main's g_outputMutexes may be what fixed it. Please show a before/after with the priority raise alone (e.g. Cat Quest III, same scene, with and without it), or a measurement of output-call latency under CPU load.
  • No test covers this and it can't run on Linux CI; the measurement above is the evidence we need.
  • If the priority raise stays unconditional, add a line under the library's technical debt file describing the deviation (the guest's own thread priority is overridden).
<!-- greyfinch-sync:boykopovar/AnyPS5:review:5570716092 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/6922#pullrequestreview-5570716092). Originally posted by [adriadam10](https://github.com/adriadam10) on 2026-10-19T11:14:58Z. **Review:** CHANGES_REQUESTED --- Thanks for narrowing this down. A few points before it can merge: - `AudioOut.cpp:135-144`: `main` already has a thread-priority mechanism (`core/libs/prx/libSceAgcDriver/Execution/src/ThreadPriority.cpp`, `APS5_THREAD_PRIORITY`, off by default, logs what it set). This change raises a guest-called thread to `THREAD_PRIORITY_HIGHEST` unconditionally, bypassing that opt-in, with no way to turn it off and no log. Please either route it through the same mode/`Raise` path or explain why audio output should differ. - `AudioOut.cpp:140`: if `SetThreadPriority` fails (restricted token, policy), the call throws and the title loses audio. For a performance hint a failure should not kill the output; if you keep throwing, say why that is right here. - Evidence: the PR body says the stutter fix was tested with per-port locks plus the priority raise, and the rebased version (priority only) was not run on a title. `main`'s `g_outputMutexes` may be what fixed it. Please show a before/after with the priority raise alone (e.g. Cat Quest III, same scene, with and without it), or a measurement of output-call latency under CPU load. - No test covers this and it can't run on Linux CI; the measurement above is the evidence we need. - If the priority raise stays unconditional, add a line under the library's technical debt file describing the deviation (the guest's own thread priority is overridden).
WallisSkill commented 2026-10-19 12:45:54 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by WallisSkill on 2026-10-19T12:45:54Z.


@adriadam10 thanks for the careful review; you were right. I measured main without this PR (c0607cc4b, priority untouched) in Cat Quest III (PPSA15065) on Windows 11, with a local counter on the main output port. Over about 8 minutes of play, 91,913 outputs of 5.3 ms went out with 0 underruns (the SDL queue was never empty when the next block arrived). The longest gap between two sceAudioOutOutput calls was 13.6 ms, and the queue never held less than 37 ms of audio. No stutter was audible. So the stutter this PR started from was fixed by the per-port serialization that main already has, and the priority raise has nothing left to fix. I'm closing this PR. If a measurement under CPU load ever shows output calls being starved, I'll come back with an opt-in through APS5_THREAD_PRIORITY that logs instead of throwing, plus a debt entry.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6267533943 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/6922#issuecomment-6267533943). Originally posted by [WallisSkill](https://github.com/WallisSkill) on 2026-10-19T12:45:54Z. --- @adriadam10 thanks for the careful review; you were right. I measured main without this PR (c0607cc4b, priority untouched) in Cat Quest III (PPSA15065) on Windows 11, with a local counter on the main output port. Over about 8 minutes of play, 91,913 outputs of 5.3 ms went out with 0 underruns (the SDL queue was never empty when the next block arrived). The longest gap between two `sceAudioOutOutput` calls was 13.6 ms, and the queue never held less than 37 ms of audio. No stutter was audible. So the stutter this PR started from was fixed by the per-port serialization that main already has, and the priority raise has nothing left to fix. I'm closing this PR. If a measurement under CPU load ever shows output calls being starved, I'll come back with an opt-in through `APS5_THREAD_PRIORITY` that logs instead of throwing, plus a debt entry.

Pull request closed

Sign in to join this conversation.
No reviewers
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
boykopovar/AnyPS5!6922
No description provided.