feat(libkernel): implement thread cancellation #1141

Merged
nicolasabosch merged 2 commits from upstream-pr/1141 into main 2026-10-10 09:40:09 +00:00
nicolasabosch commented 2026-10-07 13:41:28 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by nicolasabosch on 2026-10-07T13:41:28Z.


What

scePthreadCancel and pthread_cancel threw, scePthreadTestcancel did nothing and the cleanup handler exports did not exist (recorded in TechnicalDebt as "Thread cancellation is not modelled"). This implements deferred cancellation after FreeBSD libthr (thr_cancel.c, thr_clean.c, thr_exit.c).

  • scePthreadCancel / pthread_cancel record the request on the thread and wake it when it is blocked in a cancellable wait. A null thread returns ESRCH.
  • A request is acted on at scePthreadTestcancel, the condition variable waits, the scePthreadSem and sem waits, scePthreadJoin and the sleeps, and under their POSIX names. A condition wait takes its mutex back before the thread exits, and a cancelled join leaves the target joinable.
  • The thread exits with PTHREAD_CANCELED (1) through scePthreadExit, which now runs the handlers of the new __pthread_cleanup_push_imp and __pthread_cleanup_pop_imp exports, newest first. The handler node uses the guest's _pthread_cleanup_info like libthr's struct pthread_cleanup.
  • scePthreadSetcancelstate and scePthreadSetcanceltype return EINVAL for unknown values and, like libthr, test for a pending request when the type is asynchronous. pthread_setcanceltype is added.
  • pthread_cancel moves from Unimplemented.cpp to Pthread/Posix/Thread.cpp with its real signature.

Limits, recorded in TechnicalDebt: a sleep is not interrupted (the thread exits when it ends, so the sceKernelUsleep path keeps its timing), file and socket calls are not cancellation points, and the asynchronous type acts like the deferred one except at the two set calls. The constants and the node layout are FreeBSD's, not measured on the console; the NIDs of the two cleanup exports come from their names and I have not seen a title import them.

#760 adds three lines to Pthread.hpp and Thread.cpp in other places; this does not depend on it.

Tested

