fix(libkernel): wake every thread waiting on an equeue or kqueue when an event fires #7492
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/7492"
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 Lindamea1 on 2026-10-18T09:26:00Z.
What
sceKernelWaitEqueueandkeventsleep on the queue's condition variable inKernelEqueuePrivate::WaitForEventsUntil, andAddEvent,TriggerEventandEditEventwoke only one of the sleeping threads (m_cond.NotifyOne()). With two or more threads blocked on one equeue or kqueue:sceKernelAddUserEventandsceKernelTriggerUserEvent, or anEVFILT_USERknote withoutEV_CLEARtriggered withNOTE_TRIGGER) released one waiter. The others slept until their timeout, or for good with a null timeout, while a poll (timo0) of the same queue at that moment returned the event.sceKernelAddTimerEvent, orEVFILT_TIMERwithEV_ADD) reached only the thread woken to recompute its wait. The others returned at their own timeout, or not at all with a null timeout.On
main, with two waiters using a 5 s timeout and the event fired 50 ms after they start, the second waiter returns at 5000 ms in all three cases (user event, timer, kqueue user event), while the first returns at 50 ms, or at 100 ms for the 50 ms timer.FreeBSD 11.4 wakes every thread asleep on a kqueue when a knote becomes active.
knote_enqueuecallskqueue_wakeup, which callswakeup(kq), andwakeupmakes every thread sleeping on the queue runnable throughsleepq_broadcast;kern_event.cnever callswakeup_one. Each woken thread goes back toretryinkqueue_scan. A knote withoutEV_CLEARorEV_DISPATCHis put back at the tail after it is reported, so every waiter returns it; anEV_CLEARknote is taken by one waiter and the others findkq_countat 0 and sleep again. ANOTE_TRIGGERchange enqueues the knote inkqueue_register, and a timer expiry throughfilt_timerexpire. Not measured on a console.The three calls now use
m_cond.NotifyAll(), asClosealready does.WaitForEventsUntilcollects again after every wake and goes back to sleep when it finds nothing, so anEV_CLEARevent still reaches one waiter and a queue with a single waiter behaves as before. A queue with socket or pipe events waits inWaitForDescriptorsinstead, andWakePollersalready wakes every poller there. On Windows the non-coarseTimedWait::Condition::NotifyAllwalks the waiter list, the pathClosealready takes. I don't know of a title that blocks two threads on one queue; VideoOut, AGC, AMPR and libSceHttp2 completions reach waiters throughTriggerEvent.New checks:
VerifyEventWakesEveryWaiterinGuestEqueueEvents.cpp: two threads wait with a 5 s timeout, the main thread triggers a level user event (TriggerEvent), and both waiters must return within 2 s. The same follows on the emptied queue by adding a 50 ms timer (AddEvent).UserEventWakesEveryWaiterinGuestKqueue.cpp: the same with twokeventwaiters and aNOTE_TRIGGERchange (EditEvent).Tested
Gentoo Linux, kernel 7.2.8, GCC 16.2.1, glibc 2.43, Release,
BUILD_TESTING=ON, rebased ontomaincf2c66706.guest_equeue_eventsandguest_kqueuewith the new checks pass (0.63 s and 3.81 s). With onlyEqueue.cppreverted tomainboth abort after 5.42 s and 6.66 s, at the< 2 scheck, because the second waiter returns at its 5 s timeout.ctest --repeat until-fail:15 -j2): all pass.ctest -j4 --timeout 300: 1806 tests, 6 failed, 16 skipped. The failures (agc_driver_sample_orderwith its_grid_referenceand_recipe_depth_locationsruns, andnet_dontwait_blocking_send,_sendtoand_sendmsg) fail the same way with onlyEqueue.cppfrommain, so this change doesn't cause them. The skips are the 13video_out_flip_*tests,video_out_frame_dump_queueandvideo_out_telemetry_captionunder the offscreen SDL driver, andagc_driver_stencil_ref_export.Equeue.cpp,GuestEqueueEvents.cppandGuestKqueue.cppcompile withx86_64-w64-mingw32-g++16.2 (-fsyntax-only -Wall -Wextra). This is a syntax check, not a Windows build or run.tools/check_conventions.pypasses againstmain.tools/pr_overlap.py --branchreports no overlapping lines. It could not compare #6044, #7088, #7509, #7653 and #9099, which conflict withmain; merged with this branch, each of them has the same conflicts as withmainalone (#9099: 3 hunks inEqueue.cpp, inGatherInterestsandWaitForDescriptors, and 1 inEqueue.hpp), and none of them changesNotifyOneorNotifyAll.Not run on Windows, macOS or a console by me. @wachi35676 (Windows 11 and Ubuntu 24.04) and @screamm (Windows 11, stacked with #7509 and #7090) ran the head before this rebase, fd2eb3521, which has the same change; see their comments.
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)🤖 Generated with Claude Code
Imported from boykopovar/AnyPS5. Originally posted by wachi35676 on 2026-10-18T12:20:11Z.
Windows run (this was syntax-checked only there): Windows 11, MinGW GCC 15.2, with this PR merged onto
main9f25d0bf5, plus Ubuntu 24.04/GCC 13.3.Tests: with only the two test files from this PR,
guest_equeue_eventsandguest_kqueueabort onmainon both platforms. With the PR they pass 10/10 each with the default Windows waits, and 10/10 each withAPS5_COARSE_TIMED_WAITS=1(thecondition_variable_anypath). The socket/equeue set passes serially (Windows 38/38, Linux 26/26).Extra probe, not in the PR: eight
sceKernelWaitEqueuewaiters (3 s timeout) on one level user event, triggered after 20 ms, 100 rounds:main: 100 of 100 rounds had a waiter return at its timeout (worst 3055 ms).With an
EV_CLEARevent (sceKernelAddUserEventEdge), 4 waiters and 20 rounds, exactly one waiter got the event each round on both platforms, soNotifyAlldoesn't double-deliver.#7509: it merges cleanly with this PR, and the combination passes
guest_equeue_events/guest_kqueue5/5.Heads-up for Windows reruns: under parallel load,
guest_kqueueonmainitself can abort inPipeRead(GuestKqueue.cpp:246). In my runskeventreturned the read event 13 µs–35 ms into a wait, while the writer was still in its 50 ms sleep. That happens beforeUserEventWakesEveryWaiterruns, so a failure there is unrelated to this change.AI-assisted: yes (Claude Code); results above were run on this machine.
Imported from boykopovar/AnyPS5. Originally posted by screamm on 2026-10-18T13:53:25Z.
Windows run of head
fd2eb3521, merged ontomain0d078f75dtogether with #7509 (c170bba42) and #7090 (17d9e0a51); the three merge cleanly in that order, so this is the stack, not #7492 alone.BUILD_TESTING=ON.libspass. No warning inEqueue.cpp,GuestEqueueEvents.cpporGuestKqueue.cpp.guest_equeue_events,guest_kqueueandguest_socket_eventspass 3/3 (equeue 0.7-0.9 s, kqueue 3.0-3.1 s), and 35 equeue/kqueue/socket/poll/select/rudp tests pass 3x, 105/105. With--repeat until-fail:15under load from other builds,guest_equeue_eventsandguest_kqueuepass 15/15 each (equeue 0.77-2.27 s, kqueue 3.17-4.74 s).guest_kqueueabort in a first batch (while I was copying files): it aborted inPipeRead, which runs before the new test. Afterwards 20/20 on the stack, 20/20 onmainand 28/28 with 8 parallel copies. @wachi35676 reports the samePipeReadabort onmainunder load, so I treat it as that flake.NotifyAllback toNotifyOneinAddEvent,TriggerEventandEditEvent(everything else kept) makes bothguest_equeue_eventsandguest_kqueueabort after 8.6 s (the second waiter returns at its 5 s timeout). Restored, both pass.ctest -j8 --timeout 120 -E '^agc_driver_|^video_out_flip'): 936 tests, 929 pass, 2 skipped, 5 load timeouts (conventions_function_bodies,configure_seed,hardware_oracle_compare,guest_none_relocations,tls_function_coverage_register_address_loads) that all pass when rerun serially.tools/check_conventions.py --base origin/mainandgit diff origin/main --checkpass on the stack.kern_event.c:kqueue_wakeupclearsKQ_SLEEPand callswakeup(kq), which wakes every sleeper, and the file has nowakeup_one; this matches the description. AnEV_CLEARknote is dequeued by the first scanner, so the other waiters find nothing and sleep again, as here.Titles:
sceKernelWaitEqueueis imported by 17 local titles. I ran PPSA99169 (RetroArch), PPSA01153 (OliSe Player) and PPSA74126 (Stremio Plus) for 60 s each, the stack against the same build withEqueue.cpp,Equeue.hppandUdp.cppfrommain. No stop line in any run; frames 5494 / 3457 / 3387 before and 5914 / 3486 / 3487 after, with the same last fps (about 119 / 60 / 60). With a local diagnosticlibkernel(reverted) that logs the first time a second thread blocks on one equeue, PPSA74126, PPSA99082 and PPSA99169 (app.exeonly, not its picker child) had none in 45 s. So I found no local title that blocks two threads on one queue, as the description says; the change has no visible effect on them.Not checked: Linux, macOS,
APS5_COARSE_TIMED_WAITS=1, a console, the other 14 importers.Written with Claude Code assistance (validation by a subagent, reviewed in this session).
Imported from boykopovar/AnyPS5. Originally posted by blawesom on 2026-10-21T17:06:19Z.
Support triage — rebase note: the only rebase conflict against current
main(d34da5d69) iscore/libs/tests/GuestKqueue.cpp, which has seen several new test calls land recently (LowWaterMarks, DisableAndDispatch). The production change inEqueue/Equeue.cppitself applies cleanly. A rebase keeping main's test-runner calls alongside this PR's should be mechanical.Imported from boykopovar/AnyPS5. Originally posted by Lindamea1 on 2026-10-21T18:37:19Z.
Thanks. Rebased onto
maincf2c66706. The only conflict was in the test list ofmain()inGuestKqueue.cpp, wheremainaddedWatchKqueue()afterUserEvents()at the same spot as this PR'sUserEventWakesEveryWaiter(); both calls are kept.Equeue.cppand the new tests are unchanged.On the rebased branch both tests pass, 15 times in a row, and abort again at the
< 2 scheck with onlyEqueue.cppfrommain. The fullctestrun has no new failures: the 6 tests that fail on this machine fail the same way withEqueue.cppfrommain. The description is updated.AI-assisted: yes (Claude Code).
Imported from boykopovar/AnyPS5. Originally posted by ahmadmuj on 2026-10-25T16:15:21Z.
Review: APPROVED
Reviewed head
2feb074210, merged onto current main0119103fa9(clean). Linux (WSL2 Ubuntu 24.04), GCC 13.3, Release. Main has had 24 commits underEqueue/since this branch's basecf2c66706, so I mostly wanted to know if the fix still holds up on top of them.What I ran:
guest_equeue_eventsandguest_kqueueon the merged tree: 10/10 each (--repeat until-fail:10).Equeue.cppwith this PR's tests: both abort (equeue after 5.5 s, kqueue after 7.3 s, vs 0.6 s / 5.0 s green), which fits the second waiter sitting out its 5 s limit.Checked on the merged tree:
Closeare still the onlym_condnotifies.DeleteEventandRemoveDescriptorEventsonly wake pollers, and they don't make anything ready, so nothing's missing. The kqueueNOTE_TRIGGERpath ends up inEditEvent.NotifyAll: inCollectTriggeredandCollectOncePerWait,EV_CLEAR/EV_DISPATCH/EV_ONESHOTget cleared or erased by whichever waiter collects first, and the others loop back to sleep. AMPR'spendingEventshands the next queued completion to the second waiter, not the same one again.sceKernelAddTimerEventisEV_CLEARwith a periodic interval, so the second waiter gets the next expiry, not the same one. That matches the 100 ms / 150 ms in the description.kern_event.c:kqueue_wakeupcallswakeup(kq),wakeupgoes throughsleepq_broadcast, there's nowakeup_oneanywhere in the file, and non-EV_CLEAR/EV_DISPATCHknotes get re-queued at the tail inkqueue_scan. Your links point at those lines.Looks right to me.
Not checked: Windows (wachi35676 and screamm covered it on older bases), titles.