esp32/mphalport: Always yield at least once in delay_ms.

This helps the OS switch to and give other threads processing time during
the sleep.  It also ensures that pending events are handled, even when
sleeping for 0ms.

Fixes issue #5344.

Signed-off-by: Damien George <damien@micropython.org>
This commit is contained in:
Damien George 2021-07-21 01:33:16 +10:00
parent 2cfbe5bc0f
commit 6214fa3f9e

View File

@ -142,14 +142,22 @@ void mp_hal_delay_ms(uint32_t ms) {
uint64_t dt;
uint64_t t0 = esp_timer_get_time();
for (;;) {
mp_handle_pending(true);
MICROPY_PY_USOCKET_EVENTS_HANDLER
MP_THREAD_GIL_EXIT();
uint64_t t1 = esp_timer_get_time();
dt = t1 - t0;
if (dt + portTICK_PERIOD_MS * 1000 >= us) {
// doing a vTaskDelay would take us beyond requested delay time
taskYIELD();
MP_THREAD_GIL_ENTER();
t1 = esp_timer_get_time();
dt = t1 - t0;
break;
}
MICROPY_EVENT_POLL_HOOK
} else {
ulTaskNotifyTake(pdFALSE, 1);
MP_THREAD_GIL_ENTER();
}
}
if (dt < us) {
// do the remaining delay accurately