Skip to content

Commit 8212c07

Browse files
committed
【完善】GC 适配代码。
Signed-off-by: armink <[email protected]>
1 parent 30bb89d commit 8212c07

File tree

3 files changed

+18
-40
lines changed

3 files changed

+18
-40
lines changed

port/gccollect.c

Lines changed: 4 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -28,35 +28,15 @@
2828
#include "py/mpstate.h"
2929
#include "py/gc.h"
3030

31-
// If we don't have architecture-specific optimized support,
32-
// just fall back to setjmp-based implementation.
33-
34-
// If MICROPY_GCREGS_SETJMP was requested explicitly, or if
35-
// we enabled it as a fallback above.
36-
#include <setjmp.h>
37-
38-
typedef jmp_buf regs_t;
39-
40-
STATIC void gc_helper_get_regs(regs_t arr) {
41-
setjmp(arr);
42-
}
43-
44-
void gc_collect_regs_and_stack(void) {
45-
regs_t regs;
46-
gc_helper_get_regs(regs);
47-
// GC stack (and regs because we captured them)
48-
void **regs_ptr = (void**)(void*)&regs;
49-
gc_collect_root(regs_ptr, ((uintptr_t)MP_STATE_THREAD(stack_top) - (uintptr_t)&regs) / sizeof(uintptr_t));
50-
}
51-
5231
void gc_collect(void) {
5332
gc_collect_start();
54-
// trace root pointers from any threads
55-
gc_collect_regs_and_stack();
5633

5734
#if MICROPY_PY_THREAD
58-
//TODO has some problem when multithreading using gc at the same time
35+
// trace root pointers from any threads
5936
mp_thread_gc_others();
37+
#else
38+
// gc the main thread stack
39+
gc_collect_root(rt_thread_self()->stack_addr, ((mp_uint_t)((void *)MP_STATE_THREAD(stack_top) - rt_thread_self()->stack_addr)) / 4);
6040
#endif
6141

6242
gc_collect_end();

port/mpthreadport.c

Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -61,8 +61,8 @@ STATIC thread_t *thread_root; // root pointer, handled by mp_thread_gc_others
6161
/**
6262
* thread port initialization
6363
*
64-
* @param stack MicroPython main thread stack
65-
* @param stack_len MicroPython main thread stack
64+
* @param stack MicroPython main thread stack start address
65+
* @param stack_len number of words in the stack
6666
*/
6767
void mp_thread_init(void *stack, uint32_t stack_len) {
6868
mp_thread_set_state(&mp_state_ctx.thread);
@@ -82,20 +82,18 @@ void mp_thread_gc_others(void) {
8282

8383
mp_thread_mutex_lock(&thread_mutex, 1);
8484
for (thread_t *th = thread_root; th != NULL; th = th->next) {
85-
if (th == &thread_root_node) {
86-
continue;
85+
// the root node not using the mpy heap
86+
if (th != &thread_root_node) {
87+
gc_collect_root((void**)&th, 1);
88+
gc_collect_root(&th->arg, 1); // probably not needed
8789
}
88-
gc_collect_root((void**)&th, 1);
89-
gc_collect_root(&th->arg, 1); // probably not needed
9090

91-
if (th->id == rt_thread_self()) {
91+
if (th->status == MP_THREAD_STATUS_READY) {
9292
continue;
9393
}
94-
if (th->status != MP_THREAD_STATUS_FINISH) {
95-
continue;
96-
}
97-
gc_collect_root((void**)&th->id, 1); // probably not needed
98-
gc_collect_root((void**)&th->stack, th->stack_len); // probably not needed
94+
95+
gc_collect_root((void**) &th->id, 1); // probably not needed
96+
gc_collect_root(th->stack, th->stack_len); // probably not needed
9997
}
10098
mp_thread_mutex_unlock(&thread_mutex);
10199
}
@@ -150,7 +148,7 @@ void mp_thread_create_ex(void *(*entry)(void*), void *arg, size_t *stack_size, i
150148
// add thread to linked list of all threads
151149
th->status = MP_THREAD_STATUS_READY;
152150
th->arg = arg;
153-
th->stack_len = *stack_size;
151+
th->stack_len = *stack_size / 4;
154152
th->next = thread_root;
155153
thread_root = th;
156154

port/mpy_main.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -63,17 +63,17 @@ void do_str(const char *src, mp_parse_input_kind_t input_kind) {
6363
}
6464
#endif
6565

66-
static char *stack_top = RT_NULL;
66+
static void *stack_top = RT_NULL;
6767
static char *heap = RT_NULL;
6868

6969
void mpy_main(const char *filename) {
7070
int stack_dummy;
71-
stack_top = (char*)&stack_dummy;
71+
stack_top = (void *)&stack_dummy;
7272

7373
rtt_getchar_init();
7474

7575
#if MICROPY_PY_THREAD
76-
mp_thread_init(rt_thread_self()->stack_addr, rt_thread_self()->stack_size / 4);
76+
mp_thread_init(rt_thread_self()->stack_addr, (rt_uint32_t)(stack_top - rt_thread_self()->stack_addr) / 4);
7777
#endif
7878

7979
mp_stack_set_top(stack_top);

0 commit comments

Comments
 (0)