feat(libkernel_sys): implement pthread_main_np #9197

Closed
DavidSamir wants to merge 2 commits from upstream-pr/9197 into main
DavidSamir commented 2026-10-21 15:04:26 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by DavidSamir on 2026-10-21T15:04:26Z.


What

Implements pthread_main_np, which libkernel_sys/Export.Discovery.cpp declared as a stub that threw NotImplemented. It returns 1 on the initial thread and 0 on any other thread.

Reference: FreeBSD 11 lib/libthr/thread/thr_main_np.c returns -1 if the thread library isn't initialized, else pthread_equal(pthread_self(), _thr_initial) ? 1 : 0. The library is always initialized here, so the -1 arm never applies.

  • libkernel: IsInitialThread_nid_no_patch() in Pthread/src/Thread.cpp.
    • On Linux the initial thread is the one whose thread ID equals the process ID.
    • On Windows it is the thread that ran the initializer of the libkernel module. That is the process's primary thread: the relinked executable's entry stub loads the libraries and then enters the ELF entry point itself, and it imports no thread-creation function.
  • libkernel_sys: pthread_main_np calls it.

scePthreadMain is not part of this PR: its argument and return types are not recorded anywhere we can cite.

Tested

Windows 11, WinLibs GCC 15.2.0, Release, guest_kernel_sys, extended with pthread_main_np on the initial thread and on a std::thread. Head 8b634f5b16, rebased onto main cecb540a63:

  • The branch passes, 3 of 3 runs.
  • With main's Export.Discovery.cpp and Thread.cpp and this branch's test, it aborts with pthread_main_np_nid_postfix not implemented.
  • The first commit alone built and passed guest_kernel_sys before the rebase.
  • python tools/check_conventions.py --base origin/main passes.

Merge checks with git merge-tree: clean against main, #7838, #8987, #9097, #9156 and #9171. #7149 conflicts with main by itself, in files this PR does not change. tools/pr_overlap.py did not finish on this machine (stopped after 25 and then 55 minutes); the branch was checked against the diffs of all open pull requests on 2026-10-21 instead.

Not run on Linux or macOS, and not run with a title. The Linux branch is gettid() == getpid(), the documented identity of a process's main thread.

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
<!-- greyfinch-sync:boykopovar/AnyPS5:issue:4953753391 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/9197). Originally posted by [DavidSamir](https://github.com/DavidSamir) on 2026-10-21T15:04:26Z. --- ### What Implements `pthread_main_np`, which `libkernel_sys/Export.Discovery.cpp` declared as a stub that threw `NotImplemented`. It returns 1 on the initial thread and 0 on any other thread. Reference: FreeBSD 11 [`lib/libthr/thread/thr_main_np.c`](https://github.com/freebsd/freebsd-src/blob/stable/11/lib/libthr/thread/thr_main_np.c) returns `-1` if the thread library isn't initialized, else `pthread_equal(pthread_self(), _thr_initial) ? 1 : 0`. The library is always initialized here, so the `-1` arm never applies. - **libkernel:** `IsInitialThread_nid_no_patch()` in `Pthread/src/Thread.cpp`. - On Linux the initial thread is the one whose thread ID equals the process ID. - On Windows it is the thread that ran the initializer of the libkernel module. That is the process's primary thread: the relinked executable's entry stub loads the libraries and then enters the ELF entry point itself, and it imports no thread-creation function. - **libkernel_sys:** `pthread_main_np` calls it. `scePthreadMain` is not part of this PR: its argument and return types are not recorded anywhere we can cite. ### Tested Windows 11, WinLibs GCC 15.2.0, Release, `guest_kernel_sys`, extended with `pthread_main_np` on the initial thread and on a `std::thread`. Head `8b634f5b16`, rebased onto `main` `cecb540a63`: - The branch passes, 3 of 3 runs. - With `main`'s `Export.Discovery.cpp` and `Thread.cpp` and this branch's test, it aborts with `pthread_main_np_nid_postfix not implemented`. - The first commit alone built and passed `guest_kernel_sys` before the rebase. - `python tools/check_conventions.py --base origin/main` passes. Merge checks with `git merge-tree`: clean against `main`, #7838, #8987, #9097, #9156 and #9171. #7149 conflicts with `main` by itself, in files this PR does not change. `tools/pr_overlap.py` did not finish on this machine (stopped after 25 and then 55 minutes); the branch was checked against the diffs of all open pull requests on 2026-10-21 instead. Not run on Linux or macOS, and not run with a title. The Linux branch is `gettid() == getpid()`, the documented identity of a process's main thread. ### 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
github-actions[bot] commented 2026-10-21 15:07:21 +00:00 (Migrated from github.com)

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


Progress report (cecb540 → 8b634f5)

📈 System libraries: 42.11% (+0.01%, +1 implemented functions)

✅ 1 implemented
Library Name
libkernel_sys pthread_main_np_nid_postfix
<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6319319280 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/9197#issuecomment-6319319280). Originally posted by [github-actions[bot]](https://github.com/apps/github-actions) on 2026-10-21T15:07:21Z. --- <!-- progress-report --> ### Progress report (cecb540 → 8b634f5) 📈 **System libraries**: 42.11% (+0.01%, +1 implemented functions) <details> <summary>✅ 1 implemented</summary> | Library | Name | | - | - | | libkernel_sys | `pthread_main_np_nid_postfix` | </details>
DavidSamir commented 2026-10-21 15:09:30 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by DavidSamir on 2026-10-21T15:09:30Z.


Closing: #9160 implements pthread_main_np the same way and opened first.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6319361628 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/9197#issuecomment-6319361628). Originally posted by [DavidSamir](https://github.com/DavidSamir) on 2026-10-21T15:09:30Z. --- Closing: #9160 implements pthread_main_np the same way and opened first.

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