py/modthread: Move thread state initialisation to shared function.

Signed-off-by: Daniël van de Giessen <daniel@dvdgiessen.nl>
This commit is contained in:
Daniël van de Giessen
2024-02-28 12:41:41 +01:00
parent 678707c8b0
commit bc424ddc41
3 changed files with 29 additions and 23 deletions

View File

@@ -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);