]> git.sur5r.net Git - freertos/blobdiff - FreeRTOS/Source/tasks.c
Update version number to 9.0.0rc2.
[freertos] / FreeRTOS / Source / tasks.c
index 49c8a4aaf848148ff63ef42630e2ad6c31d103a0..47f38bea92840c45cc3efbecd462dd9cc4a20952 100644 (file)
@@ -1,5 +1,5 @@
 /*\r
-    FreeRTOS V8.2.1 - Copyright (C) 2015 Real Time Engineers Ltd.\r
+    FreeRTOS V9.0.0rc2 - Copyright (C) 2016 Real Time Engineers Ltd.\r
     All rights reserved\r
 \r
     VISIT http://www.FreeRTOS.org TO ENSURE YOU ARE USING THE LATEST VERSION.\r
@@ -8,7 +8,7 @@
 \r
     FreeRTOS is free software; you can redistribute it and/or modify it under\r
     the terms of the GNU General Public License (version 2) as published by the\r
-    Free Software Foundation >>!AND MODIFIED BY!<< the FreeRTOS exception.\r
+    Free Software Foundation >>>> AND MODIFIED BY <<<< the FreeRTOS exception.\r
 \r
     ***************************************************************************\r
     >>!   NOTE: The modification to the GPL is included to allow you to     !<<\r
@@ -98,18 +98,6 @@ functions but without including stdio.h here. */
        #include <stdio.h>\r
 #endif /* configUSE_STATS_FORMATTING_FUNCTIONS == 1 ) */\r
 \r
-/* Sanity check the configuration. */\r
-#if( configUSE_TICKLESS_IDLE != 0 )\r
-       #if( INCLUDE_vTaskSuspend != 1 )\r
-               #error INCLUDE_vTaskSuspend must be set to 1 if configUSE_TICKLESS_IDLE is not set to 0\r
-       #endif /* INCLUDE_vTaskSuspend */\r
-#endif /* configUSE_TICKLESS_IDLE */\r
-\r
-/*\r
- * Defines the size, in words, of the stack allocated to the idle task.\r
- */\r
-#define tskIDLE_STACK_SIZE     configMINIMAL_STACK_SIZE\r
-\r
 #if( configUSE_PREEMPTION == 0 )\r
        /* If the cooperative scheduler is being used then a yield should not be\r
        performed just because a higher priority task has been woken. */\r
@@ -118,157 +106,10 @@ functions but without including stdio.h here. */
        #define taskYIELD_IF_USING_PREEMPTION() portYIELD_WITHIN_API()\r
 #endif\r
 \r
-/* Value that can be assigned to the eNotifyState member of the TCB. */\r
-typedef enum\r
-{\r
-       eNotWaitingNotification = 0,\r
-       eWaitingNotification,\r
-       eNotified\r
-} eNotifyValue;\r
-\r
-/*\r
- * Task control block.  A task control block (TCB) is allocated for each task,\r
- * and stores task state information, including a pointer to the task's context\r
- * (the task's run time environment, including register values)\r
- */\r
-typedef struct tskTaskControlBlock\r
-{\r
-       volatile StackType_t    *pxTopOfStack;  /*< Points to the location of the last item placed on the tasks stack.  THIS MUST BE THE FIRST MEMBER OF THE TCB STRUCT. */\r
-\r
-       #if ( portUSING_MPU_WRAPPERS == 1 )\r
-               xMPU_SETTINGS   xMPUSettings;           /*< The MPU settings are defined as part of the port layer.  THIS MUST BE THE SECOND MEMBER OF THE TCB STRUCT. */\r
-               BaseType_t              xUsingStaticallyAllocatedStack; /* Set to pdTRUE if the stack is a statically allocated array, and pdFALSE if the stack is dynamically allocated. */\r
-       #endif\r
-\r
-       ListItem_t                      xGenericListItem;       /*< The list that the state list item of a task is reference from denotes the state of that task (Ready, Blocked, Suspended ). */\r
-       ListItem_t                      xEventListItem;         /*< Used to reference a task from an event list. */\r
-       UBaseType_t                     uxPriority;                     /*< The priority of the task.  0 is the lowest priority. */\r
-       StackType_t                     *pxStack;                       /*< Points to the start of the stack. */\r
-       char                            pcTaskName[ configMAX_TASK_NAME_LEN ];/*< Descriptive name given to the task when created.  Facilitates debugging only. */ /*lint !e971 Unqualified char types are allowed for strings and single characters only. */\r
-\r
-       #if ( portSTACK_GROWTH > 0 )\r
-               StackType_t             *pxEndOfStack;          /*< Points to the end of the stack on architectures where the stack grows up from low memory. */\r
-       #endif\r
-\r
-       #if ( portCRITICAL_NESTING_IN_TCB == 1 )\r
-               UBaseType_t     uxCriticalNesting;      /*< Holds the critical section nesting depth for ports that do not maintain their own count in the port layer. */\r
-       #endif\r
-\r
-       #if ( configUSE_TRACE_FACILITY == 1 )\r
-               UBaseType_t             uxTCBNumber;            /*< Stores a number that increments each time a TCB is created.  It allows debuggers to determine when a task has been deleted and then recreated. */\r
-               UBaseType_t     uxTaskNumber;           /*< Stores a number specifically for use by third party trace code. */\r
-       #endif\r
-\r
-       #if ( configUSE_MUTEXES == 1 )\r
-               UBaseType_t     uxBasePriority;         /*< The priority last assigned to the task - used by the priority inheritance mechanism. */\r
-               UBaseType_t     uxMutexesHeld;\r
-       #endif\r
-\r
-       #if ( configUSE_APPLICATION_TASK_TAG == 1 )\r
-               TaskHookFunction_t pxTaskTag;\r
-       #endif\r
-\r
-       #if( configNUM_THREAD_LOCAL_STORAGE_POINTERS > 0 )\r
-               void *pvThreadLocalStoragePointers[ configNUM_THREAD_LOCAL_STORAGE_POINTERS ];\r
-       #endif\r
-\r
-       #if ( configGENERATE_RUN_TIME_STATS == 1 )\r
-               uint32_t                ulRunTimeCounter;       /*< Stores the amount of time the task has spent in the Running state. */\r
-       #endif\r
-\r
-       #if ( configUSE_NEWLIB_REENTRANT == 1 )\r
-               /* Allocate a Newlib reent structure that is specific to this task.\r
-               Note Newlib support has been included by popular demand, but is not\r
-               used by the FreeRTOS maintainers themselves.  FreeRTOS is not\r
-               responsible for resulting newlib operation.  User must be familiar with\r
-               newlib and must provide system-wide implementations of the necessary\r
-               stubs. Be warned that (at the time of writing) the current newlib design\r
-               implements a system-wide malloc() that must be provided with locks. */\r
-               struct  _reent xNewLib_reent;\r
-       #endif\r
-\r
-       #if ( configUSE_TASK_NOTIFICATIONS == 1 )\r
-               volatile uint32_t ulNotifiedValue;\r
-               volatile eNotifyValue eNotifyState;\r
-       #endif\r
-\r
-} tskTCB;\r
-\r
-/* The old tskTCB name is maintained above then typedefed to the new TCB_t name\r
-below to enable the use of older kernel aware debuggers. */\r
-typedef tskTCB TCB_t;\r
-\r
-/*\r
- * Some kernel aware debuggers require the data the debugger needs access to to\r
- * be global, rather than file scope.\r
- */\r
-#ifdef portREMOVE_STATIC_QUALIFIER\r
-       #define static\r
-#endif\r
-\r
-/*lint -e956 A manual analysis and inspection has been used to determine which\r
-static variables must be declared volatile. */\r
-\r
-PRIVILEGED_DATA TCB_t * volatile pxCurrentTCB = NULL;\r
-\r
-/* Lists for ready and blocked tasks. --------------------*/\r
-PRIVILEGED_DATA static List_t pxReadyTasksLists[ configMAX_PRIORITIES ];/*< Prioritised ready tasks. */\r
-PRIVILEGED_DATA static List_t xDelayedTaskList1;                                               /*< Delayed tasks. */\r
-PRIVILEGED_DATA static List_t xDelayedTaskList2;                                               /*< Delayed tasks (two lists are used - one for delays that have overflowed the current tick count. */\r
-PRIVILEGED_DATA static List_t * volatile pxDelayedTaskList;                            /*< Points to the delayed task list currently being used. */\r
-PRIVILEGED_DATA static List_t * volatile pxOverflowDelayedTaskList;            /*< Points to the delayed task list currently being used to hold tasks that have overflowed the current tick count. */\r
-PRIVILEGED_DATA static List_t xPendingReadyList;                                               /*< Tasks that have been readied while the scheduler was suspended.  They will be moved to the ready list when the scheduler is resumed. */\r
-\r
-#if ( INCLUDE_vTaskDelete == 1 )\r
-\r
-       PRIVILEGED_DATA static List_t xTasksWaitingTermination;                         /*< Tasks that have been deleted - but their memory not yet freed. */\r
-       PRIVILEGED_DATA static volatile UBaseType_t uxTasksDeleted = ( UBaseType_t ) 0U;\r
-\r
-#endif\r
-\r
-#if ( INCLUDE_vTaskSuspend == 1 )\r
-\r
-       PRIVILEGED_DATA static List_t xSuspendedTaskList;                                       /*< Tasks that are currently suspended. */\r
-\r
-#endif\r
-\r
-#if ( INCLUDE_xTaskGetIdleTaskHandle == 1 )\r
-\r
-       PRIVILEGED_DATA static TaskHandle_t xIdleTaskHandle = NULL;                     /*< Holds the handle of the idle task.  The idle task is created automatically when the scheduler is started. */\r
-\r
-#endif\r
-\r
-/* Other file private variables. --------------------------------*/\r
-PRIVILEGED_DATA static volatile UBaseType_t uxCurrentNumberOfTasks     = ( UBaseType_t ) 0U;\r
-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 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                = ( TickType_t ) 0U; /* Initialised to portMAX_DELAY; before the scheduler starts. */\r
-\r
-/* Context switches are held pending while the scheduler is suspended.  Also,\r
-interrupts must not manipulate the xGenericListItem of a TCB, or any of the\r
-lists the xGenericListItem 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
-       PRIVILEGED_DATA static uint32_t ulTotalRunTime = 0UL;           /*< Holds the total amount of execution time as defined by the run time counter clock. */\r
-\r
-#endif\r
-\r
-/*lint +e956 */\r
-\r
-/* Debugging and trace facilities private variables and macros. ------------*/\r
+/* Values that can be assigned to the ucNotifyState member of the TCB. */\r
+#define taskNOT_WAITING_NOTIFICATION   ( ( uint8_t ) 0 )\r
+#define taskWAITING_NOTIFICATION               ( ( uint8_t ) 1 )\r
+#define taskNOTIFICATION_RECEIVED              ( ( uint8_t ) 2 )\r
 \r
 /*\r
  * The value used to fill the stack of a task when the task is created.  This\r
@@ -284,7 +125,13 @@ PRIVILEGED_DATA static volatile UBaseType_t uxSchedulerSuspended   = ( UBaseType_t
 #define tskDELETED_CHAR                ( 'D' )\r
 #define tskSUSPENDED_CHAR      ( 'S' )\r
 \r
-/*-----------------------------------------------------------*/\r
+/*\r
+ * Some kernel aware debuggers require the data the debugger needs access to be\r
+ * global, rather than file scope.\r
+ */\r
+#ifdef portREMOVE_STATIC_QUALIFIER\r
+       #define static\r
+#endif\r
 \r
 #if ( configUSE_PORT_OPTIMISED_TASK_SELECTION == 0 )\r
 \r
