feat(libkernel_sys): implement pthread_suspend_all_np and pthread_single_np #12642
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/12642"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
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_npandpthread_single_npin libkernel_sys, following FreeBSD 11.4 libthr (thr_suspend_np.c,thr_single_np.c).pthread_suspend_all_npsuspends every other thread and returns once all of them are suspended;pthread_single_npcalls it and returns 0. It builds onpthread_suspend_npfrom #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._single_threadlock, only one thread at a time may runpthread_suspend_all_nporpthread_resume_all_np(which now takes the same slot, asthr_resume_np.cdoes). A thread waiting for the slot checks it every 1 ms and parks there if it has been suspended. So when two threads callpthread_suspend_all_npat once, the first one suspends the second, and the second goes on only after the first resumes everyone.check_suspend. Apthread_suspend_npaimed at the caller takes effect after the caller leavespthread_suspend_all_np. While a thread waits inpthread_suspend_npit can be suspended itself, so a suspend-all never waits on a thread that is waiting on it.scePthreadSelfand has since exited, which stays in the thread list on Windows.no_cancel) and then calls_thr_testcancel. Here cancellation is not switched off: nothing insidepthread_suspend_all_npis a cancellation point, so a cancel cannot take effect midway, and the pending-cancel check at the end (scePthreadTestcancel, as the existingpthread_resume_all_npexport does) gives the same result.The
pthread_suspend_npdebt entry indocs/dev/debt/libkernel_sys.mdnow covers both functions. It adds thatpthread_suspend_all_npwaits for each thread in turn, so one that is busy in host calls delays the whole call.scePthreadSuspendAllandscePthreadSinglestill throw; their sce error values are unmeasured.Tested
guest_pthread_suspend_all, about 1 s per run:sceKernelUsleep(200)loop, ascePthreadRenameloop) stop underpthread_suspend_all_npand continue afterpthread_resume_all_np, 20 rounds.pthread_single_npreturns 0 and stops a spinner and a thread blocked on a mutex; the blocked thread does not take the mutex released meanwhile.pthread_multi_nplets both continue.pthread_suspend_npand a thread created suspended stay stopped through suspend-all, and both run after resume-all.pthread_suspend_all_npat once, and both finish.pthread_suspend_all_np. It exits as cancelled, and its cleanup handler sees every other thread stopped before it resumes them.pthread_suspend_npon another while that one is in a 50 ms host call just before callingpthread_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 casespthread_suspend_npreturns with the target stopped. A followingpthread_resume_all_npfrom the main thread would block if a suspended thread still held the slot. The test doesn't record which order happened.4a30c0bf9(1a0176a51on #12130 at039d7c7ad, merged ontomain35f59e44e):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_codeandpublic_headerswith its compile batches, 74 of 74 passed.guest_pthread_suspend_all,guest_pthread_suspendandguest_pthread_create_suspendedpassed 200 consecutive runs each (ctest --repeat until-fail:200).1a0176a51merged ontomain319621ba2, before the latest merge ofmain: 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.Checklist
main; no other open PR implements the same functionsNotImplemented_nid_no_patch); silent stubs are listed under Silent stubs in their library's technical debt file.mdfiles or images added to the repository (attach them to this PR)🤖 Generated with Claude Code
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
4a30c0bf9f5d8dcd374d8b9bfa8fe3ddded6c4edusing the prepared diff andrepo_read.py; I ran no commands or tests. The test results in the PR description are author-reported.Results
Negative control
Not run; no test execution was available.
Findings
Blocking
RequestGuestThreadParkrelies onSIGUSR2to interrupt the target, but guest threads inherit their creator’s signal mask and restore it before running. If that mask blocksSIGUSR2, 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 onSIGUSR2are introduced atcore/libs/prx/libkernel/System/src/Exception.cpp:410andcore/libs/prx/libkernel/System/src/Exception.cpp:1234. Pinned sourceNon-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, andTime.cpp; and the opening portion ofGuestPthreadSuspendAll.cpp. I did not read the full text of every changed file, including the complete test sources.files.jsonwas 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.
Imported from boykopovar/AnyPS5. Originally posted by ligmaSec on 2026-12-03T22:56:29Z.
Review: COMMENTED
SuspendAllGuestThreads_nid_no_patchmarks 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 inThread.cppandException.cpp, and the debt change needs a fragment.AI-assisted: yes.
Pull request closed