micropython/ports
Daniël van de Giessen 665f0e2a68 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>
2022-03-02 12:57:43 +11:00
..
bare-arm bare-arm/mpconfigport.h: Disable remaining optional features. 2021-11-01 14:23:06 +11:00
cc3200 ports: Consolidate inclusion of umachine module in built-ins. 2022-02-03 10:08:54 +11:00
esp32 esp32: Sleep one tick in MICROPY_EVENT_POLL_HOOK. 2022-03-02 12:57:43 +11:00
esp8266 ports: Consolidate inclusion of umachine module in built-ins. 2022-02-03 10:08:54 +11:00
javascript py/runtime: Allow initialising sys.path/argv with defaults. 2021-12-18 00:08:07 +11:00
mimxrt ports: Consolidate inclusion of umachine module in built-ins. 2022-02-03 10:08:54 +11:00
minimal ports: Recompile bytecode tests now that .mpy format changed. 2022-02-24 18:29:02 +11:00
nrf ports: Consolidate inclusion of umachine module in built-ins. 2022-02-03 10:08:54 +11:00
pic16bit all: Update to point to files in new shared/ directory. 2021-07-12 17:08:10 +10:00
powerpc ports: Recompile bytecode tests now that .mpy format changed. 2022-02-24 18:29:02 +11:00
qemu-arm qemu-arm: Add tests for freezing viper and asm_thumb code. 2022-02-24 18:29:02 +11:00
rp2 rp2/mpthreadport: Fix memory corruption when thread is created in core1. 2022-03-01 17:03:57 +11:00
samd ports: Consolidate inclusion of umachine module in built-ins. 2022-02-03 10:08:54 +11:00
stm32 stm32/mboot: Allow a board more control over entry initialisation. 2022-02-18 14:59:35 +11:00
teensy py/runtime: Allow initialising sys.path/argv with defaults. 2021-12-18 00:08:07 +11:00
unix unix/.gitignore: Remove *.py from ignore list. 2022-02-28 19:02:58 +11:00
windows windows: Unify project file headers. 2022-02-25 16:41:11 +11:00
zephyr ports: Consolidate inclusion of umachine module in built-ins. 2022-02-03 10:08:54 +11:00