Skip to content

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

Closed
AlxcBTW wants to merge 8 commits into
boykopovar:mainfrom
AlxcBTW:pr/libc-sce-v2-thread-time-remainder
Closed

AlxcBTW wants to merge 8 commits into
boykopovar:mainfrom
AlxcBTW:pr/libc-sce-v2-thread-time-remainder

Conversation

@AlxcBTW

@AlxcBTW AlxcBTW commented Oct 21, 2026

Copy link
Copy Markdown
Contributor

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 feat(libc): add the std::__sce_v2 exception types and throwers #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

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: feat(libc): add the std::__sce_v2 mutex, condition and thread calls #8885 (this branch is based on its head 8e08acb; 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

hypermoist and others added 8 commits October 21, 2026 17:25
The versioned Dinkumware entries operate on libkernel's own mutex,
condition and thread handles: _Mtx_init(_with_name), _Mtx_destroy,
_Mtx_lock, _Mtx_trylock, _Mtx_unlock, _Cnd_init(_with_name),
_Cnd_destroy, _Cnd_wait, _Cnd_signal, _Cnd_broadcast, _Thrd_detach,
_Thrd_join, _Thrd_yield, _Thrd_id and _Throw_Cpp_error. libkernel
registers the pthread calls with libc next to the C11 forwards.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01RbLgECLQ2bpDM7oe2urGZK
The console's std::__sce_v2::_Throw_Cpp_error raises a
std::__sce_v2::system_error, not the unversioned std::system_error this
forwarded to. It moves to the versioned exception change, which adds that
type.

Review on #8885: the versioned unlock, wait, signal and broadcast calls
return 0 whatever the kernel returns, as the console's do (each calls the
kernel and clears eax). The test now shows the kernel failing an unowned
unlock and wait while the wrappers report success.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01RbLgECLQ2bpDM7oe2urGZK
…read-sync

# Conflicts:
#	core/libs/tests/GuestLibcThreads.cpp
@github-actions

Copy link
Copy Markdown

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

@github-actions

github-actions Bot commented Oct 21, 2026 •

Copy link
Copy Markdown

Some comparisons could not be completed because a pull request conflicts with the base branch. Review these shared files manually before concluding that there is no overlap.

