feat(libc): add the std::__sce_v2 mutex, condition and thread calls #8885

Merged
hypermoist merged 6 commits from upstream-pr/8885 into main 2026-10-24 08:09:55 +00:00
hypermoist commented 2026-10-21 04:29:59 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by hypermoist on 2026-10-21T04:29:59Z.


What

Adds 16 std::__sce_v2 thread entry points from libSceLibcInternal that libc does not export yet. Titles import them:

  • Halloween (PPSA29340): its kitt-ps5-shipping.prx and kitt_support-ps5-shipping.prx modules import _Mtx_init, _Mtx_lock, _Mtx_unlock and _Mtx_destroy; kitt-ps5-shipping.prx also imports _Thrd_yield. Its kitt_webkit-ps5-shipping.prx module also imports the _Cnd_* calls.
  • PPSA03671 imports the same mutex set and _Cnd_* calls in its eboot, and _Thrd_id in two bundled middleware modules.

The upstream _Mtx_*/_Cnd_* work on this libc's own mutex type. These versioned forms are different: in the decrypted libSceLibcInternal.sprx (PS5 13.60) they take libkernel's mutex, condition and thread handles and call the scePthread* functions directly. So they forward the same way, and translate the results into Dinkumware's thrd_* codes as the console does:

Export (NID) Address Behaviour
_Mtx_init_with_name (b6I1MMimJXA) 0xe0500 Clears *mtx, creates an attribute, sets the recursive type when bit 0x100 is set, and creates the mutex with the given name (none if null or empty). It returns 1 for ENOMEM and 4 for any other failure; if destroying the attribute fails, the new mutex is destroyed and 4 returned.
_Mtx_init (TdAc0h4iomU) 0xe04d0 _Mtx_init_with_name with the name SceLibcI_Mtx
_Mtx_destroy (XP6c8M4G+fc) 0xe02b0 scePthreadMutexDestroy, result returned as is
_Mtx_lock (5qXct3c1skg) 0xe02c0 0, or 3 (busy) for EDEADLK, otherwise 4
_Mtx_trylock (S0FJ-HONta8) 0xe02e0 0, or 3 for EBUSY, otherwise 4
_Mtx_unlock (4bp9gcNLwMI) 0xe0340 always 0
_Cnd_init (bL2UrZ-G0Hk) / _Cnd_init_with_name (bXdNk0ZHk00) 0xe0630 / 0xe0670 scePthreadCondInit with no attribute and the name SceLibcI_Cnd or the given one; 0, 1 for ENOMEM, otherwise 4
_Cnd_destroy (KSs6Q1X5ABk) 0xdf3a0 result returned as is
_Cnd_wait / _Cnd_signal / _Cnd_broadcast (fUs4X3mpTi4, enG9-gUJp70, 8Y9ARRfgQc4) 0xdf3b0 / 0xdf410 / 0xdf420 always 0
_Thrd_detach (CfO+zWMbJJQ) 0xdb5b0 0 or 4
_Thrd_join (qnYxp6VUtbI) 0xdb550 4 on failure; otherwise stores the low 32 bits of the exit value when the pointer is not null, and returns 0
_Thrd_yield (919aYFGRv-k) / _Thrd_id (2ozFS9GCs+A) 0xdb620 / 0xdb630 scePthreadYield / scePthreadSelf

The bodies are written from this observed behaviour; nothing from the module is copied. libkernel registers a second table of scePthread* functions with libc at load, next to the table #8859 adds.

Not included:

  • _Throw_Cpp_error (ShHKOkWgZRM, 0x11b780): the console raises a std::__sce_v2::system_error, a type libc does not have yet. It follows in a separate PR together with the versioned exception types; an earlier revision of this PR wrongly forwarded it to the unversioned call.
  • _Mtx_timedlock, _Cnd_timedwait and _Thrd_sleep: they convert their timespec through an internal clock helper whose behaviour is not pinned down yet.
  • The _Thrd_start* family and the _Cnd_*_at_thread_exit calls: these have real bodies, not one-call forwards.
  • _Thrd_hardware_concurrency: it returns a value computed at module start.

Tested

