diff options
| author | Damien George <damien.p.george@gmail.com> | 2018-11-28 15:00:45 +1100 |
|---|---|---|
| committer | Damien George <damien.p.george@gmail.com> | 2018-11-28 15:00:45 +1100 |
| commit | 485514f57a7e629abf6b23e5ee7e2d5dc7cdf238 (patch) | |
| tree | 90cba185a7f8a3e46e55822bb8324e4247a553f3 /ports/esp32/mpthreadport.c | |
| parent | 0233049b794d8ed45f1f8dbaffb30e6b78aabb7e (diff) | |
esp32: Allocate task TCB and stack from system heap not uPy heap.
This is necessary for two reasons: 1) FreeRTOS still needs the TCB data
structure even after vPortCleanUpTCB has been called, so this latter hook
function cannot free the TCB, and there is no where else to safely delete
it (this behaviour has changed recently in the ESP IDF); 2) when using
external SPI RAM the uPy heap is in this external memory but the task stack
must be allocated from internal SRAM.
Fixes issue #3904.
Diffstat (limited to 'ports/esp32/mpthreadport.c')
| -rw-r--r-- | ports/esp32/mpthreadport.c | 17 |
1 files changed, 5 insertions, 12 deletions
diff --git a/ports/esp32/mpthreadport.c b/ports/esp32/mpthreadport.c index b002c880e..52d4d7ff4 100644 --- a/ports/esp32/mpthreadport.c +++ b/ports/esp32/mpthreadport.c @@ -47,7 +47,6 @@ typedef struct _thread_t { int ready; // whether the thread is ready and running void *arg; // thread Python args, a GC root pointer void *stack; // pointer to the stack - StaticTask_t *tcb; // pointer to the Task Control Block size_t stack_len; // number of words in the stack struct _thread_t *next; } thread_t; @@ -125,16 +124,14 @@ void mp_thread_create_ex(void *(*entry)(void*), void *arg, size_t *stack_size, i *stack_size = MP_THREAD_MIN_STACK_SIZE; // minimum stack size } - // allocate TCB, stack and linked-list node (must be outside thread_mutex lock) - StaticTask_t *tcb = m_new(StaticTask_t, 1); - StackType_t *stack = m_new(StackType_t, *stack_size / sizeof(StackType_t)); + // Allocate linked-list node (must be outside thread_mutex lock) thread_t *th = m_new_obj(thread_t); mp_thread_mutex_lock(&thread_mutex, 1); // create thread - TaskHandle_t id = xTaskCreateStaticPinnedToCore(freertos_entry, name, *stack_size / sizeof(StackType_t), arg, priority, stack, tcb, 0); - if (id == NULL) { + BaseType_t result = xTaskCreate(freertos_entry, name, *stack_size / sizeof(StackType_t), arg, priority, &th->id); + if (result != pdPASS) { mp_thread_mutex_unlock(&thread_mutex); nlr_raise(mp_obj_new_exception_msg(&mp_type_OSError, "can't create thread")); } @@ -143,11 +140,9 @@ void mp_thread_create_ex(void *(*entry)(void*), void *arg, size_t *stack_size, i *stack_size -= 1024; // add thread to linked list of all threads - th->id = id; th->ready = 0; th->arg = arg; - th->stack = stack; - th->tcb = tcb; + th->stack = pxTaskGetStackStart(th->id); th->stack_len = *stack_size / sizeof(StackType_t); th->next = thread; thread = th; @@ -175,7 +170,7 @@ void vPortCleanUpTCB(void *tcb) { mp_thread_mutex_lock(&thread_mutex, 1); for (thread_t *th = thread; th != NULL; prev = th, th = th->next) { // unlink the node from the list - if (th->tcb == tcb) { + if ((void*)th->id == tcb) { if (prev != NULL) { prev->next = th->next; } else { @@ -183,8 +178,6 @@ void vPortCleanUpTCB(void *tcb) { thread = th->next; } // explicitly release all its memory - m_del(StaticTask_t, th->tcb, 1); - m_del(StackType_t, th->stack, th->stack_len); m_del(thread_t, th, 1); break; } |
