fix(host): BroadcastConditionVariable and the timed semaphore acquires reach the HLE's objects #2765
Open
github-sync
wants to merge 3 commits from
github/pr/2765 into main
pull from: github/pr/2765
merge into: MagicBeanDev:main
MagicBeanDev:main
MagicBeanDev:github/pr/2893
MagicBeanDev:github/pr/2890
MagicBeanDev:github/pr/2887
MagicBeanDev:github/pr/2878
MagicBeanDev:github/pr/2875
MagicBeanDev:github/pr/2874
MagicBeanDev:github/pr/2873
MagicBeanDev:github/pr/2871
MagicBeanDev:github/pr/2756
MagicBeanDev:github/pr/2897
MagicBeanDev:github/pr/2896
MagicBeanDev:github/pr/2718
MagicBeanDev:github/pr/2842
MagicBeanDev:github/pr/2691
MagicBeanDev:github/pr/2895
MagicBeanDev:github/pr/2743
MagicBeanDev:github/pr/2708
MagicBeanDev:github/pr/2732
MagicBeanDev:github/pr/2755
MagicBeanDev:github/pr/2817
MagicBeanDev:github/pr/2876
MagicBeanDev:release-please--branches--main
MagicBeanDev:release-please--branches--main--release-notes
MagicBeanDev:github/pr/2778
MagicBeanDev:github/pr/2888
MagicBeanDev:github/pr/2866
MagicBeanDev:github/pr/2877
MagicBeanDev:github/pr/2800
MagicBeanDev:github/pr/2829
MagicBeanDev:github/pr/2850
MagicBeanDev:github/pr/2889
MagicBeanDev:github/pr/2882
MagicBeanDev:github/pr/2860
MagicBeanDev:github/pr/2849
MagicBeanDev:github/pr/2886
MagicBeanDev:github/pr/2794
MagicBeanDev:github/pr/2840
MagicBeanDev:github/pr/2830
MagicBeanDev:github/pr/2834
MagicBeanDev:github/pr/2719
MagicBeanDev:github/pr/2742
MagicBeanDev:github/pr/2741
MagicBeanDev:github/pr/2820
MagicBeanDev:github/pr/2771
MagicBeanDev:github/pr/2859
MagicBeanDev:github/pr/2858
MagicBeanDev:github/pr/2823
MagicBeanDev:github/pr/2757
MagicBeanDev:github/pr/2769
MagicBeanDev:github/pr/2844
MagicBeanDev:github/pr/2843
MagicBeanDev:github/pr/2673
MagicBeanDev:github/pr/2855
MagicBeanDev:github/pr/2739
MagicBeanDev:github/pr/2807
MagicBeanDev:github/pr/2851
MagicBeanDev:github/pr/2789
MagicBeanDev:github/pr/2814
MagicBeanDev:github/pr/2818
MagicBeanDev:github/pr/2774
MagicBeanDev:github/pr/2772
MagicBeanDev:github/pr/2750
MagicBeanDev:github/pr/2786
MagicBeanDev:github/pr/2785
MagicBeanDev:github/pr/2782
MagicBeanDev:github/pr/2779
MagicBeanDev:github/pr/2775
MagicBeanDev:github/pr/2706
MagicBeanDev:github/pr/2766
MagicBeanDev:github/pr/2747
MagicBeanDev:github/pr/2759
MagicBeanDev:github/pr/2745
MagicBeanDev:github/pr/2740
MagicBeanDev:github/pr/2733
MagicBeanDev:github/pr/2725
MagicBeanDev:github/pr/2727
MagicBeanDev:github/pr/2728
MagicBeanDev:github/pr/2729
MagicBeanDev:github/pr/2721
MagicBeanDev:github/pr/2722
MagicBeanDev:github/pr/2715
MagicBeanDev:github/pr/2714
MagicBeanDev:github/pr/2707
MagicBeanDev:github/pr/2703
MagicBeanDev:github/pr/2705
MagicBeanDev:github/pr/2697
MagicBeanDev:github/pr/2679
MagicBeanDev:github/pr/2676
MagicBeanDev:github/pr/2669
MagicBeanDev:docs/what-hookshot-connects-to
MagicBeanDev:fix/hdr-encode-test
MagicBeanDev:fix/main-tests
MagicBeanDev:fix/play-cache-files
MagicBeanDev:perf/game-barrier-order
MagicBeanDev:feat/heap-lazy
MagicBeanDev:feat/texture-detail
MagicBeanDev:perf/astc-opaque-bc1
MagicBeanDev:fix/fs-dir-lock
MagicBeanDev:fix/framegen-native-kept
MagicBeanDev:fix/update-no-downgrade
MagicBeanDev:port/k10-gcn-f16-mirrors
MagicBeanDev:fix/framegen-mult-cap
MagicBeanDev:chore/release-as-2.1.1
MagicBeanDev:perf/pcache-load-switch
MagicBeanDev:fix/prepare-progress-log
MagicBeanDev:fix/update-closes-stale-game
MagicBeanDev:fix/hdr-scaled-sampled
MagicBeanDev:perf/first-start-preset
MagicBeanDev:perf/zero-upload
MagicBeanDev:fix/vcore-escape-2s
MagicBeanDev:test/fake-gh-atomic
MagicBeanDev:perf/batch-idle-flush
MagicBeanDev:test/finish-stub-a
MagicBeanDev:chore/record-2.1.0
MagicBeanDev:fix/main-red-tests
MagicBeanDev:fix/test-display-startup-driver-cache
MagicBeanDev:feat/mesh-shader-mode
MagicBeanDev:fix/menu-probes-at-startup
MagicBeanDev:fix/fps-counter-classic
MagicBeanDev:fix/test-log-batch-flake
MagicBeanDev:fix/narrow-screen-hud
MagicBeanDev:ci/prereleases
MagicBeanDev:release/v2.0.15
MagicBeanDev:hotfix/v2.0.15
MagicBeanDev:fix/present-ahead-replay
MagicBeanDev:release/v2.0.13
MagicBeanDev:fix/bracket-paths-2013
MagicBeanDev:fix/hdr-overlays-2013
MagicBeanDev:fix/fps-counter-scale-2013
MagicBeanDev:fix/stick-seen-at-rest
MagicBeanDev:hotfix/menu-fits-screen
MagicBeanDev:fix/f1-always-opens-menu
MagicBeanDev:fix/ini-keep-lines
MagicBeanDev:claude/cranky-pike-7cf247
MagicBeanDev:perf/texture-answer-flat
MagicBeanDev:fix/stamp-test-modules
MagicBeanDev:fix/appimage-fonts
MagicBeanDev:fix/display-awake
MagicBeanDev:release/v2.0.12
MagicBeanDev:fix/discord-windows
MagicBeanDev:rc/v2.0.12-tests
MagicBeanDev:fix/dlss-keep-feature
MagicBeanDev:rc/v2.0.12
MagicBeanDev:button-icons
No reviewers
Labels
Clear labels
accessibility
Barrier affecting people with disabilities
autorelease: pending
autorelease: tagged
bug
Something isn't working
dependencies
Pull requests that update a dependency file
documentation
Improvements or additions to documentation
duplicate
This issue or pull request already exists
enhancement
New feature or request
github_actions
Pull requests that update GitHub Actions code
good first issue
Good for newcomers
help wanted
Extra attention is needed
invalid
This doesn't seem right
question
Further information is requested
testing wanted
wontfix
This will not be worked on
No labels
accessibility
autorelease: pending
autorelease: tagged
bug
dependencies
documentation
duplicate
enhancement
github_actions
good first issue
help wanted
invalid
question
testing wanted
wontfix
Milestone
Clear milestone
No items
No milestone
Assignees
Clear assignees
No assignees
1 participant
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".
No due date set.
Dependencies
No dependencies set
Reference
MagicBeanDev/Hookshot!2765
Loading…
Reference in a new issue
No description provided.
Delete branch "github/pr/2765"
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?
The HLE replaces the nn::os condition variable and semaphore functions with host objects, but a few siblings were left to the game's own SDK code. That code acts on guest words the HLE never writes, and signals kernel keys that no HLE waiter sleeps on. Some of these siblings have callers.
The problem
BroadcastConditionVariablewoke nobody.svcSignalProcessWideKey). The HLE'sWaitConditionVariablesleeps on its host object until the object's generation moves, and only the HLE's Signal moves it.TimedAcquireSemaphorenever saw an HLE release. The C library's timed semaphore wait and NVIDIA's OS layer call it. The light-semaphore timed and try acquires have the same gap but no direct callers.The change
host/nn_os_sync.c):cv->lk, it moves the generation and wakes all waiters.cv_reserve): one mutex for all of them, at most 8 owed.HOS_NO_HANDOFF=1turns it off, as for Signal.TimedAcquireSemaphore,TimedAcquireLightSemaphore,TryAcquireLightSemaphore: these take the HLE's count throughsema_acquire_for, which waits up to the timeout. A timeout of 0 or less tries once.wait_until: sleeps in steps of at most 0x7FFFFFFF ms, because a finite wait of 0xFFFFFFFF ms isINFINITEon Windows. Its callers already wait again until their deadline.HOS_CHAIN, likesema_acquire's. Its deadline saturates for a huge TimeSpan.How these were found: the image's exports were compared with the HLE's list, per sync object type. Siblings still left to the SDK that have direct callers:
StopTimerEvent(the C library's timers);RegisterSystemWorkerHandler.The HLE timer has no separate "signalled" state, so a correct Stop needs a model change; it is left for a follow-up.
Tested
tests/test_os_lifted_siblings.py(new) runs the real code cut from the source (cv_enter,cv_leave,cv_reserve, the Broadcast HLE,sema_acquire_for) over counting stubs and a fake clock.HOS_NO_HANDOFFunset,1and0.HOS_CHAINnotes.sema_acquire_for.tests/run_all.py: 425 passed. The 5 failures are this PC's toolchain and fail the same way on main:nx2/test_nup_paths.py: symlinks on Windows;test_spawn_emit,test_fpx_sticky:<algorithm>not found;test_pipeline_play_cache,test_prepare_stall:NOMINMAXredefined.host/*.c; a gpt-6.1-sol review (three rounds, last clean).In game
Boot to title and prologue movie OK; Broadcast is not reached in this scene, so the movie-hang link is not shown.
46780503. A new game from an empty save, the prologue movie, 190 s a run.Not tested
🤖 Generated with Claude Code
I merged this into current main. tests/test_os_lifted_siblings.py and tests/test_multiwait_wake.py pass. The full tests/run_all.py passes except tests/test_lift_cache.py, which fails on main here too, and test_recorder_sync, which timed out under load.
The code looks right. The Broadcast HLE (host/nn_os_sync.c) bumps gen under cv->lk and wakes all waiters, makes the same hand-off Signal does (capped at cv_reserve's 8), and respects HOS_NO_HANDOFF. sema_acquire_for saturates the deadline and tries once for a timeout of 0 or less, and the 0x7FFFFFFF ms cap in wait_until avoids INFINITE.
My concern is reach. Until now, every Broadcast caller (ALooper, LiveDataSource, NvOs) slept until some unrelated Signal. Now they wake on time. That's the fix, but it changes timing on every movie and NVN OS-layer path, and nothing has been run in game. Please boot to the title, play a scene with a movie (the opening, or one of the reported hangs), and add that run to the description. Then I'd merge it.
nit: this saturation is right. The other timed waits in this file (
TimedWaitConditionVariable,event_wait, the queue) still wrap on a huge TimeSpan. A shared helper would cover them all (could be a follow-up).Thanks, this is a real gap and the code holds up. I went through it for lost and spurious wake-ups and the timeout arithmetic, and agree with Joey-Magnus's read:
genmoves undercv->lkand the wake-all comes after, so a waiter that snapshotted the old generation can't miss it. The reservations balance: one per waiter counted at the broadcast, capped at 8, and every one of those waiters leaves under the same lock (a timed waiter that is timing out included), so nothing stays owed to nobody. Mixed mutexes andHOS_NO_HANDOFF=1fall back the same way Signal does.sema_acquire_for: it checks the count under the lock, loops on spurious and vcore wake-ups, takes a release that races the deadline, and a timeout of 0 or less tries once. The saturated deadline can't wrap.wait_until: clamping to 0x7FFFFFFF ms fixes both platforms. Windows treats 0xFFFFFFFF asINFINITE, and so does the POSIX shim (ms == INFINITE). All four callers loop back to their deadline.What's missing is the run in the game, the same thing Joey-Magnus asked for. Every Broadcast caller (the movie framework's queues, NVIDIA's OS layer) used to sleep until some unrelated Signal and now wakes on time. That is the fix, but it changes timing on paths nothing here has run. Before this goes in, please:
HOS_CHAINnotes on a movie condition variable. If a hang reproduces on main, show it gone with the change.A question and two small nits:
TryAcquireSemaphore(not the light one) isn't HLE'd either. Is it absent from the image's imports, or was it left out on purpose? If it is imported, it has the same gap as the light try.TimedWaitConditionVariable,event_waitand the queue's timed paths still computehos_now_ns() + timeoutwithout saturating, so a huge TimeSpan wraps negative and times out at once. A small shareddeadline_after(ns)used by all of them would fix that. It's older than this PR, so a follow-up is fine.wait_until, so the new clamp isn't covered. A line check on the0x7FFFFFFFcap would pin it.Overlap: #2708 touches the code right next to this (the Signal early-out when nothing waits, and an atomic
cv_waiting) but doesn't conflict textually. Either order works. Once both are in, Broadcast could take the same early-out for consistency. #2709 (spin) and #2487 (POSIX futex waiter count) don't touch this code, and #2487's wake skip is safe for the wake-all here.Thanks both. The in-game run is now in the description (In game). On Windows (RX 9070 XT, base main
46780503) the build boots to the title and plays the prologue movie from a new game:hckrman101's points:
TryAcquireSemaphore: the non-light one is already HLE'd on main (host/nn_os_sync.c:1325); only the light try was left to the SDK.TimedWaitConditionVariable(:1293) and the event and queue timed paths (:696,:1012,:1201) still add the timeout without saturating. They predate this PR; I'll leave them for a follow-up with a shared deadline helper, as you suggest.973970a7adds a line check onwait_until's0x7FFFFFFFcap. It fails with the cap removed. The same commit names the callers in comments by role rather than by symbol, as the description does.🤖 Generated with Claude Code