fix(libkernel): hand signalled semaphore tokens to queued waiters #5412

Closed
EagleBearerYS wants to merge 0 commits from upstream-pr/5412 into main
EagleBearerYS commented 2026-10-13 19:14:02 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by EagleBearerYS on 2026-10-13T19:14:02Z.


What

sceKernelSignalSema only added to the count and woke the waiters, each of which then had to take the count itself. A sceKernelPollSema or sceKernelWaitSema that ran first took the tokens instead, so a thread that had queued for them could time out or keep waiting. #4899 fixes the same steal for event flags.

sceKernelSignalSema now hands its tokens to the queued waiters in the order they queued, passing over one that needs more than is left, and only what remains stays on the count. A sceKernelWaitSema or sceKernelPollSema that finds enough tokens still takes them without queueing. sceKernelCancelSema reports and cancels the queued waiters as before. shadPS4's semaphore works the same way.

The second commit records that order in docs/dev/debt/libkernel.md. The priority order that SCE_KERNEL_SEMA_ATTR_TH_PRIO asks for is not modelled, as for event flags in #4899.

This PR is independent of #4899: the code is in different files, only the approach is the same.

No title is claimed.

Tested

On GitHub's runners from my fork, on main at 18c438b10 (run):

  • Linux: Ubuntu 24.04.5, GCC 13.3.0, Release with CI's configure flags and -DANYPS5_ENABLE_SPIRV_TOOLS=ON.
  • Windows: Windows Server 2025, WinLibs MinGW-w64 GCC 15.2.0 r7 (UCRT), Release with CI's configure flags.

On both:

  • New guest_semaphore_handoff, 20 rounds: a thread waits on an empty semaphore, the main thread signals one token and polls right away. The poll must fail with EBUSY, and the waiting thread must get the token. With main's Semaphore.cpp the test aborts; with the change it passes 20 runs in a row.
  • Every test registered under core/libs/prx/libc and core/libs/prx/libkernel passes: 69 and 125 on Linux, 90 and 128 on Windows, guest_semaphore included.
  • tools/check_conventions.py --base origin/main passes for both commits.
  • Not tested: macOS, any title.

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
  • Unblocks: none
  • AI-assisted: yes
<!-- greyfinch-sync:boykopovar/AnyPS5:issue:4848217447 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/5412). Originally posted by [EagleBearerYS](https://github.com/EagleBearerYS) on 2026-10-13T19:14:02Z. --- ### What `sceKernelSignalSema` only added to the count and woke the waiters, each of which then had to take the count itself. A `sceKernelPollSema` or `sceKernelWaitSema` that ran first took the tokens instead, so a thread that had queued for them could time out or keep waiting. #4899 fixes the same steal for event flags. `sceKernelSignalSema` now hands its tokens to the queued waiters in the order they queued, passing over one that needs more than is left, and only what remains stays on the count. A `sceKernelWaitSema` or `sceKernelPollSema` that finds enough tokens still takes them without queueing. `sceKernelCancelSema` reports and cancels the queued waiters as before. shadPS4's [semaphore](https://github.com/shadps4-emu/shadPS4/blob/06e813ff8c8e2a6d81b02cabfcc9543c3debf78e/src/core/libraries/kernel/threads/semaphore.cpp#L159-L206) works the same way. The second commit records that order in `docs/dev/debt/libkernel.md`. The priority order that `SCE_KERNEL_SEMA_ATTR_TH_PRIO` asks for is not modelled, as for event flags in #4899. This PR is independent of #4899: the code is in different files, only the approach is the same. No title is claimed. ### Tested On GitHub's runners from my fork, on `main` at 18c438b10 ([run](https://github.com/EagleBearerYS/AnyPS5/actions/runs/38451921961)): - Linux: Ubuntu 24.04.5, GCC 13.3.0, Release with CI's configure flags and `-DANYPS5_ENABLE_SPIRV_TOOLS=ON`. - Windows: Windows Server 2025, WinLibs MinGW-w64 GCC 15.2.0 r7 (UCRT), Release with CI's configure flags. On both: - New `guest_semaphore_handoff`, 20 rounds: a thread waits on an empty semaphore, the main thread signals one token and polls right away. The poll must fail with `EBUSY`, and the waiting thread must get the token. With `main`'s `Semaphore.cpp` the test aborts; with the change it passes 20 runs in a row. - Every test registered under `core/libs/prx/libc` and `core/libs/prx/libkernel` passes: 69 and 125 on Linux, 90 and 128 on Windows, `guest_semaphore` included. - `tools/check_conventions.py --base origin/main` passes for both commits. - Not tested: macOS, any title. ### 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] Unblocks: none - [x] AI-assisted: yes
oneandonlydean commented 2026-10-14 02:27:16 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by oneandonlydean on 2026-10-14T02:27:16Z.


Closing: no title is known to reach this. With the queue at 700+ open PRs, please hold off on new PRs that don't name a title needing the change until it's down. If a title stops on this on current main, reopen with the title ID and the log line.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6159522279 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/5412#issuecomment-6159522279). Originally posted by [oneandonlydean](https://github.com/oneandonlydean) on 2026-10-14T02:27:16Z. --- Closing: no title is known to reach this. With the queue at 700+ open PRs, please hold off on new PRs that don't name a title needing the change until it's down. If a title stops on this on current main, reopen with the title ID and the log line.

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!5412
No description provided.