Skip to content

desktop-ui: Threading rework #1906

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
May 30, 2025
Merged

Conversation

jcm93
Copy link
Contributor

@jcm93 jcm93 commented Mar 24, 2025

Rearranges ares's main run loop, putting emulation in its own dedicated thread that is separate from the main/GUI thread. This updates ares to somewhat more modern application practices. This rework should have an array of benefits, including increased UI responsiveness, improved energy efficiency/performance, and increased compatibility with various modern APIs and frameworks for rendering, input, and audio.

Details

Generally, this PR implements a fairly simple approach to threading, using nall::thread's pthread-like semantics and several basic recursive mutexes. For this initial implementation, we use very coarse locking on the emulation thread, combined with very limited access to the emulator state from the main and other ares threads, allowed to occur between emulator run loop iterations. Synchronization on the emulator state is managed via programMutex.

Since this mutex is held for the duration of the primary work loop, including while the thread is suspended, the GUI thread should try to never acquire the program mutex unless absolutely necessary. Doing so with regularity will incur a severe UI responsiveness penalty. For destructive updates like driver settings updates, memory writes, and game loads/unloads, we acquire it. For other cross-thread communication, we try to synchronize using other methods that do not incur a significant performance penalty.

Window resizes triggered via the emulation loop synchronize with the _needsResize atomic. Status messages are passed by acquiring the messageMutex. vBlanksPerSecond is also now an atomic<u64>.

Input continues to poll on the main thread for compatibility with existing code. Access to the input system is managed via inputMutex.

Known Issues

Draft status as this is still a work in progress, but PRing now for visibility, feedback, and to consolidate any issue tracking. There are likely kinks left to work out; testing is appreciated.

- Cores are loaded on the main thread before being run on the emulator thread; this causes issues for paraLLEl-RDP, which expects that the thread to initialize Vulkan is also the thread that will make RDP calls. Addressed.

- Memory editor currently does not have access to the program mutex; reads and writes are unsynchronized. Addressed; memory editor synchronizes on writes but reads occur in a thread-unsafe fashion.

- The Clang thread sanitizer still reports a couple data races in Metal and OpenGL; further testing with other Ruby drivers is needed to make sure resource access is properly managed, particularly with respect to the output framebuffers. A larger examination of locking in other parts of ares is probably necessary, but I think out of scope for the first iteration of this rework.

  • The gdb server seems to still be working fine, but could probably use more testing.

@jcm93 jcm93 force-pushed the threading-again branch 4 times, most recently from dc08225 to f7ab0b9 Compare April 3, 2025 14:59
@jcm93 jcm93 force-pushed the threading-again branch 2 times, most recently from ff41391 to 0d52cec Compare April 5, 2025 20:38
@jcm93 jcm93 force-pushed the threading-again branch 3 times, most recently from 1a7ab65 to 37cf14f Compare May 28, 2025 19:15
@jcm93 jcm93 marked this pull request as ready for review May 28, 2025 19:16
@jcm93
Copy link
Contributor Author

jcm93 commented May 28, 2025

Taking out of draft. There is still more work I would like to do to address certain aspects of the threading model, especially with respect to the screen mutex. But I think those are out of scope for this PR, and this should just be a first step that keeps rendering functioning as is.

@jcm93 jcm93 force-pushed the threading-again branch 2 times, most recently from 259fdf0 to ad5115e Compare May 28, 2025 20:09
@jcm93 jcm93 marked this pull request as draft May 29, 2025 01:16
@jcm93 jcm93 force-pushed the threading-again branch 2 times, most recently from 5f61866 to a5c17b8 Compare May 29, 2025 16:28
@jcm93 jcm93 marked this pull request as ready for review May 29, 2025 16:44
@jcm93 jcm93 force-pushed the threading-again branch from a5c17b8 to fa35700 Compare May 29, 2025 17:11
@LukeUsher LukeUsher merged commit 1d0d617 into ares-emulator:master May 30, 2025
12 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging this pull request may close these issues.

2 participants