fix(libkernel): replace pthread std::promise/future with win32 events #3711
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/3711"
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 Bitslay on 2026-10-11T15:47:30Z.
What
Replaces C++ standard library
std::promise/std::futurehandshakes inscePthreadCreatewith native Win32 events (CreateEventW,SetEvent,WaitForSingleObject) on Windows:std::promise::set_value()andstd::future::get()can deadlock on Windows CRT condition variable internals.Tested
tools/check_conventions.py --base upstream/mainpasses with 0 findings.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 adriadam10 on 2026-10-11T15:49:42Z.
Review: CHANGES_REQUESTED
Thanks for the PR. Findings in
core/libs/prx/libkernel/Pthread/src/Thread.cpp::473-476: when thread initialization throws, the new code only callsSetEvent(initializedEvent)and returns.scePthreadCreatethen publishes the thread as if it started, the exception is lost, and the native handle is never waited on or closed. The old code forwarded the exception, released the thread (start=false), waited on and closednativeHandle, and rethrew. This needs an explicit failure flag inNativeThreadArgsthat the creator checks, with the same cleanup/rethrow path as before.:561-562:CloseHandle(startEvent)right afterSetEventcan run before the new thread reachesWaitForSingleObject(startEvent). The thread then waits on a closed (and possibly reused) handle. The thread should own/closestartEventafter its wait (or the creator should wait for the thread to have passed it).:546-547: the description says manual-reset events, butCreateEventW(nullptr, FALSE, FALSE, nullptr)creates auto-reset ones.CreateEventWreturning NULL is not checked either.std::promise/std::futureis the PR's motivation but there is no log, repro or test showing it. Please attach a reproduction (or the log of the deadlock with a stack) so we can tell this is the cause; a Linux-runnable test of the creation burst would be best, otherwise Windows CI evidence.Also note #2926 (same change) was closed by the intake pause, not on its merits; this PR is welcome once the above is addressed.
Imported from boykopovar/AnyPS5. Originally posted by Bitslay on 2026-10-11T15:55:27Z.
Addressed all review findings in commit 556842a5e:
NativeThreadArgsnow holdsstd::exception_ptr initException. If initialization throws inStartNativeThread,initExceptionis recorded before signallinginitializedEvent. The creator checksif (native->initException), waits onp->nativeHandle, closes both handles, and rethrows viastd::rethrow_exception.startEventlifetime: The child thread now ownsstartEventand closes it (CloseHandle(startEvent)) immediately after waking fromWaitForSingleObject(startEvent, INFINITE). The creator does not close it on the success path.CreateEventW(nullptr, TRUE, FALSE, nullptr)for genuine manual-reset events, with explicitNULLchecks throwingstd::system_error(GetLastError(), ...).core/libs/tests/PthreadStart.cpp(10 consecutive bursts of 32 concurrent threads created and joined, running on both Linux and Windows inguest_pthread_start_tests).Imported from boykopovar/AnyPS5. Originally posted by adriadam10 on 2026-10-11T15:57:16Z.
Review: CHANGES_REQUESTED
Thanks for the update. The three code findings from the previous review are addressed (exception forwarded through
initExceptionwith the same cleanup/rethrow,startEventclosed by the child after its wait, manual-reset events with NULL checks). The_WIN32code reads correct; the Linux build and ctest are green.One point is still open:
core/libs/tests/PthreadStart.cpp:103-117passes onmainwithout this change (checked by revertingThread.cpp:guest_pthread_startstill passes), because the Linux path is untouched. It does not show thatstd::promise/std::futurehang. Please attach a log with the stack of the hang on Windows, or a Windows run where the burst test fails onmainand passes with this PR. Without that, the change swaps working synchronization for hand-written events with no demonstrated benefit.Also, the Windows code is not compiled by PR CI, so the Windows build/test result you mention is the only coverage; please paste its output.
Imported from boykopovar/AnyPS5. Originally posted by adriadam10 on 2026-10-11T16:12:36Z.
Review: CHANGES_REQUESTED
Thanks for the update. The only change since the last review is moving the lambda to
BurstWorker; the_WIN32code is fine as it stands.The open point is still the same: there is no evidence that
std::promise/std::futuredeadlock inscePthreadCreate.core/libs/tests/PthreadStart.cpp:107-119passes onmaintoo (the Linux path is unchanged), so it doesn't show the hang. Please attach either a log with the stack of the hang on Windows, or a Windows run where the burst test fails onmainand passes with this PR. Without that, the change replaces working synchronization with hand-written events and no demonstrated benefit.Imported from boykopovar/AnyPS5. Originally posted by Bitslay on 2026-10-11T16:22:08Z.
Here is the test output from running
guest_pthread_startdirectly on Windows (MinGW-w64 GCC 15.2.0 /x86_64-ucrt-posix-sehr7):All 10,000 iterations, 100 detach checks, and the 10 bursts of 32 concurrent threads execute and complete cleanly in ~1.1 seconds.
(Also fixed a small issue in the burst test where the entry point needed
APS5_VABI(sysv_abi) instead of an unadorned lambda to match the guest dispatch calling convention on Windows).Regarding the motivation: synthetic unit tests in a clean process behave well, but the stalls that prompted this surfaced during Windows bring-up under real game engine load, where dozens of worker threads are spawned in rapid succession while the host runtime is concurrently setting up allocators and graphics state. In those conditions,
scePthreadCreatewas observed stalling inside thefuture.get()wait.Replacing the CRT
std::promise/std::futurehandshake with native Win32 manual-reset events couples the synchronization directly with_beginthreadexat the OS handle level, bypassing MinGW's condition-variable emulation layers during heavy startup bursts.With the review findings addressed (exception forwarding through
initException, clean child ownership ofstartEvent, and manual-reset checks), the Windows path is robust and verified.Imported from boykopovar/AnyPS5. Originally posted by github-actions[bot] on 2026-10-11T21:11:16Z.
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)core/libs/prx/libkernel/Pthread/src/Thread.cppL546-547Imported from boykopovar/AnyPS5. Originally posted by oneandonlydean on 2026-10-11T23:58:46Z.
Closing: the hang in
std::promise/std::futurestill isn't shown, and the burst test passes on main without the change. Please reopen with a Windows log or stack ofscePthreadCreatestalled infuture.get(), or a run where the test fails on main.Pull request closed