summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAngus Gratton <angus@redyak.com.au>2025-01-09 14:38:31 +1100
committerDamien George <damien@micropython.org>2025-02-03 15:02:02 +1100
commitfd0e529a476650a070046a38b7ba7e253b65a77a (patch)
tree46d72549efe74db89ef68e391d9fea94c09b24a8
parent3bfedd0f4a9e8b11ff97851615ec4761663b0e94 (diff)
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>
-rw-r--r--ports/unix/mpthreadport.c31
-rw-r--r--ports/unix/mpthreadport.h1
2 files changed, 25 insertions, 7 deletions
diff --git a/ports/unix/mpthreadport.c b/ports/unix/mpthreadport.c
index 16ac4da8b..5172645bc 100644
--- a/ports/unix/mpthreadport.c
+++ b/ports/unix/mpthreadport.c
@@ -65,7 +65,7 @@ static pthread_key_t tls_key;
// The mutex is used for any code in this port that needs to be thread safe.
// Specifically for thread management, access to the linked list is one example.
// But also, e.g. scheduler state.
-static pthread_mutex_t thread_mutex;
+static mp_thread_recursive_mutex_t thread_mutex;
static mp_thread_t *thread;
// this is used to synchronise the signal handler of the thread
@@ -78,11 +78,11 @@ static sem_t thread_signal_done;
#endif
void mp_thread_unix_begin_atomic_section(void) {
- pthread_mutex_lock(&thread_mutex);
+ mp_thread_recursive_mutex_lock(&thread_mutex, true);
}
void mp_thread_unix_end_atomic_section(void) {
- pthread_mutex_unlock(&thread_mutex);
+ mp_thread_recursive_mutex_unlock(&thread_mutex);
}
// this signal handler is used to scan the regs and stack of a thread
@@ -113,10 +113,7 @@ void mp_thread_init(void) {
// Needs to be a recursive mutex to emulate the behavior of
// BEGIN_ATOMIC_SECTION on bare metal.
- pthread_mutexattr_t thread_mutex_attr;
- pthread_mutexattr_init(&thread_mutex_attr);
- pthread_mutexattr_settype(&thread_mutex_attr, PTHREAD_MUTEX_RECURSIVE);
- pthread_mutex_init(&thread_mutex, &thread_mutex_attr);
+ mp_thread_recursive_mutex_init(&thread_mutex);
// create first entry in linked list of all threads
thread = malloc(sizeof(mp_thread_t));
@@ -321,6 +318,26 @@ void mp_thread_mutex_unlock(mp_thread_mutex_t *mutex) {
// TODO check return value
}
+#if MICROPY_PY_THREAD_RECURSIVE_MUTEX
+
+void mp_thread_recursive_mutex_init(mp_thread_recursive_mutex_t *mutex) {
+ pthread_mutexattr_t attr;
+ pthread_mutexattr_init(&attr);
+ pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_RECURSIVE);
+ pthread_mutex_init(mutex, &attr);
+ pthread_mutexattr_destroy(&attr);
+}
+
+int mp_thread_recursive_mutex_lock(mp_thread_recursive_mutex_t *mutex, int wait) {
+ return mp_thread_mutex_lock(mutex, wait);
+}
+
+void mp_thread_recursive_mutex_unlock(mp_thread_recursive_mutex_t *mutex) {
+ mp_thread_mutex_unlock(mutex);
+}
+
+#endif // MICROPY_PY_THREAD_RECURSIVE_MUTEX
+
#endif // MICROPY_PY_THREAD
// this is used even when MICROPY_PY_THREAD is disabled
diff --git a/ports/unix/mpthreadport.h b/ports/unix/mpthreadport.h
index b365f200e..a38223e72 100644
--- a/ports/unix/mpthreadport.h
+++ b/ports/unix/mpthreadport.h
@@ -28,6 +28,7 @@
#include <stdbool.h>
typedef pthread_mutex_t mp_thread_mutex_t;
+typedef pthread_mutex_t mp_thread_recursive_mutex_t;
void mp_thread_init(void);
void mp_thread_deinit(void);