Skip to content

Commit 6ee27fc

Browse files
committed
extmod/zephyr_kernel: Initialize thread state globals/locals in zephyr_entry.
The heap-allocated mp_state_thread_t was only being zeroed, not properly initialized with dict_locals and dict_globals. This caused threads to fail with "NotImplementedError: opcode" when executing any bytecode that accessed variables or builtins. The fix initializes all required mp_state_thread_t fields (gc_lock_depth, nlr_top, nlr_jump_callback_top, mp_pending_exception) and inherits dict_locals/dict_globals from the main thread using mp_locals_set() and mp_globals_set(). Tested on STM32 NUCLEO_F429ZI. Threads can now execute loops and access global variables correctly. Signed-off-by: Andrew Leech <[email protected]>
1 parent 5229a24 commit 6ee27fc

File tree

1 file changed

+10
-0
lines changed

1 file changed

+10
-0
lines changed

extmod/zephyr_kernel/kernel/mpthread_zephyr.c

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -262,6 +262,16 @@ static void zephyr_entry(void *arg1, void *arg2, void *arg3) {
262262
// Set up thread-local storage using pre-allocated state
263263
mp_thread_set_state(self->thread_state);
264264

265+
// Initialize thread state fields
266+
self->thread_state->gc_lock_depth = 0;
267+
self->thread_state->nlr_top = NULL;
268+
self->thread_state->nlr_jump_callback_top = NULL;
269+
self->thread_state->mp_pending_exception = MP_OBJ_NULL;
270+
271+
// Inherit globals/locals from main thread
272+
mp_locals_set(mp_state_ctx.thread.dict_locals);
273+
mp_globals_set(mp_state_ctx.thread.dict_globals);
274+
265275
// Get stack info from current thread
266276
struct k_thread *current = k_current_get();
267277
void *stack_top = (void *)((uintptr_t)current->stack_info.start + current->stack_info.size);

0 commit comments

Comments
 (0)