@@ -306,16 +153,19 @@ PRIVILEGED_DATA static volatile UBaseType_t uxSchedulerSuspended  = ( UBaseType_t
 \r
        #define taskSELECT_HIGHEST_PRIORITY_TASK()                                                                                                                      \\r
        {                                                                                                                                                                                                       \\r
+       UBaseType_t uxTopPriority = uxTopReadyPriority;                                                                                                         \\r
+                                                                                                                                                                                                               \\r
                /* Find the highest priority queue that contains ready tasks. */                                                                \\r
-               while( listLIST_IS_EMPTY( &( pxReadyTasksLists[ uxTopReadyPriority ] ) ) )                                              \\r
+               while( listLIST_IS_EMPTY( &( pxReadyTasksLists[ uxTopPriority ] ) ) )                                                   \\r
                {                                                                                                                                                                                               \\r
-                       configASSERT( uxTopReadyPriority );                                                                                                                     \\r
-                       --uxTopReadyPriority;                                                                                                                                           \\r
+                       configASSERT( uxTopPriority );                                                                                                                          \\r
+                       --uxTopPriority;                                                                                                                                                        \\r
                }                                                                                                                                                                                               \\r
                                                                                                                                                                                                                \\r
                /* listGET_OWNER_OF_NEXT_ENTRY indexes through the list, so the tasks of                                                \\r
                the     same priority get an equal share of the processor time. */                                                                      \\r
-               listGET_OWNER_OF_NEXT_ENTRY( pxCurrentTCB, &( pxReadyTasksLists[ uxTopReadyPriority ] ) );              \\r
+               listGET_OWNER_OF_NEXT_ENTRY( pxCurrentTCB, &( pxReadyTasksLists[ uxTopPriority ] ) );                   \\r
+               uxTopReadyPriority = uxTopPriority;                                                                                                                             \\r
        } /* taskSELECT_HIGHEST_PRIORITY_TASK */\r
 \r
        /*-----------------------------------------------------------*/\r
@@ -341,7 +191,7 @@ PRIVILEGED_DATA static volatile UBaseType_t uxSchedulerSuspended    = ( UBaseType_t
        {                                                                                                                                                                                               \\r
        UBaseType_t uxTopPriority;                                                                                                                                              \\r
                                                                                                                                                                                                        \\r
-               /* Find the highest priority queue that contains ready tasks. */                                                        \\r
+               /* Find the highest priority list that contains ready tasks. */                                                         \\r
                portGET_HIGHEST_PRIORITY( uxTopPriority, uxTopReadyPriority );                                                          \\r
                configASSERT( listCURRENT_LIST_LENGTH( &( pxReadyTasksLists[ uxTopPriority ] ) ) > 0 );         \\r
                listGET_OWNER_OF_NEXT_ENTRY( pxCurrentTCB, &( pxReadyTasksLists[ uxTopPriority ] ) );           \\r
@@ -389,7 +239,8 @@ count overflows. */
 #define prvAddTaskToReadyList( pxTCB )                                                                                                                         \\r
        traceMOVED_TASK_TO_READY_STATE( pxTCB );                                                                                                                \\r
        taskRECORD_READY_PRIORITY( ( pxTCB )->uxPriority );                                                                                             \\r
-       vListInsertEnd( &( pxReadyTasksLists[ ( pxTCB )->uxPriority ] ), &( ( pxTCB )->xGenericListItem ) )\r
+       vListInsertEnd( &( pxReadyTasksLists[ ( pxTCB )->uxPriority ] ), &( ( pxTCB )->xStateListItem ) ); \\r
+       tracePOST_MOVED_TASK_TO_READY_STATE( pxTCB )\r
 /*-----------------------------------------------------------*/\r
 \r
 /*\r
@@ -408,28 +259,164 @@ being used for another purpose.  The following bit definition is used to inform
 the scheduler that the value should not be changed - in which case it is the\r
 responsibility of whichever module is using the value to ensure it gets set back\r
 to its original value when it is released. */\r
-#if configUSE_16_BIT_TICKS == 1\r
+#if( configUSE_16_BIT_TICKS == 1 )\r
        #define taskEVENT_LIST_ITEM_VALUE_IN_USE        0x8000U\r
 #else\r
        #define taskEVENT_LIST_ITEM_VALUE_IN_USE        0x80000000UL\r
 #endif\r
 \r
+/*\r
+ * Task control block.  A task control block (TCB) is allocated for each task,\r
+ * and stores task state information, including a pointer to the task's context\r
+ * (the task's run time environment, including register values)\r
+ */\r
+typedef struct tskTaskControlBlock\r
+{\r
+       volatile StackType_t    *pxTopOfStack;  /*< Points to the location of the last item placed on the tasks stack.  THIS MUST BE THE FIRST MEMBER OF THE TCB STRUCT. */\r
+\r
+       #if ( portUSING_MPU_WRAPPERS == 1 )\r
+               xMPU_SETTINGS   xMPUSettings;           /*< The MPU settings are defined as part of the port layer.  THIS MUST BE THE SECOND MEMBER OF THE TCB STRUCT. */\r
+       #endif\r
+\r
+       ListItem_t                      xStateListItem; /*< The list that the state list item of a task is reference from denotes the state of that task (Ready, Blocked, Suspended ). */\r
+       ListItem_t                      xEventListItem;         /*< Used to reference a task from an event list. */\r
+       UBaseType_t                     uxPriority;                     /*< The priority of the task.  0 is the lowest priority. */\r
+       StackType_t                     *pxStack;                       /*< Points to the start of the stack. */\r
+       char                            pcTaskName[ configMAX_TASK_NAME_LEN ];/*< Descriptive name given to the task when created.  Facilitates debugging only. */ /*lint !e971 Unqualified char types are allowed for strings and single characters only. */\r
+\r
+       #if ( portSTACK_GROWTH > 0 )\r
+               StackType_t             *pxEndOfStack;          /*< Points to the end of the stack on architectures where the stack grows up from low memory. */\r
+       #endif\r
+\r
+       #if ( portCRITICAL_NESTING_IN_TCB == 1 )\r
+               UBaseType_t             uxCriticalNesting;      /*< Holds the critical section nesting depth for ports that do not maintain their own count in the port layer. */\r
+       #endif\r
+\r
+       #if ( configUSE_TRACE_FACILITY == 1 )\r
+               UBaseType_t             uxTCBNumber;            /*< Stores a number that increments each time a TCB is created.  It allows debuggers to determine when a task has been deleted and then recreated. */\r
+               UBaseType_t             uxTaskNumber;           /*< Stores a number specifically for use by third party trace code. */\r
+       #endif\r
+\r
+       #if ( configUSE_MUTEXES == 1 )\r
+               UBaseType_t             uxBasePriority;         /*< The priority last assigned to the task - used by the priority inheritance mechanism. */\r
+               UBaseType_t             uxMutexesHeld;\r
+       #endif\r
+\r
+       #if ( configUSE_APPLICATION_TASK_TAG == 1 )\r
+               TaskHookFunction_t pxTaskTag;\r
+       #endif\r
+\r
+       #if( configNUM_THREAD_LOCAL_STORAGE_POINTERS > 0 )\r
+               void *pvThreadLocalStoragePointers[ configNUM_THREAD_LOCAL_STORAGE_POINTERS ];\r
+       #endif\r
+\r
+       #if( configGENERATE_RUN_TIME_STATS == 1 )\r
+               uint32_t                ulRunTimeCounter;       /*< Stores the amount of time the task has spent in the Running state. */\r
+       #endif\r
+\r
+       #if ( configUSE_NEWLIB_REENTRANT == 1 )\r
+               /* Allocate a Newlib reent structure that is specific to this task.\r
+               Note Newlib support has been included by popular demand, but is not\r
+               used by the FreeRTOS maintainers themselves.  FreeRTOS is not\r
+               responsible for resulting newlib operation.  User must be familiar with\r
+               newlib and must provide system-wide implementations of the necessary\r
+               stubs. Be warned that (at the time of writing) the current newlib design\r
+               implements a system-wide malloc() that must be provided with locks. */\r
+               struct  _reent xNewLib_reent;\r
+       #endif\r
+\r
+       #if( configUSE_TASK_NOTIFICATIONS == 1 )\r
+               volatile uint32_t ulNotifiedValue;\r
+               volatile uint8_t ucNotifyState;\r
+       #endif\r
+\r
+       #if( ( configSUPPORT_STATIC_ALLOCATION == 1 ) && ( configSUPPORT_DYNAMIC_ALLOCATION == 1 ) )\r
+               uint8_t ucStaticallyAllocated; /* Set to pdTRUE if the task is a statically allocated to ensure no attempt is made to free the memory. */\r
+       #endif\r
+\r
+       #if( INCLUDE_xTaskAbortDelay == 1 )\r
+               uint8_t ucDelayAborted;\r
+       #endif\r
+\r
+} tskTCB;\r
+\r
+/* The old tskTCB name is maintained above then typedefed to the new TCB_t name\r
+below to enable the use of older kernel aware debuggers. */\r
+typedef tskTCB TCB_t;\r
+\r
+/*lint -e956 A manual analysis and inspection has been used to determine which\r
+static variables must be declared volatile. */\r
+\r
+PRIVILEGED_DATA TCB_t * volatile pxCurrentTCB = NULL;\r
+\r
+/* Lists for ready and blocked tasks. --------------------*/\r
+PRIVILEGED_DATA static List_t pxReadyTasksLists[ configMAX_PRIORITIES ];/*< Prioritised ready tasks. */\r
+PRIVILEGED_DATA static List_t xDelayedTaskList1;                                               /*< Delayed tasks. */\r
+PRIVILEGED_DATA static List_t xDelayedTaskList2;                                               /*< Delayed tasks (two lists are used - one for delays that have overflowed the current tick count. */\r
+PRIVILEGED_DATA static List_t * volatile pxDelayedTaskList;                            /*< Points to the delayed task list currently being used. */\r
+PRIVILEGED_DATA static List_t * volatile pxOverflowDelayedTaskList;            /*< Points to the delayed task list currently being used to hold tasks that have overflowed the current tick count. */\r
+PRIVILEGED_DATA static List_t xPendingReadyList;                                               /*< Tasks that have been readied while the scheduler was suspended.  They will be moved to the ready list when the scheduler is resumed. */\r
+\r
+#if( INCLUDE_vTaskDelete == 1 )\r
+\r
+       PRIVILEGED_DATA static List_t xTasksWaitingTermination;                         /*< Tasks that have been deleted - but their memory not yet freed. */\r
+       PRIVILEGED_DATA static volatile UBaseType_t uxDeletedTasksWaitingCleanUp = ( UBaseType_t ) 0U;\r
+\r
+#endif\r
+\r
+#if ( INCLUDE_vTaskSuspend == 1 )\r
+\r
+       PRIVILEGED_DATA static List_t xSuspendedTaskList;                                       /*< Tasks that are currently suspended. */\r
+\r
+#endif\r
+\r
+/* Other file private variables. --------------------------------*/\r
+PRIVILEGED_DATA static volatile UBaseType_t uxCurrentNumberOfTasks     = ( UBaseType_t ) 0U;\r
+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 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                = ( TickType_t ) 0U; /* Initialised to portMAX_DELAY before the scheduler starts. */\r
+PRIVILEGED_DATA static TaskHandle_t xIdleTaskHandle                                    = NULL;                 /*< Holds the handle of the idle task.  The idle task is created automatically when the scheduler is started. */\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
+       PRIVILEGED_DATA static uint32_t ulTotalRunTime = 0UL;           /*< Holds the total amount of execution time as defined by the run time counter clock. */\r
+\r
+#endif\r
+\r
+/*lint +e956 */\r
+\r
+/*-----------------------------------------------------------*/\r
+\r
 /* Callback function prototypes. --------------------------*/\r
-#if configCHECK_FOR_STACK_OVERFLOW > 0\r
+#if(  configCHECK_FOR_STACK_OVERFLOW > 0 )\r
        extern void vApplicationStackOverflowHook( TaskHandle_t xTask, char *pcTaskName );\r
 #endif\r
 \r
-#if configUSE_TICK_HOOK > 0\r
+#if( configUSE_TICK_HOOK > 0 )\r
        extern void vApplicationTickHook( void );\r
 #endif\r
 \r
-/* File private functions. --------------------------------*/\r
+#if( configSUPPORT_STATIC_ALLOCATION == 1 )\r
+       extern void vApplicationGetIdleTaskMemory( StaticTask_t **ppxIdleTaskTCBBuffer, StackType_t **ppxIdleTaskStackBuffer, uint16_t *pusIdleTaskStackSize );\r
+#endif\r
 \r
-/*\r
- * Utility to ready a TCB for a given task.  Mainly just copies the parameters\r
- * into the TCB structure.\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
+/* File private functions. --------------------------------*/\r
 \r
 /**\r
  * Utility task that simply returns pdTRUE if the task referenced by xTask is\r
@@ -483,13 +470,7 @@ static void prvCheckTasksWaitingTermination( void ) PRIVILEGED_FUNCTION;
  * The currently executing task is entering the Blocked state.  Add the task to\r
  * either the current or the overflow delayed task list.\r
  */\r
-static void prvAddCurrentTaskToDelayedList( const TickType_t xTimeToWake ) PRIVILEGED_FUNCTION;\r
-\r
-/*\r
- * Allocates memory from the heap for a TCB and associated stack.  Checks the\r
- * allocation was successful.\r
- */\r
-static TCB_t *prvAllocateTCBAndStack( const uint16_t usStackDepth, StackType_t * const puxStackBuffer ) PRIVILEGED_FUNCTION;\r
+static void prvAddCurrentTaskToDelayedList( TickType_t xTicksToWait, const BaseType_t xCanBlockIndefinitely ) PRIVILEGED_FUNCTION;\r
 \r
 /*\r
  * Fills an TaskStatus_t structure with information on each task that is\r
@@ -501,7 +482,17 @@ static TCB_t *prvAllocateTCBAndStack( const uint16_t usStackDepth, StackType_t *
  */\r
 #if ( configUSE_TRACE_FACILITY == 1 )\r
 \r
-       static UBaseType_t prvListTaskWithinSingleList( TaskStatus_t *pxTaskStatusArray, List_t *pxList, eTaskState eState ) PRIVILEGED_FUNCTION;\r
+       static UBaseType_t prvListTasksWithinSingleList( TaskStatus_t *pxTaskStatusArray, List_t *pxList, eTaskState eState ) PRIVILEGED_FUNCTION;\r
+\r
+#endif\r
+\r
+/*\r
+ * Searches pxList for a task with name pcNameToQuery - returning a handle to\r
+ * the task if it is found, or NULL if the task is not found.\r
+ */\r
+#if ( INCLUDE_xTaskGetTaskHandle == 1 )\r
+\r
+       static TCB_t *prvSearchForNameWithinSingleList( List_t *pxList, const char pcNameToQuery[] ) PRIVILEGED_FUNCTION;\r
 \r
 #endif\r
 \r
@@ -543,199 +534,416 @@ static void prvResetNextTaskUnblockTime( void );
         * Helper function used to pad task names with spaces when printing out\r
         * human readable tables of task information.\r
         */\r
-       static char *prvWriteNameToBuffer( char *pcBuffer, const char *pcTaskName );\r
+       static char *prvWriteNameToBuffer( char *pcBuffer, const char *pcTaskName ) PRIVILEGED_FUNCTION;\r
 \r
 #endif\r
-/*-----------------------------------------------------------*/\r
 \r
-BaseType_t xTaskGenericCreate( TaskFunction_t pxTaskCode, const char * const pcName, const uint16_t usStackDepth, void * const pvParameters, UBaseType_t uxPriority, TaskHandle_t * const pxCreatedTask, StackType_t * const puxStackBuffer, const MemoryRegion_t * const xRegions ) /*lint !e971 Unqualified char types are allowed for strings and single characters only. */\r
-{\r
-BaseType_t xReturn;\r
-TCB_t * pxNewTCB;\r
-StackType_t *pxTopOfStack;\r
+/*\r
+ * Called after a Task_t structure has been allocated either statically or\r
+ * dynamically to fill in the structure's members.\r
+ */\r
+static void prvInitialiseNewTask( TaskFunction_t pxTaskCode, const char * const pcName, const uint16_t usStackDepth, void * const pvParameters, UBaseType_t uxPriority, TaskHandle_t * const pxCreatedTask, TCB_t *pxNewTCB ) PRIVILEGED_FUNCTION; /*lint !e971 Unqualified char types are allowed for strings and single characters only. */\r
+\r
+/*\r
+ * Called after a new task has been created and initialised to place the task\r
+ * under the control of the scheduler.\r
+ */\r
+static void prvAddNewTaskToReadyList( TCB_t *pxNewTCB ) PRIVILEGED_FUNCTION;\r
 \r
-       configASSERT( pxTaskCode );\r
-       configASSERT( ( ( uxPriority & ( UBaseType_t ) ( ~portPRIVILEGE_BIT ) ) < ( UBaseType_t ) configMAX_PRIORITIES ) );\r
+/*-----------------------------------------------------------*/\r
 \r
-       /* Allocate the memory required by the TCB and stack for the new task,\r
-       checking that the allocation was successful. */\r
-       pxNewTCB = prvAllocateTCBAndStack( usStackDepth, puxStackBuffer );\r
+#if( configSUPPORT_STATIC_ALLOCATION == 1 )\r
 \r
-       if( pxNewTCB != NULL )\r
+       BaseType_t xTaskCreateStatic( TaskFunction_t pxTaskCode, const char * const pcName, const uint16_t usStackDepth, void * const pvParameters, UBaseType_t uxPriority, TaskHandle_t * const pxCreatedTask, StackType_t * const puxStackBuffer, StaticTask_t * const pxTaskBuffer ) /*lint !e971 Unqualified char types are allowed for strings and single characters only. */\r
        {\r
-               #if( portUSING_MPU_WRAPPERS == 1 )\r
-                       /* Should the task be created in privileged mode? */\r
-                       BaseType_t xRunPrivileged;\r
-                       if( ( uxPriority & portPRIVILEGE_BIT ) != 0U )\r
+       TCB_t *pxNewTCB;\r
+       BaseType_t xReturn;\r
+\r
+               configASSERT( puxStackBuffer != NULL );\r
+               configASSERT( pxTaskBuffer != NULL );\r
+\r
+               if( ( pxTaskBuffer != NULL ) && ( puxStackBuffer != NULL ) )\r
+               {\r
+                       /* The memory used for the task's TCB and stack are passed into this\r
+                       function - use them. */\r
+                       pxNewTCB = ( TCB_t * ) pxTaskBuffer; /*lint !e740 Unusual cast is ok as the structures are designed to have the same alignment, and the size is checked by an assert. */\r
+                       pxNewTCB->pxStack = ( StackType_t * ) puxStackBuffer;\r
+\r
+                       #if( configSUPPORT_DYNAMIC_ALLOCATION == 1 )\r
                        {\r
-                               xRunPrivileged = pdTRUE;\r
+                               /* Tasks can be created statically or dynamically, so note this\r
+                               task was created statically in case the task is later deleted. */\r
+                               pxNewTCB->ucStaticallyAllocated = pdTRUE;\r
                        }\r
-                       else\r
+                       #endif /* configSUPPORT_DYNAMIC_ALLOCATION */\r
+\r
+                       prvInitialiseNewTask( pxTaskCode, pcName, usStackDepth, pvParameters, uxPriority, pxCreatedTask, pxNewTCB );\r
+                       prvAddNewTaskToReadyList( pxNewTCB );\r
+                       xReturn = pdPASS;\r
+               }\r
+               else\r
+               {\r
+                       xReturn = errCOULD_NOT_ALLOCATE_REQUIRED_MEMORY;\r
+               }\r
+\r
+               return xReturn;\r
+       }\r
+\r
+#endif /* SUPPORT_STATIC_ALLOCATION */\r
+/*-----------------------------------------------------------*/\r
+\r
+#if( configSUPPORT_DYNAMIC_ALLOCATION == 1 )\r
+\r
+       BaseType_t xTaskCreate( TaskFunction_t pxTaskCode, const char * const pcName, const uint16_t usStackDepth, void * const pvParameters, UBaseType_t uxPriority, TaskHandle_t * const pxCreatedTask ) /*lint !e971 Unqualified char types are allowed for strings and single characters only. */\r
+       {\r
+       TCB_t *pxNewTCB;\r
+       BaseType_t xReturn;\r
+\r
+               /* If the stack grows down then allocate the stack then the TCB so the stack\r
+               does not grow into the TCB.  Likewise if the stack grows up then allocate\r
+               the TCB then the stack. */\r
+               #if( portSTACK_GROWTH > 0 )\r
+               {\r
+                       /* Allocate space for the TCB.  Where the memory comes from depends on\r
+                       the implementation of the port malloc function and whether or not static\r
+                       allocation is being used. */\r
+                       pxNewTCB = ( TCB_t * ) pvPortMalloc( sizeof( TCB_t ) );\r
+\r
+                       if( pxNewTCB != NULL )\r
                        {\r
-                               xRunPrivileged = pdFALSE;\r
+                               /* Allocate space for the stack used by the task being created.\r
+                               The base of the stack memory stored in the TCB so the task can\r
+                               be deleted later if required. */\r
+                               pxNewTCB->pxStack = ( StackType_t * ) pvPortMalloc( ( ( ( size_t ) usStackDepth ) * sizeof( StackType_t ) ) ); /*lint !e961 MISRA exception as the casts are only redundant for some ports. */\r
+\r
+                               if( pxNewTCB->pxStack == NULL )\r
+                               {\r
+                                       /* Could not allocate the stack.  Delete the allocated TCB. */\r
+                                       vPortFree( pxNewTCB );\r
+                                       pxNewTCB = NULL;\r
+                               }\r
                        }\r
-                       uxPriority &= ~portPRIVILEGE_BIT;\r
+               }\r
+               #else /* portSTACK_GROWTH */\r
+               {\r
+               StackType_t *pxStack;\r
 \r
-                       if( puxStackBuffer != NULL )\r
+                       /* Allocate space for the stack used by the task being created. */\r
+                       pxStack = ( StackType_t * ) pvPortMalloc( ( ( ( size_t ) usStackDepth ) * sizeof( StackType_t ) ) ); /*lint !e961 MISRA exception as the casts are only redundant for some ports. */\r
+\r
+                       if( pxStack != NULL )\r
                        {\r
-                               /* The application provided its own stack.  Note this so no\r
-                               attempt is made to delete the stack should that task be\r
-                               deleted. */\r
-                               pxNewTCB->xUsingStaticallyAllocatedStack = pdTRUE;\r
+                               /* Allocate space for the TCB. */\r
+                               pxNewTCB = ( TCB_t * ) pvPortMalloc( sizeof( TCB_t ) ); /*lint !e961 MISRA exception as the casts are only redundant for some paths. */\r
+\r
+                               if( pxNewTCB != NULL )\r
+                               {\r
+                                       /* Store the stack location in the TCB. */\r
+                                       pxNewTCB->pxStack = pxStack;\r
+                               }\r
+                               else\r
+                               {\r
+                                       /* The stack cannot be used as the TCB was not created.  Free\r
+                                       it again. */\r
+                                       vPortFree( pxStack );\r
+                               }\r
                        }\r
                        else\r
                        {\r
-                               /* The stack was allocated dynamically.  Note this so it can be\r
-                               deleted again if the task is deleted. */\r
-                               pxNewTCB->xUsingStaticallyAllocatedStack = pdFALSE;\r
+                               pxNewTCB = NULL;\r
                        }\r
-               #endif /* portUSING_MPU_WRAPPERS == 1 */\r
+               }\r
+               #endif /* portSTACK_GROWTH */\r
 \r
-               /* Calculate the top of stack address.  This depends on whether the\r
-               stack grows from high memory to low (as per the 80x86) or vice versa.\r
-               portSTACK_GROWTH is used to make the result positive or negative as\r
-               required by the port. */\r
-               #if( portSTACK_GROWTH < 0 )\r
+               if( pxNewTCB != NULL )\r
                {\r
-                       pxTopOfStack = pxNewTCB->pxStack + ( usStackDepth - ( uint16_t ) 1 );\r
-                       pxTopOfStack = ( StackType_t * ) ( ( ( portPOINTER_SIZE_TYPE ) pxTopOfStack ) & ( ~( ( portPOINTER_SIZE_TYPE ) portBYTE_ALIGNMENT_MASK ) ) ); /*lint !e923 MISRA exception.  Avoiding casts between pointers and integers is not practical.  Size differences accounted for using portPOINTER_SIZE_TYPE type. */\r
+                       #if( configSUPPORT_STATIC_ALLOCATION == 1 )\r
+                       {\r
+                               /* Tasks can be created statically or dynamically, so note this\r
+                               task was created dynamically in case it is later deleted. */\r
+                               pxNewTCB->ucStaticallyAllocated = pdFALSE;\r
+                       }\r
+                       #endif /* configSUPPORT_STATIC_ALLOCATION */\r
 \r
-                       /* Check the alignment of the calculated top of stack is correct. */\r
-                       configASSERT( ( ( ( portPOINTER_SIZE_TYPE ) pxTopOfStack & ( portPOINTER_SIZE_TYPE ) portBYTE_ALIGNMENT_MASK ) == 0UL ) );\r
+                       prvInitialiseNewTask( pxTaskCode, pcName, usStackDepth, pvParameters, uxPriority, pxCreatedTask, pxNewTCB );\r
+                       prvAddNewTaskToReadyList( pxNewTCB );\r
+                       xReturn = pdPASS;\r
                }\r
-               #else /* portSTACK_GROWTH */\r
+               else\r
                {\r
-                       pxTopOfStack = pxNewTCB->pxStack;\r
+                       xReturn = errCOULD_NOT_ALLOCATE_REQUIRED_MEMORY;\r
+               }\r
 \r
-                       /* Check the alignment of the stack buffer is correct. */\r
-                       configASSERT( ( ( ( portPOINTER_SIZE_TYPE ) pxNewTCB->pxStack & ( portPOINTER_SIZE_TYPE ) portBYTE_ALIGNMENT_MASK ) == 0UL ) );\r
+               return xReturn;\r
+       }\r
 \r
-                       /* If we want to use stack checking on architectures that use\r
-                       a positive stack growth direction then we also need to store the\r
-                       other extreme of the stack space. */\r
-                       pxNewTCB->pxEndOfStack = pxNewTCB->pxStack + ( usStackDepth - 1 );\r
-               }\r
-               #endif /* portSTACK_GROWTH */\r
+#endif /* configSUPPORT_DYNAMIC_ALLOCATION */\r
+/*-----------------------------------------------------------*/\r
 \r
-               /* Setup the newly allocated TCB with the initial state of the task. */\r
-               prvInitialiseTCBVariables( pxNewTCB, pcName, uxPriority, xRegions, usStackDepth );\r
+static void prvInitialiseNewTask( TaskFunction_t pxTaskCode, const char * const pcName, const uint16_t usStackDepth, void * const pvParameters, UBaseType_t uxPriority, TaskHandle_t * const pxCreatedTask, TCB_t *pxNewTCB ) /*lint !e971 Unqualified char types are allowed for strings and single characters only. */\r
+{\r
+StackType_t *pxTopOfStack;\r
+UBaseType_t x;\r
 \r
-               /* Initialize the TCB stack to look as if the task was already running,\r
-               but had been interrupted by the scheduler.  The return address is set\r
-               to the start of the task function. Once the stack has been initialised\r
-               the     top of stack variable is updated. */\r
-               #if( portUSING_MPU_WRAPPERS == 1 )\r
+       #if( portUSING_MPU_WRAPPERS == 1 )\r
+               /* Should the task be created in privileged mode? */\r
+               BaseType_t xRunPrivileged;\r
+               if( ( uxPriority & portPRIVILEGE_BIT ) != 0U )\r
                {\r
-                       pxNewTCB->pxTopOfStack = pxPortInitialiseStack( pxTopOfStack, pxTaskCode, pvParameters, xRunPrivileged );\r
+                       xRunPrivileged = pdTRUE;\r
                }\r
-               #else /* portUSING_MPU_WRAPPERS */\r
+               else\r
                {\r
-                       pxNewTCB->pxTopOfStack = pxPortInitialiseStack( pxTopOfStack, pxTaskCode, pvParameters );\r
+                       xRunPrivileged = pdFALSE;\r
                }\r
-               #endif /* portUSING_MPU_WRAPPERS */\r
+               uxPriority &= ~portPRIVILEGE_BIT;\r
+       #endif /* portUSING_MPU_WRAPPERS == 1 */\r
+\r
+       /* Avoid dependency on memset() if it is not required. */\r
+       #if( ( configCHECK_FOR_STACK_OVERFLOW > 1 ) || ( configUSE_TRACE_FACILITY == 1 ) || ( INCLUDE_uxTaskGetStackHighWaterMark == 1 ) )\r
+       {\r
+               /* Fill the stack with a known value to assist debugging. */\r
+               ( void ) memset( pxNewTCB->pxStack, ( int ) tskSTACK_FILL_BYTE, ( size_t ) usStackDepth * sizeof( StackType_t ) );\r
+       }\r
+       #endif /* ( ( configCHECK_FOR_STACK_OVERFLOW > 1 ) || ( ( configUSE_TRACE_FACILITY == 1 ) || ( INCLUDE_uxTaskGetStackHighWaterMark == 1 ) ) ) */\r
+\r
+       /* Calculate the top of stack address.  This depends on whether the stack\r
+       grows from high memory to low (as per the 80x86) or vice versa.\r
+       portSTACK_GROWTH is used to make the result positive or negative as required\r
+       by the port. */\r
+       #if( portSTACK_GROWTH < 0 )\r
+       {\r
+               pxTopOfStack = pxNewTCB->pxStack + ( usStackDepth - ( uint16_t ) 1 );\r
+               pxTopOfStack = ( StackType_t * ) ( ( ( portPOINTER_SIZE_TYPE ) pxTopOfStack ) & ( ~( ( portPOINTER_SIZE_TYPE ) portBYTE_ALIGNMENT_MASK ) ) ); /*lint !e923 MISRA exception.  Avoiding casts between pointers and integers is not practical.  Size differences accounted for using portPOINTER_SIZE_TYPE type. */\r
+\r
+               /* Check the alignment of the calculated top of stack is correct. */\r
+               configASSERT( ( ( ( portPOINTER_SIZE_TYPE ) pxTopOfStack & ( portPOINTER_SIZE_TYPE ) portBYTE_ALIGNMENT_MASK ) == 0UL ) );\r
+       }\r
+       #else /* portSTACK_GROWTH */\r
+       {\r
+               pxTopOfStack = pxNewTCB->pxStack;\r
+\r
+               /* Check the alignment of the stack buffer is correct. */\r
+               configASSERT( ( ( ( portPOINTER_SIZE_TYPE ) pxNewTCB->pxStack & ( portPOINTER_SIZE_TYPE ) portBYTE_ALIGNMENT_MASK ) == 0UL ) );\r
+\r
+               /* The other extreme of the stack space is required if stack checking is\r
+               performed. */\r
+               pxNewTCB->pxEndOfStack = pxNewTCB->pxStack + ( usStackDepth - ( uint16_t ) 1 );\r
+       }\r
+       #endif /* portSTACK_GROWTH */\r
 \r
-               if( ( void * ) pxCreatedTask != NULL )\r
+       /* Store the task name in the TCB. */\r
+       for( x = ( UBaseType_t ) 0; x < ( UBaseType_t ) configMAX_TASK_NAME_LEN; x++ )\r
+       {\r
+               pxNewTCB->pcTaskName[ x ] = pcName[ x ];\r
+\r
+               /* Don't copy all configMAX_TASK_NAME_LEN if the string is shorter than\r
+               configMAX_TASK_NAME_LEN characters just in case the memory after the\r
+               string is not accessible (extremely unlikely). */\r
+               if( pcName[ x ] == 0x00 )\r
                {\r
-                       /* Pass the TCB out - in an anonymous way.  The calling function/\r
-                       task can use this as a handle to delete the task later if\r
-                       required.*/\r
-                       *pxCreatedTask = ( TaskHandle_t ) pxNewTCB;\r
+                       break;\r
                }\r
                else\r
                {\r
                        mtCOVERAGE_TEST_MARKER();\r
                }\r
+       }\r
 \r
-               /* Ensure interrupts don't access the task lists while they are being\r
-               updated. */\r
-               taskENTER_CRITICAL();\r
+       /* Ensure the name string is terminated in the case that the string length\r
+       was greater or equal to configMAX_TASK_NAME_LEN. */\r
+       pxNewTCB->pcTaskName[ configMAX_TASK_NAME_LEN - 1 ] = '\0';\r
+\r
+       /* This is used as an array index so must ensure it's not too large.  First\r
+       remove the privilege bit if one is present. */\r
+       if( uxPriority >= ( UBaseType_t ) configMAX_PRIORITIES )\r
+       {\r
+               uxPriority = ( UBaseType_t ) configMAX_PRIORITIES - ( UBaseType_t ) 1U;\r
+       }\r
+       else\r
+       {\r
+               mtCOVERAGE_TEST_MARKER();\r
+       }\r
+\r
+       pxNewTCB->uxPriority = uxPriority;\r
+       #if ( configUSE_MUTEXES == 1 )\r
+       {\r
+               pxNewTCB->uxBasePriority = uxPriority;\r
+               pxNewTCB->uxMutexesHeld = 0;\r
+       }\r
+       #endif /* configUSE_MUTEXES */\r
+\r
+       vListInitialiseItem( &( pxNewTCB->xStateListItem ) );\r
+       vListInitialiseItem( &( pxNewTCB->xEventListItem ) );\r
+\r
+       /* Set the pxNewTCB as a link back from the ListItem_t.  This is so we can get\r
+       back to the containing TCB from a generic item in a list. */\r
+       listSET_LIST_ITEM_OWNER( &( pxNewTCB->xStateListItem ), pxNewTCB );\r
+\r
+       /* Event lists are always in priority order. */\r
+       listSET_LIST_ITEM_VALUE( &( pxNewTCB->xEventListItem ), ( TickType_t ) configMAX_PRIORITIES - ( TickType_t ) uxPriority ); /*lint !e961 MISRA exception as the casts are only redundant for some ports. */\r
+       listSET_LIST_ITEM_OWNER( &( pxNewTCB->xEventListItem ), pxNewTCB );\r
+\r
+       #if ( portCRITICAL_NESTING_IN_TCB == 1 )\r
+       {\r
+               pxNewTCB->uxCriticalNesting = ( UBaseType_t ) 0U;\r
+       }\r
+       #endif /* portCRITICAL_NESTING_IN_TCB */\r
+\r
+       #if ( configUSE_APPLICATION_TASK_TAG == 1 )\r
+       {\r
+               pxNewTCB->pxTaskTag = NULL;\r
+       }\r
+       #endif /* configUSE_APPLICATION_TASK_TAG */\r
+\r
+       #if ( configGENERATE_RUN_TIME_STATS == 1 )\r
+       {\r
+               pxNewTCB->ulRunTimeCounter = 0UL;\r
+       }\r
+       #endif /* configGENERATE_RUN_TIME_STATS */\r
+\r
+       #if ( portUSING_MPU_WRAPPERS == 1 )\r
+       {\r
+               vPortStoreTaskMPUSettings( &( pxNewTCB->xMPUSettings ), xRegions, pxNewTCB->pxStack, usStackDepth );\r
+       }\r
+       #endif\r
+\r
+       #if( configNUM_THREAD_LOCAL_STORAGE_POINTERS != 0 )\r
+       {\r
+               for( x = 0; x < ( UBaseType_t ) configNUM_THREAD_LOCAL_STORAGE_POINTERS; x++ )\r
                {\r
-                       uxCurrentNumberOfTasks++;\r
-                       if( pxCurrentTCB == NULL )\r
-                       {\r
-                               /* There are no other tasks, or all the other tasks are in\r
-                               the suspended state - make this the current task. */\r
-                               pxCurrentTCB =  pxNewTCB;\r
+                       pxNewTCB->pvThreadLocalStoragePointers[ x ] = NULL;\r
+               }\r
+       }\r
+       #endif\r
 \r
-                               if( uxCurrentNumberOfTasks == ( UBaseType_t ) 1 )\r
-                               {\r
-                                       /* This is the first task to be created so do the preliminary\r
-                                       initialisation required.  We will not recover if this call\r
-                                       fails, but we will report the failure. */\r
-                                       prvInitialiseTaskLists();\r
-                               }\r
-                               else\r
-                               {\r
-                                       mtCOVERAGE_TEST_MARKER();\r
-                               }\r
+       #if ( configUSE_TASK_NOTIFICATIONS == 1 )\r
+       {\r
+               pxNewTCB->ulNotifiedValue = 0;\r
+               pxNewTCB->ucNotifyState = taskNOT_WAITING_NOTIFICATION;\r
+       }\r
+       #endif\r
+\r
+       #if ( configUSE_NEWLIB_REENTRANT == 1 )\r
+       {\r
+               /* Initialise this task's Newlib reent structure. */\r
+               _REENT_INIT_PTR( ( &( pxNewTCB->xNewLib_reent ) ) );\r
+       }\r
+       #endif\r
+\r
+       #if( INCLUDE_xTaskAbortDelay == 1 )\r
+       {\r
+               pxNewTCB->ucDelayAborted = pdFALSE;\r
+       }\r
+       #endif\r
+\r
+       /* Initialize the TCB stack to look as if the task was already running,\r
+       but had been interrupted by the scheduler.  The return address is set\r
+       to the start of the task function. Once the stack has been initialised\r
+       the     top of stack variable is updated. */\r
+       #if( portUSING_MPU_WRAPPERS == 1 )\r
+       {\r
+               pxNewTCB->pxTopOfStack = pxPortInitialiseStack( pxTopOfStack, pxTaskCode, pvParameters, xRunPrivileged );\r
+       }\r
+       #else /* portUSING_MPU_WRAPPERS */\r
+       {\r
+               pxNewTCB->pxTopOfStack = pxPortInitialiseStack( pxTopOfStack, pxTaskCode, pvParameters );\r
+       }\r
+       #endif /* portUSING_MPU_WRAPPERS */\r
+\r
+       if( ( void * ) pxCreatedTask != NULL )\r
+       {\r
+               /* Pass the handle out in an anonymous way.  The handle can be used to\r
+               change the created task's priority, delete the created task, etc.*/\r
+               *pxCreatedTask = ( TaskHandle_t ) pxNewTCB;\r
+       }\r
+       else\r
+       {\r
+               mtCOVERAGE_TEST_MARKER();\r
+       }\r
+}\r
+/*-----------------------------------------------------------*/\r
+\r
+static void prvAddNewTaskToReadyList( TCB_t *pxNewTCB )\r
+{\r
+       /* Ensure interrupts don't access the task lists while the lists are being\r
+       updated. */\r
+       taskENTER_CRITICAL();\r
+       {\r
+               uxCurrentNumberOfTasks++;\r
+               if( pxCurrentTCB == NULL )\r
+               {\r
+                       /* There are no other tasks, or all the other tasks are in\r
+                       the suspended state - make this the current task. */\r
+                       pxCurrentTCB = pxNewTCB;\r
+\r
+                       if( uxCurrentNumberOfTasks == ( UBaseType_t ) 1 )\r
+                       {\r
+                               /* This is the first task to be created so do the preliminary\r
+                               initialisation required.  We will not recover if this call\r
+                               fails, but we will report the failure. */\r
+                               prvInitialiseTaskLists();\r
                        }\r
                        else\r
                        {\r
-                               /* If the scheduler is not already running, make this task the\r
-                               current task if it is the highest priority task to be created\r
-                               so far. */\r
-                               if( xSchedulerRunning == pdFALSE )\r
+                               mtCOVERAGE_TEST_MARKER();\r
+                       }\r
+               }\r
+               else\r
+               {\r
+                       /* If the scheduler is not already running, make this task the\r
+                       current task if it is the highest priority task to be created\r
+                       so far. */\r
+                       if( xSchedulerRunning == pdFALSE )\r
+                       {\r
+                               if( pxCurrentTCB->uxPriority <= pxNewTCB->uxPriority )\r
                                {\r
-                                       if( pxCurrentTCB->uxPriority <= uxPriority )\r
-                                       {\r
-                                               pxCurrentTCB = pxNewTCB;\r
-                                       }\r
-                                       else\r
-                                       {\r
-                                               mtCOVERAGE_TEST_MARKER();\r
-                                       }\r
+                                       pxCurrentTCB = pxNewTCB;\r
                                }\r
                                else\r
                                {\r
                                        mtCOVERAGE_TEST_MARKER();\r
                                }\r
                        }\r
-\r
-                       uxTaskNumber++;\r
-\r
-                       #if ( configUSE_TRACE_FACILITY == 1 )\r
+                       else\r
                        {\r
-                               /* Add a counter into the TCB for tracing only. */\r
-                               pxNewTCB->uxTCBNumber = uxTaskNumber;\r
+                               mtCOVERAGE_TEST_MARKER();\r
                        }\r
-                       #endif /* configUSE_TRACE_FACILITY */\r
-                       traceTASK_CREATE( pxNewTCB );\r
+               }\r
 \r
-                       prvAddTaskToReadyList( pxNewTCB );\r
+               uxTaskNumber++;\r
 \r
-                       xReturn = pdPASS;\r
-                       portSETUP_TCB( pxNewTCB );\r
+               #if ( configUSE_TRACE_FACILITY == 1 )\r
+               {\r
+                       /* Add a counter into the TCB for tracing only. */\r
+                       pxNewTCB->uxTCBNumber = uxTaskNumber;\r
                }\r
-               taskEXIT_CRITICAL();\r
-       }\r
-       else\r
-       {\r
-               xReturn = errCOULD_NOT_ALLOCATE_REQUIRED_MEMORY;\r
-               traceTASK_CREATE_FAILED();\r
+               #endif /* configUSE_TRACE_FACILITY */\r
+               traceTASK_CREATE( pxNewTCB );\r
+\r
+               prvAddTaskToReadyList( pxNewTCB );\r
+\r
+               portSETUP_TCB( pxNewTCB );\r
        }\r
+       taskEXIT_CRITICAL();\r
 \r
-       if( xReturn == pdPASS )\r
+       if( xSchedulerRunning != pdFALSE )\r
        {\r
-               if( xSchedulerRunning != pdFALSE )\r
-               {\r
-                       /* If the created task is of a higher priority than the current task\r
-                       then it should run now. */\r
-                       if( pxCurrentTCB->uxPriority < uxPriority )\r
-                       {\r
-                               taskYIELD_IF_USING_PREEMPTION();\r
-                       }\r
-                       else\r
-                       {\r
-                               mtCOVERAGE_TEST_MARKER();\r
-                       }\r
+               /* If the created task is of a higher priority than the current task\r
+               then it should run now. */\r
+               if( pxCurrentTCB->uxPriority < pxNewTCB->uxPriority )\r
+               {\r
+                       taskYIELD_IF_USING_PREEMPTION();\r
                }\r
                else\r
                {\r
                        mtCOVERAGE_TEST_MARKER();\r
                }\r
        }\r
-\r
-       return xReturn;\r
+       else\r
+       {\r
+               mtCOVERAGE_TEST_MARKER();\r
+       }\r
 }\r
 /*-----------------------------------------------------------*/\r
 \r
@@ -751,11 +959,8 @@ StackType_t *pxTopOfStack;
                        being deleted. */\r
                        pxTCB = prvGetTCBFromHandle( xTaskToDelete );\r
 \r
-                       /* Remove task from the ready list and place in the     termination list.\r
-                       This will stop the task from be scheduled.  The idle task will check\r
-                       the termination list and free up any memory allocated by the\r
-                       scheduler for the TCB and stack. */\r
-                       if( uxListRemove( &( pxTCB->xGenericListItem ) ) == ( UBaseType_t ) 0 )\r
+                       /* Remove task from the ready list. */\r
+                       if( uxListRemove( &( pxTCB->xStateListItem ) ) == ( UBaseType_t ) 0 )\r
                        {\r
                                taskRESET_READY_PRIORITY( pxTCB->uxPriority );\r
                        }\r
@@ -774,15 +979,28 @@ StackType_t *pxTopOfStack;
                                mtCOVERAGE_TEST_MARKER();\r
                        }\r
 \r
-                       vListInsertEnd( &xTasksWaitingTermination, &( pxTCB->xGenericListItem ) );\r
-\r
-                       /* Increment the ucTasksDeleted variable so the idle task knows\r
-                       there is a task that has been deleted and that it should therefore\r
-                       check the xTasksWaitingTermination list. */\r
-                       ++uxTasksDeleted;\r
+                       if( pxTCB == pxCurrentTCB )\r
+                       {\r
+                               /* A task is deleting itself.  This cannot complete within the\r
+                               task itself, as a context switch to another task is required.\r
+                               Place the task in the termination list.  The idle task will\r
+                               check the termination list and free up any memory allocated by\r
+                               the scheduler for the TCB and stack of the deleted task. */\r
+                               vListInsertEnd( &xTasksWaitingTermination, &( pxTCB->xStateListItem ) );\r
+\r
+                               /* Increment the ucTasksDeleted variable so the idle task knows\r
+                               there is a task that has been deleted and that it should therefore\r
+                               check the xTasksWaitingTermination list. */\r
+                               ++uxDeletedTasksWaitingCleanUp;\r
+                       }\r
+                       else\r
+                       {\r
+                               --uxCurrentNumberOfTasks;\r
+                               prvDeleteTCB( pxTCB );\r
+                       }\r
 \r
-                       /* Increment the uxTaskNumberVariable also so kernel aware debuggers\r
-                       can detect that the task lists need re-generating. */\r
+                       /* Increment the uxTaskNumber also so kernel aware debuggers can\r
+                       detect that the task lists need re-generating. */\r
                        uxTaskNumber++;\r
 \r
                        traceTASK_DELETE( pxTCB );\r
@@ -877,23 +1095,11 @@ StackType_t *pxTopOfStack;
 \r
                        if( xShouldDelay != pdFALSE )\r
                        {\r
-                               traceTASK_DELAY_UNTIL();\r
-\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
-                                       no need to check, and the port reset macro can be called\r
-                                       directly. */\r
-                                       portRESET_READY_PRIORITY( pxCurrentTCB->uxPriority, uxTopReadyPriority );\r
-                               }\r
-                               else\r
-                               {\r
-                                       mtCOVERAGE_TEST_MARKER();\r
-                               }\r
+                               traceTASK_DELAY_UNTIL( xTimeToWake );\r
 \r
-                               prvAddCurrentTaskToDelayedList( xTimeToWake );\r
+                               /* prvAddCurrentTaskToDelayedList() needs the block time, not\r
+                               the time to wake, so subtract the current tick count. */\r
+                               prvAddCurrentTaskToDelayedList( xTimeToWake - xConstTickCount, pdFALSE );\r
                        }\r
                        else\r
                        {\r
@@ -921,10 +1127,8 @@ StackType_t *pxTopOfStack;
 \r
        void vTaskDelay( const TickType_t xTicksToDelay )\r
        {\r
-       TickType_t xTimeToWake;\r
        BaseType_t xAlreadyYielded = pdFALSE;\r
 \r
-\r
                /* A delay time of zero just forces a reschedule. */\r
                if( xTicksToDelay > ( TickType_t ) 0U )\r
                {\r
@@ -940,26 +1144,7 @@ StackType_t *pxTopOfStack;
 \r
                                This task cannot be in an event list as it is the currently\r
                                executing task. */\r
-\r
-                               /* Calculate the time to wake - this may overflow but this is\r
-                               not a problem. */\r
-                               xTimeToWake = xTickCount + xTicksToDelay;\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
-                               if( uxListRemove( &( pxCurrentTCB->xGenericListItem ) ) == ( UBaseType_t ) 0 )\r
-                               {\r
-                                       /* The current task must be in a ready list, so there is\r
-                                       no need to check, and the port reset macro can be called\r
-                                       directly. */\r
-                                       portRESET_READY_PRIORITY( pxCurrentTCB->uxPriority, uxTopReadyPriority );\r
-                               }\r
-                               else\r
-                               {\r
-                                       mtCOVERAGE_TEST_MARKER();\r
-                               }\r
-                               prvAddCurrentTaskToDelayedList( xTimeToWake );\r
+                               prvAddCurrentTaskToDelayedList( xTicksToDelay, pdFALSE );\r
                        }\r
                        xAlreadyYielded = xTaskResumeAll();\r
                }\r
@@ -983,7 +1168,7 @@ StackType_t *pxTopOfStack;
 #endif /* INCLUDE_vTaskDelay */\r
 /*-----------------------------------------------------------*/\r
 \r
-#if ( INCLUDE_eTaskGetState == 1 )\r
+#if( ( INCLUDE_eTaskGetState == 1 ) || ( configUSE_TRACE_FACILITY == 1 ) )\r
 \r
        eTaskState eTaskGetState( TaskHandle_t xTask )\r
        {\r
@@ -1002,7 +1187,7 @@ StackType_t *pxTopOfStack;
                {\r
                        taskENTER_CRITICAL();\r
                        {\r
-                               pxStateList = ( List_t * ) listLIST_ITEM_CONTAINER( &( pxTCB->xGenericListItem ) );\r
+                               pxStateList = ( List_t * ) listLIST_ITEM_CONTAINER( &( pxTCB->xStateListItem ) );\r
                        }\r
                        taskEXIT_CRITICAL();\r
 \r
@@ -1031,10 +1216,11 @@ StackType_t *pxTopOfStack;
                        #endif\r
 \r
                        #if ( INCLUDE_vTaskDelete == 1 )\r
-                               else if( pxStateList == &xTasksWaitingTermination )\r
+                               else if( ( pxStateList == &xTasksWaitingTermination ) || ( pxStateList == NULL ) )\r
                                {\r
                                        /* The task being queried is referenced from the deleted\r
-                                       tasks list. */\r
+                                       tasks list, or it is not referenced from any lists at\r
+                                       all. */\r
                                        eReturn = eDeleted;\r
                                }\r
                        #endif\r
@@ -1062,8 +1248,8 @@ StackType_t *pxTopOfStack;
 \r
                taskENTER_CRITICAL();\r
                {\r
-                       /* If null is passed in here then we are changing the\r
-                       priority of the calling function. */\r
+                       /* If null is passed in here then it is the priority of the that\r
+                       called uxTaskPriorityGet() that is being queried. */\r
                        pxTCB = prvGetTCBFromHandle( xTask );\r
                        uxReturn = pxTCB->uxPriority;\r
                }\r
@@ -1236,12 +1422,12 @@ StackType_t *pxTopOfStack;
                                nothing more than change it's priority variable. However, if\r
                                the task is in a ready list it needs to be removed and placed\r
                                in the list appropriate to its new priority. */\r
-                               if( listIS_CONTAINED_WITHIN( &( pxReadyTasksLists[ uxPriorityUsedOnEntry ] ), &( pxTCB->xGenericListItem ) ) != pdFALSE )\r
+                               if( listIS_CONTAINED_WITHIN( &( pxReadyTasksLists[ uxPriorityUsedOnEntry ] ), &( pxTCB->xStateListItem ) ) != pdFALSE )\r
                                {\r
                                        /* The task is currently in its ready list - remove before adding\r
                                        it to it's new ready list.  As we are in a critical section we\r
                                        can do this even if the scheduler is suspended. */\r
-                                       if( uxListRemove( &( pxTCB->xGenericListItem ) ) == ( UBaseType_t ) 0 )\r
+                                       if( uxListRemove( &( pxTCB->xStateListItem ) ) == ( UBaseType_t ) 0 )\r
                                        {\r
                                                /* It is known that the task is in its ready list so\r
                                                there is no need to check again and the port level\r
@@ -1259,7 +1445,7 @@ StackType_t *pxTopOfStack;
                                        mtCOVERAGE_TEST_MARKER();\r
                                }\r
 \r
-                               if( xYieldRequired == pdTRUE )\r
+                               if( xYieldRequired != pdFALSE )\r
                                {\r
                                        taskYIELD_IF_USING_PREEMPTION();\r
                                }\r
@@ -1295,7 +1481,7 @@ StackType_t *pxTopOfStack;
 \r
                        /* Remove task from the ready/delayed list and place in the\r
                        suspended list. */\r
-                       if( uxListRemove( &( pxTCB->xGenericListItem ) ) == ( UBaseType_t ) 0 )\r
+                       if( uxListRemove( &( pxTCB->xStateListItem ) ) == ( UBaseType_t ) 0 )\r
                        {\r
                                taskRESET_READY_PRIORITY( pxTCB->uxPriority );\r
                        }\r
@@ -1314,10 +1500,25 @@ StackType_t *pxTopOfStack;
                                mtCOVERAGE_TEST_MARKER();\r
                        }\r
 \r
-                       vListInsertEnd( &xSuspendedTaskList, &( pxTCB->xGenericListItem ) );\r
+                       vListInsertEnd( &xSuspendedTaskList, &( pxTCB->xStateListItem ) );\r
                }\r
                taskEXIT_CRITICAL();\r
 \r
+               if( xSchedulerRunning != pdFALSE )\r
+               {\r
+                       /* Reset the next expected unblock time in case it referred to the\r
+                       task that is now in the Suspended state. */\r
+                       taskENTER_CRITICAL();\r
+                       {\r
+                               prvResetNextTaskUnblockTime();\r
+                       }\r
+                       taskEXIT_CRITICAL();\r
+               }\r
+               else\r
+               {\r
+                       mtCOVERAGE_TEST_MARKER();\r
+               }\r
+\r
                if( pxTCB == pxCurrentTCB )\r
                {\r
                        if( xSchedulerRunning != pdFALSE )\r
@@ -1347,21 +1548,7 @@ StackType_t *pxTopOfStack;
                }\r
                else\r
                {\r
-                       if( xSchedulerRunning != pdFALSE )\r
-                       {\r
-                               /* A task other than the currently running task was suspended,\r
-                               reset the next expected unblock time in case it referred to the\r
-                               task that is now in the Suspended state. */\r
-                               taskENTER_CRITICAL();\r
-                               {\r
-                                       prvResetNextTaskUnblockTime();\r
-                               }\r
-                               taskEXIT_CRITICAL();\r
-                       }\r
-                       else\r
-                       {\r
-                               mtCOVERAGE_TEST_MARKER();\r
-                       }\r
+                       mtCOVERAGE_TEST_MARKER();\r
                }\r
        }\r
 \r
@@ -1382,7 +1569,7 @@ StackType_t *pxTopOfStack;
                configASSERT( xTask );\r
 \r
                /* Is the task being resumed actually in the suspended list? */\r
-               if( listIS_CONTAINED_WITHIN( &xSuspendedTaskList, &( pxTCB->xGenericListItem ) ) != pdFALSE )\r
+               if( listIS_CONTAINED_WITHIN( &xSuspendedTaskList, &( pxTCB->xStateListItem ) ) != pdFALSE )\r
                {\r
                        /* Has the task already been resumed from within an ISR? */\r
                        if( listIS_CONTAINED_WITHIN( &xPendingReadyList, &( pxTCB->xEventListItem ) ) == pdFALSE )\r
@@ -1429,13 +1616,13 @@ StackType_t *pxTopOfStack;
                {\r
                        taskENTER_CRITICAL();\r
                        {\r
-                               if( prvTaskIsTaskSuspended( pxTCB ) == pdTRUE )\r
+                               if( prvTaskIsTaskSuspended( pxTCB ) != pdFALSE )\r
                                {\r
                                        traceTASK_RESUME( pxTCB );\r
 \r
                                        /* As we are in a critical section we can access the ready\r
                                        lists even if the scheduler is suspended. */\r
-                                       ( void ) uxListRemove(  &( pxTCB->xGenericListItem ) );\r
+                                       ( void ) uxListRemove(  &( pxTCB->xStateListItem ) );\r
                                        prvAddTaskToReadyList( pxTCB );\r
 \r
                                        /* We may have just resumed a higher priority task. */\r
@@ -1498,7 +1685,7 @@ StackType_t *pxTopOfStack;
 \r
                uxSavedInterruptStatus = portSET_INTERRUPT_MASK_FROM_ISR();\r
                {\r
-                       if( prvTaskIsTaskSuspended( pxTCB ) == pdTRUE )\r
+                       if( prvTaskIsTaskSuspended( pxTCB ) != pdFALSE )\r
                        {\r
                                traceTASK_RESUME_FROM_ISR( pxTCB );\r
 \r
@@ -1516,7 +1703,7 @@ StackType_t *pxTopOfStack;
                                                mtCOVERAGE_TEST_MARKER();\r
                                        }\r
 \r
-                                       ( void ) uxListRemove(  &( pxTCB->xGenericListItem ) );\r
+                                       ( void ) uxListRemove( &( pxTCB->xStateListItem ) );\r
                                        prvAddTaskToReadyList( pxTCB );\r
                                }\r
                                else\r
@@ -1543,20 +1730,25 @@ StackType_t *pxTopOfStack;
 void vTaskStartScheduler( void )\r
 {\r
 BaseType_t xReturn;\r
+uint16_t usIdleTaskStackSize = configMINIMAL_STACK_SIZE;\r
 \r
        /* Add the idle task at the lowest priority. */\r
-       #if ( INCLUDE_xTaskGetIdleTaskHandle == 1 )\r
+       #if( configSUPPORT_STATIC_ALLOCATION == 1 )\r
        {\r
-               /* Create the idle task, storing its handle in xIdleTaskHandle so it can\r
-               be returned by the xTaskGetIdleTaskHandle() function. */\r
-               xReturn = xTaskCreate( prvIdleTask, "IDLE", tskIDLE_STACK_SIZE, ( void * ) NULL, ( tskIDLE_PRIORITY | portPRIVILEGE_BIT ), &xIdleTaskHandle ); /*lint !e961 MISRA exception, justified as it is not a redundant explicit cast to all supported compilers. */\r
+               StaticTask_t *pxIdleTaskTCBBuffer = NULL;\r
+               StackType_t *pxIdleTaskStackBuffer = NULL;\r
+\r
+               /* The Idle task is created using user provided RAM - obtain the\r
+               address of the RAM then create the idle task. */\r
+               vApplicationGetIdleTaskMemory( &pxIdleTaskTCBBuffer, &pxIdleTaskStackBuffer, &usIdleTaskStackSize );\r
+               xReturn = xTaskCreateStatic( prvIdleTask, "IDLE", usIdleTaskStackSize, ( void * ) NULL, ( tskIDLE_PRIORITY | portPRIVILEGE_BIT ), &xIdleTaskHandle, pxIdleTaskStackBuffer, pxIdleTaskTCBBuffer ); /*lint !e961 MISRA exception, justified as it is not a redundant explicit cast to all supported compilers. */\r
        }\r
        #else\r
        {\r
-               /* Create the idle task without storing its handle. */\r
-               xReturn = xTaskCreate( prvIdleTask, "IDLE", tskIDLE_STACK_SIZE, ( void * ) NULL, ( tskIDLE_PRIORITY | portPRIVILEGE_BIT ), NULL );  /*lint !e961 MISRA exception, justified as it is not a redundant explicit cast to all supported compilers. */\r
+               /* The Idle task is being created using dynamically allocated RAM. */\r
+               xReturn = xTaskCreate( prvIdleTask, "IDLE", usIdleTaskStackSize, ( void * ) NULL, ( tskIDLE_PRIORITY | portPRIVILEGE_BIT ), &xIdleTaskHandle ); /*lint !e961 MISRA exception, justified as it is not a redundant explicit cast to all supported compilers. */\r
        }\r
-       #endif /* INCLUDE_xTaskGetIdleTaskHandle */\r
+       #endif /* configSUPPORT_STATIC_ALLOCATION */\r
 \r
        #if ( configUSE_TIMERS == 1 )\r
        {\r
@@ -1616,6 +1808,10 @@ BaseType_t xReturn;
                or the timer task. */\r
                configASSERT( xReturn );\r
        }\r
+\r
+       /* Prevent compiler warnings if INCLUDE_xTaskGetIdleTaskHandle is set to 0,\r
+       meaning xIdleTaskHandle is not used anywhere else. */\r
+       ( void ) xIdleTaskHandle;\r
 }\r
 /*-----------------------------------------------------------*/\r
 \r
@@ -1645,6 +1841,34 @@ void vTaskSuspendAll( void )
        static TickType_t prvGetExpectedIdleTime( void )\r
        {\r
        TickType_t xReturn;\r
+       UBaseType_t uxHigherPriorityReadyTasks = pdFALSE;\r
+\r
+               /* uxHigherPriorityReadyTasks takes care of the case where\r
+               configUSE_PREEMPTION is 0, so there may be tasks above the idle priority\r
+               task that are in the Ready state, even though the idle task is\r
+               running. */\r
+               #if( configUSE_PORT_OPTIMISED_TASK_SELECTION == 0 )\r
+               {\r
+                       if( uxTopReadyPriority > tskIDLE_PRIORITY )\r
+                       {\r
+                               uxHigherPriorityReadyTasks = pdTRUE;\r
+                       }\r
+               }\r
+               #else\r
+               {\r
+                       const UBaseType_t uxLeastSignificantBit = ( UBaseType_t ) 0x01;\r
+\r
+                       /* When port optimised task selection is used the uxTopReadyPriority\r
+                       variable is used as a bit map.  If bits other than the least\r
+                       significant bit are set then there are tasks that have a priority\r
+                       above the idle priority that are in the Ready state.  This takes\r
+                       care of the case where the co-operative scheduler is in use. */\r
+                       if( uxTopReadyPriority > uxLeastSignificantBit )\r
+                       {\r
+                               uxHigherPriorityReadyTasks = pdTRUE;\r
+                       }\r
+               }\r
+               #endif\r
 \r
                if( pxCurrentTCB->uxPriority > tskIDLE_PRIORITY )\r
                {\r
@@ -1657,6 +1881,13 @@ void vTaskSuspendAll( void )
                        processed. */\r
                        xReturn = 0;\r
                }\r
+               else if( uxHigherPriorityReadyTasks != pdFALSE )\r
+               {\r
+                       /* There are tasks in the Ready state that have a priority above the\r
+                       idle priority.  This path can only be reached if\r
+                       configUSE_PREEMPTION is 0. */\r
+                       xReturn = 0;\r
+               }\r
                else\r
                {\r
                        xReturn = xNextTaskUnblockTime - xTickCount;\r
@@ -1670,7 +1901,7 @@ void vTaskSuspendAll( void )
 \r
 BaseType_t xTaskResumeAll( void )\r
 {\r
-TCB_t *pxTCB;\r
+TCB_t *pxTCB = NULL;\r
 BaseType_t xAlreadyYielded = pdFALSE;\r
 \r
        /* If uxSchedulerSuspended is zero then this function does not match a\r
@@ -1696,7 +1927,7 @@ BaseType_t xAlreadyYielded = pdFALSE;
                                {\r
                                        pxTCB = ( TCB_t * ) listGET_OWNER_OF_HEAD_ENTRY( ( &xPendingReadyList ) );\r
                                        ( void ) uxListRemove( &( pxTCB->xEventListItem ) );\r
-                                       ( void ) uxListRemove( &( pxTCB->xGenericListItem ) );\r
+                                       ( void ) uxListRemove( &( pxTCB->xStateListItem ) );\r
                                        prvAddTaskToReadyList( pxTCB );\r
 \r
                                        /* If the moved task has a priority higher than the current\r
@@ -1711,31 +1942,48 @@ BaseType_t xAlreadyYielded = pdFALSE;
                                        }\r
                                }\r
 \r
+                               if( pxTCB != NULL )\r
+                               {\r
+                                       /* A task was unblocked while the scheduler was suspended,\r
+                                       which may have prevented the next unblock time from being\r
+                                       re-calculated, in which case re-calculate it now.  Mainly\r
+                                       important for low power tickless implementations, where\r
+                                       this can prevent an unnecessary exit from low power\r
+                                       state. */\r
+                                       prvResetNextTaskUnblockTime();\r
+                               }\r
+\r
                                /* If any ticks occurred while the scheduler was suspended then\r
                                they should be processed now.  This ensures the tick count does\r
                                not     slip, and that any delayed tasks are resumed at the correct\r
                                time. */\r
-                               if( uxPendedTicks > ( UBaseType_t ) 0U )\r
                                {\r
-                                       while( uxPendedTicks > ( UBaseType_t ) 0U )\r
+                                       UBaseType_t uxPendedCounts = uxPendedTicks; /* Non-volatile copy. */\r
+\r
+                                       if( uxPendedCounts > ( UBaseType_t ) 0U )\r
                                        {\r
-                                               if( xTaskIncrementTick() != pdFALSE )\r
-                                               {\r
-                                                       xYieldPending = pdTRUE;\r
-                                               }\r
-                                               else\r
+                                               do\r
                                                {\r
-                                                       mtCOVERAGE_TEST_MARKER();\r
-                                               }\r
-                                               --uxPendedTicks;\r
+                                                       if( xTaskIncrementTick() != pdFALSE )\r
+                                                       {\r
+                                                               xYieldPending = pdTRUE;\r
+                                                       }\r
+                                                       else\r
+                                                       {\r
+                                                               mtCOVERAGE_TEST_MARKER();\r
+                                                       }\r
+                                                       --uxPendedCounts;\r
+                                               } while( uxPendedCounts > ( UBaseType_t ) 0U );\r
+\r
+                                               uxPendedTicks = 0;\r
+                                       }\r
+                                       else\r
+                                       {\r
+                                               mtCOVERAGE_TEST_MARKER();\r
                                        }\r
-                               }\r
-                               else\r
-                               {\r
-                                       mtCOVERAGE_TEST_MARKER();\r
                                }\r
 \r
-                               if( xYieldPending == pdTRUE )\r
+                               if( xYieldPending != pdFALSE )\r
                                {\r
                                        #if( configUSE_PREEMPTION != 0 )\r
                                        {\r
@@ -1815,19 +2063,142 @@ UBaseType_t uxTaskGetNumberOfTasks( void )
 }\r
 /*-----------------------------------------------------------*/\r
 \r
-#if ( INCLUDE_pcTaskGetTaskName == 1 )\r
+char *pcTaskGetName( TaskHandle_t xTaskToQuery ) /*lint !e971 Unqualified char types are allowed for strings and single characters only. */\r
+{\r
+TCB_t *pxTCB;\r
+\r
+       /* If null is passed in here then the name of the calling task is being\r
+       queried. */\r
+       pxTCB = prvGetTCBFromHandle( xTaskToQuery );\r
+       configASSERT( pxTCB );\r
+       return &( pxTCB->pcTaskName[ 0 ] );\r
+}\r
+/*-----------------------------------------------------------*/\r
+\r
+#if ( INCLUDE_xTaskGetTaskHandle == 1 )\r
 \r
-       char *pcTaskGetTaskName( TaskHandle_t xTaskToQuery ) /*lint !e971 Unqualified char types are allowed for strings and single characters only. */\r
+       static TCB_t *prvSearchForNameWithinSingleList( List_t *pxList, const char pcNameToQuery[] )\r
        {\r
-       TCB_t *pxTCB;\r
+       TCB_t *pxNextTCB, *pxFirstTCB, *pxReturn = NULL;\r
+       UBaseType_t x;\r
+       char cNextChar;\r
 \r
-               /* If null is passed in here then the name of the calling task is being queried. */\r
-               pxTCB = prvGetTCBFromHandle( xTaskToQuery );\r
-               configASSERT( pxTCB );\r
-               return &( pxTCB->pcTaskName[ 0 ] );\r
+               /* This function is called with the scheduler suspended. */\r
+\r
+               if( listCURRENT_LIST_LENGTH( pxList ) > ( UBaseType_t ) 0 )\r
+               {\r
+                       listGET_OWNER_OF_NEXT_ENTRY( pxFirstTCB, pxList );\r
+\r
+                       do\r
+                       {\r
+                               listGET_OWNER_OF_NEXT_ENTRY( pxNextTCB, pxList );\r
+\r
+                               /* Check each character in the name looking for a match or\r
+                               mismatch. */\r
+                               for( x = ( UBaseType_t ) 0; x < ( UBaseType_t ) configMAX_TASK_NAME_LEN; x++ )\r
+                               {\r
+                                       cNextChar = pxNextTCB->pcTaskName[ x ];\r
+\r
+                                       if( cNextChar != pcNameToQuery[ x ] )\r
+                                       {\r
+                                               /* Characters didn't match. */\r
+                                               break;\r
+                                       }\r
+                                       else if( cNextChar == 0x00 )\r
+                                       {\r
+                                               /* Both strings terminated, a match must have been\r
+                                               found. */\r
+                                               pxReturn = pxNextTCB;\r
+                                               break;\r
+                                       }\r
+                                       else\r
+                                       {\r
+                                               mtCOVERAGE_TEST_MARKER();\r
+                                       }\r
+                               }\r
+\r
+                               if( pxReturn != NULL )\r
+                               {\r
+                                       /* The handle has been found. */\r
+                                       break;\r
+                               }\r
+\r
+                       } while( pxNextTCB != pxFirstTCB );\r
+               }\r
+               else\r
+               {\r
+                       mtCOVERAGE_TEST_MARKER();\r
+               }\r
+\r
+               return pxReturn;\r
+       }\r
+\r
+#endif /* INCLUDE_xTaskGetTaskHandle */\r
+/*-----------------------------------------------------------*/\r
+\r
+#if ( INCLUDE_xTaskGetTaskHandle == 1 )\r
+\r
+       TaskHandle_t xTaskGetTaskHandle( const char *pcNameToQuery ) /*lint !e971 Unqualified char types are allowed for strings and single characters only. */\r
+       {\r
+       UBaseType_t uxQueue = configMAX_PRIORITIES;\r
+       TCB_t* pxTCB;\r
+\r
+               /* Task names will be truncated to configMAX_TASK_NAME_LEN - 1 bytes. */\r
+               configASSERT( strlen( pcNameToQuery ) < configMAX_TASK_NAME_LEN );\r
+\r
+               vTaskSuspendAll();\r
+               {\r
+                       /* Search the ready lists. */\r
+                       do\r
+                       {\r
+                               uxQueue--;\r
+                               pxTCB = prvSearchForNameWithinSingleList( ( List_t * ) &( pxReadyTasksLists[ uxQueue ] ), pcNameToQuery );\r
+\r
+                               if( pxTCB != NULL )\r
+                               {\r
+                                       /* Found the handle. */\r
+                                       break;\r
+                               }\r
+\r
+                       } while( uxQueue > ( UBaseType_t ) tskIDLE_PRIORITY ); /*lint !e961 MISRA exception as the casts are only redundant for some ports. */\r
+\r
+                       /* Search the delayed lists. */\r
+                       if( pxTCB == NULL )\r
+                       {\r
+                               pxTCB = prvSearchForNameWithinSingleList( ( List_t * ) pxDelayedTaskList, pcNameToQuery );\r
+                       }\r
+\r
+                       if( pxTCB == NULL )\r
+                       {\r
+                               pxTCB = prvSearchForNameWithinSingleList( ( List_t * ) pxOverflowDelayedTaskList, pcNameToQuery );\r
+                       }\r
+\r
+                       #if ( INCLUDE_vTaskSuspend == 1 )\r
+                       {\r
+                               if( pxTCB == NULL )\r
+                               {\r
+                                       /* Search the suspended list. */\r
+                                       pxTCB = prvSearchForNameWithinSingleList( &xSuspendedTaskList, pcNameToQuery );\r
+                               }\r
+                       }\r
+                       #endif\r
+\r
+                       #if( INCLUDE_vTaskDelete == 1 )\r
+                       {\r
+                               if( pxTCB == NULL )\r
+                               {\r
+                                       /* Search the deleted list. */\r
+                                       pxTCB = prvSearchForNameWithinSingleList( &xTasksWaitingTermination, pcNameToQuery );\r
+                               }\r
+                       }\r
+                       #endif\r
+               }\r
+               ( void ) xTaskResumeAll();\r
+\r
+               return ( TaskHandle_t ) pxTCB;\r
        }\r
 \r
-#endif /* INCLUDE_pcTaskGetTaskName */\r
+#endif /* INCLUDE_xTaskGetTaskHandle */\r
 /*-----------------------------------------------------------*/\r
 \r
 #if ( configUSE_TRACE_FACILITY == 1 )\r
@@ -1846,20 +2217,20 @@ UBaseType_t uxTaskGetNumberOfTasks( void )
                                do\r
                                {\r
                                        uxQueue--;\r
-                                       uxTask += prvListTaskWithinSingleList( &( pxTaskStatusArray[ uxTask ] ), &( pxReadyTasksLists[ uxQueue ] ), eReady );\r
+                                       uxTask += prvListTasksWithinSingleList( &( pxTaskStatusArray[ uxTask ] ), &( pxReadyTasksLists[ uxQueue ] ), eReady );\r
 \r
                                } while( uxQueue > ( UBaseType_t ) tskIDLE_PRIORITY ); /*lint !e961 MISRA exception as the casts are only redundant for some ports. */\r
 \r
                                /* Fill in an TaskStatus_t structure with information on each\r
                                task in the Blocked state. */\r
-                               uxTask += prvListTaskWithinSingleList( &( pxTaskStatusArray[ uxTask ] ), ( List_t * ) pxDelayedTaskList, eBlocked );\r
-                               uxTask += prvListTaskWithinSingleList( &( pxTaskStatusArray[ uxTask ] ), ( List_t * ) pxOverflowDelayedTaskList, eBlocked );\r
+                               uxTask += prvListTasksWithinSingleList( &( pxTaskStatusArray[ uxTask ] ), ( List_t * ) pxDelayedTaskList, eBlocked );\r
+                               uxTask += prvListTasksWithinSingleList( &( pxTaskStatusArray[ uxTask ] ), ( List_t * ) pxOverflowDelayedTaskList, eBlocked );\r
 \r
                                #if( INCLUDE_vTaskDelete == 1 )\r
                                {\r
                                        /* Fill in an TaskStatus_t structure with information on\r
                                        each task that has been deleted but not yet cleaned up. */\r
-                                       uxTask += prvListTaskWithinSingleList( &( pxTaskStatusArray[ uxTask ] ), &xTasksWaitingTermination, eDeleted );\r
+                                       uxTask += prvListTasksWithinSingleList( &( pxTaskStatusArray[ uxTask ] ), &xTasksWaitingTermination, eDeleted );\r
                                }\r
                                #endif\r
 \r
@@ -1867,7 +2238,7 @@ UBaseType_t uxTaskGetNumberOfTasks( void )
                                {\r
                                        /* Fill in an TaskStatus_t structure with information on\r
                                        each task in the Suspended state. */\r
-                                       uxTask += prvListTaskWithinSingleList( &( pxTaskStatusArray[ uxTask ] ), &xSuspendedTaskList, eSuspended );\r
+                                       uxTask += prvListTasksWithinSingleList( &( pxTaskStatusArray[ uxTask ] ), &xSuspendedTaskList, eSuspended );\r
                                }\r
                                #endif\r
 \r
@@ -1886,54 +2257,128 @@ UBaseType_t uxTaskGetNumberOfTasks( void )
                                {\r
                                        if( pulTotalRunTime != NULL )\r
                                        {\r
-                                               *pulTotalRunTime = 0;\r
+                                               *pulTotalRunTime = 0;\r
+                                       }\r
+                               }\r
+                               #endif\r
+                       }\r
+                       else\r
+                       {\r
+                               mtCOVERAGE_TEST_MARKER();\r
+                       }\r
+               }\r
+               ( void ) xTaskResumeAll();\r
+\r
+               return uxTask;\r
+       }\r
+\r
+#endif /* configUSE_TRACE_FACILITY */\r
+/*----------------------------------------------------------*/\r
+\r
+#if ( INCLUDE_xTaskGetIdleTaskHandle == 1 )\r
+\r
+       TaskHandle_t xTaskGetIdleTaskHandle( void )\r
+       {\r
+               /* If xTaskGetIdleTaskHandle() is called before the scheduler has been\r
+               started, then xIdleTaskHandle will be NULL. */\r
+               configASSERT( ( xIdleTaskHandle != NULL ) );\r
+               return xIdleTaskHandle;\r
+       }\r
+\r
+#endif /* INCLUDE_xTaskGetIdleTaskHandle */\r
+/*----------------------------------------------------------*/\r
+\r
+/* This conditional compilation should use inequality to 0, not equality to 1.\r
+This is to ensure vTaskStepTick() is available when user defined low power mode\r
+implementations require configUSE_TICKLESS_IDLE to be set to a value other than\r
+1. */\r
+#if ( configUSE_TICKLESS_IDLE != 0 )\r
+\r
+       void vTaskStepTick( const TickType_t xTicksToJump )\r
+       {\r
+               /* Correct the tick count value after a period during which the tick\r
+               was suppressed.  Note this does *not* call the tick hook function for\r
+               each stepped tick. */\r
+               configASSERT( ( xTickCount + xTicksToJump ) <= xNextTaskUnblockTime );\r
+               xTickCount += xTicksToJump;\r
+               traceINCREASE_TICK_COUNT( xTicksToJump );\r
+       }\r
+\r
+#endif /* configUSE_TICKLESS_IDLE */\r
+/*----------------------------------------------------------*/\r
+\r
+#if ( INCLUDE_xTaskAbortDelay == 1 )\r
+\r
+       BaseType_t xTaskAbortDelay( TaskHandle_t xTask )\r
+       {\r
+       TCB_t *pxTCB = ( TCB_t * ) xTask;\r
+       BaseType_t xReturn = pdFALSE;\r
+\r
+               configASSERT( pxTCB );\r
+\r
+               vTaskSuspendAll();\r
+               {\r
+                       /* A task can only be prematurely removed from the Blocked state if\r
+                       it is actually in the Blocked state. */\r
+                       if( eTaskGetState( xTask ) == eBlocked )\r
+                       {\r
+                               /* Remove the reference to the task from the blocked list.  An\r
+                               interrupt won't touch the xStateListItem because the\r
+                               scheduler is suspended. */\r
+                               ( void ) uxListRemove( &( pxTCB->xStateListItem ) );\r
+\r
+                               /* Is the task waiting on an event also?  If so remove it from\r
+                               the event list too.  Interrupts can touch the event list item,\r
+                               even though the scheduler is suspended, so a critical section\r
+                               is used. */\r
+                               taskENTER_CRITICAL();\r
+                               {\r
+                                       if( listLIST_ITEM_CONTAINER( &( pxTCB->xEventListItem ) ) != NULL )\r
+                                       {\r
+                                               ( void ) uxListRemove( &( pxTCB->xEventListItem ) );\r
+                                               pxTCB->ucDelayAborted = pdTRUE;\r
+                                       }\r
+                                       else\r
+                                       {\r
+                                               mtCOVERAGE_TEST_MARKER();\r
+                                       }\r
+                               }\r
+                               taskEXIT_CRITICAL();\r
+\r
+                               /* Place the unblocked task into the appropriate ready list. */\r
+                               prvAddTaskToReadyList( pxTCB );\r
+\r
+                               /* A task being unblocked cannot cause an immediate context\r
+                               switch if preemption is turned off. */\r
+                               #if (  configUSE_PREEMPTION == 1 )\r
+                               {\r
+                                       /* Preemption is on, but a context switch should only be\r
+                                       performed if the unblocked task has a priority that is\r
+                                       equal to or higher than the currently executing task. */\r
+                                       if( pxTCB->uxPriority > pxCurrentTCB->uxPriority )\r
+                                       {\r
+                                               /* Pend the yield to be performed when the scheduler\r
+                                               is unsuspended. */\r
+                                               xYieldPending = pdTRUE;\r
+                                       }\r
+                                       else\r
+                                       {\r
+                                               mtCOVERAGE_TEST_MARKER();\r
                                        }\r
                                }\r
-                               #endif\r
+                               #endif /* configUSE_PREEMPTION */\r
                        }\r
                        else\r
                        {\r
                                mtCOVERAGE_TEST_MARKER();\r
                        }\r
                }\r
-               ( void ) xTaskResumeAll();\r
-\r
-               return uxTask;\r
-       }\r
-\r
-#endif /* configUSE_TRACE_FACILITY */\r
-/*----------------------------------------------------------*/\r
-\r
-#if ( INCLUDE_xTaskGetIdleTaskHandle == 1 )\r
-\r
-       TaskHandle_t xTaskGetIdleTaskHandle( void )\r
-       {\r
-               /* If xTaskGetIdleTaskHandle() is called before the scheduler has been\r
-               started, then xIdleTaskHandle will be NULL. */\r
-               configASSERT( ( xIdleTaskHandle != NULL ) );\r
-               return xIdleTaskHandle;\r
-       }\r
-\r
-#endif /* INCLUDE_xTaskGetIdleTaskHandle */\r
-/*----------------------------------------------------------*/\r
-\r
-/* This conditional compilation should use inequality to 0, not equality to 1.\r
-This is to ensure vTaskStepTick() is available when user defined low power mode\r
-implementations require configUSE_TICKLESS_IDLE to be set to a value other than\r
-1. */\r
-#if ( configUSE_TICKLESS_IDLE != 0 )\r
+               xTaskResumeAll();\r
 \r
-       void vTaskStepTick( const TickType_t xTicksToJump )\r
-       {\r
-               /* Correct the tick count value after a period during which the tick\r
-               was suppressed.  Note this does *not* call the tick hook function for\r
-               each stepped tick. */\r
-               configASSERT( ( xTickCount + xTicksToJump ) <= xNextTaskUnblockTime );\r
-               xTickCount += xTicksToJump;\r
-               traceINCREASE_TICK_COUNT( xTicksToJump );\r
+               return xReturn;\r
        }\r
 \r
-#endif /* configUSE_TICKLESS_IDLE */\r
+#endif /* INCLUDE_xTaskAbortDelay */\r
 /*----------------------------------------------------------*/\r
 \r
 BaseType_t xTaskIncrementTick( void )\r
@@ -1948,103 +2393,101 @@ BaseType_t xSwitchRequired = pdFALSE;
        traceTASK_INCREMENT_TICK( xTickCount );\r
        if( uxSchedulerSuspended == ( UBaseType_t ) pdFALSE )\r
        {\r
+               /* Minor optimisation.  The tick count cannot change in this\r
+               block. */\r
+               const TickType_t xConstTickCount = xTickCount + 1;\r
+\r
                /* Increment the RTOS tick, switching the delayed and overflowed\r
                delayed lists if it wraps to 0. */\r
-               ++xTickCount;\r
+               xTickCount = xConstTickCount;\r
 \r
+               if( xConstTickCount == ( TickType_t ) 0U )\r
                {\r
-                       /* Minor optimisation.  The tick count cannot change in this\r
-                       block. */\r
-                       const TickType_t xConstTickCount = xTickCount;\r
-\r
-                       if( xConstTickCount == ( TickType_t ) 0U )\r
-                       {\r
-                               taskSWITCH_DELAYED_LISTS();\r
-                       }\r
-                       else\r
-                       {\r
-                               mtCOVERAGE_TEST_MARKER();\r
-                       }\r
+                       taskSWITCH_DELAYED_LISTS();\r
+               }\r
+               else\r
+               {\r
+                       mtCOVERAGE_TEST_MARKER();\r
+               }\r
 \r
-                       /* See if this tick has made a timeout expire.  Tasks are stored in\r
-                       the     queue in the order of their wake time - meaning once one task\r
-                       has been found whose block time has not expired there is no need to\r
-                       look any further down the list. */\r
-                       if( xConstTickCount >= xNextTaskUnblockTime )\r
+               /* See if this tick has made a timeout expire.  Tasks are stored in\r
+               the     queue in the order of their wake time - meaning once one task\r
+               has been found whose block time has not expired there is no need to\r
+               look any further down the list. */\r
+               if( xConstTickCount >= xNextTaskUnblockTime )\r
+               {\r
+                       for( ;; )\r
                        {\r
-                               for( ;; )\r
+                               if( listLIST_IS_EMPTY( pxDelayedTaskList ) != pdFALSE )\r
+                               {\r
+                                       /* The delayed list is empty.  Set xNextTaskUnblockTime\r
+                                       to the maximum possible value so it is extremely\r
+                                       unlikely that the\r
+                                       if( xTickCount >= xNextTaskUnblockTime ) test will pass\r
+                                       next time through. */\r
+                                       xNextTaskUnblockTime = portMAX_DELAY; /*lint !e961 MISRA exception as the casts are only redundant for some ports. */\r
+                                       break;\r
+                               }\r
+                               else\r
                                {\r
-                                       if( listLIST_IS_EMPTY( pxDelayedTaskList ) != pdFALSE )\r
+                                       /* The delayed list is not empty, get the value of the\r
+                                       item at the head of the delayed list.  This is the time\r
+                                       at which the task at the head of the delayed list must\r
+                                       be removed from the Blocked state. */\r
+                                       pxTCB = ( TCB_t * ) listGET_OWNER_OF_HEAD_ENTRY( pxDelayedTaskList );\r
+                                       xItemValue = listGET_LIST_ITEM_VALUE( &( pxTCB->xStateListItem ) );\r
+\r
+                                       if( xConstTickCount < xItemValue )\r
                                        {\r
-                                               /* The delayed list is empty.  Set xNextTaskUnblockTime\r
-                                               to the maximum possible value so it is extremely\r
-                                               unlikely that the\r
-                                               if( xTickCount >= xNextTaskUnblockTime ) test will pass\r
-                                               next time through. */\r
-                                               xNextTaskUnblockTime = portMAX_DELAY;\r
+                                               /* It is not time to unblock this item yet, but the\r
+                                               item value is the time at which the task at the head\r
+                                               of the blocked list must be removed from the Blocked\r
+                                               state - so record the item value in\r
+                                               xNextTaskUnblockTime. */\r
+                                               xNextTaskUnblockTime = xItemValue;\r
                                                break;\r
                                        }\r
                                        else\r
                                        {\r
-                                               /* The delayed list is not empty, get the value of the\r
-                                               item at the head of the delayed list.  This is the time\r
-                                               at which the task at the head of the delayed list must\r
-                                               be removed from the Blocked state. */\r
-                                               pxTCB = ( TCB_t * ) listGET_OWNER_OF_HEAD_ENTRY( pxDelayedTaskList );\r
-                                               xItemValue = listGET_LIST_ITEM_VALUE( &( pxTCB->xGenericListItem ) );\r
-\r
-                                               if( xConstTickCount < xItemValue )\r
-                                               {\r
-                                                       /* It is not time to unblock this item yet, but the\r
-                                                       item value is the time at which the task at the head\r
-                                                       of the blocked list must be removed from the Blocked\r
-                                                       state - so record the item value in\r
-                                                       xNextTaskUnblockTime. */\r
-                                                       xNextTaskUnblockTime = xItemValue;\r
-                                                       break;\r
-                                               }\r
-                                               else\r
-                                               {\r
-                                                       mtCOVERAGE_TEST_MARKER();\r
-                                               }\r
+                                               mtCOVERAGE_TEST_MARKER();\r
+                                       }\r
+\r
+                                       /* It is time to remove the item from the Blocked state. */\r
+                                       ( void ) uxListRemove( &( pxTCB->xStateListItem ) );\r
+\r
+                                       /* Is the task waiting on an event also?  If so remove\r
+                                       it from the event list. */\r
+                                       if( listLIST_ITEM_CONTAINER( &( pxTCB->xEventListItem ) ) != NULL )\r
+                                       {\r
+                                               ( void ) uxListRemove( &( pxTCB->xEventListItem ) );\r
+                                       }\r
+                                       else\r
+                                       {\r
+                                               mtCOVERAGE_TEST_MARKER();\r
+                                       }\r
 \r
-                                               /* It is time to remove the item from the Blocked state. */\r
-                                               ( void ) uxListRemove( &( pxTCB->xGenericListItem ) );\r
+                                       /* Place the unblocked task into the appropriate ready\r
+                                       list. */\r
+                                       prvAddTaskToReadyList( pxTCB );\r
 \r
-                                               /* Is the task waiting on an event also?  If so remove\r
-                                               it from the event list. */\r
-                                               if( listLIST_ITEM_CONTAINER( &( pxTCB->xEventListItem ) ) != NULL )\r
+                                       /* A task being unblocked cannot cause an immediate\r
+                                       context switch if preemption is turned off. */\r
+                                       #if (  configUSE_PREEMPTION == 1 )\r
+                                       {\r
+                                               /* Preemption is on, but a context switch should\r
+                                               only be performed if the unblocked task has a\r
+                                               priority that is equal to or higher than the\r
+                                               currently executing task. */\r
+                                               if( pxTCB->uxPriority >= pxCurrentTCB->uxPriority )\r
                                                {\r
-                                                       ( void ) uxListRemove( &( pxTCB->xEventListItem ) );\r
+                                                       xSwitchRequired = pdTRUE;\r
                                                }\r
                                                else\r
                                                {\r
                                                        mtCOVERAGE_TEST_MARKER();\r
                                                }\r
-\r
-                                               /* Place the unblocked task into the appropriate ready\r
-                                               list. */\r
-                                               prvAddTaskToReadyList( pxTCB );\r
-\r
-                                               /* A task being unblocked cannot cause an immediate\r
-                                               context switch if preemption is turned off. */\r
-                                               #if (  configUSE_PREEMPTION == 1 )\r
-                                               {\r
-                                                       /* Preemption is on, but a context switch should\r
-                                                       only be performed if the unblocked task has a\r
-                                                       priority that is equal to or higher than the\r
-                                                       currently executing task. */\r
-                                                       if( pxTCB->uxPriority >= pxCurrentTCB->uxPriority )\r
-                                                       {\r
-                                                               xSwitchRequired = pdTRUE;\r
-                                                       }\r
-                                                       else\r
-                                                       {\r
-                                                               mtCOVERAGE_TEST_MARKER();\r
-                                                       }\r
-                                               }\r
-                                               #endif /* configUSE_PREEMPTION */\r
                                        }\r
+                                       #endif /* configUSE_PREEMPTION */\r
                                }\r
                        }\r
                }\r
@@ -2222,9 +2665,9 @@ void vTaskSwitchContext( void )
                                #endif\r
 \r
                                /* Add the amount of time the task has been running to the\r
-                               accumulated     time so far.  The time the task started running was\r
+                               accumulated time so far.  The time the task started running was\r
                                stored in ulTaskSwitchedInTime.  Note that there is no overflow\r
-                               protection here so count values are only valid until the timer\r
+                               protection here so count values are only valid until the timer\r
                                overflows.  The guard against negative values is to protect\r
                                against suspect run time stat counter implementations - which\r
                                are provided by the application, not the kernel. */\r
@@ -2241,8 +2684,7 @@ void vTaskSwitchContext( void )
                #endif /* configGENERATE_RUN_TIME_STATS */\r
 \r
                /* Check for stack overflow, if configured. */\r
-               taskFIRST_CHECK_FOR_STACK_OVERFLOW();\r
-               taskSECOND_CHECK_FOR_STACK_OVERFLOW();\r
+               taskCHECK_FOR_STACK_OVERFLOW();\r
 \r
                /* Select a new task to run using either the generic C or port\r
                optimised asm code. */\r
@@ -2262,8 +2704,6 @@ void vTaskSwitchContext( void )
 \r
 void vTaskPlaceOnEventList( List_t * const pxEventList, const TickType_t xTicksToWait )\r
 {\r
-TickType_t xTimeToWake;\r
-\r
        configASSERT( pxEventList );\r
 \r
        /* THIS FUNCTION MUST BE CALLED WITH EITHER INTERRUPTS DISABLED OR THE\r
@@ -2275,54 +2715,12 @@ TickType_t xTimeToWake;
        list is locked, preventing simultaneous access from interrupts. */\r
        vListInsert( pxEventList, &( pxCurrentTCB->xEventListItem ) );\r
 \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
-               check, and the port reset macro can be called directly. */\r
-               portRESET_READY_PRIORITY( pxCurrentTCB->uxPriority, uxTopReadyPriority );\r
-       }\r
-       else\r
-       {\r
-               mtCOVERAGE_TEST_MARKER();\r
-       }\r
-\r
-       #if ( INCLUDE_vTaskSuspend == 1 )\r
-       {\r
-               if( xTicksToWait == portMAX_DELAY )\r
-               {\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\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
-       }\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, the scheduler\r
-                       will handle it. */\r
-                       xTimeToWake = xTickCount + xTicksToWait;\r
-                       prvAddCurrentTaskToDelayedList( xTimeToWake );\r
-       }\r
-       #endif /* INCLUDE_vTaskSuspend */\r
+       prvAddCurrentTaskToDelayedList( xTicksToWait, pdTRUE );\r
 }\r
 /*-----------------------------------------------------------*/\r
 \r
 void vTaskPlaceOnUnorderedEventList( List_t * pxEventList, const TickType_t xItemValue, const TickType_t xTicksToWait )\r
 {\r
-TickType_t xTimeToWake;\r
-\r
        configASSERT( pxEventList );\r
 \r
        /* THIS FUNCTION MUST BE CALLED WITH THE SCHEDULER SUSPENDED.  It is used by\r
@@ -2341,56 +2739,14 @@ TickType_t xTimeToWake;
        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
-       blocked list.  Exclusive access can be assured to the ready list as the\r
-       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
-               check, and the port reset macro can be called directly. */\r
-               portRESET_READY_PRIORITY( pxCurrentTCB->uxPriority, uxTopReadyPriority );\r
-       }\r
-       else\r
-       {\r
-               mtCOVERAGE_TEST_MARKER();\r
-       }\r
-\r
-       #if ( INCLUDE_vTaskSuspend == 1 )\r
-       {\r
-               if( xTicksToWait == portMAX_DELAY )\r
-               {\r
-                       /* Add the task to the suspended task list instead of a delayed task\r
-                       list to ensure it is not woken by a timing event.  It will block\r
-                       indefinitely. */\r
-                       vListInsertEnd( &xSuspendedTaskList, &( pxCurrentTCB->xGenericListItem ) );\r
-               }\r
-               else\r
-               {\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
-       }\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, the kernel\r
-                       will manage it correctly. */\r
-                       xTimeToWake = xTickCount + xTicksToWait;\r
-                       prvAddCurrentTaskToDelayedList( xTimeToWake );\r
-       }\r
-       #endif /* INCLUDE_vTaskSuspend */\r
+       prvAddCurrentTaskToDelayedList( xTicksToWait, pdTRUE );\r
 }\r
 /*-----------------------------------------------------------*/\r
 \r
-#if configUSE_TIMERS == 1\r
+#if( configUSE_TIMERS == 1 )\r
 \r
-       void vTaskPlaceOnEventListRestricted( List_t * const pxEventList, const TickType_t xTicksToWait, const BaseType_t xWaitIndefinitely )\r
+       void vTaskPlaceOnEventListRestricted( List_t * const pxEventList, TickType_t xTicksToWait, const BaseType_t xWaitIndefinitely )\r
        {\r
-       TickType_t xTimeToWake;\r
-\r
                configASSERT( pxEventList );\r
 \r
                /* This function should not be called by application code hence the\r
@@ -2405,59 +2761,16 @@ TickType_t xTimeToWake;
                can be used in place of vListInsert. */\r
                vListInsertEnd( pxEventList, &( pxCurrentTCB->xEventListItem ) );\r
 \r
-               /* We must remove this task from the ready list before adding it to the\r
-               blocked list as the same list item is used for both lists.  This\r
-               function is called with the scheduler locked so interrupts will not\r
-               access the lists at the same time. */\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
-                       check, and the port reset macro can be called directly. */\r
-                       portRESET_READY_PRIORITY( pxCurrentTCB->uxPriority, uxTopReadyPriority );\r
-               }\r
-               else\r
-               {\r
-                       mtCOVERAGE_TEST_MARKER();\r
-               }\r
-\r
-               /* If vTaskSuspend() is available then the suspended task list is also\r
-               available and a task that is blocking indefinitely can enter the\r
-               suspended state (it is not really suspended as it will re-enter the\r
-               Ready state when the event it is waiting indefinitely for occurs).\r
-               Blocking indefinitely is useful when using tickless idle mode as when\r
-               all tasks are blocked indefinitely all timers can be turned off. */\r
-               #if( INCLUDE_vTaskSuspend == 1 )\r
+               /* If the task should block indefinitely then set the block time to a\r
+               value that will be recognised as an indefinite delay inside the\r
+               prvAddCurrentTaskToDelayedList() function. */\r
+               if( xWaitIndefinitely != pdFALSE )\r
                {\r
-                       if( xWaitIndefinitely == pdTRUE )\r
-                       {\r
-                               /* Add the task to the suspended task list instead of a delayed\r
-                               task list to ensure the task is not woken by a timing event.  It\r
-                               will block indefinitely. */\r
-                               vListInsertEnd( &xSuspendedTaskList, &( pxCurrentTCB->xGenericListItem ) );\r
-                       }\r
-                       else\r
-                       {\r
-                               /* Calculate the time at which the task should be woken if the\r
-                               event does not occur.  This may overflow but this doesn't\r
-                               matter. */\r
-                               xTimeToWake = xTickCount + xTicksToWait;\r
-                               traceTASK_DELAY_UNTIL();\r
-                               prvAddCurrentTaskToDelayedList( xTimeToWake );\r
-                       }\r
+                       xTicksToWait = portMAX_DELAY;\r
                }\r
-               #else\r
-               {\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. */\r
-                       xTimeToWake = xTickCount + xTicksToWait;\r
-                       traceTASK_DELAY_UNTIL();\r
-                       prvAddCurrentTaskToDelayedList( xTimeToWake );\r
 \r
-                       /* Remove compiler warnings when INCLUDE_vTaskSuspend() is not\r
-                       defined. */\r
-                       ( void ) xWaitIndefinitely;\r
-               }\r
-               #endif\r
+               traceTASK_DELAY_UNTIL( ( xTickCount + xTicksToWait ) );\r
+               prvAddCurrentTaskToDelayedList( xTicksToWait, xWaitIndefinitely );\r
        }\r
 \r
 #endif /* configUSE_TIMERS */\r
@@ -2487,7 +2800,7 @@ BaseType_t xReturn;
 \r
        if( uxSchedulerSuspended == ( UBaseType_t ) pdFALSE )\r
        {\r
-               ( void ) uxListRemove( &( pxUnblockedTCB->xGenericListItem ) );\r
+               ( void ) uxListRemove( &( pxUnblockedTCB->xStateListItem ) );\r
                prvAddTaskToReadyList( pxUnblockedTCB );\r
        }\r
        else\r
@@ -2552,7 +2865,7 @@ BaseType_t xReturn;
        /* 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
+       ( void ) uxListRemove( &( pxUnblockedTCB->xStateListItem ) );\r
        prvAddTaskToReadyList( pxUnblockedTCB );\r
 \r
        if( pxUnblockedTCB->uxPriority > pxCurrentTCB->uxPriority )\r
@@ -2596,29 +2909,41 @@ BaseType_t xReturn;
                /* Minor optimisation.  The tick count cannot change in this block. */\r
                const TickType_t xConstTickCount = xTickCount;\r
 \r
+               #if( INCLUDE_xTaskAbortDelay == 1 )\r
+                       if( pxCurrentTCB->ucDelayAborted != pdFALSE )\r
+                       {\r
+                               /* The delay was aborted, which is not the same as a time out,\r
+                               but has the same result. */\r
+                               pxCurrentTCB->ucDelayAborted = pdFALSE;\r
+                               xReturn = pdTRUE;\r
+                       }\r
+                       else\r
+               #endif\r
+\r
                #if ( INCLUDE_vTaskSuspend == 1 )\r
-                       /* If INCLUDE_vTaskSuspend is set to 1 and the block time specified is\r
-                       the maximum block time then the task should block indefinitely, and\r
-                       therefore never time out. */\r
                        if( *pxTicksToWait == portMAX_DELAY )\r
                        {\r
+                               /* If INCLUDE_vTaskSuspend is set to 1 and the block time\r
+                               specified is the maximum block time then the task should block\r
+                               indefinitely, and therefore never time out. */\r
                                xReturn = pdFALSE;\r
                        }\r
-                       else /* We are not blocking indefinitely, perform the checks below. */\r
+                       else\r
                #endif\r
 \r
                if( ( xNumOfOverflows != pxTimeOut->xOverflowCount ) && ( xConstTickCount >= pxTimeOut->xTimeOnEntering ) ) /*lint !e525 Indentation preferred as is to make code within pre-processor directives clearer. */\r
                {\r
-                       /* The tick count is greater than the time at which vTaskSetTimeout()\r
-                       was called, but has also overflowed since vTaskSetTimeOut() was called.\r
-                       It must have wrapped all the way around and gone past us again. This\r
-                       passed since vTaskSetTimeout() was called. */\r
+                       /* The tick count is greater than the time at which\r
+                       vTaskSetTimeout() was called, but has also overflowed since\r
+                       vTaskSetTimeOut() was called.  It must have wrapped all the way\r
+                       around and gone past again. This passed since vTaskSetTimeout()\r
+                       was called. */\r
                        xReturn = pdTRUE;\r
                }\r
-               else if( ( xConstTickCount - pxTimeOut->xTimeOnEntering ) < *pxTicksToWait )\r
+               else if( ( ( TickType_t ) ( xConstTickCount - pxTimeOut->xTimeOnEntering ) ) < *pxTicksToWait ) /*lint !e961 Explicit casting is only redundant with some compilers, whereas others require it to prevent integer conversion errors. */\r
                {\r
                        /* Not a genuine timeout. Adjust parameters for time remaining. */\r
-                       *pxTicksToWait -= ( xConstTickCount -  pxTimeOut->xTimeOnEntering );\r
+                       *pxTicksToWait -= ( xConstTickCount - pxTimeOut->xTimeOnEntering );\r
                        vTaskSetTimeOutState( pxTimeOut );\r
                        xReturn = pdFALSE;\r
                }\r
@@ -2693,9 +3018,13 @@ static portTASK_FUNCTION( prvIdleTask, pvParameters )
        /* Stop warnings. */\r
        ( void ) pvParameters;\r
 \r
+       /** THIS IS THE RTOS IDLE TASK - WHICH IS CREATED AUTOMATICALLY WHEN THE\r
+       SCHEDULER IS STARTED. **/\r
+\r
        for( ;; )\r
        {\r
-               /* See if any tasks have been deleted. */\r
+               /* See if any tasks have deleted themselves - if so then the idle task\r
+               is responsible for freeing the deleted task's TCB and stack. */\r
                prvCheckTasksWaitingTermination();\r
 \r
                #if ( configUSE_PREEMPTION == 0 )\r
@@ -2823,122 +3152,12 @@ static portTASK_FUNCTION( prvIdleTask, pvParameters )
                        {\r
                                mtCOVERAGE_TEST_MARKER();\r
                        }\r
-               }\r
-\r
-               return eReturn;\r
-       }\r
-\r
-#endif /* configUSE_TICKLESS_IDLE */\r
-/*-----------------------------------------------------------*/\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 ) /*lint !e971 Unqualified char types are allowed for strings and single characters only. */\r
-{\r
-UBaseType_t x;\r
-\r
-       /* Store the task name in the TCB. */\r
-       for( x = ( UBaseType_t ) 0; x < ( UBaseType_t ) configMAX_TASK_NAME_LEN; x++ )\r
-       {\r
-               pxTCB->pcTaskName[ x ] = pcName[ x ];\r
-\r
-               /* Don't copy all configMAX_TASK_NAME_LEN if the string is shorter than\r
-               configMAX_TASK_NAME_LEN characters just in case the memory after the\r
-               string is not accessible (extremely unlikely). */\r
-               if( pcName[ x ] == 0x00 )\r
-               {\r
-                       break;\r
-               }\r
-               else\r
-               {\r
-                       mtCOVERAGE_TEST_MARKER();\r
-               }\r
-       }\r
-\r
-       /* Ensure the name string is terminated in the case that the string length\r
-       was greater or equal to configMAX_TASK_NAME_LEN. */\r
-       pxTCB->pcTaskName[ configMAX_TASK_NAME_LEN - 1 ] = '\0';\r
-\r
-       /* This is used as an array index so must ensure it's not too large.  First\r
-       remove the privilege bit if one is present. */\r
-       if( uxPriority >= ( UBaseType_t ) configMAX_PRIORITIES )\r
-       {\r
-               uxPriority = ( UBaseType_t ) configMAX_PRIORITIES - ( UBaseType_t ) 1U;\r
-       }\r
-       else\r
-       {\r
-               mtCOVERAGE_TEST_MARKER();\r
-       }\r
-\r
-       pxTCB->uxPriority = uxPriority;\r
-       #if ( configUSE_MUTEXES == 1 )\r
-       {\r
-               pxTCB->uxBasePriority = uxPriority;\r
-               pxTCB->uxMutexesHeld = 0;\r
-       }\r
-       #endif /* configUSE_MUTEXES */\r
-\r
-       vListInitialiseItem( &( pxTCB->xGenericListItem ) );\r
-       vListInitialiseItem( &( pxTCB->xEventListItem ) );\r
-\r
-       /* Set the pxTCB as a link back from the ListItem_t.  This is so we can get\r
-       back to the containing TCB from a generic item in a list. */\r
-       listSET_LIST_ITEM_OWNER( &( pxTCB->xGenericListItem ), pxTCB );\r
-\r
-       /* Event lists are always in priority order. */\r
-       listSET_LIST_ITEM_VALUE( &( pxTCB->xEventListItem ), ( TickType_t ) configMAX_PRIORITIES - ( TickType_t ) uxPriority ); /*lint !e961 MISRA exception as the casts are only redundant for some ports. */\r
-       listSET_LIST_ITEM_OWNER( &( pxTCB->xEventListItem ), pxTCB );\r
-\r
-       #if ( portCRITICAL_NESTING_IN_TCB == 1 )\r
-       {\r
-               pxTCB->uxCriticalNesting = ( UBaseType_t ) 0U;\r
-       }\r
-       #endif /* portCRITICAL_NESTING_IN_TCB */\r
-\r
-       #if ( configUSE_APPLICATION_TASK_TAG == 1 )\r
-       {\r
-               pxTCB->pxTaskTag = NULL;\r
-       }\r
-       #endif /* configUSE_APPLICATION_TASK_TAG */\r
-\r
-       #if ( configGENERATE_RUN_TIME_STATS == 1 )\r
-       {\r
-               pxTCB->ulRunTimeCounter = 0UL;\r
-       }\r
-       #endif /* configGENERATE_RUN_TIME_STATS */\r
-\r
-       #if ( portUSING_MPU_WRAPPERS == 1 )\r
-       {\r
-               vPortStoreTaskMPUSettings( &( pxTCB->xMPUSettings ), xRegions, pxTCB->pxStack, usStackDepth );\r
-       }\r
-       #else /* portUSING_MPU_WRAPPERS */\r
-       {\r
-               ( void ) xRegions;\r
-               ( void ) usStackDepth;\r
-       }\r
-       #endif /* portUSING_MPU_WRAPPERS */\r
-\r
-       #if( configNUM_THREAD_LOCAL_STORAGE_POINTERS != 0 )\r
-       {\r
-               for( x = 0; x < ( UBaseType_t ) configNUM_THREAD_LOCAL_STORAGE_POINTERS; x++ )\r
-               {\r
-                       pxTCB->pvThreadLocalStoragePointers[ x ] = NULL;\r
-               }\r
-       }\r
-       #endif\r
+               }\r
 \r
-       #if ( configUSE_TASK_NOTIFICATIONS == 1 )\r
-       {\r
-               pxTCB->ulNotifiedValue = 0;\r
-               pxTCB->eNotifyState = eNotWaitingNotification;\r
+               return eReturn;\r
        }\r
-       #endif\r
 \r
-       #if ( configUSE_NEWLIB_REENTRANT == 1 )\r
-       {\r
-               /* Initialise this task's Newlib reent structure. */\r
-               _REENT_INIT_PTR( ( &( pxTCB->xNewLib_reent ) ) );\r
-       }\r
-       #endif /* configUSE_NEWLIB_REENTRANT */\r
-}\r
+#endif /* configUSE_TICKLESS_IDLE */\r
 /*-----------------------------------------------------------*/\r
 \r
 #if ( configNUM_THREAD_LOCAL_STORAGE_POINTERS != 0 )\r
@@ -2990,7 +3209,7 @@ UBaseType_t x;
                the calling task. */\r
                pxTCB = prvGetTCBFromHandle( xTaskToModify );\r
 \r
-        vPortStoreTaskMPUSettings( &( pxTCB->xMPUSettings ), xRegions, NULL, 0 );\r
+               vPortStoreTaskMPUSettings( &( pxTCB->xMPUSettings ), xRegions, NULL, 0 );\r
        }\r
 \r
 #endif /* portUSING_MPU_WRAPPERS */\r
@@ -3030,13 +3249,16 @@ UBaseType_t uxPriority;
 \r
 static void prvCheckTasksWaitingTermination( void )\r
 {\r
+\r
+       /** THIS FUNCTION IS CALLED FROM THE RTOS IDLE TASK **/\r
+\r
        #if ( INCLUDE_vTaskDelete == 1 )\r
        {\r
                BaseType_t xListIsEmpty;\r
 \r
                /* ucTasksDeleted is used to prevent vTaskSuspendAll() being called\r
                too often in the idle task. */\r
-               while( uxTasksDeleted > ( UBaseType_t ) 0U )\r
+               while( uxDeletedTasksWaitingCleanUp > ( UBaseType_t ) 0U )\r
                {\r
                        vTaskSuspendAll();\r
                        {\r
@@ -3051,9 +3273,9 @@ static void prvCheckTasksWaitingTermination( void )
                                taskENTER_CRITICAL();\r
                                {\r
                                        pxTCB = ( TCB_t * ) listGET_OWNER_OF_HEAD_ENTRY( ( &xTasksWaitingTermination ) );\r
-                                       ( void ) uxListRemove( &( pxTCB->xGenericListItem ) );\r
+                                       ( void ) uxListRemove( &( pxTCB->xStateListItem ) );\r
                                        --uxCurrentNumberOfTasks;\r
-                                       --uxTasksDeleted;\r
+                                       --uxDeletedTasksWaitingCleanUp;\r
                                }\r
                                taskEXIT_CRITICAL();\r
 \r
@@ -3065,118 +3287,102 @@ static void prvCheckTasksWaitingTermination( void )
                        }\r
                }\r
        }\r
-       #endif /* vTaskDelete */\r
+       #endif /* INCLUDE_vTaskDelete */\r
 }\r
 /*-----------------------------------------------------------*/\r
 \r
-static void prvAddCurrentTaskToDelayedList( const TickType_t xTimeToWake )\r
-{\r
-       /* The list item will be inserted in wake time order. */\r
-       listSET_LIST_ITEM_VALUE( &( pxCurrentTCB->xGenericListItem ), xTimeToWake );\r
+#if( configUSE_TRACE_FACILITY == 1 )\r
 \r
-       if( xTimeToWake < xTickCount )\r
-       {\r
-               /* Wake time has overflowed.  Place this item in the overflow list. */\r
-               vListInsert( pxOverflowDelayedTaskList, &( pxCurrentTCB->xGenericListItem ) );\r
-       }\r
-       else\r
+       void vTaskGetTaskInfo( TaskHandle_t xTask, TaskStatus_t *pxTaskStatus, BaseType_t xGetFreeStackSpace, eTaskState eState )\r
        {\r
-               /* The wake time has not overflowed, so the current block list is used. */\r
-               vListInsert( pxDelayedTaskList, &( pxCurrentTCB->xGenericListItem ) );\r
-\r
-               /* If the task entering the blocked state was placed at the head of the\r
-               list of blocked tasks then xNextTaskUnblockTime needs to be updated\r
-               too. */\r
-               if( xTimeToWake < xNextTaskUnblockTime )\r
-               {\r
-                       xNextTaskUnblockTime = xTimeToWake;\r
-               }\r
-               else\r
-               {\r
-                       mtCOVERAGE_TEST_MARKER();\r
-               }\r
-       }\r
-}\r
-/*-----------------------------------------------------------*/\r
+       TCB_t *pxTCB;\r
 \r
-static TCB_t *prvAllocateTCBAndStack( const uint16_t usStackDepth, StackType_t * const puxStackBuffer )\r
-{\r
-TCB_t *pxNewTCB;\r
+               /* xTask is NULL then get the state of the calling task. */\r
+               pxTCB = prvGetTCBFromHandle( xTask );\r
 \r
-       /* If the stack grows down then allocate the stack then the TCB so the stack\r
-       does not grow into the TCB.  Likewise if the stack grows up then allocate\r
-       the TCB then the stack. */\r
-       #if( portSTACK_GROWTH > 0 )\r
-       {\r
-               /* Allocate space for the TCB.  Where the memory comes from depends on\r
-               the implementation of the port malloc function. */\r
-               pxNewTCB = ( TCB_t * ) pvPortMalloc( sizeof( TCB_t ) );\r
+               pxTaskStatus->xHandle = ( TaskHandle_t ) pxTCB;\r
+               pxTaskStatus->pcTaskName = ( const char * ) &( pxTCB->pcTaskName [ 0 ] );\r
+               pxTaskStatus->uxCurrentPriority = pxTCB->uxPriority;\r
+               pxTaskStatus->pxStackBase = pxTCB->pxStack;\r
+               pxTaskStatus->xTaskNumber = pxTCB->uxTCBNumber;\r
 \r
-               if( pxNewTCB != NULL )\r
+               #if ( INCLUDE_vTaskSuspend == 1 )\r
                {\r
-                       /* Allocate space for the stack used by the task being created.\r
-                       The base of the stack memory stored in the TCB so the task can\r
-                       be deleted later if required. */\r
-                       pxNewTCB->pxStack = ( StackType_t * ) pvPortMallocAligned( ( ( ( size_t ) usStackDepth ) * sizeof( StackType_t ) ), puxStackBuffer ); /*lint !e961 MISRA exception as the casts are only redundant for some ports. */\r
-\r
-                       if( pxNewTCB->pxStack == NULL )\r
+                       /* If the task is in the suspended list then there is a chance it is\r
+                       actually just blocked indefinitely - so really it should be reported as\r
+                       being in the Blocked state. */\r
+                       if( pxTaskStatus->eCurrentState == eSuspended )\r
                        {\r
-                               /* Could not allocate the stack.  Delete the allocated TCB. */\r
-                               vPortFree( pxNewTCB );\r
-                               pxNewTCB = NULL;\r
+                               vTaskSuspendAll();\r
+                               {\r
+                                       if( listLIST_ITEM_CONTAINER( &( pxTCB->xEventListItem ) ) != NULL )\r
+                                       {\r
+                                               pxTaskStatus->eCurrentState = eBlocked;\r
+                                       }\r
+                               }\r
+                               xTaskResumeAll();\r
                        }\r
                }\r
-       }\r
-       #else /* portSTACK_GROWTH */\r
-       {\r
-       StackType_t *pxStack;\r
+               #endif /* INCLUDE_vTaskSuspend */\r
+\r
+               #if ( configUSE_MUTEXES == 1 )\r
+               {\r
+                       pxTaskStatus->uxBasePriority = pxTCB->uxBasePriority;\r
+               }\r
+               #else\r
+               {\r
+                       pxTaskStatus->uxBasePriority = 0;\r
+               }\r
+               #endif\r
 \r
-               /* Allocate space for the stack used by the task being created. */\r
-               pxStack = ( StackType_t * ) pvPortMallocAligned( ( ( ( size_t ) usStackDepth ) * sizeof( StackType_t ) ), puxStackBuffer ); /*lint !e961 MISRA exception as the casts are only redundant for some ports. */\r
+               #if ( configGENERATE_RUN_TIME_STATS == 1 )\r
+               {\r
+                       pxTaskStatus->ulRunTimeCounter = pxTCB->ulRunTimeCounter;\r
+               }\r
+               #else\r
+               {\r
+                       pxTaskStatus->ulRunTimeCounter = 0;\r
+               }\r
+               #endif\r
 \r
-               if( pxStack != NULL )\r
+               /* Obtaining the task state is a little fiddly, so is only done if the value\r
+               of eState passed into this function is eInvalid - otherwise the state is\r
+               just set to whatever is passed in. */\r
+               if( eState != eInvalid )\r
                {\r
-                       /* Allocate space for the TCB.  Where the memory comes from depends\r
-                       on the implementation of the port malloc function. */\r
-                       pxNewTCB = ( TCB_t * ) pvPortMalloc( sizeof( TCB_t ) );\r
+                       pxTaskStatus->eCurrentState = eState;\r
+               }\r
+               else\r
+               {\r
+                       pxTaskStatus->eCurrentState = eTaskGetState( xTask );\r
+               }\r
 \r
-                       if( pxNewTCB != NULL )\r
+               /* Obtaining the stack space takes some time, so the xGetFreeStackSpace\r
+               parameter is provided to allow it to be skipped. */\r
+               if( xGetFreeStackSpace != pdFALSE )\r
+               {\r
+                       #if ( portSTACK_GROWTH > 0 )\r
                        {\r
-                               /* Store the stack location in the TCB. */\r
-                               pxNewTCB->pxStack = pxStack;\r
+                               pxTaskStatus->usStackHighWaterMark = prvTaskCheckFreeStackSpace( ( uint8_t * ) pxTCB->pxEndOfStack );\r
                        }\r
-                       else\r
+                       #else\r
                        {\r
-                               /* The stack cannot be used as the TCB was not created.  Free it\r
-                               again. */\r
-                               vPortFree( pxStack );\r
+                               pxTaskStatus->usStackHighWaterMark = prvTaskCheckFreeStackSpace( ( uint8_t * ) pxTCB->pxStack );\r
                        }\r
+                       #endif\r
                }\r
                else\r
                {\r
-                       pxNewTCB = NULL;\r
-               }\r
-       }\r
-       #endif /* portSTACK_GROWTH */\r
-\r
-       if( pxNewTCB != NULL )\r
-       {\r
-               /* Avoid dependency on memset() if it is not required. */\r
-               #if( ( configCHECK_FOR_STACK_OVERFLOW > 1 ) || ( configUSE_TRACE_FACILITY == 1 ) || ( INCLUDE_uxTaskGetStackHighWaterMark == 1 ) )\r
-               {\r
-                       /* Just to help debugging. */\r
-                       ( void ) memset( pxNewTCB->pxStack, ( int ) tskSTACK_FILL_BYTE, ( size_t ) usStackDepth * sizeof( StackType_t ) );\r
+                       pxTaskStatus->usStackHighWaterMark = 0;\r
                }\r
-               #endif /* ( ( configCHECK_FOR_STACK_OVERFLOW > 1 ) || ( ( configUSE_TRACE_FACILITY == 1 ) || ( INCLUDE_uxTaskGetStackHighWaterMark == 1 ) ) ) */\r
        }\r
 \r
-       return pxNewTCB;\r
-}\r
+#endif /* configUSE_TRACE_FACILITY */\r
 /*-----------------------------------------------------------*/\r
 \r
 #if ( configUSE_TRACE_FACILITY == 1 )\r
 \r
-       static UBaseType_t prvListTaskWithinSingleList( TaskStatus_t *pxTaskStatusArray, List_t *pxList, eTaskState eState )\r
+       static UBaseType_t prvListTasksWithinSingleList( TaskStatus_t *pxTaskStatusArray, List_t *pxList, eTaskState eState )\r
        {\r
        volatile TCB_t *pxNextTCB, *pxFirstTCB;\r
        UBaseType_t uxTask = 0;\r
@@ -3192,60 +3398,8 @@ TCB_t *pxNewTCB;
                        do\r
                        {\r
                                listGET_OWNER_OF_NEXT_ENTRY( pxNextTCB, pxList );\r
-\r
-                               pxTaskStatusArray[ uxTask ].xHandle = ( TaskHandle_t ) pxNextTCB;\r
-                               pxTaskStatusArray[ uxTask ].pcTaskName = ( const char * ) &( pxNextTCB->pcTaskName [ 0 ] );\r
-                               pxTaskStatusArray[ uxTask ].xTaskNumber = pxNextTCB->uxTCBNumber;\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
-                               }\r
-                               #else\r
-                               {\r
-                                       pxTaskStatusArray[ uxTask ].uxBasePriority = 0;\r
-                               }\r
-                               #endif\r
-\r
-                               #if ( configGENERATE_RUN_TIME_STATS == 1 )\r
-                               {\r
-                                       pxTaskStatusArray[ uxTask ].ulRunTimeCounter = pxNextTCB->ulRunTimeCounter;\r
-                               }\r
-                               #else\r
-                               {\r
-                                       pxTaskStatusArray[ uxTask ].ulRunTimeCounter = 0;\r
-                               }\r
-                               #endif\r
-\r
-                               #if ( portSTACK_GROWTH > 0 )\r
-                               {\r
-                                       pxTaskStatusArray[ uxTask ].usStackHighWaterMark = prvTaskCheckFreeStackSpace( ( uint8_t * ) pxNextTCB->pxEndOfStack );\r
-                               }\r
-                               #else\r
-                               {\r
-                                       pxTaskStatusArray[ uxTask ].usStackHighWaterMark = prvTaskCheckFreeStackSpace( ( uint8_t * ) pxNextTCB->pxStack );\r
-                               }\r
-                               #endif\r
-\r
+                               vTaskGetTaskInfo( ( TaskHandle_t ) pxNextTCB, &( pxTaskStatusArray[ uxTask ] ), pdTRUE, eState );\r
                                uxTask++;\r
-\r
                        } while( pxNextTCB != pxFirstTCB );\r
                }\r
                else\r
@@ -3324,22 +3478,28 @@ TCB_t *pxNewTCB;
                }\r
                #endif /* configUSE_NEWLIB_REENTRANT */\r
 \r
