feat(libc): export the std::__sce_v2 at-thread-exit registry calls #15879

Merged
Danigamer05 merged 1 commit from upstream-pr/15879 into main 2026-11-12 21:52:01 +00:00
Danigamer05 commented 2026-11-04 21:24:02 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by Danigamer05 on 2026-11-04T21:24:02Z.


What

Export the versioned std::__sce_v2 at-thread-exit calls from libc.

Export NID Original RVA
_ZNSt8__sce_v228_Cnd_register_at_thread_exitEPP12pthread_condPP13pthread_mutexPi +fAmL52-yfQ 0x10AE00
_ZNSt8__sce_v230_Cnd_unregister_at_thread_exitEPP13pthread_mutex SwcNvp-Af6c 0x10B110
_ZNSt8__sce_v232_Cnd_do_broadcast_at_thread_exitEv 2s6aLdPIA4I 0x10B1C0
_ZNSt8__sce_v226_Lock_at_thread_exit_mutexEv 2qvjaae0N-M 0x10ADB0
_ZNSt8__sce_v228_Unlock_at_thread_exit_mutexEv hx11Xia3FSY 0x10ADE0

The behavior comes from the libc.prx bundled with PPSA03671 v01.001.005 (J03848509, SHA-256 483961e086b8a1fffa8138b3c0badf798987fb5f35ec908d828946408be2d8a6).

These back std::notify_all_at_thread_exit and the promise ..._at_thread_exit calls. They take the versioned handles (pthread_cond**, pthread_mutex**) and use main's versioned _Mtx_*, _Cnd_broadcast, _Thrd_id and _Throw_Cpp_error. As in the module:

  • the entries (thread id, mutex, condition, flag) live in blocks of 20 with a count and a next pointer, laid out as the module's 0x290-byte block. A full block gets a new one from calloc, and an allocation failure drops the entry;
  • every call holds a recursive mutex named SceLibc_V2AtThreadExit (type 0x102), which _Lock_at_thread_exit_mutex and _Unlock_at_thread_exit_mutex take and release. A failed lock throws through _Throw_Cpp_error(5);
  • registering stores the entry in the first free slot and leaves the caller's mutex as it is;
  • unregistering removes every entry of the mutex;
  • a broadcast handles only the calling thread's entries. An entry with a flag relocks the mutex and sets the flag; every entry then broadcasts the condition, unlocks the mutex and is removed. An entry without a flag thus releases the lock that notify_all_at_thread_exit left held.

Nothing in the module calls the broadcast when a thread ends: as in Dinkumware's std::thread, the title's own thread code calls _Cnd_do_broadcast_at_thread_exit after the thread function returns, so nothing here adds a thread-exit hook. The module creates its mutex when it loads; here it is created on first use. The debt entry records both.

Tested

