Skip to content

Commit 330165a

Browse files
committed
py: Add MP_STATE_THREAD to hold state specific to a given thread.
1 parent 3545ef8 commit 330165a

8 files changed

Lines changed: 33 additions & 26 deletions

File tree

mpy-cross/gccollect.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,7 @@ void gc_collect(void) {
142142
gc_helper_get_regs(regs);
143143
// GC stack (and regs because we captured them)
144144
void **regs_ptr = (void**)(void*)&regs;
145-
gc_collect_root(regs_ptr, ((mp_uint_t)MP_STATE_VM(stack_top) - (mp_uint_t)&regs) / sizeof(mp_uint_t));
145+
gc_collect_root(regs_ptr, ((mp_uint_t)MP_STATE_THREAD(stack_top) - (mp_uint_t)&regs) / sizeof(mp_uint_t));
146146
#if MICROPY_EMIT_NATIVE
147147
mp_unix_mark_exec();
148148
#endif

pic16bit/main.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ int main(int argc, char **argv) {
6464

6565
// init MicroPython runtime
6666
int stack_dummy;
67-
MP_STATE_VM(stack_top) = (char*)&stack_dummy;
67+
MP_STATE_THREAD(stack_top) = (char*)&stack_dummy;
6868
gc_init(heap, heap + sizeof(heap));
6969
mp_init();
7070
mp_hal_init();
@@ -93,7 +93,7 @@ void gc_collect(void) {
9393
void *dummy;
9494
gc_collect_start();
9595
// Node: stack is ascending
96-
gc_collect_root(&dummy, ((mp_uint_t)&dummy - (mp_uint_t)MP_STATE_VM(stack_top)) / sizeof(mp_uint_t));
96+
gc_collect_root(&dummy, ((mp_uint_t)&dummy - (mp_uint_t)MP_STATE_THREAD(stack_top)) / sizeof(mp_uint_t));
9797
gc_collect_end();
9898
}
9999

py/gc.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -279,7 +279,7 @@ void gc_collect_start(void) {
279279
// correctly in the mp_state_ctx structure. We scan nlr_top, dict_locals,
280280
// dict_globals, then the root pointer section of mp_state_vm.
281281
void **ptrs = (void**)(void*)&mp_state_ctx;
282-
gc_collect_root(ptrs, offsetof(mp_state_ctx_t, vm.stack_top) / sizeof(void*));
282+
gc_collect_root(ptrs, offsetof(mp_state_ctx_t, vm.qstr_last_chunk) / sizeof(void*));
283283
}
284284

285285
void gc_collect_root(void **ptrs, size_t len) {
@@ -713,7 +713,7 @@ void gc_dump_alloc_table(void) {
713713
}
714714
if (c == 'h') {
715715
ptrs = (void**)&c;
716-
len = ((mp_uint_t)MP_STATE_VM(stack_top) - (mp_uint_t)&c) / sizeof(mp_uint_t);
716+
len = ((mp_uint_t)MP_STATE_THREAD(stack_top) - (mp_uint_t)&c) / sizeof(mp_uint_t);
717717
for (mp_uint_t i = 0; i < len; i++) {
718718
mp_uint_t ptr = (mp_uint_t)ptrs[i];
719719
if (VERIFY_PTR(ptr) && BLOCK_FROM_PTR(ptr) == bl) {

py/modmicropython.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ mp_obj_t mp_micropython_mem_info(size_t n_args, const mp_obj_t *args) {
6060
(mp_uint_t)m_get_total_bytes_allocated(), (mp_uint_t)m_get_current_bytes_allocated(), (mp_uint_t)m_get_peak_bytes_allocated());
6161
#endif
6262
#if MICROPY_STACK_CHECK
63-
mp_printf(&mp_plat_print, "stack: " UINT_FMT " out of " INT_FMT "\n", mp_stack_usage(), MP_STATE_VM(stack_limit));
63+
mp_printf(&mp_plat_print, "stack: " UINT_FMT " out of " INT_FMT "\n", mp_stack_usage(), MP_STATE_THREAD(stack_limit));
6464
#else
6565
mp_printf(&mp_plat_print, "stack: " UINT_FMT "\n", mp_stack_usage());
6666
#endif

py/mpstate.h

Lines changed: 20 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -91,9 +91,6 @@ typedef struct _mp_state_vm_t {
9191
// this must start at the start of this structure
9292
//
9393

94-
// Note: nlr asm code has the offset of this hard-coded
95-
nlr_buf_t *nlr_top;
96-
9794
qstr_pool_t *last_pool;
9895

9996
// non-heap memory for creating an exception if we can't allocate RAM
@@ -161,14 +158,6 @@ typedef struct _mp_state_vm_t {
161158
size_t qstr_last_alloc;
162159
size_t qstr_last_used;
163160

164-
// Stack top at the start of program
165-
// Note: this entry is used to locate the end of the root pointer section.
166-
char *stack_top;
167-
168-
#if MICROPY_STACK_CHECK
169-
mp_uint_t stack_limit;
170-
#endif
171-
172161
mp_uint_t mp_optimise_value;
173162

174163
// size of the emergency exception buf, if it's dynamically allocated
@@ -177,15 +166,31 @@ typedef struct _mp_state_vm_t {
177166
#endif
178167
} mp_state_vm_t;
179168

180-
// This structure combines the above 2 structures, and adds the local
169+
// This structure holds state that is specific to a given thread.
170+
// Everything in this structure is scanned for root pointers.
171+
typedef struct _mp_state_thread_t {
172+
// Note: nlr asm code has the offset of this hard-coded
173+
nlr_buf_t *nlr_top; // ROOT POINTER
174+
175+
// Stack top at the start of program
176+
// Note: this entry is used to locate the end of the root pointer section.
177+
char *stack_top;
178+
179+
#if MICROPY_STACK_CHECK
180+
size_t stack_limit;
181+
#endif
182+
} mp_state_thread_t;
183+
184+
// This structure combines the above 3 structures, and adds the local
181185
// and global dicts.
182186
// Note: if this structure changes then revisit all nlr asm code since they
183187
// have the offset of nlr_top hard-coded.
184188
typedef struct _mp_state_ctx_t {
185189
// these must come first for root pointer scanning in GC to work
186190
mp_obj_dict_t *dict_locals;
187191
mp_obj_dict_t *dict_globals;
188-
// this must come next for root pointer scanning in GC to work
192+
// these must come next in this order for root pointer scanning in GC to work
193+
mp_state_thread_t thread;
189194
mp_state_vm_t vm;
190195
mp_state_mem_t mem;
191196
} mp_state_ctx_t;
@@ -196,4 +201,6 @@ extern mp_state_ctx_t mp_state_ctx;
196201
#define MP_STATE_VM(x) (mp_state_ctx.vm.x)
197202
#define MP_STATE_MEM(x) (mp_state_ctx.mem.x)
198203

204+
#define MP_STATE_THREAD(x) (mp_state_ctx.thread.x)
205+
199206
#endif // __MICROPY_INCLUDED_PY_MPSTATE_H__

py/stackctrl.c

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -32,23 +32,23 @@
3232

3333
void mp_stack_ctrl_init(void) {
3434
volatile int stack_dummy;
35-
MP_STATE_VM(stack_top) = (char*)&stack_dummy;
35+
MP_STATE_THREAD(stack_top) = (char*)&stack_dummy;
3636
}
3737

3838
void mp_stack_set_top(void *top) {
39-
MP_STATE_VM(stack_top) = top;
39+
MP_STATE_THREAD(stack_top) = top;
4040
}
4141

4242
mp_uint_t mp_stack_usage(void) {
4343
// Assumes descending stack
4444
volatile int stack_dummy;
45-
return MP_STATE_VM(stack_top) - (char*)&stack_dummy;
45+
return MP_STATE_THREAD(stack_top) - (char*)&stack_dummy;
4646
}
4747

4848
#if MICROPY_STACK_CHECK
4949

5050
void mp_stack_set_limit(mp_uint_t limit) {
51-
MP_STATE_VM(stack_limit) = limit;
51+
MP_STATE_THREAD(stack_limit) = limit;
5252
}
5353

5454
void mp_exc_recursion_depth(void) {
@@ -57,7 +57,7 @@ void mp_exc_recursion_depth(void) {
5757
}
5858

5959
void mp_stack_check(void) {
60-
if (mp_stack_usage() >= MP_STATE_VM(stack_limit)) {
60+
if (mp_stack_usage() >= MP_STATE_THREAD(stack_limit)) {
6161
mp_exc_recursion_depth();
6262
}
6363
}

qemu-arm/test_main.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ void gc_collect(void) {
7373
void *sp = (void*)&dummy;
7474

7575
// trace the stack, including the registers (since they live on the stack in this function)
76-
gc_collect_root((void**)sp, ((uint32_t)MP_STATE_VM(stack_top) - (uint32_t)sp) / sizeof(uint32_t));
76+
gc_collect_root((void**)sp, ((uint32_t)MP_STATE_THREAD(stack_top) - (uint32_t)sp) / sizeof(uint32_t));
7777

7878
gc_collect_end();
7979
}

unix/gccollect.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,7 @@ void gc_collect(void) {
144144
gc_helper_get_regs(regs);
145145
// GC stack (and regs because we captured them)
146146
void **regs_ptr = (void**)(void*)&regs;
147-
gc_collect_root(regs_ptr, ((uintptr_t)MP_STATE_VM(stack_top) - (uintptr_t)&regs) / sizeof(uintptr_t));
147+
gc_collect_root(regs_ptr, ((uintptr_t)MP_STATE_THREAD(stack_top) - (uintptr_t)&regs) / sizeof(uintptr_t));
148148
#if MICROPY_EMIT_NATIVE
149149
mp_unix_mark_exec();
150150
#endif

0 commit comments

Comments
 (0)