summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDaniël van de Giessen <daniel@dvdgiessen.nl>2022-02-23 17:28:32 +0100
committerDamien George <damien@micropython.org>2022-03-02 12:57:43 +1100
commit665f0e2a68dfa4944f60e85a00d0543bb3d7fee8 (patch)
tree4f9f040c2c9f50152a8fa6b4b51be97c84e606c2
parent919e586e46af67e7eaa819b0f187b600a3165526 (diff)
esp32: Sleep one tick in MICROPY_EVENT_POLL_HOOK.
If MicroPython threads are enabled, loops waiting for an incoming event should release the GIL and suspend, allowing other tasks to run while they wait. Prior to this commit, the problem can easily be observed by running a thread that is both busy and regularly releases the GIL (for example a loop doing something then sleeping a few ms after each iteration). When the main task is at the REPL, the thread is significantly stalled. If the main task is manually made to release the GIL (for example, by calling utime.sleep_ms(500)) the other thread can be seen immediately working at the expected speed again. Additionally, there are various instances in where blocking functions run MICROPY_EVENT_POLL_HOOK in a loop while they wait for a certain event/ condition. For example the uselect methods poll objects to determine whether data is available, but uses 100% of CPU while it does, constantly calling MICROPY_EVENT_POLL_HOOK in the process. The MICROPY_EVENT_POLL_HOOK macro is only ever used in waiting loops, where (if threads are enabled) it makes sense to yield for a single tick so that these loops do not consume all CPU cycles but instead other threads may execute. (In fact, the thing these loops wait for may even indirectly or directly depend on another task being able to run.) This change moves the sleep that was inside the REPL input function to inside the MICROPY_EVENT_POLL_HOOK macro, where the GIL is already being released, solving both the blocking REPL issue and the 100% CPU use issue at the same time. Signed-off-by: Daniël van de Giessen <daniel@dvdgiessen.nl>
-rw-r--r--ports/esp32/mpconfigport.h1
-rw-r--r--ports/esp32/mphalport.c1
2 files changed, 1 insertions, 1 deletions
diff --git a/ports/esp32/mpconfigport.h b/ports/esp32/mpconfigport.h
index 1ea19ada0..d4f542f66 100644
--- a/ports/esp32/mpconfigport.h
+++ b/ports/esp32/mpconfigport.h
@@ -195,6 +195,7 @@ void *esp_native_code_commit(void *, size_t, void *);
mp_handle_pending(true); \
MICROPY_PY_USOCKET_EVENTS_HANDLER \
MP_THREAD_GIL_EXIT(); \
+ ulTaskNotifyTake(pdFALSE, 1); \
MP_THREAD_GIL_ENTER(); \
} while (0);
#else
diff --git a/ports/esp32/mphalport.c b/ports/esp32/mphalport.c
index 15a8dce1f..41e6e6ec0 100644
--- a/ports/esp32/mphalport.c
+++ b/ports/esp32/mphalport.c
@@ -98,7 +98,6 @@ int mp_hal_stdin_rx_chr(void) {
return c;
}
MICROPY_EVENT_POLL_HOOK
- ulTaskNotifyTake(pdFALSE, 1);
}
}