Skip to content

Commit 3bfedd0

Browse files
projectgusdpgeorge
authored andcommitted
rp2: Migrate to the new mp_thread_recursive_mutex_t.
Necessary for GC support, also refactored pendsv usage. This work was funded through GitHub Sponsors. Signed-off-by: Angus Gratton <[email protected]>
1 parent 4bcbe88 commit 3bfedd0

File tree

3 files changed

+28
-10
lines changed

3 files changed

+28
-10
lines changed

ports/rp2/mpthreadport.c

+2-2
Original file line numberDiff line numberDiff line change
@@ -84,10 +84,10 @@ void mp_thread_deinit(void) {
8484
assert(get_core_num() == 0);
8585
// Must ensure that core1 is not currently holding the GC lock, otherwise
8686
// it will be terminated while holding the lock.
87-
mp_thread_mutex_lock(&MP_STATE_MEM(gc_mutex), 1);
87+
mp_thread_recursive_mutex_lock(&MP_STATE_MEM(gc_mutex), 1);
8888
multicore_reset_core1();
8989
core1_entry = NULL;
90-
mp_thread_mutex_unlock(&MP_STATE_MEM(gc_mutex));
90+
mp_thread_recursive_mutex_unlock(&MP_STATE_MEM(gc_mutex));
9191
}
9292

9393
void mp_thread_gc_others(void) {

ports/rp2/mpthreadport.h

+19-1
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,10 @@
2626
#ifndef MICROPY_INCLUDED_RP2_MPTHREADPORT_H
2727
#define MICROPY_INCLUDED_RP2_MPTHREADPORT_H
2828

29-
#include "pico/mutex.h"
29+
#include "mutex_extra.h"
3030

3131
typedef struct mutex mp_thread_mutex_t;
32+
typedef recursive_mutex_nowait_t mp_thread_recursive_mutex_t;
3233

3334
extern void *core_state[2];
3435

@@ -65,4 +66,21 @@ static inline void mp_thread_mutex_unlock(mp_thread_mutex_t *m) {
6566
mutex_exit(m);
6667
}
6768

69+
static inline void mp_thread_recursive_mutex_init(mp_thread_recursive_mutex_t *m) {
70+
recursive_mutex_nowait_init(m);
71+
}
72+
73+
static inline int mp_thread_recursive_mutex_lock(mp_thread_recursive_mutex_t *m, int wait) {
74+
if (wait) {
75+
recursive_mutex_nowait_enter_blocking(m);
76+
return 1;
77+
} else {
78+
return recursive_mutex_nowait_try_enter(m, NULL);
79+
}
80+
}
81+
82+
static inline void mp_thread_recursive_mutex_unlock(mp_thread_recursive_mutex_t *m) {
83+
recursive_mutex_nowait_exit(m);
84+
}
85+
6886
#endif // MICROPY_INCLUDED_RP2_MPTHREADPORT_H

ports/rp2/pendsv.c

+7-7
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626

2727
#include <assert.h>
2828
#include "py/mpconfig.h"
29-
#include "mutex_extra.h"
29+
#include "py/mpthread.h"
3030
#include "pendsv.h"
3131

3232
#if PICO_RP2040
@@ -47,21 +47,21 @@ void PendSV_Handler(void);
4747

4848
// Using the nowait variant here as softtimer updates PendSV from the loop of mp_wfe_or_timeout(),
4949
// where we don't want the CPU event bit to be set.
50-
static recursive_mutex_nowait_t pendsv_mutex;
50+
static mp_thread_recursive_mutex_t pendsv_mutex;
5151

5252
void pendsv_init(void) {
53-
recursive_mutex_nowait_init(&pendsv_mutex);
53+
mp_thread_recursive_mutex_init(&pendsv_mutex);
5454
}
5555

5656
void pendsv_suspend(void) {
5757
// Recursive Mutex here as either core may call pendsv_suspend() and expect
5858
// both mutual exclusion (other core can't enter pendsv_suspend() at the
5959
// same time), and that no PendSV handler will run.
60-
recursive_mutex_nowait_enter_blocking(&pendsv_mutex);
60+
mp_thread_recursive_mutex_lock(&pendsv_mutex, 1);
6161
}
6262

6363
void pendsv_resume(void) {
64-
recursive_mutex_nowait_exit(&pendsv_mutex);
64+
mp_thread_recursive_mutex_unlock(&pendsv_mutex);
6565

6666
// Run pendsv if needed. Find an entry with a dispatch and call pendsv dispatch
6767
// with it. If pendsv runs it will service all slots.
@@ -97,7 +97,7 @@ void pendsv_schedule_dispatch(size_t slot, pendsv_dispatch_t f) {
9797
// PendSV interrupt handler to perform background processing.
9898
void PendSV_Handler(void) {
9999

100-
if (!recursive_mutex_nowait_try_enter(&pendsv_mutex, NULL)) {
100+
if (!mp_thread_recursive_mutex_lock(&pendsv_mutex, 0)) {
101101
// Failure here means core 1 holds pendsv_mutex. ISR will
102102
// run again after core 1 calls pendsv_resume().
103103
return;
@@ -117,5 +117,5 @@ void PendSV_Handler(void) {
117117
}
118118
}
119119

120-
recursive_mutex_nowait_exit(&pendsv_mutex);
120+
mp_thread_recursive_mutex_unlock(&pendsv_mutex);
121121
}

0 commit comments

Comments
 (0)