Skip to content

Commit

Permalink
fix(freertos): Fixed assert failure in vTaskDeleteWithCaps
Browse files Browse the repository at this point in the history
This commit fixes an assert failure in vTaskDeleteWithCaps() when
multiple un-pinned tasks are created with stack in the external memory
and such tasks delete themselves.

Closes #14222
  • Loading branch information
sudeep-mohanty committed Sep 20, 2024
1 parent 288fd05 commit 2275491
Showing 1 changed file with 42 additions and 60 deletions.
102 changes: 42 additions & 60 deletions components/freertos/esp_additions/idf_additions.c
Original file line number Diff line number Diff line change
Expand Up @@ -85,15 +85,44 @@

#if ( configSUPPORT_STATIC_ALLOCATION == 1 )

static void prvTaskDeleteWithCapsTask( void * pvParameters )
static void prvTaskDeleteWithCaps( TaskHandle_t xTaskToDelete )
{
TaskHandle_t xTaskToDelete = ( TaskHandle_t ) pvParameters;
/* Return value unused if asserts are disabled */
BaseType_t __attribute__( ( unused ) ) xResult;
StaticTask_t * pxTaskBuffer;
StackType_t * puxStackBuffer;

/* The task to be deleted must not be running.
* So we suspend the task before deleting it. */
vTaskSuspend( xTaskToDelete );

/* Wait for the task to be suspended */
while( eRunning == eTaskGetState( xTaskToDelete ) )
{
taskYIELD();
}

/* The task to be deleted must not be running */
configASSERT( eRunning != eTaskGetState( xTaskToDelete ) );

xResult = xTaskGetStaticBuffers( xTaskToDelete, &puxStackBuffer, &pxTaskBuffer );
configASSERT( xResult == pdTRUE );
configASSERT( puxStackBuffer != NULL );
configASSERT( pxTaskBuffer != NULL );

/* We can delete the task and free the memory buffers. */
vTaskDelete( xTaskToDelete );

/* Free the memory buffers */
heap_caps_free( puxStackBuffer );
vPortFree( pxTaskBuffer );
}

static void prvTaskDeleteWithCapsTask( void * pvParameters )
{
TaskHandle_t xTaskToDelete = ( TaskHandle_t ) pvParameters;

/* Delete the WithCaps task */
vTaskDeleteWithCaps( xTaskToDelete );
prvTaskDeleteWithCaps( xTaskToDelete );

/* Delete the temporary clean up task */
vTaskDelete( NULL );
Expand All @@ -102,7 +131,7 @@
void vTaskDeleteWithCaps( TaskHandle_t xTaskToDelete )
{
/* THIS FUNCTION SHOULD NOT BE CALLED FROM AN INTERRUPT CONTEXT. */
/*TODO: Update it to use portASSERT_IF_IN_ISR() instead. (IDF-10540) */
/* TODO: Update it to use portASSERT_IF_IN_ISR() instead. (IDF-10540) */
vPortAssertIfInISR();

TaskHandle_t xCurrentTaskHandle = xTaskGetCurrentTaskHandle();
Expand Down Expand Up @@ -155,58 +184,8 @@
}
}

#if ( configNUM_CORES > 1 )
else if( eRunning == eTaskGetState( xTaskToDelete ) )
{
/* The WithCaps task is running on another core.
* We suspend the task first and then delete it. */
vTaskSuspend( xTaskToDelete );

/* Wait for the task to be suspended */
while( eRunning == eTaskGetState( xTaskToDelete ) )
{
portYIELD_WITHIN_API();
}

BaseType_t xResult;
StaticTask_t * pxTaskBuffer;
StackType_t * puxStackBuffer;

xResult = xTaskGetStaticBuffers( xTaskToDelete, &puxStackBuffer, &pxTaskBuffer );
configASSERT( xResult == pdTRUE );
configASSERT( puxStackBuffer != NULL );
configASSERT( pxTaskBuffer != NULL );

/* Delete the task */
vTaskDelete( xTaskToDelete );

/* Free the memory buffers */
heap_caps_free( puxStackBuffer );
vPortFree( pxTaskBuffer );
}
#endif /* if ( configNUM_CORES > 1 ) */
else
{
/* The WithCaps task is not running and is being deleted
* from another task's context. */
configASSERT( eRunning != eTaskGetState( xTaskToDelete ) );

BaseType_t xResult;
StaticTask_t * pxTaskBuffer;
StackType_t * puxStackBuffer;

xResult = xTaskGetStaticBuffers( xTaskToDelete, &puxStackBuffer, &pxTaskBuffer );
configASSERT( xResult == pdTRUE );
configASSERT( puxStackBuffer != NULL );
configASSERT( pxTaskBuffer != NULL );

/* We can delete the task and free the memory buffers. */
vTaskDelete( xTaskToDelete );

/* Free the memory buffers */
heap_caps_free( puxStackBuffer );
vPortFree( pxTaskBuffer );
} /* if( ( xTaskToDelete == NULL ) || ( xTaskToDelete == xCurrentTaskHandle ) ) */
/* Delete the WithCaps task */
prvTaskDeleteWithCaps( xTaskToDelete );
}

/* ---------------------------------- Queue --------------------------------- */
Expand Down Expand Up @@ -254,7 +233,8 @@

void vQueueDeleteWithCaps( QueueHandle_t xQueue )
{
BaseType_t xResult;
/* Return value unused if asserts are disabled */
BaseType_t __attribute__( ( unused ) ) xResult;
StaticQueue_t * pxQueueBuffer;
uint8_t * pucQueueStorageBuffer;

Expand Down Expand Up @@ -316,7 +296,8 @@

void vSemaphoreDeleteWithCaps( SemaphoreHandle_t xSemaphore )
{
BaseType_t xResult;
/* Return value unused if asserts are disabled */
BaseType_t __attribute__( ( unused ) ) xResult;
StaticSemaphore_t * pxSemaphoreBuffer;

/* Retrieve the buffer used to create the semaphore before deleting it
Expand Down Expand Up @@ -378,7 +359,8 @@
void vStreamBufferGenericDeleteWithCaps( StreamBufferHandle_t xStreamBuffer,
BaseType_t xIsMessageBuffer )
{
BaseType_t xResult;
/* Return value unused if asserts are disabled */
BaseType_t __attribute__( ( unused ) ) xResult;
StaticStreamBuffer_t * pxStaticStreamBuffer;
uint8_t * pucStreamBufferStorageArea;

Expand Down

0 comments on commit 2275491

Please sign in to comment.