Skip to content

Commit b2a1ceb

Browse files
committed
py: Add hooks for Zephyr threading integration.
Add support for port-overridable GIL macros in mpthread.h, allowing ports to inject k_yield() for cooperative scheduling. Minor adjustments to gc.c and modthread.c for Zephyr threading compatibility. Signed-off-by: Andrew Leech <[email protected]>
1 parent 14994e5 commit b2a1ceb

File tree

4 files changed

+21
-1
lines changed

4 files changed

+21
-1
lines changed

py/gc.c

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,10 +27,13 @@
2727

2828
#include <assert.h>
2929
#include <stdio.h>
30+
#include <stdlib.h>
3031
#include <string.h>
3132

3233
#include "py/gc.h"
3334
#include "py/runtime.h"
35+
#include "py/mpthread.h"
36+
#include "py/mphal.h"
3437

3538
#if MICROPY_DEBUG_VALGRIND
3639
#include <valgrind/memcheck.h>
@@ -214,7 +217,9 @@ void gc_init(void *start, void *end) {
214217
#endif
215218

216219
// unlock the GC
220+
#if MICROPY_PY_THREAD
217221
MP_STATE_THREAD(gc_lock_depth) = 0;
222+
#endif
218223

219224
// allow auto collection
220225
MP_STATE_MEM(gc_auto_collect_enabled) = 1;

py/modthread.c

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -169,7 +169,6 @@ static void *thread_entry(void *args_in) {
169169

170170
MP_THREAD_GIL_ENTER();
171171

172-
// signal that we are set up and running
173172
mp_thread_start();
174173

175174
// TODO set more thread-specific state here:
@@ -196,7 +195,11 @@ static void *thread_entry(void *args_in) {
196195
}
197196
}
198197

198+
#if !MICROPY_ZEPHYR_THREADING
199199
DEBUG_printf("[thread] finish ts=%p\n", &ts);
200+
#else
201+
DEBUG_printf("[thread] finish\n");
202+
#endif
200203

201204
// signal that we are finished
202205
mp_thread_finish();

py/mpthread.h

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,11 +58,20 @@ void mp_thread_recursive_mutex_unlock(mp_thread_recursive_mutex_t *mutex);
5858

5959
#if MICROPY_PY_THREAD && MICROPY_PY_THREAD_GIL
6060
#include "py/mpstate.h"
61+
// Allow ports to override GIL macros (e.g., to add k_yield() for cooperative scheduling)
62+
#ifndef MP_THREAD_GIL_ENTER
6163
#define MP_THREAD_GIL_ENTER() mp_thread_mutex_lock(&MP_STATE_VM(gil_mutex), 1)
64+
#endif
65+
#ifndef MP_THREAD_GIL_EXIT
6266
#define MP_THREAD_GIL_EXIT() mp_thread_mutex_unlock(&MP_STATE_VM(gil_mutex))
67+
#endif
6368
#else
69+
#ifndef MP_THREAD_GIL_ENTER
6470
#define MP_THREAD_GIL_ENTER()
71+
#endif
72+
#ifndef MP_THREAD_GIL_EXIT
6573
#define MP_THREAD_GIL_EXIT()
6674
#endif
75+
#endif
6776

6877
#endif // MICROPY_INCLUDED_PY_MPTHREAD_H

py/qstrdefs.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,3 +76,6 @@ Q(/rom/lib)
7676
#if MICROPY_ENABLE_PYSTACK
7777
Q(pystack exhausted)
7878
#endif
79+
80+
// Special methods
81+
Q(__del__)

0 commit comments

Comments
 (0)