Skip to content

Commit 05fe66f

Browse files
committed
py: Move locals/globals dicts to the thread-specific state.
Each threads needs to have its own private references to its current locals/globals dicts, otherwise functions running within different contexts (eg imported from different files) can behave very strangely.
1 parent fe866d9 commit 05fe66f

4 files changed

Lines changed: 18 additions & 11 deletions

File tree

py/modthread.c

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,8 @@ STATIC mp_obj_t mod_thread_stack_size(size_t n_args, const mp_obj_t *args) {
143143
STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(mod_thread_stack_size_obj, 0, 1, mod_thread_stack_size);
144144

145145
typedef struct _thread_entry_args_t {
146+
mp_obj_dict_t *dict_locals;
147+
mp_obj_dict_t *dict_globals;
146148
size_t stack_size;
147149
mp_obj_t fun;
148150
size_t n_args;
@@ -161,6 +163,10 @@ STATIC void *thread_entry(void *args_in) {
161163
mp_stack_set_top(&ts + 1); // need to include ts in root-pointer scan
162164
mp_stack_set_limit(args->stack_size);
163165

166+
// set locals and globals from the calling context
167+
mp_locals_set(args->dict_locals);
168+
mp_globals_set(args->dict_globals);
169+
164170
MP_THREAD_GIL_ENTER();
165171

166172
// signal that we are set up and running
@@ -169,7 +175,6 @@ STATIC void *thread_entry(void *args_in) {
169175
// TODO set more thread-specific state here:
170176
// mp_pending_exception? (root pointer)
171177
// cur_exception (root pointer)
172-
// dict_locals? (root pointer) uPy doesn't make a new locals dict for functions, just for classes, so it's different to CPy
173178

174179
DEBUG_printf("[thread] start ts=%p args=%p stack=%p\n", &ts, &args, MP_STATE_THREAD(stack_top));
175180

@@ -240,6 +245,10 @@ STATIC mp_obj_t mod_thread_start_new_thread(size_t n_args, const mp_obj_t *args)
240245
th_args->n_args = pos_args_len;
241246
memcpy(th_args->args, pos_args_items, pos_args_len * sizeof(mp_obj_t));
242247

248+
// pass our locals and globals into the new thread
249+
th_args->dict_locals = mp_locals_get();
250+
th_args->dict_globals = mp_globals_get();
251+
243252
// set the stack size to use
244253
th_args->stack_size = thread_stack_size;
245254

py/mpstate.h

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -196,35 +196,33 @@ typedef struct _mp_state_vm_t {
196196
// This structure holds state that is specific to a given thread.
197197
// Everything in this structure is scanned for root pointers.
198198
typedef struct _mp_state_thread_t {
199+
mp_obj_dict_t *dict_locals;
200+
mp_obj_dict_t *dict_globals;
201+
199202
// Note: nlr asm code has the offset of this hard-coded
200203
nlr_buf_t *nlr_top; // ROOT POINTER
201204

202205
// Stack top at the start of program
203-
// Note: this entry is used to locate the end of the root pointer section.
204206
char *stack_top;
205207

206208
#if MICROPY_STACK_CHECK
207209
size_t stack_limit;
208210
#endif
209211
} mp_state_thread_t;
210212

211-
// This structure combines the above 3 structures, and adds the local
212-
// and global dicts.
213+
// This structure combines the above 3 structures.
214+
// The order of the entries are important for root pointer scanning in the GC to work.
213215
// Note: if this structure changes then revisit all nlr asm code since they
214216
// have the offset of nlr_top hard-coded.
215217
typedef struct _mp_state_ctx_t {
216-
// these must come first for root pointer scanning in GC to work
217-
mp_obj_dict_t *dict_locals;
218-
mp_obj_dict_t *dict_globals;
219-
// these must come next in this order for root pointer scanning in GC to work
220218
mp_state_thread_t thread;
221219
mp_state_vm_t vm;
222220
mp_state_mem_t mem;
223221
} mp_state_ctx_t;
224222

225223
extern mp_state_ctx_t mp_state_ctx;
226224

227-
#define MP_STATE_CTX(x) (mp_state_ctx.x)
225+
#define MP_STATE_CTX(x) MP_STATE_THREAD(x)
228226
#define MP_STATE_VM(x) (mp_state_ctx.vm.x)
229227
#define MP_STATE_MEM(x) (mp_state_ctx.mem.x)
230228

py/nlrx64.S

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@
4444
#endif
4545

4646
// offset of nlr_top within mp_state_thread_t structure
47-
#define NLR_TOP_TH_OFF (0)
47+
#define NLR_TOP_TH_OFF (2 * 8)
4848

4949
#if defined(_WIN32) || defined(__CYGWIN__)
5050
#define NLR_OS_WINDOWS

py/nlrx86.S

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@
5252
#endif
5353

5454
// offset of nlr_top within mp_state_thread_t structure
55-
#define NLR_TOP_TH_OFF (0)
55+
#define NLR_TOP_TH_OFF (2 * 4)
5656

5757
.file "nlr.s"
5858
.text

0 commit comments

Comments
 (0)