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>
This commit is contained in:
Damien George
2022-03-23 17:13:03 +11:00
parent d242a9b7f7
commit 75506e496f
5 changed files with 85 additions and 2 deletions

View File

@@ -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