Skip to content

Commit

Permalink
Fix unlocking in FreeRTOS _RunEventLoop.
Browse files Browse the repository at this point in the history
Ensure we always start with an empty event queue after FreeRTOS _InitChipStack.
  • Loading branch information
bzbarsky-apple committed Sep 8, 2022
1 parent e33d6fb commit cf2d6b8
Showing 1 changed file with 14 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,12 @@ CHIP_ERROR GenericPlatformManagerImpl_FreeRTOS<ImplClass>::_InitChipStack(void)
ExitNow(err = CHIP_ERROR_NO_MEMORY);
}
}
else
{
// Clear out any events that might be stuck in the queue, so we start
// with a clean slate, as if we had just re-created the queue.
xQueueReset(mChipEventQueue);
}

mShouldRunEventLoop.store(false);

Expand Down Expand Up @@ -158,7 +164,7 @@ void GenericPlatformManagerImpl_FreeRTOS<ImplClass>::_RunEventLoop(void)
ChipDeviceEvent event;

// Lock the CHIP stack.
Impl()->LockChipStack();
StackLock lock;

bool oldShouldRunEventLoop = false;
if (!mShouldRunEventLoop.compare_exchange_strong(oldShouldRunEventLoop /* expected */, true /* desired */))
Expand Down Expand Up @@ -211,13 +217,13 @@ void GenericPlatformManagerImpl_FreeRTOS<ImplClass>::_RunEventLoop(void)
waitTime = portMAX_DELAY;
}

// Unlock the CHIP stack, allowing other threads to enter CHIP while the event loop thread is sleeping.
Impl()->UnlockChipStack();

BaseType_t eventReceived = xQueueReceive(mChipEventQueue, &event, waitTime);

// Lock the CHIP stack.
Impl()->LockChipStack();
BaseType_t eventReceived = pdFALSE;
{
// Unlock the CHIP stack, allowing other threads to enter CHIP while
// the event loop thread is sleeping.
StackUnlock unlock;
eventReceived = xQueueReceive(mChipEventQueue, &event, waitTime);
}

// If an event was received, dispatch it. Continue receiving events from the queue and
// dispatching them until the queue is empty.
Expand Down

0 comments on commit cf2d6b8

Please sign in to comment.