@@ -65,7 +65,7 @@ static pthread_key_t tls_key;
65
65
// The mutex is used for any code in this port that needs to be thread safe.
66
66
// Specifically for thread management, access to the linked list is one example.
67
67
// But also, e.g. scheduler state.
68
- static pthread_mutex_t thread_mutex ;
68
+ static mp_thread_recursive_mutex_t thread_mutex ;
69
69
static mp_thread_t * thread ;
70
70
71
71
// this is used to synchronise the signal handler of the thread
@@ -78,11 +78,11 @@ static sem_t thread_signal_done;
78
78
#endif
79
79
80
80
void mp_thread_unix_begin_atomic_section (void ) {
81
- pthread_mutex_lock (& thread_mutex );
81
+ mp_thread_recursive_mutex_lock (& thread_mutex , true );
82
82
}
83
83
84
84
void mp_thread_unix_end_atomic_section (void ) {
85
- pthread_mutex_unlock (& thread_mutex );
85
+ mp_thread_recursive_mutex_unlock (& thread_mutex );
86
86
}
87
87
88
88
// this signal handler is used to scan the regs and stack of a thread
@@ -113,10 +113,7 @@ void mp_thread_init(void) {
113
113
114
114
// Needs to be a recursive mutex to emulate the behavior of
115
115
// 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 );
120
117
121
118
// create first entry in linked list of all threads
122
119
thread = malloc (sizeof (mp_thread_t ));
@@ -321,6 +318,26 @@ void mp_thread_mutex_unlock(mp_thread_mutex_t *mutex) {
321
318
// TODO check return value
322
319
}
323
320
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
+
324
341
#endif // MICROPY_PY_THREAD
325
342
326
343
// this is used even when MICROPY_PY_THREAD is disabled
0 commit comments