summaryrefslogtreecommitdiffstatshomepage
path: root/py/scheduler.c
diff options
context:
space:
mode:
authorAngus Gratton <angus@redyak.com.au>2023-11-30 16:06:32 +1100
committerDamien George <damien@micropython.org>2023-12-08 12:47:00 +1100
commitf5be0128e4da1417136495c20888f8291cd22386 (patch)
tree0a75714bdd2219bfdc36931c7e6a87d18b221950 /py/scheduler.c
parent66be82da7ce63e9c907d7d2ed00341dae80ee83f (diff)
downloadmicropython-f5be0128e4da1417136495c20888f8291cd22386.tar.gz
micropython-f5be0128e4da1417136495c20888f8291cd22386.zip
py: Add port-agnostic inline functions for event handling.
These are intended to replace MICROPY_EVENT_POLL_HOOK and MICROPY_EVENT_POLL_HOOK_FAST, which are insufficient for tickless ports. This implementation is along the lines suggested here: https://github.com/micropython/micropython/issues/12925#issuecomment-1803038430 Currently any usage of these functions expands to use the existing hook macros, but this can be switched over port by port. This work was funded through GitHub Sponsors. Signed-off-by: Angus Gratton <angus@redyak.com.au>
Diffstat (limited to 'py/scheduler.c')
-rw-r--r--py/scheduler.c36
1 files changed, 36 insertions, 0 deletions
diff --git a/py/scheduler.c b/py/scheduler.c
index b9a43bfbdb..3eae8b4fa3 100644
--- a/py/scheduler.c
+++ b/py/scheduler.c
@@ -241,3 +241,39 @@ void mp_handle_pending(bool raise_exc) {
}
#endif
}
+
+// Handles any pending MicroPython events without waiting for an interrupt or event.
+void mp_event_handle_nowait(void) {
+ #if defined(MICROPY_EVENT_POLL_HOOK_FAST) && !MICROPY_PREVIEW_VERSION_2
+ // For ports still using the old macros.
+ MICROPY_EVENT_POLL_HOOK_FAST
+ #else
+ // Process any port layer (non-blocking) events.
+ MICROPY_INTERNAL_EVENT_HOOK;
+ mp_handle_pending(true);
+ #endif
+}
+
+// Handles any pending MicroPython events and then suspends execution until the
+// next interrupt or event.
+void mp_event_wait_indefinite(void) {
+ #if defined(MICROPY_EVENT_POLL_HOOK) && !MICROPY_PREVIEW_VERSION_2
+ // For ports still using the old macros.
+ MICROPY_EVENT_POLL_HOOK
+ #else
+ mp_event_handle_nowait();
+ MICROPY_INTERNAL_WFE(-1);
+ #endif
+}
+
+// Handle any pending MicroPython events and then suspends execution until the
+// next interrupt or event, or until timeout_ms milliseconds have elapsed.
+void mp_event_wait_ms(mp_uint_t timeout_ms) {
+ #if defined(MICROPY_EVENT_POLL_HOOK) && !MICROPY_PREVIEW_VERSION_2
+ // For ports still using the old macros.
+ MICROPY_EVENT_POLL_HOOK
+ #else
+ mp_event_handle_nowait();
+ MICROPY_INTERNAL_WFE(timeout_ms);
+ #endif
+}