Skip to content

feat(libkernel_sys): implement pthread_suspend_all_np and pthread_single_np - #12642

Closed
2Swagger wants to merge 12 commits into
boykopovar:mainfrom
2Swagger:feat/pthread-suspend-all-np
Closed

2Swagger wants to merge 12 commits into
boykopovar:mainfrom
2Swagger:feat/pthread-suspend-all-np

Conversation

@2Swagger

Copy link
Copy Markdown
Contributor

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

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 4a30c0b (1a0176a on feat(libkernel_sys): implement pthread_suspend_np #12130 at 039d7c7, merged onto main 35f59e4): 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 1a0176a merged onto main 319621b, 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

🤖 Generated with Claude Code

2Swagger and others added 12 commits October 27, 2026 18:30
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…export

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…g it

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…nded

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…d-np

# Conflicts:
#	docs/dev/debt/libkernel_sys.md
…eave the thread list

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…gle_np

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@github-actions

Copy link
Copy Markdown

Progress report (3a2b919 → 4a30c0b)

📈 System libraries: 45.93% (+0.02%, +3 implemented functions)

✅ 3 implemented
Library Name
libkernel_sys pthread_single_np_nid_postfix
libkernel_sys pthread_suspend_all_np_nid_postfix
libkernel_sys pthread_suspend_np_nid_postfix

Changed libraries (implemented exports)

Library Before After Change
libkernel_sys 116/514 (22.57%) 119/514 (23.15%) +0.58 pp

@github-actions

github-actions Bot commented Oct 29, 2026 •

Copy link
Copy Markdown

Other open pull requests touch the same code. Merging one will break the other, or both implement the same thing. If this one builds on them, list them in Depends on.

Pull request Conflicting files (lines on main) Same exports Same new files
PR 18112 pthread_suspend_all_np
pthread_suspend_np

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 7275 core/libs/CMakeLists.txt
PR 9302 core/libs/CMakeLists.txt
core/libs/prx/libkernel_sys/Export.Discovery.cpp
docs/dev/debt/libkernel_sys.md
PR 9938 core/libs/prx/libkernel/Pthread/include/Pthread.hpp
core/libs/prx/libkernel/Pthread/src/Thread.cpp
PR 10657 core/libs/prx/libkernel_sys/Export.Discovery.cpp
core/libs/prx/libkernel_sys/include/Api.hpp
docs/dev/debt/libkernel_sys.md
PR 11628 core/libs/prx/libkernel/System/src/Exception.cpp
PR 11661 core/libs/prx/libkernel/System/src/Exception.cpp
PR 11689 core/libs/prx/libkernel/System/src/Exception.cpp
PR 11698 core/libs/prx/libkernel/System/src/Exception.cpp
PR 12418 core/libs/prx/libkernel/Time/Time.cpp
PR 12544 core/libs/prx/libkernel/Pthread/src/Thread.cpp
PR 12682 core/libs/prx/libkernel/Pthread/include/Pthread.hpp
core/libs/prx/libkernel/Pthread/src/Thread.cpp
core/libs/prx/libkernel/System/src/Exception.cpp
PR 12688 core/libs/prx/libkernel/Pthread/include/Pthread.hpp
core/libs/prx/libkernel/Pthread/src/Thread.cpp
core/libs/prx/libkernel/System/src/Exception.cpp
PR 12700 core/libs/prx/libkernel/Pthread/include/Pthread.hpp
core/libs/prx/libkernel/Pthread/src/Thread.cpp
core/libs/prx/libkernel/System/src/Exception.cpp
PR 12965 core/libs/prx/libkernel/Pthread/include/Pthread.hpp
core/libs/prx/libkernel/Pthread/src/Thread.cpp
core/libs/prx/libkernel/System/src/Exception.cpp
PR 12994 core/libs/CMakeLists.txt
PR 15037 core/libs/prx/libkernel_sys/Export.Discovery.cpp
core/libs/prx/libkernel_sys/include/Api.hpp
docs/dev/debt/libkernel_sys.md
PR 15040 core/libs/prx/libkernel/Pthread/include/Pthread.hpp
core/libs/prx/libkernel/Pthread/src/Thread.cpp
PR 15838 core/libs/CMakeLists.txt
PR 16018 core/libs/CMakeLists.txt
PR 16052 core/libs/CMakeLists.txt
PR 16790 core/libs/prx/libkernel/Pthread/src/Thread.cpp
PR 16793 core/libs/CMakeLists.txt
PR 17936 core/libs/CMakeLists.txt
core/libs/prx/libkernel_sys/Export.Discovery.cpp
core/libs/prx/libkernel_sys/include/Api.hpp
docs/dev/debt/libkernel_sys.md
PR 18190 core/libs/prx/libkernel/Pthread/src/Mutex.cpp
core/libs/prx/libkernel/Pthread/src/Thread.cpp
core/libs/prx/libkernel/Time/Time.cpp
core/libs/prx/libkernel_sys/Export.Discovery.cpp
core/libs/prx/libkernel_sys/include/Api.hpp
docs/dev/debt/libkernel_sys.md
PR 18300 core/libs/CMakeLists.txt
PR 18324 core/libs/prx/libkernel/System/src/Exception.cpp
PR 19181 core/libs/prx/libkernel/Pthread/src/Thread.cpp
PR 19434 core/libs/CMakeLists.txt
PR 21571 core/libs/prx/libkernel/Pthread/src/Thread.cpp
PR 23395 core/libs/CMakeLists.txt
PR 23786 core/libs/CMakeLists.txt
PR 23953 core/libs/prx/libkernel/Pthread/src/Thread.cpp
PR 23975 core/libs/CMakeLists.txt
PR 24015 core/libs/CMakeLists.txt
PR 24641 core/libs/prx/libkernel/Time/Time.cpp
PR 24731 core/libs/CMakeLists.txt
PR 24762 core/libs/prx/libkernel/Time/Time.cpp
PR 25454 core/libs/CMakeLists.txt
PR 25470 core/libs/CMakeLists.txt
PR 25477 core/libs/CMakeLists.txt
PR 25493 core/libs/CMakeLists.txt
PR 25500 core/libs/CMakeLists.txt
PR 25999 core/libs/prx/libkernel/Pthread/include/Pthread.hpp
core/libs/prx/libkernel/Pthread/src/Thread.cpp
core/libs/prx/libkernel/Time/Time.cpp
PR 26069 core/libs/prx/libkernel/System/src/Exception.cpp
PR 26174 core/libs/prx/libkernel/Pthread/src/Thread.cpp
PR 26300 core/libs/prx/libkernel_sys/include/Api.hpp
PR 26805 core/libs/CMakeLists.txt
PR 27547 core/libs/CMakeLists.txt
PR 27750 core/libs/CMakeLists.txt
PR 27762 core/libs/CMakeLists.txt
PR 27801 core/libs/prx/libkernel/System/src/Exception.cpp
PR 27816 core/libs/prx/libkernel_sys/include/Api.hpp
PR 27864 core/libs/prx/libkernel/Pthread/src/Thread.cpp
PR 27988 core/libs/prx/libkernel/System/src/Exception.cpp
PR 27994 core/libs/prx/libkernel/Pthread/include/Pthread.hpp
core/libs/prx/libkernel/Pthread/src/Thread.cpp
PR 28006 core/libs/prx/libkernel/System/src/Exception.cpp
PR 28013 core/libs/prx/libkernel/System/src/Exception.cpp
PR 28043 core/libs/CMakeLists.txt
PR 28144 core/libs/CMakeLists.txt
PR 28210 core/libs/prx/libkernel/Pthread/src/Thread.cpp
PR 28253 core/libs/prx/libkernel/Pthread/src/Thread.cpp
PR 28255 core/libs/prx/libkernel/Pthread/src/Thread.cpp
PR 28294 core/libs/CMakeLists.txt
PR 28299 core/libs/prx/libkernel/Pthread/src/Mutex.cpp
core/libs/prx/libkernel/Pthread/src/Thread.cpp
core/libs/prx/libkernel/System/src/Exception.cpp
core/libs/prx/libkernel/Time/TimedWait.cpp
core/libs/prx/libkernel/Time/include/TimedWait.hpp
PR 28369 core/libs/prx/libkernel/Time/include/TimedWait.hpp
PR 28434 core/libs/prx/libkernel/Pthread/src/Thread.cpp
PR 28436 core/libs/prx/libkernel/Time/Time.cpp
PR 28679 core/libs/prx/libkernel/Pthread/src/Thread.cpp
core/libs/prx/libkernel/Time/Time.cpp
PR 28702 core/libs/prx/libkernel/Time/Time.cpp
PR 28808 core/libs/prx/libkernel/Pthread/src/Thread.cpp
core/libs/prx/libkernel/System/src/Exception.cpp
core/libs/prx/libkernel/Time/TimedWait.cpp
core/libs/prx/libkernel/Time/include/TimedWait.hpp

@JJenkx

JJenkx commented Oct 29, 2026

Copy link
Copy Markdown
Contributor

Windows run of this head (4a30c0bf9f), merged onto main 52c09d567d (clean merge, local only). Windows 11 Enterprise 10.0.22621, Ryzen 9 5950X, WinLibs GCC 15.2.0 UCRT r7, Ninja, Release, BUILD_TESTING=ON, no Vulkan driver visible.

  • This PR: guest_pthread_suspend, guest_pthread_suspend_all pass 3 of 3 (ctest --repeat until-fail:3).
  • Negative control: not possible. With this PR's source files (core/libs/prx/libkernel/Pthread/include/Pthread.hpp, core/libs/prx/libkernel/Pthread/src/Mutex.cpp, core/libs/prx/libkernel/Pthread/src/Thread.cpp, core/libs/prx/libkernel/System/src/Exception.cpp, core/libs/prx/libkernel/Time/Time.cpp, core/libs/prx/libkernel/Time/TimedWait.cpp and 3 more) put back to main's version, the build fails against the rest of the PR, so there is no source-only control.

AI-assisted: yes (Claude Code).

@github-actions github-actions Bot added the needs rebase Conflicts with main, rebase needed label Oct 30, 2026

@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.

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.

@frankea

frankea commented Nov 27, 2026

Copy link
Copy Markdown
Contributor

For information: CodeRabbit's review of #21464, which is stacked on this PR, flagged several suspension races in code that comes from this PR. Fixes for them are on #21464's branch (feat/sce-pthread-suspend-wrappers on frankea/AnyPS5) as five separate commits, 6d888ad, 5d6d6f8, 4a6a671, a63e12a and 2b0d8ff, each answering its review thread on #21464: a stale park acknowledged after a resume, a reference taken on a record that thread creation can still delete, a thread adopted during suspend-all that never got a park request, exited threads tracked by address instead of registry ID, one Windows host-suspend owner per target, the Linux park signal unblocked on guest threads, and the suspend tests skipped on macOS. They can be cherry-picked here if you prefer them in this PR; #21464 will keep them otherwise. Run on the Windows box (5 of 5 for every suspend test) and in a Linux container; details are in the #21464 threads.
AI-assisted: yes (Claude Code)

@ligmaSec ligmaSec left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

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.

@oneandonlydean

Copy link
Copy Markdown
Collaborator

I am closing this draft as stale: there has been no push since 28 Oct and it is still unfinished. The change implements pthread_suspend_all_np and pthread_single_np in libkernel_sys, and it still needs a rebase and the open review points. It can be reopened or re-pushed on a fresh main any time and we will look at it again.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

build docs libkernel needs rebase Conflicts with main, rebase needed prx

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants