feat(libc): implement std::_Execute_once #735

Merged
adriadam10 merged 1 commit from upstream-pr/735 into main 2026-10-05 23:38:08 +00:00
adriadam10 commented 2026-10-05 18:23:28 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by adriadam10 on 2026-10-05T18:23:28Z.


What

Implements std::_Execute_once(std::once_flag&, int (*)(void*, void*, void**), void*) (DiGVep5yB5w, _ZSt13_Execute_onceRSt9once_flagPFiPvS1_PS1_ES1_). It replaces the unnamed libcCyberUnknown01 stub in libc/Export.cpp. The NID was checked against the mangled name with tools/nid_names.py's hash.

No emulator reference has a body for it, so the behaviour comes from the disassembly of the real sce_module/libc.prx that a title ships (export at 0x81590, 157 bytes). It has the same Dinkumware lineage as Microsoft STL's xonce.cpp, but MS STL wraps InitOnceExecuteOnce instead:

  • one process-wide mutex, lazily created as SCE_PTHREAD_MUTEX_RECURSIVE (init helper called with flag 0x100 → settype(2)), held for the whole call
  • *flag != 0 → return 1 without calling the callback
  • otherwise callback(nullptr, arg, nullptr); a non-zero result sets *flag = 1 and returns 1, zero leaves the flag at 0 and returns 0 (a later call retries)
  • an exception from the callback unlocks the mutex and propagates (landing pad: unlock + _Unwind_Resume); the flag stays 0

Here: a static std::recursive_mutex held by a lock_guard, with the same flag/return logic.

The existing std_execute_once_nid_postfix (NID of the literal name std_execute_once, so no title can bind it) is left as it is.

Removing the stub sits next to the Export.cpp stubs #492 deletes. #492 already conflicts with main, and the extra hunk is a trivial delete/delete.

Tested

