diff --git a/ports/esp32/modsocket.c b/ports/esp32/modsocket.c index e11af60e2e1cb85c21b2e09dd0c972d3488e083c..60a028c13046d2e15a7891c00558d7094ea2ae7c 100644 --- a/ports/esp32/modsocket.c +++ b/ports/esp32/modsocket.c @@ -162,7 +162,7 @@ NORETURN static void exception_from_errno(int _errno) { } static inline void check_for_exceptions(void) { - mp_handle_pending(); + mp_handle_pending(true); } // This function mimics lwip_getaddrinfo, with added support for mDNS queries diff --git a/ports/esp32/mpconfigport.h b/ports/esp32/mpconfigport.h index 983c882ae8aca37ddea298e242102a7f50d634ec..96e47fbba7361a3cc9e0cf35de664fb958e90600 100644 --- a/ports/esp32/mpconfigport.h +++ b/ports/esp32/mpconfigport.h @@ -244,8 +244,8 @@ void *esp_native_code_commit(void*, size_t, void*); #if MICROPY_PY_THREAD #define MICROPY_EVENT_POLL_HOOK \ do { \ - extern void mp_handle_pending(void); \ - mp_handle_pending(); \ + extern void mp_handle_pending(bool); \ + mp_handle_pending(true); \ MICROPY_PY_USOCKET_EVENTS_HANDLER \ MP_THREAD_GIL_EXIT(); \ MP_THREAD_GIL_ENTER(); \ @@ -253,8 +253,8 @@ void *esp_native_code_commit(void*, size_t, void*); #else #define MICROPY_EVENT_POLL_HOOK \ do { \ - extern void mp_handle_pending(void); \ - mp_handle_pending(); \ + extern void mp_handle_pending(bool); \ + mp_handle_pending(true); \ MICROPY_PY_USOCKET_EVENTS_HANDLER \ asm("waiti 0"); \ } while (0); diff --git a/ports/esp32/mphalport.c b/ports/esp32/mphalport.c index 305e87593eeb680deb654952749723c5c604d26e..c701cb4bcbae28a324fbec1ad396d26bd7929b6d 100644 --- a/ports/esp32/mphalport.c +++ b/ports/esp32/mphalport.c @@ -157,7 +157,7 @@ void mp_hal_delay_us(uint32_t us) { if (dt + pend_overhead < us) { // we have enough time to service pending events // (don't use MICROPY_EVENT_POLL_HOOK because it also yields) - mp_handle_pending(); + mp_handle_pending(true); } } } diff --git a/ports/esp8266/esp_mphal.c b/ports/esp8266/esp_mphal.c index 25201425b3201d35e026810b472ba2678679bcbf..d840cf0408b5e4d44a4492f743ab04f1d0e0c94e 100644 --- a/ports/esp8266/esp_mphal.c +++ b/ports/esp8266/esp_mphal.c @@ -144,7 +144,7 @@ void mp_hal_delay_ms(uint32_t delay) { void ets_event_poll(void) { ets_loop_iter(); - mp_handle_pending(); + mp_handle_pending(true); } void __assert_func(const char *file, int line, const char *func, const char *expr) { diff --git a/ports/javascript/mpconfigport.h b/ports/javascript/mpconfigport.h index 8ea43d84ce38a2da2ef30988f9043780bf377147..bc4e053430ff77729e478765ed175b4afbc8492c 100644 --- a/ports/javascript/mpconfigport.h +++ b/ports/javascript/mpconfigport.h @@ -135,8 +135,8 @@ extern const struct _mp_obj_module_t mp_module_utime; #if MICROPY_PY_THREAD #define MICROPY_EVENT_POLL_HOOK \ do { \ - extern void mp_handle_pending(void); \ - mp_handle_pending(); \ + extern void mp_handle_pending(bool); \ + mp_handle_pending(true); \ if (pyb_thread_enabled) { \ MP_THREAD_GIL_EXIT(); \ pyb_thread_yield(); \ @@ -149,8 +149,8 @@ extern const struct _mp_obj_module_t mp_module_utime; #else #define MICROPY_EVENT_POLL_HOOK \ do { \ - extern void mp_handle_pending(void); \ - mp_handle_pending(); \ + extern void mp_handle_pending(bool); \ + mp_handle_pending(true); \ } while (0); #define MICROPY_THREAD_YIELD() diff --git a/ports/samd/mpconfigport.h b/ports/samd/mpconfigport.h index 5b81d96b3ac820ae552bdcaf5a96bec000182bf0..d77b9d56bde81b0a247c53d6c60e98f91887e10d 100644 --- a/ports/samd/mpconfigport.h +++ b/ports/samd/mpconfigport.h @@ -92,8 +92,8 @@ extern const struct _mp_obj_module_t mp_module_utime; #define MICROPY_EVENT_POLL_HOOK \ do { \ - extern void mp_handle_pending(void); \ - mp_handle_pending(); \ + extern void mp_handle_pending(bool); \ + mp_handle_pending(true); \ __WFI(); \ } while (0); diff --git a/ports/stm32/mpconfigport.h b/ports/stm32/mpconfigport.h index 2e4fc04061359481658e762e22ec125f9ccd06bf..90e3875547f9126f28857721e9d5050466f1365e 100644 --- a/ports/stm32/mpconfigport.h +++ b/ports/stm32/mpconfigport.h @@ -343,8 +343,8 @@ static inline mp_uint_t disable_irq(void) { #if MICROPY_PY_THREAD #define MICROPY_EVENT_POLL_HOOK \ do { \ - extern void mp_handle_pending(void); \ - mp_handle_pending(); \ + extern void mp_handle_pending(bool); \ + mp_handle_pending(true); \ if (pyb_thread_enabled) { \ MP_THREAD_GIL_EXIT(); \ pyb_thread_yield(); \ @@ -358,8 +358,8 @@ static inline mp_uint_t disable_irq(void) { #else #define MICROPY_EVENT_POLL_HOOK \ do { \ - extern void mp_handle_pending(void); \ - mp_handle_pending(); \ + extern void mp_handle_pending(bool); \ + mp_handle_pending(true); \ __WFI(); \ } while (0); diff --git a/ports/unix/coverage.c b/ports/unix/coverage.c index 1b6132cddddc587391ccefe0afdb3eadfa043743..0703d0a3332ee784e825108f29fa74ddbfc137cc 100644 --- a/ports/unix/coverage.c +++ b/ports/unix/coverage.c @@ -466,7 +466,7 @@ STATIC mp_obj_t extra_coverage(void) { mp_sched_unlock(); // shouldn't do anything while scheduler is locked - mp_handle_pending(); + mp_handle_pending(true); // unlock scheduler mp_sched_unlock(); @@ -474,7 +474,7 @@ STATIC mp_obj_t extra_coverage(void) { // drain pending callbacks while (mp_sched_num_pending()) { - mp_handle_pending(); + mp_handle_pending(true); } } diff --git a/ports/unix/modtime.c b/ports/unix/modtime.c index 479d2a79dd904c3551dc2f3ae936a978065b140b..4ea8cb62c75ec714d56ce153a4ac04e6cf828795 100644 --- a/ports/unix/modtime.c +++ b/ports/unix/modtime.c @@ -109,7 +109,7 @@ STATIC mp_obj_t mod_time_sleep(mp_obj_t arg) { if (res != -1 || errno != EINTR) { break; } - mp_handle_pending(); + mp_handle_pending(true); //printf("select: EINTR: %ld:%ld\n", tv.tv_sec, tv.tv_usec); #else break; diff --git a/py/runtime.h b/py/runtime.h index b54b17b683951c0fbe1df62765041a1a8b0e412d..1c078ae81a09160d364ffcec215881ebd226f5bb 100644 --- a/py/runtime.h +++ b/py/runtime.h @@ -64,7 +64,7 @@ extern const byte mp_binary_op_method_name[]; void mp_init(void); void mp_deinit(void); -void mp_handle_pending(void); +void mp_handle_pending(bool raise_exc); void mp_handle_pending_tail(mp_uint_t atomic_state); #if MICROPY_ENABLE_SCHEDULER diff --git a/py/scheduler.c b/py/scheduler.c index e7cbb524de7d339b049cb885d516b98eb3fe4754..ff88be9583a674ef2b90e9229eb87d8519110c65 100644 --- a/py/scheduler.c +++ b/py/scheduler.c @@ -44,7 +44,7 @@ static inline bool mp_sched_empty(void) { } // A variant of this is inlined in the VM at the pending exception check -void mp_handle_pending(void) { +void mp_handle_pending(bool raise_exc) { if (MP_STATE_VM(sched_state) == MP_SCHED_PENDING) { mp_uint_t atomic_state = MICROPY_BEGIN_ATOMIC_SECTION(); mp_obj_t obj = MP_STATE_VM(mp_pending_exception); @@ -53,8 +53,10 @@ void mp_handle_pending(void) { if (!mp_sched_num_pending()) { MP_STATE_VM(sched_state) = MP_SCHED_IDLE; } - MICROPY_END_ATOMIC_SECTION(atomic_state); - nlr_raise(obj); + if (raise_exc) { + MICROPY_END_ATOMIC_SECTION(atomic_state); + nlr_raise(obj); + } } mp_handle_pending_tail(atomic_state); } @@ -121,11 +123,13 @@ bool mp_sched_schedule(mp_obj_t function, mp_obj_t arg) { #else // MICROPY_ENABLE_SCHEDULER // A variant of this is inlined in the VM at the pending exception check -void mp_handle_pending(void) { +void mp_handle_pending(bool raise_exc) { if (MP_STATE_VM(mp_pending_exception) != MP_OBJ_NULL) { mp_obj_t obj = MP_STATE_VM(mp_pending_exception); MP_STATE_VM(mp_pending_exception) = MP_OBJ_NULL; - nlr_raise(obj); + if (raise_exc) { + nlr_raise(obj); + } } }