fix(libkernel): release event flag waiters when the pattern is set #4899

Closed
EagleBearerYS wants to merge 1 commit from upstream-pr/4899 into main
EagleBearerYS commented 2026-10-13 02:54:11 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by EagleBearerYS on 2026-10-13T02:54:11Z.


What

sceKernelSetEventFlag (libkernel/EventFlag/src/EventFlag.cpp) only ORs the bits in and wakes the waiters. Each waiter checks the pattern again only once it runs, so anything that takes the bits first leaves it waiting:

  • a sceKernelClearEventFlag right after the set;
  • another waiter's clear mode;
  • a poll.

The waiter then fails with ETIMEDOUT or, without a timeout, never returns. A set followed by a clear (a pulse) therefore misses its waiter.

Measured on main with the new guest_event_flag_release test:

  • A thread waits for bit 0 (OR mode, 1 s timeout) on a single-waiter flag.
  • Once it is queued, the main thread sets bit 0 and clears the flag right away.
  • The waiter misses the set and its 1 s wait runs out. The test aborts after 1.4 s on Windows and 1.1 s on Linux.

Now the set releases every queued waiter whose condition the new pattern meets, before it returns:

  • in the order the waiters started waiting, applying each one's clear mode in turn;
  • a released waiter returns SCE_OK and the pattern it was released with, even if the flag is cleared, cancelled or deleted before it runs again;
  • sceKernelCancelEventFlag's waiter count and the single-waiter EPERM check count only threads that are still waiting.

shadPS4 and fpPS4 both release waiters this way:

No AnyPS5 title is claimed. The new entry in docs/dev/debt/libkernel.md says that the order and the result pattern are unverified on a console. It also says that the thread-priority attribute (0x02) is not modelled: waiters queue in arrival order, as in fpPS4.

Tested

Windows 11 (10.0.26300), WinLibs MinGW-w64 GCC 15.2.0 r7 (UCRT), Release, -DBUILD_TESTING=ON, on main at 495bf91a0:

  • New guest_event_flag_release: 100 rounds of the pulse above. Each round requires the waiter to return SCE_OK with the pattern 1.
  • With main's EventFlag.cpp the test aborts after 1.4 s, when a waiter's 1 s wait runs out. With the change it passes 20 runs in a row.
  • 129 tests pass: the tests registered in core/libs/prx/libc/CMakeLists.txt and core/libs/prx/libkernel/CMakeLists.txt that build on Windows, the new test among them.
  • tools/check_conventions.py --base origin/main passes.
  • Linux: WSL2 (Ubuntu 24.04, GCC 13.3.0, Release with CI's configure flags), same commit. With main's EventFlag.cpp the test aborts after 1.1 s. With the change it passes 20 runs in a row.
  • 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:4837676364 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/4899). Originally posted by [EagleBearerYS](https://github.com/EagleBearerYS) on 2026-10-13T02:54:11Z. --- ### What `sceKernelSetEventFlag` (`libkernel/EventFlag/src/EventFlag.cpp`) only ORs the bits in and wakes the waiters. Each waiter checks the pattern again only once it runs, so anything that takes the bits first leaves it waiting: - a `sceKernelClearEventFlag` right after the set; - another waiter's clear mode; - a poll. The waiter then fails with `ETIMEDOUT` or, without a timeout, never returns. A set followed by a clear (a pulse) therefore misses its waiter. Measured on `main` with the new `guest_event_flag_release` test: - A thread waits for bit 0 (OR mode, 1 s timeout) on a single-waiter flag. - Once it is queued, the main thread sets bit 0 and clears the flag right away. - The waiter misses the set and its 1 s wait runs out. The test aborts after 1.4 s on Windows and 1.1 s on Linux. Now the set releases every queued waiter whose condition the new pattern meets, before it returns: - in the order the waiters started waiting, applying each one's clear mode in turn; - a released waiter returns `SCE_OK` and the pattern it was released with, even if the flag is cleared, cancelled or deleted before it runs again; - `sceKernelCancelEventFlag`'s waiter count and the single-waiter `EPERM` check count only threads that are still waiting. shadPS4 and fpPS4 both release waiters this way: - shadPS4's [`Set`](https://github.com/shadps4-emu/shadPS4/blob/bf4809fed2865da53ede58723965635e514e2ef5/src/core/libraries/kernel/threads/event_flag.cpp#L187-L205), adopted in [shadPS4#4772](https://github.com/shadps4-emu/shadPS4/pull/4772), which "fixes random hangs in SotC" (Shadow of the Colossus); - fpPS4's [`_sceKernelSetEventFlag`](https://github.com/red-prig/fpPS4/blob/3e8cc63d3a65b5a1e647ee459766f681646d8adf/kernel/ps4_event_flag.pas#L533-L572). No AnyPS5 title is claimed. The new entry in `docs/dev/debt/libkernel.md` says that the order and the result pattern are unverified on a console. It also says that the thread-priority attribute (0x02) is not modelled: waiters queue in arrival order, as in fpPS4. ### Tested Windows 11 (10.0.26300), WinLibs MinGW-w64 GCC 15.2.0 r7 (UCRT), Release, `-DBUILD_TESTING=ON`, on `main` at 495bf91a0: - New `guest_event_flag_release`: 100 rounds of the pulse above. Each round requires the waiter to return `SCE_OK` with the pattern 1. - With `main`'s `EventFlag.cpp` the test aborts after 1.4 s, when a waiter's 1 s wait runs out. With the change it passes 20 runs in a row. - 129 tests pass: the tests registered in `core/libs/prx/libc/CMakeLists.txt` and `core/libs/prx/libkernel/CMakeLists.txt` that build on Windows, the new test among them. - `tools/check_conventions.py --base origin/main` passes. - Linux: WSL2 (Ubuntu 24.04, GCC 13.3.0, Release with CI's configure flags), same commit. With `main`'s `EventFlag.cpp` the test aborts after 1.1 s. With the change it passes 20 runs in a row. - 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
xXPBSXx commented 2026-10-13 06:42:58 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by xXPBSXx on 2026-10-13T06:42:58Z.


Windows test: head a71d2da79 merged onto current main ea2bbefb2 (clean merge, no conflicts). Windows 10, MinGW GCC (WinLibs), Ninja Release, -j2: full build 0 errors; ctest with the filter guest_event_flag_release: guest_event_flag_release passed (1 test(s), 0 failed); python tools/check_conventions.py --base upstream/main passed. I did not run the new tests against main without the change.

AI-assisted (Claude Code).

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6139897698 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/4899#issuecomment-6139897698). Originally posted by [xXPBSXx](https://github.com/xXPBSXx) on 2026-10-13T06:42:58Z. --- Windows test: head `a71d2da79` merged onto current `main` `ea2bbefb2` (clean merge, no conflicts). Windows 10, MinGW GCC (WinLibs), Ninja Release, `-j2`: full build 0 errors; ctest with the filter `guest_event_flag_release`: guest_event_flag_release passed (1 test(s), 0 failed); `python tools/check_conventions.py --base upstream/main` passed. I did not run the new tests against `main` without the change. AI-assisted (Claude Code).
oneandonlydean commented 2026-10-14 02:36:32 +00:00 (Migrated from github.com)

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


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:6159629648 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/4899#issuecomment-6159629648). Originally posted by [oneandonlydean](https://github.com/oneandonlydean) on 2026-10-14T02:36:32Z. --- 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!4899
No description provided.