Skip to content

Commit ca920f7

Browse files
dlechdpgeorge
authored andcommitted
py/mpstate: Make exceptions thread-local.
This moves mp_pending_exception from mp_state_vm_t to mp_state_thread_t. This allows exceptions to be scheduled on a specific thread. Signed-off-by: David Lechner <[email protected]>
1 parent 7c51cb2 commit ca920f7

File tree

13 files changed

+27
-26
lines changed

13 files changed

+27
-26
lines changed

ports/esp8266/modnetwork.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -237,7 +237,7 @@ STATIC mp_obj_t esp_scan(mp_obj_t self_in) {
237237
while (esp_scan_list != NULL) {
238238
// our esp_scan_cb is called via ets_loop_iter so it's safe to set the
239239
// esp_scan_list variable to NULL without disabling interrupts
240-
if (MP_STATE_VM(mp_pending_exception) != NULL) {
240+
if (MP_STATE_THREAD(mp_pending_exception) != NULL) {
241241
esp_scan_list = NULL;
242242
mp_handle_pending(true);
243243
}

ports/nrf/boards/microbit/modules/microbitdisplay.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -149,7 +149,7 @@ STATIC void async_stop(void) {
149149
STATIC void wait_for_event(void) {
150150
while (!wakeup_event) {
151151
// allow CTRL-C to stop the animation
152-
if (MP_STATE_VM(mp_pending_exception) != MP_OBJ_NULL) {
152+
if (MP_STATE_THREAD(mp_pending_exception) != MP_OBJ_NULL) {
153153
async_stop();
154154
return;
155155
}

ports/nrf/modules/music/modmusic.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,7 @@ STATIC void wait_async_music_idle(void) {
140140
// wait for the async music state to become idle
141141
while (music_data->async_state != ASYNC_MUSIC_STATE_IDLE) {
142142
// allow CTRL-C to stop the music
143-
if (MP_STATE_VM(mp_pending_exception) != MP_OBJ_NULL) {
143+
if (MP_STATE_THREAD(mp_pending_exception) != MP_OBJ_NULL) {
144144
music_data->async_state = ASYNC_MUSIC_STATE_IDLE;
145145
pwm_set_duty_cycle(music_data->async_pin->pin, 0); // TODO: remove pin setting.
146146
break;

ports/pic16bit/board.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,7 @@ int switch_get(int sw) {
140140
// TODO need an irq
141141
void uart_rx_irq(void) {
142142
if (c == interrupt_char) {
143-
MP_STATE_VM(mp_pending_exception) = MP_STATE_PORT(keyboard_interrupt_obj);
143+
MP_STATE_THREAD(mp_pending_exception) = MP_STATE_PORT(keyboard_interrupt_obj);
144144
}
145145
}
146146
*/

ports/stm32/pendsv.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,10 +60,10 @@ void pendsv_init(void) {
6060
// the given exception object using nlr_jump in the context of the top-level
6161
// thread.
6262
void pendsv_kbd_intr(void) {
63-
if (MP_STATE_VM(mp_pending_exception) == MP_OBJ_NULL) {
63+
if (MP_STATE_THREAD(mp_pending_exception) == MP_OBJ_NULL) {
6464
mp_sched_keyboard_interrupt();
6565
} else {
66-
MP_STATE_VM(mp_pending_exception) = MP_OBJ_NULL;
66+
MP_STATE_THREAD(mp_pending_exception) = MP_OBJ_NULL;
6767
pendsv_object = &MP_STATE_VM(mp_kbd_exception);
6868
SCB->ICSR = SCB_ICSR_PENDSVSET_Msk;
6969
}

ports/unix/unix_mphal.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ STATIC void sighandler(int signum) {
5454
sigprocmask(SIG_SETMASK, &mask, NULL);
5555
nlr_raise(MP_OBJ_FROM_PTR(&MP_STATE_VM(mp_kbd_exception)));
5656
#else
57-
if (MP_STATE_VM(mp_pending_exception) == MP_OBJ_FROM_PTR(&MP_STATE_VM(mp_kbd_exception))) {
57+
if (MP_STATE_THREAD(mp_pending_exception) == MP_OBJ_FROM_PTR(&MP_STATE_VM(mp_kbd_exception))) {
5858
// this is the second time we are called, so die straight away
5959
exit(1);
6060
}

ports/windows/windows_mphal.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ void mp_hal_stdio_mode_orig(void) {
8080
// the thread created for handling it might not be running yet so we'd miss the notification.
8181
BOOL WINAPI console_sighandler(DWORD evt) {
8282
if (evt == CTRL_C_EVENT) {
83-
if (MP_STATE_VM(mp_pending_exception) == MP_OBJ_FROM_PTR(&MP_STATE_VM(mp_kbd_exception))) {
83+
if (MP_STATE_THREAD(mp_pending_exception) == MP_OBJ_FROM_PTR(&MP_STATE_VM(mp_kbd_exception))) {
8484
// this is the second time we are called, so die straight away
8585
exit(1);
8686
}

py/modthread.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -174,6 +174,8 @@ STATIC void *thread_entry(void *args_in) {
174174
// The GC starts off unlocked on this thread.
175175
ts.gc_lock_depth = 0;
176176

177+
ts.mp_pending_exception = MP_OBJ_NULL;
178+
177179
// set locals and globals from the calling context
178180
mp_locals_set(args->dict_locals);
179181
mp_globals_set(args->dict_globals);
@@ -184,7 +186,6 @@ STATIC void *thread_entry(void *args_in) {
184186
mp_thread_start();
185187

186188
// TODO set more thread-specific state here:
187-
// mp_pending_exception? (root pointer)
188189
// cur_exception (root pointer)
189190

190191
DEBUG_printf("[thread] start ts=%p args=%p stack=%p\n", &ts, &args, MP_STATE_THREAD(stack_top));

py/mpstate.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -137,9 +137,6 @@ typedef struct _mp_state_vm_t {
137137
// dictionary with loaded modules (may be exposed as sys.modules)
138138
mp_obj_dict_t mp_loaded_modules_dict;
139139

140-
// pending exception object (MP_OBJ_NULL if not pending)
141-
volatile mp_obj_t mp_pending_exception;
142-
143140
#if MICROPY_ENABLE_SCHEDULER
144141
mp_sched_item_t sched_queue[MICROPY_SCHEDULER_DEPTH];
145142
#endif
@@ -266,6 +263,9 @@ typedef struct _mp_state_thread_t {
266263

267264
nlr_buf_t *nlr_top;
268265

266+
// pending exception object (MP_OBJ_NULL if not pending)
267+
volatile mp_obj_t mp_pending_exception;
268+
269269
#if MICROPY_PY_SYS_SETTRACE
270270
mp_obj_t prof_trace_callback;
271271
bool prof_callback_is_executing;

py/profile.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -297,7 +297,7 @@ STATIC mp_obj_t mp_prof_callback_invoke(mp_obj_t callback, prof_callback_args_t
297297

298298
mp_prof_is_executing = false;
299299

300-
if (MP_STATE_VM(mp_pending_exception) != MP_OBJ_NULL) {
300+
if (MP_STATE_THREAD(mp_pending_exception) != MP_OBJ_NULL) {
301301
mp_handle_pending(true);
302302
}
303303
return top;

0 commit comments

Comments
 (0)