summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorleo chung <gewalalb@gmail.com>2021-09-16 17:02:14 +0800
committerDamien George <damien@micropython.org>2021-09-24 12:12:03 +1000
commit4fdf795efa4eca3a9f8166e33b991a762569ae20 (patch)
tree88054f79c606e33493d81b812642e649a575274d
parenta39a596b791f14e277ba526f9b6bd8ce2da36550 (diff)
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 <gewalalb@gmail.com>
-rw-r--r--ports/esp32/mpthreadport.c19
1 files changed, 12 insertions, 7 deletions
diff --git a/ports/esp32/mpthreadport.c b/ports/esp32/mpthreadport.c
index f575d99e6..9d1c4a758 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) {