Pull request Shared files not compared
PR 1835 core/libs/prx/libc/src/Threads.cpp
PR 7447 docs/dev/debt/libc.md
PR 7544 docs/dev/debt/libc.md
PR 7715 docs/dev/debt/libc.md
PR 8150 docs/dev/debt/libc.md
PR 8965 docs/dev/debt/libc.md
PR 9087 docs/dev/debt/libc.md
PR 9340 docs/dev/debt/libc.md
PR 9371 docs/dev/debt/libc.md
PR 10007 docs/dev/debt/libc.md
PR 10409 core/libs/prx/libc/src/Time.cpp
docs/dev/debt/libc.md
PR 11109 docs/dev/debt/libc.md
PR 11172 docs/dev/debt/libc.md
PR 11332 docs/dev/debt/libc.md
PR 11661 docs/dev/debt/libc.md
PR 11689 docs/dev/debt/libc.md
PR 11698 docs/dev/debt/libc.md
PR 12184 docs/dev/debt/libc.md
PR 12849 docs/dev/debt/libc.md
PR 13449 docs/dev/debt/libc.md
PR 13794 docs/dev/debt/libc.md
PR 13804 docs/dev/debt/libc.md
PR 15973 docs/dev/debt/libc.md
PR 16214 docs/dev/debt/libc.md
PR 16516 docs/dev/debt/libc.md
PR 16560 docs/dev/debt/libc.md
PR 16791 docs/dev/debt/libc.md
PR 16919 docs/dev/debt/libc.md
PR 16924 docs/dev/debt/libc.md
PR 16944 docs/dev/debt/libc.md
PR 16949 docs/dev/debt/libc.md
PR 16975 docs/dev/debt/libc.md
PR 17259 docs/dev/debt/libc.md
PR 17261 docs/dev/debt/libc.md
PR 17277 docs/dev/debt/libc.md
PR 17296 docs/dev/debt/libc.md
PR 17331 docs/dev/debt/libc.md
PR 17539 docs/dev/debt/libc.md
PR 17555 docs/dev/debt/libc.md
PR 17556 docs/dev/debt/libc.md
PR 17557 docs/dev/debt/libc.md
PR 17558 docs/dev/debt/libc.md
PR 17568 docs/dev/debt/libc.md
PR 17578 docs/dev/debt/libc.md
PR 17607 docs/dev/debt/libc.md
PR 17610 docs/dev/debt/libc.md
PR 18190 core/libs/prx/libkernel/Pthread/Posix/Thread.cpp
PR 18347 core/libs/prx/libc/src/Threads.cpp
core/libs/tests/GuestLibcThreads.cpp
docs/dev/debt/libc.md
PR 18915 docs/dev/debt/libc.md
PR 20315 core/libs/prx/libc/include/General.hpp
PR 21571 core/libs/prx/libkernel/Pthread/Posix/Thread.cpp
PR 21742 core/libs/prx/libkernel/Pthread/Posix/Thread.cpp
PR 22380 core/libs/prx/libc/src/Threads.cpp
core/libs/tests/GuestLibcThreads.cpp
PR 22515 core/libs/prx/libc/src/Threads.cpp
PR 22929 core/libs/prx/libc/src/Time.cpp
PR 23680 core/libs/prx/libc/include/General.hpp
PR 23849 core/libs/prx/libc/include/General.hpp
PR 23881 core/libs/prx/libc/include/General.hpp
PR 23897 core/libs/prx/libc/include/General.hpp
PR 23953 core/libs/prx/libkernel/Pthread/Posix/Thread.cpp
PR 24262 core/libs/prx/libc/src/Threads.cpp
PR 24377 docs/dev/debt/libc.md
PR 24466 core/libs/prx/libc/src/Threads.cpp
core/libs/tests/GuestLibcThreads.cpp
PR 25749 core/libs/prx/libc/src/Time.cpp
PR 26004 core/libs/prx/libkernel/Pthread/Posix/Thread.cpp

@MarcZX10 MarcZX10 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

@ycbuzi

ycbuzi commented Oct 22, 2026

Copy link
Copy Markdown
Contributor

Windows run of 2dc7b70 merged onto main a6646f8 (b60ba2cef1), the platform the description lists as not run. Windows 11 Home 10.0.26200, MinGW-w64 GCC 15.2.0 (ucrt-posix-seh r7), Release, BUILD_TESTING=ON.

The merge conflicts only in docs/dev/debt/libc.md, as GitHub shows; for this run I took this PR's side of that file. Built libc, libkernel and guest_libc_threads_tests with no errors or warnings. ctest --repeat until-fail:2 on guest_libc_threads: passes on both repeats. No negative control: the test calls functions this PR adds, so with main's sources it does not link. Not run: a title.

@orezraey

Copy link
Copy Markdown
Contributor

@AlxcBTW A data point for _Thrd_hardware_concurrency: on a retail PS5 (firmware 13.60) the system libSceLibcInternal's plain std::thread::hardware_concurrency returns 5 in a payload process whose threads all have scePthreadGetaffinity mask 0xea00 (5 bits), while hw.ncpu reads 16 and sceKernelGetAvailableCpumask 0x1fff. So the popcount-of-affinity rule you read from libc.prx holds for the system library too. Probe and log: https://gist.github.com/orezraey/d67df3ee47b5ebab2d6b159a9df3b67f (recorded in #11164).

@adriadam10 adriadam10 removed their assignment Nov 17, 2026
@AlxcBTW AlxcBTW closed this by deleting the head repository Nov 23, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

docs libc libkernel needs rebase Conflicts with main, rebase needed

Projects

None yet

Development

Successfully merging this pull request may close these issues.

8 participants