aboutsummaryrefslogtreecommitdiff
path: root/py/runtime.c
diff options
context:
space:
mode:
authorDamien George <damien@micropython.org>2022-03-23 17:13:03 +1100
committerDamien George <damien@micropython.org>2022-04-14 12:31:53 +1000
commit75506e496f7545c43c4d76bac131c4e01c258fa5 (patch)
treecbd1c429156a780a559ece6f98e260f52a877f0b /py/runtime.c
parentd242a9b7f7b6d4e744889d5af6c17dfb54aed8c3 (diff)
py/scheduler: Add support for scheduling static C-based callbacks.
If MICROPY_SCHEDULER_STATIC_NODES is enabled then C code can declare a static mp_sched_node_t and schedule a callback using mp_sched_schedule_node(). In contrast to using mp_sched_schedule(), the node version will have at most one pending callback outstanding, and will always be able to schedule if there is nothing already scheduled on this node. This guarantees that the the callback will be called exactly once after it is scheduled. Signed-off-by: Damien George <damien@micropython.org>
Diffstat (limited to 'py/runtime.c')
-rw-r--r--py/runtime.c10
1 files changed, 9 insertions, 1 deletions
diff --git a/py/runtime.c b/py/runtime.c
index 2a07df664..02b866d83 100644
--- a/py/runtime.c
+++ b/py/runtime.c
@@ -65,7 +65,15 @@ void mp_init(void) {
// no pending exceptions to start with
MP_STATE_THREAD(mp_pending_exception) = MP_OBJ_NULL;
#if MICROPY_ENABLE_SCHEDULER
- MP_STATE_VM(sched_state) = MP_SCHED_IDLE;
+ #if MICROPY_SCHEDULER_STATIC_NODES
+ if (MP_STATE_VM(sched_head) == NULL) {
+ // no pending callbacks to start with
+ MP_STATE_VM(sched_state) = MP_SCHED_IDLE;
+ } else {
+ // pending callbacks are on the list, eg from before a soft reset
+ MP_STATE_VM(sched_state) = MP_SCHED_PENDING;
+ }
+ #endif
MP_STATE_VM(sched_idx) = 0;
MP_STATE_VM(sched_len) = 0;
#endif