Skip to content

Commit

Permalink
update modMessageService to avoid starving timers, triggering watchdo…
Browse files Browse the repository at this point in the history
…g, etc. #1257
  • Loading branch information
phoddie committed Dec 21, 2023
1 parent 42225dc commit 0bd542e
Showing 1 changed file with 17 additions and 5 deletions.
22 changes: 17 additions & 5 deletions xs/platforms/esp/xsHost.c
Original file line number Diff line number Diff line change
Expand Up @@ -1029,9 +1029,12 @@ int modMessagePostToMachineFromISR(xsMachine *the, modMessageDeliver callback, v
void modMessageService(xsMachine *the, int maxDelayMS)
{
modMessageRecord msg;
uint32_t startTime = modMilliseconds();
uint32_t maxDuration = (uint32_t)maxDelayMS;
unsigned portBASE_TYPE count = uxQueueMessagesWaiting(the->msgQueue);
if (!count) count = 1; // if no messages pending on entry, service no more than 1 (cannot exit immediately - debug queue messages, and caller expects delay to avoid busy wait)

#if CONFIG_ESP_TASK_WDT_EN
modWatchDogReset();
#ifndef CONFIG_ESP_TASK_WDT_TIMEOUT_S
// The default timeout is 5s, but it can be changed using this CONFIG decl as well as
// dynamically via esp_task_wdt_reconfigure, we assume "worst case" 1s here if it's not
Expand All @@ -1049,28 +1052,37 @@ void modMessageService(xsMachine *the, int maxDelayMS)
#endif

#ifdef mxDebug
while (true) {
do {
QueueSetMemberHandle_t queue = xQueueSelectFromSet(the->queues, maxDelayMS);
if (!queue)
break;

if (!xQueueReceive(queue, &msg, 0))
break;

modWatchDogReset();
(msg.callback)(the, msg.refcon, msg.message, msg.length);
if (msg.message)
c_free(msg.message);

if ((queue == the->msgQueue) && !--count)
break;
maxDelayMS = 0;
}
} while ((modMilliseconds() - startTime) < maxDuration);
#else
while (xQueueReceive(the->msgQueue, &msg, maxDelayMS)) {
do {
if (!xQueueReceive(the->msgQueue, &msg, maxDelayMS))
break;

modWatchDogReset();
(msg.callback)(the, msg.refcon, msg.message, msg.length);
if (msg.message)
c_free(msg.message);

if (!--count)
break;
maxDelayMS = 0;
}
} while ((modMilliseconds() - startTime) < maxDuration);
#endif

modWatchDogReset();
Expand Down

0 comments on commit 0bd542e

Please sign in to comment.