-               #if( portUSING_MPU_WRAPPERS == 1 )\r
+               #if( ( configSUPPORT_DYNAMIC_ALLOCATION == 1 ) && ( configSUPPORT_STATIC_ALLOCATION == 0 ) )\r
                {\r
-                       /* Only free the stack if it was allocated dynamically in the first\r
-                       place. */\r
-                       if( pxTCB->xUsingStaticallyAllocatedStack == pdFALSE )\r
-                       {\r
-                               vPortFreeAligned( pxTCB->pxStack );\r
-                       }\r
+                       /* The task can only have been allocated dynamically - free it\r
+                       again. */\r
+                       vPortFree( pxTCB->pxStack );\r
+                       vPortFree( pxTCB );\r
                }\r
-               #else\r
+               #elif( ( configSUPPORT_DYNAMIC_ALLOCATION == 1 ) && ( configSUPPORT_STATIC_ALLOCATION == 1 ) )\r
                {\r
-                       vPortFreeAligned( pxTCB->pxStack );\r
+                       /* The task could have been allocated statically or dynamically, so\r
+                       check before attempting to free the memory. */\r
+                       if( pxTCB->ucStaticallyAllocated == ( uint8_t ) pdFALSE )\r
+                       {\r
+                               vPortFree( pxTCB->pxStack );\r
+                               vPortFree( pxTCB );\r
+                       }\r
+                       else\r
+                       {\r
+                               mtCOVERAGE_TEST_MARKER();\r
+                       }\r
                }\r
