Skip to content

Commit 30d2f1c

Browse files
committed
extmod/zephyr_kernel: Fix mp_thread_start() to match ports/zephyr pattern.
Added missing mutex protection in mp_thread_start() and removed stack size validation to exactly match ports/zephyr implementation. Testing shows threads now execute (previously they never ran), but there are still memory corruption issues suggesting stack or TLS problems remain. Signed-off-by: Andrew Leech <[email protected]>
1 parent 110d6bb commit 30d2f1c

File tree

1 file changed

+12
-11
lines changed

1 file changed

+12
-11
lines changed

extmod/zephyr_kernel/kernel/mpthread_zephyr.c

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -221,15 +221,14 @@ mp_uint_t mp_thread_get_id(void) {
221221

222222
// Mark thread as started (called by new thread)
223223
void mp_thread_start(void) {
224-
// Update status without locking - the thread list is only modified by
225-
// the main thread, and we're only updating our own status field.
226-
// The status field is used for informational purposes only.
224+
mp_thread_mutex_lock(&thread_mutex, 1);
227225
for (mp_thread_t *th = thread; th != NULL; th = th->next) {
228226
if (th->id == k_current_get()) {
229227
th->status = MP_THREAD_STATUS_READY;
230228
break;
231229
}
232230
}
231+
mp_thread_mutex_unlock(&thread_mutex);
233232
}
234233

235234
// Zephyr thread entry point wrapper
@@ -249,14 +248,16 @@ static void zephyr_entry(void *arg1, void *arg2, void *arg3) {
249248

250249
// Create new thread
251250
mp_uint_t mp_thread_create_ex(void *(*entry)(void *), void *arg, size_t *stack_size, int priority, char *name) {
252-
// Default stack size
253-
if (*stack_size == 0) {
254-
*stack_size = MP_THREAD_DEFAULT_STACK_SIZE;
255-
} else if (*stack_size < MP_THREAD_MIN_STACK_SIZE) {
256-
*stack_size = MP_THREAD_MIN_STACK_SIZE;
257-
}
258-
259-
// Try to garbage collect old threads
251+
// TODO: we need to support CONFIG_DYNAMIC_THREAD in order to dynamically allocate the stack of a thread
252+
// For now we use statically allocated stacks, so stack_size parameter is ignored during creation
253+
// but we still update it to reflect the actual stack size allocated
254+
// if (*stack_size == 0) {
255+
// *stack_size = MP_THREAD_DEFAULT_STACK_SIZE;
256+
// } else if (*stack_size < MP_THREAD_MIN_STACK_SIZE) {
257+
// *stack_size = MP_THREAD_MIN_STACK_SIZE;
258+
// }
259+
260+
// in case some threads have finished but their stack has not been collected yet
260261
gc_collect();
261262

262263
// Allocate thread node (must be outside mutex lock for GC)

0 commit comments

Comments
 (0)