Update unpaired critical section in vTaskDelete for readability (#958)

* Modify unpaired critical section for readability
* Move prvDeleteTCB out of critical section for SMP

---------

Signed-off-by: Gaurav Aggarwal <aggarg@amazon.com>
Co-authored-by: Gaurav Aggarwal <aggarg@amazon.com>
This commit is contained in:
chinglee-iot 2024-01-23 17:02:15 +08:00 committed by GitHub
parent e6f6d0ecf4
commit cf2366c949
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

33
tasks.c
View file

@ -2190,6 +2190,7 @@ static void prvInitialiseNewTask( TaskFunction_t pxTaskCode,
void vTaskDelete( TaskHandle_t xTaskToDelete ) void vTaskDelete( TaskHandle_t xTaskToDelete )
{ {
TCB_t * pxTCB; TCB_t * pxTCB;
BaseType_t xDeleteTCBInIdleTask = pdFALSE;
traceENTER_vTaskDelete( xTaskToDelete ); traceENTER_vTaskDelete( xTaskToDelete );
@ -2247,6 +2248,9 @@ static void prvInitialiseNewTask( TaskFunction_t pxTaskCode,
* portPRE_TASK_DELETE_HOOK() does not return in the Win32 port. */ * portPRE_TASK_DELETE_HOOK() does not return in the Win32 port. */
traceTASK_DELETE( pxTCB ); traceTASK_DELETE( pxTCB );
/* Delete the task TCB in idle task. */
xDeleteTCBInIdleTask = pdTRUE;
/* The pre-delete hook is primarily for the Windows simulator, /* The pre-delete hook is primarily for the Windows simulator,
* in which Windows specific clean up operations are performed, * in which Windows specific clean up operations are performed,
* after which it is not possible to yield away from this task - * after which it is not possible to yield away from this task -
@ -2268,15 +2272,12 @@ static void prvInitialiseNewTask( TaskFunction_t pxTaskCode,
prvResetNextTaskUnblockTime(); prvResetNextTaskUnblockTime();
} }
} }
#if ( configNUMBER_OF_CORES == 1 )
{
taskEXIT_CRITICAL(); taskEXIT_CRITICAL();
/* If the task is not deleting itself, call prvDeleteTCB from outside of /* If the task is not deleting itself, call prvDeleteTCB from outside of
* critical section. If a task deletes itself, prvDeleteTCB is called * critical section. If a task deletes itself, prvDeleteTCB is called
* from prvCheckTasksWaitingTermination which is called from Idle task. */ * from prvCheckTasksWaitingTermination which is called from Idle task. */
if( pxTCB != pxCurrentTCB ) if( xDeleteTCBInIdleTask != pdTRUE )
{ {
prvDeleteTCB( pxTCB ); prvDeleteTCB( pxTCB );
} }
@ -2284,45 +2285,43 @@ static void prvInitialiseNewTask( TaskFunction_t pxTaskCode,
/* Force a reschedule if it is the currently running task that has just /* Force a reschedule if it is the currently running task that has just
* been deleted. */ * been deleted. */
if( xSchedulerRunning != pdFALSE ) if( xSchedulerRunning != pdFALSE )
{
#if ( configNUMBER_OF_CORES == 1 )
{ {
if( pxTCB == pxCurrentTCB ) if( pxTCB == pxCurrentTCB )
{ {
configASSERT( uxSchedulerSuspended == 0 ); configASSERT( uxSchedulerSuspended == 0 );
portYIELD_WITHIN_API(); taskYIELD_WITHIN_API();
} }
else else
{ {
mtCOVERAGE_TEST_MARKER(); mtCOVERAGE_TEST_MARKER();
} }
} }
}
#else /* #if ( configNUMBER_OF_CORES == 1 ) */ #else /* #if ( configNUMBER_OF_CORES == 1 ) */
{ {
/* If a running task is not deleting itself, call prvDeleteTCB. If a running /* It is important to use critical section here because
* task deletes itself, prvDeleteTCB is called from prvCheckTasksWaitingTermination * checking run state of a task must be done inside a
* which is called from Idle task. */ * critical section. */
if( pxTCB->xTaskRunState == taskTASK_NOT_RUNNING ) taskENTER_CRITICAL();
{ {
prvDeleteTCB( pxTCB ); if( taskTASK_IS_RUNNING( pxTCB ) == pdTRUE )
}
/* Force a reschedule if the task that has just been deleted was running. */
if( ( xSchedulerRunning != pdFALSE ) && ( taskTASK_IS_RUNNING( pxTCB ) == pdTRUE ) )
{ {
if( pxTCB->xTaskRunState == ( BaseType_t ) portGET_CORE_ID() ) if( pxTCB->xTaskRunState == ( BaseType_t ) portGET_CORE_ID() )
{ {
configASSERT( uxSchedulerSuspended == 0 ); configASSERT( uxSchedulerSuspended == 0 );
vTaskYieldWithinAPI(); taskYIELD_WITHIN_API();
} }
else else
{ {
prvYieldCore( pxTCB->xTaskRunState ); prvYieldCore( pxTCB->xTaskRunState );
} }
} }
}
taskEXIT_CRITICAL(); taskEXIT_CRITICAL();
} }
#endif /* #if ( configNUMBER_OF_CORES == 1 ) */ #endif /* #if ( configNUMBER_OF_CORES == 1 ) */
}
traceRETURN_vTaskDelete(); traceRETURN_vTaskDelete();
} }