Skip to content

feat(libkernel_sys): implement pthread_suspend_np - #12130

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

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

Conversation

@2Swagger

@2Swagger 2Swagger commented Oct 27, 2026 •

Copy link
Copy Markdown
Contributor

What

Implements pthread_suspend_np in libkernel_sys, following FreeBSD 11.4 libthr (thr_suspend_np.c): EINVAL for a null thread, ESRCH for a finished one, EDEADLK for the caller, otherwise 0 once the target is suspended. It builds on the create-suspended and resume side from #9573, so pthread_resume_np, pthread_resume_all_np and pthread_multi_np resume 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):

  • In guest code: on Windows the target is frozen with SuspendThread and stays frozen until resumed; on Linux a SIGUSR2 handler parks it when the interrupted address is in the main executable or a *.guest.prx (code ranges collected with dl_iterate_phdr outside the handler).
  • In a libkernel wait: on Windows an APC queued into the alertable wait parks it; on Linux the signal handler parks it inside the TimedWait delivery scope, and a new TimedWait::SuspendSafeWait scope marks the Linux mutex waits in Mutex.cpp the same way.
  • When a guest sleep returns: sceKernelUsleep, nanosleep, usleep and sceKernelSleep (all through CancellableSleep) park a thread with a pending suspend.
  • Anywhere else (host code): the caller samples the target again until it reaches one of these points, yielding between attempts and sleeping 1 ms every 256 so that the caller itself stays suspendable.

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_create and the first suspend.

