summaryrefslogtreecommitdiffstatshomepage
path: root/py/modthread.c
diff options
context:
space:
mode:
authorDamien George <damien.p.george@gmail.com>2017-02-27 23:56:46 +1100
committerDamien George <damien.p.george@gmail.com>2017-03-06 17:01:56 +1100
commit05fe66f68a1cf1b7587c55149472ab7bca843631 (patch)
treee2e38a0124db195eb8ae011cd05f2a7316c9b06e /py/modthread.c
parentfe866d996f54df0c5687c94b98abff5410ef02c9 (diff)
downloadmicropython-05fe66f68a1cf1b7587c55149472ab7bca843631.tar.gz
micropython-05fe66f68a1cf1b7587c55149472ab7bca843631.zip
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.
Diffstat (limited to 'py/modthread.c')
-rw-r--r--py/modthread.c11
1 files changed, 10 insertions, 1 deletions
diff --git a/py/modthread.c b/py/modthread.c
index 975e7d1f63..dda9e93481 100644
--- a/py/modthread.c
+++ b/py/modthread.c
@@ -143,6 +143,8 @@ STATIC mp_obj_t mod_thread_stack_size(size_t n_args, const mp_obj_t *args) {
STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(mod_thread_stack_size_obj, 0, 1, mod_thread_stack_size);
typedef struct _thread_entry_args_t {
+ mp_obj_dict_t *dict_locals;
+ mp_obj_dict_t *dict_globals;
size_t stack_size;
mp_obj_t fun;
size_t n_args;
@@ -161,6 +163,10 @@ STATIC void *thread_entry(void *args_in) {
mp_stack_set_top(&ts + 1); // need to include ts in root-pointer scan
mp_stack_set_limit(args->stack_size);
+ // 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
@@ -169,7 +175,6 @@ STATIC void *thread_entry(void *args_in) {
// TODO set more thread-specific state here:
// mp_pending_exception? (root pointer)
// cur_exception (root pointer)
- // dict_locals? (root pointer) uPy doesn't make a new locals dict for functions, just for classes, so it's different to CPy
DEBUG_printf("[thread] start ts=%p args=%p stack=%p\n", &ts, &args, MP_STATE_THREAD(stack_top));
@@ -240,6 +245,10 @@ STATIC mp_obj_t mod_thread_start_new_thread(size_t n_args, const mp_obj_t *args)
th_args->n_args = pos_args_len;
memcpy(th_args->args, pos_args_items, pos_args_len * sizeof(mp_obj_t));
+ // pass our locals and globals into the new thread
+ th_args->dict_locals = mp_locals_get();
+ th_args->dict_globals = mp_globals_get();
+
// set the stack size to use
th_args->stack_size = thread_stack_size;