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.
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 viaprogramMutex
.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 themessageMutex
.vBlanksPerSecond
is also now anatomic<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.