Linux (CachyOS, GCC, Release): full build. All 539 guest_*, kernel and Ampr tests pass. guest_libc_threads gains VersionedSync, which runs against the real libkernel:

  • a recursive mutex locked three times and unlocked three times;
  • a relock of a default mutex returning 3;
  • _Mtx_trylock from a second thread returning 3 while the mutex is held;
  • a condition handshake between two threads;
  • _Thrd_join returning the exit code, and 4 on a second join;
  • _Thrd_detach, _Thrd_yield and _Thrd_id;
  • an unlock and a wait with a mutex the caller does not own: the test shows libkernel failing both, while the wrappers return 0 as the console's do.

It passed 10 runs in a row. Mapping EDEADLK to 4 instead of 3 makes the test fail.

tools/check_conventions.py is clean. None of these names or NIDs appear in any of the 816 open pull requests, and there is no line overlap with them.

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: #8859 (this branch is stacked on it and reuses its libkernel registration)
  • Fixes: none
  • Unblocks: none
  • GPUs tested: n/a (libc only)
  • AI-assisted: yes (Claude Code), reviewed by the contributor

🤖 Generated with Claude Code

https://claude.ai/code/session_01RbLgECLQ2bpDM7oe2urGZK

<!-- greyfinch-sync:boykopovar/AnyPS5:issue:4946412312 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/8885). Originally posted by [hypermoist](https://github.com/hypermoist) on 2026-10-21T04:29:59Z. --- ### What Adds 16 `std::__sce_v2` thread entry points from `libSceLibcInternal` that libc does not export yet. Titles import them: - Halloween (PPSA29340): its `kitt-ps5-shipping.prx` and `kitt_support-ps5-shipping.prx` modules import `_Mtx_init`, `_Mtx_lock`, `_Mtx_unlock` and `_Mtx_destroy`; `kitt-ps5-shipping.prx` also imports `_Thrd_yield`. Its `kitt_webkit-ps5-shipping.prx` module also imports the `_Cnd_*` calls. - PPSA03671 imports the same mutex set and `_Cnd_*` calls in its `eboot`, and `_Thrd_id` in two bundled middleware modules. The upstream `_Mtx_*`/`_Cnd_*` work on this libc's own mutex type. These versioned forms are different: in the decrypted `libSceLibcInternal.sprx` (PS5 13.60) they take libkernel's mutex, condition and thread handles and call the `scePthread*` functions directly. So they forward the same way, and translate the results into Dinkumware's `thrd_*` codes as the console does: | Export (NID) | Address | Behaviour | |---|---|---| | `_Mtx_init_with_name` (`b6I1MMimJXA`) | 0xe0500 | Clears `*mtx`, creates an attribute, sets the recursive type when bit `0x100` is set, and creates the mutex with the given name (none if null or empty). It returns 1 for `ENOMEM` and 4 for any other failure; if destroying the attribute fails, the new mutex is destroyed and 4 returned. | | `_Mtx_init` (`TdAc0h4iomU`) | 0xe04d0 | `_Mtx_init_with_name` with the name `SceLibcI_Mtx` | | `_Mtx_destroy` (`XP6c8M4G+fc`) | 0xe02b0 | `scePthreadMutexDestroy`, result returned as is | | `_Mtx_lock` (`5qXct3c1skg`) | 0xe02c0 | 0, or 3 (busy) for `EDEADLK`, otherwise 4 | | `_Mtx_trylock` (`S0FJ-HONta8`) | 0xe02e0 | 0, or 3 for `EBUSY`, otherwise 4 | | `_Mtx_unlock` (`4bp9gcNLwMI`) | 0xe0340 | always 0 | | `_Cnd_init` (`bL2UrZ-G0Hk`) / `_Cnd_init_with_name` (`bXdNk0ZHk00`) | 0xe0630 / 0xe0670 | `scePthreadCondInit` with no attribute and the name `SceLibcI_Cnd` or the given one; 0, 1 for `ENOMEM`, otherwise 4 | | `_Cnd_destroy` (`KSs6Q1X5ABk`) | 0xdf3a0 | result returned as is | | `_Cnd_wait` / `_Cnd_signal` / `_Cnd_broadcast` (`fUs4X3mpTi4`, `enG9-gUJp70`, `8Y9ARRfgQc4`) | 0xdf3b0 / 0xdf410 / 0xdf420 | always 0 | | `_Thrd_detach` (`CfO+zWMbJJQ`) | 0xdb5b0 | 0 or 4 | | `_Thrd_join` (`qnYxp6VUtbI`) | 0xdb550 | 4 on failure; otherwise stores the low 32 bits of the exit value when the pointer is not null, and returns 0 | | `_Thrd_yield` (`919aYFGRv-k`) / `_Thrd_id` (`2ozFS9GCs+A`) | 0xdb620 / 0xdb630 | `scePthreadYield` / `scePthreadSelf` | The bodies are written from this observed behaviour; nothing from the module is copied. libkernel registers a second table of `scePthread*` functions with libc at load, next to the table #8859 adds. Not included: - `_Throw_Cpp_error` (`ShHKOkWgZRM`, 0x11b780): the console raises a `std::__sce_v2::system_error`, a type libc does not have yet. It follows in a separate PR together with the versioned exception types; an earlier revision of this PR wrongly forwarded it to the unversioned call. - `_Mtx_timedlock`, `_Cnd_timedwait` and `_Thrd_sleep`: they convert their `timespec` through an internal clock helper whose behaviour is not pinned down yet. - The `_Thrd_start*` family and the `_Cnd_*_at_thread_exit` calls: these have real bodies, not one-call forwards. - `_Thrd_hardware_concurrency`: it returns a value computed at module start. ### Tested Linux (CachyOS, GCC, Release): full build. All 539 `guest_*`, kernel and Ampr tests pass. `guest_libc_threads` gains `VersionedSync`, which runs against the real libkernel: - a recursive mutex locked three times and unlocked three times; - a relock of a default mutex returning 3; - `_Mtx_trylock` from a second thread returning 3 while the mutex is held; - a condition handshake between two threads; - `_Thrd_join` returning the exit code, and 4 on a second join; - `_Thrd_detach`, `_Thrd_yield` and `_Thrd_id`; - an unlock and a wait with a mutex the caller does not own: the test shows libkernel failing both, while the wrappers return 0 as the console's do. It passed 10 runs in a row. Mapping `EDEADLK` to 4 instead of 3 makes the test fail. `tools/check_conventions.py` is clean. None of these names or NIDs appear in any of the 816 open pull requests, and there is no line overlap with them. ### 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: #8859 (this branch is stacked on it and reuses its libkernel registration) - [x] Fixes: none - [x] Unblocks: none - [x] GPUs tested: n/a (libc only) - [x] AI-assisted: yes (Claude Code), reviewed by the contributor 🤖 Generated with [Claude Code](https://claude.com/claude-code) https://claude.ai/code/session_01RbLgECLQ2bpDM7oe2urGZK
MarcZX10 commented 2026-10-21 04:37:45 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-21T04:37:45Z.


The new _Mtx_unlock, _Cnd_wait, _Cnd_signal, and _Cnd_broadcast wrappers currently discard the libkernel return code and unconditionally report success. The underlying scePthreadMutexUnlock can return SCE_KERNEL_ERROR_EPERM for a non-owner, and condition wait can return EINVAL/EPERM for destroyed or unowned objects (Pthread/src/Mutex.cpp, Cond.cpp). Could these wrappers translate nonzero kernel results to the CRT error/throw convention, as the existing libc _Cnd_wait and mutex wrappers do, and add fake-forward tests for each failure path? As written, callers can believe a failed unlock/wait/signal completed normally.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6308168440 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/8885#issuecomment-6308168440). Originally posted by [MarcZX10](https://github.com/MarcZX10) on 2026-10-21T04:37:45Z. --- The new `_Mtx_unlock`, `_Cnd_wait`, `_Cnd_signal`, and `_Cnd_broadcast` wrappers currently discard the libkernel return code and unconditionally report success. The underlying `scePthreadMutexUnlock` can return `SCE_KERNEL_ERROR_EPERM` for a non-owner, and condition wait can return `EINVAL`/`EPERM` for destroyed or unowned objects (`Pthread/src/Mutex.cpp`, `Cond.cpp`). Could these wrappers translate nonzero kernel results to the CRT error/throw convention, as the existing libc `_Cnd_wait` and mutex wrappers do, and add fake-forward tests for each failure path? As written, callers can believe a failed unlock/wait/signal completed normally.
hypermoist commented 2026-10-21 04:47:46 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by hypermoist on 2026-10-21T04:47:46Z.


Thanks. I checked this against the decrypted libSceLibcInternal.sprx (12.00) before changing anything. All four versioned calls drop the kernel result on the console: _Mtx_unlock (0xe0340), _Cnd_wait (0xdf3b0), _Cnd_signal (0xdf410) and _Cnd_broadcast (0xdf420) each make the scePthread* call and then return 0. The unversioned _Mtx_unlock, _Cnd_wait and _Cnd_signal in the same module do the same. Upstream's own unversioned _Mtx_unlock and _Cnd_wait also return success for a non-owner (Threads.cpp, the IsOwnedByCaller early returns). Translating the errors would make these differ from the console and from the existing wrappers, so I've kept them returning 0.

What I did add (ae4d607a54): VersionedSync now unlocks and waits with a mutex the caller does not own. It shows libkernel itself failing both calls (EPERM), and both wrappers returning 0. So the behaviour is pinned by a test, not left implicit. Replacing the wait assertion with != success makes the test abort.

That commit also removes the versioned _Throw_Cpp_error: the console raises a std::__sce_v2::system_error there, not the unversioned type this forwarded to. It will come back with the versioned exception types in a separate PR.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6308290754 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/8885#issuecomment-6308290754). Originally posted by [hypermoist](https://github.com/hypermoist) on 2026-10-21T04:47:46Z. --- Thanks. I checked this against the decrypted `libSceLibcInternal.sprx` (12.00) before changing anything. All four versioned calls drop the kernel result on the console: `_Mtx_unlock` (0xe0340), `_Cnd_wait` (0xdf3b0), `_Cnd_signal` (0xdf410) and `_Cnd_broadcast` (0xdf420) each make the `scePthread*` call and then return 0. The unversioned `_Mtx_unlock`, `_Cnd_wait` and `_Cnd_signal` in the same module do the same. Upstream's own unversioned `_Mtx_unlock` and `_Cnd_wait` also return success for a non-owner (`Threads.cpp`, the `IsOwnedByCaller` early returns). Translating the errors would make these differ from the console and from the existing wrappers, so I've kept them returning 0. What I did add (ae4d607a54): `VersionedSync` now unlocks and waits with a mutex the caller does not own. It shows libkernel itself failing both calls (EPERM), and both wrappers returning 0. So the behaviour is pinned by a test, not left implicit. Replacing the wait assertion with `!= success` makes the test abort. That commit also removes the versioned `_Throw_Cpp_error`: the console raises a `std::__sce_v2::system_error` there, not the unversioned type this forwarded to. It will come back with the versioned exception types in a separate PR.
tkcm01 commented 2026-10-21 18:12:37 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by tkcm01 on 2026-10-21T18:12:37Z.


Windows data point for 8e08acba70, merged onto main 99844c7ee (clean merge). GitHub Actions windows-latest running this repo's Build workflow (full Windows build, WinLibs GCC, Release, BUILD_TESTING=ON) on my fork.

  • With the PR: 1814 tests, all passed, guest_libc_threads included (run).
  • Negative control, main plus only this PR's test changes: guest_libc_threads_tests does not link (the std::__sce_v2 _Mtx_*, _Cnd_* and _Thrd_* exports are undefined), as expected for new exports (run).

Not tested: Linux, macOS, a title.
AI-assisted: yes

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6323165570 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/8885#issuecomment-6323165570). Originally posted by [tkcm01](https://github.com/tkcm01) on 2026-10-21T18:12:37Z. --- Windows data point for `8e08acba70`, merged onto `main` `99844c7ee` (clean merge). GitHub Actions `windows-latest` running this repo's Build workflow (full Windows build, WinLibs GCC, Release, `BUILD_TESTING=ON`) on my fork. - With the PR: 1814 tests, all passed, `guest_libc_threads` included ([run](https://github.com/tkcm01/AnyPS5/actions/runs/39644114658)). - Negative control, `main` plus only this PR's test changes: `guest_libc_threads_tests` does not link (the `std::__sce_v2` `_Mtx_*`, `_Cnd_*` and `_Thrd_*` exports are undefined), as expected for new exports ([run](https://github.com/tkcm01/AnyPS5/actions/runs/39644125259)). Not tested: Linux, macOS, a title. AI-assisted: yes
harisibrar1 commented 2026-10-22 05:47:37 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by harisibrar1 on 2026-10-22T05:47:37Z.

Review: APPROVED


Checked against main 4a17c7175 and ps5-payload-dev/sdk sce_stubs/libSceLibcInternal.c:

  • The SDK stubs (and the 3.20 inventory) have no std::__sce_v2 symbols, so whether these exports exist depends on the 12.00 module and title imports you cite. I computed the NIDs from the 16 mangled names and all of them match your table (for example, _ZNSt8__sce_v29_Mtx_initEPP13pthread_mutexi gives TdAc0h4iomU and _ZNSt8__sce_v28_Thrd_idEv gives 2ozFS9GCs+A).
  • The mangled parameter types (PP13pthread_mutex, PP12pthread_cond, P7pthread) agree with the pointer-to-handle and by-value pthread_t forwards. The kernel error constants (0x8002000b/0c/10) and recursive type 2 agree with libkernel's Mutex.cpp:141 and Pthread.hpp:28.
  • _Thrd_join/_Thrd_detach match main's unversioned implementations. The always-0 unlock/wait/signal/broadcast matches the existing debt entry for the unversioned calls, and the unowned-mutex test now pins it.
  • Nit: Thread.cpp:43 adds a second scePthreadYield declaration; the one at line 50 already exists.
  • Follow-up, not for this PR: debt/libc.md:136 still says the unversioned _Thrd_id throws because the return type is unknown. The 0xdb630 scePthreadSelf observation could settle that for the unversioned export too.

Reviewed with AI assistance (Claude Code); the checks above were run locally on my machine.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5609774266 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/8885#pullrequestreview-5609774266). Originally posted by [harisibrar1](https://github.com/harisibrar1) on 2026-10-22T05:47:37Z. **Review:** APPROVED --- Checked against main 4a17c7175 and ps5-payload-dev/sdk `sce_stubs/libSceLibcInternal.c`: - The SDK stubs (and the 3.20 inventory) have no `std::__sce_v2` symbols, so whether these exports exist depends on the 12.00 module and title imports you cite. I computed the NIDs from the 16 mangled names and all of them match your table (for example, `_ZNSt8__sce_v29_Mtx_initEPP13pthread_mutexi` gives TdAc0h4iomU and `_ZNSt8__sce_v28_Thrd_idEv` gives 2ozFS9GCs+A). - The mangled parameter types (`PP13pthread_mutex`, `PP12pthread_cond`, `P7pthread`) agree with the pointer-to-handle and by-value pthread_t forwards. The kernel error constants (0x8002000b/0c/10) and recursive type 2 agree with libkernel's Mutex.cpp:141 and Pthread.hpp:28. - `_Thrd_join`/`_Thrd_detach` match main's unversioned implementations. The always-0 unlock/wait/signal/broadcast matches the existing debt entry for the unversioned calls, and the unowned-mutex test now pins it. - Nit: Thread.cpp:43 adds a second `scePthreadYield` declaration; the one at line 50 already exists. - Follow-up, not for this PR: debt/libc.md:136 still says the unversioned `_Thrd_id` throws because the return type is unknown. The 0xdb630 `scePthreadSelf` observation could settle that for the unversioned export too. Reviewed with AI assistance (Claude Code); the checks above were run locally on my machine.
hypermoist commented 2026-10-22 09:08:01 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by hypermoist on 2026-10-22T09:08:01Z.


@harisibrar1 thanks for checking the NIDs and the mangled types. Fixed the nit in f465759bcf: the second scePthreadYield declaration is gone (I also merged current main); guest_libc_threads and the guest_pthread* tests still pass on Linux.

Agreed on the follow-up. The unversioned _Thrd_id debt line can be settled the same way, and I'll do it in a separate PR so this one stays on the __sce_v2 exports.

@tkcm01 thanks for the Windows run and the negative control.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6337042765 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/8885#issuecomment-6337042765). Originally posted by [hypermoist](https://github.com/hypermoist) on 2026-10-22T09:08:01Z. --- @harisibrar1 thanks for checking the NIDs and the mangled types. Fixed the nit in f465759bcf: the second `scePthreadYield` declaration is gone (I also merged current `main`); `guest_libc_threads` and the `guest_pthread*` tests still pass on Linux. Agreed on the follow-up. The unversioned `_Thrd_id` debt line can be settled the same way, and I'll do it in a separate PR so this one stays on the `__sce_v2` exports. @tkcm01 thanks for the Windows run and the negative control.
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!8885
No description provided.