Suspending, resuming and ending a task in FreeRTOS
How vTaskSuspend and vTaskResume move a task in and out of xSuspendedTaskList, how the kernel tells a suspended task from one that waits forever, and why a task that deletes itself is freed by the idle task.
Simulated kernel: FreeRTOS V11.3.3. This scenario and every other one are in the scheduler playground. The simulator and its data files are not covered by the site's CC BY-SA license; see their license notices.
The task states lesson follows Worker through every state. This
walkthrough reads the three FreeRTOS functions behind the last ones: vTaskSuspend(),
vTaskResume() and vTaskDelete(). Set the step level to Line and open the Kernel tab.
Worker (priority 1) works for three ticks, sleeps for two, waits for the semaphore S and works
one more tick. Supervisor (priority 2) starts at tick 1. It suspends Worker, resumes it a tick
later, sleeps until tick 8, gives S and ends.
Suspending: vTaskSuspend
void vTaskSuspend( TaskHandle_t xTaskToSuspend )
{
TCB_t * pxTCB;
traceENTER_vTaskSuspend( xTaskToSuspend );
taskENTER_CRITICAL();
{
/* If null is passed in here then it is the running task that is
* being suspended. */
pxTCB = prvGetTCBFromHandle( xTaskToSuspend );
configASSERT( pxTCB != NULL );
traceTASK_SUSPEND( pxTCB );
/* Remove task from the ready/delayed list and place in the
* suspended list. */
if( uxListRemove( &( pxTCB->xStateListItem ) ) == ( UBaseType_t ) 0 )
{
taskRESET_READY_PRIORITY( pxTCB->uxPriority );
}
else
{
mtCOVERAGE_TEST_MARKER();
}
/* Is the task waiting on an event also? */
if( listLIST_ITEM_CONTAINER( &( pxTCB->xEventListItem ) ) != NULL )
{
( void ) uxListRemove( &( pxTCB->xEventListItem ) );
}
else
{
mtCOVERAGE_TEST_MARKER();
}
vListInsertEnd( &xSuspendedTaskList, &( pxTCB->xStateListItem ) );
#if ( configUSE_TASK_NOTIFICATIONS == 1 )
{
BaseType_t x;
for( x = ( BaseType_t ) 0; x < ( BaseType_t ) configTASK_NOTIFICATION_ARRAY_ENTRIES; x++ )
{
if( pxTCB->ucNotifyState[ x ] == taskWAITING_NOTIFICATION )
{
/* The task was blocked to wait for a notification, but is
* now suspended, so no notification was received. */
pxTCB->ucNotifyState[ x ] = taskNOT_WAITING_NOTIFICATION;
}
}
}
#endif /* if ( configUSE_TASK_NOTIFICATIONS == 1 ) */
/* In the case of SMP, it is possible that the task being suspended
* is running on another core. We must evict the task before
* exiting the critical section to ensure that the task cannot
* take an action which puts it back on ready/state/event list,
* thereby nullifying the suspend operation. Once evicted, the
* task won't be scheduled before it is resumed as it will no longer
* be on the ready list. */
#if ( configNUMBER_OF_CORES > 1 )
{
if( xSchedulerRunning != pdFALSE )
{
/* Reset the next expected unblock time in case it referred to the
* task that is now in the Suspended state. */
prvResetNextTaskUnblockTime();
if( taskTASK_IS_RUNNING( pxTCB ) == pdTRUE )
{
if( pxTCB->xTaskRunState == ( BaseType_t ) portGET_CORE_ID() )
{
/* The current task has just been suspended. */
configASSERT( uxSchedulerSuspended == 0 );
vTaskYieldWithinAPI();
}
else
{
prvYieldCore( pxTCB->xTaskRunState );
}
}
else
{
mtCOVERAGE_TEST_MARKER();
}
}
else
{
mtCOVERAGE_TEST_MARKER();
}
}
#endif /* #if ( configNUMBER_OF_CORES > 1 ) */
}
taskEXIT_CRITICAL();
#if ( configNUMBER_OF_CORES == 1 )
{
UBaseType_t uxCurrentListLength;
if( xSchedulerRunning != pdFALSE )
{
/* Reset the next expected unblock time in case it referred to the
* task that is now in the Suspended state. */
taskENTER_CRITICAL();
{
prvResetNextTaskUnblockTime();
}
taskEXIT_CRITICAL();
}
else
{
mtCOVERAGE_TEST_MARKER();
}
if( pxTCB == pxCurrentTCB )
{
if( xSchedulerRunning != pdFALSE )
{
/* The current task has just been suspended. */
configASSERT( uxSchedulerSuspended == 0 );
portYIELD_WITHIN_API();
}
else
{
/* The scheduler is not running, but the task that was pointed
* to by pxCurrentTCB has just been suspended and pxCurrentTCB
* must be adjusted to point to a different task. */
/* Use a temp variable as a distinct sequence point for reading
* volatile variables prior to a comparison to ensure compliance
* with MISRA C 2012 Rule 13.2. */
uxCurrentListLength = listCURRENT_LIST_LENGTH( &xSuspendedTaskList );
if( uxCurrentListLength == uxCurrentNumberOfTasks )
{
/* No other tasks are ready, so set pxCurrentTCB back to
* NULL so when the next task is created pxCurrentTCB will
* be set to point to it no matter what its relative priority
* is. */
pxCurrentTCB = NULL;
}
else
{
vTaskSwitchContext();
}
}
}
else
{
mtCOVERAGE_TEST_MARKER();
}
}
#endif /* #if ( configNUMBER_OF_CORES == 1 ) */
traceRETURN_vTaskSuspend();
}
tasks.c, lines 3197–3353 · FreeRTOS V11.3.3 · MIT license
At tick 2 Supervisor calls vTaskSuspend( xWorker ). Worker is ready:
Supervisor preempted it at tick 1. Everything that changes a list happens in one critical
section:
uxListRemove()takesWorker’sxStateListItemout of whatever list it is in. Here that is the ready list for priority 1. It could as well be the delayed list, orxSuspendedTaskListfor a task that waits without a timeout.- If the task’s
xEventListItemis in an object’s waiting list, it is taken out too. A suspended task no longer waits for anything: giving the object later will not wake it.Workerwaits for nothing here. vListInsertEnd()puts the task intoxSuspendedTaskList.- If the task was waiting for a notification, that wait is cancelled as well.
After the critical section, prvResetNextTaskUnblockTime() runs, in case the task was the next one
due to wake: a suspended task has no wake-up time. Last, if the task suspended itself
(vTaskSuspend( NULL )), it yields. Worker is not the running task, so Supervisor carries on.
Nothing in the kernel ever takes a task out of xSuspendedTaskList because of time. The tick does
not look at it. Only vTaskResume() (or xTaskResumeFromISR()) can.
Suspended or waiting forever?
xSuspendedTaskList holds two kinds of task. A task that is suspended, and a task that waits
with portMAX_DELAY: it has no wake-up time to be sorted by, so it goes there instead of a delayed
list. The task states lesson shows Worker in
this list at tick 7, blocked on S, not suspended.
vTaskResume() must only resume the first kind. prvTaskIsTaskSuspended() tells them apart:
STATIC BaseType_t prvTaskIsTaskSuspended( const TaskHandle_t xTask )
{
BaseType_t xReturn = pdFALSE;
const TCB_t * const pxTCB = xTask;
/* Accesses xPendingReadyList so must be called from a critical
* section. */
/* It does not make sense to check if the calling task is suspended. */
configASSERT( xTask );
/* Is the task being resumed actually in the suspended list? */
if( listIS_CONTAINED_WITHIN( &xSuspendedTaskList, &( pxTCB->xStateListItem ) ) != pdFALSE )
{
/* Has the task already been resumed from within an ISR? */
if( listIS_CONTAINED_WITHIN( &xPendingReadyList, &( pxTCB->xEventListItem ) ) == pdFALSE )
{
/* Is it in the suspended list because it is in the Suspended
* state, or because it is blocked with no timeout? */
if( listIS_CONTAINED_WITHIN( NULL, &( pxTCB->xEventListItem ) ) != pdFALSE )
{
#if ( configUSE_TASK_NOTIFICATIONS == 1 )
{
BaseType_t x;
/* The task does not appear on the event list item of
* and of the RTOS objects, but could still be in the
* blocked state if it is waiting on its notification
* rather than waiting on an object. If not, is
* suspended. */
xReturn = pdTRUE;
for( x = ( BaseType_t ) 0; x < ( BaseType_t ) configTASK_NOTIFICATION_ARRAY_ENTRIES; x++ )
{
if( pxTCB->ucNotifyState[ x ] == taskWAITING_NOTIFICATION )
{
xReturn = pdFALSE;
break;
}
}
}
#else /* if ( configUSE_TASK_NOTIFICATIONS == 1 ) */
{
xReturn = pdTRUE;
}
#endif /* if ( configUSE_TASK_NOTIFICATIONS == 1 ) */
}
else
{
mtCOVERAGE_TEST_MARKER();
}
}
else
{
mtCOVERAGE_TEST_MARKER();
}
}
else
{
mtCOVERAGE_TEST_MARKER();
}
return xReturn;
}
tasks.c, lines 3360–3423 · FreeRTOS V11.3.3 · MIT license
A task is suspended only if all of these hold:
- its
xStateListItemis inxSuspendedTaskList; - its
xEventListItemis not inxPendingReadyList, which would mean an interrupt has already resumed it while the scheduler was suspended; - its
xEventListItemis in no list at all. A task that waits forever for an object is in that object’s waiting list; - it is not waiting for a task notification. A notification has no waiting list, so this is the only way to see that wait.
Resuming: vTaskResume
void vTaskResume( TaskHandle_t xTaskToResume )
{
TCB_t * const pxTCB = xTaskToResume;
traceENTER_vTaskResume( xTaskToResume );
/* It does not make sense to resume the calling task. */
configASSERT( xTaskToResume );
#if ( configNUMBER_OF_CORES == 1 )
/* The parameter cannot be NULL as it is impossible to resume the
* currently executing task. */
if( ( pxTCB != pxCurrentTCB ) && ( pxTCB != NULL ) )
#else
/* The parameter cannot be NULL as it is impossible to resume the
* currently executing task. It is also impossible to resume a task
* that is actively running on another core but it is not safe
* to check their run state here. Therefore, we get into a critical
* section and check if the task is actually suspended or not. */
if( pxTCB != NULL )
#endif
{
taskENTER_CRITICAL();
{
if( prvTaskIsTaskSuspended( pxTCB ) != pdFALSE )
{
traceTASK_RESUME( pxTCB );
/* The ready list can be accessed even if the scheduler is
* suspended because this is inside a critical section. */
( void ) uxListRemove( &( pxTCB->xStateListItem ) );
prvAddTaskToReadyList( pxTCB );
/* This yield may not cause the task just resumed to run,
* but will leave the lists in the correct state for the
* next yield. */
taskYIELD_ANY_CORE_IF_USING_PREEMPTION( pxTCB );
}
else
{
mtCOVERAGE_TEST_MARKER();
}
}
taskEXIT_CRITICAL();
}
else
{
mtCOVERAGE_TEST_MARKER();
}
traceRETURN_vTaskResume();
}
tasks.c, lines 3430–3483 · FreeRTOS V11.3.3 · MIT license
At tick 3 Supervisor calls vTaskResume( xWorker ). A task cannot resume
itself, so the function first checks that the handle is not pxCurrentTCB. Then, in a critical
section:
prvTaskIsTaskSuspended()answers yes forWorker: it waits for no object and no notification.uxListRemove()takes it out ofxSuspendedTaskList, andprvAddTaskToReadyList()puts it at the end of the ready list for priority 1.taskYIELD_ANY_CORE_IF_USING_PREEMPTION()yields if the resumed task is more urgent than the running one:
#define taskYIELD_ANY_CORE_IF_USING_PREEMPTION( pxTCB ) \
do { \
if( pxCurrentTCB->uxPriority < ( pxTCB )->uxPriority ) \
{ \
portYIELD_WITHIN_API(); \
} \
else \
{ \
mtCOVERAGE_TEST_MARKER(); \
} \
} while( 0 )
tasks.c, lines 88–98 · FreeRTOS V11.3.3 · MIT license
Worker (1) is not more urgent than Supervisor (2), so nothing switches yet. Worker runs a few
lines later, when Supervisor calls vTaskDelay( 5 ).
A task that was delayed when it was suspended does not go back to its delay. It goes straight to
its ready list, and its vTaskDelay() returns early. The same holds for a task that was waiting
for an object: the wait was cancelled at the suspension, and it runs again inside the call that was
waiting. See What to try.
Ending: vTaskDelete
A FreeRTOS task function must never return. A task that has finished deletes itself with
vTaskDelete( NULL ), and that is what the simulator does when a task’s program ends.
void vTaskDelete( TaskHandle_t xTaskToDelete )
{
TCB_t * pxTCB;
BaseType_t xDeleteTCBInIdleTask = pdFALSE;
BaseType_t xTaskIsRunningOrYielding;
traceENTER_vTaskDelete( xTaskToDelete );
taskENTER_CRITICAL();
{
/* If null is passed in here then it is the calling task that is
* being deleted. */
pxTCB = prvGetTCBFromHandle( xTaskToDelete );
configASSERT( pxTCB != NULL );
/* Remove task from the ready/delayed list. */
if( uxListRemove( &( pxTCB->xStateListItem ) ) == ( UBaseType_t ) 0 )
{
taskRESET_READY_PRIORITY( pxTCB->uxPriority );
}
else
{
mtCOVERAGE_TEST_MARKER();
}
/* Is the task waiting on an event also? */
if( listLIST_ITEM_CONTAINER( &( pxTCB->xEventListItem ) ) != NULL )
{
( void ) uxListRemove( &( pxTCB->xEventListItem ) );
}
else
{
mtCOVERAGE_TEST_MARKER();
}
/* Increment the uxTaskNumber also so kernel aware debuggers can
* detect that the task lists need re-generating. This is done before
* portPRE_TASK_DELETE_HOOK() as in the Windows port that macro will
* not return. */
uxTaskNumber++;
/* Use temp variable as distinct sequence points for reading volatile
* variables prior to a logical operator to ensure compliance with
* MISRA C 2012 Rule 13.5. */
xTaskIsRunningOrYielding = taskTASK_IS_RUNNING_OR_SCHEDULED_TO_YIELD( pxTCB );
/* If the task is running (or yielding), we must add it to the
* termination list so that an idle task can delete it when it is
* no longer running. */
if( ( xSchedulerRunning != pdFALSE ) && ( xTaskIsRunningOrYielding != pdFALSE ) )
{
/* A running task or a task which is scheduled to yield is being
* deleted. This cannot complete when the task is still running
* on a core, as a context switch to another task is required.
* Place the task in the termination list. The idle task will check
* the termination list and free up any memory allocated by the
* scheduler for the TCB and stack of the deleted task. */
vListInsertEnd( &xTasksWaitingTermination, &( pxTCB->xStateListItem ) );
/* Increment the ucTasksDeleted variable so the idle task knows
* there is a task that has been deleted and that it should therefore
* check the xTasksWaitingTermination list. */
++uxDeletedTasksWaitingCleanUp;
/* Call the delete hook before portPRE_TASK_DELETE_HOOK() as
* portPRE_TASK_DELETE_HOOK() does not return in the Win32 port. */
traceTASK_DELETE( pxTCB );
/* Delete the task TCB in idle task. */
xDeleteTCBInIdleTask = pdTRUE;
/* The pre-delete hook is primarily for the Windows simulator,
* in which Windows specific clean up operations are performed,
* after which it is not possible to yield away from this task -
* hence xYieldPending is used to latch that a context switch is
* required. */
#if ( configNUMBER_OF_CORES == 1 )
portPRE_TASK_DELETE_HOOK( pxTCB, &( xYieldPendings[ 0 ] ) );
#else
portPRE_TASK_DELETE_HOOK( pxTCB, &( xYieldPendings[ pxTCB->xTaskRunState ] ) );
#endif
/* In the case of SMP, it is possible that the task being deleted
* is running on another core. We must evict the task before
* exiting the critical section to ensure that the task cannot
* take an action which puts it back on ready/state/event list,
* thereby nullifying the delete operation. Once evicted, the
* task won't be scheduled ever as it will no longer be on the
* ready list. */
#if ( configNUMBER_OF_CORES > 1 )
{
if( taskTASK_IS_RUNNING( pxTCB ) == pdTRUE )
{
if( pxTCB->xTaskRunState == ( BaseType_t ) portGET_CORE_ID() )
{
configASSERT( uxSchedulerSuspended == 0 );
taskYIELD_WITHIN_API();
}
else
{
prvYieldCore( pxTCB->xTaskRunState );
}
}
}
#endif /* #if ( configNUMBER_OF_CORES > 1 ) */
}
else
{
--uxCurrentNumberOfTasks;
traceTASK_DELETE( pxTCB );
/* Reset the next expected unblock time in case it referred to
* the task that has just been deleted. */
prvResetNextTaskUnblockTime();
}
}
taskEXIT_CRITICAL();
/* If the task is not deleting itself, call prvDeleteTCB from outside of
* critical section. If a task deletes itself, prvDeleteTCB is called
* from prvCheckTasksWaitingTermination which is called from Idle task. */
if( xDeleteTCBInIdleTask != pdTRUE )
{
prvDeleteTCB( pxTCB );
}
/* Force a reschedule if it is the currently running task that has just
* been deleted. */
#if ( configNUMBER_OF_CORES == 1 )
{
if( xSchedulerRunning != pdFALSE )
{
if( pxTCB == pxCurrentTCB )
{
configASSERT( uxSchedulerSuspended == 0 );
taskYIELD_WITHIN_API();
}
else
{
mtCOVERAGE_TEST_MARKER();
}
}
}
#endif /* #if ( configNUMBER_OF_CORES == 1 ) */
traceRETURN_vTaskDelete();
}
tasks.c, lines 2212–2358 · FreeRTOS V11.3.3 · MIT license
At tick 9 Supervisor has given S, done its last tick of work, and
calls vTaskDelete( NULL ):
uxListRemove()takes it out of its ready list, and its event list item out of any waiting list, as for a suspension.- The task is the running one. Its stack and
task control block cannot be freed now: the CPU is still
using that stack, and
vTaskDelete()itself is running on it. So the task goes intoxTasksWaitingTermination, anduxDeletedTasksWaitingCleanUpgoes up by one. - After the critical section, the task yields. PendSV saves its registers one last time and
switches to
Worker, which takesS.
Deleting another task is simpler. It is not running, so vTaskDelete() frees it at once with
prvDeleteTCB().
The idle task cleans up
The memory is freed by the idle task. Its loop starts with:
STATIC portTASK_FUNCTION( prvIdleTask,
pvParameters )
{
/* Stop warnings. */
( void ) pvParameters;
/** THIS IS THE RTOS IDLE TASK - WHICH IS CREATED AUTOMATICALLY WHEN THE
* SCHEDULER IS STARTED. **/
/* In case a task that has a secure context deletes itself, in which case
* the idle task is responsible for deleting the task's secure context, if
* any. */
portALLOCATE_SECURE_CONTEXT( configMINIMAL_SECURE_STACK_SIZE );
#if ( configNUMBER_OF_CORES > 1 )
{
/* SMP all cores start up in the idle task. This initial yield gets the application
* tasks started. */
taskYIELD();
}
#endif /* #if ( configNUMBER_OF_CORES > 1 ) */
for( ; configCONTROL_INFINITE_LOOP(); )
{
/* See if any tasks have deleted themselves - if so then the idle task
* is responsible for freeing the deleted task's TCB and stack. */
prvCheckTasksWaitingTermination();
#if ( configUSE_PREEMPTION == 0 )
{
/* If we are not using preemption we keep forcing a task switch to
* see if any other task has become available. If we are using
* preemption we don't need to do this as any task becoming available
* will automatically get the processor anyway. */
taskYIELD();
}
#endif /* configUSE_PREEMPTION */
#if ( ( configUSE_PREEMPTION == 1 ) && ( configIDLE_SHOULD_YIELD == 1 ) )
{
/* When using preemption tasks of equal priority will be
* timesliced. If a task that is sharing the idle priority is ready
* to run then the idle task should yield before the end of the
* timeslice.
*
* A critical region is not required here as we are just reading from
* the list, and an occasional incorrect value will not matter. If
* the ready list at the idle priority contains one more task than the
* number of idle tasks, which is equal to the configured numbers of cores
* then a task other than the idle task is ready to execute. */
if( listCURRENT_LIST_LENGTH( &( pxReadyTasksLists[ tskIDLE_PRIORITY ] ) ) > ( UBaseType_t ) configNUMBER_OF_CORES )
{
taskYIELD();
}
else
{
mtCOVERAGE_TEST_MARKER();
}
}
#endif /* ( ( configUSE_PREEMPTION == 1 ) && ( configIDLE_SHOULD_YIELD == 1 ) ) */
#if ( configUSE_IDLE_HOOK == 1 )
{
/* Call the user defined function from within the idle task. */
vApplicationIdleHook();
}
#endif /* configUSE_IDLE_HOOK */
/* This conditional compilation should use inequality to 0, not equality
* to 1. This is to ensure portSUPPRESS_TICKS_AND_SLEEP() is called when
* user defined low power mode implementations require
* configUSE_TICKLESS_IDLE to be set to a value other than 1. */
#if ( configUSE_TICKLESS_IDLE != 0 )
{
TickType_t xExpectedIdleTime;
/* It is not desirable to suspend then resume the scheduler on
* each iteration of the idle task. Therefore, a preliminary
* test of the expected idle time is performed without the
* scheduler suspended. The result here is not necessarily
* valid. */
xExpectedIdleTime = prvGetExpectedIdleTime();
if( xExpectedIdleTime >= ( TickType_t ) configEXPECTED_IDLE_TIME_BEFORE_SLEEP )
{
vTaskSuspendAll();
{
/* Now the scheduler is suspended, the expected idle
* time can be sampled again, and this time its value can
* be used. */
configASSERT( xNextTaskUnblockTime >= xTickCount );
xExpectedIdleTime = prvGetExpectedIdleTime();
/* Define the following macro to set xExpectedIdleTime to 0
* if the application does not want
* portSUPPRESS_TICKS_AND_SLEEP() to be called. */
configPRE_SUPPRESS_TICKS_AND_SLEEP_PROCESSING( xExpectedIdleTime );
if( xExpectedIdleTime >= ( TickType_t ) configEXPECTED_IDLE_TIME_BEFORE_SLEEP )
{
traceLOW_POWER_IDLE_BEGIN();
portSUPPRESS_TICKS_AND_SLEEP( xExpectedIdleTime );
traceLOW_POWER_IDLE_END();
}
else
{
mtCOVERAGE_TEST_MARKER();
}
}
( void ) xTaskResumeAll();
}
else
{
mtCOVERAGE_TEST_MARKER();
}
}
#endif /* configUSE_TICKLESS_IDLE */
#if ( ( configNUMBER_OF_CORES > 1 ) && ( configUSE_PASSIVE_IDLE_HOOK == 1 ) )
{
/* Call the user defined function from within the idle task. This
* allows the application designer to add background functionality
* without the overhead of a separate task.
*
* This hook is intended to manage core activity such as disabling cores that go idle.
*
* NOTE: vApplicationPassiveIdleHook() MUST NOT, UNDER ANY CIRCUMSTANCES,
* CALL A FUNCTION THAT MIGHT BLOCK. */
vApplicationPassiveIdleHook();
}
#endif /* #if ( ( configNUMBER_OF_CORES > 1 ) && ( configUSE_PASSIVE_IDLE_HOOK == 1 ) ) */
}
}
tasks.c, lines 5908–6040 · FreeRTOS V11.3.3 · MIT license
STATIC void prvCheckTasksWaitingTermination( void )
{
/** THIS FUNCTION IS CALLED FROM THE RTOS IDLE TASK **/
#if ( INCLUDE_vTaskDelete == 1 )
{
TCB_t * pxTCB;
/* uxDeletedTasksWaitingCleanUp is used to prevent taskENTER_CRITICAL()
* being called too often in the idle task. */
while( uxDeletedTasksWaitingCleanUp > ( UBaseType_t ) 0U )
{
#if ( configNUMBER_OF_CORES == 1 )
{
taskENTER_CRITICAL();
{
{
/* MISRA Ref 11.5.3 [Void pointer assignment] */
/* More details at: https://github.com/FreeRTOS/FreeRTOS-Kernel/blob/main/MISRA.md#rule-115 */
/* coverity[misra_c_2012_rule_11_5_violation] */
pxTCB = listGET_OWNER_OF_HEAD_ENTRY( ( &xTasksWaitingTermination ) );
( void ) uxListRemove( &( pxTCB->xStateListItem ) );
--uxCurrentNumberOfTasks;
--uxDeletedTasksWaitingCleanUp;
}
}
taskEXIT_CRITICAL();
prvDeleteTCB( pxTCB );
}
#else /* #if( configNUMBER_OF_CORES == 1 ) */
{
pxTCB = NULL;
taskENTER_CRITICAL();
{
/* For SMP, multiple idles can be running simultaneously
* and we need to check that other idles did not cleanup while we were
* waiting to enter the critical section. */
if( uxDeletedTasksWaitingCleanUp > ( UBaseType_t ) 0U )
{
/* MISRA Ref 11.5.3 [Void pointer assignment] */
/* More details at: https://github.com/FreeRTOS/FreeRTOS-Kernel/blob/main/MISRA.md#rule-115 */
/* coverity[misra_c_2012_rule_11_5_violation] */
pxTCB = listGET_OWNER_OF_HEAD_ENTRY( ( &xTasksWaitingTermination ) );
if( pxTCB->xTaskRunState == taskTASK_NOT_RUNNING )
{
( void ) uxListRemove( &( pxTCB->xStateListItem ) );
--uxCurrentNumberOfTasks;
--uxDeletedTasksWaitingCleanUp;
}
else
{
/* The TCB to be deleted still has not yet been switched out
* by the scheduler, so we will just exit this loop early and
* try again next time. */
taskEXIT_CRITICAL();
break;
}
}
}
taskEXIT_CRITICAL();
if( pxTCB != NULL )
{
prvDeleteTCB( pxTCB );
}
}
#endif /* #if( configNUMBER_OF_CORES == 1 ) */
}
}
#endif /* INCLUDE_vTaskDelete */
}
tasks.c, lines 6207–6280 · FreeRTOS V11.3.3 · MIT license
uxDeletedTasksWaitingCleanUp keeps the common case cheap: when it is 0, the idle task does not
even enter a critical section. When it is not, the idle task takes each task off
xTasksWaitingTermination and frees its stack and TCB with prvDeleteTCB(). It can do this
safely because it runs on its own stack.
In this run, Supervisor waits for its memory to be freed for a whole tick. Worker runs from tick
9 to 10, ends the same way, and only then
does the CPU go to the idle task. Step on from there: the idle task
frees Supervisor, then Worker, in the order they ended.
That is why the idle task must get some CPU time in a system where tasks delete themselves. If more urgent tasks always keep the CPU, the deleted tasks’ memory is never given back.
What to try
- Give
Workeran urgency of 3. It now runs first and waits forSforever at tick 5, inxSuspendedTaskList.Supervisorthen suspends it:vTaskSuspend()takes it out of the list of tasks waiting forSas well. At tick 6prvTaskIsTaskSuspended()finds no wait for an object, sovTaskResume()resumes it, and becauseWorkeris more urgent, switches to it at once.Workergoes round the loop inxSemaphoreTake(), findsSstill empty and waits again. - Give
Supervisoran urgency of 1, the same asWorker. The two take turns tick by tick, so the suspension comes at tick 3 and the resume at tick 4.vTaskResume()does not switch here either:Workeris as urgent asSupervisor, not more.