feat(libkernel): implement thread cancellation #1141
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/1141"
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 nicolasabosch on 2026-10-07T13:41:28Z.
What
scePthreadCancelandpthread_cancelthrew,scePthreadTestcanceldid 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_cancelrecord the request on the thread and wake it when it is blocked in a cancellable wait. A null thread returnsESRCH.scePthreadTestcancel, the condition variable waits, thescePthreadSemandsemwaits,scePthreadJoinand 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.PTHREAD_CANCELED(1) throughscePthreadExit, which now runs the handlers of the new__pthread_cleanup_push_impand__pthread_cleanup_pop_impexports, newest first. The handler node uses the guest's_pthread_cleanup_infolike libthr'sstruct pthread_cleanup.scePthreadSetcancelstateandscePthreadSetcanceltypereturnEINVALfor unknown values and, like libthr, test for a pending request when the type is asynchronous.pthread_setcanceltypeis added.pthread_cancelmoves fromUnimplemented.cpptoPthread/Posix/Thread.cppwith its real signature.Limits, recorded in TechnicalDebt: a sleep is not interrupted (the thread exits when it ends, so the
sceKernelUsleeppath 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.hppandThread.cppin other places; this does not depend on it.Tested
Windows 11, WinLibs GCC 15.2.0: full build,
libstarget andctest(387 passed;agc_driver_sdwa_float_selectors,shader_clock,vop1_float_unary,vop3_mad_i64andvopc_compareexcluded, they fail on this machine's Intel Arc GPU without this change). Newguest_pthread_canceltest cancels threads blocked in a condition wait (while the canceller holds the mutex),scePthreadSemWait,sem_wait,scePthreadJoinand asceKernelUsleeploop, checks the disabled state, the asynchronous type, the handler order and the error codes; it passed 40 runs in a row, and 15 withAPS5_COARSE_TIMED_WAITS=1. Not tested on Linux and not tested with a title.Checklist
main; no other open PR implements the same functionsNotImplemented_nid_no_patch); silent stubs are listed in TechnicalDebt.mdfiles or images added to the repository (attach them to this PR)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
__pthread_cleanup_pop_imp_nid_postfix__pthread_cleanup_push_imp_nid_postfixpthread_cancel_nid_postfixpthread_setcanceltype_nid_postfixscePthreadCancelImported 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). Onmain(cea1ef13), startup stops atFAIL: 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.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 atPthread/Posix/Thread.cpp:87-89; code comments are only allowed as technical-debt markers.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) anddocs/dev/TechnicalDebt.md; please rebase.Imported from boykopovar/AnyPS5. Originally posted by adriadam10 on 2026-10-10T09:27:58Z.
Rebased onto current
mainas @oneandonlydean asked (conflicts inPthread/src/Cond.cpp,Pthread/src/Thread.cppanddocs/dev/TechnicalDebt.md); thepthread_cancelcomment was already dropped.Cond.cpp: the recursive-mutex wait keeps main's unlock/relock of the extra levels aroundThreadCancel::Wait.Thread.cpp: the cancel state, cancel type, exiting flag and cleanup handler stack now live inGuestThreadState(main moved the thread state toWindowsThreadLocal) instead of separatethread_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_cancelpasses 30 runs in a row.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 wasUnimplemented.cpp, where the oldpthread_cancelstub and its comment are now removed since the real export lives inPthread/Posix/Thread.cpp.Rebuilt and ran the libkernel/libs tests on Linux: all green.
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.
main)docs/dev/TechnicalDebt.mdL325