Skip to content

Commit fd0e529

Browse files
projectgusdpgeorge
authored andcommitted
unix: Add recursive mutex support.
Allows refactoring the existing thread_mutex atomic section support to use the new recursive mutex type. This work was funded through GitHub Sponsors. Signed-off-by: Angus Gratton <angus@redyak.com.au>
1 parent 3bfedd0 commit fd0e529

File tree

2 files changed

+25
-7
lines changed

2 files changed

+25
-7
lines changed

ports/unix/mpthreadport.c

+24-7
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ static pthread_key_t tls_key;
6565
// The mutex is used for any code in this port that needs to be thread safe.
6666
// Specifically for thread management, access to the linked list is one example.
6767
// But also, e.g. scheduler state.
68-
static pthread_mutex_t thread_mutex;
68+
static mp_thread_recursive_mutex_t thread_mutex;
6969
static mp_thread_t *thread;
7070

7171
// this is used to synchronise the signal handler of the thread
@@ -78,11 +78,11 @@ static sem_t thread_signal_done;
7878
#endif
7979

8080
void mp_thread_unix_begin_atomic_section(void) {
81-
pthread_mutex_lock(&thread_mutex);
81+
mp_thread_recursive_mutex_lock(&thread_mutex, true);
8282
}
8383

8484
void mp_thread_unix_end_atomic_section(void) {
85-
pthread_mutex_unlock(&thread_mutex);
85+
mp_thread_recursive_mutex_unlock(&thread_mutex);
8686
}
8787

8888
// this signal handler is used to scan the regs and stack of a thread
@@ -113,10 +113,7 @@ void mp_thread_init(void) {
113113

114114
// Needs to be a recursive mutex to emulate the behavior of
115115
// BEGIN_ATOMIC_SECTION on bare metal.
116-
pthread_mutexattr_t thread_mutex_attr;
117-
pthread_mutexattr_init(&thread_mutex_attr);
118-
pthread_mutexattr_settype(&thread_mutex_attr, PTHREAD_MUTEX_RECURSIVE);
119-
pthread_mutex_init(&thread_mutex, &thread_mutex_attr);
116+
mp_thread_recursive_mutex_init(&thread_mutex);
120117

121118
// create first entry in linked list of all threads
122119
thread = malloc(sizeof(mp_thread_t));
@@ -321,6 +318,26 @@ void mp_thread_mutex_unlock(mp_thread_mutex_t *mutex) {
321318
// TODO check return value
322319
}
323320

321+
#if MICROPY_PY_THREAD_RECURSIVE_MUTEX
322+
323+
void mp_thread_recursive_mutex_init(mp_thread_recursive_mutex_t *mutex) {
324+
pthread_mutexattr_t attr;
325+
pthread_mutexattr_init(&attr);
326+
pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_RECURSIVE);
327+
pthread_mutex_init(mutex, &attr);
328+
pthread_mutexattr_destroy(&attr);
329+
}
330+
331+
int mp_thread_recursive_mutex_lock(mp_thread_recursive_mutex_t *mutex, int wait) {
332+
return mp_thread_mutex_lock(mutex, wait);
333+
}
334+
335+
void mp_thread_recursive_mutex_unlock(mp_thread_recursive_mutex_t *mutex) {
336+
mp_thread_mutex_unlock(mutex);
337+
}
338+
339+
#endif // MICROPY_PY_THREAD_RECURSIVE_MUTEX
340+
324341
#endif // MICROPY_PY_THREAD
325342

326343
// this is used even when MICROPY_PY_THREAD is disabled

ports/unix/mpthreadport.h

+1
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
#include <stdbool.h>
2929

3030
typedef pthread_mutex_t mp_thread_mutex_t;
31+
typedef pthread_mutex_t mp_thread_recursive_mutex_t;
3132

3233
void mp_thread_init(void);
3334
void mp_thread_deinit(void);

0 commit comments

Comments
 (0)