Skip to content

Commit 4bcbe88

Browse files
projectgusdpgeorge
authored andcommitted
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 <[email protected]>
1 parent 40e1c11 commit 4bcbe88

File tree

4 files changed

+18
-6
lines changed

4 files changed

+18
-6
lines changed

py/gc.c

+6-5
Original file line numberDiff line numberDiff line change
@@ -113,9 +113,12 @@
113113
#endif
114114

115115
#if MICROPY_PY_THREAD && !MICROPY_PY_THREAD_GIL
116-
#define GC_ENTER() mp_thread_mutex_lock(&MP_STATE_MEM(gc_mutex), 1)
117-
#define GC_EXIT() mp_thread_mutex_unlock(&MP_STATE_MEM(gc_mutex))
116+
#define GC_MUTEX_INIT() mp_thread_recursive_mutex_init(&MP_STATE_MEM(gc_mutex))
117+
#define GC_ENTER() mp_thread_recursive_mutex_lock(&MP_STATE_MEM(gc_mutex), 1)
118+
#define GC_EXIT() mp_thread_recursive_mutex_unlock(&MP_STATE_MEM(gc_mutex))
118119
#else
120+
// Either no threading, or assume callers to gc_collect() hold the GIL
121+
#define GC_MUTEX_INIT()
119122
#define GC_ENTER()
120123
#define GC_EXIT()
121124
#endif
@@ -210,9 +213,7 @@ void gc_init(void *start, void *end) {
210213
MP_STATE_MEM(gc_alloc_amount) = 0;
211214
#endif
212215

213-
#if MICROPY_PY_THREAD && !MICROPY_PY_THREAD_GIL
214-
mp_thread_mutex_init(&MP_STATE_MEM(gc_mutex));
215-
#endif
216+
GC_MUTEX_INIT();
216217
}
217218

218219
#if MICROPY_GC_SPLIT_HEAP

py/mpconfig.h

+5
Original file line numberDiff line numberDiff line change
@@ -1629,6 +1629,11 @@ typedef double mp_float_t;
16291629
#define MICROPY_PY_THREAD_GIL_VM_DIVISOR (32)
16301630
#endif
16311631

1632+
// Is a recursive mutex type in use?
1633+
#ifndef MICROPY_PY_THREAD_RECURSIVE_MUTEX
1634+
#define MICROPY_PY_THREAD_RECURSIVE_MUTEX (MICROPY_PY_THREAD && !MICROPY_PY_THREAD_GIL)
1635+
#endif
1636+
16321637
// Extended modules
16331638

16341639
#ifndef MICROPY_PY_ASYNCIO

py/mpstate.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,7 @@ typedef struct _mp_state_mem_t {
145145

146146
#if MICROPY_PY_THREAD && !MICROPY_PY_THREAD_GIL
147147
// This is a global mutex used to make the GC thread-safe.
148-
mp_thread_mutex_t gc_mutex;
148+
mp_thread_recursive_mutex_t gc_mutex;
149149
#endif
150150
} mp_state_mem_t;
151151

py/mpthread.h

+6
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,12 @@ void mp_thread_mutex_init(mp_thread_mutex_t *mutex);
4848
int mp_thread_mutex_lock(mp_thread_mutex_t *mutex, int wait);
4949
void mp_thread_mutex_unlock(mp_thread_mutex_t *mutex);
5050

51+
#if MICROPY_PY_THREAD_RECURSIVE_MUTEX
52+
void mp_thread_recursive_mutex_init(mp_thread_recursive_mutex_t *mutex);
53+
int mp_thread_recursive_mutex_lock(mp_thread_recursive_mutex_t *mutex, int wait);
54+
void mp_thread_recursive_mutex_unlock(mp_thread_recursive_mutex_t *mutex);
55+
#endif
56+
5157
#endif // MICROPY_PY_THREAD
5258

5359
#if MICROPY_PY_THREAD && MICROPY_PY_THREAD_GIL

0 commit comments

Comments
 (0)