Skip to content

Commit cf2366c

Browse files
chinglee-iotaggarg
andauthored
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 <[email protected]> Co-authored-by: Gaurav Aggarwal <[email protected]>
1 parent e6f6d0e commit cf2366c

File tree

1 file changed

+36
-37
lines changed

1 file changed

+36
-37
lines changed

tasks.c

Lines changed: 36 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -2190,6 +2190,7 @@ static void prvInitialiseNewTask( TaskFunction_t pxTaskCode,
21902190
void vTaskDelete( TaskHandle_t xTaskToDelete )
21912191
{
21922192
TCB_t * pxTCB;
2193+
BaseType_t xDeleteTCBInIdleTask = pdFALSE;
21932194

21942195
traceENTER_vTaskDelete( xTaskToDelete );
21952196

@@ -2247,6 +2248,9 @@ static void prvInitialiseNewTask( TaskFunction_t pxTaskCode,
22472248
* portPRE_TASK_DELETE_HOOK() does not return in the Win32 port. */
22482249
traceTASK_DELETE( pxTCB );
22492250

2251+
/* Delete the task TCB in idle task. */
2252+
xDeleteTCBInIdleTask = pdTRUE;
2253+
22502254
/* The pre-delete hook is primarily for the Windows simulator,
22512255
* in which Windows specific clean up operations are performed,
22522256
* after which it is not possible to yield away from this task -
@@ -2268,61 +2272,56 @@ static void prvInitialiseNewTask( TaskFunction_t pxTaskCode,
22682272
prvResetNextTaskUnblockTime();
22692273
}
22702274
}
2275+
taskEXIT_CRITICAL();
22712276

2272-
#if ( configNUMBER_OF_CORES == 1 )
2277+
/* If the task is not deleting itself, call prvDeleteTCB from outside of
2278+
* critical section. If a task deletes itself, prvDeleteTCB is called
2279+
* from prvCheckTasksWaitingTermination which is called from Idle task. */
2280+
if( xDeleteTCBInIdleTask != pdTRUE )
22732281
{
2274-
taskEXIT_CRITICAL();
2275-
2276-
/* If the task is not deleting itself, call prvDeleteTCB from outside of
2277-
* critical section. If a task deletes itself, prvDeleteTCB is called
2278-
* from prvCheckTasksWaitingTermination which is called from Idle task. */
2279-
if( pxTCB != pxCurrentTCB )
2280-
{
2281-
prvDeleteTCB( pxTCB );
2282-
}
2282+
prvDeleteTCB( pxTCB );
2283+
}
22832284

2284-
/* Force a reschedule if it is the currently running task that has just
2285-
* been deleted. */
2286-
if( xSchedulerRunning != pdFALSE )
2285+
/* Force a reschedule if it is the currently running task that has just
2286+
* been deleted. */
2287+
if( xSchedulerRunning != pdFALSE )
2288+
{
2289+
#if ( configNUMBER_OF_CORES == 1 )
22872290
{
22882291
if( pxTCB == pxCurrentTCB )
22892292
{
22902293
configASSERT( uxSchedulerSuspended == 0 );
2291-
portYIELD_WITHIN_API();
2294+
taskYIELD_WITHIN_API();
22922295
}
22932296
else
22942297
{
22952298
mtCOVERAGE_TEST_MARKER();
22962299
}
22972300
}
2298-
}
2299-
#else /* #if ( configNUMBER_OF_CORES == 1 ) */
2300-
{
2301-
/* If a running task is not deleting itself, call prvDeleteTCB. If a running
2302-
* task deletes itself, prvDeleteTCB is called from prvCheckTasksWaitingTermination
2303-
* which is called from Idle task. */
2304-
if( pxTCB->xTaskRunState == taskTASK_NOT_RUNNING )
2305-
{
2306-
prvDeleteTCB( pxTCB );
2307-
}
2308-
2309-
/* Force a reschedule if the task that has just been deleted was running. */
2310-
if( ( xSchedulerRunning != pdFALSE ) && ( taskTASK_IS_RUNNING( pxTCB ) == pdTRUE ) )
2301+
#else /* #if ( configNUMBER_OF_CORES == 1 ) */
23112302
{
2312-
if( pxTCB->xTaskRunState == ( BaseType_t ) portGET_CORE_ID() )
2313-
{
2314-
configASSERT( uxSchedulerSuspended == 0 );
2315-
vTaskYieldWithinAPI();
2316-
}
2317-
else
2303+
/* It is important to use critical section here because
2304+
* checking run state of a task must be done inside a
2305+
* critical section. */
2306+
taskENTER_CRITICAL();
23182307
{
2319-
prvYieldCore( pxTCB->xTaskRunState );
2308+
if( taskTASK_IS_RUNNING( pxTCB ) == pdTRUE )
2309+
{
2310+
if( pxTCB->xTaskRunState == ( BaseType_t ) portGET_CORE_ID() )
2311+
{
2312+
configASSERT( uxSchedulerSuspended == 0 );
2313+
taskYIELD_WITHIN_API();
2314+
}
2315+
else
2316+
{
2317+
prvYieldCore( pxTCB->xTaskRunState );
2318+
}
2319+
}
23202320
}
2321+
taskEXIT_CRITICAL();
23212322
}
2322-
2323-
taskEXIT_CRITICAL();
2323+
#endif /* #if ( configNUMBER_OF_CORES == 1 ) */
23242324
}
2325-
#endif /* #if ( configNUMBER_OF_CORES == 1 ) */
23262325

23272326
traceRETURN_vTaskDelete();
23282327
}

0 commit comments

Comments
 (0)