feat(libkernel_sys): implement pthread_suspend_all_np and pthread_single_np #12642

Closed
2Swagger wants to merge 12 commits from upstream-pr/12642 into main
2Swagger commented 2026-10-28 23:45:51 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by 2Swagger on 2026-10-28T23:45:51Z.


Stacked on #12130: this diff includes #12130's commits until it merges. PR B's own change is commit 1a0176a51.

What

Implements pthread_suspend_all_np and pthread_single_np in libkernel_sys, following FreeBSD 11.4 libthr (thr_suspend_np.c, thr_single_np.c). pthread_suspend_all_np suspends every other thread and returns once all of them are suspended; pthread_single_np calls it and returns 0. It builds on pthread_suspend_np from #12130 and reuses its safe points, so a thread is only ever stopped in guest code, in a libkernel wait or when a libkernel sleep returns.

  • One caller at a time: like libthr's _single_thread lock, only one thread at a time may run pthread_suspend_all_np or pthread_resume_all_np (which now takes the same slot, as thr_resume_np.c does). A thread waiting for the slot checks it every 1 ms and parks there if it has been suspended. So when two threads call pthread_suspend_all_np at once, the first one suspends the second, and the second goes on only after the first resumes everyone.
  • The caller is never suspended while it holds the slot: that matches libthr's check_suspend. A pthread_suspend_np aimed at the caller takes effect after the caller leaves pthread_suspend_all_np. While a thread waits in pthread_suspend_np it can be suspended itself, so a suspend-all never waits on a thread that is waiting on it.
  • Rescan after every wait: threads are marked first. The caller then repeatedly takes the first other thread that is not suspended yet, waits for it with the thread list unlocked, and starts the scan over. A thread created meanwhile is therefore caught by the same or the next scan, and a thread created suspended parks when it starts. A thread that exits while being waited for is skipped. That includes a host thread that adopted itself through scePthreadSelf and has since exited, which stays in the thread list on Windows.
  • Cancellation: a cancel that arrives during the call takes effect when it returns. libthr switches cancellation off for the call (no_cancel) and then calls _thr_testcancel. Here cancellation is not switched off: nothing inside pthread_suspend_all_np is a cancellation point, so a cancel cannot take effect midway, and the pending-cancel check at the end (scePthreadTestcancel, as the existing pthread_resume_all_np export does) gives the same result.

The pthread_suspend_np debt entry in docs/dev/debt/libkernel_sys.md now covers both functions. It adds that pthread_suspend_all_np waits for each thread in turn, so one that is busy in host calls delays the whole call. scePthreadSuspendAll and scePthreadSingle still throw; their sce error values are unmeasured.