docs/dev/debt/libkernel_sys.md gets an entry for the gaps: a thread that spends nearly all its time in host calls is suspended late (with scePthreadRename called 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, and sceKernelRaiseException on a suspended thread throws, as for one created suspended. pthread_suspend_all_np, pthread_single_np and the sce forms still throw.

Tested

  • New guest_pthread_suspend: error codes; a thread created suspended stays parked; a spinner in guest code, a thread looping on sceKernelUsleep(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 through scePthreadSelf (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.
  • Head b09a133, merged onto main fd9b3f6. 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_code and public_headers with 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_suspend and guest_pthread_create_suspended passed 20 consecutive runs each on both platforms with the code of b09a133 (ctest --repeat until-fail:20), and 200 each at 039d7c7, before the 256-segment check. The check itself is defensive: no practical test loads 257 executable guest segments.
  • 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: fix(libkernel): return ESRCH from join, detach and cancel of a released thread handle #10817
  • Fixes: none
  • Unblocks: none
  • GPUs tested: n/a
  • AI-assisted: yes

🤖 Generated with Claude Code

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

github-actions Bot commented Oct 27, 2026 •

Copy link
Copy Markdown

Progress report (9d2e737 → 56e24d1)

📈 System libraries: 34.78% (+0.0026%, +1 implemented functions)

✅ 1 implemented
Library Name
libkernel_sys pthread_suspend_np_nid_postfix

Changed libraries (implemented exports)

Library Before After Change
libkernel_sys 164/820 (20.00%) 165/820 (20.12%) +0.12%

@oneandonlydean

Copy link
Copy Markdown
Collaborator

@2Swagger thanks, the safe-point design reads well: the epoch loop that re-checks suspended, the started flag and the osParked exchange on Windows all hold up. There's one blocker and one CI fix:

  1. Windows: InGuestCode can deadlock the caller on the loader lock. RequestGuestThreadPark calls InGuestCode while the target is frozen by SuspendThread. On a cache miss, InGuestCode calls ClassifyImage (Exception.cpp:373-391), and that calls GetModuleFileNameW, which takes the loader lock. If the target was frozen while holding it (thread start or exit, LoadLibrary, GetProcAddress), the caller waits on the lock forever and the target is never resumed. When 256 images are already cached, every lookup of an unseen image misses, so the risk repeats. The exception delivery path in the same file avoids this on purpose. While the thread is suspended it uses only RunningCode (Exception.cpp:393-398 at this head, cache only), and it calls ClassifyImage only after ResumeThread (Exception.cpp:749-756). Could InGuestCode follow that pattern: RunningCode while frozen, and on Unknown resume, classify, then retry?
  2. Conventions: the new debt entry's first link is core/libs/prx/libkernel/System/src/Exception.cpp, so the check requires the entry in docs/dev/debt/libkernel.md. Alternatively, link core/libs/prx/libkernel_sys/Export.Discovery.cpp first, as the neighbouring pthread_resume_np entry does, and keep it in libkernel_sys.md.

Small wording point for the same entry: it says a target blocked in a host call is suspended only after the call returns. Please add that pthread_suspend_np doesn't return until then, so the caller waits as long as the call does.

@github-actions

github-actions Bot commented Oct 27, 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_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 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 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 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 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 27801 core/libs/prx/libkernel/System/src/Exception.cpp
PR 27864 core/libs/prx/libkernel/Pthread/src/Thread.cpp
PR 27988 core/libs/prx/libkernel/System/src/Exception.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 28253 core/libs/prx/libkernel/Pthread/src/Thread.cpp
PR 28255 core/libs/prx/libkernel/Pthread/src/Thread.cpp
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 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
PR 28960 core/libs/prx/libkernel/Time/Time.cpp
PR 28962 core/libs/prx/libkernel/Time/Time.cpp
PR 28964 core/libs/prx/libkernel/Time/Time.cpp
PR 29159 core/libs/CMakeLists.txt
PR 29168 core/libs/prx/libkernel/Pthread/src/Mutex.cpp
PR 29213 core/libs/CMakeLists.txt
PR 29246 core/libs/prx/libkernel/Pthread/src/Thread.cpp
PR 29260 core/libs/prx/libkernel/Time/Time.cpp

@2Swagger

Copy link
Copy Markdown
Contributor Author

@oneandonlydean thanks for the careful review. All three points are addressed:

  1. Loader lock (1126f64): applied as you suggested. InGuestCode is now FrozenCode: while the target is frozen it uses only the cache lookup RunningCode. On Unknown, the target is resumed first, then ClassifyImage runs, then the loop retries, the same order TryDeliver uses. When the cache is full, RunningCode reports unseen images as host code, so the loop always ends.

    I couldn't reproduce the blocking on Windows 11 26200. I froze a thread inside the DllMain of 30 freshly loaded module copies, so it was holding the loader lock with its RIP in an image not yet classified. Each time, GetModuleFileNameW on that image returned in 1-2 us from a thread that was already running. Because of that, I have no test that fails without the fix. I'm keeping the change as a defensive one, since other Windows versions may take the loader lock there. I also added a case to guest_pthread_suspend that suspends and resumes a spinning thread 40 times while another thread keeps creating and joining threads.

  2. Conventions (37e253b): the entry now links libkernel_sys/Export.Discovery.cpp first and stays in libkernel_sys.md. The check is green.

  3. Wording (1126f64): the entry now says pthread_suspend_np does not return until the blocked host call returns, so the caller waits as long as the call does.

Tested at 1126f64, merged onto main cccdaa2:

  • Windows 11 (WinLibs GCC 15.2.0): 73 of 73 passed.
  • Linux (Ubuntu 24.04 under WSL2, GCC 13.3.0): 69 of 69 passed.
  • guest_pthread_suspend and guest_pthread_create_suspended: 200 consecutive runs each on both platforms, all passed.

I also added #10817 and #11441 to Depends on. Both are older, and git merge-tree shows conflicts with them in Thread.cpp and in the debt file.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@2Swagger

Copy link
Copy Markdown
Contributor Author

One more fix, found while planning pthread_suspend_all_np (7831f0e):

A title's main thread isn't created with pthread_create. It becomes a guest thread when it first calls scePthreadSelf, which adopts it. Adoption never set the started flag, so pthread_suspend_np never asked an adopted thread to park, and waited until something resumed it. Adoption now sets started on both platforms: the thread is already running by the time anyone can get its handle.

New case in guest_pthread_suspend: a host thread adopts itself through scePthreadSelf and spins, and it is suspended and resumed 40 times.

  • Without the fix, the test hung until it was killed after 30 s.
  • With the fix, it passed, about 3.2 s per run.

Tested at 7831f0e, merged onto main cccdaa2:

  • Windows 11 (WinLibs GCC 15.2.0): 73 of 73 passed.
  • Linux (Ubuntu 24.04 under WSL2, GCC 13.3.0): 69 of 69 passed.
  • guest_pthread_suspend and guest_pthread_create_suspended: 200 consecutive runs each on both platforms, all passed.

@oneandonlydean

Copy link
Copy Markdown
Collaborator

@2Swagger the loader-lock fix reads right to me: FrozenCode only uses VirtualQuery and the cached image kinds while the target is frozen, and ClassifyImage runs after ResumeThread. One small robustness point on Linux, not blocking: the retry loop in RequestGuestThreadPark stops only when the target parks, the suspend is withdrawn, or pthread_kill returns ESRCH. It doesn't check that the target is still live, as the Windows loop does with Exited(). For an adopted thread, SuspendGuestThread also holds no reference while it polls, and that PthreadPrivate is freed when the host thread exits. So a target that exits in host code while it is being suspended can be read after it is freed, or signalled after its pthread_t is gone. Could you check GuestThreadIsLive in that loop (and send the signal while holding the live-thread lock)?

2Swagger and others added 3 commits October 28, 2026 15:52
…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>
@2Swagger

Copy link
Copy Markdown
Contributor Author

@oneandonlydean thanks, you were right. AddressSanitizer confirmed it, and it's fixed.

Fix (a7fb496):

  • On Linux, each pass of the retry loop in RequestGuestThreadPark now takes the live-thread lock first. It stops if the target is gone, and otherwise reads parked/suspended and calls pthread_kill while still holding the lock, so the record can't be freed during the signal.
  • The wait loop in SuspendGuestThread_nid_no_patch now checks liveness under the same lock before reading the target. started is read under the lock too.
  • An adopted target is no longer passed to ReleaseThread, which used to read _adopted after the lock was released.
  • RequestGuestThreadPark now reports a target that exited, so the wait ends. This also matters on Windows, where an adopted thread stays in the thread list after it exits.

Also fixed:

  • Windows: SuspendThread fails on a thread that is already terminating but not yet signalled, and pthread_suspend_np threw "cannot suspend the target thread". It now waits up to 1 s for the exit and treats the thread as gone. Any other failure still throws, with the Windows error code.
  • Linux (039d7c7): with the lock taken on every attempt, an exiting target couldn't get the lock to remove itself from the thread list. The new case took 12-20 s. The loop now pauses 20 us between signals instead of only yielding, and the test is back to about 3 s.

New case in guest_pthread_suspend: an adopted host thread makes a 2 ms host call and exits while it is being suspended, 200 times.

  • Linux with AddressSanitizer (-DANYPS5_ENABLE_ASAN=ON, GCC 13.3.0): before the fix, the first run reported a heap-use-after-free in RequestGuestThreadPark's retry loop. After the fix, 20 of 20 runs passed with no reports.
  • Windows: before the fix, 3 of 3 runs aborted with the "cannot suspend the target thread" error. After the fix, it passes.

Tested at 039d7c7, merged onto main 319621b:

  • Windows 11 (WinLibs GCC 15.2.0): 73 of 73 passed. public_headers_compile_6 timed out once at 30 s while running next to 9 other tests; all 65 header checks pass at --parallel 4.
  • Linux (Ubuntu 24.04 under WSL2, GCC 13.3.0): 69 of 69 passed.
  • guest_pthread_suspend and guest_pthread_create_suspended: 200 consecutive runs each on both platforms, all passed.

#11441 has merged, so Depends on is now just #10817.

@github-actions github-actions Bot removed the needs rebase Conflicts with main, rebase needed label Nov 9, 2026
@gabichuuuuu

Copy link
Copy Markdown
Contributor

Windows check of head 56e24d1b14, merged onto main e37e9fa3f9 (clean merge, built together with other open PRs that touch different files).

Setup: Windows 11 Pro 10.0.26300, WinLibs GCC 15.2.0 (r7), Ninja, Release, BUILD_TESTING=ON, Python 3.15.

  • Tests chosen with tools/select_tests.py explain from this PR's test files: guest_pthread_suspend.
  • The new guest_pthread_suspend test timed out at 30 s in 2 of 3 runs and passed in the third, so on Windows it is intermittent.
  • No negative control was run in this pass, so this does not show that the tests fail without the change.

AI-assisted: yes.

@Kal0s-design

Copy link
Copy Markdown

Linux and Windows check of head 56e24d1b14, merged onto main 4c0998e1a8 (clean merge, nothing else merged in).

Linux 6.18, GCC 16, Release. guest_pthread_suspend, 30 runs per build:

Build Passed Slowest run
plain 30 of 30 3.5 s
ASan + UBSan 30 of 30 3.0 s
TSan 30 of 30, no report 9.1 s

Windows, WinLibs GCC 15, Release. The 9 Nov check above saw the test time out at 30 s in 2 of 3 runs, in a build that also carried other open PRs. With only this PR on main I could not reproduce that:

Machine state Passed Run time
idle 15 of 15 6.45 s to 6.54 s
every core kept busy by a spinning process 10 of 10 8.0 s to 10.8 s

No run came near the 30 s limit, so the earlier timeouts look tied to that combined build or that machine, not to this head on its own.

Not covered: macOS, the console, and the stacked #12642.

AI-assisted: yes.

@github-actions github-actions Bot added review queue Next in the maintainer review queue: checks pass, no open change request, no conflict and removed review queue Next in the maintainer review queue: checks pass, no open change request, no conflict labels Nov 16, 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 — 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

  • Head 56e24d1b14647cdd7111772e2d7686827c521837, base main. No merged-preview check.
  • Read the change description, commit list, manifest, exclusion list and diff.patch.
  • Read the house rules sections for readable reviews, the standard response format and accuracy and evidence.
  • Read head context with the repository reader:
Commands
python3 repo_read.py cat core/libs/prx/libkernel/System/src/Exception.cpp 1640 1672
python3 repo_read.py cat core/libs/prx/libkernel/Time/TimedWait.cpp 336 352
python3 repo_read.py cat core/libs/prx/libkernel/Time/TimedWait.cpp 484 494
python3 repo_read.py search SIGUSR2
python3 repo_read.py search DeliverDeferred
python3 repo_read.py search SleepNanos

Results

Check Result Note
Diff of 12 files passed Read from diff.patch
Full file text not run Nothing read from files/
Build and tests not run Builds are out of scope for this review
Author's test runs not applicable Reported in the description, not reproduced

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 on pendingException fails 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 that sceKernelRaiseException on a suspended thread throws does not match the code as read, unless pendingException can hold a negative value, which I did not check. See the debt entry at docs/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

  • Exception check, head:
    if (thread->suspended.load(std::memory_order_acquire)) {
    int pending = 0;
    if (thread->pendingException.compare_exchange_strong(pending, signum, std::memory_order_acq_rel) || pending > 0) return 0;
    if (thread->suspended.load(std::memory_order_seq_cst)) throw std::runtime_error("sceKernelRaiseException: the target thread is suspended");
    }
    return RaiseOn(thread, handler, signum) ? 0 : SCE_KERNEL_ERROR_ESRCH;
    #else
    return RaiseOn(thread) ? 0 : SCE_KERNEL_ERROR_ESRCH;
  • Debt entry, head:
    - [pthread_suspend_np](../../../core/libs/prx/libkernel_sys/Export.Discovery.cpp) (libkernel_sys) - a running target is suspended only where it holds no host lock: in guest code, in a libkernel wait (on Linux also a mutex wait), or when `sceKernelUsleep`, `nanosleep`, `usleep` or `sceKernelSleep` returns. One mostly in host calls is suspended late: with `scePthreadRename` called back to back, Windows 11 took a median of 160-205 ms and at worst 1.3 s per suspend, Linux at worst 3 ms. A target blocked in a host call outside those waits is suspended only when the call returns, and `pthread_suspend_np` waits that long; FreeBSD 11.4 libthr ([`thr_suspend_np.c`](https://github.com/freebsd/freebsd-src/blob/8c639945/lib/libthr/thread/thr_suspend_np.c)) suspends it at once. Return values follow that file. `sceKernelRaiseException` on a thread suspended after it started throws. On Linux, suspending throws while more than 256 executable guest segments are loaded. Console behaviour is unmeasured

Not verified

  • Full text of all 12 files, read only as diff. No file under files/ was read.
  • The Windows park path (SuspendThread, GetThreadContext, the APC park, the osParked handoff with ResumeThread): read as diff only, and the race reasoning was not checked against a run.
  • Linux SIGUSR2: the only production host use in the head is Exception.cpp, but I did not check whether guest signal delivery uses a host SIGUSR2 anywhere else. The head search matched a test in libSceAgcDriver and a guest signal number in GuestSignalList.cpp.
  • Whether ParkCurrentGuestThread_nid_no_patch is visible from Time.cpp (the include list was not read).
  • Console behaviour is unmeasured, as the description says. The author's Windows and Linux test runs were not reproduced here.

To get this to a good state

Nothing is required.

Optional, once:

  • Make the debt entry and the sceKernelRaiseException check agree: state the throw is Windows-only and for the negative-pending case, or move the check ahead of the compare-exchange.
  • Remove the duplicate <link.h> and <string_view> includes in Exception.cpp.

@ycbuzi

ycbuzi commented Nov 17, 2026

Copy link
Copy Markdown
Contributor

Tested 56e24d1b14 merged into main, with the whole ctest suite on each machine below. Nothing fails that does not also fail on main on the same machine.

  • Windows Server 2022 (GitHub-hosted runner), full build, WinLibs GCC 15.2.0: 99% tests passed, 3 tests failed out of 6105; 99% tests passed, 4 tests failed out of 6105. 3 of the failing tests fail on main too. run
  • Windows Server 2025 (GitHub-hosted runner), full build, WinLibs GCC 15.2.0: 100% tests passed, 0 tests failed out of 6105; 100% tests passed, 0 tests failed out of 6105. run

AI-assisted: yes (Claude Code); the runs are automated, this summary was written from their reports.

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

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

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

This head conflicts in the fault-delivery code in Exception.cpp and overlaps #21464 on the exported pthread_suspend_np implementation. Please coordinate those entry points and preserve the current signal-delivery setup when rebasing the narrower change.

AI-assisted: yes.

This branch has not been deployed

No deployments
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.

9 participants