Skip to content

Commit

Permalink
ports/all: Run atexit during soft-reset.
Browse files Browse the repository at this point in the history
Signed-off-by: Andrew Leech <[email protected]>
  • Loading branch information
pi-anl authored and andrewleech committed Oct 24, 2024
1 parent a952fe1 commit fb36eab
Show file tree
Hide file tree
Showing 11 changed files with 48 additions and 4 deletions.
5 changes: 5 additions & 0 deletions ports/esp32/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@
#include "py/repl.h"
#include "py/gc.h"
#include "py/mphal.h"
#include "py/objmodule.h"
#include "shared/readline/readline.h"
#include "shared/runtime/pyexec.h"
#include "shared/timeutils/timeutils.h"
Expand Down Expand Up @@ -182,6 +183,10 @@ void mp_task(void *pvParameter) {
mp_thread_deinit();
#endif

#if MICROPY_PY_SYS_ATEXIT
mp_sys_atexit_execute();
#endif

gc_sweep_all();

// Free any native code pointers that point to iRAM.
Expand Down
4 changes: 4 additions & 0 deletions ports/esp8266/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
#include "py/mperrno.h"
#include "py/mphal.h"
#include "py/gc.h"
#include "py/objmodule.h"

#include "extmod/misc.h"
#include "extmod/modmachine.h"
Expand Down Expand Up @@ -88,6 +89,9 @@ static void mp_reset(void) {
}

void soft_reset(void) {
#if MICROPY_PY_SYS_ATEXIT
mp_sys_atexit_execute();
#endif
gc_sweep_all();
mp_hal_stdout_tx_str("MPY: soft reboot\r\n");
mp_hal_delay_us(10000); // allow UART to flush output
Expand Down
4 changes: 4 additions & 0 deletions ports/mimxrt/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
#include "py/gc.h"
#include "py/mperrno.h"
#include "py/stackctrl.h"
#include "py/objmodule.h"
#include "shared/readline/readline.h"
#include "shared/runtime/gchelper.h"
#include "shared/runtime/pyexec.h"
Expand Down Expand Up @@ -159,6 +160,9 @@ int main(void) {
machine_uart_deinit_all();
machine_pwm_deinit_all();
soft_timer_deinit();
#if MICROPY_PY_SYS_ATEXIT
mp_sys_atexit_execute();
#endif
gc_sweep_all();
mp_deinit();
}
Expand Down
6 changes: 6 additions & 0 deletions ports/nrf/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
#include "py/lexer.h"
#include "py/parse.h"
#include "py/obj.h"
#include "py/objmodule.h"
#include "py/runtime.h"
#include "py/stackctrl.h"
#include "py/gc.h"
Expand Down Expand Up @@ -292,6 +293,11 @@ void NORETURN _start(void) {
pwm_deinit_all();
#endif

#if MICROPY_PY_SYS_ATEXIT
mp_sys_atexit_execute();
#endif

gc_sweep_all();
mp_deinit();

printf("MPY: soft reboot\n");
Expand Down
4 changes: 4 additions & 0 deletions ports/qemu/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
#include "py/stackctrl.h"
#include "py/gc.h"
#include "py/mperrno.h"
#include "py/objmodule.h"
#include "shared/runtime/gchelper.h"
#include "shared/runtime/pyexec.h"

Expand Down Expand Up @@ -60,6 +61,9 @@ int main(int argc, char **argv) {

mp_printf(&mp_plat_print, "MPY: soft reboot\n");

#if MICROPY_PY_SYS_ATEXIT
mp_sys_atexit_execute();
#endif
gc_sweep_all();
mp_deinit();
}
Expand Down
5 changes: 5 additions & 0 deletions ports/renesas-ra/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
#include "py/gc.h"
#include "py/mperrno.h"
#include "py/mphal.h"
#include "py/objmodule.h"
#include "shared/readline/readline.h"
#include "shared/runtime/pyexec.h"
#include "shared/runtime/softtimer.h"
Expand Down Expand Up @@ -435,6 +436,10 @@ int main(void) {
pyb_thread_deinit();
#endif

#if MICROPY_PY_SYS_ATEXIT
mp_sys_atexit_execute();
#endif

MICROPY_BOARD_END_SOFT_RESET(&state);

gc_sweep_all();
Expand Down
4 changes: 4 additions & 0 deletions ports/rp2/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
#include "py/gc.h"
#include "py/mperrno.h"
#include "py/mphal.h"
#include "py/objmodule.h"
#include "extmod/modbluetooth.h"
#include "extmod/modnetwork.h"
#include "shared/readline/readline.h"
Expand Down Expand Up @@ -241,6 +242,9 @@ int main(int argc, char **argv) {
#if MICROPY_HW_ENABLE_USB_RUNTIME_DEVICE
mp_usbd_deinit();
#endif
#if MICROPY_PY_SYS_ATEXIT
mp_sys_atexit_execute();
#endif

// Hook for resetting anything right at the end of a soft reset command.
MICROPY_BOARD_END_SOFT_RESET();
Expand Down
4 changes: 4 additions & 0 deletions ports/samd/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
#include "py/gc.h"
#include "py/mperrno.h"
#include "py/stackctrl.h"
#include "py/objmodule.h"
#include "shared/readline/readline.h"
#include "shared/runtime/gchelper.h"
#include "shared/runtime/pyexec.h"
Expand Down Expand Up @@ -98,6 +99,9 @@ void samd_main(void) {
#if MICROPY_HW_ENABLE_USB_RUNTIME_DEVICE
mp_usbd_deinit();
#endif
#if MICROPY_PY_SYS_ATEXIT
mp_sys_atexit_execute();
#endif
gc_sweep_all();
#if MICROPY_PY_MACHINE_I2C || MICROPY_PY_MACHINE_SPI || MICROPY_PY_MACHINE_UART
sercom_deinit_all();
Expand Down
5 changes: 5 additions & 0 deletions ports/stm32/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
#include "py/gc.h"
#include "py/mperrno.h"
#include "py/mphal.h"
#include "py/objmodule.h"
#include "shared/readline/readline.h"
#include "shared/runtime/pyexec.h"
#include "shared/runtime/softtimer.h"
Expand Down Expand Up @@ -698,6 +699,10 @@ void stm32_main(uint32_t reset_mode) {
MP_STATE_PORT(pyb_stdio_uart) = NULL;
#endif

#if MICROPY_PY_SYS_ATEXIT
mp_sys_atexit_execute();
#endif

MICROPY_BOARD_END_SOFT_RESET(&state);

gc_sweep_all();
Expand Down
6 changes: 2 additions & 4 deletions ports/unix/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
#include "py/builtin.h"
#include "py/repl.h"
#include "py/gc.h"
#include "py/objmodule.h"
#include "py/objstr.h"
#include "py/cstack.h"
#include "py/mphal.h"
Expand Down Expand Up @@ -753,10 +754,7 @@ MP_NOINLINE int main_(int argc, char **argv) {
#endif

#if MICROPY_PY_SYS_ATEXIT
// Beware, the sys.settrace callback should be disabled before running sys.atexit.
if (mp_obj_is_callable(MP_STATE_VM(sys_exitfunc))) {
mp_call_function_0(MP_STATE_VM(sys_exitfunc));
}
mp_sys_atexit_execute();
#endif

#if MICROPY_PY_MICROPYTHON_MEM_INFO
Expand Down
5 changes: 5 additions & 0 deletions ports/zephyr/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@
#include "py/gc.h"
#include "py/mphal.h"
#include "py/stackctrl.h"
#include "py/objmodule.h"
#include "shared/runtime/gchelper.h"
#include "shared/runtime/pyexec.h"
#include "shared/readline/readline.h"
Expand Down Expand Up @@ -172,6 +173,10 @@ int real_main(void) {
gc_collect();
#endif

#if MICROPY_PY_SYS_ATEXIT
mp_sys_atexit_execute();
#endif

gc_sweep_all();
mp_deinit();

Expand Down

0 comments on commit fb36eab

Please sign in to comment.