Skip to content

Commit afa6fbd

Browse files
committed
Core: Obtain mutex in mCoreThreadIsActive
1 parent 41123e7 commit afa6fbd

File tree

1 file changed

+11
-6
lines changed

1 file changed

+11
-6
lines changed

src/core/thread.c

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -563,13 +563,18 @@ void mCoreThreadJoin(struct mCoreThread* threadContext) {
563563
threadContext->impl = NULL;
564564
}
565565

566+
static bool _isActive(struct mCoreThreadInternal* impl) {
567+
return impl->state >= mTHREAD_RUNNING && impl->state < mTHREAD_EXITING && !(impl->requested & mTHREAD_REQ_CRASHED);
568+
}
569+
566570
bool mCoreThreadIsActive(struct mCoreThread* threadContext) {
567571
if (!threadContext->impl) {
568572
return false;
569573
}
570-
return threadContext->impl->state >= mTHREAD_RUNNING &&
571-
threadContext->impl->state < mTHREAD_EXITING &&
572-
!(threadContext->impl->requested & mTHREAD_REQ_CRASHED);
574+
MutexLock(&threadContext->impl->stateMutex);
575+
bool active = _isActive(threadContext->impl);
576+
MutexUnlock(&threadContext->impl->stateMutex);
577+
return active;
573578
}
574579

575580
void mCoreThreadInterrupt(struct mCoreThread* threadContext) {
@@ -578,7 +583,7 @@ void mCoreThreadInterrupt(struct mCoreThread* threadContext) {
578583
}
579584
MutexLock(&threadContext->impl->stateMutex);
580585
++threadContext->impl->interruptDepth;
581-
if (threadContext->impl->interruptDepth > 1 || !mCoreThreadIsActive(threadContext)) {
586+
if (threadContext->impl->interruptDepth > 1 || !_isActive(threadContext->impl)) {
582587
MutexUnlock(&threadContext->impl->stateMutex);
583588
return;
584589
}
@@ -593,7 +598,7 @@ void mCoreThreadInterruptFromThread(struct mCoreThread* threadContext) {
593598
}
594599
MutexLock(&threadContext->impl->stateMutex);
595600
++threadContext->impl->interruptDepth;
596-
if (threadContext->impl->interruptDepth > 1 || !mCoreThreadIsActive(threadContext)) {
601+
if (threadContext->impl->interruptDepth > 1 || !_isActive(threadContext->impl)) {
597602
if (threadContext->impl->state == mTHREAD_INTERRUPTING) {
598603
threadContext->impl->state = mTHREAD_INTERRUPTED;
599604
}
@@ -610,7 +615,7 @@ void mCoreThreadContinue(struct mCoreThread* threadContext) {
610615
}
611616
MutexLock(&threadContext->impl->stateMutex);
612617
--threadContext->impl->interruptDepth;
613-
if (threadContext->impl->interruptDepth < 1 && mCoreThreadIsActive(threadContext)) {
618+
if (threadContext->impl->interruptDepth < 1 && _isActive(threadContext->impl)) {
614619
if (threadContext->impl->requested) {
615620
threadContext->impl->state = mTHREAD_REQUEST;
616621
} else {

0 commit comments

Comments
 (0)