diff options
author | Damien George <damien.p.george@gmail.com> | 2020-05-27 17:53:31 +1000 |
---|---|---|
committer | Damien George <damien.p.george@gmail.com> | 2020-05-27 17:53:31 +1000 |
commit | 4bbba3060d451d4dfa147ebc793acd1cc7364e5a (patch) | |
tree | 6356018ffb5eb9630fe848ffaa97dde9a3e3c374 | |
parent | d6803067c0a15c03af0639aa02b68898a3f9cc81 (diff) |
lib/utils: Lock the scheduler when executing hard callback functions.
Otherwise scheduled functions may execute during the hard callback and then
fail if they try to allocate heap memory.
-rw-r--r-- | lib/utils/mpirq.c | 7 |
1 files changed, 5 insertions, 2 deletions
diff --git a/lib/utils/mpirq.c b/lib/utils/mpirq.c index d04fab68b..8de13b0b6 100644 --- a/lib/utils/mpirq.c +++ b/lib/utils/mpirq.c @@ -62,8 +62,10 @@ mp_irq_obj_t *mp_irq_new(const mp_irq_methods_t *methods, mp_obj_t parent) { void mp_irq_handler(mp_irq_obj_t *self) { if (self->handler != mp_const_none) { if (self->ishard) { - // When executing code within a handler we must lock the GC to prevent - // any memory allocations. + // When executing code within a handler we must lock the scheduler to + // prevent any scheduled callbacks from running, and lock the GC to + // prevent any memory allocations. + mp_sched_lock(); gc_lock(); nlr_buf_t nlr; if (nlr_push(&nlr) == 0) { @@ -77,6 +79,7 @@ void mp_irq_handler(mp_irq_obj_t *self) { mp_obj_print_exception(&mp_plat_print, MP_OBJ_FROM_PTR(nlr.ret_val)); } gc_unlock(); + mp_sched_unlock(); } else { // Schedule call to user function mp_sched_schedule(self->handler, self->parent); |