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>
This commit is contained in:
committed by
Damien George
parent
3bfedd0f4a
commit
fd0e529a47
@@ -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.
|
// 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.
|
// Specifically for thread management, access to the linked list is one example.
|
||||||
// But also, e.g. scheduler state.
|
// 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;
|
static mp_thread_t *thread;
|
||||||
|
|
||||||
// this is used to synchronise the signal handler of the thread
|
// this is used to synchronise the signal handler of the thread
|
||||||
@@ -78,11 +78,11 @@ static sem_t thread_signal_done;
|
|||||||
#endif
|
#endif
|
||||||
|
|
||||||
void mp_thread_unix_begin_atomic_section(void) {
|
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) {
|
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
|
// 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
|
// Needs to be a recursive mutex to emulate the behavior of
|
||||||
// BEGIN_ATOMIC_SECTION on bare metal.
|
// BEGIN_ATOMIC_SECTION on bare metal.
|
||||||
pthread_mutexattr_t thread_mutex_attr;
|
mp_thread_recursive_mutex_init(&thread_mutex);
|
||||||
pthread_mutexattr_init(&thread_mutex_attr);
|
|
||||||
pthread_mutexattr_settype(&thread_mutex_attr, PTHREAD_MUTEX_RECURSIVE);
|
|
||||||
pthread_mutex_init(&thread_mutex, &thread_mutex_attr);
|
|
||||||
|
|
||||||
// create first entry in linked list of all threads
|
// create first entry in linked list of all threads
|
||||||
thread = malloc(sizeof(mp_thread_t));
|
thread = malloc(sizeof(mp_thread_t));
|
||||||
@@ -321,6 +318,26 @@ void mp_thread_mutex_unlock(mp_thread_mutex_t *mutex) {
|
|||||||
// TODO check return value
|
// 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
|
#endif // MICROPY_PY_THREAD
|
||||||
|
|
||||||
// this is used even when MICROPY_PY_THREAD is disabled
|
// this is used even when MICROPY_PY_THREAD is disabled
|
||||||
|
|||||||
@@ -28,6 +28,7 @@
|
|||||||
#include <stdbool.h>
|
#include <stdbool.h>
|
||||||
|
|
||||||
typedef pthread_mutex_t mp_thread_mutex_t;
|
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_init(void);
|
||||||
void mp_thread_deinit(void);
|
void mp_thread_deinit(void);
|
||||||
|
|||||||
Reference in New Issue
Block a user