diff --git a/extmod/modbluetooth.c b/extmod/modbluetooth.c index 32a15bf511..91e4e62213 100644 --- a/extmod/modbluetooth.c +++ b/extmod/modbluetooth.c @@ -34,7 +34,6 @@ #include "py/objarray.h" #include "py/qstr.h" #include "py/runtime.h" -#include "py/stackctrl.h" #include "extmod/modbluetooth.h" #include @@ -1272,14 +1271,7 @@ STATIC mp_obj_t invoke_irq_handler(uint16_t event, mp_state_thread_t ts; if (ts_orig == NULL) { - mp_thread_set_state(&ts); - mp_stack_set_top(&ts + 1); // need to include ts in root-pointer scan - mp_stack_set_limit(MICROPY_PY_BLUETOOTH_SYNC_EVENT_STACK_SIZE); - ts.gc_lock_depth = 0; - ts.nlr_jump_callback_top = NULL; - ts.mp_pending_exception = MP_OBJ_NULL; - mp_locals_set(mp_state_ctx.thread.dict_locals); // set from the outer context - mp_globals_set(mp_state_ctx.thread.dict_globals); // set from the outer context + mp_thread_init_state(&ts, MICROPY_PY_BLUETOOTH_SYNC_EVENT_STACK_SIZE, NULL, NULL); MP_THREAD_GIL_ENTER(); } diff --git a/py/modthread.c b/py/modthread.c index 3a8a1e03ce..9fc20570be 100644 --- a/py/modthread.c +++ b/py/modthread.c @@ -160,10 +160,7 @@ STATIC void *thread_entry(void *args_in) { thread_entry_args_t *args = (thread_entry_args_t *)args_in; mp_state_thread_t ts; - mp_thread_set_state(&ts); - - mp_stack_set_top(&ts + 1); // need to include ts in root-pointer scan - mp_stack_set_limit(args->stack_size); + mp_thread_init_state(&ts, args->stack_size, args->dict_locals, args->dict_globals); #if MICROPY_ENABLE_PYSTACK // TODO threading and pystack is not fully supported, for now just make a small stack @@ -171,16 +168,6 @@ STATIC void *thread_entry(void *args_in) { mp_pystack_init(mini_pystack, &mini_pystack[128]); #endif - // The GC starts off unlocked on this thread. - ts.gc_lock_depth = 0; - - ts.nlr_jump_callback_top = NULL; - ts.mp_pending_exception = MP_OBJ_NULL; - - // set locals and globals from the calling context - mp_locals_set(args->dict_locals); - mp_globals_set(args->dict_globals); - MP_THREAD_GIL_ENTER(); // signal that we are set up and running diff --git a/py/runtime.h b/py/runtime.h index a04d4584f8..5465c06d8a 100644 --- a/py/runtime.h +++ b/py/runtime.h @@ -28,6 +28,7 @@ #include "py/mpstate.h" #include "py/pystack.h" +#include "py/stackctrl.h" // For use with mp_call_function_1_from_nlr_jump_callback. #define MP_DEFINE_NLR_JUMP_CALLBACK_FUNCTION_1(ctx, f, a) \ @@ -154,6 +155,32 @@ static inline void mp_globals_set(mp_obj_dict_t *d) { void mp_globals_locals_set_from_nlr_jump_callback(void *ctx_in); void mp_call_function_1_from_nlr_jump_callback(void *ctx_in); +#if MICROPY_PY_THREAD +static inline void mp_thread_init_state(mp_state_thread_t *ts, size_t stack_size, mp_obj_dict_t *locals, mp_obj_dict_t *globals) { + mp_thread_set_state(ts); + + mp_stack_set_top(ts + 1); // need to include ts in root-pointer scan + mp_stack_set_limit(stack_size); + + // GC starts off unlocked + ts->gc_lock_depth = 0; + + // There are no pending jump callbacks or exceptions yet + ts->nlr_jump_callback_top = NULL; + ts->mp_pending_exception = MP_OBJ_NULL; + + // If locals/globals are not given, inherit from main thread + if (locals == NULL) { + locals = mp_state_ctx.thread.dict_locals; + } + if (globals == NULL) { + globals = mp_state_ctx.thread.dict_globals; + } + mp_locals_set(locals); + mp_globals_set(globals); +} +#endif + mp_obj_t mp_load_name(qstr qst); mp_obj_t mp_load_global(qstr qst); mp_obj_t mp_load_build_class(void);