Minor EmuThread refactor

This commit is contained in:
Henrik Rydgård
2026-08-05 00:15:56 +02:00
parent edd968062a
commit 5eb4772644
9 changed files with 41 additions and 32 deletions
+11 -18
View File
@@ -36,7 +36,7 @@ bool MainThread_Ready() {
return g_inLoop;
}
static void EmuThreadFunc(GraphicsContext *graphicsContext, Application *application, std::function<void()> postFrame) {
static void EmuThreadFunc(GraphicsContext *graphicsContext, Application *application, std::function<bool (GraphicsContext *)> frame) {
INFO_LOG(Log::G3D, "Entering separate emu thread");
SetCurrentThreadName("EmuThread");
@@ -57,11 +57,7 @@ static void EmuThreadFunc(GraphicsContext *graphicsContext, Application *applica
// We're here again, so the game quit. Restart Run() which controls the UI.
// This way they can load a new game.
// This normally calls NativeFrame()
application->Frame(graphicsContext);
if (postFrame) {
postFrame();
}
if (GetUIState() == UISTATE_EXIT) {
if (!frame(graphicsContext)) {
g_emuThreadState = EmuThreadState::QUIT_REQUESTED;
}
}
@@ -77,10 +73,10 @@ static void EmuThreadFunc(GraphicsContext *graphicsContext, Application *applica
g_emuThreadState = EmuThreadState::STOPPED;
}
std::thread EmuThread_Start(GraphicsContext *graphicsContext, Application *application, std::function<void()> postFrame) {
std::thread EmuThread_Start(GraphicsContext *graphicsContext, Application *application, std::function<bool(GraphicsContext *)> frame) {
INFO_LOG(Log::System, "EmuTread_Start");
_dbg_assert_(g_emuThreadState == EmuThreadState::STOPPED);
std::thread emuThread = std::thread(&EmuThreadFunc, graphicsContext, application, postFrame);
std::thread emuThread = std::thread(&EmuThreadFunc, graphicsContext, application, frame);
graphicsContext->ThreadStart();
return emuThread;
}
@@ -98,7 +94,7 @@ void EmuThread_Join(GraphicsContext *graphicsContext, std::thread &emuThread) {
graphicsContext->ThreadEnd();
}
bool RunMainLoop(GraphicsContext *graphicsContext, Application *application, std::function<bool()> runCondition, std::function<void()> postFrame) {
bool RunMainLoop(GraphicsContext *graphicsContext, Application *application, std::function<bool(GraphicsContext *)> frame) {
// This is the main thread. the graphics contexts will spawn and handle its own threads if needed.
// InitFromRenderThread/ShutdownFromRenderThread are not used.
@@ -109,19 +105,17 @@ bool RunMainLoop(GraphicsContext *graphicsContext, Application *application, std
g_inLoop = true;
while (runCondition()) {
while (frame(graphicsContext)) {
// We're here again, so the game quit. Restart Run() which controls the UI.
// This way they can load a new game.
application->Frame(graphicsContext);
postFrame();
}
// NOTE: Don't call stuff like Core_Stop here. On Android, we fully shut down graphics when you switch away from the app,
// then boot it up again when returning. That means stopping this thread and restarting it.
// Process the shutdown. Without this, non-GL delays 800ms on shutdown.
// Process the shutdown. Without this, non-GL delays 800ms on shutdown. TODO: is this still an issue?
Core_StateProcessed();
application->Frame(graphicsContext);
frame(graphicsContext);
g_inLoop = false;
@@ -131,7 +125,7 @@ bool RunMainLoop(GraphicsContext *graphicsContext, Application *application, std
}
// Call InitAPI and ShutdownAPI outside this!
bool MainThreadFunc(GraphicsContext *graphicsContext, Application *application, WindowSystem windowSystem, void *windowData1, void *windowData2, std::function<void()> postFrame) {
bool MainThreadFunc(GraphicsContext *graphicsContext, Application *application, WindowSystem windowSystem, void *windowData1, void *windowData2, std::function<bool(GraphicsContext *)> frame) {
// This is now the render thread, and will spawn the emu thread below.
std::string error_string;
bool success = graphicsContext->InitSurface(windowSystem, windowData1, windowData2, &error_string);
@@ -146,7 +140,7 @@ bool MainThreadFunc(GraphicsContext *graphicsContext, Application *application,
DEBUG_LOG(Log::Boot, "Done.");
g_inLoop = true;
std::thread emuThread = EmuThread_Start(graphicsContext, application, postFrame);
std::thread emuThread = EmuThread_Start(graphicsContext, application, frame);
graphicsContext->ThreadStart();
// This thread becomes the render thread. EmuThread will tell it when to quit by sending a message.
while (graphicsContext->ThreadFrame()) {}
@@ -160,9 +154,8 @@ bool MainThreadFunc(GraphicsContext *graphicsContext, Application *application,
} else {
SetCurrentThreadName("MainThread");
RunMainLoop(graphicsContext, application, []() { return GetUIState() != UISTATE_EXIT; }, postFrame);
RunMainLoop(graphicsContext, application, frame);
}
graphicsContext->ShutdownSurface();
return true;
}