From 4fdf795efa4eca3a9f8166e33b991a762569ae20 Mon Sep 17 00:00:00 2001 From: leo chung Date: Thu, 16 Sep 2021 17:02:14 +0800 Subject: [PATCH] esp32/mpthreadport: Fix TCB cleanup function so thread_mutex is ready. Because vPortCleanUpTCB is called by the FreeRTOS idle task, and it checks thread, but didn't check the thread_mutex. And if thread is not NULL, but thread_mutex not ready then it will crash with an error when calling mp_thread_mutex_lock(&thread_mutex, 1). As suggested by @dpgeorge, move the thread = &thread_entry0 line to the end of mp_thread_init(). Signed-off-by: leo chung --- ports/esp32/mpthreadport.c | 19 ++++++++++++------- 1 file changed, 12 insertions(+), 7 deletions(-) diff --git a/ports/esp32/mpthreadport.c b/ports/esp32/mpthreadport.c index f575d99e6e..9d1c4a758c 100644 --- a/ports/esp32/mpthreadport.c +++ b/ports/esp32/mpthreadport.c @@ -59,14 +59,19 @@ STATIC thread_t *thread = NULL; // root pointer, handled by mp_thread_gc_others void mp_thread_init(void *stack, uint32_t stack_len) { mp_thread_set_state(&mp_state_ctx.thread); // create the first entry in the linked list of all threads - thread = &thread_entry0; - thread->id = xTaskGetCurrentTaskHandle(); - thread->ready = 1; - thread->arg = NULL; - thread->stack = stack; - thread->stack_len = stack_len; - thread->next = NULL; + thread_entry0.id = xTaskGetCurrentTaskHandle(); + thread_entry0.ready = 1; + thread_entry0.arg = NULL; + thread_entry0.stack = stack; + thread_entry0.stack_len = stack_len; + thread_entry0.next = NULL; mp_thread_mutex_init(&thread_mutex); + + // memory barrier to ensure above data is committed + __sync_synchronize(); + + // vPortCleanUpTCB needs the thread ready after thread_mutex is ready + thread = &thread_entry0; } void mp_thread_gc_others(void) {