Skip to content

Commit

Permalink
py/modthread: Move thread state initialisation to shared function.
Browse files Browse the repository at this point in the history
Signed-off-by: Daniël van de Giessen <daniel@dvdgiessen.nl>
  • Loading branch information
DvdGiessen committed Feb 29, 2024
1 parent 678707c commit bc424dd
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 23 deletions.
10 changes: 1 addition & 9 deletions extmod/modbluetooth.c
Original file line number Diff line number Diff line change
Expand Up @@ -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 <string.h>

Expand Down Expand Up @@ -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();
}

Expand Down
15 changes: 1 addition & 14 deletions py/modthread.c
Original file line number Diff line number Diff line change
Expand Up @@ -160,27 +160,14 @@ 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
mp_obj_t mini_pystack[128];
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
Expand Down
27 changes: 27 additions & 0 deletions py/runtime.h
Original file line number Diff line number Diff line change
Expand Up @@ -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) \
Expand Down Expand Up @@ -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);
Expand Down

0 comments on commit bc424dd

Please sign in to comment.