PRIVILEGED_DATA static volatile TickType_t xTickCount = ( TickType_t ) 0U;\r
PRIVILEGED_DATA static volatile UBaseType_t uxTopReadyPriority = tskIDLE_PRIORITY;\r
PRIVILEGED_DATA static volatile BaseType_t xSchedulerRunning = pdFALSE;\r
-PRIVILEGED_DATA static volatile UBaseType_t uxSchedulerSuspended = ( UBaseType_t ) pdFALSE;\r
PRIVILEGED_DATA static volatile UBaseType_t uxPendedTicks = ( UBaseType_t ) 0U;\r
PRIVILEGED_DATA static volatile BaseType_t xYieldPending = pdFALSE;\r
PRIVILEGED_DATA static volatile BaseType_t xNumOfOverflows = ( BaseType_t ) 0;\r
PRIVILEGED_DATA static UBaseType_t uxTaskNumber = ( UBaseType_t ) 0U;\r
PRIVILEGED_DATA static volatile TickType_t xNextTaskUnblockTime = portMAX_DELAY;\r
\r
+/* Context switches are held pending while the scheduler is suspended. Also,\r
+interrupts must not manipulate the xStateListItem of a TCB, or any of the\r
+lists the xStateListItem can be referenced from, if the scheduler is suspended.\r
+If an interrupt needs to unblock a task while the scheduler is suspended then it\r
+moves the task's event list item into the xPendingReadyList, ready for the\r
+kernel to move the task from the pending ready list into the real ready list\r
+when the scheduler is unsuspended. The pending ready list itself can only be\r
+accessed from a critical section. */\r
+PRIVILEGED_DATA static volatile UBaseType_t uxSchedulerSuspended = ( UBaseType_t ) pdFALSE;\r
+\r
#if ( configGENERATE_RUN_TIME_STATS == 1 )\r
\r
PRIVILEGED_DATA static uint32_t ulTaskSwitchedInTime = 0UL; /*< Holds the value of a timer/counter the last time a task was switched in. */\r
*/\r
static void prvInitialiseTCBVariables( TCB_t * const pxTCB, const char * const pcName, UBaseType_t uxPriority, const MemoryRegion_t * const xRegions, const uint16_t usStackDepth ) PRIVILEGED_FUNCTION; /*lint !e971 Unqualified char types are allowed for strings and single characters only. */\r
\r
+/**\r
+ * Utility task that simply returns pdTRUE if the task referenced by xTask is\r
+ * currently in the Suspended state, or pdFALSE if the task referenced by xTask\r
+ * is in any other state.\r
+ */\r
+static BaseType_t prvTaskIsTaskSuspended( const TaskHandle_t xTask ) PRIVILEGED_FUNCTION;\r
+\r
/*\r
* Utility to ready all the lists used by the scheduler. This is called\r
* automatically upon the creation of the first task.\r
{\r
traceTASK_DELAY_UNTIL();\r
\r
- /* We must remove ourselves from the ready list before adding\r
- ourselves to the blocked list as the same list item is used for\r
- both lists. */\r
+ /* Remove the task from the ready list before adding it to the\r
+ blocked list as the same list item is used for both lists. */\r
if( uxListRemove( &( pxCurrentTCB->xGenericListItem ) ) == ( UBaseType_t ) 0 )\r
{\r
/* The current task must be in a ready list, so there is\r
\r
#if ( INCLUDE_vTaskSuspend == 1 )\r
\r
- BaseType_t xTaskIsTaskSuspended( const TaskHandle_t xTask )\r
+ static BaseType_t prvTaskIsTaskSuspended( const TaskHandle_t xTask )\r
{\r
BaseType_t xReturn = pdFALSE;\r
const TCB_t * const pxTCB = ( TCB_t * ) xTask;\r
\r
+ /* Accesses xPendingReadyList so must be called from a critical\r
+ section. */\r
+\r
/* It does not make sense to check if the calling task is suspended. */\r
configASSERT( xTask );\r
\r
- /* Is the task we are attempting to resume actually in the\r
- suspended list? */\r
+ /* Is the task being resumed actually in the suspended list? */\r
if( listIS_CONTAINED_WITHIN( &xSuspendedTaskList, &( pxTCB->xGenericListItem ) ) != pdFALSE )\r
{\r
/* Has the task already been resumed from within an ISR? */\r
if( listIS_CONTAINED_WITHIN( &xPendingReadyList, &( pxTCB->xEventListItem ) ) == pdFALSE )\r
{\r
- /* Is it in the suspended list because it is in the\r
- Suspended state? It is possible to be in the suspended\r
- list because it is blocked on a task with no timeout\r
- specified. */\r
+ /* Is it in the suspended list because it is in the Suspended \r
+ state, or because is is blocked with no timeout? */\r
if( listIS_CONTAINED_WITHIN( NULL, &( pxTCB->xEventListItem ) ) != pdFALSE )\r
{\r
xReturn = pdTRUE;\r
{\r
taskENTER_CRITICAL();\r
{\r
- if( xTaskIsTaskSuspended( pxTCB ) == pdTRUE )\r
+ if( prvTaskIsTaskSuspended( pxTCB ) == pdTRUE )\r
{\r
traceTASK_RESUME( pxTCB );\r
\r
\r
uxSavedInterruptStatus = portSET_INTERRUPT_MASK_FROM_ISR();\r
{\r
- if( xTaskIsTaskSuspended( pxTCB ) == pdTRUE )\r
+ if( prvTaskIsTaskSuspended( pxTCB ) == pdTRUE )\r
{\r
traceTASK_RESUME_FROM_ISR( pxTCB );\r
\r
+ /* Check the ready lists can be accessed. */\r
if( uxSchedulerSuspended == ( UBaseType_t ) pdFALSE )\r
{\r
+ /* Ready lists can be accessed so move the task from the\r
+ suspended list to the ready list directly. */\r
if( pxTCB->uxPriority >= pxCurrentTCB->uxPriority )\r
{\r
xYieldRequired = pdTRUE;\r
}\r
else\r
{\r
- /* We cannot access the delayed or ready lists, so will hold this\r
- task pending until the scheduler is resumed, at which point a\r
- yield will be performed if necessary. */\r
+ /* The delayed or ready lists cannot be accessed so the task\r
+ is held in the pending ready list until the scheduler is\r
+ unsuspended. */\r
vListInsertEnd( &( xPendingReadyList ), &( pxTCB->xEventListItem ) );\r
}\r
}\r
\r
configASSERT( pxEventList );\r
\r
- /* THIS FUNCTION MUST BE CALLED WITH INTERRUPTS DISABLED OR THE\r
- SCHEDULER SUSPENDED. */\r
+ /* THIS FUNCTION MUST BE CALLED WITH EITHER INTERRUPTS DISABLED OR THE\r
+ SCHEDULER SUSPENDED AND THE QUEUE BEING ACCESSED LOCKED. */\r
\r
/* Place the event list item of the TCB in the appropriate event list.\r
This is placed in the list in priority order so the highest priority task\r
- is the first to be woken by the event. */\r
+ is the first to be woken by the event. The queue that contains the event\r
+ list is locked, preventing simultaneous access from interrupts. */\r
vListInsert( pxEventList, &( pxCurrentTCB->xEventListItem ) );\r
\r
- /* We must remove ourselves from the ready list before adding ourselves\r
- to the blocked list as the same list item is used for both lists. We have\r
- exclusive access to the ready lists as the scheduler is locked. */\r
+ /* The task must be removed from from the ready list before it is added to\r
+ the blocked list as the same list item is used for both lists. Exclusive\r
+ access to the ready lists guaranteed because the scheduler is locked. */\r
if( uxListRemove( &( pxCurrentTCB->xGenericListItem ) ) == ( UBaseType_t ) 0 )\r
{\r
/* The current task must be in a ready list, so there is no need to\r
{\r
if( xTicksToWait == portMAX_DELAY )\r
{\r
- /* Add ourselves to the suspended task list instead of a delayed task\r
- list to ensure we are not woken by a timing event. We will block\r
- indefinitely. */\r
+ /* Add the task to the suspended task list instead of a delayed task\r
+ list to ensure the task is not woken by a timing event. It will\r
+ block indefinitely. */\r
vListInsertEnd( &xSuspendedTaskList, &( pxCurrentTCB->xGenericListItem ) );\r
}\r
else\r
{\r
- /* Calculate the time at which the task should be woken if the event does\r
- not occur. This may overflow but this doesn't matter. */\r
+ /* Calculate the time at which the task should be woken if the event\r
+ does not occur. This may overflow but this doesn't matter, the\r
+ scheduler will handle it. */\r
xTimeToWake = xTickCount + xTicksToWait;\r
prvAddCurrentTaskToDelayedList( xTimeToWake );\r
}\r
#else /* INCLUDE_vTaskSuspend */\r
{\r
/* Calculate the time at which the task should be woken if the event does\r
- not occur. This may overflow but this doesn't matter. */\r
+ not occur. This may overflow but this doesn't matter, the scheduler\r
+ will handle it. */\r
xTimeToWake = xTickCount + xTicksToWait;\r
prvAddCurrentTaskToDelayedList( xTimeToWake );\r
}\r
\r
configASSERT( pxEventList );\r
\r
- /* THIS FUNCTION MUST BE CALLED WITH INTERRUPTS DISABLED OR THE\r
- SCHEDULER SUSPENDED. */\r
+ /* THIS FUNCTION MUST BE CALLED WITH THE SCHEDULER SUSPENDED. It is used by\r
+ the event groups implementation. */\r
+ configASSERT( uxSchedulerSuspended != 0 );\r
\r
- /* Store the item value in the event list item. */\r
+ /* Store the item value in the event list item. It is safe to access the\r
+ event list item here as interrupts won't access the event list item of a\r
+ task that is not in the Blocked state. */\r
listSET_LIST_ITEM_VALUE( &( pxCurrentTCB->xEventListItem ), xItemValue | taskEVENT_LIST_ITEM_VALUE_IN_USE );\r
\r
/* Place the event list item of the TCB at the end of the appropriate event\r
- list. */\r
+ list. It is safe to access the event list here because it is part of an\r
+ event group implementation - and interrupts don't access event groups\r
+ directly (instead they access them indirectly by pending function calls to\r
+ the task level). */\r
vListInsertEnd( pxEventList, &( pxCurrentTCB->xEventListItem ) );\r
\r
/* The task must be removed from the ready list before it is added to the\r
}\r
else\r
{\r
- /* Calculate the time at which the task should be woken if the event does\r
- not occur. This may overflow but this doesn't matter. */\r
+ /* Calculate the time at which the task should be woken if the event\r
+ does not occur. This may overflow but this doesn't matter, the\r
+ kernel will manage it correctly. */\r
xTimeToWake = xTickCount + xTicksToWait;\r
prvAddCurrentTaskToDelayedList( xTimeToWake );\r
}\r
#else /* INCLUDE_vTaskSuspend */\r
{\r
/* Calculate the time at which the task should be woken if the event does\r
- not occur. This may overflow but this doesn't matter. */\r
+ not occur. This may overflow but this doesn't matter, the kernel\r
+ will manage it correctly. */\r
xTimeToWake = xTickCount + xTicksToWait;\r
prvAddCurrentTaskToDelayedList( xTimeToWake );\r
}\r
TCB_t *pxUnblockedTCB;\r
BaseType_t xReturn;\r
\r
- /* THIS FUNCTION MUST BE CALLED WITH INTERRUPTS DISABLED OR THE\r
- SCHEDULER SUSPENDED. It can also be called from within an ISR. */\r
+ /* THIS FUNCTION MUST BE CALLED FROM A CRITICAL SECTION. It can also be\r
+ called from a critical section within an ISR. */\r
\r
- /* The event list is sorted in priority order, so we can remove the\r
- first in the list, remove the TCB from the delayed list, and add\r
- it to the ready list.\r
+ /* The event list is sorted in priority order, so the first in the list can\r
+ be removed as it is known to be the highest priority. Remove the TCB from \r
+ the delayed list, and add it to the ready list.\r
\r
If an event is for a queue that is locked then this function will never\r
get called - the lock count on the queue will get modified instead. This\r
- means we can always expect exclusive access to the event list here.\r
+ means exclusive access to the event list is guaranteed here.\r
\r
This function assumes that a check has already been made to ensure that\r
pxEventList is not empty. */\r
\r
if( pxUnblockedTCB->uxPriority > pxCurrentTCB->uxPriority )\r
{\r
- /* Return true if the task removed from the event list has\r
- a higher priority than the calling task. This allows\r
- the calling task to know if it should force a context\r
- switch now. */\r
+ /* Return true if the task removed from the event list has a higher\r
+ priority than the calling task. This allows the calling task to know if\r
+ it should force a context switch now. */\r
xReturn = pdTRUE;\r
\r
/* Mark that a yield is pending in case the user is not using the\r
TCB_t *pxUnblockedTCB;\r
BaseType_t xReturn;\r
\r
- /* THIS FUNCTION MUST BE CALLED WITH INTERRUPTS DISABLED OR THE\r
- SCHEDULER SUSPENDED. It can also be called from within an ISR. */\r
+ /* THIS FUNCTION MUST BE CALLED WITH THE SCHEDULER SUSPENDED. It is used by\r
+ the event flags implementation. */\r
+ configASSERT( uxSchedulerSuspended != pdFALSE );\r
\r
/* Store the new item value in the event list. */\r
listSET_LIST_ITEM_VALUE( pxEventListItem, xItemValue | taskEVENT_LIST_ITEM_VALUE_IN_USE );\r
\r
- /* Remove the TCB from the delayed list, and add it to the ready list. */\r
-\r
+ /* Remove the event list form the event flag. Interrupts do not access\r
+ event flags. */\r
pxUnblockedTCB = ( TCB_t * ) listGET_LIST_ITEM_OWNER( pxEventListItem );\r
configASSERT( pxUnblockedTCB );\r
( void ) uxListRemove( pxEventListItem );\r
\r
- if( uxSchedulerSuspended == ( UBaseType_t ) pdFALSE )\r
- {\r
- ( void ) uxListRemove( &( pxUnblockedTCB->xGenericListItem ) );\r
- prvAddTaskToReadyList( pxUnblockedTCB );\r
- }\r
- else\r
- {\r
- /* Cannot access the delayed or ready lists, so will hold this task\r
- pending until the scheduler is resumed. */\r
- vListInsertEnd( &( xPendingReadyList ), pxEventListItem );\r
- }\r
+ /* Remove the task from the delayed list and add it to the ready list. The\r
+ scheduler is suspended so interrupts will not be accessing the ready\r
+ lists. */\r
+ ( void ) uxListRemove( &( pxUnblockedTCB->xGenericListItem ) );\r
+ prvAddTaskToReadyList( pxUnblockedTCB );\r
\r
if( pxUnblockedTCB->uxPriority > pxCurrentTCB->uxPriority )\r
{\r
while( uxTasksDeleted > ( UBaseType_t ) 0U )\r
{\r
vTaskSuspendAll();\r
+ {\r
xListIsEmpty = listLIST_IS_EMPTY( &xTasksWaitingTermination );\r
+ }\r
( void ) xTaskResumeAll();\r
\r
if( xListIsEmpty == pdFALSE )\r
pxTaskStatusArray[ uxTask ].eCurrentState = eState;\r
pxTaskStatusArray[ uxTask ].uxCurrentPriority = pxNextTCB->uxPriority;\r
\r
+ #if ( INCLUDE_vTaskSuspend == 1 )\r
+ {\r
+ /* If the task is in the suspended list then there is a chance\r
+ it is actually just blocked indefinitely - so really it should\r
+ be reported as being in the Blocked state. */\r
+ if( eState == eSuspended )\r
+ {\r
+ if( listLIST_ITEM_CONTAINER( &( pxNextTCB->xEventListItem ) ) != NULL )\r
+ {\r
+ pxTaskStatusArray[ uxTask ].eCurrentState = eBlocked;\r
+ }\r
+ }\r
+ }\r
+ #endif /* INCLUDE_vTaskSuspend */\r
+\r
#if ( configUSE_MUTEXES == 1 )\r
{\r
pxTaskStatusArray[ uxTask ].uxBasePriority = pxNextTCB->uxBasePriority;\r