Core: Clean up thread state mutex usage

This commit is contained in:
Vicki Pfau 2024-03-19 21:30:47 -07:00
parent c80f3afd77
commit aa7b9349f8
1 changed files with 20 additions and 12 deletions

View File

@ -42,13 +42,9 @@ static BOOL CALLBACK _createTLS(PINIT_ONCE once, PVOID param, PVOID* context) {
static void _mCoreLog(struct mLogger* logger, int category, enum mLogLevel level, const char* format, va_list args); static void _mCoreLog(struct mLogger* logger, int category, enum mLogLevel level, const char* format, va_list args);
static void _changeState(struct mCoreThreadInternal* threadContext, enum mCoreThreadState newState, bool broadcast) { static void _changeState(struct mCoreThreadInternal* threadContext, enum mCoreThreadState newState) {
MutexLock(&threadContext->stateMutex);
threadContext->state = newState; threadContext->state = newState;
if (broadcast) { ConditionWake(&threadContext->stateCond);
ConditionWake(&threadContext->stateCond);
}
MutexUnlock(&threadContext->stateMutex);
} }
static void _waitOnInterrupt(struct mCoreThreadInternal* threadContext) { static void _waitOnInterrupt(struct mCoreThreadInternal* threadContext) {
@ -178,7 +174,9 @@ void _crashed(void* context) {
if (!thread) { if (!thread) {
return; return;
} }
_changeState(thread->impl, mTHREAD_CRASHED, true); MutexLock(&thread->impl->stateMutex);
_changeState(thread->impl, mTHREAD_CRASHED);
MutexUnlock(&thread->impl->stateMutex);
} }
void _coreSleep(void* context) { void _coreSleep(void* context) {
@ -196,7 +194,9 @@ void _coreShutdown(void* context) {
if (!thread) { if (!thread) {
return; return;
} }
_changeState(thread->impl, mTHREAD_EXITING, true); MutexLock(&thread->impl->stateMutex);
_changeState(thread->impl, mTHREAD_EXITING);
MutexUnlock(&thread->impl->stateMutex);
} }
#ifdef ENABLE_SCRIPTING #ifdef ENABLE_SCRIPTING
@ -303,7 +303,9 @@ static THREAD_ENTRY _mCoreThreadRun(void* context) {
core->reset(core); core->reset(core);
threadContext->impl->core = core; threadContext->impl->core = core;
_changeState(threadContext->impl, mTHREAD_RUNNING, true); MutexLock(&threadContext->impl->stateMutex);
_changeState(threadContext->impl, mTHREAD_RUNNING);
MutexUnlock(&threadContext->impl->stateMutex);
if (threadContext->resetCallback) { if (threadContext->resetCallback) {
threadContext->resetCallback(threadContext); threadContext->resetCallback(threadContext);
@ -326,23 +328,27 @@ static THREAD_ENTRY _mCoreThreadRun(void* context) {
bool wasPaused = false; bool wasPaused = false;
int pendingRequests = 0; int pendingRequests = 0;
MutexLock(&impl->stateMutex);
while (impl->state < mTHREAD_EXITING) { while (impl->state < mTHREAD_EXITING) {
#ifdef USE_DEBUGGERS #ifdef USE_DEBUGGERS
struct mDebugger* debugger = core->debugger; struct mDebugger* debugger = core->debugger;
if (debugger) { if (debugger) {
MutexUnlock(&impl->stateMutex);
mDebuggerRun(debugger); mDebuggerRun(debugger);
MutexLock(&impl->stateMutex);
if (debugger->state == DEBUGGER_SHUTDOWN) { if (debugger->state == DEBUGGER_SHUTDOWN) {
_changeState(impl, mTHREAD_EXITING, false); impl->state = mTHREAD_EXITING;
} }
} else } else
#endif #endif
{ {
while (impl->state == mTHREAD_RUNNING) { while (impl->state == mTHREAD_RUNNING) {
MutexUnlock(&impl->stateMutex);
core->runLoop(core); core->runLoop(core);
MutexLock(&impl->stateMutex);
} }
} }
MutexLock(&impl->stateMutex);
while (impl->state >= mTHREAD_MIN_WAITING && impl->state < mTHREAD_EXITING) { while (impl->state >= mTHREAD_MIN_WAITING && impl->state < mTHREAD_EXITING) {
if (impl->state == mTHREAD_INTERRUPTING) { if (impl->state == mTHREAD_INTERRUPTING) {
impl->state = mTHREAD_INTERRUPTED; impl->state = mTHREAD_INTERRUPTED;
@ -430,7 +436,9 @@ static THREAD_ENTRY _mCoreThreadRun(void* context) {
} }
while (impl->state < mTHREAD_SHUTDOWN) { while (impl->state < mTHREAD_SHUTDOWN) {
_changeState(impl, mTHREAD_SHUTDOWN, false); MutexLock(&impl->stateMutex);
impl->state = mTHREAD_SHUTDOWN;
MutexUnlock(&impl->stateMutex);
} }
if (core->opts.rewindEnable) { if (core->opts.rewindEnable) {