mirror of https://github.com/mgba-emu/mgba.git
Core: More threading cleanup
This commit is contained in:
parent
008a6f3f23
commit
e61a324df2
|
@ -87,7 +87,8 @@ struct mCoreThreadInternal {
|
|||
int requested;
|
||||
|
||||
Mutex stateMutex;
|
||||
Condition stateCond;
|
||||
Condition stateOnThreadCond;
|
||||
Condition stateOffThreadCond;
|
||||
int interruptDepth;
|
||||
bool frameWasOn;
|
||||
|
||||
|
|
|
@ -44,12 +44,12 @@ static void _mCoreLog(struct mLogger* logger, int category, enum mLogLevel level
|
|||
|
||||
static void _changeState(struct mCoreThreadInternal* threadContext, enum mCoreThreadState newState) {
|
||||
threadContext->state = newState;
|
||||
ConditionWake(&threadContext->stateCond);
|
||||
ConditionWake(&threadContext->stateOffThreadCond);
|
||||
}
|
||||
|
||||
static void _waitOnInterrupt(struct mCoreThreadInternal* threadContext) {
|
||||
while (threadContext->state == mTHREAD_INTERRUPTED || threadContext->state == mTHREAD_INTERRUPTING) {
|
||||
ConditionWait(&threadContext->stateCond, &threadContext->stateMutex);
|
||||
ConditionWait(&threadContext->stateOnThreadCond, &threadContext->stateMutex);
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -110,7 +110,7 @@ static void _wait(struct mCoreThreadInternal* threadContext) {
|
|||
#endif
|
||||
|
||||
MutexLock(&threadContext->stateMutex);
|
||||
ConditionWake(&threadContext->stateCond);
|
||||
ConditionWake(&threadContext->stateOnThreadCond);
|
||||
}
|
||||
|
||||
static void _waitOnRequest(struct mCoreThreadInternal* threadContext, enum mCoreThreadRequest request) {
|
||||
|
@ -140,7 +140,7 @@ static void _sendRequest(struct mCoreThreadInternal* threadContext, enum mCoreTh
|
|||
static void _cancelRequest(struct mCoreThreadInternal* threadContext, enum mCoreThreadRequest request) {
|
||||
threadContext->requested &= ~request;
|
||||
_pokeRequest(threadContext);
|
||||
ConditionWake(&threadContext->stateCond);
|
||||
ConditionWake(&threadContext->stateOffThreadCond);
|
||||
}
|
||||
|
||||
void _frameStarted(void* context) {
|
||||
|
@ -351,19 +351,18 @@ static THREAD_ENTRY _mCoreThreadRun(void* context) {
|
|||
|
||||
while (impl->state >= mTHREAD_MIN_WAITING && impl->state < mTHREAD_EXITING) {
|
||||
if (impl->state == mTHREAD_INTERRUPTING) {
|
||||
impl->state = mTHREAD_INTERRUPTED;
|
||||
ConditionWake(&impl->stateCond);
|
||||
_changeState(impl, mTHREAD_INTERRUPTED);
|
||||
}
|
||||
|
||||
while (impl->state >= mTHREAD_MIN_WAITING && impl->state <= mTHREAD_MAX_WAITING) {
|
||||
#ifdef USE_DEBUGGERS
|
||||
if (debugger && debugger->state != DEBUGGER_SHUTDOWN) {
|
||||
mDebuggerUpdate(debugger);
|
||||
ConditionWaitTimed(&impl->stateCond, &impl->stateMutex, 10);
|
||||
ConditionWaitTimed(&impl->stateOnThreadCond, &impl->stateMutex, 10);
|
||||
} else
|
||||
#endif
|
||||
{
|
||||
ConditionWait(&impl->stateCond, &impl->stateMutex);
|
||||
ConditionWait(&impl->stateOnThreadCond, &impl->stateMutex);
|
||||
}
|
||||
|
||||
if (impl->sync.audioWait) {
|
||||
|
@ -392,14 +391,13 @@ static THREAD_ENTRY _mCoreThreadRun(void* context) {
|
|||
if (impl->state == mTHREAD_REQUEST) {
|
||||
if (pendingRequests) {
|
||||
if (pendingRequests & mTHREAD_REQ_PAUSE) {
|
||||
impl->state = mTHREAD_PAUSED;
|
||||
_changeState(impl, mTHREAD_PAUSED);
|
||||
}
|
||||
if (pendingRequests & mTHREAD_REQ_WAIT) {
|
||||
impl->state = mTHREAD_PAUSED;
|
||||
_changeState(impl, mTHREAD_PAUSED);
|
||||
}
|
||||
} else {
|
||||
impl->state = mTHREAD_RUNNING;
|
||||
ConditionWake(&threadContext->impl->stateCond);
|
||||
_changeState(impl, mTHREAD_RUNNING);
|
||||
}
|
||||
}
|
||||
MutexUnlock(&impl->stateMutex);
|
||||
|
@ -439,6 +437,7 @@ static THREAD_ENTRY _mCoreThreadRun(void* context) {
|
|||
if (impl->state < mTHREAD_SHUTDOWN) {
|
||||
impl->state = mTHREAD_SHUTDOWN;
|
||||
}
|
||||
ConditionWake(&threadContext->impl->stateOffThreadCond);
|
||||
MutexUnlock(&impl->stateMutex);
|
||||
|
||||
if (core->opts.rewindEnable) {
|
||||
|
@ -477,7 +476,8 @@ bool mCoreThreadStart(struct mCoreThread* threadContext) {
|
|||
}
|
||||
|
||||
MutexInit(&threadContext->impl->stateMutex);
|
||||
ConditionInit(&threadContext->impl->stateCond);
|
||||
ConditionInit(&threadContext->impl->stateOnThreadCond);
|
||||
ConditionInit(&threadContext->impl->stateOffThreadCond);
|
||||
|
||||
MutexInit(&threadContext->impl->sync.videoFrameMutex);
|
||||
ConditionInit(&threadContext->impl->sync.videoFrameAvailableCond);
|
||||
|
@ -502,7 +502,7 @@ bool mCoreThreadStart(struct mCoreThread* threadContext) {
|
|||
MutexLock(&threadContext->impl->stateMutex);
|
||||
ThreadCreate(&threadContext->impl->thread, _mCoreThreadRun, threadContext);
|
||||
while (threadContext->impl->state < mTHREAD_RUNNING) {
|
||||
ConditionWait(&threadContext->impl->stateCond, &threadContext->impl->stateMutex);
|
||||
ConditionWait(&threadContext->impl->stateOffThreadCond, &threadContext->impl->stateMutex);
|
||||
}
|
||||
MutexUnlock(&threadContext->impl->stateMutex);
|
||||
|
||||
|
@ -544,7 +544,7 @@ bool mCoreThreadHasCrashed(struct mCoreThread* threadContext) {
|
|||
|
||||
void mCoreThreadMarkCrashed(struct mCoreThread* threadContext) {
|
||||
MutexLock(&threadContext->impl->stateMutex);
|
||||
threadContext->impl->state = mTHREAD_CRASHED;
|
||||
_changeState(threadContext->impl, mTHREAD_CRASHED);
|
||||
MutexUnlock(&threadContext->impl->stateMutex);
|
||||
}
|
||||
|
||||
|
@ -552,7 +552,7 @@ void mCoreThreadClearCrashed(struct mCoreThread* threadContext) {
|
|||
MutexLock(&threadContext->impl->stateMutex);
|
||||
if (threadContext->impl->state == mTHREAD_CRASHED) {
|
||||
threadContext->impl->state = mTHREAD_REQUEST;
|
||||
ConditionWake(&threadContext->impl->stateCond);
|
||||
ConditionWake(&threadContext->impl->stateOnThreadCond);
|
||||
}
|
||||
MutexUnlock(&threadContext->impl->stateMutex);
|
||||
}
|
||||
|
@ -561,7 +561,7 @@ void mCoreThreadEnd(struct mCoreThread* threadContext) {
|
|||
MutexLock(&threadContext->impl->stateMutex);
|
||||
_waitOnInterrupt(threadContext->impl);
|
||||
threadContext->impl->state = mTHREAD_EXITING;
|
||||
ConditionWake(&threadContext->impl->stateCond);
|
||||
ConditionWake(&threadContext->impl->stateOnThreadCond);
|
||||
MutexUnlock(&threadContext->impl->stateMutex);
|
||||
MutexLock(&threadContext->impl->sync.audioBufferMutex);
|
||||
threadContext->impl->sync.audioWait = 0;
|
||||
|
@ -590,7 +590,8 @@ void mCoreThreadJoin(struct mCoreThread* threadContext) {
|
|||
ThreadJoin(&threadContext->impl->thread);
|
||||
|
||||
MutexDeinit(&threadContext->impl->stateMutex);
|
||||
ConditionDeinit(&threadContext->impl->stateCond);
|
||||
ConditionDeinit(&threadContext->impl->stateOnThreadCond);
|
||||
ConditionDeinit(&threadContext->impl->stateOffThreadCond);
|
||||
|
||||
MutexDeinit(&threadContext->impl->sync.videoFrameMutex);
|
||||
ConditionWake(&threadContext->impl->sync.videoFrameAvailableCond);
|
||||
|
@ -642,7 +643,6 @@ void mCoreThreadInterruptFromThread(struct mCoreThread* threadContext) {
|
|||
return;
|
||||
}
|
||||
threadContext->impl->state = mTHREAD_INTERRUPTING;
|
||||
ConditionWake(&threadContext->impl->stateCond);
|
||||
MutexUnlock(&threadContext->impl->stateMutex);
|
||||
}
|
||||
|
||||
|
@ -658,7 +658,7 @@ void mCoreThreadContinue(struct mCoreThread* threadContext) {
|
|||
} else {
|
||||
threadContext->impl->state = mTHREAD_RUNNING;
|
||||
}
|
||||
ConditionWake(&threadContext->impl->stateCond);
|
||||
ConditionWake(&threadContext->impl->stateOnThreadCond);
|
||||
}
|
||||
MutexUnlock(&threadContext->impl->stateMutex);
|
||||
}
|
||||
|
@ -718,7 +718,7 @@ void mCoreThreadSetRewinding(struct mCoreThread* threadContext, bool rewinding)
|
|||
threadContext->impl->rewinding = rewinding;
|
||||
if (rewinding && threadContext->impl->state == mTHREAD_CRASHED) {
|
||||
threadContext->impl->state = mTHREAD_REQUEST;
|
||||
ConditionWake(&threadContext->impl->stateCond);
|
||||
ConditionWake(&threadContext->impl->stateOnThreadCond);
|
||||
}
|
||||
MutexUnlock(&threadContext->impl->stateMutex);
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue