Skip to content

Commit 6214fa3

Browse files
committed
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 micropython#5344. Signed-off-by: Damien George <[email protected]>
1 parent 2cfbe5b commit 6214fa3

File tree

1 file changed

+10
-2
lines changed

1 file changed

+10
-2
lines changed

ports/esp32/mphalport.c

+10-2
Original file line numberDiff line numberDiff line change
@@ -142,14 +142,22 @@ void mp_hal_delay_ms(uint32_t ms) {
142142
uint64_t dt;
143143
uint64_t t0 = esp_timer_get_time();
144144
for (;;) {
145+
mp_handle_pending(true);
146+
MICROPY_PY_USOCKET_EVENTS_HANDLER
147+
MP_THREAD_GIL_EXIT();
145148
uint64_t t1 = esp_timer_get_time();
146149
dt = t1 - t0;
147150
if (dt + portTICK_PERIOD_MS * 1000 >= us) {
148151
// doing a vTaskDelay would take us beyond requested delay time
152+
taskYIELD();
153+
MP_THREAD_GIL_ENTER();
154+
t1 = esp_timer_get_time();
155+
dt = t1 - t0;
149156
break;
157+
} else {
158+
ulTaskNotifyTake(pdFALSE, 1);
159+
MP_THREAD_GIL_ENTER();
150160
}
151-
MICROPY_EVENT_POLL_HOOK
152-
ulTaskNotifyTake(pdFALSE, 1);
153161
}
154162
if (dt < us) {
155163
// do the remaining delay accurately

0 commit comments

Comments
 (0)