-               #endif\r
-\r
-               vPortFree( pxTCB );\r
+               #endif /* configSUPPORT_DYNAMIC_ALLOCATION */\r
        }\r
 \r
 #endif /* INCLUDE_vTaskDelete */\r
@@ -3364,7 +3524,7 @@ TCB_t *pxTCB;
                which the task at the head of the delayed list should be removed\r
                from the Blocked state. */\r
                ( pxTCB ) = ( TCB_t * ) listGET_OWNER_OF_HEAD_ENTRY( pxDelayedTaskList );\r
-               xNextTaskUnblockTime = listGET_LIST_ITEM_VALUE( &( ( pxTCB )->xGenericListItem ) );\r
+               xNextTaskUnblockTime = listGET_LIST_ITEM_VALUE( &( ( pxTCB )->xStateListItem ) );\r
        }\r
 }\r
 /*-----------------------------------------------------------*/\r
@@ -3443,9 +3603,9 @@ TCB_t *pxTCB;
 \r
                                /* If the task being modified is in the ready state it will need\r
                                to be moved into a new list. */\r
-                               if( listIS_CONTAINED_WITHIN( &( pxReadyTasksLists[ pxTCB->uxPriority ] ), &( pxTCB->xGenericListItem ) ) != pdFALSE )\r
+                               if( listIS_CONTAINED_WITHIN( &( pxReadyTasksLists[ pxTCB->uxPriority ] ), &( pxTCB->xStateListItem ) ) != pdFALSE )\r
                                {\r
-                                       if( uxListRemove( &( pxTCB->xGenericListItem ) ) == ( UBaseType_t ) 0 )\r
+                                       if( uxListRemove( &( pxTCB->xStateListItem ) ) == ( UBaseType_t ) 0 )\r
                                        {\r
                                                taskRESET_READY_PRIORITY( pxTCB->uxPriority );\r
                                        }\r
@@ -3489,6 +3649,12 @@ TCB_t *pxTCB;
 \r
                if( pxMutexHolder != NULL )\r
                {\r
+                       /* A task can only have an inherited priority if it holds the mutex.\r
+                       If the mutex is held by a task then it cannot be given from an\r
+                       interrupt, and if a mutex is given by the holding task then it must\r
+                       be the running state task. */\r
+                       configASSERT( pxTCB == pxCurrentTCB );\r
+\r
                        configASSERT( pxTCB->uxMutexesHeld );\r
                        ( pxTCB->uxMutexesHeld )--;\r
 \r
@@ -3504,7 +3670,7 @@ TCB_t *pxTCB;
                                        given from an interrupt, and if a mutex is given by the\r
                                        holding task then it must be the running state task.  Remove\r
                                        the     holding task from the ready     list. */\r
-                                       if( uxListRemove( &( pxTCB->xGenericListItem ) ) == ( UBaseType_t ) 0 )\r
+                                       if( uxListRemove( &( pxTCB->xStateListItem ) ) == ( UBaseType_t ) 0 )\r
                                        {\r
                                                taskRESET_READY_PRIORITY( pxTCB->uxPriority );\r
                                        }\r
@@ -3575,7 +3741,6 @@ TCB_t *pxTCB;
                        {\r
                                portASSERT_IF_IN_ISR();\r
                        }\r
-\r
                }\r
                else\r
                {\r
@@ -3623,14 +3788,14 @@ TCB_t *pxTCB;
 \r
        static char *prvWriteNameToBuffer( char *pcBuffer, const char *pcTaskName )\r
        {\r
-       BaseType_t x;\r
+       size_t x;\r
 \r
                /* Start by copying the entire string. */\r
                strcpy( pcBuffer, pcTaskName );\r
 \r
                /* Pad the end of the string with spaces to ensure columns line up when\r
                printed out. */\r
-               for( x = strlen( pcBuffer ); x < ( configMAX_TASK_NAME_LEN - 1 ); x++ )\r
+               for( x = strlen( pcBuffer ); x < ( size_t ) ( configMAX_TASK_NAME_LEN - 1 ); x++ )\r
                {\r
                        pcBuffer[ x ] = ' ';\r
                }\r
@@ -3685,7 +3850,9 @@ TCB_t *pxTCB;
                function is executing. */\r
                uxArraySize = uxCurrentNumberOfTasks;\r
 \r
-               /* Allocate an array index for each task. */\r
+               /* Allocate an array index for each task.  NOTE!  if\r
+               configSUPPORT_DYNAMIC_ALLOCATION is set to 0 then pvPortMalloc() will\r
+               equate to NULL. */\r
                pxTaskStatusArray = pvPortMalloc( uxCurrentNumberOfTasks * sizeof( TaskStatus_t ) );\r
 \r
                if( pxTaskStatusArray != NULL )\r
@@ -3725,7 +3892,8 @@ TCB_t *pxTCB;
                                pcWriteBuffer += strlen( pcWriteBuffer );\r
                        }\r
 \r
-                       /* Free the array again. */\r
+                       /* Free the array again.  NOTE!  If configSUPPORT_DYNAMIC_ALLOCATION\r
+                       is 0 then vPortFree() will be #defined to nothing. */\r
                        vPortFree( pxTaskStatusArray );\r
                }\r
                else\r
@@ -3783,7 +3951,9 @@ TCB_t *pxTCB;
                function is executing. */\r
                uxArraySize = uxCurrentNumberOfTasks;\r
 \r
-               /* Allocate an array index for each task. */\r
+               /* Allocate an array index for each task.  NOTE!  If\r
+               configSUPPORT_DYNAMIC_ALLOCATION is set to 0 then pvPortMalloc() will\r
+               equate to NULL. */\r
                pxTaskStatusArray = pvPortMalloc( uxCurrentNumberOfTasks * sizeof( TaskStatus_t ) );\r
 \r
                if( pxTaskStatusArray != NULL )\r
@@ -3849,7 +4019,8 @@ TCB_t *pxTCB;
                                mtCOVERAGE_TEST_MARKER();\r
                        }\r
 \r
