summaryrefslogtreecommitdiff
path: root/py/runtime.h
diff options
context:
space:
mode:
authorDaniël van de Giessen <daniel@dvdgiessen.nl>2024-02-28 12:41:41 +0100
committerDaniël van de Giessen <daniel@dvdgiessen.nl>2024-02-29 14:28:58 +0100
commitbc424ddc4165ffd8203b4e781f7c686598601e15 (patch)
tree2cd881caccdb3605fa4f68d71e23d4d820c66b47 /py/runtime.h
parent678707c8b07323c5b914778708a2858387c3b60c (diff)
py/modthread: Move thread state initialisation to shared function.
Signed-off-by: Daniël van de Giessen <daniel@dvdgiessen.nl>
Diffstat (limited to 'py/runtime.h')
-rw-r--r--py/runtime.h27
1 files changed, 27 insertions, 0 deletions
diff --git a/py/runtime.h b/py/runtime.h
index a04d4584f..5465c06d8 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);