diff options
author | iabdalkader <i.abdalkader@gmail.com> | 2025-09-25 16:28:38 +0200 |
---|---|---|
committer | Damien George <damien@micropython.org> | 2025-10-03 00:52:29 +1000 |
commit | c57aebf790c40125b663231ec4307d2a3f3cf193 (patch) | |
tree | 77224fb0830b2d5ecd2c5dca90d6cb3986ad202e /py/scheduler.c | |
parent | c91e091ad72e3e15ca8981bd953f64714a5afb3e (diff) |
py/scheduler: Allow selective handling in mp_handle_pending.
Extend mp_handle_pending to support three distinct behaviors via
mp_handle_pending_internal():
- MP_HANDLE_PENDING_CALLBACKS_ONLY: process callbacks only
- MP_HANDLE_PENDING_CALLBACKS_AND_EXCEPTIONS: callbacks + raise exceptions
- MP_HANDLE_PENDING_CALLBACKS_AND_CLEAR_EXCEPTIONS: callbacks + clear only
Original mp_handle_pending(bool) preserved as inline wrapper for
backward compatibility.
Signed-off-by: iabdalkader <i.abdalkader@gmail.com>
Diffstat (limited to 'py/scheduler.c')
-rw-r--r-- | py/scheduler.c | 13 |
1 files changed, 8 insertions, 5 deletions
diff --git a/py/scheduler.c b/py/scheduler.c index d4cdb59ef..5355074a7 100644 --- a/py/scheduler.c +++ b/py/scheduler.c @@ -213,24 +213,27 @@ MP_REGISTER_ROOT_POINTER(mp_sched_item_t sched_queue[MICROPY_SCHEDULER_DEPTH]); // Called periodically from the VM or from "waiting" code (e.g. sleep) to // process background tasks and pending exceptions (e.g. KeyboardInterrupt). -void mp_handle_pending(bool raise_exc) { +void mp_handle_pending_internal(mp_handle_pending_behaviour_t behavior) { + bool handle_exceptions = (behavior != MP_HANDLE_PENDING_CALLBACKS_ONLY); + bool raise_exceptions = (behavior == MP_HANDLE_PENDING_CALLBACKS_AND_EXCEPTIONS); + // Handle pending VM abort. #if MICROPY_ENABLE_VM_ABORT - if (MP_STATE_VM(vm_abort) && mp_thread_is_main_thread()) { + if (handle_exceptions && MP_STATE_VM(vm_abort) && mp_thread_is_main_thread()) { MP_STATE_VM(vm_abort) = false; - if (raise_exc && nlr_get_abort() != NULL) { + if (raise_exceptions && nlr_get_abort() != NULL) { nlr_jump_abort(); } } #endif // Handle any pending exception. - if (MP_STATE_THREAD(mp_pending_exception) != MP_OBJ_NULL) { + if (handle_exceptions && MP_STATE_THREAD(mp_pending_exception) != MP_OBJ_NULL) { mp_uint_t atomic_state = MICROPY_BEGIN_ATOMIC_SECTION(); mp_obj_t obj = MP_STATE_THREAD(mp_pending_exception); if (obj != MP_OBJ_NULL) { MP_STATE_THREAD(mp_pending_exception) = MP_OBJ_NULL; - if (raise_exc) { + if (raise_exceptions) { MICROPY_END_ATOMIC_SECTION(atomic_state); nlr_raise(obj); } |