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>
This commit is contained in:
Angus Gratton
2024-12-10 14:50:42 +11:00
committed by Damien George
parent 40e1c111e1
commit 4bcbe88e74
4 changed files with 18 additions and 6 deletions

11
py/gc.c
View File

@@ -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

View File

@@ -1629,6 +1629,11 @@ typedef double mp_float_t;
#define MICROPY_PY_THREAD_GIL_VM_DIVISOR (32)
#endif
// Is a recursive mutex type in use?
#ifndef MICROPY_PY_THREAD_RECURSIVE_MUTEX
#define MICROPY_PY_THREAD_RECURSIVE_MUTEX (MICROPY_PY_THREAD && !MICROPY_PY_THREAD_GIL)
#endif
// Extended modules
#ifndef MICROPY_PY_ASYNCIO

View File

@@ -145,7 +145,7 @@ typedef struct _mp_state_mem_t {
#if MICROPY_PY_THREAD && !MICROPY_PY_THREAD_GIL
// This is a global mutex used to make the GC thread-safe.
mp_thread_mutex_t gc_mutex;
mp_thread_recursive_mutex_t gc_mutex;
#endif
} mp_state_mem_t;

View File

@@ -48,6 +48,12 @@ void mp_thread_mutex_init(mp_thread_mutex_t *mutex);
int mp_thread_mutex_lock(mp_thread_mutex_t *mutex, int wait);
void mp_thread_mutex_unlock(mp_thread_mutex_t *mutex);
#if MICROPY_PY_THREAD_RECURSIVE_MUTEX
void mp_thread_recursive_mutex_init(mp_thread_recursive_mutex_t *mutex);
int mp_thread_recursive_mutex_lock(mp_thread_recursive_mutex_t *mutex, int wait);
void mp_thread_recursive_mutex_unlock(mp_thread_recursive_mutex_t *mutex);
#endif
#endif // MICROPY_PY_THREAD
#if MICROPY_PY_THREAD && MICROPY_PY_THREAD_GIL