aboutsummaryrefslogtreecommitdiff
path: root/py/mpstate.h
diff options
context:
space:
mode:
authorDamien George <damien@micropython.org>2021-05-04 23:56:43 +1000
committerDamien George <damien@micropython.org>2021-05-10 13:07:16 +1000
commitb6b39bff47b5ff1ffc322a6e795d57451d4215a8 (patch)
treeda7e45d7a1e1b54352e686d5d6bddb075884a59e /py/mpstate.h
parentd0de16266f92de180bd34f03aa43f1c489cd883a (diff)
py/gc: Make gc_lock_depth have a count per thread.
This commit makes gc_lock_depth have one counter per thread, instead of one global counter. This makes threads properly independent with respect to the GC, in particular threads can now independently lock the GC for themselves without locking it for other threads. It also means a given thread can run a hard IRQ without temporarily locking the GC for all other threads and potentially making them have MemoryError exceptions at random locations (this really only occurs on MCUs with multiple cores and no GIL, eg on the rp2 port). The commit also removes protection of the GC lock/unlock functions, which is no longer needed when the counter is per thread (and this also fixes the cas where a hard IRQ calling gc_lock() may stall waiting for the mutex). It also puts the check for `gc_lock_depth > 0` outside the GC mutex in gc_alloc, gc_realloc and gc_free, to potentially prevent a hard IRQ from waiting on a mutex if it does attempt to allocate heap memory (and putting the check outside the GC mutex is now safe now that there is a gc_lock_depth per thread). Signed-off-by: Damien George <damien@micropython.org>
Diffstat (limited to 'py/mpstate.h')
-rw-r--r--py/mpstate.h4
1 files changed, 3 insertions, 1 deletions
diff --git a/py/mpstate.h b/py/mpstate.h
index 2519c77e2..a0e3d4f14 100644
--- a/py/mpstate.h
+++ b/py/mpstate.h
@@ -80,7 +80,6 @@ typedef struct _mp_state_mem_t {
int gc_stack_overflow;
MICROPY_GC_STACK_ENTRY_TYPE gc_stack[MICROPY_ALLOC_GC_STACK_SIZE];
- uint16_t gc_lock_depth;
// This variable controls auto garbage collection. If set to 0 then the
// GC won't automatically run when gc_alloc can't find enough blocks. But
@@ -253,6 +252,9 @@ typedef struct _mp_state_thread_t {
uint8_t *pystack_cur;
#endif
+ // Locking of the GC is done per thread.
+ uint16_t gc_lock_depth;
+
////////////////////////////////////////////////////////////
// START ROOT POINTER SECTION
// Everything that needs GC scanning must start here, and