summaryrefslogtreecommitdiffstatshomepage
path: root/py/gc.c
diff options
context:
space:
mode:
authorAngus Gratton <angus@redyak.com.au>2024-12-10 14:50:42 +1100
committerDamien George <damien@micropython.org>2025-02-03 15:02:02 +1100
commit4bcbe88e74de245fdee029b6a0746e3485d82a7e (patch)
tree400657f71958868f75558374efa3146394561dd3 /py/gc.c
parent40e1c111e17864044190596dff6d32955d11280c (diff)
downloadmicropython-4bcbe88e74de245fdee029b6a0746e3485d82a7e.tar.gz
micropython-4bcbe88e74de245fdee029b6a0746e3485d82a7e.zip
py: Add optional support for recursive mutexes, use for gc mutex.
Enabled by default if using threading and no GIL This work was funded through GitHub Sponsors. Signed-off-by: Angus Gratton <angus@redyak.com.au>
Diffstat (limited to 'py/gc.c')
-rw-r--r--py/gc.c11
1 files changed, 6 insertions, 5 deletions
diff --git a/py/gc.c b/py/gc.c
index 3d48e06249..6a3f48ac3c 100644
--- a/py/gc.c
+++ b/py/gc.c
@@ -113,9 +113,12 @@
#endif
#if MICROPY_PY_THREAD && !MICROPY_PY_THREAD_GIL
-#define GC_ENTER() mp_thread_mutex_lock(&MP_STATE_MEM(gc_mutex), 1)
-#define GC_EXIT() mp_thread_mutex_unlock(&MP_STATE_MEM(gc_mutex))
+#define GC_MUTEX_INIT() mp_thread_recursive_mutex_init(&MP_STATE_MEM(gc_mutex))
+#define GC_ENTER() mp_thread_recursive_mutex_lock(&MP_STATE_MEM(gc_mutex), 1)
+#define GC_EXIT() mp_thread_recursive_mutex_unlock(&MP_STATE_MEM(gc_mutex))
#else
+// Either no threading, or assume callers to gc_collect() hold the GIL
+#define GC_MUTEX_INIT()
#define GC_ENTER()
#define GC_EXIT()
#endif
@@ -210,9 +213,7 @@ void gc_init(void *start, void *end) {
MP_STATE_MEM(gc_alloc_amount) = 0;
#endif
- #if MICROPY_PY_THREAD && !MICROPY_PY_THREAD_GIL
- mp_thread_mutex_init(&MP_STATE_MEM(gc_mutex));
- #endif
+ GC_MUTEX_INIT();
}
#if MICROPY_GC_SPLIT_HEAP