feat(libc): add the std::__sce_v2 thread start, timed wait, clock and processor count calls #9447

Closed
AlxcBTW wants to merge 5 commits from upstream-pr/9447 into main
AlxcBTW commented 2026-10-21 22:23:56 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by AlxcBTW on 2026-10-21T22:23:56Z.


What

#8885 adds the versioned (std::__sce_v2) _Mtx, _Cnd and _Thrd calls on the libkernel scePthread* functions and lists the calls it leaves out. This adds three of them and one clock, in Threads.cpp and Time.cpp next to #8885's, each written from the console's libc.prx disassembly:

Export (NID) Behaviour
std::__sce_v2::_Thrd_start(pthread**, void*(*)(void*), void*) (2cXiHqvKFUM) scePthreadCreate with default attributes and the name Thr; returns 0, or 4 for any failure (the console returns nothing else)
std::__sce_v2::_Thrd_hardware_concurrency() (dt3C+gjXHKk) the population count of a libkernel affinity mask (scePthreadGetaffinity of the first calling thread), computed once
std::__sce_v2::_Cnd_timedwait(pthread_cond**, pthread_mutex**, const timespec*) (230scaAkbIU) the absolute realtime deadline is turned into the remaining microseconds (0 once passed, rounded up to the next microsecond) and passed to scePthreadCondTimedwait; returns 0 when signalled, 2 on ETIMEDOUT, 4 for other errors and 5 for EPERM, as the console's result mapping does
std::__sce_v2::chrono::steady_clock::now_v1_compat() (EMwrSFcyowE) gettimeofday as seconds * 1000000 + microseconds, the unversioned _Xtime_get_ticks value

What the disassembly says that the topic's first description did not: now_v1_compat does not read CLOCK_MONOTONIC and does not count nanoseconds; the console calls gettimeofday and returns microseconds of the realtime clock, so that is what it does here (and it is not monotonic if the system clock steps). _Thrd_hardware_concurrency is not the host's processor count: the module caches the population count of the main thread's affinity mask (13 with the libkernel default mask 0x1FFF). The libkernel forwards for scePthreadCreate, scePthreadCondTimedwait and scePthreadGetaffinity are three more members of #8885's PthreadSyncForwards_nid_no_patch, registered where #8885 registers the others (libkernel/Pthread/Posix/Thread.cpp, libc/include/General.hpp).

Not in this PR:

  • std::__sce_v2::_Xtime_get_ticks (XXOOIxrIKDM) is in #8901 with the same body (the unversioned call, microseconds); tools/pr_overlap.py lists it, so it is left to that PR.
  • std::__sce_v2::_Cnd_do_broadcast_at_thread_exit (2s6aLdPIA4I) is left out. The disassembled body does not register a broadcast; it walks the per-process table of registrations for the calling thread, signals and broadcasts each and clears it. That table is filled by _Cnd_register_at_thread_exit, which is not implemented in libc (the _Pad::_Launch entry in the debt file says the same), so an implementation now would be a function that never finds anything. The debt entry says so.

Known differences, in docs/dev/debt/libc.md: the remaining time saturates at 4294967295 microseconds where the console wraps its 32-bit result; the processor count is read from the first caller's mask, not the main thread's at module start.

Titles that import these: Minecraft Dungeons II (PPSA16064) imports the four calls above and the two left out below.

Tested

  • Linux (Ubuntu 24.04, Mesa lavapipe), fork CI at 74719013c (the PR head 2dc7b701a plus the CI scaffolding commit; run 39679999433, the test filter guest_libc|guest_freebsd|libc_|libkernel|pthread): 33 of 33 passed, among them guest_libc_threads (0.10 s). Not run on Windows, and not run on a console or with a title.
  • guest_libc_threads (core/libs/tests/GuestLibcThreads.cpp) has three new cases, written the way #8885's VersionedSync is. _Thrd_start runs a function with a distinct argument and the test joins it (_Thrd_join): the argument arrives, the return value 7 comes back as the exit code, and _Thrd_start returns 0. _Cnd_timedwait with the mutex held: a deadline 50 ms ahead and nobody signalling returns 2 after at least 45 ms and under 2 s; a deadline 1 s in the past returns 2 in under 500 ms; a second thread that locks the mutex and signals while the first waits on a deadline 10 s ahead makes the wait return 0 in under 5 s. now_v1_compat is non-decreasing over 1000 samples, advances by 9000 to 500000 across a 10 ms sleep (microsecond unit) and lies within one second of the realtime clock. _Thrd_hardware_concurrency is between 1 and 64.
  • python3 tools/check_conventions.py --base origin/main passes.
  • tools/pr_overlap.py --branch lists #8901 (_Xtime_get_ticks, left out of this PR above) and #9416 (_Thrd_detach, _Cnd_broadcast, which are #8885's), and textual overlaps in docs/dev/debt/libc.md line 136 (#8872, #8880, #9001, #9133, #9170) and Time.cpp near line 570 (#9388); none implements the same function.

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: #8885 (this branch is based on its head 8e08acba7; rebase onto main after it merges)
  • Fixes: none
  • Unblocks: none (PPSA16064 imports these six; no Game bring-up discussion yet)
  • GPUs tested: n/a
  • AI-assisted: yes