Windows x86-64, WinLibs GCC 15.2.0, Release, BUILD_TESTING=ON, main b611e634f7f. Exact tested commit: 5880453e377.

  • Built libc, libkernel, their NID-patched production libraries and the related tests. The new guest_sce_v2_at_thread_exit passes 20 times in a row, along with guest_libc_threads, guest_libc_shared_mutex and public_headers.

  • The new test uses real guest mutexes and conditions. It checks:

    • flag entries, with and without the mutex held, and entries without a flag on a mutex locked once and twice;
    • three entries on one mutex, and unregistering several entries of a mutex;
    • that an entry is broadcast once, and that another thread's entry is skipped;
    • a waiter woken by a thread that registers and broadcasts;
    • 45 entries across three blocks with a slot reused after unregistering;
    • that the global mutex is recursive and makes another thread's registration wait.
  • The original functions were loaded from the bundled module with their relocations applied and its static constructor for the mutex run. Their imports were bound by NID to this branch's libc and libkernel, so both sides used the same guest mutexes and conditions. The module's heap is not set up outside a console process, so its calloc was redirected to the host's; without that, the module drops every entry past the twentieth.

  • Side by side with the patched production library, the same random operation sequences ran on each side over 1,400 sequences and two seeds, with 25,041 registrations. The sequences registered from the main thread and from three long-lived helper threads, with and without flags and held locks, and unregistered, broadcast and nested the global mutex. Each run then compared the 48 flags and whether each mutex was still held, and no run differed. Long-lived helpers were needed because a thread id is the address of the thread's object, which a new thread can reuse.

  • Negative control: four lines were altered one at a time:

    • a broadcast handled every thread's entries;
    • a flag entry no longer relocked its mutex;
    • unregistering stopped at the first entry;
    • a full block got no new block.

    The new CTest failed on each, and the side-by-side run reported differences or, for the first, deadlocked. The source was then restored byte for byte (SHA-256 checked) and rebuilt, and the test and side-by-side run passed again.

  • Code conventions and git diff --check pass.

  • tools/pr_overlap.py read 1,859 open PRs at the tested commit and reports no overlap. It keeps exit 2 for 42 branches that already conflict with main. Merging each of the 42 with this branch and with main using git merge-tree gives the same conflict blocks except in docs/dev/debt/libc.md for #14212, which reflows the whole file, so the new debt entry lands inside one of its conflict blocks wherever it goes. #9447 names _Cnd_do_broadcast_at_thread_exit and _Cnd_register_at_thread_exit only in a debt entry that says they are not implemented; it adds no code for them. The branch also merges cleanly with #15336, #15471, #15484, #15490, #15501, #15519, #15552, #15561, #15602, #15614, #15667, #15683, #15791, #15796, #15809 and #15834.

Not tested: Linux, macOS, another SDK, a game, GPU or console. No game compatibility claim. No original module, extracted bytes, harness or log is committed or uploaded.

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
  • GPUs tested: n/a
  • AI-assisted: yes

🤖 Generated with Claude Code

