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

Merged
EagleBearerYS merged 14 commits from upstream-pr/9491 into main 2026-11-02 07:57:53 +00:00
EagleBearerYS commented 2026-10-21 23:19:50 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by EagleBearerYS on 2026-10-21T23:19:50Z.


Opened again from #5412: title-less conformance fixes of this kind are merged now, for example #7365 and #8411.

What

sceKernelSignalSema only adds to the count and wakes the waiters, each of which then has to take the count itself. A sceKernelPollSema or sceKernelWaitSema that runs first takes the tokens instead, so a thread that had queued for them can time out or keep waiting. Event flags on main already hand their bits to queued waiters when the pattern is set, since #6737.

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 APS5_TRACE_SYNC lines main added to these calls are kept; a wait that finds enough tokens prints its begin and end lines too.

A semaphore created with SCE_KERNEL_SEMA_ATTR_TH_PRIO (0x02) queues a waiter by the priority its thread has when it starts to wait: before the first queued waiter with a numerically higher, that is lower, priority, so the signal serves higher-priority threads first and equal ones in arrival order. Attributes 1 and 0 keep arrival order.

The second commit records that order in docs/dev/debt/libkernel.md. 0a353f80ef, after MarcZX10's review, adds the priority order and its debt sentence: a priority change while a thread waits does not move it, and the console's order and its rule for a waiter that needs more than is left are unverified.

2132238f45, after oneandonlydean's review, makes sceKernelWaitSema return EACCES for a semaphore that sceKernelDeleteSema is deleting before it looks at the free tokens, as main does. Before it, a wait that found enough tokens took them from a semaphore being deleted.

No title is claimed.

Overlap: #9566 (DavidSamir) changes the tcsendbreak … tcsetpgrp entry of docs/dev/debt/libkernel.md, right after the semaphore entry this PR adds, and the two conflict in that file only. Neither needs to wait for the other: whichever merges second keeps both entries, and this PR takes the rebase in either order.

#10558 (ZSWolt) adds a handle lookup at the top of sceKernelSignalSema, sceKernelWaitSema and sceKernelCancelSema, where this PR changes the bodies, and the two conflict in Semaphore.cpp. Agreed order: #10558 first (comment). Once it merges, this PR merges main and keeps the token handoff after that lookup.

Tested

