esp32: Simplify thread cleanup.

Now we only support the case of
!CONFIG_FREERTOS_ENABLE_STATIC_TASK_CLEAN_UP, can simplify
the cleanup code.

This work was funded through GitHub Sponsors.

Signed-off-by: Angus Gratton <angus@redyak.com.au>
This commit is contained in:
Angus Gratton 2024-12-04 17:11:15 +11:00 committed by Damien George
parent d90aff5e13
commit d4d1d4798c

View file

@ -41,12 +41,6 @@
#define MP_THREAD_DEFAULT_STACK_SIZE (MP_THREAD_MIN_STACK_SIZE + MICROPY_STACK_CHECK_MARGIN) #define MP_THREAD_DEFAULT_STACK_SIZE (MP_THREAD_MIN_STACK_SIZE + MICROPY_STACK_CHECK_MARGIN)
#define MP_THREAD_PRIORITY (ESP_TASK_PRIO_MIN + 1) #define MP_THREAD_PRIORITY (ESP_TASK_PRIO_MIN + 1)
#if !CONFIG_FREERTOS_ENABLE_STATIC_TASK_CLEAN_UP
#define FREERTOS_TASK_DELETE_HOOK vTaskPreDeletionHook
#else
#define FREERTOS_TASK_DELETE_HOOK vPortCleanUpTCB
#endif
// this structure forms a linked list, one node per active thread // this structure forms a linked list, one node per active thread
typedef struct _mp_thread_t { typedef struct _mp_thread_t {
TaskHandle_t id; // system id of thread TaskHandle_t id; // system id of thread
@ -76,7 +70,7 @@ void mp_thread_init(void *stack, uint32_t stack_len) {
// memory barrier to ensure above data is committed // memory barrier to ensure above data is committed
__sync_synchronize(); __sync_synchronize();
// FREERTOS_TASK_DELETE_HOOK needs the thread ready after thread_mutex is ready // vTaskPreDeletionHook needs the thread ready after thread_mutex is ready
thread = &thread_entry0; thread = &thread_entry0;
} }
@ -180,9 +174,10 @@ void mp_thread_finish(void) {
mp_thread_mutex_unlock(&thread_mutex); mp_thread_mutex_unlock(&thread_mutex);
} }
// This is called from the FreeRTOS idle task and is not within Python context, // This is called either from vTaskDelete() or from the FreeRTOS idle task, so
// so MP_STATE_THREAD is not valid and it does not have the GIL. // may not be within Python context. Therefore MP_STATE_THREAD may not be valid
void FREERTOS_TASK_DELETE_HOOK(void *tcb) { // and it does not have the GIL.
void vTaskPreDeletionHook(void *tcb) {
if (thread == NULL) { if (thread == NULL) {
// threading not yet initialised // threading not yet initialised
return; return;
@ -243,7 +238,7 @@ void mp_thread_deinit(void) {
// No tasks left to delete // No tasks left to delete
break; break;
} else { } else {
// Call FreeRTOS to delete the task (it will call FREERTOS_TASK_DELETE_HOOK) // Call FreeRTOS to delete the task (it will call vTaskPreDeletionHook)
vTaskDelete(id); vTaskDelete(id);
} }
} }
@ -251,7 +246,7 @@ void mp_thread_deinit(void) {
#else #else
void FREERTOS_TASK_DELETE_HOOK(void *tcb) { void vTaskPreDeletionHook(void *tcb) {
} }
#endif // MICROPY_PY_THREAD #endif // MICROPY_PY_THREAD