feat(libc): implement _Mtx_timedlock #10688

Merged
edubox merged 2 commits from upstream-pr/10688 into main 2026-10-25 20:21:47 +00:00
edubox commented 2026-10-24 17:51:56 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by edubox on 2026-10-24T17:51:56Z.


What

Export _Mtx_timedlock (hPzYSd5Nasc), the timed counterpart of _Mtx_lock, which Minecraft Legends (PPSA05510) imports from libc.prx.

It takes the same absolute xtime as _Cnd_timedwait, converts it once through the shared XtimePoint helper, and keeps the mutex's recursive bookkeeping: a non-recursive mutex already held by the caller returns _Thrd_busy (3), a recursive one raises the count, and a free mutex acquires immediately. Expiry returns _Thrd_timedout (2); other host failures return 5, matching the console's libc.prx at 0x5d50 (PPSA02530), where 0x8002003c becomes 2 and every other nonzero result becomes 5.

On non-Windows hosts the native mutex and condition are std::timed_mutex and std::condition_variable_any. Windows keeps the previous CRITICAL_SECTION/CONDITION_VARIABLE pair and its WaitUntil loop because the standard condition_variable_any path failed guest_libc_cnd_timed_wait on the full Windows runner; _Mtx_timedlock polls TryEnterCriticalSection with Sleep(1) until the deadline. docs/dev/debt/libc.md records that the Windows timing of both the standard primitives and this polling path is not measured on the console.

pr_overlap finds no shared export. It shares docs/dev/debt/libc.md:38 with #8903, and the PRs that conflict with the base also touch Threads.cpp/GuestLibcThreads.cpp (#1835, #5021, #7201, #9447, #10615, #10648); their diffs do not implement _Mtx_timedlock or change the native mutex type.

Tested

CachyOS Linux 7.3.0-3, Ryzen 9 9900X, GCC 16.2.1, main 137a8fab89 plus this PR.

  • guest_libc_threads passes with TimedMutex: acquire with a future deadline, same-thread non-recursive busy, another thread times out (2) while the mutex is held, release, null handle and null deadline throws, and a recursive mutex takes the timed lock twice.
  • guest_libc_cnd_timed_wait passes with the restored Windows path (Linux host).
  • The patched libc.prx exports hPzYSd5Nasc (nm -D).
  • Not tested with a title. This removes one of the libc imports PPSA05510 needs; the title still stops on the next one.
  • The previous head failed guest_libc_cnd_timed_wait on the Windows full CI; this head restores the path that passed there. A Windows rerun is pending (no MinGW locally).

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 (removes one of the libc imports PPSA05510 needs; it still stops on the next one)
  • GPUs tested: n/a
  • AI-assisted: yes
