From 621f40b12c272788533dcafcef62ed2a3449bb3b Mon Sep 17 00:00:00 2001 From: Damien George Date: Tue, 2 Jun 2020 16:42:37 +1000 Subject: [PATCH] esp32/mpthreadport: Fix calculation of thread stack size. With this commit the code should work correctly regardless of the size of StackType_t (it's actually 1 byte in size for the esp32's custom FreeRTOS). Fixes issue #6072. --- ports/esp32/main.c | 5 ++--- ports/esp32/mpthreadport.c | 10 +++++----- 2 files changed, 7 insertions(+), 8 deletions(-) diff --git a/ports/esp32/main.c b/ports/esp32/main.c index 81e4a6434..6f9ab82d0 100644 --- a/ports/esp32/main.c +++ b/ports/esp32/main.c @@ -65,7 +65,6 @@ // MicroPython runs as a task under FreeRTOS #define MP_TASK_PRIORITY (ESP_TASK_PRIO_MIN + 1) #define MP_TASK_STACK_SIZE (16 * 1024) -#define MP_TASK_STACK_LEN (MP_TASK_STACK_SIZE / sizeof(StackType_t)) int vprintf_null(const char *format, va_list ap) { // do nothing: this is used as a log target during raw repl mode @@ -75,7 +74,7 @@ int vprintf_null(const char *format, va_list ap) { void mp_task(void *pvParameter) { volatile uint32_t sp = (uint32_t)get_sp(); #if MICROPY_PY_THREAD - mp_thread_init(pxTaskGetStackStart(NULL), MP_TASK_STACK_LEN); + mp_thread_init(pxTaskGetStackStart(NULL), MP_TASK_STACK_SIZE / sizeof(uintptr_t)); #endif uart_init(); @@ -169,7 +168,7 @@ void app_main(void) { nvs_flash_erase(); nvs_flash_init(); } - xTaskCreatePinnedToCore(mp_task, "mp_task", MP_TASK_STACK_LEN, NULL, MP_TASK_PRIORITY, &mp_main_task_handle, MP_TASK_COREID); + xTaskCreatePinnedToCore(mp_task, "mp_task", MP_TASK_STACK_SIZE / sizeof(StackType_t), NULL, MP_TASK_PRIORITY, &mp_main_task_handle, MP_TASK_COREID); } void nlr_jump_fail(void *val) { diff --git a/ports/esp32/mpthreadport.c b/ports/esp32/mpthreadport.c index f39c99b68..d294c9272 100644 --- a/ports/esp32/mpthreadport.c +++ b/ports/esp32/mpthreadport.c @@ -83,7 +83,7 @@ void mp_thread_gc_others(void) { if (!th->ready) { continue; } - gc_collect_root(th->stack, th->stack_len); // probably not needed + gc_collect_root(th->stack, th->stack_len); } mp_thread_mutex_unlock(&thread_mutex); } @@ -140,17 +140,17 @@ void mp_thread_create_ex(void *(*entry)(void *), void *arg, size_t *stack_size, mp_raise_msg(&mp_type_OSError, MP_ERROR_TEXT("can't create thread")); } - // adjust the stack_size to provide room to recover from hitting the limit - *stack_size -= 1024; - // add thread to linked list of all threads th->ready = 0; th->arg = arg; th->stack = pxTaskGetStackStart(th->id); - th->stack_len = *stack_size / sizeof(StackType_t); + th->stack_len = *stack_size / sizeof(uintptr_t); th->next = thread; thread = th; + // adjust the stack_size to provide room to recover from hitting the limit + *stack_size -= 1024; + mp_thread_mutex_unlock(&thread_mutex); }