<!-- greyfinch-sync:boykopovar/AnyPS5:issue:5161845333 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/15879). Originally posted by [Danigamer05](https://github.com/Danigamer05) on 2026-11-04T21:24:02Z. --- ### What Export the versioned `std::__sce_v2` at-thread-exit calls from libc. | Export | NID | Original RVA | | --- | --- | --- | | `_ZNSt8__sce_v228_Cnd_register_at_thread_exitEPP12pthread_condPP13pthread_mutexPi` | `+fAmL52-yfQ` | `0x10AE00` | | `_ZNSt8__sce_v230_Cnd_unregister_at_thread_exitEPP13pthread_mutex` | `SwcNvp-Af6c` | `0x10B110` | | `_ZNSt8__sce_v232_Cnd_do_broadcast_at_thread_exitEv` | `2s6aLdPIA4I` | `0x10B1C0` | | `_ZNSt8__sce_v226_Lock_at_thread_exit_mutexEv` | `2qvjaae0N-M` | `0x10ADB0` | | `_ZNSt8__sce_v228_Unlock_at_thread_exit_mutexEv` | `hx11Xia3FSY` | `0x10ADE0` | The behavior comes from the `libc.prx` bundled with PPSA03671 v01.001.005 (J03848509, SHA-256 `483961e086b8a1fffa8138b3c0badf798987fb5f35ec908d828946408be2d8a6`). These back `std::notify_all_at_thread_exit` and the `promise` `..._at_thread_exit` calls. They take the versioned handles (`pthread_cond**`, `pthread_mutex**`) and use main's versioned `_Mtx_*`, `_Cnd_broadcast`, `_Thrd_id` and `_Throw_Cpp_error`. As in the module: - the entries (thread id, mutex, condition, flag) live in blocks of 20 with a count and a next pointer, laid out as the module's 0x290-byte block. A full block gets a new one from `calloc`, and an allocation failure drops the entry; - every call holds a recursive mutex named `SceLibc_V2AtThreadExit` (type `0x102`), which `_Lock_at_thread_exit_mutex` and `_Unlock_at_thread_exit_mutex` take and release. A failed lock throws through `_Throw_Cpp_error(5)`; - registering stores the entry in the first free slot and leaves the caller's mutex as it is; - unregistering removes every entry of the mutex; - a broadcast handles only the calling thread's entries. An entry with a flag relocks the mutex and sets the flag; every entry then broadcasts the condition, unlocks the mutex and is removed. An entry without a flag thus releases the lock that `notify_all_at_thread_exit` left held. Nothing in the module calls the broadcast when a thread ends: as in Dinkumware's `std::thread`, the title's own thread code calls `_Cnd_do_broadcast_at_thread_exit` after the thread function returns, so nothing here adds a thread-exit hook. The module creates its mutex when it loads; here it is created on first use. The debt entry records both. ### Tested <!-- OS, title or homebrew, what was checked. "Not tested" is a valid answer. A performance change: the summary lines of main and of this PR from the same machine and scene, with the traces attached, captured as the Performance report issue form describes. --> Windows x86-64, WinLibs GCC 15.2.0, Release, `BUILD_TESTING=ON`, main `b611e634f7f`. Exact tested commit: `5880453e377`. - Built libc, libkernel, their NID-patched production libraries and the related tests. The new `guest_sce_v2_at_thread_exit` passes 20 times in a row, along with `guest_libc_threads`, `guest_libc_shared_mutex` and `public_headers`. - The new test uses real guest mutexes and conditions. It checks: - flag entries, with and without the mutex held, and entries without a flag on a mutex locked once and twice; - three entries on one mutex, and unregistering several entries of a mutex; - that an entry is broadcast once, and that another thread's entry is skipped; - a waiter woken by a thread that registers and broadcasts; - 45 entries across three blocks with a slot reused after unregistering; - that the global mutex is recursive and makes another thread's registration wait. - The original functions were loaded from the bundled module with their relocations applied and its static constructor for the mutex run. Their imports were bound by NID to this branch's libc and libkernel, so both sides used the same guest mutexes and conditions. The module's heap is not set up outside a console process, so its `calloc` was redirected to the host's; without that, the module drops every entry past the twentieth. - Side by side with the patched production library, the same random operation sequences ran on each side over 1,400 sequences and two seeds, with 25,041 registrations. The sequences registered from the main thread and from three long-lived helper threads, with and without flags and held locks, and unregistered, broadcast and nested the global mutex. Each run then compared the 48 flags and whether each mutex was still held, and no run differed. Long-lived helpers were needed because a thread id is the address of the thread's object, which a new thread can reuse. - Negative control: four lines were altered one at a time: - a broadcast handled every thread's entries; - a flag entry no longer relocked its mutex; - unregistering stopped at the first entry; - a full block got no new block. The new CTest failed on each, and the side-by-side run reported differences or, for the first, deadlocked. The source was then restored byte for byte (SHA-256 checked) and rebuilt, and the test and side-by-side run passed again. - Code conventions and `git diff --check` pass. - `tools/pr_overlap.py` read 1,859 open PRs at the tested commit and reports no overlap. It keeps exit 2 for 42 branches that already conflict with main. Merging each of the 42 with this branch and with main using `git merge-tree` gives the same conflict blocks except in `docs/dev/debt/libc.md` for #14212, which reflows the whole file, so the new debt entry lands inside one of its conflict blocks wherever it goes. #9447 names `_Cnd_do_broadcast_at_thread_exit` and `_Cnd_register_at_thread_exit` only in a debt entry that says they are not implemented; it adds no code for them. The branch also merges cleanly with #15336, #15471, #15484, #15490, #15501, #15519, #15552, #15561, #15602, #15614, #15667, #15683, #15791, #15796, #15809 and #15834. Not tested: Linux, macOS, another SDK, a game, GPU or console. No game compatibility claim. No original module, extracted bytes, harness or log is committed or uploaded. ### 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: none - [x] Fixes: none - [x] Unblocks: none - [x] GPUs tested: n/a - [x] AI-assisted: yes 🤖 Generated with [Claude Code](https://claude.com/claude-code)
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!15879
No description provided.