feat(libkernel_sys): create threads suspended and resume them #9573
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/9573"
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 zaryabzip on 2026-10-22T02:47:44Z.
What
Implements
pthread_attr_setcreatesuspend_np,pthread_resume_np,pthread_resume_all_np,pthread_multi_np,scePthreadAttrSetcreatesuspend,scePthreadResume,scePthreadResumeAllandscePthreadMulti(libkernel_sys), following FreeBSD 11.4 libthr at 8c639945.Suspended start. A thread created with the create-suspended attribute waits in
RunThreadbefore its start routine. That is after it is registered and before the POSIX signal mask is restored.attr.suspend(thr_create.c#L127-L132)._thr_ast, which runscheck_suspendbeforecheck_cancel(thr_sig.c#L265-L274, thr_sig.c#L351-L399). It gets there fromthread_start, before the mask restore and the start routine (thr_create.c#L252-L294).pthread_attr_setcreatesuspend_npreturnsEINVALfor a nullattror*attr, otherwise 0 (thr_attr.c#L369-L381).scePthreadAttrSetcreatesuspendreturns0x80020000 | errno.GZSR0Ooae9QasORBIS(posix_pthread_attr_setcreatesuspend_np)(pthread_attr.cpp#L399-L400).ORBISaddsORBIS_KERNEL_ERROR_UNKNOWN(0x80020000) to a non-zero result (kernel.h#L26-L39, orbis_error.h#L9).pthread_resume_npreturnsEINVALfor a null thread,ESRCHfor a thread that is gone or has finished, and 0 otherwise (thr_resume_np.c#L47-L60, thr_list.c#L339-L363).pthread_resume_all_npreturnsvoid. It resumes every thread except the caller, then reaches a cancellation point (thr_resume_np.c#L62-L97).pthread_multi_npresumes all threads and returns 0 (thr_multi_np.c#L40-L51).Locking. Resume clears the flag and wakes the thread while it holds the guest-thread registry lock. A detached thread that exits at the same moment therefore cannot be freed during the wake.
Attribute getters.
scePthreadAttrGetandpthread_attr_get_npnow report the create-suspended attribute the thread was created with. libthr keepsattr.suspendin the thread and copies it with the other attributes (thr_create.c#L84-L88, thr_attr.c#L139-L172, thr_private.h#L263-L277).Raise on Windows.
sceKernelRaiseExceptionthrows for a thread still suspended at creation.SIGTRAPblocked (thr_create.c#L177-L198) and restores its mask only after resume, so the signal waits.Test. New test
guest_pthread_create_suspended(TIMEOUT 20) covers:pthread_multi_npandpthread_resume_all_np;pthread_resume_all_npas a cancellation point;scePthreadResume,scePthreadMultiandscePthreadResumeAllfollow the code of firmware 13.42'slibkernel_sys.sprx, which BunnyFl read and ran in an emulator (comment):scePthreadResume(thread)callspthread_resume_npand returns0x80020000plus a non-zero errno (0x80020016for a null thread,0x80020003for one that is gone or finished).scePthreadMulti()callspthread_multi_npthe same way, so it always returns 0.scePthreadResumeAllis a jump topthread_resume_all_npand returns nothing.scePthreadAttrSetcreatesuspenderror form and the return values of the four POSIX calls above.Inferred or unverified:
docs/dev/debt/libkernel_sys.mdand, for the Windows raise, indocs/dev/debt/libkernel.md.pthread_suspend_np,pthread_suspend_all_npandpthread_single_npstill throw.Overlap: no open PR implements any of these functions (searched the diffs of all 1175 other open PRs).
Merge order: #9156 goes first, and this PR depends on it.
core/libs/prx/libkernel_sys/Export.Discovery.cppat neighbouring lines. #9156 deletes thepthread_attr_getaffinity_np,pthread_attr_getsolosched_npandpthread_attr_setaffinity_npstubs (L361-375 on main). This PR changes the line right after them (L376), thepthread_attr_setcreatesuspend_npsignature, which has to takeattr. Git treats edits on adjacent lines as one conflict, so moving this PR's edit cannot avoid it.mainconflicts with this one.Tested
At
0b60e97834, on maine7e3e9c198, after rebasing over main's per-libraryApi.hppheaders and adding the three sce forms.libkernel_sys/include/Api.hppnow declares these eight functions with their signatures, and the test includes theApi.hppheaders instead of declaring the exports itself. The thread logic is unchanged.x86_64-ucrt-posix-sehr7), CMake 4.3.1, Ninja 1.13.2, Release,BUILD_TESTING=ON:libkernel_sysand the 8 test targets build with no warnings in the changed files. ctest100% tests passed, 0 tests failed out of 8(guest_kernel_sys,guest_pthread_create_suspended,guest_pthread_attr,guest_pthread_start,guest_pthread_join_detach,guest_pthread_cancel,guest_raise_exception,guest_raise_in_host_code).guest_pthread_create_suspendedwith--repeat until-fail:3: 3 of 3 (0.61 to 0.64 s).5949d8bf92as the Linux CI job does:libkernel_sysfully rebuilt with no warnings in the changed files. ctest100% tests passed, 0 tests failed out of 7. The repeat passes 3 of 3 (0.49 to 0.50 s). All 14 executables built from files that includelibkernel_sys/include/Api.hppalso build and link.Earlier runs:
At 6cad46b612, on main
524398151f:x86_64-ucrt-posix-seh,15.2.0posix-14.0.0-ucrt-r7), CMake 4.3.1, Ninja 1.13.2, Release,BUILD_TESTING=ON:mainit stops at the unrelatedlibSceAgcDriver/tests/execution/ImageArrayWindowPastLayerLimit.cpp:33(alignas(65536)exceeds the 8192 limit of Windows object files).100% tests passed, 0 tests failed out of 8):guest_kernel_sys0.14 s,guest_pthread_create_suspended0.48 s,guest_pthread_attr0.55 s,guest_pthread_start1.57 s,guest_pthread_join_detach0.13 s,guest_pthread_cancel0.33 s,guest_raise_exception12.10 s (about 11.4 s on main) andguest_raise_in_host_code0.06 s.guest_pthread_create_suspendedwith--repeat until-fail:3: passed 3 of 3 (0.44 to 0.45 s).BUILD_TESTING=ON, with this PR merged into main524398151fas the Linux CI job does:100% tests passed, 0 tests failed out of 7):guest_kernel_sys0.01 s,guest_pthread_create_suspended0.39 s,guest_pthread_attr0.04 s,guest_pthread_start1.80 s,guest_pthread_join_detach0.06 s,guest_pthread_cancel0.17 s andguest_raise_exception2.66 s.guest_raise_in_host_codeis registered only on Windows.guest_pthread_create_suspendedwith--repeat until-fail:3: passed 3 of 3 (0.39 s each).ef0d8ae3b5, with the code unchanged; only thedocs/dev/debt/libkernel_sys.mdentry was re-merged next to new entries frommain. The runs above were not repeated on that head.6ef5a1c4cwith only the new test file and its CMake block, the test aborts withpthread_attr_setcreatesuspend_np_nid_postfix not implementedon both hosts.tools/pr_overlap.py --branch feat/pthread-create-suspended --base main(main524398151f): no shared exports or new files. Its only conflict is the one with #9156 above.Run earlier on an arm64 Mac without Rosetta, which can only compile:
a6dcea9279, repeated against main110a18ba01after the rebase onto it and the include move (the later rebase onto524398151fchanges none of these files):-fsyntax-only -Wall -Wextraand-c -O0, onPthread/src/Thread.cpp,System/src/Exception.cpp,libkernel_sys/Export.Discovery.cpp,Pthread/src/Attr.cpp,Pthread/Posix/Attr.cpp,Pthread/Posix/Thread.cppand the new test: no new diagnostics.clang -target x86_64-apple-macos13 -fsyntax-onlyon the same files: the same diagnostics as main.Thread.cppandException.cppalready fail on macOS (missingpthread_getattr_np,pthread_getcpuclockidand the Linuxgregs), so the Linux path was not compiled.tools/check_conventions.py --base origin/main: passed.tools/progress.py: onmaine7e3e9c198, library functions go from 6054 to 6062 of 12288, and libkernel_sys from 49 to 57. These are the 8 functions above.git merge-tree: clean against main110a18ba01and524398151f. Against the heads of all 1175 other open PRs, the only new conflict is the one with #9156 above.No title or console was used.
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)Imported from boykopovar/AnyPS5. Originally posted by zaryabzip on 2026-10-22T03:27:31Z.
Test results for e4ecf3025, on x86-64 (i7-11800H). The Tested section is updated with the same results.
Windows 11 25H2, WinLibs MinGW-w64 GCC 15.2.0 UCRT r7, Release,
BUILD_TESTING=ON:guest_pthread_create_suspended,guest_pthread_start,guest_pthread_join_detach,guest_pthread_attr,guest_pthread_cancel,guest_kernel_sys,guest_raise_exceptionandguest_raise_in_host_code.guest_pthread_create_suspendedwith--repeat until-fail:3passes 3 of 3, in about 0.44 s each.libSceAgcDriver/tests/execution/ImageArrayWindowPastLayerLimit.cpp:33(alignas(65536)exceeds the 8192 limit). Main fails there too, and this PR doesn't touch that file; #8310 and #9150 address it.Ubuntu 24.04.5 on WSL2, GCC 13.3.0, Release,
BUILD_TESTING=ON, with the PR merged into main6ef5a1c4cthe way the Linux CI job does:guest_raise_in_host_codeis registered only on Windows.--repeat until-fail:3passes 3 of 3, in 0.42 s each.Baseline: on main with only the new test and its CMake block, the test aborts with
pthread_attr_setcreatesuspend_np_nid_postfix not implemented, on both hosts.Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-22T03:52:47Z.
Review: CHANGES_REQUESTED
The implementation and test report are unusually thorough, including Linux/Windows runs, cancellation and detached-thread cases, and the explicit limits on console/ABI evidence. I did not find a substantiated runtime defect in the code reviewed.
I am requesting changes for the repository's overlap rule. The automated overlap report names open PRs #8987, #9141, #9369 and #9156 in
core/libs/prx/libkernel_sys/Export.Discovery.cpp. The PR body calls these expected textual conflicts, but it does not record coordination with those authors or an agreed merge order. In particular, #9156 touches the pthread attribute stubs around thepthread_attr_setcreatesuspend_npinsertion; #8987, #9141 and #9369 share the file's include insertion. Please coordinate with the affected authors and updateDepends onto list any agreed prerequisites (including transitive prerequisites that are still open). A clean merge-tree against currentmaindoes not establish the ordering between these concurrently open changes.Hosted eligibility, intake, labels, report and conventions checks pass. Hosted Linux, Windows, relinker and sanitizer jobs are skipped; I relied on the author-reported Windows/WSL test results and did not treat those hosted jobs as executed. The unverified console behavior and PS4-derived error encoding are clearly disclosed in the description and debt entry.
Imported from boykopovar/AnyPS5. Originally posted by zaryabzip on 2026-10-22T08:04:33Z.
@MarcZX10 Thanks for the review. I rebased onto main
524398151fand updated the PR:Export.Discovery.cppnow sit above the existing ones, so they no longer meet the blocks #8987, #9141 and #9369 add below them. That is the only code change.tools/pr_overlap.pynow shows no conflict with any open PR except #9156.pthread_attr_setcreatesuspend_npsignature on the very next line, L376. I proposed on #9156 that it goes first, andDepends onnow reads#9156. #9156 has no prerequisites of its own.guest_pthread_create_suspendedpasses 3 of 3 repeats on both. The Tested section has the details.Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-22T10:58:23Z.
Review: CHANGES_REQUESTED
Re-reviewed head
6cad46b6128b7d65f2c905b1f0a55fa90aeaef8c. The earlier code concerns appear resolved: #9156 is explicitly declared as a prerequisite, and DavidSamir agreed that it lands first. The include reorder also removes the previously flagged overlaps with #8987, #9141 and #9369. I found no new implementation defect in the reviewed delta. I builtguest_pthread_create_suspended_testslocally. CTest aborts inHostCpubefore assertions because this Xeon E5-1620 v2 lacks LZCNT, BMI1/2, MOVBE, AVX2 and FMA. The Windows and WSL focused runs and repeats are author-reported; current hosted eligibility, conventions, labels and report checks pass, with product/relinker jobs skipped. Current-basediff --checkand conventions pass. Please refresh from currentmain:check_main_sync.pyreports 1 ahead / 66 behind (merge base524398151f), despite a clean merge. I also reranpr_overlap.pyagainst current main. #9156 is the only comparable conflict and its authors have agreed to the stated order. The scan cannot compare several shared files because those PRs conflict with main. I manually confirmed relevant interactions to preserve during the rebase: #3986 adds inherited guest-signal-mask setup inRunThreadnear the new suspended wait; #4563 adds Windows detached-thread reaping around theRunThreadexit/ReleaseThreadpath, which intersects this PR's detached-suspended-thread case; #8592 adds thread signal-deferral binding and exception handling relevant to this PR's raised-exception case; #6044 reorganizescore/libs/CMakeLists.txtand the test tree. Please coordinate with the affected authors as needed, retain the new tests/registration in the relocated tree if #6044 lands first, and add a dependency only where the agreed sequence requires one.I’ve noticed that holding this PR because of #5239 isn’t necessary. It’s fine for PRs to keep coming in; they’ll be handled in due course. Please disregard my earlier request to hold or defer it.
Imported from boykopovar/AnyPS5. Originally posted by zaryabzip on 2026-10-22T12:31:19Z.
@MarcZX10 Thanks for the re-review.
8b3a6ba88d(head 0bab52b5e6). The patch is unchanged (samegit patch-id), andcheck_main_sync.pyandcheck_conventions.pypass. The Windows and WSL2 runs in Tested are from 6cad46b612, one rebase earlier, and the description says so.Unblocks:line if a title turns up.maintoday, so none of them is a merge-order prerequisite. Whichever lands second carries the other's change. If #6044 lands first,guest_pthread_create_suspendedmoves into its layout. If #3986 lands first, its guest signal-masksigprocmaskcall inRunThreadmoves after the suspended wait, because libthr restores a suspended thread's signal mask only after it is resumed (thread_start, thr_create.c L252-294). #8592'sBindThreadSignalDeferralStatestays with the other per-thread bindings ahead of the wait. If #4563 lands first, the rebase keeps resume clearing and waking a thread under the registry lock, ahead of its Windows reaping, so a detached suspended thread cannot be freed during the wake.ResumesDetachedThreadsWhileTheyExitchecks this.Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-22T14:14:38Z.
Review: DISMISSED
Re-reviewed exact current head
0bab52b5e6a907850db4e0e204d77ac28cfb9b38. I found no new behavior defect. The test covers suspended start, resume of self/other threads, cancellation timing, attribute getters, detached exit races and the platform-specific raise path. The CTest timeout is 20 seconds, within the repository limit. The #9156 prerequisite order is explicitly agreed, and the integration notes for #3986/#4563/#8592/#6044 identify the state/tests to preserve without creating unnecessary dependency edges.Correction to my earlier review and your latest comment: please disregard the intake hold and title-unblock request. Your contribution-count eligibility means this PR does not need to wait for the old #5239 queue direction.
The remaining request is current-base validation. This head is based on
8b3a6ba88d8fc74e768f1a7e8e9e36fb69842768, 39 commits behind current upstream8b3e39832af85f404c6195418d11b3e1ebd1924d;git diff --check, current-main conventions and merge-tree pass. The Windows/WSL focused runs and 3/3 repeats are attributed by the author to6cad46b, before the latest rebase. I ran GCC 12.2 C++20-Wall -Wextra -Werror -fsyntax-onlyonGuestPthreadCreateSuspended.cppand the test registration audit (171 scripts); both pass. I did not rerun local runtime tests here; the earlier exact target build was blocked at execution by this host's unsupported CPU features before assertions. Please update to current main and rerun the focused Windows/Linux matrix, including the repeat test, after #9156 lands.