diff --git a/src/gba/gba-thread.c b/src/gba/gba-thread.c index 48a6f9d32..9060f1798 100644 --- a/src/gba/gba-thread.c +++ b/src/gba/gba-thread.c @@ -426,10 +426,14 @@ void GBAThreadJoin(struct GBAThread* threadContext) { } } +bool GBAThreadIsActive(struct GBAThread* threadContext) { + return threadContext->state >= THREAD_RUNNING && threadContext->state < THREAD_EXITING; +} + void GBAThreadInterrupt(struct GBAThread* threadContext) { MutexLock(&threadContext->stateMutex); ++threadContext->interruptDepth; - if (threadContext->interruptDepth > 1) { + if (threadContext->interruptDepth > 1 || !GBAThreadIsActive(threadContext)) { MutexUnlock(&threadContext->stateMutex); return; } @@ -447,7 +451,7 @@ void GBAThreadInterrupt(struct GBAThread* threadContext) { void GBAThreadContinue(struct GBAThread* threadContext) { MutexLock(&threadContext->stateMutex); --threadContext->interruptDepth; - if (threadContext->interruptDepth < 1) { + if (threadContext->interruptDepth < 1 && GBAThreadIsActive(threadContext)) { threadContext->state = threadContext->savedState; ConditionWake(&threadContext->stateCond); } diff --git a/src/gba/gba-thread.h b/src/gba/gba-thread.h index 1f00941bd..41bfeec6e 100644 --- a/src/gba/gba-thread.h +++ b/src/gba/gba-thread.h @@ -104,6 +104,7 @@ void GBAThreadEnd(struct GBAThread* threadContext); void GBAThreadReset(struct GBAThread* threadContext); void GBAThreadJoin(struct GBAThread* threadContext); +bool GBAThreadIsActive(struct GBAThread* threadContext); void GBAThreadInterrupt(struct GBAThread* threadContext); void GBAThreadContinue(struct GBAThread* threadContext); diff --git a/src/platform/qt/Display.cpp b/src/platform/qt/Display.cpp index 59b872f0a..8d08e9026 100644 --- a/src/platform/qt/Display.cpp +++ b/src/platform/qt/Display.cpp @@ -52,13 +52,17 @@ void Display::startDrawing(const uint32_t* buffer, GBAThread* thread) { void Display::stopDrawing() { if (m_drawThread) { - GBAThreadInterrupt(m_context); - GBASyncSuspendDrawing(&m_context->sync); + if (GBAThreadIsActive(m_context)) { + GBAThreadInterrupt(m_context); + GBASyncSuspendDrawing(&m_context->sync); + } QMetaObject::invokeMethod(m_painter, "stop", Qt::BlockingQueuedConnection); m_drawThread->exit(); m_drawThread = nullptr; - GBASyncResumeDrawing(&m_context->sync); - GBAThreadContinue(m_context); + if (GBAThreadIsActive(m_context)) { + GBASyncResumeDrawing(&m_context->sync); + GBAThreadContinue(m_context); + } } } diff --git a/src/platform/qt/GameController.cpp b/src/platform/qt/GameController.cpp index fac8578b7..03c1810ff 100644 --- a/src/platform/qt/GameController.cpp +++ b/src/platform/qt/GameController.cpp @@ -250,13 +250,9 @@ void GameController::setAudioBufferSamples(int samples) { } void GameController::setFPSTarget(float fps) { - if (m_gameOpen) { - GBAThreadInterrupt(&m_threadContext); - m_threadContext.fpsTarget = fps; - GBAThreadContinue(&m_threadContext); - } else { - m_threadContext.fpsTarget = fps; - } + GBAThreadInterrupt(&m_threadContext); + m_threadContext.fpsTarget = fps; + GBAThreadContinue(&m_threadContext); QMetaObject::invokeMethod(m_audioProcessor, "inputParametersChanged"); } @@ -315,23 +311,15 @@ void GameController::setTurbo(bool set, bool forced) { } void GameController::setAVStream(GBAAVStream* stream) { - if (m_gameOpen) { - GBAThreadInterrupt(&m_threadContext); - m_threadContext.stream = stream; - GBAThreadContinue(&m_threadContext); - } else { - m_threadContext.stream = stream; - } + GBAThreadInterrupt(&m_threadContext); + m_threadContext.stream = stream; + GBAThreadContinue(&m_threadContext); } void GameController::clearAVStream() { - if (m_gameOpen) { - GBAThreadInterrupt(&m_threadContext); - m_threadContext.stream = nullptr; - GBAThreadContinue(&m_threadContext); - } else { - m_threadContext.stream = nullptr; - } + GBAThreadInterrupt(&m_threadContext); + m_threadContext.stream = nullptr; + GBAThreadContinue(&m_threadContext); } void GameController::updateKeys() {