feat(libkernel_sys): implement pthread_suspend_np #12130
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/12130"
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-27T22:31:26Z.
What
Implements
pthread_suspend_npin libkernel_sys, following FreeBSD 11.4 libthr (thr_suspend_np.c):EINVALfor a null thread,ESRCHfor a finished one,EDEADLKfor the caller, otherwise 0 once the target is suspended. It builds on the create-suspended and resume side from #9573, sopthread_resume_np,pthread_resume_all_npandpthread_multi_npresume it unchanged.A running thread is only ever stopped at a safe point, where it holds no host lock, so suspending a thread can't stall the emulator itself (the kind of freeze #10666 describes for exception delivery):
SuspendThreadand stays frozen until resumed; on Linux aSIGUSR2handler parks it when the interrupted address is in the main executable or a*.guest.prx(code ranges collected withdl_iterate_phdroutside the handler).TimedWaitdelivery scope, and a newTimedWait::SuspendSafeWaitscope marks the Linux mutex waits inMutex.cppthe same way.sceKernelUsleep,nanosleep,usleepandsceKernelSleep(all throughCancellableSleep) park a thread with a pending suspend.Parked threads wait on a per-thread epoch word (a raw futex on Linux, so the signal handler stays async-signal-safe), and threads created suspended now park through the same function. A start flag closes the race between
pthread_createand the first suspend.docs/dev/debt/libkernel_sys.mdgets an entry for the gaps: a thread that spends nearly all its time in host calls is suspended late (withscePthreadRenamecalled back to back: median 160-205 ms and worst 1.3 s per suspend on Windows 11, median under 0.1 ms and worst 3 ms on Linux), a thread blocked in a host call outside the libkernel waits is suspended only after the call returns while FreeBSD suspends it at once, andsceKernelRaiseExceptionon a suspended thread throws, as for one created suspended.pthread_suspend_all_np,pthread_single_npand the sce forms still throw.Tested
guest_pthread_suspend: error codes; a thread created suspended stays parked; a spinner in guest code, a thread looping onsceKernelUsleep(200), and a thread that renames itself under its name lock while the caller reads that name during each suspend (40 suspend/resume rounds each, checking the target makes no progress while suspended and continues after resume); a thread blocked on a mutex stays suspended after the mutex is released and takes it only after resume; a cancel sent to a suspended thread takes effect only after resume; a spinner is suspended and resumed 40 times while another thread keeps creating and joining threads; a host thread that became a guest thread throughscePthreadSelf(as a title's main thread does) is suspended and resumed 40 times; such a thread makes a 2 ms host call and exits while it is being suspended, 200 times (also run 20 times under AddressSanitizer on Linux, no reports). About 3 s per run.b09a13358, merged ontomainfd9b3f62d. Windows 11 (WinLibs GCC 15.2.0, Release):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 pass, 73 of 73. Linux (Ubuntu 24.04 under WSL2, GCC 13.3.0, Release): the same set as registered there, 69 of 69.guest_pthread_suspendandguest_pthread_create_suspendedpassed 20 consecutive runs each on both platforms with the code ofb09a13358(ctest --repeat until-fail:20), and 200 each at039d7c7ad, before the 256-segment check. The check itself is defensive: no practical test loads 257 executable guest segments.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-17T08:52:23Z.
Review: COMMENTED
Reviewed — no blocking findings
Nothing blocks this; the points below are optional. The review read the diff of all 12 changed files at head
56e24d1b14647cdd7111772e2d7686827c521837, but no build or test was run and the full file text was not read.What I ran
56e24d1b14647cdd7111772e2d7686827c521837, basemain. No merged-preview check.diff.patch.Commands
Results
diff.patchfiles/Findings
Blocking
None.
Non-blocking
core/libs/prx/libkernel/System/src/Exception.cpp:1656(Windows only): the throw is reached only when the compare-exchange onpendingExceptionfails and the stored value is 0 or less. A new raise on a suspended thread takes the first branch and returns 0 without throwing. The Linux branch has no suspended check at all. So the claim thatsceKernelRaiseExceptionon a suspended thread throws does not match the code as read, unlesspendingExceptioncan hold a negative value, which I did not check. See the debt entry atdocs/dev/debt/libkernel_sys.md:391, which says it throws without a platform qualifier.core/libs/prx/libkernel/System/src/Exception.cpp:40-44: the new__linux__block adds<link.h>and<string_view>, which the existing__linux__block below already includes. Harmless, but a duplicate.Sources
github.com/boykopovar/AnyPS5@56e24d1b14/core/libs/prx/libkernel/System/src/Exception.cpp (L1653-L1660)github.com/boykopovar/AnyPS5@56e24d1b14/docs/dev/debt/libkernel_sys.md (L391)Not verified
files/was read.SuspendThread,GetThreadContext, the APC park, theosParkedhandoff withResumeThread): read as diff only, and the race reasoning was not checked against a run.SIGUSR2: the only production host use in the head isException.cpp, but I did not check whether guest signal delivery uses a hostSIGUSR2anywhere else. The head search matched a test inlibSceAgcDriverand a guest signal number inGuestSignalList.cpp.ParkCurrentGuestThread_nid_no_patchis visible fromTime.cpp(the include list was not read).To get this to a good state
Nothing is required.
Optional, once:
sceKernelRaiseExceptioncheck agree: state the throw is Windows-only and for the negative-pending case, or move the check ahead of the compare-exchange.<link.h>and<string_view>includes inException.cpp.Imported from boykopovar/AnyPS5. Originally posted by ligmaSec on 2026-12-03T16:20:31Z.
Review: COMMENTED
This head conflicts in the fault-delivery code in
Exception.cppand overlaps #21464 on the exportedpthread_suspend_npimplementation. Please coordinate those entry points and preserve the current signal-delivery setup when rebasing the narrower change.AI-assisted: yes.
View command line instructions
Checkout
From your project repository, check out a new branch and test the changes.Merge
Merge the changes and update on Forgejo.Warning: The "Autodetect manual merge" setting is not enabled for this repository, you will have to mark this pull request as manually merged afterwards.