Windows 11, WinLibs GCC 15.2.0: full build, libs target and ctest (387 passed; agc_driver_sdwa_float_selectors, shader_clock, vop1_float_unary, vop3_mad_i64 and vopc_compare excluded, they fail on this machine's Intel Arc GPU without this change). New guest_pthread_cancel test cancels threads blocked in a condition wait (while the canceller holds the mutex), scePthreadSemWait, sem_wait, scePthreadJoin and a sceKernelUsleep loop, checks the disabled state, the asynchronous type, the handler order and the error codes; it passed 40 runs in a row, and 15 with APS5_COARSE_TIMED_WAITS=1. Not tested on Linux and not tested with 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 in TechnicalDebt
  • 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: none
  • AI-assisted: yes
<!-- greyfinch-sync:boykopovar/AnyPS5:issue:4773511984 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/1141). Originally posted by [nicolasabosch](https://github.com/nicolasabosch) on 2026-10-07T13:41:28Z. --- ### What `scePthreadCancel` and `pthread_cancel` threw, `scePthreadTestcancel` did nothing and the cleanup handler exports did not exist (recorded in TechnicalDebt as "Thread cancellation is not modelled"). This implements deferred cancellation after FreeBSD libthr ([`thr_cancel.c`](https://github.com/freebsd/freebsd-src/blob/stable/11/lib/libthr/thread/thr_cancel.c), [`thr_clean.c`](https://github.com/freebsd/freebsd-src/blob/stable/11/lib/libthr/thread/thr_clean.c), [`thr_exit.c`](https://github.com/freebsd/freebsd-src/blob/stable/11/lib/libthr/thread/thr_exit.c)). - `scePthreadCancel` / `pthread_cancel` record the request on the thread and wake it when it is blocked in a cancellable wait. A null thread returns `ESRCH`. - A request is acted on at `scePthreadTestcancel`, the condition variable waits, the `scePthreadSem` and `sem` waits, `scePthreadJoin` and the sleeps, and under their POSIX names. A condition wait takes its mutex back before the thread exits, and a cancelled join leaves the target joinable. - The thread exits with `PTHREAD_CANCELED` (1) through `scePthreadExit`, which now runs the handlers of the new `__pthread_cleanup_push_imp` and `__pthread_cleanup_pop_imp` exports, newest first. The handler node uses the guest's `_pthread_cleanup_info` like libthr's `struct pthread_cleanup`. - `scePthreadSetcancelstate` and `scePthreadSetcanceltype` return `EINVAL` for unknown values and, like libthr, test for a pending request when the type is asynchronous. `pthread_setcanceltype` is added. - `pthread_cancel` moves from `Unimplemented.cpp` to `Pthread/Posix/Thread.cpp` with its real signature. Limits, recorded in TechnicalDebt: a sleep is not interrupted (the thread exits when it ends, so the `sceKernelUsleep` path keeps its timing), file and socket calls are not cancellation points, and the asynchronous type acts like the deferred one except at the two set calls. The constants and the node layout are FreeBSD's, not measured on the console; the NIDs of the two cleanup exports come from their names and I have not seen a title import them. #760 adds three lines to `Pthread.hpp` and `Thread.cpp` in other places; this does not depend on it. ### Tested Windows 11, WinLibs GCC 15.2.0: full build, `libs` target and `ctest` (387 passed; `agc_driver_sdwa_float_selectors`, `shader_clock`, `vop1_float_unary`, `vop3_mad_i64` and `vopc_compare` excluded, they fail on this machine's Intel Arc GPU without this change). New `guest_pthread_cancel` test cancels threads blocked in a condition wait (while the canceller holds the mutex), `scePthreadSemWait`, `sem_wait`, `scePthreadJoin` and a `sceKernelUsleep` loop, checks the disabled state, the asynchronous type, the handler order and the error codes; it passed 40 runs in a row, and 15 with `APS5_COARSE_TIMED_WAITS=1`. Not tested on Linux and not tested with a title. ### Checklist - [x] Based on current `main`; no other open PR implements the same functions - [x] One topic per PR; follow-ups go in a new PR - [x] No comments in code except [technical debt](https://github.com/boykopovar/AnyPS5/blob/main/docs/dev/CONVENTIONS.md) - [x] Unimplemented paths throw (`NotImplemented_nid_no_patch`); silent stubs are listed in [TechnicalDebt](https://github.com/boykopovar/AnyPS5/blob/main/docs/dev/TechnicalDebt.md#silent-stubs) - [x] No notes, investigation `.md` files or images added to the repository (attach them to this PR) - [x] New third-party code is a submodule built from source - [x] Generic behaviour, not specific to one title - [x] Depends on: none - [x] AI-assisted: yes
github-actions[bot] commented 2026-10-07 13:41:58 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by github-actions[bot] on 2026-10-07T13:41:58Z.


Progress report (948a526 → ef9d0dd)

📈 System libraries: 84.51% (+0.07%, +5 implemented functions)

✅ 5 implemented
Library Name
libkernel __pthread_cleanup_pop_imp_nid_postfix
libkernel __pthread_cleanup_push_imp_nid_postfix
libkernel pthread_cancel_nid_postfix
libkernel pthread_setcanceltype_nid_postfix
libkernel scePthreadCancel
<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6039228365 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/1141#issuecomment-6039228365). Originally posted by [github-actions[bot]](https://github.com/apps/github-actions) on 2026-10-07T13:41:58Z. --- <!-- progress-report --> ### Progress report (948a526 → ef9d0dd) 📈 **System libraries**: 84.51% (+0.07%, +5 implemented functions) <details> <summary>✅ 5 implemented</summary> | Library | Name | | - | - | | libkernel | `__pthread_cleanup_pop_imp_nid_postfix` | | libkernel | `__pthread_cleanup_push_imp_nid_postfix` | | libkernel | `pthread_cancel_nid_postfix` | | libkernel | `pthread_setcanceltype_nid_postfix` | | libkernel | `scePthreadCancel` | </details>
tv7 commented 2026-10-07 19:25:21 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by tv7 on 2026-10-07T19:25:21Z.


Data point: PPSA03306 (Silt, Unity) imports __pthread_cleanup_push_imp (1xvtUVx1-Sg). On main (cea1ef13), startup stops at FAIL: unresolved ELF import 1xvtUVx1-Sg from libkernel.prx. With this PR merged locally, the import resolves and startup continues to the next missing import (hypotf, which #1196 adds). Windows 11, GCC 15.2.0, RTX 3070. I only checked startup, not whether the cleanup handlers run.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6045207049 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/1141#issuecomment-6045207049). Originally posted by [tv7](https://github.com/tv7) on 2026-10-07T19:25:21Z. --- Data point: PPSA03306 (Silt, Unity) imports `__pthread_cleanup_push_imp` (`1xvtUVx1-Sg`). On `main` (`cea1ef13`), startup stops at `FAIL: unresolved ELF import 1xvtUVx1-Sg from libkernel.prx`. With this PR merged locally, the import resolves and startup continues to the next missing import (`hypotf`, which #1196 adds). Windows 11, GCC 15.2.0, RTX 3070. I only checked startup, not whether the cleanup handlers run.
oneandonlydean commented 2026-10-08 03:25:46 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by oneandonlydean on 2026-10-08T03:25:46Z.


One thing before this can go in: please drop the // Canonical lib is libScePosix ... comment at Pthread/Posix/Thread.cpp:87-89; code comments are only allowed as technical-debt markers.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6051539513 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/1141#issuecomment-6051539513). Originally posted by [oneandonlydean](https://github.com/oneandonlydean) on 2026-10-08T03:25:46Z. --- One thing before this can go in: please drop the `// Canonical lib is libScePosix ...` comment at `Pthread/Posix/Thread.cpp:87-89`; code comments are only allowed as technical-debt markers.
oneandonlydean commented 2026-10-09 20:14:29 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by oneandonlydean on 2026-10-09T20:14:29Z.


Conflicts with main in Pthread/src/Cond.cpp (the recursive-mutex cond wait change, 7406c975d2) and docs/dev/TechnicalDebt.md; please rebase.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6088506966 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/1141#issuecomment-6088506966). Originally posted by [oneandonlydean](https://github.com/oneandonlydean) on 2026-10-09T20:14:29Z. --- Conflicts with main in `Pthread/src/Cond.cpp` (the recursive-mutex cond wait change, 7406c975d2) and `docs/dev/TechnicalDebt.md`; please rebase.
adriadam10 commented 2026-10-10 09:27:58 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by adriadam10 on 2026-10-10T09:27:58Z.


Rebased onto current main as @oneandonlydean asked (conflicts in Pthread/src/Cond.cpp, Pthread/src/Thread.cpp and docs/dev/TechnicalDebt.md); the pthread_cancel comment was already dropped.

  • Cond.cpp: the recursive-mutex wait keeps main's unlock/relock of the extra levels around ThreadCancel::Wait.
  • Thread.cpp: the cancel state, cancel type, exiting flag and cleanup handler stack now live in GuestThreadState (main moved the thread state to WindowsThreadLocal) instead of separate thread_locals.
  • TechnicalDebt.md: the "Thread cancellation" line replaces the old one; the neighbouring lines are main's.

Rebuilt and ran the libkernel/libs tests on Linux: all green, guest_pthread_cancel passes 30 runs in a row.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6096121901 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/1141#issuecomment-6096121901). Originally posted by [adriadam10](https://github.com/adriadam10) on 2026-10-10T09:27:58Z. --- Rebased onto current `main` as @oneandonlydean asked (conflicts in `Pthread/src/Cond.cpp`, `Pthread/src/Thread.cpp` and `docs/dev/TechnicalDebt.md`); the `pthread_cancel` comment was already dropped. - `Cond.cpp`: the recursive-mutex wait keeps main's unlock/relock of the extra levels around `ThreadCancel::Wait`. - `Thread.cpp`: the cancel state, cancel type, exiting flag and cleanup handler stack now live in `GuestThreadState` (main moved the thread state to `WindowsThreadLocal`) instead of separate `thread_local`s. - `TechnicalDebt.md`: the "Thread cancellation" line replaces the old one; the neighbouring lines are main's. Rebuilt and ran the libkernel/libs tests on Linux: all green, `guest_pthread_cancel` passes 30 runs in a row.
adriadam10 commented 2026-10-10 09:33:58 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by adriadam10 on 2026-10-10T09:33:58Z.


Rebased again onto current main (@oneandonlydean asked for a rebase): the only conflict was Unimplemented.cpp, where the old pthread_cancel stub and its comment are now removed since the real export lives in Pthread/Posix/Thread.cpp.

Rebuilt and ran the libkernel/libs tests on Linux: all green.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6096167192 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/1141#issuecomment-6096167192). Originally posted by [adriadam10](https://github.com/adriadam10) on 2026-10-10T09:33:58Z. --- Rebased again onto current `main` (@oneandonlydean asked for a rebase): the only conflict was `Unimplemented.cpp`, where the old `pthread_cancel` stub and its comment are now removed since the real export lives in `Pthread/Posix/Thread.cpp`. Rebuilt and ran the libkernel/libs tests on Linux: all green.
github-actions[bot] commented 2026-10-10 09:36:39 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by github-actions[bot] on 2026-10-10T09:36:39Z.


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
#2478 docs/dev/TechnicalDebt.md L325
<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6096187126 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/1141#issuecomment-6096187126). Originally posted by [github-actions[bot]](https://github.com/apps/github-actions) on 2026-10-10T09:36:39Z. --- <!-- pr-overlap --> 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 | |---|---|---|---| | #2478 | `docs/dev/TechnicalDebt.md` [L325](https://github.com/boykopovar/AnyPS5/blob/663cc42c8513f050176ceb50c2dd1442e60b234c/docs/dev/TechnicalDebt.md#L325) | | |
Sign in to join this conversation.
No reviewers
No labels
No milestone
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Reference
boykopovar/AnyPS5!1141
No description provided.