Skip to content

Commit bc424dd

Browse files
committed
py/modthread: Move thread state initialisation to shared function.
Signed-off-by: Daniël van de Giessen <[email protected]>
1 parent 678707c commit bc424dd

File tree

3 files changed

+29
-23
lines changed

3 files changed

+29
-23
lines changed

extmod/modbluetooth.c

+1-9
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,6 @@
3434
#include "py/objarray.h"
3535
#include "py/qstr.h"
3636
#include "py/runtime.h"
37-
#include "py/stackctrl.h"
3837
#include "extmod/modbluetooth.h"
3938
#include <string.h>
4039

@@ -1272,14 +1271,7 @@ STATIC mp_obj_t invoke_irq_handler(uint16_t event,
12721271

12731272
mp_state_thread_t ts;
12741273
if (ts_orig == NULL) {
1275-
mp_thread_set_state(&ts);
1276-
mp_stack_set_top(&ts + 1); // need to include ts in root-pointer scan
1277-
mp_stack_set_limit(MICROPY_PY_BLUETOOTH_SYNC_EVENT_STACK_SIZE);
1278-
ts.gc_lock_depth = 0;
1279-
ts.nlr_jump_callback_top = NULL;
1280-
ts.mp_pending_exception = MP_OBJ_NULL;
1281-
mp_locals_set(mp_state_ctx.thread.dict_locals); // set from the outer context
1282-
mp_globals_set(mp_state_ctx.thread.dict_globals); // set from the outer context
1274+
mp_thread_init_state(&ts, MICROPY_PY_BLUETOOTH_SYNC_EVENT_STACK_SIZE, NULL, NULL);
12831275
MP_THREAD_GIL_ENTER();
12841276
}
12851277

py/modthread.c

+1-14
Original file line numberDiff line numberDiff line change
@@ -160,27 +160,14 @@ STATIC void *thread_entry(void *args_in) {
160160
thread_entry_args_t *args = (thread_entry_args_t *)args_in;
161161

162162
mp_state_thread_t ts;
163-
mp_thread_set_state(&ts);
164-
165-
mp_stack_set_top(&ts + 1); // need to include ts in root-pointer scan
166-
mp_stack_set_limit(args->stack_size);
163+
mp_thread_init_state(&ts, args->stack_size, args->dict_locals, args->dict_globals);
167164

168165
#if MICROPY_ENABLE_PYSTACK
169166
// TODO threading and pystack is not fully supported, for now just make a small stack
170167
mp_obj_t mini_pystack[128];
171168
mp_pystack_init(mini_pystack, &mini_pystack[128]);
172169
#endif
173170

174-
// The GC starts off unlocked on this thread.
175-
ts.gc_lock_depth = 0;
176-
177-
ts.nlr_jump_callback_top = NULL;
178-
ts.mp_pending_exception = MP_OBJ_NULL;
179-
180-
// set locals and globals from the calling context
181-
mp_locals_set(args->dict_locals);
182-
mp_globals_set(args->dict_globals);
183-
184171
MP_THREAD_GIL_ENTER();
185172

186173
// signal that we are set up and running

py/runtime.h

+27
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828

2929
#include "py/mpstate.h"
3030
#include "py/pystack.h"
31+
#include "py/stackctrl.h"
3132

3233
// For use with mp_call_function_1_from_nlr_jump_callback.
3334
#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) {
154155
void mp_globals_locals_set_from_nlr_jump_callback(void *ctx_in);
155156
void mp_call_function_1_from_nlr_jump_callback(void *ctx_in);
156157

158+
#if MICROPY_PY_THREAD
159+
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) {
160+
mp_thread_set_state(ts);
161+
162+
mp_stack_set_top(ts + 1); // need to include ts in root-pointer scan
163+
mp_stack_set_limit(stack_size);
164+
165+
// GC starts off unlocked
166+
ts->gc_lock_depth = 0;
167+
168+
// There are no pending jump callbacks or exceptions yet
169+
ts->nlr_jump_callback_top = NULL;
170+
ts->mp_pending_exception = MP_OBJ_NULL;
171+
172+
// If locals/globals are not given, inherit from main thread
173+
if (locals == NULL) {
174+
locals = mp_state_ctx.thread.dict_locals;
175+
}
176+
if (globals == NULL) {
177+
globals = mp_state_ctx.thread.dict_globals;
178+
}
179+
mp_locals_set(locals);
180+
mp_globals_set(globals);
181+
}
182+
#endif
183+
157184
mp_obj_t mp_load_name(qstr qst);
158185
mp_obj_t mp_load_global(qstr qst);
159186
mp_obj_t mp_load_build_class(void);

0 commit comments

Comments
 (0)