Tested

  • New guest_pthread_suspend_all, about 1 s per run:
    • Three workers (a guest-code spinner, a sceKernelUsleep(200) loop, a scePthreadRename loop) stop under pthread_suspend_all_np and continue after pthread_resume_all_np, 20 rounds.
    • pthread_single_np returns 0 and stops a spinner and a thread blocked on a mutex; the blocked thread does not take the mutex released meanwhile. pthread_multi_np lets both continue.
    • A thread suspended earlier with pthread_suspend_np and a thread created suspended stay stopped through suspend-all, and both run after resume-all.
    • Two threads each call suspend-all and resume-all 30 times, while the test's main thread spins; they are never both past pthread_suspend_all_np at once, and both finish.
    • A thread keeps creating spinning threads; after each of 5 suspend-alls, no thread runs and no new one is created until resume-all.
    • A thread cancels itself and then calls pthread_suspend_all_np. It exits as cancelled, and its cleanup handler sees every other thread stopped before it resumes them.
    • One thread calls pthread_suspend_np on another while that one is in a 50 ms host call just before calling pthread_suspend_all_np. Either the suspend lands before the suspend-all, or the suspend-all suspends the waiting thread and the suspend lands after it returns. In both cases pthread_suspend_np returns with the target stopped. A following pthread_resume_all_np from the main thread would block if a suspended thread still held the slot. The test doesn't record which order happened.
  • Windows 11 (WinLibs GCC 15.2.0, Release), at head 4a30c0bf9 (1a0176a51 on #12130 at 039d7c7ad, merged onto main 35f59e44e): guest_pthread_suspend_all, guest_pthread_suspend, guest_pthread_create_suspended, guest_exception_handler, guest_raise_exception, guest_raise_nested_sleep, guest_mutex_raise_exception, guest_raise_in_host_code, guest_raise_leaves_host_code and public_headers with its compile batches, 74 of 74 passed. guest_pthread_suspend_all, guest_pthread_suspend and guest_pthread_create_suspended passed 200 consecutive runs each (ctest --repeat until-fail:200).
  • Linux (Ubuntu 24.04 under WSL2, GCC 13.3.0, Release), at 1a0176a51 merged onto main 319621ba2, before the latest merge of main: the same set as registered there, 70 of 70 passed, and the same three tests passed 200 consecutive runs each. That merge brought no change to the suspend code.
  • Conventions and test registration pass. Not tested on a console or a title.

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: #10817, #12130
  • Fixes: none
  • Unblocks: none
  • GPUs tested: n/a
  • AI-assisted: yes

🤖 Generated with Claude Code

<!-- greyfinch-sync:boykopovar/AnyPS5:issue:5059513979 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/12642). Originally posted by [2Swagger](https://github.com/2Swagger) on 2026-10-28T23:45:51Z. --- Stacked on #12130: this diff includes #12130's commits until it merges. PR B's own change is commit 1a0176a51. ### What Implements `pthread_suspend_all_np` and `pthread_single_np` in libkernel_sys, following FreeBSD 11.4 libthr ([`thr_suspend_np.c`](https://github.com/freebsd/freebsd-src/blob/8c639945/lib/libthr/thread/thr_suspend_np.c), [`thr_single_np.c`](https://github.com/freebsd/freebsd-src/blob/8c639945/lib/libthr/thread/thr_single_np.c)). `pthread_suspend_all_np` suspends every other thread and returns once all of them are suspended; `pthread_single_np` calls it and returns 0. It builds on `pthread_suspend_np` from #12130 and reuses its safe points, so a thread is only ever stopped in guest code, in a libkernel wait or when a libkernel sleep returns. - **One caller at a time:** like libthr's `_single_thread` lock, only one thread at a time may run `pthread_suspend_all_np` or `pthread_resume_all_np` (which now takes the same slot, as `thr_resume_np.c` does). A thread waiting for the slot checks it every 1 ms and parks there if it has been suspended. So when two threads call `pthread_suspend_all_np` at once, the first one suspends the second, and the second goes on only after the first resumes everyone. - **The caller is never suspended while it holds the slot:** that matches libthr's `check_suspend`. A `pthread_suspend_np` aimed at the caller takes effect after the caller leaves `pthread_suspend_all_np`. While a thread waits in `pthread_suspend_np` it can be suspended itself, so a suspend-all never waits on a thread that is waiting on it. - **Rescan after every wait:** threads are marked first. The caller then repeatedly takes the first other thread that is not suspended yet, waits for it with the thread list unlocked, and starts the scan over. A thread created meanwhile is therefore caught by the same or the next scan, and a thread created suspended parks when it starts. A thread that exits while being waited for is skipped. That includes a host thread that adopted itself through `scePthreadSelf` and has since exited, which stays in the thread list on Windows. - **Cancellation:** a cancel that arrives during the call takes effect when it returns. libthr switches cancellation off for the call (`no_cancel`) and then calls `_thr_testcancel`. Here cancellation is not switched off: nothing inside `pthread_suspend_all_np` is a cancellation point, so a cancel cannot take effect midway, and the pending-cancel check at the end (`scePthreadTestcancel`, as the existing `pthread_resume_all_np` export does) gives the same result. The `pthread_suspend_np` debt entry in `docs/dev/debt/libkernel_sys.md` now covers both functions. It adds that `pthread_suspend_all_np` waits for each thread in turn, so one that is busy in host calls delays the whole call. `scePthreadSuspendAll` and `scePthreadSingle` still throw; their sce error values are unmeasured. ### Tested - New `guest_pthread_suspend_all`, about 1 s per run: - Three workers (a guest-code spinner, a `sceKernelUsleep(200)` loop, a `scePthreadRename` loop) stop under `pthread_suspend_all_np` and continue after `pthread_resume_all_np`, 20 rounds. - `pthread_single_np` returns 0 and stops a spinner and a thread blocked on a mutex; the blocked thread does not take the mutex released meanwhile. `pthread_multi_np` lets both continue. - A thread suspended earlier with `pthread_suspend_np` and a thread created suspended stay stopped through suspend-all, and both run after resume-all. - Two threads each call suspend-all and resume-all 30 times, while the test's main thread spins; they are never both past `pthread_suspend_all_np` at once, and both finish. - A thread keeps creating spinning threads; after each of 5 suspend-alls, no thread runs and no new one is created until resume-all. - A thread cancels itself and then calls `pthread_suspend_all_np`. It exits as cancelled, and its cleanup handler sees every other thread stopped before it resumes them. - One thread calls `pthread_suspend_np` on another while that one is in a 50 ms host call just before calling `pthread_suspend_all_np`. Either the suspend lands before the suspend-all, or the suspend-all suspends the waiting thread and the suspend lands after it returns. In both cases `pthread_suspend_np` returns with the target stopped. A following `pthread_resume_all_np` from the main thread would block if a suspended thread still held the slot. The test doesn't record which order happened. - Windows 11 (WinLibs GCC 15.2.0, Release), at head 4a30c0bf9 (1a0176a51 on #12130 at 039d7c7ad, merged onto `main` 35f59e44e): `guest_pthread_suspend_all`, `guest_pthread_suspend`, `guest_pthread_create_suspended`, `guest_exception_handler`, `guest_raise_exception`, `guest_raise_nested_sleep`, `guest_mutex_raise_exception`, `guest_raise_in_host_code`, `guest_raise_leaves_host_code` and `public_headers` with its compile batches, 74 of 74 passed. `guest_pthread_suspend_all`, `guest_pthread_suspend` and `guest_pthread_create_suspended` passed 200 consecutive runs each (`ctest --repeat until-fail:200`). - Linux (Ubuntu 24.04 under WSL2, GCC 13.3.0, Release), at 1a0176a51 merged onto `main` 319621ba2, before the latest merge of `main`: the same set as registered there, 70 of 70 passed, and the same three tests passed 200 consecutive runs each. That merge brought no change to the suspend code. - Conventions and test registration pass. Not tested on a console or a title. ### 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: #10817, #12130 - [x] Fixes: none - [x] Unblocks: none - [x] GPUs tested: n/a - [x] AI-assisted: yes 🤖 Generated with [Claude Code](https://claude.com/claude-code)
MarcZX10 commented 2026-11-20 09:50:22 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-11-20T09:50:22Z.

Review: CHANGES_REQUESTED


Reviewed — changes requested

Changes are required before merge: unblock or otherwise reliably deliver the Linux park signal to targets whose inherited signal mask blocks it.

What I ran

Reviewed head 4a30c0bf9f5d8dcd374d8b9bfa8fe3ddded6c4ed using the prepared diff and repo_read.py; I ran no commands or tests. The test results in the PR description are author-reported.

Results

Check Result Note
Diff and targeted source review passed Found a Linux signal-mask hang risk
Tests in this review not run No execution environment was available
Console behavior not run PR also reports this as untested

Negative control

Not run; no test execution was available.

Findings

Blocking

  • On Linux, RequestGuestThreadPark relies on SIGUSR2 to interrupt the target, but guest threads inherit their creator’s signal mask and restore it before running. If that mask blocks SIGUSR2, the handler cannot run and the suspend call keeps resending the blocked signal while waiting for the target to park. Ensure the park signal is deliverable to guest threads even when inherited masks block it, while preserving the guest-visible signal mask. The signal handler installation and reliance on SIGUSR2 are introduced at core/libs/prx/libkernel/System/src/Exception.cpp:410 and core/libs/prx/libkernel/System/src/Exception.cpp:1234. Pinned source

Non-blocking

None.

Not verified

No tests or console checks were run. I read the prepared description, commits, manifest, and diff; targeted source ranges in Thread.cpp, Exception.cpp, TimedWait.cpp, TimedWait.hpp, Pthread.hpp, Mutex.cpp, Export.Discovery.cpp, and Time.cpp; and the opening portion of GuestPthreadSuspendAll.cpp. I did not read the full text of every changed file, including the complete test sources. files.json was unavailable through the reader, so excluded-file details are not verified.

To get this to a good state

Make the Linux park signal reliably deliverable despite inherited signal masks, add coverage for a target created while that signal is blocked, and rerun the relevant suspend tests.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5990718056 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/12642#pullrequestreview-5990718056). Originally posted by [MarcZX10](https://github.com/MarcZX10) on 2026-11-20T09:50:22Z. **Review:** CHANGES_REQUESTED --- # Reviewed — changes requested Changes are required before merge: unblock or otherwise reliably deliver the Linux park signal to targets whose inherited signal mask blocks it. ## What I ran Reviewed head `4a30c0bf9f5d8dcd374d8b9bfa8fe3ddded6c4ed` using the prepared diff and `repo_read.py`; I ran no commands or tests. The test results in the PR description are author-reported. ## Results | Check | Result | Note | |---|---|---| | Diff and targeted source review | passed | Found a Linux signal-mask hang risk | | Tests in this review | not run | No execution environment was available | | Console behavior | not run | PR also reports this as untested | ## Negative control Not run; no test execution was available. ## Findings ### Blocking - On Linux, `RequestGuestThreadPark` relies on `SIGUSR2` to interrupt the target, but guest threads inherit their creator’s signal mask and restore it before running. If that mask blocks `SIGUSR2`, the handler cannot run and the suspend call keeps resending the blocked signal while waiting for the target to park. Ensure the park signal is deliverable to guest threads even when inherited masks block it, while preserving the guest-visible signal mask. The signal handler installation and reliance on `SIGUSR2` are introduced at `core/libs/prx/libkernel/System/src/Exception.cpp:410` and `core/libs/prx/libkernel/System/src/Exception.cpp:1234`. [Pinned source](https://github.com/boykopovar/AnyPS5/blob/4a30c0bf9f5d8dcd374d8b9bfa8fe3ddded6c4ed/core/libs/prx/libkernel/System/src/Exception.cpp) ### Non-blocking None. ## Not verified No tests or console checks were run. I read the prepared description, commits, manifest, and diff; targeted source ranges in `Thread.cpp`, `Exception.cpp`, `TimedWait.cpp`, `TimedWait.hpp`, `Pthread.hpp`, `Mutex.cpp`, `Export.Discovery.cpp`, and `Time.cpp`; and the opening portion of `GuestPthreadSuspendAll.cpp`. I did not read the full text of every changed file, including the complete test sources. `files.json` was unavailable through the reader, so excluded-file details are not verified. ## To get this to a good state Make the Linux park signal reliably deliverable despite inherited signal masks, add coverage for a target created while that signal is blocked, and rerun the relevant suspend tests.
ligmaSec commented 2026-12-03 22:56:29 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by ligmaSec on 2026-12-03T22:56:29Z.

Review: COMMENTED


SuspendAllGuestThreads_nid_no_patch marks every live peer suspended before waiting for individual acknowledgements. On an exception from the host park path, the slot is released by RAII, but there is no corresponding rollback for the other peers whose state was changed in the first pass. Please restore only the suspensions owned by this invocation on failure, preserving peers already suspended by a prior call. The trial merge conflicts in Thread.cpp and Exception.cpp, and the debt change needs a fragment.

AI-assisted: yes.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:6180101260 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/12642#pullrequestreview-6180101260). Originally posted by [ligmaSec](https://github.com/ligmaSec) on 2026-12-03T22:56:29Z. **Review:** COMMENTED --- `SuspendAllGuestThreads_nid_no_patch` marks every live peer suspended before waiting for individual acknowledgements. On an exception from the host park path, the slot is released by RAII, but there is no corresponding rollback for the other peers whose state was changed in the first pass. Please restore only the suspensions owned by this invocation on failure, preserving peers already suspended by a prior call. The trial merge conflicts in `Thread.cpp` and `Exception.cpp`, and the debt change needs a fragment. AI-assisted: yes.

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