feat(libc): add the std::__sce_v2 thread start, timed wait, clock and processor count calls #9447
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/9447"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
Imported from boykopovar/AnyPS5. Originally posted by AlxcBTW on 2026-10-21T22:23:56Z.
What
#8885 adds the versioned (
std::__sce_v2)_Mtx,_Cndand_Thrdcalls on the libkernelscePthread*functions and lists the calls it leaves out. This adds three of them and one clock, inThreads.cppandTime.cppnext to #8885's, each written from the console'slibc.prxdisassembly:std::__sce_v2::_Thrd_start(pthread**, void*(*)(void*), void*)(2cXiHqvKFUM)scePthreadCreatewith default attributes and the nameThr; returns 0, or 4 for any failure (the console returns nothing else)std::__sce_v2::_Thrd_hardware_concurrency()(dt3C+gjXHKk)scePthreadGetaffinityof the first calling thread), computed oncestd::__sce_v2::_Cnd_timedwait(pthread_cond**, pthread_mutex**, const timespec*)(230scaAkbIU)scePthreadCondTimedwait; returns 0 when signalled, 2 onETIMEDOUT, 4 for other errors and 5 forEPERM, as the console's result mapping doesstd::__sce_v2::chrono::steady_clock::now_v1_compat()(EMwrSFcyowE)gettimeofdayasseconds * 1000000 + microseconds, the unversioned_Xtime_get_ticksvalueWhat the disassembly says that the topic's first description did not:
now_v1_compatdoes not readCLOCK_MONOTONICand does not count nanoseconds; the console callsgettimeofdayand 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_concurrencyis not the host's processor count: the module caches the population count of the main thread's affinity mask (13 with the libkernel default mask0x1FFF). The libkernel forwards forscePthreadCreate,scePthreadCondTimedwaitandscePthreadGetaffinityare three more members of #8885'sPthreadSyncForwards_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.pylists 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::_Launchentry 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
2dc7b701aplus the CI scaffolding commit; run 39679999433, the test filterguest_libc|guest_freebsd|libc_|libkernel|pthread): 33 of 33 passed, among themguest_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'sVersionedSyncis._Thrd_startruns 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_startreturns 0._Cnd_timedwaitwith 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_compatis 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_concurrencyis between 1 and 64.python3 tools/check_conventions.py --base origin/mainpasses.tools/pr_overlap.py --branchlists #8901 (_Xtime_get_ticks, left out of this PR above) and #9416 (_Thrd_detach,_Cnd_broadcast, which are #8885's), and textual overlaps indocs/dev/debt/libc.mdline 136 (#8872, #8880, #9001, #9133, #9170) andTime.cppnear line 570 (#9388); none implements the same function.Checklist
main; no other open PR implements the same functionsNotImplemented_nid_no_patch); silent stubs are listed under Silent stubs in their library's technical debt file.mdfiles or images added to the repository (attach them to this PR)8e08acba7; rebase ontomainafter it merges)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
_ZNSt8__sce_v210_Thrd_joinEP7pthreadPi_nid_postfix_ZNSt8__sce_v211_Cnd_signalEPP12pthread_cond_nid_postfix_ZNSt8__sce_v211_Mtx_unlockEPP13pthread_mutex_nid_postfix_ZNSt8__sce_v211_Thrd_startEPP7pthreadPFPvS3_ES3__nid_postfix_ZNSt8__sce_v211_Thrd_yieldEv_nid_postfix_ZNSt8__sce_v212_Cnd_destroyEPP12pthread_cond_nid_postfix_ZNSt8__sce_v212_Mtx_destroyEPP13pthread_mutex_nid_postfix_ZNSt8__sce_v212_Mtx_trylockEPP13pthread_mutex_nid_postfix_ZNSt8__sce_v212_Thrd_detachEP7pthread_nid_postfix_ZNSt8__sce_v214_Cnd_broadcastEPP12pthread_cond_nid_postfix_ZNSt8__sce_v214_Cnd_timedwaitEPP12pthread_condPP13pthread_mutexPK8timespec_nid_postfix_ZNSt8__sce_v219_Cnd_init_with_nameEPP12pthread_condPKc_nid_postfix_ZNSt8__sce_v219_Mtx_init_with_nameEPP13pthread_mutexiPKc_nid_postfix_ZNSt8__sce_v226_Thrd_hardware_concurrencyEv_nid_postfix_ZNSt8__sce_v26chrono12steady_clock13now_v1_compatEv_nid_postfix_ZNSt8__sce_v28_Thrd_idEv_nid_postfix_ZNSt8__sce_v29_Cnd_initEPP12pthread_cond_nid_postfix_ZNSt8__sce_v29_Cnd_waitEPP12pthread_condPP13pthread_mutex_nid_postfix_ZNSt8__sce_v29_Mtx_initEPP13pthread_mutexi_nid_postfix_ZNSt8__sce_v29_Mtx_lockEPP13pthread_mutex_nid_postfixImported 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 exceedsUINT_MAX, but the guard only clamps whenseconds > 4294; casting the larger total tounsigned intwraps it to a short timeout. Please clamp/check the total microseconds (including the fractional part) before conversion and add a boundary test aroundUINT_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