Linux (GCC Release, same as CI). New guest_execute_once test (core/libs/tests/GuestExecuteOnce.cpp, registered in libc's CMakeLists):

  • 16 threads on one flag: the callback runs exactly once while the others wait, and every caller sees its side effects after returning 1. It gets (nullptr, arg, nullptr), and a nested _Execute_once on another flag from inside the callback works (recursive mutex).
  • A later call on a done flag doesn't call the callback. A pre-set non-zero flag (-3) is treated as done and left untouched.
  • A callback returning 0 → returns 0, flag stays 0, the next call retries.
  • A throwing callback → the exception propagates, flag stays 0, the next call succeeds.

On main the test doesn't link (only the unnamed throwing stub exists). Negative checks, each one makes the test fail: not setting the flag, returning 1 on failure, wrong argument order, no lock, a non-recursive mutex, == 1 instead of != 0. The full guest_|libc ctest set passes (96).

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 in TechnicalDebt
  • 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
  • AI-assisted: yes
<!-- greyfinch-sync:boykopovar/AnyPS5:issue:4749173012 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/735). Originally posted by [adriadam10](https://github.com/adriadam10) on 2026-10-05T18:23:28Z. --- ### What Implements `std::_Execute_once(std::once_flag&, int (*)(void*, void*, void**), void*)` (`DiGVep5yB5w`, `_ZSt13_Execute_onceRSt9once_flagPFiPvS1_PS1_ES1_`). It replaces the unnamed `libcCyberUnknown01` stub in `libc/Export.cpp`. The NID was checked against the mangled name with `tools/nid_names.py`'s hash. No emulator reference has a body for it, so the behaviour comes from the disassembly of the real `sce_module/libc.prx` that a title ships (export at `0x81590`, 157 bytes). It has the same Dinkumware lineage as Microsoft STL's `xonce.cpp`, but MS STL wraps `InitOnceExecuteOnce` instead: - one process-wide mutex, lazily created as `SCE_PTHREAD_MUTEX_RECURSIVE` (init helper called with flag `0x100` → `settype(2)`), held for the whole call - `*flag != 0` → return 1 without calling the callback - otherwise `callback(nullptr, arg, nullptr)`; a non-zero result sets `*flag = 1` and returns 1, zero leaves the flag at 0 and returns 0 (a later call retries) - an exception from the callback unlocks the mutex and propagates (landing pad: unlock + `_Unwind_Resume`); the flag stays 0 Here: a static `std::recursive_mutex` held by a `lock_guard`, with the same flag/return logic. The existing `std_execute_once_nid_postfix` (NID of the literal name `std_execute_once`, so no title can bind it) is left as it is. Removing the stub sits next to the `Export.cpp` stubs #492 deletes. #492 already conflicts with `main`, and the extra hunk is a trivial delete/delete. ### Tested Linux (GCC Release, same as CI). New `guest_execute_once` test (`core/libs/tests/GuestExecuteOnce.cpp`, registered in libc's CMakeLists): - 16 threads on one flag: the callback runs exactly once while the others wait, and every caller sees its side effects after returning 1. It gets `(nullptr, arg, nullptr)`, and a nested `_Execute_once` on another flag from inside the callback works (recursive mutex). - A later call on a done flag doesn't call the callback. A pre-set non-zero flag (-3) is treated as done and left untouched. - A callback returning 0 → returns 0, flag stays 0, the next call retries. - A throwing callback → the exception propagates, flag stays 0, the next call succeeds. On `main` the test doesn't link (only the unnamed throwing stub exists). Negative checks, each one makes the test fail: not setting the flag, returning 1 on failure, wrong argument order, no lock, a non-recursive mutex, `== 1` instead of `!= 0`. The full `guest_|libc` ctest set passes (96). ### 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 in [TechnicalDebt](https://github.com/boykopovar/AnyPS5/blob/main/docs/dev/TechnicalDebt.md#silent-stubs) - [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] AI-assisted: yes
github-actions[bot] commented 2026-10-05 18:23:54 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by github-actions[bot] on 2026-10-05T18:23:54Z.


Progress report (bc24783 → 948d3cd)

📈 System libraries: 79.57% (+0.03%, +1 implemented, -1 removed functions)

✅ 1 implemented
Library Name
libc _ZSt13_Execute_onceRSt9once_flagPFiPvS1_PS1_ES1__nid_postfix
🗑️ 1 removed
Library Name
libc libcCyberUnknown01
<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6000525588 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/735#issuecomment-6000525588). Originally posted by [github-actions[bot]](https://github.com/apps/github-actions) on 2026-10-05T18:23:54Z. --- <!-- progress-report --> ### Progress report (bc24783 → 948d3cd) 📈 **System libraries**: 79.57% (+0.03%, +1 implemented, -1 removed functions) <details> <summary>✅ 1 implemented</summary> | Library | Name | | - | - | | libc | `_ZSt13_Execute_onceRSt9once_flagPFiPvS1_PS1_ES1__nid_postfix` | </details> <details> <summary>🗑️ 1 removed</summary> | Library | Name | | - | - | | libc | `libcCyberUnknown01` | </details>
oneandonlydean commented 2026-10-05 20:30:11 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by oneandonlydean on 2026-10-05T20:30:11Z.


Thanks for this. It needs a rebase: core/libs/prx/libc/Export.cpp conflicts with current main. #718 removed the neighbouring libcCyberUnknown00 stub (2gbcltk3swE, now div), and this PR removes libcCyberUnknown01 right after it, so the resolution is to drop both stubs.

The rest checks out. The NID DiGVep5yB5w matches the name, and the behaviour matches the _Execute_once in the libc.prx that PPSA21564 bundles: one global lock around the flag check, the callback called with (nullptr, arg, nullptr), and the flag set to 1 only when the callback returns non-zero. guest_execute_once fails to link on main's code and passes with the change, also 20 times in a row (Linux, GCC).

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6002365587 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/735#issuecomment-6002365587). Originally posted by [oneandonlydean](https://github.com/oneandonlydean) on 2026-10-05T20:30:11Z. --- Thanks for this. It needs a rebase: `core/libs/prx/libc/Export.cpp` conflicts with current `main`. #718 removed the neighbouring `libcCyberUnknown00` stub (`2gbcltk3swE`, now `div`), and this PR removes `libcCyberUnknown01` right after it, so the resolution is to drop both stubs. The rest checks out. The NID `DiGVep5yB5w` matches the name, and the behaviour matches the `_Execute_once` in the `libc.prx` that PPSA21564 bundles: one global lock around the flag check, the callback called with `(nullptr, arg, nullptr)`, and the flag set to 1 only when the callback returns non-zero. `guest_execute_once` fails to link on main's code and passes with the change, also 20 times in a row (Linux, GCC).
adriadam10 commented 2026-10-05 22:29:03 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by adriadam10 on 2026-10-05T22:29:03Z.


Rebased on current main: both neighbouring stubs (2gbcltk3swE, DiGVep5yB5w) are gone now. guest_execute_once passes.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6004461547 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/735#issuecomment-6004461547). Originally posted by [adriadam10](https://github.com/adriadam10) on 2026-10-05T22:29:03Z. --- Rebased on current `main`: both neighbouring stubs (`2gbcltk3swE`, `DiGVep5yB5w`) are gone now. `guest_execute_once` passes.
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!735
No description provided.