From 3cc3e4e032edd912dd3a55b8df65927e48217f2c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dani=C3=ABl=20van=20de=20Giessen?= Date: Wed, 23 Nov 2022 14:20:58 +0100 Subject: [PATCH] esp32/machine_uart: Release GIL for blocking reads. MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit If we're reading from an UART with a non-zero timeout, we can release the GIL so that other threads/tasks may run while we are sleeping waiting for data to arrive. Signed-off-by: Daniƫl van de Giessen --- ports/esp32/machine_uart.c | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/ports/esp32/machine_uart.c b/ports/esp32/machine_uart.c index 541d7ccc5..0b4f5890b 100644 --- a/ports/esp32/machine_uart.c +++ b/ports/esp32/machine_uart.c @@ -472,8 +472,17 @@ STATIC mp_uint_t machine_uart_read(mp_obj_t self_in, void *buf_in, mp_uint_t siz time_to_wait = pdMS_TO_TICKS(self->timeout); } + bool release_gil = time_to_wait > 0; + if (release_gil) { + MP_THREAD_GIL_EXIT(); + } + int bytes_read = uart_read_bytes(self->uart_num, buf_in, size, time_to_wait); + if (release_gil) { + MP_THREAD_GIL_ENTER(); + } + if (bytes_read <= 0) { *errcode = MP_EAGAIN; return MP_STREAM_ERROR;