<!-- greyfinch-sync:boykopovar/AnyPS5:issue:4997727287 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/10688). Originally posted by [edubox](https://github.com/edubox) on 2026-10-24T17:51:56Z. --- ### What Export `_Mtx_timedlock` (`hPzYSd5Nasc`), the timed counterpart of `_Mtx_lock`, which Minecraft Legends (PPSA05510) imports from `libc.prx`. It takes the same absolute `xtime` as `_Cnd_timedwait`, converts it once through the shared `XtimePoint` helper, and keeps the mutex's recursive bookkeeping: a non-recursive mutex already held by the caller returns `_Thrd_busy` (3), a recursive one raises the count, and a free mutex acquires immediately. Expiry returns `_Thrd_timedout` (2); other host failures return 5, matching the console's `libc.prx` at `0x5d50` (PPSA02530), where `0x8002003c` becomes 2 and every other nonzero result becomes 5. On non-Windows hosts the native mutex and condition are `std::timed_mutex` and `std::condition_variable_any`. Windows keeps the previous `CRITICAL_SECTION`/`CONDITION_VARIABLE` pair and its `WaitUntil` loop because the standard `condition_variable_any` path failed `guest_libc_cnd_timed_wait` on the full Windows runner; `_Mtx_timedlock` polls `TryEnterCriticalSection` with `Sleep(1)` until the deadline. `docs/dev/debt/libc.md` records that the Windows timing of both the standard primitives and this polling path is not measured on the console. `pr_overlap` finds no shared export. It shares `docs/dev/debt/libc.md:38` with #8903, and the PRs that conflict with the base also touch `Threads.cpp`/`GuestLibcThreads.cpp` (#1835, #5021, #7201, #9447, #10615, #10648); their diffs do not implement `_Mtx_timedlock` or change the native mutex type. ### Tested CachyOS Linux 7.3.0-3, Ryzen 9 9900X, GCC 16.2.1, `main` `137a8fab89` plus this PR. - `guest_libc_threads` passes with `TimedMutex`: acquire with a future deadline, same-thread non-recursive busy, another thread times out (2) while the mutex is held, release, null handle and null deadline throws, and a recursive mutex takes the timed lock twice. - `guest_libc_cnd_timed_wait` passes with the restored Windows path (Linux host). - The patched `libc.prx` exports `hPzYSd5Nasc` (`nm -D`). - Not tested with a title. This removes one of the libc imports PPSA05510 needs; the title still stops on the next one. - The previous head failed `guest_libc_cnd_timed_wait` on the Windows full CI; this head restores the path that passed there. A Windows rerun is pending (no MinGW locally). ### 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 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 (removes one of the libc imports PPSA05510 needs; it still stops on the next one) - [x] GPUs tested: n/a - [x] AI-assisted: yes
MarcZX10 commented 2026-10-24 19:17:54 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-24T19:17:54Z.

Review: APPROVED


Reviewed exact head e46fd5038350e4a0e4d5e7112af00e14f480736a. The shared XtimePoint conversion keeps the existing overflow/normalization checks while giving both timed condition waits and timed mutex acquisition the same absolute deadline. The timed path checks ownership before locking, preserves recursive-count handling, and maps timeout and system errors to the stated thread results. The Linux guest_libc_threads and guest_libc_cnd_timed_wait runs are reported passing by the author; locally, both changed translation units passed MinGW syntax checks (with a define for a high-resolution timer constant missing from this container's older MinGW headers). git diff --check and merge-tree checks pass. Hosted product tests are skipped, and no Windows runtime test is reported, so Windows behavior remains unverified here; I found no blocking source issue in the reviewed code.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5639579805 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/10688#pullrequestreview-5639579805). Originally posted by [MarcZX10](https://github.com/MarcZX10) on 2026-10-24T19:17:54Z. **Review:** APPROVED --- Reviewed exact head `e46fd5038350e4a0e4d5e7112af00e14f480736a`. The shared `XtimePoint` conversion keeps the existing overflow/normalization checks while giving both timed condition waits and timed mutex acquisition the same absolute deadline. The timed path checks ownership before locking, preserves recursive-count handling, and maps timeout and system errors to the stated thread results. The Linux `guest_libc_threads` and `guest_libc_cnd_timed_wait` runs are reported passing by the author; locally, both changed translation units passed MinGW syntax checks (with a define for a high-resolution timer constant missing from this container's older MinGW headers). `git diff --check` and merge-tree checks pass. Hosted product tests are skipped, and no Windows runtime test is reported, so Windows behavior remains unverified here; I found no blocking source issue in the reviewed code.
MarcZX10 commented 2026-10-24 20:23:35 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-24T20:23:35Z.

Review: APPROVED


Reviewed the full diff at e46fd5038350e4a0e4d5e7112af00e14f480736a. The new export uses the shared absolute-xtime conversion, preserves recursive ownership bookkeeping, returns busy for a same-thread plain mutex and timedout on expiry; the Windows native mutex/condition pair is unified with the timed-capable standard primitives so _Cnd_wait and _Cnd_timedwait share the same mutex. The focused test covers future acquisition, same-thread busy, cross-thread timeout, invalid arguments and recursive acquisition; the author reports the libc thread tests pass on Linux. Hosted Linux/Windows/macOS relinker and ASAN jobs, register-context on Ubuntu/macOS and write-watch on macOS pass; product Linux/Windows jobs were skipped. No title execution is claimed.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5639812014 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/10688#pullrequestreview-5639812014). Originally posted by [MarcZX10](https://github.com/MarcZX10) on 2026-10-24T20:23:35Z. **Review:** APPROVED --- Reviewed the full diff at e46fd5038350e4a0e4d5e7112af00e14f480736a. The new export uses the shared absolute-xtime conversion, preserves recursive ownership bookkeeping, returns busy for a same-thread plain mutex and timedout on expiry; the Windows native mutex/condition pair is unified with the timed-capable standard primitives so `_Cnd_wait` and `_Cnd_timedwait` share the same mutex. The focused test covers future acquisition, same-thread busy, cross-thread timeout, invalid arguments and recursive acquisition; the author reports the libc thread tests pass on Linux. Hosted Linux/Windows/macOS relinker and ASAN jobs, register-context on Ubuntu/macOS and write-watch on macOS pass; product Linux/Windows jobs were skipped. No title execution is claimed.
MarcZX10 commented 2026-10-25 00:15:50 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-25T00:15:50Z.

Review: APPROVED


The implementation adds the missing timed-lock export, shares the absolute XTIME conversion with timed condition waits, and retains the existing recursive/non-recursive ownership bookkeeping. The Linux tests cover future acquisition, same-thread busy, another-thread timeout, recursive reacquisition, invalid handles/deadline, and the existing timed condition wait; the PR also reports inspection of the console error mapping and export NID. I verified the diff against current main, git diff --check, and conventions. Hosted relinker/ASan, register-context and write-watch matrices pass across the available platforms; product build jobs are path-skipped.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5640730600 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/10688#pullrequestreview-5640730600). Originally posted by [MarcZX10](https://github.com/MarcZX10) on 2026-10-25T00:15:50Z. **Review:** APPROVED --- The implementation adds the missing timed-lock export, shares the absolute XTIME conversion with timed condition waits, and retains the existing recursive/non-recursive ownership bookkeeping. The Linux tests cover future acquisition, same-thread busy, another-thread timeout, recursive reacquisition, invalid handles/deadline, and the existing timed condition wait; the PR also reports inspection of the console error mapping and export NID. I verified the diff against current main, `git diff --check`, and conventions. Hosted relinker/ASan, register-context and write-watch matrices pass across the available platforms; product build jobs are path-skipped.
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!10688
No description provided.