<!-- greyfinch-sync:boykopovar/AnyPS5:issue:4959170901 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/9447). Originally posted by [AlxcBTW](https://github.com/AlxcBTW) on 2026-10-21T22:23:56Z. --- ### What #8885 adds the versioned (`std::__sce_v2`) `_Mtx`, `_Cnd` and `_Thrd` calls on the libkernel `scePthread*` functions and lists the calls it leaves out. This adds three of them and one clock, in `Threads.cpp` and `Time.cpp` next to #8885's, each written from the console's `libc.prx` disassembly: | Export (NID) | Behaviour | |---|---| | `std::__sce_v2::_Thrd_start(pthread**, void*(*)(void*), void*)` (`2cXiHqvKFUM`) | `scePthreadCreate` with default attributes and the name `Thr`; returns 0, or 4 for any failure (the console returns nothing else) | | `std::__sce_v2::_Thrd_hardware_concurrency()` (`dt3C+gjXHKk`) | the population count of a libkernel affinity mask (`scePthreadGetaffinity` of the first calling thread), computed once | | `std::__sce_v2::_Cnd_timedwait(pthread_cond**, pthread_mutex**, const timespec*)` (`230scaAkbIU`) | the absolute realtime deadline is turned into the remaining microseconds (0 once passed, rounded up to the next microsecond) and passed to `scePthreadCondTimedwait`; returns 0 when signalled, 2 on `ETIMEDOUT`, 4 for other errors and 5 for `EPERM`, as the console's result mapping does | | `std::__sce_v2::chrono::steady_clock::now_v1_compat()` (`EMwrSFcyowE`) | `gettimeofday` as `seconds * 1000000 + microseconds`, the unversioned `_Xtime_get_ticks` value | What the disassembly says that the topic's first description did not: `now_v1_compat` does not read `CLOCK_MONOTONIC` and does not count nanoseconds; the console calls `gettimeofday` and returns microseconds of the realtime clock, so that is what it does here (and it is not monotonic if the system clock steps). `_Thrd_hardware_concurrency` is not the host's processor count: the module caches the population count of the main thread's affinity mask (13 with the libkernel default mask `0x1FFF`). The libkernel forwards for `scePthreadCreate`, `scePthreadCondTimedwait` and `scePthreadGetaffinity` are three more members of #8885's `PthreadSyncForwards_nid_no_patch`, registered where #8885 registers the others (`libkernel/Pthread/Posix/Thread.cpp`, `libc/include/General.hpp`). Not in this PR: - `std::__sce_v2::_Xtime_get_ticks` (`XXOOIxrIKDM`) is in #8901 with the same body (the unversioned call, microseconds); `tools/pr_overlap.py` lists it, so it is left to that PR. - `std::__sce_v2::_Cnd_do_broadcast_at_thread_exit` (`2s6aLdPIA4I`) is left out. The disassembled body does not register a broadcast; it walks the per-process table of registrations for the calling thread, signals and broadcasts each and clears it. That table is filled by `_Cnd_register_at_thread_exit`, which is not implemented in libc (the `_Pad::_Launch` entry in the debt file says the same), so an implementation now would be a function that never finds anything. The debt entry says so. Known differences, in `docs/dev/debt/libc.md`: the remaining time saturates at 4294967295 microseconds where the console wraps its 32-bit result; the processor count is read from the first caller's mask, not the main thread's at module start. Titles that import these: Minecraft Dungeons II (PPSA16064) imports the four calls above and the two left out below. ### Tested - Linux (Ubuntu 24.04, Mesa lavapipe), fork CI at 74719013c (the PR head 2dc7b701a plus the CI scaffolding commit; run 39679999433, the test filter `guest_libc|guest_freebsd|libc_|libkernel|pthread`): 33 of 33 passed, among them `guest_libc_threads` (0.10 s). Not run on Windows, and not run on a console or with a title. - `guest_libc_threads` (`core/libs/tests/GuestLibcThreads.cpp`) has three new cases, written the way #8885's `VersionedSync` is. `_Thrd_start` runs a function with a distinct argument and the test joins it (`_Thrd_join`): the argument arrives, the return value 7 comes back as the exit code, and `_Thrd_start` returns 0. `_Cnd_timedwait` with the mutex held: a deadline 50 ms ahead and nobody signalling returns 2 after at least 45 ms and under 2 s; a deadline 1 s in the past returns 2 in under 500 ms; a second thread that locks the mutex and signals while the first waits on a deadline 10 s ahead makes the wait return 0 in under 5 s. `now_v1_compat` is non-decreasing over 1000 samples, advances by 9000 to 500000 across a 10 ms sleep (microsecond unit) and lies within one second of the realtime clock. `_Thrd_hardware_concurrency` is between 1 and 64. - `python3 tools/check_conventions.py --base origin/main` passes. - `tools/pr_overlap.py --branch` lists #8901 (`_Xtime_get_ticks`, left out of this PR above) and #9416 (`_Thrd_detach`, `_Cnd_broadcast`, which are #8885's), and textual overlaps in `docs/dev/debt/libc.md` line 136 (#8872, #8880, #9001, #9133, #9170) and `Time.cpp` near line 570 (#9388); none implements the same function. ### 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: #8885 (this branch is based on its head 8e08acba7; rebase onto `main` after it merges) - [x] Fixes: none - [x] Unblocks: none (PPSA16064 imports these six; no Game bring-up discussion yet) - [x] GPUs tested: n/a - [x] AI-assisted: yes
github-actions[bot] commented 2026-10-21 22:24:47 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by github-actions[bot] on 2026-10-21T22:24:47Z.


Progress report (a6dcea9 → 2dc7b70)

📈 System libraries: 42.44% (+0.11%, +20 implemented functions)

✅ 20 implemented
Library Name
libc _ZNSt8__sce_v210_Thrd_joinEP7pthreadPi_nid_postfix
libc _ZNSt8__sce_v211_Cnd_signalEPP12pthread_cond_nid_postfix
libc _ZNSt8__sce_v211_Mtx_unlockEPP13pthread_mutex_nid_postfix
libc _ZNSt8__sce_v211_Thrd_startEPP7pthreadPFPvS3_ES3__nid_postfix
libc _ZNSt8__sce_v211_Thrd_yieldEv_nid_postfix
libc _ZNSt8__sce_v212_Cnd_destroyEPP12pthread_cond_nid_postfix
libc _ZNSt8__sce_v212_Mtx_destroyEPP13pthread_mutex_nid_postfix
libc _ZNSt8__sce_v212_Mtx_trylockEPP13pthread_mutex_nid_postfix
libc _ZNSt8__sce_v212_Thrd_detachEP7pthread_nid_postfix
libc _ZNSt8__sce_v214_Cnd_broadcastEPP12pthread_cond_nid_postfix
libc _ZNSt8__sce_v214_Cnd_timedwaitEPP12pthread_condPP13pthread_mutexPK8timespec_nid_postfix
libc _ZNSt8__sce_v219_Cnd_init_with_nameEPP12pthread_condPKc_nid_postfix
libc _ZNSt8__sce_v219_Mtx_init_with_nameEPP13pthread_mutexiPKc_nid_postfix
libc _ZNSt8__sce_v226_Thrd_hardware_concurrencyEv_nid_postfix
libc _ZNSt8__sce_v26chrono12steady_clock13now_v1_compatEv_nid_postfix
libc _ZNSt8__sce_v28_Thrd_idEv_nid_postfix
libc _ZNSt8__sce_v29_Cnd_initEPP12pthread_cond_nid_postfix
libc _ZNSt8__sce_v29_Cnd_waitEPP12pthread_condPP13pthread_mutex_nid_postfix
libc _ZNSt8__sce_v29_Mtx_initEPP13pthread_mutexi_nid_postfix
libc _ZNSt8__sce_v29_Mtx_lockEPP13pthread_mutex_nid_postfix
<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6327890464 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/9447#issuecomment-6327890464). Originally posted by [github-actions[bot]](https://github.com/apps/github-actions) on 2026-10-21T22:24:47Z. --- <!-- progress-report --> ### Progress report (a6dcea9 → 2dc7b70) 📈 **System libraries**: 42.44% (+0.11%, +20 implemented functions) <details> <summary>✅ 20 implemented</summary> | Library | Name | | - | - | | libc | `_ZNSt8__sce_v210_Thrd_joinEP7pthreadPi_nid_postfix` | | libc | `_ZNSt8__sce_v211_Cnd_signalEPP12pthread_cond_nid_postfix` | | libc | `_ZNSt8__sce_v211_Mtx_unlockEPP13pthread_mutex_nid_postfix` | | libc | `_ZNSt8__sce_v211_Thrd_startEPP7pthreadPFPvS3_ES3__nid_postfix` | | libc | `_ZNSt8__sce_v211_Thrd_yieldEv_nid_postfix` | | libc | `_ZNSt8__sce_v212_Cnd_destroyEPP12pthread_cond_nid_postfix` | | libc | `_ZNSt8__sce_v212_Mtx_destroyEPP13pthread_mutex_nid_postfix` | | libc | `_ZNSt8__sce_v212_Mtx_trylockEPP13pthread_mutex_nid_postfix` | | libc | `_ZNSt8__sce_v212_Thrd_detachEP7pthread_nid_postfix` | | libc | `_ZNSt8__sce_v214_Cnd_broadcastEPP12pthread_cond_nid_postfix` | | libc | `_ZNSt8__sce_v214_Cnd_timedwaitEPP12pthread_condPP13pthread_mutexPK8timespec_nid_postfix` | | libc | `_ZNSt8__sce_v219_Cnd_init_with_nameEPP12pthread_condPKc_nid_postfix` | | libc | `_ZNSt8__sce_v219_Mtx_init_with_nameEPP13pthread_mutexiPKc_nid_postfix` | | libc | `_ZNSt8__sce_v226_Thrd_hardware_concurrencyEv_nid_postfix` | | libc | `_ZNSt8__sce_v26chrono12steady_clock13now_v1_compatEv_nid_postfix` | | libc | `_ZNSt8__sce_v28_Thrd_idEv_nid_postfix` | | libc | `_ZNSt8__sce_v29_Cnd_initEPP12pthread_cond_nid_postfix` | | libc | `_ZNSt8__sce_v29_Cnd_waitEPP12pthread_condPP13pthread_mutex_nid_postfix` | | libc | `_ZNSt8__sce_v29_Mtx_initEPP13pthread_mutexi_nid_postfix` | | libc | `_ZNSt8__sce_v29_Mtx_lockEPP13pthread_mutex_nid_postfix` | </details>
MarcZX10 commented 2026-10-22 04:37:17 +00:00 (Migrated from github.com)

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

Review: CHANGES_REQUESTED


The focused condition-variable cases cover signalled, expired and future deadlines, and the thread-start/current-affinity paths are backed by guest tests. I found a conversion boundary defect in MicrosecondsUntil: with a normalized deadline exactly 4,294 seconds ahead and a fractional component above 967,295 microseconds, the computed total exceeds UINT_MAX, but the guard only clamps when seconds > 4294; casting the larger total to unsigned int wraps it to a short timeout. Please clamp/check the total microseconds (including the fractional part) before conversion and add a boundary test around UINT_MAX.

The PR correctly declares its dependency on #8885 and says to rebase after that PR merges. Keep that ordering; once #8885 lands, rebase this branch onto then-current main and rerun the focused thread tests, conventions and overlap checks. The current head is tied to the older #8885 branch and its Linux tests were run on fork CI; Windows/title/console runs are explicitly not claimed. I did not run local tests.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5609319740 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/9447#pullrequestreview-5609319740). Originally posted by [MarcZX10](https://github.com/MarcZX10) on 2026-10-22T04:37:17Z. **Review:** CHANGES_REQUESTED --- The focused condition-variable cases cover signalled, expired and future deadlines, and the thread-start/current-affinity paths are backed by guest tests. I found a conversion boundary defect in `MicrosecondsUntil`: with a normalized deadline exactly 4,294 seconds ahead and a fractional component above 967,295 microseconds, the computed total exceeds `UINT_MAX`, but the guard only clamps when `seconds > 4294`; casting the larger total to `unsigned int` wraps it to a short timeout. Please clamp/check the total microseconds (including the fractional part) before conversion and add a boundary test around `UINT_MAX`. The PR correctly declares its dependency on #8885 and says to rebase after that PR merges. Keep that ordering; once #8885 lands, rebase this branch onto then-current main and rerun the focused thread tests, conventions and overlap checks. The current head is tied to the older #8885 branch and its Linux tests were run on fork CI; Windows/title/console runs are explicitly not claimed. I did not run local tests.

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!9447
No description provided.