32
32
#include "py/mpthread.h"
33
33
#include "py/gc.h"
34
34
35
+ #include "mpthreadport.h"
36
+
35
37
#if MICROPY_PY_THREAD
36
38
37
39
#include <fcntl.h>
@@ -65,7 +67,7 @@ static pthread_key_t tls_key;
65
67
// The mutex is used for any code in this port that needs to be thread safe.
66
68
// Specifically for thread management, access to the linked list is one example.
67
69
// But also, e.g. scheduler state.
68
- static pthread_mutex_t thread_mutex ;
70
+ static mp_thread_recursive_mutex_t thread_mutex ;
69
71
static mp_thread_t * thread ;
70
72
71
73
// this is used to synchronise the signal handler of the thread
@@ -78,11 +80,11 @@ static sem_t thread_signal_done;
78
80
#endif
79
81
80
82
void mp_thread_unix_begin_atomic_section (void ) {
81
- pthread_mutex_lock (& thread_mutex );
83
+ mp_thread_recursive_mutex_lock (& thread_mutex , true );
82
84
}
83
85
84
86
void mp_thread_unix_end_atomic_section (void ) {
85
- pthread_mutex_unlock (& thread_mutex );
87
+ mp_thread_recursive_mutex_unlock (& thread_mutex );
86
88
}
87
89
88
90
// this signal handler is used to scan the regs and stack of a thread
@@ -113,10 +115,7 @@ void mp_thread_init(void) {
113
115
114
116
// Needs to be a recursive mutex to emulate the behavior of
115
117
// 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 );
118
+ mp_thread_recursive_mutex_init (& thread_mutex );
120
119
121
120
// create first entry in linked list of all threads
122
121
thread = malloc (sizeof (mp_thread_t ));
@@ -321,6 +320,26 @@ void mp_thread_mutex_unlock(mp_thread_mutex_t *mutex) {
321
320
// TODO check return value
322
321
}
323
322
323
+ #if MICROPY_PY_THREAD_RECURSIVE_MUTEX
324
+
325
+ void mp_thread_recursive_mutex_init (mp_thread_recursive_mutex_t * mutex ) {
326
+ pthread_mutexattr_t attr ;
327
+ pthread_mutexattr_init (& attr );
328
+ pthread_mutexattr_settype (& attr , PTHREAD_MUTEX_RECURSIVE );
329
+ pthread_mutex_init (mutex , & attr );
330
+ pthread_mutexattr_destroy (& attr );
331
+ }
332
+
333
+ int mp_thread_recursive_mutex_lock (mp_thread_recursive_mutex_t * mutex , int wait ) {
334
+ return mp_thread_mutex_lock (mutex , wait );
335
+ }
336
+
337
+ void mp_thread_recursive_mutex_unlock (mp_thread_recursive_mutex_t * mutex ) {
338
+ mp_thread_mutex_unlock (mutex );
339
+ }
340
+
341
+ #endif // MICROPY_PY_THREAD_RECURSIVE_MUTEX
342
+
324
343
#endif // MICROPY_PY_THREAD
325
344
326
345
// this is used even when MICROPY_PY_THREAD is disabled
0 commit comments