feat(libc): implement _Cnd_timedwait #5037

Merged
Feuris merged 1 commit from upstream-pr/5037 into main 2026-10-13 10:26:10 +00:00
Feuris commented 2026-10-13 09:07:50 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by Feuris on 2026-10-13T09:07:50Z.


What

Implement _Cnd_timedwait in libc for Dinkumware condition variables, waiting until the specified xtime deadline and returning _Thrd_timedout (2) on expiration or _Thrd_success (0) on notification.

Super Meat Boy 3D (PPSA31162) imports McaImWKXong from libc.prx. The NID matches aerolib symbol _Cnd_timedwait.

Tested

  • Windows 11, MinGW-w64 GCC 15.2.0: Unit test guest_libc_cnd_timed_wait (ctest -R guest_libc_cnd_timed_wait) verifies immediate timeout on past deadline, successful wakeup by signaling thread before deadline, and null deadline argument validation (std::invalid_argument).
  • python tools/check_conventions.py --base origin/main passed.

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 in TechnicalDebt
  • 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: PPSA31162
  • AI-assisted: yes
<!-- greyfinch-sync:boykopovar/AnyPS5:issue:4840948652 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/5037). Originally posted by [Feuris](https://github.com/Feuris) on 2026-10-13T09:07:50Z. --- ### What Implement `_Cnd_timedwait` in libc for Dinkumware condition variables, waiting until the specified xtime deadline and returning `_Thrd_timedout` (2) on expiration or `_Thrd_success` (0) on notification. Super Meat Boy 3D (`PPSA31162`) imports `McaImWKXong` from `libc.prx`. The NID matches aerolib symbol `_Cnd_timedwait`. ### Tested - Windows 11, MinGW-w64 GCC 15.2.0: Unit test `guest_libc_cnd_timed_wait` (`ctest -R guest_libc_cnd_timed_wait`) verifies immediate timeout on past deadline, successful wakeup by signaling thread before deadline, and null deadline argument validation (`std::invalid_argument`). - `python tools/check_conventions.py --base origin/main` passed. ### Checklist - [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 in [TechnicalDebt](https://github.com/boykopovar/AnyPS5/blob/main/docs/dev/TechnicalDebt.md#silent-stubs) - [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: PPSA31162 - [x] AI-assisted: yes
github-actions[bot] commented 2026-10-13 09:09:04 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by github-actions[bot] on 2026-10-13T09:09:04Z.


Progress report (bb9a4d8 → 572d330)

➖ System libraries: 88.41% (+0.0%, +1 implemented functions)

✅ 1 implemented
Library Name
libc _Cnd_timedwait_nid_postfix
<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6142198348 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/5037#issuecomment-6142198348). Originally posted by [github-actions[bot]](https://github.com/apps/github-actions) on 2026-10-13T09:09:04Z. --- <!-- progress-report --> ### Progress report (bb9a4d8 → 572d330) ➖ **System libraries**: 88.41% (+0.0%, +1 implemented functions) <details> <summary>✅ 1 implemented</summary> | Library | Name | | - | - | | libc | `_Cnd_timedwait_nid_postfix` | </details>
adriadam10 commented 2026-10-13 09:15:40 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by adriadam10 on 2026-10-13T09:15:40Z.

Review: CHANGES_REQUESTED


Thanks. Built and ran guest_libc_cnd_timed_wait locally, and compared with the console's _Cnd_timedwait (disassembled from a title's libc.prx): the xtime is an absolute deadline converted against the current time, ETIMEDOUT maps to 2 and success to 0, so the two return values match. A few things to fix:

  • Threads.cpp:239-240: std::chrono::seconds(target->sec) + std::chrono::nanoseconds(target->nsec) overflows (signed UB) for a large guest sec. XtimeDeadline in the same file already normalizes the nanoseconds and checks overflow; reuse it (or an equivalent check) and throw on overflow like _Thrd_sleep does.
  • GuestLibcCndTimedWait.cpp: only an already-past deadline and a signalled wake-up are covered. Add a case with a short future deadline and no signal that returns 2 only after the deadline has passed (loop on system_clock as in other timer tests, since Windows can wake early), and an nsec outside [0, 1e9).
  • docs/dev/debt/libc.md:9: say where the behaviour comes from (disassembly of the console's libc.prx) and that the console returns 4 for other pthread errors while this throws.

The linux/windows jobs are skipped on this PR, so the full Build will need to run before merge.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5501336786 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/5037#pullrequestreview-5501336786). Originally posted by [adriadam10](https://github.com/adriadam10) on 2026-10-13T09:15:40Z. **Review:** CHANGES_REQUESTED --- Thanks. Built and ran `guest_libc_cnd_timed_wait` locally, and compared with the console's `_Cnd_timedwait` (disassembled from a title's `libc.prx`): the xtime is an absolute deadline converted against the current time, `ETIMEDOUT` maps to 2 and success to 0, so the two return values match. A few things to fix: - `Threads.cpp:239-240`: `std::chrono::seconds(target->sec) + std::chrono::nanoseconds(target->nsec)` overflows (signed UB) for a large guest `sec`. `XtimeDeadline` in the same file already normalizes the nanoseconds and checks overflow; reuse it (or an equivalent check) and throw on overflow like `_Thrd_sleep` does. - `GuestLibcCndTimedWait.cpp`: only an already-past deadline and a signalled wake-up are covered. Add a case with a short future deadline and no signal that returns 2 only after the deadline has passed (loop on `system_clock` as in other timer tests, since Windows can wake early), and an `nsec` outside `[0, 1e9)`. - `docs/dev/debt/libc.md:9`: say where the behaviour comes from (disassembly of the console's `libc.prx`) and that the console returns 4 for other pthread errors while this throws. The `linux`/`windows` jobs are skipped on this PR, so the full Build will need to run before merge.
Feuris commented 2026-10-13 09:39:46 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by Feuris on 2026-10-13T09:39:46Z.


Updated to address review feedback:

  • Reused XtimeDeadline in _Cnd_timedwait to normalize nanoseconds and added bounds checking against system_clock::duration overflow, throwing std::overflow_error on overflow.
  • Expanded GuestLibcCndTimedWait.cpp to cover short future timeouts without a signal (looping on system_clock), unnormalized
    sec values (both negative and > 1s), and std::overflow_error on extreme sec.
  • Updated docs/dev/debt/libc.md documenting the console libc.prx disassembly origin and noting that the console returns 4 for other internal pthread errors while this implementation throws.
<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6142719885 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/5037#issuecomment-6142719885). Originally posted by [Feuris](https://github.com/Feuris) on 2026-10-13T09:39:46Z. --- Updated to address review feedback: - Reused XtimeDeadline in _Cnd_timedwait to normalize nanoseconds and added bounds checking against system_clock::duration overflow, throwing std::overflow_error on overflow. - Expanded GuestLibcCndTimedWait.cpp to cover short future timeouts without a signal (looping on system_clock), unnormalized sec values (both negative and > 1s), and std::overflow_error on extreme sec. - Updated docs/dev/debt/libc.md documenting the console libc.prx disassembly origin and noting that the console returns 4 for other internal pthread errors while this implementation throws.
adriadam10 commented 2026-10-13 09:54:33 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by adriadam10 on 2026-10-13T09:54:33Z.

Review: APPROVED


Rechecked the update: XtimeDeadline is reused with overflow checks, the new tests cover a short future deadline (looping on system_clock), unnormalized nsec and overflow, and the debt line names the source and the console's return 4. guest_libc_cnd_timed_wait passes 5/5 here. Merging once the full Build is green.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5501781435 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/5037#pullrequestreview-5501781435). Originally posted by [adriadam10](https://github.com/adriadam10) on 2026-10-13T09:54:33Z. **Review:** APPROVED --- Rechecked the update: `XtimeDeadline` is reused with overflow checks, the new tests cover a short future deadline (looping on `system_clock`), unnormalized `nsec` and overflow, and the debt line names the source and the console's return 4. `guest_libc_cnd_timed_wait` passes 5/5 here. Merging once the full Build is green.
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!5037
No description provided.