-                       /* Free the array again. */\r
+                       /* Free the array again.  NOTE!  If configSUPPORT_DYNAMIC_ALLOCATION\r
+                       is 0 then vPortFree() will be #defined to nothing. */\r
                        vPortFree( pxTaskStatusArray );\r
                }\r
                else\r
@@ -3896,7 +4067,6 @@ TickType_t uxReturn;
 \r
        uint32_t ulTaskNotifyTake( BaseType_t xClearCountOnExit, TickType_t xTicksToWait )\r
        {\r
-       TickType_t xTimeToWake;\r
        uint32_t ulReturn;\r
 \r
                taskENTER_CRITICAL();\r
@@ -3905,54 +4075,12 @@ TickType_t uxReturn;
                        if( pxCurrentTCB->ulNotifiedValue == 0UL )\r
                        {\r
                                /* Mark this task as waiting for a notification. */\r
-                               pxCurrentTCB->eNotifyState = eWaitingNotification;\r
+                               pxCurrentTCB->ucNotifyState = taskWAITING_NOTIFICATION;\r
 \r
                                if( xTicksToWait > ( TickType_t ) 0 )\r
                                {\r
-                                       /* The task is going to block.  First it must be removed\r
-                                       from the ready list. */\r
-                                       if( uxListRemove( &( pxCurrentTCB->xGenericListItem ) ) == ( UBaseType_t ) 0 )\r
-                                       {\r
-                                               /* The current task must be in a ready list, so there is\r
-                                               no need to check, and the port reset macro can be called\r
-                                               directly. */\r
-                                               portRESET_READY_PRIORITY( pxCurrentTCB->uxPriority, uxTopReadyPriority );\r
-                                       }\r
-                                       else\r
-                                       {\r
-                                               mtCOVERAGE_TEST_MARKER();\r
-                                       }\r
-\r
-                                       #if ( INCLUDE_vTaskSuspend == 1 )\r
-                                       {\r
-                                               if( xTicksToWait == portMAX_DELAY )\r
-                                               {\r
-                                                       /* Add the task to the suspended task list instead\r
-                                                       of a delayed task list to ensure the task is not\r
-                                                       woken by a timing event.  It will block\r
-                                                       indefinitely. */\r
-                                                       vListInsertEnd( &xSuspendedTaskList, &( pxCurrentTCB->xGenericListItem ) );\r
-                                               }\r
-                                               else\r
-                                               {\r
-                                                       /* Calculate the time at which the task should be\r
-                                                       woken if no notification events occur.  This may\r
-                                                       overflow but this doesn't matter, the scheduler will\r
-                                                       handle it. */\r
-                                                       xTimeToWake = xTickCount + xTicksToWait;\r
-                                                       prvAddCurrentTaskToDelayedList( xTimeToWake );\r
-                                               }\r
-                                       }\r
-                                       #else /* INCLUDE_vTaskSuspend */\r
-                                       {\r
-                                                       /* Calculate the time at which the task should be\r
-                                                       woken if the event does not occur.  This may\r
-                                                       overflow but this doesn't matter, the scheduler will\r
-                                                       handle it. */\r
-                                                       xTimeToWake = xTickCount + xTicksToWait;\r
-                                                       prvAddCurrentTaskToDelayedList( xTimeToWake );\r
-                                       }\r
-                                       #endif /* INCLUDE_vTaskSuspend */\r
+                                       prvAddCurrentTaskToDelayedList( xTicksToWait, pdTRUE );\r
+                                       traceTASK_NOTIFY_TAKE_BLOCK();\r
 \r
                                        /* All ports are written to allow a yield in a critical\r
                                        section (some will yield immediately, others wait until the\r
@@ -3974,6 +4102,7 @@ TickType_t uxReturn;
 \r
                taskENTER_CRITICAL();\r
                {\r
+                       traceTASK_NOTIFY_TAKE();\r
                        ulReturn = pxCurrentTCB->ulNotifiedValue;\r
 \r
                        if( ulReturn != 0UL )\r
@@ -3984,7 +4113,7 @@ TickType_t uxReturn;
                                }\r
                                else\r
                                {\r
-                                       ( pxCurrentTCB->ulNotifiedValue )--;\r
+                                       pxCurrentTCB->ulNotifiedValue = ulReturn - 1;\r
                                }\r
                        }\r
                        else\r
@@ -3992,7 +4121,7 @@ TickType_t uxReturn;
                                mtCOVERAGE_TEST_MARKER();\r
                        }\r
 \r
-                       pxCurrentTCB->eNotifyState = eNotWaitingNotification;\r
+                       pxCurrentTCB->ucNotifyState = taskNOT_WAITING_NOTIFICATION;\r
                }\r
                taskEXIT_CRITICAL();\r
 \r
@@ -4006,13 +4135,12 @@ TickType_t uxReturn;
 \r
        BaseType_t xTaskNotifyWait( uint32_t ulBitsToClearOnEntry, uint32_t ulBitsToClearOnExit, uint32_t *pulNotificationValue, TickType_t xTicksToWait )\r
        {\r
-       TickType_t xTimeToWake;\r
        BaseType_t xReturn;\r
 \r
                taskENTER_CRITICAL();\r
                {\r
                        /* Only block if a notification is not already pending. */\r
-                       if( pxCurrentTCB->eNotifyState != eNotified )\r
+                       if( pxCurrentTCB->ucNotifyState != taskNOTIFICATION_RECEIVED )\r
                        {\r
                                /* Clear bits in the task's notification value as bits may get\r
                                set     by the notifying task or interrupt.  This can be used to\r
@@ -4020,54 +4148,12 @@ TickType_t uxReturn;
                                pxCurrentTCB->ulNotifiedValue &= ~ulBitsToClearOnEntry;\r
 \r
                                /* Mark this task as waiting for a notification. */\r
-                               pxCurrentTCB->eNotifyState = eWaitingNotification;\r
+                               pxCurrentTCB->ucNotifyState = taskWAITING_NOTIFICATION;\r
 \r
                                if( xTicksToWait > ( TickType_t ) 0 )\r
                                {\r
-                                       /* The task is going to block.  First it must be removed\r
-                                       from the        ready list. */\r
-                                       if( uxListRemove( &( pxCurrentTCB->xGenericListItem ) ) == ( UBaseType_t ) 0 )\r
-                                       {\r
-                                               /* The current task must be in a ready list, so there is\r
-                                               no need to check, and the port reset macro can be called\r
-                                               directly. */\r
-                                               portRESET_READY_PRIORITY( pxCurrentTCB->uxPriority, uxTopReadyPriority );\r
-                                       }\r
-                                       else\r
-                                       {\r
-                                               mtCOVERAGE_TEST_MARKER();\r
-                                       }\r
-\r
-                                       #if ( INCLUDE_vTaskSuspend == 1 )\r
-                                       {\r
-                                               if( xTicksToWait == portMAX_DELAY )\r
-                                               {\r
-                                                       /* Add the task to the suspended task list instead\r
-                                                       of a delayed task list to ensure the task is not\r
-                                                       woken by a timing event.  It will block\r
-                                                       indefinitely. */\r
-                                                       vListInsertEnd( &xSuspendedTaskList, &( pxCurrentTCB->xGenericListItem ) );\r
-                                               }\r
-                                               else\r
-                                               {\r
-                                                       /* Calculate the time at which the task should be\r
-                                                       woken if no notification events occur.  This may\r
-                                                       overflow but this doesn't matter, the scheduler will\r
-                                                       handle it. */\r
-                                                       xTimeToWake = xTickCount + xTicksToWait;\r
-                                                       prvAddCurrentTaskToDelayedList( xTimeToWake );\r
-                                               }\r
-                                       }\r
-                                       #else /* INCLUDE_vTaskSuspend */\r
-                                       {\r
-                                                       /* Calculate the time at which the task should be\r
-                                                       woken if the event does not occur.  This may\r
-                                                       overflow but this doesn't matter, the scheduler will\r
-                                                       handle it. */\r
-                                                       xTimeToWake = xTickCount + xTicksToWait;\r
-                                                       prvAddCurrentTaskToDelayedList( xTimeToWake );\r
-                                       }\r
-                                       #endif /* INCLUDE_vTaskSuspend */\r
+                                       prvAddCurrentTaskToDelayedList( xTicksToWait, pdTRUE );\r
+                                       traceTASK_NOTIFY_WAIT_BLOCK();\r
 \r
                                        /* All ports are written to allow a yield in a critical\r
                                        section (some will yield immediately, others wait until the\r
@@ -4089,6 +4175,8 @@ TickType_t uxReturn;
 \r
                taskENTER_CRITICAL();\r
                {\r
+                       traceTASK_NOTIFY_WAIT();\r
+\r
                        if( pulNotificationValue != NULL )\r
                        {\r
                                /* Output the current notification value, which may or may not\r
@@ -4096,11 +4184,11 @@ TickType_t uxReturn;
                                *pulNotificationValue = pxCurrentTCB->ulNotifiedValue;\r
                        }\r
 \r
-                       /* If eNotifyValue is set then either the task never entered the\r
+                       /* If ucNotifyValue is set then either the task never entered the\r
                        blocked state (because a notification was already pending) or the\r
                        task unblocked because of a notification.  Otherwise the task\r
                        unblocked because of a timeout. */\r
-                       if( pxCurrentTCB->eNotifyState == eWaitingNotification )\r
+                       if( pxCurrentTCB->ucNotifyState == taskWAITING_NOTIFICATION )\r
                        {\r
                                /* A notification was not received. */\r
                                xReturn = pdFALSE;\r
@@ -4113,7 +4201,7 @@ TickType_t uxReturn;
                                xReturn = pdTRUE;\r
                        }\r
 \r
-                       pxCurrentTCB->eNotifyState = eNotWaitingNotification;\r
+                       pxCurrentTCB->ucNotifyState = taskNOT_WAITING_NOTIFICATION;\r
                }\r
                taskEXIT_CRITICAL();\r
 \r
@@ -4128,8 +4216,8 @@ TickType_t uxReturn;
        BaseType_t xTaskGenericNotify( TaskHandle_t xTaskToNotify, uint32_t ulValue, eNotifyAction eAction, uint32_t *pulPreviousNotificationValue )\r
        {\r
        TCB_t * pxTCB;\r
-       eNotifyValue eOriginalNotifyState;\r
        BaseType_t xReturn = pdPASS;\r
+       uint8_t ucOriginalNotifyState;\r
 \r
                configASSERT( xTaskToNotify );\r
                pxTCB = ( TCB_t * ) xTaskToNotify;\r
@@ -4141,9 +4229,9 @@ TickType_t uxReturn;
                                *pulPreviousNotificationValue = pxTCB->ulNotifiedValue;\r
                        }\r
 \r
-                       eOriginalNotifyState = pxTCB->eNotifyState;\r
+                       ucOriginalNotifyState = pxTCB->ucNotifyState;\r
 \r
-                       pxTCB->eNotifyState = eNotified;\r
+                       pxTCB->ucNotifyState = taskNOTIFICATION_RECEIVED;\r
 \r
                        switch( eAction )\r
                        {\r
@@ -4160,7 +4248,7 @@ TickType_t uxReturn;
                                        break;\r
 \r
                                case eSetValueWithoutOverwrite :\r
-                                       if( eOriginalNotifyState != eNotified )\r
+                                       if( ucOriginalNotifyState != taskNOTIFICATION_RECEIVED )\r
                                        {\r
                                                pxTCB->ulNotifiedValue = ulValue;\r
                                        }\r
@@ -4177,12 +4265,13 @@ TickType_t uxReturn;
                                        break;\r
                        }\r
 \r
+                       traceTASK_NOTIFY();\r
 \r
                        /* If the task is in the blocked state specifically to wait for a\r
                        notification then unblock it now. */\r
-                       if( eOriginalNotifyState == eWaitingNotification )\r
+                       if( ucOriginalNotifyState == taskWAITING_NOTIFICATION )\r
                        {\r
-                               ( void ) uxListRemove( &( pxTCB->xGenericListItem ) );\r
+                               ( void ) uxListRemove( &( pxTCB->xStateListItem ) );\r
                                prvAddTaskToReadyList( pxTCB );\r
 \r
                                /* The task should not have been on an event list. */\r
@@ -4233,7 +4322,7 @@ TickType_t uxReturn;
        BaseType_t xTaskGenericNotifyFromISR( TaskHandle_t xTaskToNotify, uint32_t ulValue, eNotifyAction eAction, uint32_t *pulPreviousNotificationValue, BaseType_t *pxHigherPriorityTaskWoken )\r
        {\r
        TCB_t * pxTCB;\r
-       eNotifyValue eOriginalNotifyState;\r
+       uint8_t ucOriginalNotifyState;\r
        BaseType_t xReturn = pdPASS;\r
        UBaseType_t uxSavedInterruptStatus;\r
 \r
@@ -4266,8 +4355,8 @@ TickType_t uxReturn;
                                *pulPreviousNotificationValue = pxTCB->ulNotifiedValue;\r
                        }\r
 \r
-                       eOriginalNotifyState = pxTCB->eNotifyState;\r
-                       pxTCB->eNotifyState = eNotified;\r
+                       ucOriginalNotifyState = pxTCB->ucNotifyState;\r
+                       pxTCB->ucNotifyState = taskNOTIFICATION_RECEIVED;\r
 \r
                        switch( eAction )\r
                        {\r
@@ -4284,7 +4373,7 @@ TickType_t uxReturn;
                                        break;\r
 \r
                                case eSetValueWithoutOverwrite :\r
-                                       if( eOriginalNotifyState != eNotified )\r
+                                       if( ucOriginalNotifyState != taskNOTIFICATION_RECEIVED )\r
                                        {\r
                                                pxTCB->ulNotifiedValue = ulValue;\r
                                        }\r
@@ -4301,17 +4390,18 @@ TickType_t uxReturn;
                                        break;\r
                        }\r
 \r
+                       traceTASK_NOTIFY_FROM_ISR();\r
 \r
                        /* If the task is in the blocked state specifically to wait for a\r
                        notification then unblock it now. */\r
-                       if( eOriginalNotifyState == eWaitingNotification )\r
+                       if( ucOriginalNotifyState == taskWAITING_NOTIFICATION )\r
                        {\r
                                /* The task should not have been on an event list. */\r
                                configASSERT( listLIST_ITEM_CONTAINER( &( pxTCB->xEventListItem ) ) == NULL );\r
 \r
                                if( uxSchedulerSuspended == ( UBaseType_t ) pdFALSE )\r
                                {\r
-                                       ( void ) uxListRemove( &( pxTCB->xGenericListItem ) );\r
+                                       ( void ) uxListRemove( &( pxTCB->xStateListItem ) );\r
                                        prvAddTaskToReadyList( pxTCB );\r
                                }\r
                                else\r
@@ -4329,6 +4419,13 @@ TickType_t uxReturn;
                                        {\r
                                                *pxHigherPriorityTaskWoken = pdTRUE;\r
                                        }\r
+                                       else\r
+                                       {\r
+                                               /* Mark that a yield is pending in case the user is not\r
+                                               using the "xHigherPriorityTaskWoken" parameter to an ISR\r
+                                               safe FreeRTOS function. */\r
+                                               xYieldPending = pdTRUE;\r
+                                       }\r
                                }\r
                                else\r
                                {\r
@@ -4349,7 +4446,7 @@ TickType_t uxReturn;
        void vTaskNotifyGiveFromISR( TaskHandle_t xTaskToNotify, BaseType_t *pxHigherPriorityTaskWoken )\r
        {\r
        TCB_t * pxTCB;\r
-       eNotifyValue eOriginalNotifyState;\r
+       uint8_t ucOriginalNotifyState;\r
        UBaseType_t uxSavedInterruptStatus;\r
 \r
                configASSERT( xTaskToNotify );\r
@@ -4376,23 +4473,25 @@ TickType_t uxReturn;
 \r
                uxSavedInterruptStatus = portSET_INTERRUPT_MASK_FROM_ISR();\r
                {\r
-                       eOriginalNotifyState = pxTCB->eNotifyState;\r
-                       pxTCB->eNotifyState = eNotified;\r
+                       ucOriginalNotifyState = pxTCB->ucNotifyState;\r
+                       pxTCB->ucNotifyState = taskNOTIFICATION_RECEIVED;\r
 \r
                        /* 'Giving' is equivalent to incrementing a count in a counting\r
                        semaphore. */\r
                        ( pxTCB->ulNotifiedValue )++;\r
 \r
+                       traceTASK_NOTIFY_GIVE_FROM_ISR();\r
+\r
                        /* If the task is in the blocked state specifically to wait for a\r
                        notification then unblock it now. */\r
-                       if( eOriginalNotifyState == eWaitingNotification )\r
+                       if( ucOriginalNotifyState == taskWAITING_NOTIFICATION )\r
                        {\r
                                /* The task should not have been on an event list. */\r
                                configASSERT( listLIST_ITEM_CONTAINER( &( pxTCB->xEventListItem ) ) == NULL );\r
 \r
                                if( uxSchedulerSuspended == ( UBaseType_t ) pdFALSE )\r
                                {\r
-                                       ( void ) uxListRemove( &( pxTCB->xGenericListItem ) );\r
+                                       ( void ) uxListRemove( &( pxTCB->xStateListItem ) );\r
                                        prvAddTaskToReadyList( pxTCB );\r
                                }\r
                                else\r
@@ -4410,6 +4509,13 @@ TickType_t uxReturn;
                                        {\r
                                                *pxHigherPriorityTaskWoken = pdTRUE;\r
                                        }\r
+                                       else\r
+                                       {\r
+                                               /* Mark that a yield is pending in case the user is not\r
+                                               using the "xHigherPriorityTaskWoken" parameter in an ISR\r
+                                               safe FreeRTOS function. */\r
+                                               xYieldPending = pdTRUE;\r
+                                       }\r
                                }\r
                                else\r
                                {\r
@@ -4424,6 +4530,149 @@ TickType_t uxReturn;
 \r
 /*-----------------------------------------------------------*/\r
 \r
+#if( configUSE_TASK_NOTIFICATIONS == 1 )\r
+\r
+       BaseType_t xTaskNotifyStateClear( TaskHandle_t xTask )\r
+       {\r
+       TCB_t *pxTCB;\r
+       BaseType_t xReturn;\r
+\r
+               /* If null is passed in here then it is the calling task that is having\r
+               its notification state cleared. */\r
+               pxTCB = prvGetTCBFromHandle( xTask );\r
+\r
+               taskENTER_CRITICAL();\r
+               {\r
+                       if( pxTCB->ucNotifyState == taskNOTIFICATION_RECEIVED )\r
+                       {\r
+                               pxTCB->ucNotifyState = taskNOT_WAITING_NOTIFICATION;\r
+                               xReturn = pdPASS;\r
+                       }\r
+                       else\r
+                       {\r
+                               xReturn = pdFAIL;\r
+                       }\r
+               }\r
+               taskEXIT_CRITICAL();\r
+\r
+               return xReturn;\r
+       }\r
+\r
+#endif /* configUSE_TASK_NOTIFICATIONS */\r
+/*-----------------------------------------------------------*/\r
+\r
+\r
+static void prvAddCurrentTaskToDelayedList( TickType_t xTicksToWait, const BaseType_t xCanBlockIndefinitely )\r
+{\r
+TickType_t xTimeToWake;\r
+const TickType_t xConstTickCount = xTickCount;\r
+\r
+       #if( INCLUDE_xTaskAbortDelay == 1 )\r
+       {\r
+               /* About to enter a delayed list, so ensure the ucDelayAborted flag is\r
+               reset to pdFALSE so it can be detected as having been set to pdTRUE\r
+               when the task leaves the Blocked state. */\r
+               pxCurrentTCB->ucDelayAborted = pdFALSE;\r
+       }\r
+       #endif\r
+\r
+       /* Remove the task from the ready list before adding it to the blocked list\r
+       as the same list item is used for both lists. */\r
+       if( uxListRemove( &( pxCurrentTCB->xStateListItem ) ) == ( UBaseType_t ) 0 )\r
+       {\r
+               /* The current task must be in a ready list, so there is no need to\r
+               check, and the port reset macro can be called directly. */\r
+               portRESET_READY_PRIORITY( pxCurrentTCB->uxPriority, uxTopReadyPriority );\r
+       }\r
+       else\r
+       {\r
+               mtCOVERAGE_TEST_MARKER();\r
+       }\r
+\r
+       #if ( INCLUDE_vTaskSuspend == 1 )\r
+       {\r
+               if( ( xTicksToWait == portMAX_DELAY ) && ( xCanBlockIndefinitely != pdFALSE ) )\r
+               {\r
+                       /* Add the task to the suspended task list instead of a delayed task\r
+                       list to ensure it is not woken by a timing event.  It will block\r
+                       indefinitely. */\r
+                       vListInsertEnd( &xSuspendedTaskList, &( pxCurrentTCB->xStateListItem ) );\r
+               }\r
+               else\r
+               {\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 = xConstTickCount + xTicksToWait;\r
+\r
+                       /* The list item will be inserted in wake time order. */\r
+                       listSET_LIST_ITEM_VALUE( &( pxCurrentTCB->xStateListItem ), xTimeToWake );\r
+\r
+                       if( xTimeToWake < xConstTickCount )\r
+                       {\r
+                               /* Wake time has overflowed.  Place this item in the overflow\r
+                               list. */\r
+                               vListInsert( pxOverflowDelayedTaskList, &( pxCurrentTCB->xStateListItem ) );\r
+                       }\r
+                       else\r
+                       {\r
+                               /* The wake time has not overflowed, so the current block list\r
+                               is used. */\r
+                               vListInsert( pxDelayedTaskList, &( pxCurrentTCB->xStateListItem ) );\r
+\r
+                               /* If the task entering the blocked state was placed at the\r
+                               head of the list of blocked tasks then xNextTaskUnblockTime\r
+                               needs to be updated too. */\r
+                               if( xTimeToWake < xNextTaskUnblockTime )\r
+                               {\r
+                                       xNextTaskUnblockTime = xTimeToWake;\r
+                               }\r
+                               else\r
+                               {\r
+                                       mtCOVERAGE_TEST_MARKER();\r
+                               }\r
+                       }\r
+               }\r
+       }\r
+       #else /* INCLUDE_vTaskSuspend */\r
+       {\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 kernel\r
+               will manage it correctly. */\r
+               xTimeToWake = xConstTickCount + xTicksToWait;\r
+\r
+               /* The list item will be inserted in wake time order. */\r
+               listSET_LIST_ITEM_VALUE( &( pxCurrentTCB->xStateListItem ), xTimeToWake );\r
+\r
+               if( xTimeToWake < xConstTickCount )\r
+               {\r
+                       /* Wake time has overflowed.  Place this item in the overflow list. */\r
+                       vListInsert( pxOverflowDelayedTaskList, &( pxCurrentTCB->xStateListItem ) );\r
+               }\r
+               else\r
+               {\r
+                       /* The wake time has not overflowed, so the current block list is used. */\r
+                       vListInsert( pxDelayedTaskList, &( pxCurrentTCB->xStateListItem ) );\r
+\r
+                       /* If the task entering the blocked state was placed at the head of the\r
+                       list of blocked tasks then xNextTaskUnblockTime needs to be updated\r
+                       too. */\r
+                       if( xTimeToWake < xNextTaskUnblockTime )\r
+                       {\r
+                               xNextTaskUnblockTime = xTimeToWake;\r
+                       }\r
+                       else\r
+                       {\r
+                               mtCOVERAGE_TEST_MARKER();\r
+                       }\r
+               }\r
+\r
+               /* Avoid compiler warning when INCLUDE_vTaskSuspend is not 1. */\r
+               ( void ) xCanBlockIndefinitely;\r
+       }\r
+       #endif /* INCLUDE_vTaskSuspend */\r
+}\r
+\r
 \r
 #ifdef FREERTOS_MODULE_TEST\r
        #include "tasks_test_access_functions.h"\r