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.
This commit is contained in:
@@ -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) {
|
void mp_irq_handler(mp_irq_obj_t *self) {
|
||||||
if (self->handler != mp_const_none) {
|
if (self->handler != mp_const_none) {
|
||||||
if (self->ishard) {
|
if (self->ishard) {
|
||||||
// When executing code within a handler we must lock the GC to prevent
|
// When executing code within a handler we must lock the scheduler to
|
||||||
// any memory allocations.
|
// prevent any scheduled callbacks from running, and lock the GC to
|
||||||
|
// prevent any memory allocations.
|
||||||
|
mp_sched_lock();
|
||||||
gc_lock();
|
gc_lock();
|
||||||
nlr_buf_t nlr;
|
nlr_buf_t nlr;
|
||||||
if (nlr_push(&nlr) == 0) {
|
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));
|
mp_obj_print_exception(&mp_plat_print, MP_OBJ_FROM_PTR(nlr.ret_val));
|
||||||
}
|
}
|
||||||
gc_unlock();
|
gc_unlock();
|
||||||
|
mp_sched_unlock();
|
||||||
} else {
|
} else {
|
||||||
// Schedule call to user function
|
// Schedule call to user function
|
||||||
mp_sched_schedule(self->handler, self->parent);
|
mp_sched_schedule(self->handler, self->parent);
|
||||||
|
|||||||
Reference in New Issue
Block a user