Skip to content

Commit 665f0e2

Browse files
DvdGiessendpgeorge
authored andcommitted
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>
1 parent 919e586 commit 665f0e2

File tree

2 files changed

+1
-1
lines changed

2 files changed

+1
-1
lines changed

ports/esp32/mpconfigport.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -195,6 +195,7 @@ void *esp_native_code_commit(void *, size_t, void *);
195195
mp_handle_pending(true); \
196196
MICROPY_PY_USOCKET_EVENTS_HANDLER \
197197
MP_THREAD_GIL_EXIT(); \
198+
ulTaskNotifyTake(pdFALSE, 1); \
198199
MP_THREAD_GIL_ENTER(); \
199200
} while (0);
200201
#else

ports/esp32/mphalport.c

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,6 @@ int mp_hal_stdin_rx_chr(void) {
9898
return c;
9999
}
100100
MICROPY_EVENT_POLL_HOOK
101-
ulTaskNotifyTake(pdFALSE, 1);
102101
}
103102
}
104103

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy