apple: fix crash when video_threaded is enabled with the Cocoa/Metal driver#19165
Draft
vanreece wants to merge 1 commit into
Draft
apple: fix crash when video_threaded is enabled with the Cocoa/Metal driver#19165vanreece wants to merge 1 commit into
vanreece wants to merge 1 commit into
Conversation
…driver
Enabling video_threaded on macOS crashes immediately. The threaded video
worker runs the graphics driver init, which reaches into the Cocoa UI
layer -- -[CocoaView setViewType:] and -[CocoaView setVideoMode:] -- and
mutates NSWindow/NSView state off the main thread. AppKit forbids that,
so the process aborts ("NSWindow should only be modified on the main
thread").
A plain dispatch_sync to the main queue is not enough on its own: during
CMD_INIT the main thread is parked in scond_wait() inside
video_thread_wait_reply(), so it never drains the main queue and the
dispatch would deadlock.
Fix both halves:
- video_thread_wrapper.c: for CMD_INIT on Apple platforms, wait by
pumping the main run loop in short slices (CFRunLoopRunInMode, 2 ms)
and polling for the reply, instead of scond_wait(). This lets the
worker's main-queue AppKit work execute during init. All other
commands keep the original lightweight wait.
- ui_cocoa.m: -[CocoaView setViewType:] and -[CocoaView setVideoMode:]
bounce to the main thread when invoked off it; combined with the pump
above, the dispatch_sync completes instead of deadlocking.
With both changes, video_threaded=true no longer crashes the Metal
driver on macOS.
Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
Author
|
Sorry if AI generated PRs aren't welcome. Feel free to hate on it, but I thought it might be useful. |
Contributor
|
If this actually works it would be a big deal - for a long time we had to simply 'stub out' threaded video because we couldn't figure out a way to make it work with Cocoa |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Enabling
video_threaded = trueon macOS crashes RetroArch immediately with theCocoa/Metal driver.
The threaded-video worker thread runs graphics-driver init, which calls into the
Cocoa UI layer —
-[CocoaView setViewType:]and-[CocoaView setVideoMode:]—and mutates
NSWindow/NSViewstate off the main thread. AppKit forbids that,so the process aborts ("NSWindow should only be modified on the main thread").
A plain
dispatch_syncto the main queue is not enough on its own: duringCMD_INITthe main thread is parked inscond_wait()insidevideo_thread_wait_reply(), so it never drains the main queue and the dispatchwould deadlock.
Fix
Two halves, both required:
gfx/video_thread_wrapper.c— forCMD_INITon Apple platforms, wait bypumping the main run loop in short slices (
CFRunLoopRunInMode, 2 ms) andpolling for the reply, instead of
scond_wait(). All other commands keep theoriginal lightweight wait, so there is no change off the init path.
ui/drivers/ui_cocoa.m—-[CocoaView setViewType:]and-[CocoaView setVideoMode:]bounce to the main thread when invoked off it;combined with the pump above, the
dispatch_synccompletes instead ofdeadlocking.
Testing
Built on macOS (Apple Silicon, Metal). Before:
video_threaded=true+video_driver=metalcrashes on load. After: launches and runs normally. Thedefault
video_threaded=falsepath is unchanged — new code is gated toCMD_INITon__APPLE__.🤖 Generated with Claude Code