mirror of
https://github.com/micropython/micropython.git
synced 2025-07-21 21:11:12 +02:00
py/scheduler: Only run scheduler callbacks queued before run started.
Without this change, a scheduler callback which itself queues a new callback will have that callback executed as part of the same scheduler run. Where a callback may re-queue itself, this can lead to an infinite loop. With this change, each call to mp_handle_pending() will only service the callbacks which were queued when the scheduler pass started - any callbacks added during the run are serviced on the next mp_handle_pending(). This does mean some interrupts may have higher latency (as callback is deferred until next scheduler run), but the worst-case latency should stay very similar. This work was funded through GitHub Sponsors. Signed-off-by: Angus Gratton <angus@redyak.com.au>
This commit is contained in:
committed by
Damien George
parent
b15348415e
commit
7f274c7550
@@ -88,8 +88,11 @@ static inline void mp_sched_run_pending(void) {
|
||||
|
||||
#if MICROPY_SCHEDULER_STATIC_NODES
|
||||
// Run all pending C callbacks.
|
||||
while (MP_STATE_VM(sched_head) != NULL) {
|
||||
mp_sched_node_t *node = MP_STATE_VM(sched_head);
|
||||
mp_sched_node_t *original_tail = MP_STATE_VM(sched_tail);
|
||||
if (original_tail != NULL) {
|
||||
mp_sched_node_t *node;
|
||||
do {
|
||||
node = MP_STATE_VM(sched_head);
|
||||
MP_STATE_VM(sched_head) = node->next;
|
||||
if (MP_STATE_VM(sched_head) == NULL) {
|
||||
MP_STATE_VM(sched_tail) = NULL;
|
||||
@@ -99,6 +102,7 @@ static inline void mp_sched_run_pending(void) {
|
||||
MICROPY_END_ATOMIC_SECTION(atomic_state);
|
||||
callback(node);
|
||||
atomic_state = MICROPY_BEGIN_ATOMIC_SECTION();
|
||||
} while (node != original_tail); // Don't execute any callbacks scheduled during this run
|
||||
}
|
||||
#endif
|
||||
|
||||
|
Reference in New Issue
Block a user