On GitHub's runners from my fork, merged with main at a6dcea9279 (run), on both Linux and Windows:

  • 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 and guest_semaphore pass 10 runs in a row.
  • Every test registered under core/libs/prx/libc and core/libs/prx/libkernel passes: 115 and 198 on Linux, 142 and 208 on Windows. In the first Windows attempt guest_raise_in_system_call timed out after 30 s; the rerun of that job passed it with the rest.
  • 0a353f80ef (run): guest_semaphore_handoff gains two-waiter cases. A waiter that needs 2, queued before one that needs 1, must be passed over by a signal of 1 and served by the next signal of 2. Two waiters must be served in arrival order with attribute 1, whatever their priorities, and with attribute 0x02 by priority (767 and 256 either way round) or in arrival order when equal. With the previous head, which serves both attributes in arrival order, the test fails 3 of 3 on both runners; with the commit guest_semaphore_handoff, guest_semaphore and guest_semaphore_trace pass 10 of 10, and the libc and libkernel suites pass. semaphore_trace_tests compiles Semaphore.cpp on its own, so it gains stubs for the two pthread calls the priority lookup uses.
  • After the merge with main at da54c652e3 (d1604d79c9, no conflicts, the PR's own diff unchanged; run): guest_semaphore_handoff passes 10 runs in a row on both runners. The libc and libkernel suites pass on Linux (116, 198); on Windows libc passes (143) and libkernel's guest_raise_in_system_call timed out once. Run alone with guest_semaphore_handoff (run), it passes 20 runs in a row on Windows, and that run's libkernel suite passes (208), as in the first Windows attempt above.
  • After the merge with main at fa515def85 (406e9bc542; run): the only conflict was in docs/dev/debt/libkernel.md, where main added its kevent measurements entry at the place this PR adds its semaphore entry, and both are kept. The PR's own added and removed lines are unchanged. guest_semaphore_handoff, guest_semaphore and guest_semaphore_trace pass ×10: 3 run, 3 pass, 0 skip, 0 fail on Linux and on Windows. The libc and libkernel suites pass on both (121 and 205 tests on Linux, 148 and 217 on Windows), run with APS5_SKIP_TIMING_TESTS=1, which relaxes precise_wait's median bounds on GitHub's hosted runners.
  • tools/check_conventions.py --base origin/main passes.
  • Not tested: macOS, a console, any title.
  • 87b2a04b4f and a302f0c516, merges of main 85f8e11372 and 518d36eaec (run, run). Both conflicted only in docs/dev/debt/libkernel.md, where this PR adds its sceKernelWaitSema and sceKernelSignalSema entry after the sem_timedwait one: orezraey's #10137 rewrote that entry with a PS5's results, and xXPBSXx's #10105 then added a title's call site to it. Each merge keeps main's entry and this PR's, and this PR's other changed lines are the same. The first run took the semaphore tests with the tests for every file the first merge changes, 10 runs each. Linux: 828 run, 813 pass, 15 skip (seven AGC tests on lavapipe and eight video-out tests with no display), 0 fail. Windows: 850 run, 755 pass, 95 skip (85 AGC tests, nine video-out tests and guest_job_manager_profiler_gpu, with no Vulkan device or display), 0 fail. The second took the semaphore tests with the tests for the files the second merge changes, 10 runs each. Linux: 25 run, 25 pass, 0 skip, 0 fail. Windows: 24 run, 24 pass, 0 skip, 0 fail. Suites: libc and libkernel pass on Linux (145 and 213) and on Windows (174 and 234). The suites ran with APS5_SKIP_TIMING_TESTS=1, which relaxes precise_wait's median bounds on GitHub's hosted runners.
  • 2132238f45, after oneandonlydean's review, on af93ca4e6c, a merge of main ae0e0a284b (merge run, fix run).
    • The merge conflicted in Semaphore.cpp (an include next to one main added) and in docs/dev/debt/libkernel.md, where main added its kevent entry next to this PR's semaphore entry; both keep main's lines and this PR's. orezraey's argument checks from main (b7e5eb7f31) merged without a conflict. This PR's own added and removed lines are the same. Without the new case, guest_semaphore, guest_semaphore_handoff and guest_semaphore_trace pass 10 runs in a row on the merge. Linux: 3 run, 3 pass, 0 skip, 0 fail. Windows: 3 run, 3 pass, 0 skip, 0 fail.
    • 2132238f45 adds a guest_semaphore_handoff case: it sets deleted on a semaphore holding 2 tokens, as sceKernelDeleteSema does while it waits for its waiters, and checks that a wait for 1 returns EACCES and leaves both tokens. With the merge's code the test fails 3 of 3 on both runners. With the commit the three tests pass 10 runs in a row. Linux: 3 run, 3 pass, 0 skip, 0 fail. Windows: 3 run, 3 pass, 0 skip, 0 fail.
    • Suites: libc and libkernel pass on Linux (161 and 265). On Windows libc passes (189) and libkernel passes 284 of 287: exec_arguments, guest_timed_join_import_libkernel_sys and guest_timed_join_import_libkernel_web fail at their setup in the verify's Windows build (FileNotFoundError starting the title, or copying the libraries they load) with and without the commit, and pass in main's own Windows job at 1829def9bf (job). The suites ran with APS5_SKIP_TIMING_TESTS=1, which relaxes precise_wait's median bounds on GitHub's hosted runners.
  • 8b1608d2de and c87a1fb54e, after oneandonlydean's second review (run). 8b1608d2de removes the local pthread declarations from Semaphore.cpp and GuestSemaphoreHandoff.cpp, which now include prx/libkernel/include/Api.hpp; the test keeps the internal semaphore header for the fixture state. c87a1fb54e makes the first handoff loop wait with WaitForQueue(sem, 1) before signalling, in place of the started flag and a 50 ms sleep. guest_semaphore_handoff, guest_semaphore, guest_semaphore_trace, guest_pthread_sem, guest_sem_canceled_wake and guest_sem_canceled_wake_coarse pass 10 runs in a row. Linux: 6 run, 6 pass, 0 skip, 0 fail. Windows: 6 run, 6 pass, 0 skip, 0 fail. The libc and libkernel suites run 161 and 265 tests on Linux and 189 and 287 on Windows, with 0 failed.
  • e6d4929f65, a merge of main 1db077e876 (run). The one conflict was in docs/dev/debt/libkernel.md, where main adds kallmek3nny's sem_setname entry (#11267) where this PR adds its semaphore entry; the merge keeps both. This PR's own added and removed lines are the same. The same six tests pass 10 runs in a row. Linux: 6 run, 6 pass, 0 skip, 0 fail. Windows: 6 run, 6 pass, 0 skip, 0 fail. The libc and libkernel suites run 191 and 293 tests on Linux and 220 and 316 on Windows, with 0 failed. The suites ran with APS5_SKIP_TIMING_TESTS=1, which relaxes precise_wait's median bounds on GitHub's hosted runners.
  • 7a6f4ee910, a merge of main 1b91748340, which brings akkadbakkad89's #13600 (run).
    • The conflict: sceKernelCreateSema in Semaphore.cpp. #13600 accepts attribute bit 0x100, in place of main's NotImplemented_nid_no_patch for it, and reads the FIFO mode as (attr & 3u) == 1u.
    • The resolution: main's lines are kept, and this PR's priority-queue flag reads the low two bits the same way, (attr & 3u) == 2u in place of attr == 2, so 0x102 still queues by priority. The merge commit's message names that line. This PR's other added and removed lines are the same.
    • What the tests cover: guest_semaphore (#13600's case) creates semaphores with 0x100, 0x101 and 0x102 and checks only that they're accepted. guest_semaphore_handoff checks the queue order with attributes 1 and 2 only. So no test here runs the masked read's priority order with bit 0x100 set.
    • ×10: guest_semaphore_handoff, guest_semaphore, guest_semaphore_trace, guest_pthread_sem, guest_sem_canceled_wake and guest_sem_canceled_wake_coarse.
      • Linux: 6 run, 6 pass, 0 skip, 0 fail.
      • Windows: 6 run, 6 pass, 0 skip, 0 fail.
      • The libc and libkernel suites run 199 and 309 tests on Linux and 226 and 331 on Windows, with 0 failed. The suites ran with APS5_SKIP_TIMING_TESTS=1, which relaxes precise_wait's median bounds on GitHub's hosted runners.

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: #10558 (merge order only: it adds a handle lookup at the top of sceKernelSignalSema, sceKernelWaitSema and sceKernelCancelSema, where this PR changes the bodies; this PR goes after it). No order with #9566, see Overlap; merge order with orezraey's #11195 (the semaphore code and its debt entry): whichever merges second keeps both
  • Fixes: none
  • Unblocks: none
  • GPUs tested: n/a
  • AI-assisted: yes
<!-- greyfinch-sync:boykopovar/AnyPS5:issue:4959666429 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/9491). Originally posted by [EagleBearerYS](https://github.com/EagleBearerYS) on 2026-10-21T23:19:50Z. --- Opened again from #5412: title-less conformance fixes of this kind are merged now, for example #7365 and #8411. ### What `sceKernelSignalSema` only adds to the count and wakes the waiters, each of which then has to take the count itself. A `sceKernelPollSema` or `sceKernelWaitSema` that runs first takes the tokens instead, so a thread that had queued for them can time out or keep waiting. Event flags on `main` already hand their bits to queued waiters when the pattern is set, since #6737. `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 `APS5_TRACE_SYNC` lines `main` added to these calls are kept; a wait that finds enough tokens prints its begin and end lines too. A semaphore created with `SCE_KERNEL_SEMA_ATTR_TH_PRIO` (0x02) queues a waiter by the priority its thread has when it starts to wait: before the first queued waiter with a numerically higher, that is lower, priority, so the signal serves higher-priority threads first and equal ones in arrival order. Attributes 1 and 0 keep arrival order. The second commit records that order in `docs/dev/debt/libkernel.md`. 0a353f80ef, after MarcZX10's review, adds the priority order and its debt sentence: a priority change while a thread waits does not move it, and the console's order and its rule for a waiter that needs more than is left are unverified. 2132238f45, after oneandonlydean's review, makes `sceKernelWaitSema` return `EACCES` for a semaphore that `sceKernelDeleteSema` is deleting before it looks at the free tokens, as `main` does. Before it, a wait that found enough tokens took them from a semaphore being deleted. No title is claimed. **Overlap:** #9566 (DavidSamir) changes the `tcsendbreak … tcsetpgrp` entry of `docs/dev/debt/libkernel.md`, right after the semaphore entry this PR adds, and the two conflict in that file only. Neither needs to wait for the other: whichever merges second keeps both entries, and this PR takes the rebase in either order. #10558 (ZSWolt) adds a handle lookup at the top of `sceKernelSignalSema`, `sceKernelWaitSema` and `sceKernelCancelSema`, where this PR changes the bodies, and the two conflict in `Semaphore.cpp`. Agreed order: #10558 first ([comment](https://github.com/boykopovar/AnyPS5/pull/9491#issuecomment-6383687464)). Once it merges, this PR merges `main` and keeps the token handoff after that lookup. ### Tested On GitHub's runners from my fork, merged with `main` at a6dcea9279 ([run](https://github.com/EagleBearerYS/AnyPS5/actions/runs/39686408336)), on both Linux and Windows: - 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 and `guest_semaphore` pass 10 runs in a row. - Every test registered under `core/libs/prx/libc` and `core/libs/prx/libkernel` passes: 115 and 198 on Linux, 142 and 208 on Windows. In the first Windows attempt `guest_raise_in_system_call` timed out after 30 s; the rerun of that job passed it with the rest. - 0a353f80ef ([run](https://github.com/EagleBearerYS/AnyPS5/actions/runs/39695243095)): `guest_semaphore_handoff` gains two-waiter cases. A waiter that needs 2, queued before one that needs 1, must be passed over by a signal of 1 and served by the next signal of 2. Two waiters must be served in arrival order with attribute 1, whatever their priorities, and with attribute 0x02 by priority (767 and 256 either way round) or in arrival order when equal. With the previous head, which serves both attributes in arrival order, the test fails 3 of 3 on both runners; with the commit `guest_semaphore_handoff`, `guest_semaphore` and `guest_semaphore_trace` pass 10 of 10, and the libc and libkernel suites pass. `semaphore_trace_tests` compiles `Semaphore.cpp` on its own, so it gains stubs for the two pthread calls the priority lookup uses. - **After the merge with `main` at da54c652e3** (d1604d79c9, no conflicts, the PR's own diff unchanged; [run](https://github.com/EagleBearerYS/AnyPS5/actions/runs/39728867414)): `guest_semaphore_handoff` passes 10 runs in a row on both runners. The libc and libkernel suites pass on Linux (116, 198); on Windows libc passes (143) and libkernel's `guest_raise_in_system_call` timed out once. Run alone with `guest_semaphore_handoff` ([run](https://github.com/EagleBearerYS/AnyPS5/actions/runs/39730561441)), it passes 20 runs in a row on Windows, and that run's libkernel suite passes (208), as in the first Windows attempt above. - **After the merge with `main` at fa515def85** (406e9bc542; [run](https://github.com/EagleBearerYS/AnyPS5/actions/runs/40088830631)): the only conflict was in `docs/dev/debt/libkernel.md`, where `main` added its kevent measurements entry at the place this PR adds its semaphore entry, and both are kept. The PR's own added and removed lines are unchanged. `guest_semaphore_handoff`, `guest_semaphore` and `guest_semaphore_trace` pass ×10: 3 run, 3 pass, 0 skip, 0 fail on Linux and on Windows. The libc and libkernel suites pass on both (121 and 205 tests on Linux, 148 and 217 on Windows), run with `APS5_SKIP_TIMING_TESTS=1`, which relaxes `precise_wait`'s median bounds on GitHub's hosted runners. - `tools/check_conventions.py --base origin/main` passes. - Not tested: macOS, a console, any title. - **87b2a04b4f and a302f0c516, merges of `main` 85f8e11372 and 518d36eaec** ([run](https://github.com/EagleBearerYS/AnyPS5/actions/runs/40340545597), [run](https://github.com/EagleBearerYS/AnyPS5/actions/runs/40356755575)). Both conflicted only in `docs/dev/debt/libkernel.md`, where this PR adds its `sceKernelWaitSema` and `sceKernelSignalSema` entry after the `sem_timedwait` one: orezraey's #10137 rewrote that entry with a PS5's results, and xXPBSXx's #10105 then added a title's call site to it. Each merge keeps `main`'s entry and this PR's, and this PR's other changed lines are the same. The first run took the semaphore tests with the tests for every file the first merge changes, 10 runs each. Linux: 828 run, 813 pass, 15 skip (seven AGC tests on lavapipe and eight video-out tests with no display), 0 fail. Windows: 850 run, 755 pass, 95 skip (85 AGC tests, nine video-out tests and `guest_job_manager_profiler_gpu`, with no Vulkan device or display), 0 fail. The second took the semaphore tests with the tests for the files the second merge changes, 10 runs each. Linux: 25 run, 25 pass, 0 skip, 0 fail. Windows: 24 run, 24 pass, 0 skip, 0 fail. Suites: libc and libkernel pass on Linux (145 and 213) and on Windows (174 and 234). The suites ran with `APS5_SKIP_TIMING_TESTS=1`, which relaxes `precise_wait`'s median bounds on GitHub's hosted runners. - **2132238f45, after oneandonlydean's review, on af93ca4e6c, a merge of `main` ae0e0a284b** ([merge run](https://github.com/EagleBearerYS/AnyPS5/actions/runs/40625460448), [fix run](https://github.com/EagleBearerYS/AnyPS5/actions/runs/40625694771)). - The merge conflicted in `Semaphore.cpp` (an include next to one `main` added) and in `docs/dev/debt/libkernel.md`, where `main` added its kevent entry next to this PR's semaphore entry; both keep `main`'s lines and this PR's. orezraey's argument checks from `main` (b7e5eb7f31) merged without a conflict. This PR's own added and removed lines are the same. Without the new case, `guest_semaphore`, `guest_semaphore_handoff` and `guest_semaphore_trace` pass 10 runs in a row on the merge. Linux: 3 run, 3 pass, 0 skip, 0 fail. Windows: 3 run, 3 pass, 0 skip, 0 fail. - 2132238f45 adds a `guest_semaphore_handoff` case: it sets `deleted` on a semaphore holding 2 tokens, as `sceKernelDeleteSema` does while it waits for its waiters, and checks that a wait for 1 returns `EACCES` and leaves both tokens. With the merge's code the test fails 3 of 3 on both runners. With the commit the three tests pass 10 runs in a row. Linux: 3 run, 3 pass, 0 skip, 0 fail. Windows: 3 run, 3 pass, 0 skip, 0 fail. - Suites: libc and libkernel pass on Linux (161 and 265). On Windows libc passes (189) and libkernel passes 284 of 287: `exec_arguments`, `guest_timed_join_import_libkernel_sys` and `guest_timed_join_import_libkernel_web` fail at their setup in the verify's Windows build (`FileNotFoundError` starting the title, or copying the libraries they load) with and without the commit, and pass in `main`'s own Windows job at 1829def9bf ([job](https://github.com/boykopovar/AnyPS5/actions/runs/40627345977/job/122615375186)). The suites ran with `APS5_SKIP_TIMING_TESTS=1`, which relaxes `precise_wait`'s median bounds on GitHub's hosted runners. - **8b1608d2de and c87a1fb54e, after oneandonlydean's second review** ([run](https://github.com/EagleBearerYS/AnyPS5/actions/runs/40842874789)). 8b1608d2de removes the local pthread declarations from `Semaphore.cpp` and `GuestSemaphoreHandoff.cpp`, which now include `prx/libkernel/include/Api.hpp`; the test keeps the internal semaphore header for the fixture state. c87a1fb54e makes the first handoff loop wait with `WaitForQueue(sem, 1)` before signalling, in place of the `started` flag and a 50 ms sleep. `guest_semaphore_handoff`, `guest_semaphore`, `guest_semaphore_trace`, `guest_pthread_sem`, `guest_sem_canceled_wake` and `guest_sem_canceled_wake_coarse` pass 10 runs in a row. Linux: 6 run, 6 pass, 0 skip, 0 fail. Windows: 6 run, 6 pass, 0 skip, 0 fail. The libc and libkernel suites run 161 and 265 tests on Linux and 189 and 287 on Windows, with 0 failed. - **e6d4929f65, a merge of `main` 1db077e876** ([run](https://github.com/EagleBearerYS/AnyPS5/actions/runs/40859529209)). The one conflict was in `docs/dev/debt/libkernel.md`, where `main` adds kallmek3nny's `sem_setname` entry (#11267) where this PR adds its semaphore entry; the merge keeps both. This PR's own added and removed lines are the same. The same six tests pass 10 runs in a row. Linux: 6 run, 6 pass, 0 skip, 0 fail. Windows: 6 run, 6 pass, 0 skip, 0 fail. The libc and libkernel suites run 191 and 293 tests on Linux and 220 and 316 on Windows, with 0 failed. The suites ran with `APS5_SKIP_TIMING_TESTS=1`, which relaxes `precise_wait`'s median bounds on GitHub's hosted runners. - **7a6f4ee910, a merge of `main` 1b91748340**, which brings akkadbakkad89's #13600 ([run](https://github.com/EagleBearerYS/AnyPS5/actions/runs/41188451544)). - **The conflict:** `sceKernelCreateSema` in `Semaphore.cpp`. #13600 accepts attribute bit 0x100, in place of `main`'s `NotImplemented_nid_no_patch` for it, and reads the FIFO mode as `(attr & 3u) == 1u`. - **The resolution:** `main`'s lines are kept, and this PR's priority-queue flag reads the low two bits the same way, `(attr & 3u) == 2u` in place of `attr == 2`, so 0x102 still queues by priority. The merge commit's message names that line. This PR's other added and removed lines are the same. - **What the tests cover:** `guest_semaphore` (#13600's case) creates semaphores with 0x100, 0x101 and 0x102 and checks only that they're accepted. `guest_semaphore_handoff` checks the queue order with attributes 1 and 2 only. So no test here runs the masked read's priority order with bit 0x100 set. - **×10:** `guest_semaphore_handoff`, `guest_semaphore`, `guest_semaphore_trace`, `guest_pthread_sem`, `guest_sem_canceled_wake` and `guest_sem_canceled_wake_coarse`. - Linux: 6 run, 6 pass, 0 skip, 0 fail. - Windows: 6 run, 6 pass, 0 skip, 0 fail. - The libc and libkernel suites run 199 and 309 tests on Linux and 226 and 331 on Windows, with 0 failed. The suites ran with `APS5_SKIP_TIMING_TESTS=1`, which relaxes `precise_wait`'s median bounds on GitHub's hosted runners. ### 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: #10558 (merge order only: it adds a handle lookup at the top of `sceKernelSignalSema`, `sceKernelWaitSema` and `sceKernelCancelSema`, where this PR changes the bodies; this PR goes after it). No order with #9566, see Overlap; merge order with orezraey's #11195 (the semaphore code and its debt entry): whichever merges second keeps both - [x] Fixes: none - [x] Unblocks: none - [x] GPUs tested: n/a - [x] AI-assisted: yes
MarcZX10 commented 2026-10-21 23:24:11 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-21T23:24:11Z.

Review: CHANGES_REQUESTED


The queue handoff is a useful fix for the signal/poll race, and the reported Linux/Windows library suites pass. I cannot approve the current behavior for the supported priority attribute: KernelSemaPrivate::isFifo is set at creation (attr == 1), but the new queue is always traversed in arrival order. The PR’s debt entry confirms SCE_KERNEL_SEMA_ATTR_TH_PRIO is not modeled; the unknown ordering therefore silently gets FIFO behavior instead of throwing or implementing the priority contract. Please handle this attribute explicitly (implement its ordering, or fail the unsupported case and record the limitation) and add a focused test for it. Also, the regression currently has one waiter, so it does not exercise the new loop’s multi-waiter behavior, especially passing a waiter whose need exceeds the remaining signal count. Please add a two-waiter case to verify the ordering/skip rule. The current conventions, intake, eligibility, labels and report checks pass; hosted product build jobs are skipped.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5607445406 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/9491#pullrequestreview-5607445406). Originally posted by [MarcZX10](https://github.com/MarcZX10) on 2026-10-21T23:24:11Z. **Review:** CHANGES_REQUESTED --- The queue handoff is a useful fix for the signal/poll race, and the reported Linux/Windows library suites pass. I cannot approve the current behavior for the supported priority attribute: `KernelSemaPrivate::isFifo` is set at creation (`attr == 1`), but the new queue is always traversed in arrival order. The PR’s debt entry confirms `SCE_KERNEL_SEMA_ATTR_TH_PRIO` is not modeled; the unknown ordering therefore silently gets FIFO behavior instead of throwing or implementing the priority contract. Please handle this attribute explicitly (implement its ordering, or fail the unsupported case and record the limitation) and add a focused test for it. Also, the regression currently has one waiter, so it does not exercise the new loop’s multi-waiter behavior, especially passing a waiter whose need exceeds the remaining signal count. Please add a two-waiter case to verify the ordering/skip rule. The current conventions, intake, eligibility, labels and report checks pass; hosted product build jobs are skipped.
EagleBearerYS commented 2026-10-22 00:02:01 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by EagleBearerYS on 2026-10-22T00:02:01Z.


Thanks, @MarcZX10. 0a353f80ef handles SCE_KERNEL_SEMA_ATTR_TH_PRIO (0x02) explicitly: a waiter on such a semaphore is queued by the priority its thread has when it starts to wait, before the first queued waiter with a numerically higher (that is, lower) priority, so the signal serves higher-priority threads first and equal ones in arrival order. Attributes 1 and 0 keep arrival order. The debt entry now says so, and that a priority change while waiting doesn't move a waiter.

guest_semaphore_handoff gains the two-waiter cases: a waiter needing 2 queued before one needing 1, where a signal of 1 serves the second and leaves the first queued until 2 more arrive; and the order of two waiters for both attributes, with priorities 767 and 256 either way round and with equal priorities. On GitHub's runners (run) the test fails 3 of 3 with the previous head, and with the commit guest_semaphore_handoff, guest_semaphore and guest_semaphore_trace pass 10 of 10 on Linux and Windows, with the libc and libkernel suites. semaphore_trace_tests builds Semaphore.cpp on its own, so it gains stubs for the two pthread calls the lookup uses.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6329305550 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/9491#issuecomment-6329305550). Originally posted by [EagleBearerYS](https://github.com/EagleBearerYS) on 2026-10-22T00:02:01Z. --- Thanks, @MarcZX10. 0a353f80ef handles `SCE_KERNEL_SEMA_ATTR_TH_PRIO` (0x02) explicitly: a waiter on such a semaphore is queued by the priority its thread has when it starts to wait, before the first queued waiter with a numerically higher (that is, lower) priority, so the signal serves higher-priority threads first and equal ones in arrival order. Attributes 1 and 0 keep arrival order. The debt entry now says so, and that a priority change while waiting doesn't move a waiter. `guest_semaphore_handoff` gains the two-waiter cases: a waiter needing 2 queued before one needing 1, where a signal of 1 serves the second and leaves the first queued until 2 more arrive; and the order of two waiters for both attributes, with priorities 767 and 256 either way round and with equal priorities. On GitHub's runners ([run](https://github.com/EagleBearerYS/AnyPS5/actions/runs/39695243095)) the test fails 3 of 3 with the previous head, and with the commit `guest_semaphore_handoff`, `guest_semaphore` and `guest_semaphore_trace` pass 10 of 10 on Linux and Windows, with the libc and libkernel suites. `semaphore_trace_tests` builds `Semaphore.cpp` on its own, so it gains stubs for the two pthread calls the lookup uses.
MarcZX10 commented 2026-10-22 02:12:33 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-22T02:12:33Z.

Review: APPROVED


Re-reviewed the updated head. The FIFO path preserves arrival order, and attribute 0x02 inserts waiters by the priority captured when they enter the queue, with equal priorities retaining arrival order. The regression now covers two waiters, passing over a larger unmet need, both priority orderings and equal-priority FIFO order. The author reports the focused semaphore tests plus libc/libkernel suites passing on Linux and Windows after this update; conventions, intake, eligibility, labels and report checks pass, while hosted product and relinker jobs are skipped. I did not run tests locally. The debt entry clearly states that console ordering and priority changes while waiting remain unverified.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5608489380 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/9491#pullrequestreview-5608489380). Originally posted by [MarcZX10](https://github.com/MarcZX10) on 2026-10-22T02:12:33Z. **Review:** APPROVED --- Re-reviewed the updated head. The FIFO path preserves arrival order, and attribute 0x02 inserts waiters by the priority captured when they enter the queue, with equal priorities retaining arrival order. The regression now covers two waiters, passing over a larger unmet need, both priority orderings and equal-priority FIFO order. The author reports the focused semaphore tests plus libc/libkernel suites passing on Linux and Windows after this update; conventions, intake, eligibility, labels and report checks pass, while hosted product and relinker jobs are skipped. I did not run tests locally. The debt entry clearly states that console ordering and priority changes while waiting remain unverified.
MarcZX10 commented 2026-10-22 04:27:31 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-22T04:27:31Z.

Review: CHANGES_REQUESTED


The semaphore handoff now covers the original token-stealing race, multi-waiter skip behavior and priority ordering for SCE_KERNEL_SEMA_ATTR_TH_PRIO, with reported focused and full libc/libkernel runs on Linux and Windows. I independently checked the new #9566 shared debt warning: #9566 edits the terminal process-group clause around line 128, while this PR edits the semaphore clause later in the file; the changes are distinct but both debt edits must survive integration. However, the tested branch is based on a6dcea9279 and current upstream main is ab347d144d. Please rebase onto current main, rerun the focused semaphore/handoff/trace tests and conventions, and refresh the overlap scan, preserving the #9566 debt change. Hosted product jobs are skipped; I did not run local tests.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5609261817 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/9491#pullrequestreview-5609261817). Originally posted by [MarcZX10](https://github.com/MarcZX10) on 2026-10-22T04:27:31Z. **Review:** CHANGES_REQUESTED --- The semaphore handoff now covers the original token-stealing race, multi-waiter skip behavior and priority ordering for `SCE_KERNEL_SEMA_ATTR_TH_PRIO`, with reported focused and full libc/libkernel runs on Linux and Windows. I independently checked the new #9566 shared debt warning: #9566 edits the terminal process-group clause around line 128, while this PR edits the semaphore clause later in the file; the changes are distinct but both debt edits must survive integration. However, the tested branch is based on a6dcea9279 and current upstream main is ab347d144d88fae054d9770cf0db8d3565735c02. Please rebase onto current main, rerun the focused semaphore/handoff/trace tests and conventions, and refresh the overlap scan, preserving the #9566 debt change. Hosted product jobs are skipped; I did not run local tests.
EagleBearerYS commented 2026-10-22 06:07:34 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by EagleBearerYS on 2026-10-22T06:07:34Z.


Thanks, @MarcZX10. The branch now has main at da54c652e3 merged in (d1604d79c9), with the PR's own diff unchanged. On GitHub's runners (run) guest_semaphore_handoff passes 10 runs in a row on both, and conventions pass. Windows' libkernel suite timed guest_raise_in_system_call out once there; run alone it passes 20 times in a row, with that run's whole suite passing (run). #9566's debt entry is kept either way: the two conflict only in that file, and Overlap and Depends on now say neither needs to wait. Tested now says so.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6334016509 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/9491#issuecomment-6334016509). Originally posted by [EagleBearerYS](https://github.com/EagleBearerYS) on 2026-10-22T06:07:34Z. --- Thanks, @MarcZX10. The branch now has `main` at da54c652e3 merged in (d1604d79c9), with the PR's own diff unchanged. On GitHub's runners ([run](https://github.com/EagleBearerYS/AnyPS5/actions/runs/39728867414)) `guest_semaphore_handoff` passes 10 runs in a row on both, and conventions pass. Windows' libkernel suite timed `guest_raise_in_system_call` out once there; run alone it passes 20 times in a row, with that run's whole suite passing ([run](https://github.com/EagleBearerYS/AnyPS5/actions/runs/39730561441)). #9566's debt entry is kept either way: the two conflict only in that file, and Overlap and Depends on now say neither needs to wait. Tested now says so.
MarcZX10 commented 2026-10-29 08:31:20 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-29T08:31:20Z.

Review: CHANGES_REQUESTED


Re-reviewed exact head 2132238f4539d6a1b98bade9c0d7a7ddfbdb05ef against current upstream/main 4c7d275bef89d309313d16bc2bc05d37a1b50be2. Two review items remain in this head:

  1. Semaphore.cpp still declares scePthreadSelf and scePthreadGetprio locally. Please include prx/libkernel/include/Api.hpp and use its matching declarations, as required by the public-header rule. Keep the private semaphore header for fixture state.
  2. The first handoff loop waits only for waiter.started and then sleeps 50 ms. started is set before WaitSema queues, so a delayed worker can signal before it is queued and make the test nondeterministic. Use the existing WaitForQueue(sem, 1) before signaling.

The current-main merge preview is clean; git diff --check and conventions pass. I did not rerun the semaphore tests locally.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5694813940 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/9491#pullrequestreview-5694813940). Originally posted by [MarcZX10](https://github.com/MarcZX10) on 2026-10-29T08:31:20Z. **Review:** CHANGES_REQUESTED --- Re-reviewed exact head `2132238f4539d6a1b98bade9c0d7a7ddfbdb05ef` against current `upstream/main` `4c7d275bef89d309313d16bc2bc05d37a1b50be2`. Two review items remain in this head: 1. `Semaphore.cpp` still declares `scePthreadSelf` and `scePthreadGetprio` locally. Please include `prx/libkernel/include/Api.hpp` and use its matching declarations, as required by the public-header rule. Keep the private semaphore header for fixture state. 2. The first handoff loop waits only for `waiter.started` and then sleeps 50 ms. `started` is set before `WaitSema` queues, so a delayed worker can signal before it is queued and make the test nondeterministic. Use the existing `WaitForQueue(sem, 1)` before signaling. The current-main merge preview is clean; `git diff --check` and conventions pass. I did not rerun the semaphore tests locally.
MarcZX10 commented 2026-10-29 11:24:29 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-29T11:24:29Z.

Review: CHANGES_REQUESTED


Re-reviewed exact head e6d4929f658d8a81a3d823f6805128548980549e. The two findings from my last code review are addressed: the local pthread declarations are gone in favor of Api.hpp, and the first handoff waits for WaitForQueue rather than a pre-queue flag/sleep. The reported Linux/Windows focused semaphore, trace and libc/libkernel runs on this integrated head are useful.

One integration point remains before this can be approved: the checklist correctly says this PR follows #10558, but #10558 is still open and this head does not contain its handle changes. GuestSemaphoreHandoff.cpp still reaches into sem->mutex, sem->queue, sem->deleted and sem->tokenCount. Please apply the handoff after #10558 lands, adapt those fixture accesses to the resulting handle API, rebase onto current main, then rerun the focused semaphore tests and public-header/conventions checks on that final head. Current GitHub mergeability alone does not exercise the declared stacked integration.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5696890993 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/9491#pullrequestreview-5696890993). Originally posted by [MarcZX10](https://github.com/MarcZX10) on 2026-10-29T11:24:29Z. **Review:** CHANGES_REQUESTED --- Re-reviewed exact head `e6d4929f658d8a81a3d823f6805128548980549e`. The two findings from my last code review are addressed: the local pthread declarations are gone in favor of `Api.hpp`, and the first handoff waits for `WaitForQueue` rather than a pre-queue flag/sleep. The reported Linux/Windows focused semaphore, trace and libc/libkernel runs on this integrated head are useful. One integration point remains before this can be approved: the checklist correctly says this PR follows #10558, but #10558 is still open and this head does not contain its handle changes. `GuestSemaphoreHandoff.cpp` still reaches into `sem->mutex`, `sem->queue`, `sem->deleted` and `sem->tokenCount`. Please apply the handoff after #10558 lands, adapt those fixture accesses to the resulting handle API, rebase onto current `main`, then rerun the focused semaphore tests and public-header/conventions checks on that final head. Current GitHub mergeability alone does not exercise the declared stacked integration.
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!9491
No description provided.