Skip to content

Commit

Permalink
esp32: Simplify thread cleanup.
Browse files Browse the repository at this point in the history
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>
  • Loading branch information
projectgus authored and dpgeorge committed Dec 10, 2024
1 parent d90aff5 commit d4d1d47
Showing 1 changed file with 7 additions and 12 deletions.
19 changes: 7 additions & 12 deletions ports/esp32/mpthreadport.c
Original file line number Diff line number Diff line change
Expand Up @@ -41,12 +41,6 @@
#define MP_THREAD_DEFAULT_STACK_SIZE (MP_THREAD_MIN_STACK_SIZE + MICROPY_STACK_CHECK_MARGIN)
#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
typedef struct _mp_thread_t {
TaskHandle_t id; // system id of thread
Expand Down Expand Up @@ -76,7 +70,7 @@ void mp_thread_init(void *stack, uint32_t stack_len) {
// memory barrier to ensure above data is committed
__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;
}

Expand Down Expand Up @@ -180,9 +174,10 @@ void mp_thread_finish(void) {
mp_thread_mutex_unlock(&thread_mutex);
}

// This is called from the FreeRTOS idle task and is not within Python context,
// so MP_STATE_THREAD is not valid and it does not have the GIL.
void FREERTOS_TASK_DELETE_HOOK(void *tcb) {
// This is called either from vTaskDelete() or from the FreeRTOS idle task, so
// may not be within Python context. Therefore MP_STATE_THREAD may not be valid
// and it does not have the GIL.
void vTaskPreDeletionHook(void *tcb) {
if (thread == NULL) {
// threading not yet initialised
return;
Expand Down Expand Up @@ -243,15 +238,15 @@ void mp_thread_deinit(void) {
// No tasks left to delete
break;
} 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);
}
}
}

#else

void FREERTOS_TASK_DELETE_HOOK(void *tcb) {
void vTaskPreDeletionHook(void *tcb) {
}

#endif // MICROPY_PY_THREAD

0 comments on commit d4d1d47

Please sign in to comment.