]> begriffs open source - freertos/blob - tasks.c
Reduce memory usage of ACL. (#809)
[freertos] / tasks.c
1 /*
2  * FreeRTOS Kernel <DEVELOPMENT BRANCH>
3  * Copyright (C) 2021 Amazon.com, Inc. or its affiliates.  All Rights Reserved.
4  *
5  * SPDX-License-Identifier: MIT
6  *
7  * Permission is hereby granted, free of charge, to any person obtaining a copy of
8  * this software and associated documentation files (the "Software"), to deal in
9  * the Software without restriction, including without limitation the rights to
10  * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
11  * the Software, and to permit persons to whom the Software is furnished to do so,
12  * subject to the following conditions:
13  *
14  * The above copyright notice and this permission notice shall be included in all
15  * copies or substantial portions of the Software.
16  *
17  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
19  * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
20  * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
21  * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
22  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23  *
24  * https://www.FreeRTOS.org
25  * https://github.com/FreeRTOS
26  *
27  */
28
29 /* Standard includes. */
30 #include <stdlib.h>
31 #include <string.h>
32
33 /* Defining MPU_WRAPPERS_INCLUDED_FROM_API_FILE prevents task.h from redefining
34  * all the API functions to use the MPU wrappers.  That should only be done when
35  * task.h is included from an application file. */
36 #define MPU_WRAPPERS_INCLUDED_FROM_API_FILE
37
38 /* FreeRTOS includes. */
39 #include "FreeRTOS.h"
40 #include "task.h"
41 #include "timers.h"
42 #include "stack_macros.h"
43
44 /* Lint e9021, e961 and e750 are suppressed as a MISRA exception justified
45  * because the MPU ports require MPU_WRAPPERS_INCLUDED_FROM_API_FILE to be defined
46  * for the header files above, but not in this file, in order to generate the
47  * correct privileged Vs unprivileged linkage and placement. */
48 #undef MPU_WRAPPERS_INCLUDED_FROM_API_FILE /*lint !e961 !e750 !e9021. */
49
50 /* Set configUSE_STATS_FORMATTING_FUNCTIONS to 2 to include the stats formatting
51  * functions but without including stdio.h here. */
52 #if ( configUSE_STATS_FORMATTING_FUNCTIONS == 1 )
53
54 /* At the bottom of this file are two optional functions that can be used
55  * to generate human readable text from the raw data generated by the
56  * uxTaskGetSystemState() function.  Note the formatting functions are provided
57  * for convenience only, and are NOT considered part of the kernel. */
58     #include <stdio.h>
59 #endif /* configUSE_STATS_FORMATTING_FUNCTIONS == 1 ) */
60
61 #if ( configUSE_PREEMPTION == 0 )
62
63 /* If the cooperative scheduler is being used then a yield should not be
64  * performed just because a higher priority task has been woken. */
65     #define taskYIELD_TASK_CORE_IF_USING_PREEMPTION( pxTCB )
66     #define taskYIELD_ANY_CORE_IF_USING_PREEMPTION( pxTCB )
67 #else
68
69     #if ( configNUMBER_OF_CORES == 1 )
70
71 /* This macro requests the running task pxTCB to yield. In single core
72  * scheduler, a running task always runs on core 0 and portYIELD_WITHIN_API()
73  * can be used to request the task running on core 0 to yield. Therefore, pxTCB
74  * is not used in this macro. */
75         #define taskYIELD_TASK_CORE_IF_USING_PREEMPTION( pxTCB ) \
76     do {                                                         \
77         ( void ) ( pxTCB );                                      \
78         portYIELD_WITHIN_API();                                  \
79     } while( 0 )
80
81         #define taskYIELD_ANY_CORE_IF_USING_PREEMPTION( pxTCB ) \
82     do {                                                        \
83         if( pxCurrentTCB->uxPriority < ( pxTCB )->uxPriority )  \
84         {                                                       \
85             portYIELD_WITHIN_API();                             \
86         }                                                       \
87         else                                                    \
88         {                                                       \
89             mtCOVERAGE_TEST_MARKER();                           \
90         }                                                       \
91     } while( 0 )
92
93     #else /* if ( configNUMBER_OF_CORES == 1 ) */
94
95 /* Yield the core on which this task is running. */
96         #define taskYIELD_TASK_CORE_IF_USING_PREEMPTION( pxTCB )    prvYieldCore( ( pxTCB )->xTaskRunState )
97
98 /* Yield for the task if a running task has priority lower than this task. */
99         #define taskYIELD_ANY_CORE_IF_USING_PREEMPTION( pxTCB )     prvYieldForTask( pxTCB )
100
101     #endif /* #if ( configNUMBER_OF_CORES == 1 ) */
102
103 #endif /* if ( configUSE_PREEMPTION == 0 ) */
104
105 /* Values that can be assigned to the ucNotifyState member of the TCB. */
106 #define taskNOT_WAITING_NOTIFICATION              ( ( uint8_t ) 0 ) /* Must be zero as it is the initialised value. */
107 #define taskWAITING_NOTIFICATION                  ( ( uint8_t ) 1 )
108 #define taskNOTIFICATION_RECEIVED                 ( ( uint8_t ) 2 )
109
110 /*
111  * The value used to fill the stack of a task when the task is created.  This
112  * is used purely for checking the high water mark for tasks.
113  */
114 #define tskSTACK_FILL_BYTE                        ( 0xa5U )
115
116 /* Bits used to record how a task's stack and TCB were allocated. */
117 #define tskDYNAMICALLY_ALLOCATED_STACK_AND_TCB    ( ( uint8_t ) 0 )
118 #define tskSTATICALLY_ALLOCATED_STACK_ONLY        ( ( uint8_t ) 1 )
119 #define tskSTATICALLY_ALLOCATED_STACK_AND_TCB     ( ( uint8_t ) 2 )
120
121 /* If any of the following are set then task stacks are filled with a known
122  * value so the high water mark can be determined.  If none of the following are
123  * set then don't fill the stack so there is no unnecessary dependency on memset. */
124 #if ( ( configCHECK_FOR_STACK_OVERFLOW > 1 ) || ( configUSE_TRACE_FACILITY == 1 ) || ( INCLUDE_uxTaskGetStackHighWaterMark == 1 ) || ( INCLUDE_uxTaskGetStackHighWaterMark2 == 1 ) )
125     #define tskSET_NEW_STACKS_TO_KNOWN_VALUE    1
126 #else
127     #define tskSET_NEW_STACKS_TO_KNOWN_VALUE    0
128 #endif
129
130 /*
131  * Macros used by vListTask to indicate which state a task is in.
132  */
133 #define tskRUNNING_CHAR      ( 'X' )
134 #define tskBLOCKED_CHAR      ( 'B' )
135 #define tskREADY_CHAR        ( 'R' )
136 #define tskDELETED_CHAR      ( 'D' )
137 #define tskSUSPENDED_CHAR    ( 'S' )
138
139 /*
140  * Some kernel aware debuggers require the data the debugger needs access to to
141  * be global, rather than file scope.
142  */
143 #ifdef portREMOVE_STATIC_QUALIFIER
144     #define static
145 #endif
146
147 /* The name allocated to the Idle task.  This can be overridden by defining
148  * configIDLE_TASK_NAME in FreeRTOSConfig.h. */
149 #ifndef configIDLE_TASK_NAME
150     #define configIDLE_TASK_NAME    "IDLE"
151 #endif
152
153 #if ( configUSE_PORT_OPTIMISED_TASK_SELECTION == 0 )
154
155 /* If configUSE_PORT_OPTIMISED_TASK_SELECTION is 0 then task selection is
156  * performed in a generic way that is not optimised to any particular
157  * microcontroller architecture. */
158
159 /* uxTopReadyPriority holds the priority of the highest priority ready
160  * state task. */
161     #define taskRECORD_READY_PRIORITY( uxPriority ) \
162     do {                                            \
163         if( ( uxPriority ) > uxTopReadyPriority )   \
164         {                                           \
165             uxTopReadyPriority = ( uxPriority );    \
166         }                                           \
167     } while( 0 ) /* taskRECORD_READY_PRIORITY */
168
169 /*-----------------------------------------------------------*/
170
171     #if ( configNUMBER_OF_CORES == 1 )
172         #define taskSELECT_HIGHEST_PRIORITY_TASK()                            \
173     do {                                                                      \
174         UBaseType_t uxTopPriority = uxTopReadyPriority;                       \
175                                                                               \
176         /* Find the highest priority queue that contains ready tasks. */      \
177         while( listLIST_IS_EMPTY( &( pxReadyTasksLists[ uxTopPriority ] ) ) ) \
178         {                                                                     \
179             configASSERT( uxTopPriority );                                    \
180             --uxTopPriority;                                                  \
181         }                                                                     \
182                                                                               \
183         /* listGET_OWNER_OF_NEXT_ENTRY indexes through the list, so the tasks of \
184          * the  same priority get an equal share of the processor time. */                    \
185         listGET_OWNER_OF_NEXT_ENTRY( pxCurrentTCB, &( pxReadyTasksLists[ uxTopPriority ] ) ); \
186         uxTopReadyPriority = uxTopPriority;                                                   \
187     } while( 0 ) /* taskSELECT_HIGHEST_PRIORITY_TASK */
188     #else /* if ( configNUMBER_OF_CORES == 1 ) */
189
190         #define taskSELECT_HIGHEST_PRIORITY_TASK( xCoreID )    prvSelectHighestPriorityTask( xCoreID )
191
192     #endif /* if ( configNUMBER_OF_CORES == 1 ) */
193
194 /*-----------------------------------------------------------*/
195
196 /* Define away taskRESET_READY_PRIORITY() and portRESET_READY_PRIORITY() as
197  * they are only required when a port optimised method of task selection is
198  * being used. */
199     #define taskRESET_READY_PRIORITY( uxPriority )
200     #define portRESET_READY_PRIORITY( uxPriority, uxTopReadyPriority )
201
202 #else /* configUSE_PORT_OPTIMISED_TASK_SELECTION */
203
204 /* If configUSE_PORT_OPTIMISED_TASK_SELECTION is 1 then task selection is
205  * performed in a way that is tailored to the particular microcontroller
206  * architecture being used. */
207
208 /* A port optimised version is provided.  Call the port defined macros. */
209     #define taskRECORD_READY_PRIORITY( uxPriority )    portRECORD_READY_PRIORITY( ( uxPriority ), uxTopReadyPriority )
210
211 /*-----------------------------------------------------------*/
212
213     #define taskSELECT_HIGHEST_PRIORITY_TASK()                                                  \
214     do {                                                                                        \
215         UBaseType_t uxTopPriority;                                                              \
216                                                                                                 \
217         /* Find the highest priority list that contains ready tasks. */                         \
218         portGET_HIGHEST_PRIORITY( uxTopPriority, uxTopReadyPriority );                          \
219         configASSERT( listCURRENT_LIST_LENGTH( &( pxReadyTasksLists[ uxTopPriority ] ) ) > 0 ); \
220         listGET_OWNER_OF_NEXT_ENTRY( pxCurrentTCB, &( pxReadyTasksLists[ uxTopPriority ] ) );   \
221     } while( 0 )
222
223 /*-----------------------------------------------------------*/
224
225 /* A port optimised version is provided, call it only if the TCB being reset
226  * is being referenced from a ready list.  If it is referenced from a delayed
227  * or suspended list then it won't be in a ready list. */
228     #define taskRESET_READY_PRIORITY( uxPriority )                                                     \
229     do {                                                                                               \
230         if( listCURRENT_LIST_LENGTH( &( pxReadyTasksLists[ ( uxPriority ) ] ) ) == ( UBaseType_t ) 0 ) \
231         {                                                                                              \
232             portRESET_READY_PRIORITY( ( uxPriority ), ( uxTopReadyPriority ) );                        \
233         }                                                                                              \
234     } while( 0 )
235
236 #endif /* configUSE_PORT_OPTIMISED_TASK_SELECTION */
237
238 /*-----------------------------------------------------------*/
239
240 /* pxDelayedTaskList and pxOverflowDelayedTaskList are switched when the tick
241  * count overflows. */
242 #define taskSWITCH_DELAYED_LISTS()                                                \
243     do {                                                                          \
244         List_t * pxTemp;                                                          \
245                                                                                   \
246         /* The delayed tasks list should be empty when the lists are switched. */ \
247         configASSERT( ( listLIST_IS_EMPTY( pxDelayedTaskList ) ) );               \
248                                                                                   \
249         pxTemp = pxDelayedTaskList;                                               \
250         pxDelayedTaskList = pxOverflowDelayedTaskList;                            \
251         pxOverflowDelayedTaskList = pxTemp;                                       \
252         xNumOfOverflows++;                                                        \
253         prvResetNextTaskUnblockTime();                                            \
254     } while( 0 )
255
256 /*-----------------------------------------------------------*/
257
258 /*
259  * Place the task represented by pxTCB into the appropriate ready list for
260  * the task.  It is inserted at the end of the list.
261  */
262 #define prvAddTaskToReadyList( pxTCB )                                                                     \
263     do {                                                                                                   \
264         traceMOVED_TASK_TO_READY_STATE( pxTCB );                                                           \
265         taskRECORD_READY_PRIORITY( ( pxTCB )->uxPriority );                                                \
266         listINSERT_END( &( pxReadyTasksLists[ ( pxTCB )->uxPriority ] ), &( ( pxTCB )->xStateListItem ) ); \
267         tracePOST_MOVED_TASK_TO_READY_STATE( pxTCB );                                                      \
268     } while( 0 )
269 /*-----------------------------------------------------------*/
270
271 /*
272  * Several functions take a TaskHandle_t parameter that can optionally be NULL,
273  * where NULL is used to indicate that the handle of the currently executing
274  * task should be used in place of the parameter.  This macro simply checks to
275  * see if the parameter is NULL and returns a pointer to the appropriate TCB.
276  */
277 #define prvGetTCBFromHandle( pxHandle )    ( ( ( pxHandle ) == NULL ) ? pxCurrentTCB : ( pxHandle ) )
278
279 /* The item value of the event list item is normally used to hold the priority
280  * of the task to which it belongs (coded to allow it to be held in reverse
281  * priority order).  However, it is occasionally borrowed for other purposes.  It
282  * is important its value is not updated due to a task priority change while it is
283  * being used for another purpose.  The following bit definition is used to inform
284  * the scheduler that the value should not be changed - in which case it is the
285  * responsibility of whichever module is using the value to ensure it gets set back
286  * to its original value when it is released. */
287 #if ( configTICK_TYPE_WIDTH_IN_BITS == TICK_TYPE_WIDTH_16_BITS )
288     #define taskEVENT_LIST_ITEM_VALUE_IN_USE    0x8000U
289 #elif ( configTICK_TYPE_WIDTH_IN_BITS == TICK_TYPE_WIDTH_32_BITS )
290     #define taskEVENT_LIST_ITEM_VALUE_IN_USE    0x80000000UL
291 #elif ( configTICK_TYPE_WIDTH_IN_BITS == TICK_TYPE_WIDTH_64_BITS )
292     #define taskEVENT_LIST_ITEM_VALUE_IN_USE    0x8000000000000000ULL
293 #endif
294
295 /* Indicates that the task is not actively running on any core. */
296 #define taskTASK_NOT_RUNNING           ( ( BaseType_t ) ( -1 ) )
297
298 /* Indicates that the task is actively running but scheduled to yield. */
299 #define taskTASK_SCHEDULED_TO_YIELD    ( ( BaseType_t ) ( -2 ) )
300
301 /* Returns pdTRUE if the task is actively running and not scheduled to yield. */
302 #if ( configNUMBER_OF_CORES == 1 )
303     #define taskTASK_IS_RUNNING( pxTCB )                          ( ( ( pxTCB ) == pxCurrentTCB ) ? ( pdTRUE ) : ( pdFALSE ) )
304     #define taskTASK_IS_RUNNING_OR_SCHEDULED_TO_YIELD( pxTCB )    ( ( ( pxTCB ) == pxCurrentTCB ) ? ( pdTRUE ) : ( pdFALSE ) )
305 #else
306     #define taskTASK_IS_RUNNING( pxTCB )                          ( ( ( ( pxTCB )->xTaskRunState >= ( BaseType_t ) 0 ) && ( ( pxTCB )->xTaskRunState < ( BaseType_t ) configNUMBER_OF_CORES ) ) ? ( pdTRUE ) : ( pdFALSE ) )
307     #define taskTASK_IS_RUNNING_OR_SCHEDULED_TO_YIELD( pxTCB )    ( ( ( pxTCB )->xTaskRunState != taskTASK_NOT_RUNNING ) ? ( pdTRUE ) : ( pdFALSE ) )
308 #endif
309
310 /* Indicates that the task is an Idle task. */
311 #define taskATTRIBUTE_IS_IDLE    ( UBaseType_t ) ( 1UL << 0UL )
312
313 #if ( ( configNUMBER_OF_CORES > 1 ) && ( portCRITICAL_NESTING_IN_TCB == 1 ) )
314     #define portGET_CRITICAL_NESTING_COUNT()          ( pxCurrentTCBs[ portGET_CORE_ID() ]->uxCriticalNesting )
315     #define portSET_CRITICAL_NESTING_COUNT( x )       ( pxCurrentTCBs[ portGET_CORE_ID() ]->uxCriticalNesting = ( x ) )
316     #define portINCREMENT_CRITICAL_NESTING_COUNT()    ( pxCurrentTCBs[ portGET_CORE_ID() ]->uxCriticalNesting++ )
317     #define portDECREMENT_CRITICAL_NESTING_COUNT()    ( pxCurrentTCBs[ portGET_CORE_ID() ]->uxCriticalNesting-- )
318 #endif /* #if ( ( configNUMBER_OF_CORES > 1 ) && ( portCRITICAL_NESTING_IN_TCB == 1 ) ) */
319
320 /* Code below here allows infinite loop controlling, especially for the infinite loop
321  * in idle task function (for example when performing unit tests). */
322 #ifndef INFINITE_LOOP
323     #define INFINITE_LOOP()    1
324 #endif
325
326 #define taskBITS_PER_BYTE    ( ( size_t ) 8 )
327
328 /*
329  * Task control block.  A task control block (TCB) is allocated for each task,
330  * and stores task state information, including a pointer to the task's context
331  * (the task's run time environment, including register values)
332  */
333 typedef struct tskTaskControlBlock       /* The old naming convention is used to prevent breaking kernel aware debuggers. */
334 {
335     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. */
336
337     #if ( portUSING_MPU_WRAPPERS == 1 )
338         xMPU_SETTINGS xMPUSettings; /**< The MPU settings are defined as part of the port layer.  THIS MUST BE THE SECOND MEMBER OF THE TCB STRUCT. */
339     #endif
340
341     #if ( configUSE_CORE_AFFINITY == 1 ) && ( configNUMBER_OF_CORES > 1 )
342         UBaseType_t uxCoreAffinityMask; /**< Used to link the task to certain cores.  UBaseType_t must have greater than or equal to the number of bits as configNUMBER_OF_CORES. */
343     #endif
344
345     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 ). */
346     ListItem_t xEventListItem;                  /**< Used to reference a task from an event list. */
347     UBaseType_t uxPriority;                     /**< The priority of the task.  0 is the lowest priority. */
348     StackType_t * pxStack;                      /**< Points to the start of the stack. */
349     #if ( configNUMBER_OF_CORES > 1 )
350         volatile BaseType_t xTaskRunState;      /**< Used to identify the core the task is running on, if the task is running. Otherwise, identifies the task's state - not running or yielding. */
351         UBaseType_t uxTaskAttributes;           /**< Task's attributes - currently used to identify the idle tasks. */
352     #endif
353     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. */
354
355     #if ( configUSE_TASK_PREEMPTION_DISABLE == 1 )
356         BaseType_t xPreemptionDisable; /**< Used to prevent the task from being preempted. */
357     #endif
358
359     #if ( ( portSTACK_GROWTH > 0 ) || ( configRECORD_STACK_HIGH_ADDRESS == 1 ) )
360         StackType_t * pxEndOfStack; /**< Points to the highest valid address for the stack. */
361     #endif
362
363     #if ( portCRITICAL_NESTING_IN_TCB == 1 )
364         UBaseType_t uxCriticalNesting; /**< Holds the critical section nesting depth for ports that do not maintain their own count in the port layer. */
365     #endif
366
367     #if ( configUSE_TRACE_FACILITY == 1 )
368         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. */
369         UBaseType_t uxTaskNumber; /**< Stores a number specifically for use by third party trace code. */
370     #endif
371
372     #if ( configUSE_MUTEXES == 1 )
373         UBaseType_t uxBasePriority; /**< The priority last assigned to the task - used by the priority inheritance mechanism. */
374         UBaseType_t uxMutexesHeld;
375     #endif
376
377     #if ( configUSE_APPLICATION_TASK_TAG == 1 )
378         TaskHookFunction_t pxTaskTag;
379     #endif
380
381     #if ( configNUM_THREAD_LOCAL_STORAGE_POINTERS > 0 )
382         void * pvThreadLocalStoragePointers[ configNUM_THREAD_LOCAL_STORAGE_POINTERS ];
383     #endif
384
385     #if ( configGENERATE_RUN_TIME_STATS == 1 )
386         configRUN_TIME_COUNTER_TYPE ulRunTimeCounter; /**< Stores the amount of time the task has spent in the Running state. */
387     #endif
388
389     #if ( configUSE_C_RUNTIME_TLS_SUPPORT == 1 )
390         configTLS_BLOCK_TYPE xTLSBlock; /**< Memory block used as Thread Local Storage (TLS) Block for the task. */
391     #endif
392
393     #if ( configUSE_TASK_NOTIFICATIONS == 1 )
394         volatile uint32_t ulNotifiedValue[ configTASK_NOTIFICATION_ARRAY_ENTRIES ];
395         volatile uint8_t ucNotifyState[ configTASK_NOTIFICATION_ARRAY_ENTRIES ];
396     #endif
397
398     /* See the comments in FreeRTOS.h with the definition of
399      * tskSTATIC_AND_DYNAMIC_ALLOCATION_POSSIBLE. */
400     #if ( tskSTATIC_AND_DYNAMIC_ALLOCATION_POSSIBLE != 0 ) /*lint !e731 !e9029 Macro has been consolidated for readability reasons. */
401         uint8_t ucStaticallyAllocated;                     /**< Set to pdTRUE if the task is a statically allocated to ensure no attempt is made to free the memory. */
402     #endif
403
404     #if ( INCLUDE_xTaskAbortDelay == 1 )
405         uint8_t ucDelayAborted;
406     #endif
407
408     #if ( configUSE_POSIX_ERRNO == 1 )
409         int iTaskErrno;
410     #endif
411 } tskTCB;
412
413 /* The old tskTCB name is maintained above then typedefed to the new TCB_t name
414  * below to enable the use of older kernel aware debuggers. */
415 typedef tskTCB TCB_t;
416
417 /*lint -save -e956 A manual analysis and inspection has been used to determine
418  * which static variables must be declared volatile. */
419 #if ( configNUMBER_OF_CORES == 1 )
420     portDONT_DISCARD PRIVILEGED_DATA TCB_t * volatile pxCurrentTCB = NULL;
421 #else
422     /* MISRA Ref 8.4.1 [Declaration shall be visible] */
423     /* More details at: https://github.com/FreeRTOS/FreeRTOS-Kernel/blob/main/MISRA.md#rule-84 */
424     /* coverity[misra_c_2012_rule_8_4_violation] */
425     portDONT_DISCARD PRIVILEGED_DATA TCB_t * volatile pxCurrentTCBs[ configNUMBER_OF_CORES ];
426     #define pxCurrentTCB    xTaskGetCurrentTaskHandle()
427 #endif
428
429 /* Lists for ready and blocked tasks. --------------------
430  * xDelayedTaskList1 and xDelayedTaskList2 could be moved to function scope but
431  * doing so breaks some kernel aware debuggers and debuggers that rely on removing
432  * the static qualifier. */
433 PRIVILEGED_DATA static List_t pxReadyTasksLists[ configMAX_PRIORITIES ]; /**< Prioritised ready tasks. */
434 PRIVILEGED_DATA static List_t xDelayedTaskList1;                         /**< Delayed tasks. */
435 PRIVILEGED_DATA static List_t xDelayedTaskList2;                         /**< Delayed tasks (two lists are used - one for delays that have overflowed the current tick count. */
436 PRIVILEGED_DATA static List_t * volatile pxDelayedTaskList;              /**< Points to the delayed task list currently being used. */
437 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. */
438 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. */
439
440 #if ( INCLUDE_vTaskDelete == 1 )
441
442     PRIVILEGED_DATA static List_t xTasksWaitingTermination; /**< Tasks that have been deleted - but their memory not yet freed. */
443     PRIVILEGED_DATA static volatile UBaseType_t uxDeletedTasksWaitingCleanUp = ( UBaseType_t ) 0U;
444
445 #endif
446
447 #if ( INCLUDE_vTaskSuspend == 1 )
448
449     PRIVILEGED_DATA static List_t xSuspendedTaskList; /**< Tasks that are currently suspended. */
450
451 #endif
452
453 /* Global POSIX errno. Its value is changed upon context switching to match
454  * the errno of the currently running task. */
455 #if ( configUSE_POSIX_ERRNO == 1 )
456     int FreeRTOS_errno = 0;
457 #endif
458
459 /* Other file private variables. --------------------------------*/
460 PRIVILEGED_DATA static volatile UBaseType_t uxCurrentNumberOfTasks = ( UBaseType_t ) 0U;
461 PRIVILEGED_DATA static volatile TickType_t xTickCount = ( TickType_t ) configINITIAL_TICK_COUNT;
462 PRIVILEGED_DATA static volatile UBaseType_t uxTopReadyPriority = tskIDLE_PRIORITY;
463 PRIVILEGED_DATA static volatile BaseType_t xSchedulerRunning = pdFALSE;
464 PRIVILEGED_DATA static volatile TickType_t xPendedTicks = ( TickType_t ) 0U;
465 PRIVILEGED_DATA static volatile BaseType_t xYieldPendings[ configNUMBER_OF_CORES ] = { pdFALSE };
466 PRIVILEGED_DATA static volatile BaseType_t xNumOfOverflows = ( BaseType_t ) 0;
467 PRIVILEGED_DATA static UBaseType_t uxTaskNumber = ( UBaseType_t ) 0U;
468 PRIVILEGED_DATA static volatile TickType_t xNextTaskUnblockTime = ( TickType_t ) 0U; /* Initialised to portMAX_DELAY before the scheduler starts. */
469 PRIVILEGED_DATA static TaskHandle_t xIdleTaskHandles[ configNUMBER_OF_CORES ];       /**< Holds the handles of the idle tasks.  The idle tasks are created automatically when the scheduler is started. */
470
471 /* Improve support for OpenOCD. The kernel tracks Ready tasks via priority lists.
472  * For tracking the state of remote threads, OpenOCD uses uxTopUsedPriority
473  * to determine the number of priority lists to read back from the remote target. */
474 const volatile UBaseType_t uxTopUsedPriority = configMAX_PRIORITIES - 1U;
475
476 /* Context switches are held pending while the scheduler is suspended.  Also,
477  * interrupts must not manipulate the xStateListItem of a TCB, or any of the
478  * lists the xStateListItem can be referenced from, if the scheduler is suspended.
479  * If an interrupt needs to unblock a task while the scheduler is suspended then it
480  * moves the task's event list item into the xPendingReadyList, ready for the
481  * kernel to move the task from the pending ready list into the real ready list
482  * when the scheduler is unsuspended.  The pending ready list itself can only be
483  * accessed from a critical section.
484  *
485  * Updates to uxSchedulerSuspended must be protected by both the task lock and the ISR lock
486  * and must not be done from an ISR. Reads must be protected by either lock and may be done
487  * from either an ISR or a task. */
488 PRIVILEGED_DATA static volatile UBaseType_t uxSchedulerSuspended = ( UBaseType_t ) 0U;
489
490 #if ( configGENERATE_RUN_TIME_STATS == 1 )
491
492 /* Do not move these variables to function scope as doing so prevents the
493  * code working with debuggers that need to remove the static qualifier. */
494 PRIVILEGED_DATA static configRUN_TIME_COUNTER_TYPE ulTaskSwitchedInTime[ configNUMBER_OF_CORES ] = { 0U };    /**< Holds the value of a timer/counter the last time a task was switched in. */
495 PRIVILEGED_DATA static volatile configRUN_TIME_COUNTER_TYPE ulTotalRunTime[ configNUMBER_OF_CORES ] = { 0U }; /**< Holds the total amount of execution time as defined by the run time counter clock. */
496
497 #endif
498
499 #if ( configSUPPORT_STATIC_ALLOCATION == 1 ) && ( configNUMBER_OF_CORES > 1 )
500
501 /* Do not move these variables to function scope as doing so prevents the
502  * code working with debuggers that need to remove the static qualifier. */
503     static StaticTask_t xIdleTCBBuffers[ configNUMBER_OF_CORES - 1 ];
504     static StackType_t xIdleTaskStackBuffers[ configNUMBER_OF_CORES - 1 ][ configMINIMAL_STACK_SIZE ];
505
506 #endif /* #if ( configSUPPORT_STATIC_ALLOCATION == 1 ) && ( configNUMBER_OF_CORES > 1 ) */
507
508 /*lint -restore */
509
510 /*-----------------------------------------------------------*/
511
512 /* File private functions. --------------------------------*/
513
514 /*
515  * Creates the idle tasks during scheduler start.
516  */
517 static BaseType_t prvCreateIdleTasks( void );
518
519 #if ( configNUMBER_OF_CORES > 1 )
520
521 /*
522  * Checks to see if another task moved the current task out of the ready
523  * list while it was waiting to enter a critical section and yields, if so.
524  */
525     static void prvCheckForRunStateChange( void );
526 #endif /* #if ( configNUMBER_OF_CORES > 1 ) */
527
528 #if ( configNUMBER_OF_CORES > 1 )
529
530 /*
531  * Yields the given core.
532  */
533     static void prvYieldCore( BaseType_t xCoreID );
534 #endif /* #if ( configNUMBER_OF_CORES > 1 ) */
535
536 #if ( configNUMBER_OF_CORES > 1 )
537
538 /*
539  * Yields a core, or cores if multiple priorities are not allowed to run
540  * simultaneously, to allow the task pxTCB to run.
541  */
542     static void prvYieldForTask( const TCB_t * pxTCB );
543 #endif /* #if ( configNUMBER_OF_CORES > 1 ) */
544
545 #if ( configNUMBER_OF_CORES > 1 )
546
547 /*
548  * Selects the highest priority available task for the given core.
549  */
550     static void prvSelectHighestPriorityTask( BaseType_t xCoreID );
551 #endif /* #if ( configNUMBER_OF_CORES > 1 ) */
552
553 /**
554  * Utility task that simply returns pdTRUE if the task referenced by xTask is
555  * currently in the Suspended state, or pdFALSE if the task referenced by xTask
556  * is in any other state.
557  */
558 #if ( INCLUDE_vTaskSuspend == 1 )
559
560     static BaseType_t prvTaskIsTaskSuspended( const TaskHandle_t xTask ) PRIVILEGED_FUNCTION;
561
562 #endif /* INCLUDE_vTaskSuspend */
563
564 /*
565  * Utility to ready all the lists used by the scheduler.  This is called
566  * automatically upon the creation of the first task.
567  */
568 static void prvInitialiseTaskLists( void ) PRIVILEGED_FUNCTION;
569
570 /*
571  * The idle task, which as all tasks is implemented as a never ending loop.
572  * The idle task is automatically created and added to the ready lists upon
573  * creation of the first user task.
574  *
575  * In the FreeRTOS SMP, configNUMBER_OF_CORES - 1 minimal idle tasks are also
576  * created to ensure that each core has an idle task to run when no other
577  * task is available to run.
578  *
579  * The portTASK_FUNCTION_PROTO() macro is used to allow port/compiler specific
580  * language extensions.  The equivalent prototype for these functions are:
581  *
582  * void prvIdleTask( void *pvParameters );
583  * void prvMinimalIdleTask( void *pvParameters );
584  *
585  */
586 static portTASK_FUNCTION_PROTO( prvIdleTask, pvParameters ) PRIVILEGED_FUNCTION;
587 #if ( configNUMBER_OF_CORES > 1 )
588     static portTASK_FUNCTION_PROTO( prvMinimalIdleTask, pvParameters ) PRIVILEGED_FUNCTION;
589 #endif
590
591 /*
592  * Utility to free all memory allocated by the scheduler to hold a TCB,
593  * including the stack pointed to by the TCB.
594  *
595  * This does not free memory allocated by the task itself (i.e. memory
596  * allocated by calls to pvPortMalloc from within the tasks application code).
597  */
598 #if ( INCLUDE_vTaskDelete == 1 )
599
600     static void prvDeleteTCB( TCB_t * pxTCB ) PRIVILEGED_FUNCTION;
601
602 #endif
603
604 /*
605  * Used only by the idle task.  This checks to see if anything has been placed
606  * in the list of tasks waiting to be deleted.  If so the task is cleaned up
607  * and its TCB deleted.
608  */
609 static void prvCheckTasksWaitingTermination( void ) PRIVILEGED_FUNCTION;
610
611 /*
612  * The currently executing task is entering the Blocked state.  Add the task to
613  * either the current or the overflow delayed task list.
614  */
615 static void prvAddCurrentTaskToDelayedList( TickType_t xTicksToWait,
616                                             const BaseType_t xCanBlockIndefinitely ) PRIVILEGED_FUNCTION;
617
618 /*
619  * Fills an TaskStatus_t structure with information on each task that is
620  * referenced from the pxList list (which may be a ready list, a delayed list,
621  * a suspended list, etc.).
622  *
623  * THIS FUNCTION IS INTENDED FOR DEBUGGING ONLY, AND SHOULD NOT BE CALLED FROM
624  * NORMAL APPLICATION CODE.
625  */
626 #if ( configUSE_TRACE_FACILITY == 1 )
627
628     static UBaseType_t prvListTasksWithinSingleList( TaskStatus_t * pxTaskStatusArray,
629                                                      List_t * pxList,
630                                                      eTaskState eState ) PRIVILEGED_FUNCTION;
631
632 #endif
633
634 /*
635  * Searches pxList for a task with name pcNameToQuery - returning a handle to
636  * the task if it is found, or NULL if the task is not found.
637  */
638 #if ( INCLUDE_xTaskGetHandle == 1 )
639
640     static TCB_t * prvSearchForNameWithinSingleList( List_t * pxList,
641                                                      const char pcNameToQuery[] ) PRIVILEGED_FUNCTION;
642
643 #endif
644
645 /*
646  * When a task is created, the stack of the task is filled with a known value.
647  * This function determines the 'high water mark' of the task stack by
648  * determining how much of the stack remains at the original preset value.
649  */
650 #if ( ( configUSE_TRACE_FACILITY == 1 ) || ( INCLUDE_uxTaskGetStackHighWaterMark == 1 ) || ( INCLUDE_uxTaskGetStackHighWaterMark2 == 1 ) )
651
652     static configSTACK_DEPTH_TYPE prvTaskCheckFreeStackSpace( const uint8_t * pucStackByte ) PRIVILEGED_FUNCTION;
653
654 #endif
655
656 /*
657  * Return the amount of time, in ticks, that will pass before the kernel will
658  * next move a task from the Blocked state to the Running state.
659  *
660  * This conditional compilation should use inequality to 0, not equality to 1.
661  * This is to ensure portSUPPRESS_TICKS_AND_SLEEP() can be called when user
662  * defined low power mode implementations require configUSE_TICKLESS_IDLE to be
663  * set to a value other than 1.
664  */
665 #if ( configUSE_TICKLESS_IDLE != 0 )
666
667     static TickType_t prvGetExpectedIdleTime( void ) PRIVILEGED_FUNCTION;
668
669 #endif
670
671 /*
672  * Set xNextTaskUnblockTime to the time at which the next Blocked state task
673  * will exit the Blocked state.
674  */
675 static void prvResetNextTaskUnblockTime( void ) PRIVILEGED_FUNCTION;
676
677 #if ( configUSE_STATS_FORMATTING_FUNCTIONS > 0 )
678
679 /*
680  * Helper function used to pad task names with spaces when printing out
681  * human readable tables of task information.
682  */
683     static char * prvWriteNameToBuffer( char * pcBuffer,
684                                         const char * pcTaskName ) PRIVILEGED_FUNCTION;
685
686 #endif
687
688 /*
689  * Called after a Task_t structure has been allocated either statically or
690  * dynamically to fill in the structure's members.
691  */
692 static void prvInitialiseNewTask( TaskFunction_t pxTaskCode,
693                                   const char * const pcName, /*lint !e971 Unqualified char types are allowed for strings and single characters only. */
694                                   const uint32_t ulStackDepth,
695                                   void * const pvParameters,
696                                   UBaseType_t uxPriority,
697                                   TaskHandle_t * const pxCreatedTask,
698                                   TCB_t * pxNewTCB,
699                                   const MemoryRegion_t * const xRegions ) PRIVILEGED_FUNCTION;
700
701 /*
702  * Called after a new task has been created and initialised to place the task
703  * under the control of the scheduler.
704  */
705 static void prvAddNewTaskToReadyList( TCB_t * pxNewTCB ) PRIVILEGED_FUNCTION;
706
707 /*
708  * freertos_tasks_c_additions_init() should only be called if the user definable
709  * macro FREERTOS_TASKS_C_ADDITIONS_INIT() is defined, as that is the only macro
710  * called by the function.
711  */
712 #ifdef FREERTOS_TASKS_C_ADDITIONS_INIT
713
714     static void freertos_tasks_c_additions_init( void ) PRIVILEGED_FUNCTION;
715
716 #endif
717
718 #if ( configUSE_MINIMAL_IDLE_HOOK == 1 )
719     extern void vApplicationMinimalIdleHook( void );
720 #endif /* #if ( configUSE_MINIMAL_IDLE_HOOK == 1 ) */
721
722 /*-----------------------------------------------------------*/
723
724 #if ( configNUMBER_OF_CORES > 1 )
725     static void prvCheckForRunStateChange( void )
726     {
727         UBaseType_t uxPrevCriticalNesting;
728         const TCB_t * pxThisTCB;
729
730         /* This must only be called from within a task. */
731         portASSERT_IF_IN_ISR();
732
733         /* This function is always called with interrupts disabled
734          * so this is safe. */
735         pxThisTCB = pxCurrentTCBs[ portGET_CORE_ID() ];
736
737         while( pxThisTCB->xTaskRunState == taskTASK_SCHEDULED_TO_YIELD )
738         {
739             /* We are only here if we just entered a critical section
740             * or if we just suspended the scheduler, and another task
741             * has requested that we yield.
742             *
743             * This is slightly complicated since we need to save and restore
744             * the suspension and critical nesting counts, as well as release
745             * and reacquire the correct locks. And then, do it all over again
746             * if our state changed again during the reacquisition. */
747             uxPrevCriticalNesting = portGET_CRITICAL_NESTING_COUNT();
748
749             if( uxPrevCriticalNesting > 0U )
750             {
751                 portSET_CRITICAL_NESTING_COUNT( 0U );
752                 portRELEASE_ISR_LOCK();
753             }
754             else
755             {
756                 /* The scheduler is suspended. uxSchedulerSuspended is updated
757                  * only when the task is not requested to yield. */
758                 mtCOVERAGE_TEST_MARKER();
759             }
760
761             portRELEASE_TASK_LOCK();
762             portMEMORY_BARRIER();
763             configASSERT( pxThisTCB->xTaskRunState == taskTASK_SCHEDULED_TO_YIELD );
764
765             portENABLE_INTERRUPTS();
766
767             /* Enabling interrupts should cause this core to immediately
768              * service the pending interrupt and yield. If the run state is still
769              * yielding here then that is a problem. */
770             configASSERT( pxThisTCB->xTaskRunState != taskTASK_SCHEDULED_TO_YIELD );
771
772             portDISABLE_INTERRUPTS();
773             portGET_TASK_LOCK();
774             portGET_ISR_LOCK();
775
776             portSET_CRITICAL_NESTING_COUNT( uxPrevCriticalNesting );
777
778             if( uxPrevCriticalNesting == 0U )
779             {
780                 portRELEASE_ISR_LOCK();
781             }
782         }
783     }
784 #endif /* #if ( configNUMBER_OF_CORES > 1 ) */
785
786 /*-----------------------------------------------------------*/
787
788 #if ( configNUMBER_OF_CORES > 1 )
789     static void prvYieldCore( BaseType_t xCoreID )
790     {
791         /* This must be called from a critical section and xCoreID must be valid. */
792         if( ( portCHECK_IF_IN_ISR() == pdTRUE ) && ( xCoreID == ( BaseType_t ) portGET_CORE_ID() ) )
793         {
794             xYieldPendings[ xCoreID ] = pdTRUE;
795         }
796         else
797         {
798             if( pxCurrentTCBs[ xCoreID ]->xTaskRunState != taskTASK_SCHEDULED_TO_YIELD )
799             {
800                 if( xCoreID == ( BaseType_t ) portGET_CORE_ID() )
801                 {
802                     xYieldPendings[ xCoreID ] = pdTRUE;
803                 }
804                 else
805                 {
806                     portYIELD_CORE( xCoreID );
807                     pxCurrentTCBs[ xCoreID ]->xTaskRunState = taskTASK_SCHEDULED_TO_YIELD;
808                 }
809             }
810         }
811     }
812 #endif /* #if ( configNUMBER_OF_CORES > 1 ) */
813 /*-----------------------------------------------------------*/
814
815 #if ( configNUMBER_OF_CORES > 1 )
816     static void prvYieldForTask( const TCB_t * pxTCB )
817     {
818         BaseType_t xLowestPriorityToPreempt;
819         BaseType_t xCurrentCoreTaskPriority;
820         BaseType_t xLowestPriorityCore = ( BaseType_t ) -1;
821         BaseType_t xCoreID;
822
823         #if ( configRUN_MULTIPLE_PRIORITIES == 0 )
824             BaseType_t xYieldCount = 0;
825         #endif /* #if ( configRUN_MULTIPLE_PRIORITIES == 0 ) */
826
827         /* This must be called from a critical section. */
828         configASSERT( portGET_CRITICAL_NESTING_COUNT() > 0U );
829
830         #if ( configRUN_MULTIPLE_PRIORITIES == 0 )
831
832             /* No task should yield for this one if it is a lower priority
833              * than priority level of currently ready tasks. */
834             if( pxTCB->uxPriority >= uxTopReadyPriority )
835         #else
836             /* Yield is not required for a task which is already running. */
837             if( taskTASK_IS_RUNNING( pxTCB ) == pdFALSE )
838         #endif
839         {
840             xLowestPriorityToPreempt = ( BaseType_t ) pxTCB->uxPriority;
841
842             /* xLowestPriorityToPreempt will be decremented to -1 if the priority of pxTCB
843              * is 0. This is ok as we will give system idle tasks a priority of -1 below. */
844             --xLowestPriorityToPreempt;
845
846             for( xCoreID = ( BaseType_t ) 0; xCoreID < ( BaseType_t ) configNUMBER_OF_CORES; xCoreID++ )
847             {
848                 xCurrentCoreTaskPriority = ( BaseType_t ) pxCurrentTCBs[ xCoreID ]->uxPriority;
849
850                 /* System idle tasks are being assigned a priority of tskIDLE_PRIORITY - 1 here. */
851                 if( ( pxCurrentTCBs[ xCoreID ]->uxTaskAttributes & taskATTRIBUTE_IS_IDLE ) != 0U )
852                 {
853                     xCurrentCoreTaskPriority = xCurrentCoreTaskPriority - 1;
854                 }
855
856                 if( ( taskTASK_IS_RUNNING( pxCurrentTCBs[ xCoreID ] ) != pdFALSE ) && ( xYieldPendings[ xCoreID ] == pdFALSE ) )
857                 {
858                     #if ( configRUN_MULTIPLE_PRIORITIES == 0 )
859                         if( taskTASK_IS_RUNNING( pxTCB ) == pdFALSE )
860                     #endif
861                     {
862                         if( xCurrentCoreTaskPriority <= xLowestPriorityToPreempt )
863                         {
864                             #if ( configUSE_CORE_AFFINITY == 1 )
865                                 if( ( pxTCB->uxCoreAffinityMask & ( ( UBaseType_t ) 1U << ( UBaseType_t ) xCoreID ) ) != 0U )
866                             #endif
867                             {
868                                 #if ( configUSE_TASK_PREEMPTION_DISABLE == 1 )
869                                     if( pxCurrentTCBs[ xCoreID ]->xPreemptionDisable == pdFALSE )
870                                 #endif
871                                 {
872                                     xLowestPriorityToPreempt = xCurrentCoreTaskPriority;
873                                     xLowestPriorityCore = xCoreID;
874                                 }
875                             }
876                         }
877                         else
878                         {
879                             mtCOVERAGE_TEST_MARKER();
880                         }
881                     }
882
883                     #if ( configRUN_MULTIPLE_PRIORITIES == 0 )
884                     {
885                         /* Yield all currently running non-idle tasks with a priority lower than
886                          * the task that needs to run. */
887                         if( ( xCurrentCoreTaskPriority > ( ( BaseType_t ) tskIDLE_PRIORITY - 1 ) ) &&
888                             ( xCurrentCoreTaskPriority < ( BaseType_t ) pxTCB->uxPriority ) )
889                         {
890                             prvYieldCore( xCoreID );
891                             xYieldCount++;
892                         }
893                         else
894                         {
895                             mtCOVERAGE_TEST_MARKER();
896                         }
897                     }
898                     #endif /* #if ( configRUN_MULTIPLE_PRIORITIES == 0 ) */
899                 }
900                 else
901                 {
902                     mtCOVERAGE_TEST_MARKER();
903                 }
904             }
905
906             #if ( configRUN_MULTIPLE_PRIORITIES == 0 )
907                 if( ( xYieldCount == 0 ) && ( xLowestPriorityCore >= 0 ) )
908             #else /* #if ( configRUN_MULTIPLE_PRIORITIES == 0 ) */
909                 if( xLowestPriorityCore >= 0 )
910             #endif /* #if ( configRUN_MULTIPLE_PRIORITIES == 0 ) */
911             {
912                 prvYieldCore( xLowestPriorityCore );
913             }
914
915             #if ( configRUN_MULTIPLE_PRIORITIES == 0 )
916                 /* Verify that the calling core always yields to higher priority tasks. */
917                 if( ( ( pxCurrentTCBs[ portGET_CORE_ID() ]->uxTaskAttributes & taskATTRIBUTE_IS_IDLE ) == 0 ) &&
918                     ( pxTCB->uxPriority > pxCurrentTCBs[ portGET_CORE_ID() ]->uxPriority ) )
919                 {
920                     configASSERT( ( xYieldPendings[ portGET_CORE_ID() ] == pdTRUE ) ||
921                                   ( taskTASK_IS_RUNNING( pxCurrentTCBs[ portGET_CORE_ID() ] ) == pdFALSE ) );
922                 }
923             #endif
924         }
925     }
926 #endif /* #if ( configNUMBER_OF_CORES > 1 ) */
927 /*-----------------------------------------------------------*/
928
929 #if ( configNUMBER_OF_CORES > 1 )
930     static void prvSelectHighestPriorityTask( BaseType_t xCoreID )
931     {
932         UBaseType_t uxCurrentPriority = uxTopReadyPriority;
933         BaseType_t xTaskScheduled = pdFALSE;
934         BaseType_t xDecrementTopPriority = pdTRUE;
935
936         #if ( configUSE_CORE_AFFINITY == 1 )
937             const TCB_t * pxPreviousTCB = NULL;
938         #endif
939         #if ( configRUN_MULTIPLE_PRIORITIES == 0 )
940             BaseType_t xPriorityDropped = pdFALSE;
941         #endif
942
943         /* This function should be called when scheduler is running. */
944         configASSERT( xSchedulerRunning == pdTRUE );
945
946         /* A new task is created and a running task with the same priority yields
947          * itself to run the new task. When a running task yields itself, it is still
948          * in the ready list. This running task will be selected before the new task
949          * since the new task is always added to the end of the ready list.
950          * The other problem is that the running task still in the same position of
951          * the ready list when it yields itself. It is possible that it will be selected
952          * earlier then other tasks which waits longer than this task.
953          *
954          * To fix these problems, the running task should be put to the end of the
955          * ready list before searching for the ready task in the ready list. */
956         if( listIS_CONTAINED_WITHIN( &( pxReadyTasksLists[ pxCurrentTCBs[ xCoreID ]->uxPriority ] ),
957                                      &pxCurrentTCBs[ xCoreID ]->xStateListItem ) == pdTRUE )
958         {
959             ( void ) uxListRemove( &pxCurrentTCBs[ xCoreID ]->xStateListItem );
960             vListInsertEnd( &( pxReadyTasksLists[ pxCurrentTCBs[ xCoreID ]->uxPriority ] ),
961                             &pxCurrentTCBs[ xCoreID ]->xStateListItem );
962         }
963
964         while( xTaskScheduled == pdFALSE )
965         {
966             #if ( configRUN_MULTIPLE_PRIORITIES == 0 )
967             {
968                 if( uxCurrentPriority < uxTopReadyPriority )
969                 {
970                     /* We can't schedule any tasks, other than idle, that have a
971                      * priority lower than the priority of a task currently running
972                      * on another core. */
973                     uxCurrentPriority = tskIDLE_PRIORITY;
974                 }
975             }
976             #endif
977
978             if( listLIST_IS_EMPTY( &( pxReadyTasksLists[ uxCurrentPriority ] ) ) == pdFALSE )
979             {
980                 const List_t * const pxReadyList = &( pxReadyTasksLists[ uxCurrentPriority ] );
981                 const ListItem_t * pxEndMarker = listGET_END_MARKER( pxReadyList );
982                 ListItem_t * pxIterator;
983
984                 /* The ready task list for uxCurrentPriority is not empty, so uxTopReadyPriority
985                  * must not be decremented any further. */
986                 xDecrementTopPriority = pdFALSE;
987
988                 for( pxIterator = listGET_HEAD_ENTRY( pxReadyList ); pxIterator != pxEndMarker; pxIterator = listGET_NEXT( pxIterator ) )
989                 {
990                     TCB_t * pxTCB = ( TCB_t * ) listGET_LIST_ITEM_OWNER( pxIterator );
991
992                     #if ( configRUN_MULTIPLE_PRIORITIES == 0 )
993                     {
994                         /* When falling back to the idle priority because only one priority
995                          * level is allowed to run at a time, we should ONLY schedule the true
996                          * idle tasks, not user tasks at the idle priority. */
997                         if( uxCurrentPriority < uxTopReadyPriority )
998                         {
999                             if( ( pxTCB->uxTaskAttributes & taskATTRIBUTE_IS_IDLE ) == 0 )
1000                             {
1001                                 continue;
1002                             }
1003                         }
1004                     }
1005                     #endif /* #if ( configRUN_MULTIPLE_PRIORITIES == 0 ) */
1006
1007                     if( pxTCB->xTaskRunState == taskTASK_NOT_RUNNING )
1008                     {
1009                         #if ( configUSE_CORE_AFFINITY == 1 )
1010                             if( ( pxTCB->uxCoreAffinityMask & ( ( UBaseType_t ) 1U << ( UBaseType_t ) xCoreID ) ) != 0U )
1011                         #endif
1012                         {
1013                             /* If the task is not being executed by any core swap it in. */
1014                             pxCurrentTCBs[ xCoreID ]->xTaskRunState = taskTASK_NOT_RUNNING;
1015                             #if ( configUSE_CORE_AFFINITY == 1 )
1016                                 pxPreviousTCB = pxCurrentTCBs[ xCoreID ];
1017                             #endif
1018                             pxTCB->xTaskRunState = xCoreID;
1019                             pxCurrentTCBs[ xCoreID ] = pxTCB;
1020                             xTaskScheduled = pdTRUE;
1021                         }
1022                     }
1023                     else if( pxTCB == pxCurrentTCBs[ xCoreID ] )
1024                     {
1025                         configASSERT( ( pxTCB->xTaskRunState == xCoreID ) || ( pxTCB->xTaskRunState == taskTASK_SCHEDULED_TO_YIELD ) );
1026
1027                         #if ( configUSE_CORE_AFFINITY == 1 )
1028                             if( ( pxTCB->uxCoreAffinityMask & ( ( UBaseType_t ) 1U << ( UBaseType_t ) xCoreID ) ) != 0U )
1029                         #endif
1030                         {
1031                             /* The task is already running on this core, mark it as scheduled. */
1032                             pxTCB->xTaskRunState = xCoreID;
1033                             xTaskScheduled = pdTRUE;
1034                         }
1035                     }
1036                     else
1037                     {
1038                         /* This task is running on the core other than xCoreID. */
1039                         mtCOVERAGE_TEST_MARKER();
1040                     }
1041
1042                     if( xTaskScheduled != pdFALSE )
1043                     {
1044                         /* A task has been selected to run on this core. */
1045                         break;
1046                     }
1047                 }
1048             }
1049             else
1050             {
1051                 if( xDecrementTopPriority != pdFALSE )
1052                 {
1053                     uxTopReadyPriority--;
1054                     #if ( configRUN_MULTIPLE_PRIORITIES == 0 )
1055                     {
1056                         xPriorityDropped = pdTRUE;
1057                     }
1058                     #endif
1059                 }
1060             }
1061
1062             /* There are configNUMBER_OF_CORES Idle tasks created when scheduler started.
1063              * The scheduler should be able to select a task to run when uxCurrentPriority
1064              * is tskIDLE_PRIORITY. uxCurrentPriority is never decreased to value blow
1065              * tskIDLE_PRIORITY. */
1066             if( uxCurrentPriority > tskIDLE_PRIORITY )
1067             {
1068                 uxCurrentPriority--;
1069             }
1070             else
1071             {
1072                 /* This function is called when idle task is not created. Break the
1073                  * loop to prevent uxCurrentPriority overrun. */
1074                 break;
1075             }
1076         }
1077
1078         #if ( configRUN_MULTIPLE_PRIORITIES == 0 )
1079         {
1080             if( xTaskScheduled == pdTRUE )
1081             {
1082                 if( xPriorityDropped != pdFALSE )
1083                 {
1084                     /* There may be several ready tasks that were being prevented from running because there was
1085                      * a higher priority task running. Now that the last of the higher priority tasks is no longer
1086                      * running, make sure all the other idle tasks yield. */
1087                     BaseType_t x;
1088
1089                     for( x = ( BaseType_t ) 0; x < ( BaseType_t ) configNUMBER_OF_CORES; x++ )
1090                     {
1091                         if( ( pxCurrentTCBs[ x ]->uxTaskAttributes & taskATTRIBUTE_IS_IDLE ) != 0 )
1092                         {
1093                             prvYieldCore( x );
1094                         }
1095                     }
1096                 }
1097             }
1098         }
1099         #endif /* #if ( configRUN_MULTIPLE_PRIORITIES == 0 ) */
1100
1101         #if ( configUSE_CORE_AFFINITY == 1 )
1102         {
1103             if( xTaskScheduled == pdTRUE )
1104             {
1105                 if( ( pxPreviousTCB != NULL ) && ( listIS_CONTAINED_WITHIN( &( pxReadyTasksLists[ pxPreviousTCB->uxPriority ] ), &( pxPreviousTCB->xStateListItem ) ) != pdFALSE ) )
1106                 {
1107                     /* A ready task was just evicted from this core. See if it can be
1108                      * scheduled on any other core. */
1109                     UBaseType_t uxCoreMap = pxPreviousTCB->uxCoreAffinityMask;
1110                     BaseType_t xLowestPriority = ( BaseType_t ) pxPreviousTCB->uxPriority;
1111                     BaseType_t xLowestPriorityCore = -1;
1112                     BaseType_t x;
1113
1114                     if( ( pxPreviousTCB->uxTaskAttributes & taskATTRIBUTE_IS_IDLE ) != 0U )
1115                     {
1116                         xLowestPriority = xLowestPriority - 1;
1117                     }
1118
1119                     if( ( uxCoreMap & ( ( UBaseType_t ) 1U << ( UBaseType_t ) xCoreID ) ) != 0U )
1120                     {
1121                         /* The ready task that was removed from this core is not excluded from it.
1122                          * Only look at the intersection of the cores the removed task is allowed to run
1123                          * on with the cores that the new task is excluded from. It is possible that the
1124                          * new task was only placed onto this core because it is excluded from another.
1125                          * Check to see if the previous task could run on one of those cores. */
1126                         uxCoreMap &= ~( pxCurrentTCBs[ xCoreID ]->uxCoreAffinityMask );
1127                     }
1128                     else
1129                     {
1130                         /* The ready task that was removed from this core is excluded from it. */
1131                     }
1132
1133                     uxCoreMap &= ( ( 1U << configNUMBER_OF_CORES ) - 1U );
1134
1135                     for( x = ( ( BaseType_t ) configNUMBER_OF_CORES - 1 ); x >= ( BaseType_t ) 0; x-- )
1136                     {
1137                         UBaseType_t uxCore = ( UBaseType_t ) x;
1138                         BaseType_t xTaskPriority;
1139
1140                         if( ( uxCoreMap & ( ( UBaseType_t ) 1U << uxCore ) ) != 0U )
1141                         {
1142                             xTaskPriority = ( BaseType_t ) pxCurrentTCBs[ uxCore ]->uxPriority;
1143
1144                             if( ( pxCurrentTCBs[ uxCore ]->uxTaskAttributes & taskATTRIBUTE_IS_IDLE ) != 0U )
1145                             {
1146                                 xTaskPriority = xTaskPriority - ( BaseType_t ) 1;
1147                             }
1148
1149                             uxCoreMap &= ~( ( UBaseType_t ) 1U << uxCore );
1150
1151                             if( ( xTaskPriority < xLowestPriority ) &&
1152                                 ( taskTASK_IS_RUNNING( pxCurrentTCBs[ uxCore ] ) != pdFALSE ) &&
1153                                 ( xYieldPendings[ uxCore ] == pdFALSE ) )
1154                             {
1155                                 #if ( configUSE_TASK_PREEMPTION_DISABLE == 1 )
1156                                     if( pxCurrentTCBs[ uxCore ]->xPreemptionDisable == pdFALSE )
1157                                 #endif
1158                                 {
1159                                     xLowestPriority = xTaskPriority;
1160                                     xLowestPriorityCore = ( BaseType_t ) uxCore;
1161                                 }
1162                             }
1163                         }
1164                     }
1165
1166                     if( xLowestPriorityCore >= 0 )
1167                     {
1168                         prvYieldCore( xLowestPriorityCore );
1169                     }
1170                 }
1171             }
1172         }
1173         #endif /* #if ( configUSE_CORE_AFFINITY == 1 ) */
1174     }
1175
1176 #endif /* ( configNUMBER_OF_CORES > 1 ) */
1177
1178 /*-----------------------------------------------------------*/
1179
1180 #if ( configSUPPORT_STATIC_ALLOCATION == 1 )
1181
1182     TaskHandle_t xTaskCreateStatic( TaskFunction_t pxTaskCode,
1183                                     const char * const pcName, /*lint !e971 Unqualified char types are allowed for strings and single characters only. */
1184                                     const uint32_t ulStackDepth,
1185                                     void * const pvParameters,
1186                                     UBaseType_t uxPriority,
1187                                     StackType_t * const puxStackBuffer,
1188                                     StaticTask_t * const pxTaskBuffer )
1189     #if ( ( configNUMBER_OF_CORES > 1 ) && ( configUSE_CORE_AFFINITY == 1 ) )
1190     {
1191         return xTaskCreateStaticAffinitySet( pxTaskCode, pcName, ulStackDepth, pvParameters, uxPriority, puxStackBuffer, pxTaskBuffer, tskNO_AFFINITY );
1192     }
1193
1194     TaskHandle_t xTaskCreateStaticAffinitySet( TaskFunction_t pxTaskCode,
1195                                                const char * const pcName, /*lint !e971 Unqualified char types are allowed for strings and single characters only. */
1196                                                const uint32_t ulStackDepth,
1197                                                void * const pvParameters,
1198                                                UBaseType_t uxPriority,
1199                                                StackType_t * const puxStackBuffer,
1200                                                StaticTask_t * const pxTaskBuffer,
1201                                                UBaseType_t uxCoreAffinityMask )
1202     #endif /* #if ( ( configNUMBER_OF_CORES > 1 ) && ( configUSE_CORE_AFFINITY == 1 ) ) */
1203     {
1204         TCB_t * pxNewTCB;
1205         TaskHandle_t xReturn;
1206
1207         traceENTER_xTaskCreateStatic( pxTaskCode, pcName, ulStackDepth, pvParameters, uxPriority, puxStackBuffer, pxTaskBuffer );
1208
1209         configASSERT( puxStackBuffer != NULL );
1210         configASSERT( pxTaskBuffer != NULL );
1211
1212         #if ( configASSERT_DEFINED == 1 )
1213         {
1214             /* Sanity check that the size of the structure used to declare a
1215              * variable of type StaticTask_t equals the size of the real task
1216              * structure. */
1217             volatile size_t xSize = sizeof( StaticTask_t );
1218             configASSERT( xSize == sizeof( TCB_t ) );
1219             ( void ) xSize; /* Prevent lint warning when configASSERT() is not used. */
1220         }
1221         #endif /* configASSERT_DEFINED */
1222
1223         if( ( pxTaskBuffer != NULL ) && ( puxStackBuffer != NULL ) )
1224         {
1225             /* The memory used for the task's TCB and stack are passed into this
1226              * function - use them. */
1227             pxNewTCB = ( TCB_t * ) pxTaskBuffer; /*lint !e740 !e9087 Unusual cast is ok as the structures are designed to have the same alignment, and the size is checked by an assert. */
1228             ( void ) memset( ( void * ) pxNewTCB, 0x00, sizeof( TCB_t ) );
1229             pxNewTCB->pxStack = ( StackType_t * ) puxStackBuffer;
1230
1231             #if ( tskSTATIC_AND_DYNAMIC_ALLOCATION_POSSIBLE != 0 ) /*lint !e731 !e9029 Macro has been consolidated for readability reasons. */
1232             {
1233                 /* Tasks can be created statically or dynamically, so note this
1234                  * task was created statically in case the task is later deleted. */
1235                 pxNewTCB->ucStaticallyAllocated = tskSTATICALLY_ALLOCATED_STACK_AND_TCB;
1236             }
1237             #endif /* tskSTATIC_AND_DYNAMIC_ALLOCATION_POSSIBLE */
1238
1239             prvInitialiseNewTask( pxTaskCode, pcName, ulStackDepth, pvParameters, uxPriority, &xReturn, pxNewTCB, NULL );
1240
1241             #if ( ( configNUMBER_OF_CORES > 1 ) && ( configUSE_CORE_AFFINITY == 1 ) )
1242             {
1243                 /* Set the task's affinity before scheduling it. */
1244                 pxNewTCB->uxCoreAffinityMask = uxCoreAffinityMask;
1245             }
1246             #endif
1247
1248             prvAddNewTaskToReadyList( pxNewTCB );
1249         }
1250         else
1251         {
1252             xReturn = NULL;
1253         }
1254
1255         traceRETURN_xTaskCreateStatic( xReturn );
1256
1257         return xReturn;
1258     }
1259
1260 #endif /* SUPPORT_STATIC_ALLOCATION */
1261 /*-----------------------------------------------------------*/
1262
1263 #if ( ( portUSING_MPU_WRAPPERS == 1 ) && ( configSUPPORT_STATIC_ALLOCATION == 1 ) )
1264
1265     BaseType_t xTaskCreateRestrictedStatic( const TaskParameters_t * const pxTaskDefinition,
1266                                             TaskHandle_t * pxCreatedTask )
1267     #if ( ( configNUMBER_OF_CORES > 1 ) && ( configUSE_CORE_AFFINITY == 1 ) )
1268     {
1269         return xTaskCreateRestrictedStaticAffinitySet( pxTaskDefinition, tskNO_AFFINITY, pxCreatedTask );
1270     }
1271
1272     BaseType_t xTaskCreateRestrictedStaticAffinitySet( const TaskParameters_t * const pxTaskDefinition,
1273                                                        UBaseType_t uxCoreAffinityMask,
1274                                                        TaskHandle_t * pxCreatedTask )
1275     #endif /* #if ( ( configNUMBER_OF_CORES > 1 ) && ( configUSE_CORE_AFFINITY == 1 ) ) */
1276     {
1277         TCB_t * pxNewTCB;
1278         BaseType_t xReturn = errCOULD_NOT_ALLOCATE_REQUIRED_MEMORY;
1279
1280         traceENTER_xTaskCreateRestrictedStatic( pxTaskDefinition, pxCreatedTask );
1281
1282         configASSERT( pxTaskDefinition->puxStackBuffer != NULL );
1283         configASSERT( pxTaskDefinition->pxTaskBuffer != NULL );
1284
1285         if( ( pxTaskDefinition->puxStackBuffer != NULL ) && ( pxTaskDefinition->pxTaskBuffer != NULL ) )
1286         {
1287             /* Allocate space for the TCB.  Where the memory comes from depends
1288              * on the implementation of the port malloc function and whether or
1289              * not static allocation is being used. */
1290             pxNewTCB = ( TCB_t * ) pxTaskDefinition->pxTaskBuffer;
1291             ( void ) memset( ( void * ) pxNewTCB, 0x00, sizeof( TCB_t ) );
1292
1293             /* Store the stack location in the TCB. */
1294             pxNewTCB->pxStack = pxTaskDefinition->puxStackBuffer;
1295
1296             #if ( tskSTATIC_AND_DYNAMIC_ALLOCATION_POSSIBLE != 0 )
1297             {
1298                 /* Tasks can be created statically or dynamically, so note this
1299                  * task was created statically in case the task is later deleted. */
1300                 pxNewTCB->ucStaticallyAllocated = tskSTATICALLY_ALLOCATED_STACK_AND_TCB;
1301             }
1302             #endif /* tskSTATIC_AND_DYNAMIC_ALLOCATION_POSSIBLE */
1303
1304             prvInitialiseNewTask( pxTaskDefinition->pvTaskCode,
1305                                   pxTaskDefinition->pcName,
1306                                   ( uint32_t ) pxTaskDefinition->usStackDepth,
1307                                   pxTaskDefinition->pvParameters,
1308                                   pxTaskDefinition->uxPriority,
1309                                   pxCreatedTask, pxNewTCB,
1310                                   pxTaskDefinition->xRegions );
1311
1312             #if ( ( configNUMBER_OF_CORES > 1 ) && ( configUSE_CORE_AFFINITY == 1 ) )
1313             {
1314                 /* Set the task's affinity before scheduling it. */
1315                 pxNewTCB->uxCoreAffinityMask = uxCoreAffinityMask;
1316             }
1317             #endif
1318
1319             prvAddNewTaskToReadyList( pxNewTCB );
1320             xReturn = pdPASS;
1321         }
1322
1323         traceRETURN_xTaskCreateRestrictedStatic( xReturn );
1324
1325         return xReturn;
1326     }
1327
1328 #endif /* ( portUSING_MPU_WRAPPERS == 1 ) && ( configSUPPORT_STATIC_ALLOCATION == 1 ) */
1329 /*-----------------------------------------------------------*/
1330
1331 #if ( ( portUSING_MPU_WRAPPERS == 1 ) && ( configSUPPORT_DYNAMIC_ALLOCATION == 1 ) )
1332
1333     BaseType_t xTaskCreateRestricted( const TaskParameters_t * const pxTaskDefinition,
1334                                       TaskHandle_t * pxCreatedTask )
1335     #if ( ( configNUMBER_OF_CORES > 1 ) && ( configUSE_CORE_AFFINITY == 1 ) )
1336     {
1337         return xTaskCreateRestrictedAffinitySet( pxTaskDefinition, tskNO_AFFINITY, pxCreatedTask );
1338     }
1339
1340     BaseType_t xTaskCreateRestrictedAffinitySet( const TaskParameters_t * const pxTaskDefinition,
1341                                                  UBaseType_t uxCoreAffinityMask,
1342                                                  TaskHandle_t * pxCreatedTask )
1343     #endif /* #if ( ( configNUMBER_OF_CORES > 1 ) && ( configUSE_CORE_AFFINITY == 1 ) ) */
1344     {
1345         TCB_t * pxNewTCB;
1346         BaseType_t xReturn = errCOULD_NOT_ALLOCATE_REQUIRED_MEMORY;
1347
1348         traceENTER_xTaskCreateRestricted( pxTaskDefinition, pxCreatedTask );
1349
1350         configASSERT( pxTaskDefinition->puxStackBuffer );
1351
1352         if( pxTaskDefinition->puxStackBuffer != NULL )
1353         {
1354             pxNewTCB = ( TCB_t * ) pvPortMalloc( sizeof( TCB_t ) );
1355
1356             if( pxNewTCB != NULL )
1357             {
1358                 ( void ) memset( ( void * ) pxNewTCB, 0x00, sizeof( TCB_t ) );
1359
1360                 /* Store the stack location in the TCB. */
1361                 pxNewTCB->pxStack = pxTaskDefinition->puxStackBuffer;
1362
1363                 #if ( tskSTATIC_AND_DYNAMIC_ALLOCATION_POSSIBLE != 0 )
1364                 {
1365                     /* Tasks can be created statically or dynamically, so note
1366                      * this task had a statically allocated stack in case it is
1367                      * later deleted.  The TCB was allocated dynamically. */
1368                     pxNewTCB->ucStaticallyAllocated = tskSTATICALLY_ALLOCATED_STACK_ONLY;
1369                 }
1370                 #endif /* tskSTATIC_AND_DYNAMIC_ALLOCATION_POSSIBLE */
1371
1372                 prvInitialiseNewTask( pxTaskDefinition->pvTaskCode,
1373                                       pxTaskDefinition->pcName,
1374                                       ( uint32_t ) pxTaskDefinition->usStackDepth,
1375                                       pxTaskDefinition->pvParameters,
1376                                       pxTaskDefinition->uxPriority,
1377                                       pxCreatedTask, pxNewTCB,
1378                                       pxTaskDefinition->xRegions );
1379
1380                 #if ( ( configNUMBER_OF_CORES > 1 ) && ( configUSE_CORE_AFFINITY == 1 ) )
1381                 {
1382                     /* Set the task's affinity before scheduling it. */
1383                     pxNewTCB->uxCoreAffinityMask = uxCoreAffinityMask;
1384                 }
1385                 #endif
1386
1387                 prvAddNewTaskToReadyList( pxNewTCB );
1388                 xReturn = pdPASS;
1389             }
1390         }
1391
1392         traceRETURN_xTaskCreateRestricted( xReturn );
1393
1394         return xReturn;
1395     }
1396
1397 #endif /* portUSING_MPU_WRAPPERS */
1398 /*-----------------------------------------------------------*/
1399
1400 #if ( configSUPPORT_DYNAMIC_ALLOCATION == 1 )
1401
1402     BaseType_t xTaskCreate( TaskFunction_t pxTaskCode,
1403                             const char * const pcName, /*lint !e971 Unqualified char types are allowed for strings and single characters only. */
1404                             const configSTACK_DEPTH_TYPE usStackDepth,
1405                             void * const pvParameters,
1406                             UBaseType_t uxPriority,
1407                             TaskHandle_t * const pxCreatedTask )
1408     #if ( ( configNUMBER_OF_CORES > 1 ) && ( configUSE_CORE_AFFINITY == 1 ) )
1409     {
1410         return xTaskCreateAffinitySet( pxTaskCode, pcName, usStackDepth, pvParameters, uxPriority, tskNO_AFFINITY, pxCreatedTask );
1411     }
1412
1413     BaseType_t xTaskCreateAffinitySet( TaskFunction_t pxTaskCode,
1414                                        const char * const pcName, /*lint !e971 Unqualified char types are allowed for strings and single characters only. */
1415                                        const configSTACK_DEPTH_TYPE usStackDepth,
1416                                        void * const pvParameters,
1417                                        UBaseType_t uxPriority,
1418                                        UBaseType_t uxCoreAffinityMask,
1419                                        TaskHandle_t * const pxCreatedTask )
1420     #endif /* #if ( ( configNUMBER_OF_CORES > 1 ) && ( configUSE_CORE_AFFINITY == 1 ) ) */
1421     {
1422         TCB_t * pxNewTCB;
1423         BaseType_t xReturn;
1424
1425         traceENTER_xTaskCreate( pxTaskCode, pcName, usStackDepth, pvParameters, uxPriority, pxCreatedTask );
1426
1427         /* If the stack grows down then allocate the stack then the TCB so the stack
1428          * does not grow into the TCB.  Likewise if the stack grows up then allocate
1429          * the TCB then the stack. */
1430         #if ( portSTACK_GROWTH > 0 )
1431         {
1432             /* Allocate space for the TCB.  Where the memory comes from depends on
1433              * the implementation of the port malloc function and whether or not static
1434              * allocation is being used. */
1435             pxNewTCB = ( TCB_t * ) pvPortMalloc( sizeof( TCB_t ) );
1436
1437             if( pxNewTCB != NULL )
1438             {
1439                 ( void ) memset( ( void * ) pxNewTCB, 0x00, sizeof( TCB_t ) );
1440
1441                 /* Allocate space for the stack used by the task being created.
1442                  * The base of the stack memory stored in the TCB so the task can
1443                  * be deleted later if required. */
1444                 pxNewTCB->pxStack = ( StackType_t * ) pvPortMallocStack( ( ( ( size_t ) usStackDepth ) * sizeof( StackType_t ) ) ); /*lint !e961 MISRA exception as the casts are only redundant for some ports. */
1445
1446                 if( pxNewTCB->pxStack == NULL )
1447                 {
1448                     /* Could not allocate the stack.  Delete the allocated TCB. */
1449                     vPortFree( pxNewTCB );
1450                     pxNewTCB = NULL;
1451                 }
1452             }
1453         }
1454         #else /* portSTACK_GROWTH */
1455         {
1456             StackType_t * pxStack;
1457
1458             /* Allocate space for the stack used by the task being created. */
1459             pxStack = pvPortMallocStack( ( ( ( size_t ) usStackDepth ) * sizeof( StackType_t ) ) ); /*lint !e9079 All values returned by pvPortMalloc() have at least the alignment required by the MCU's stack and this allocation is the stack. */
1460
1461             if( pxStack != NULL )
1462             {
1463                 /* Allocate space for the TCB. */
1464                 pxNewTCB = ( TCB_t * ) pvPortMalloc( sizeof( TCB_t ) ); /*lint !e9087 !e9079 All values returned by pvPortMalloc() have at least the alignment required by the MCU's stack, and the first member of TCB_t is always a pointer to the task's stack. */
1465
1466                 if( pxNewTCB != NULL )
1467                 {
1468                     ( void ) memset( ( void * ) pxNewTCB, 0x00, sizeof( TCB_t ) );
1469
1470                     /* Store the stack location in the TCB. */
1471                     pxNewTCB->pxStack = pxStack;
1472                 }
1473                 else
1474                 {
1475                     /* The stack cannot be used as the TCB was not created.  Free
1476                      * it again. */
1477                     vPortFreeStack( pxStack );
1478                 }
1479             }
1480             else
1481             {
1482                 pxNewTCB = NULL;
1483             }
1484         }
1485         #endif /* portSTACK_GROWTH */
1486
1487         if( pxNewTCB != NULL )
1488         {
1489             #if ( tskSTATIC_AND_DYNAMIC_ALLOCATION_POSSIBLE != 0 ) /*lint !e9029 !e731 Macro has been consolidated for readability reasons. */
1490             {
1491                 /* Tasks can be created statically or dynamically, so note this
1492                  * task was created dynamically in case it is later deleted. */
1493                 pxNewTCB->ucStaticallyAllocated = tskDYNAMICALLY_ALLOCATED_STACK_AND_TCB;
1494             }
1495             #endif /* tskSTATIC_AND_DYNAMIC_ALLOCATION_POSSIBLE */
1496
1497             prvInitialiseNewTask( pxTaskCode, pcName, ( uint32_t ) usStackDepth, pvParameters, uxPriority, pxCreatedTask, pxNewTCB, NULL );
1498
1499             #if ( ( configNUMBER_OF_CORES > 1 ) && ( configUSE_CORE_AFFINITY == 1 ) )
1500             {
1501                 /* Set the task's affinity before scheduling it. */
1502                 pxNewTCB->uxCoreAffinityMask = uxCoreAffinityMask;
1503             }
1504             #endif
1505
1506             prvAddNewTaskToReadyList( pxNewTCB );
1507             xReturn = pdPASS;
1508         }
1509         else
1510         {
1511             xReturn = errCOULD_NOT_ALLOCATE_REQUIRED_MEMORY;
1512         }
1513
1514         traceRETURN_xTaskCreate( xReturn );
1515
1516         return xReturn;
1517     }
1518
1519 #endif /* configSUPPORT_DYNAMIC_ALLOCATION */
1520 /*-----------------------------------------------------------*/
1521
1522 static void prvInitialiseNewTask( TaskFunction_t pxTaskCode,
1523                                   const char * const pcName, /*lint !e971 Unqualified char types are allowed for strings and single characters only. */
1524                                   const uint32_t ulStackDepth,
1525                                   void * const pvParameters,
1526                                   UBaseType_t uxPriority,
1527                                   TaskHandle_t * const pxCreatedTask,
1528                                   TCB_t * pxNewTCB,
1529                                   const MemoryRegion_t * const xRegions )
1530 {
1531     StackType_t * pxTopOfStack;
1532     UBaseType_t x;
1533
1534     #if ( portUSING_MPU_WRAPPERS == 1 )
1535         /* Should the task be created in privileged mode? */
1536         BaseType_t xRunPrivileged;
1537
1538         if( ( uxPriority & portPRIVILEGE_BIT ) != 0U )
1539         {
1540             xRunPrivileged = pdTRUE;
1541         }
1542         else
1543         {
1544             xRunPrivileged = pdFALSE;
1545         }
1546         uxPriority &= ~portPRIVILEGE_BIT;
1547     #endif /* portUSING_MPU_WRAPPERS == 1 */
1548
1549     /* Avoid dependency on memset() if it is not required. */
1550     #if ( tskSET_NEW_STACKS_TO_KNOWN_VALUE == 1 )
1551     {
1552         /* Fill the stack with a known value to assist debugging. */
1553         ( void ) memset( pxNewTCB->pxStack, ( int ) tskSTACK_FILL_BYTE, ( size_t ) ulStackDepth * sizeof( StackType_t ) );
1554     }
1555     #endif /* tskSET_NEW_STACKS_TO_KNOWN_VALUE */
1556
1557     /* Calculate the top of stack address.  This depends on whether the stack
1558      * grows from high memory to low (as per the 80x86) or vice versa.
1559      * portSTACK_GROWTH is used to make the result positive or negative as required
1560      * by the port. */
1561     #if ( portSTACK_GROWTH < 0 )
1562     {
1563         pxTopOfStack = &( pxNewTCB->pxStack[ ulStackDepth - ( uint32_t ) 1 ] );
1564         pxTopOfStack = ( StackType_t * ) ( ( ( portPOINTER_SIZE_TYPE ) pxTopOfStack ) & ( ~( ( portPOINTER_SIZE_TYPE ) portBYTE_ALIGNMENT_MASK ) ) ); /*lint !e923 !e9033 !e9078 MISRA exception.  Avoiding casts between pointers and integers is not practical.  Size differences accounted for using portPOINTER_SIZE_TYPE type.  Checked by assert(). */
1565
1566         /* Check the alignment of the calculated top of stack is correct. */
1567         configASSERT( ( ( ( portPOINTER_SIZE_TYPE ) pxTopOfStack & ( portPOINTER_SIZE_TYPE ) portBYTE_ALIGNMENT_MASK ) == 0UL ) );
1568
1569         #if ( configRECORD_STACK_HIGH_ADDRESS == 1 )
1570         {
1571             /* Also record the stack's high address, which may assist
1572              * debugging. */
1573             pxNewTCB->pxEndOfStack = pxTopOfStack;
1574         }
1575         #endif /* configRECORD_STACK_HIGH_ADDRESS */
1576     }
1577     #else /* portSTACK_GROWTH */
1578     {
1579         pxTopOfStack = pxNewTCB->pxStack;
1580         pxTopOfStack = ( StackType_t * ) ( ( ( ( portPOINTER_SIZE_TYPE ) pxTopOfStack ) + portBYTE_ALIGNMENT_MASK ) & ( ~( ( portPOINTER_SIZE_TYPE ) portBYTE_ALIGNMENT_MASK ) ) ); /*lint !e923 !e9033 !e9078 MISRA exception.  Avoiding casts between pointers and integers is not practical.  Size differences accounted for using portPOINTER_SIZE_TYPE type.  Checked by assert(). */
1581
1582         /* Check the alignment of the calculated top of stack is correct. */
1583         configASSERT( ( ( ( portPOINTER_SIZE_TYPE ) pxTopOfStack & ( portPOINTER_SIZE_TYPE ) portBYTE_ALIGNMENT_MASK ) == 0UL ) );
1584
1585         /* The other extreme of the stack space is required if stack checking is
1586          * performed. */
1587         pxNewTCB->pxEndOfStack = pxNewTCB->pxStack + ( ulStackDepth - ( uint32_t ) 1 );
1588     }
1589     #endif /* portSTACK_GROWTH */
1590
1591     /* Store the task name in the TCB. */
1592     if( pcName != NULL )
1593     {
1594         for( x = ( UBaseType_t ) 0; x < ( UBaseType_t ) configMAX_TASK_NAME_LEN; x++ )
1595         {
1596             pxNewTCB->pcTaskName[ x ] = pcName[ x ];
1597
1598             /* Don't copy all configMAX_TASK_NAME_LEN if the string is shorter than
1599              * configMAX_TASK_NAME_LEN characters just in case the memory after the
1600              * string is not accessible (extremely unlikely). */
1601             if( pcName[ x ] == ( char ) 0x00 )
1602             {
1603                 break;
1604             }
1605             else
1606             {
1607                 mtCOVERAGE_TEST_MARKER();
1608             }
1609         }
1610
1611         /* Ensure the name string is terminated in the case that the string length
1612          * was greater or equal to configMAX_TASK_NAME_LEN. */
1613         pxNewTCB->pcTaskName[ configMAX_TASK_NAME_LEN - 1 ] = '\0';
1614     }
1615     else
1616     {
1617         mtCOVERAGE_TEST_MARKER();
1618     }
1619
1620     /* This is used as an array index so must ensure it's not too large. */
1621     configASSERT( uxPriority < configMAX_PRIORITIES );
1622
1623     if( uxPriority >= ( UBaseType_t ) configMAX_PRIORITIES )
1624     {
1625         uxPriority = ( UBaseType_t ) configMAX_PRIORITIES - ( UBaseType_t ) 1U;
1626     }
1627     else
1628     {
1629         mtCOVERAGE_TEST_MARKER();
1630     }
1631
1632     pxNewTCB->uxPriority = uxPriority;
1633     #if ( configUSE_MUTEXES == 1 )
1634     {
1635         pxNewTCB->uxBasePriority = uxPriority;
1636     }
1637     #endif /* configUSE_MUTEXES */
1638
1639     vListInitialiseItem( &( pxNewTCB->xStateListItem ) );
1640     vListInitialiseItem( &( pxNewTCB->xEventListItem ) );
1641
1642     /* Set the pxNewTCB as a link back from the ListItem_t.  This is so we can get
1643      * back to  the containing TCB from a generic item in a list. */
1644     listSET_LIST_ITEM_OWNER( &( pxNewTCB->xStateListItem ), pxNewTCB );
1645
1646     /* Event lists are always in priority order. */
1647     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. */
1648     listSET_LIST_ITEM_OWNER( &( pxNewTCB->xEventListItem ), pxNewTCB );
1649
1650     #if ( portUSING_MPU_WRAPPERS == 1 )
1651     {
1652         vPortStoreTaskMPUSettings( &( pxNewTCB->xMPUSettings ), xRegions, pxNewTCB->pxStack, ulStackDepth );
1653     }
1654     #else
1655     {
1656         /* Avoid compiler warning about unreferenced parameter. */
1657         ( void ) xRegions;
1658     }
1659     #endif
1660
1661     #if ( configUSE_C_RUNTIME_TLS_SUPPORT == 1 )
1662     {
1663         /* Allocate and initialize memory for the task's TLS Block. */
1664         configINIT_TLS_BLOCK( pxNewTCB->xTLSBlock, pxTopOfStack );
1665     }
1666     #endif
1667
1668     #if ( ( configNUMBER_OF_CORES > 1 ) && ( configUSE_CORE_AFFINITY == 1 ) )
1669     {
1670         pxNewTCB->uxCoreAffinityMask = tskNO_AFFINITY;
1671     }
1672     #endif
1673
1674     #if ( configUSE_TASK_PREEMPTION_DISABLE == 1 )
1675     {
1676         pxNewTCB->xPreemptionDisable = 0;
1677     }
1678     #endif
1679
1680     /* Initialize the TCB stack to look as if the task was already running,
1681      * but had been interrupted by the scheduler.  The return address is set
1682      * to the start of the task function. Once the stack has been initialised
1683      * the top of stack variable is updated. */
1684     #if ( portUSING_MPU_WRAPPERS == 1 )
1685     {
1686         /* If the port has capability to detect stack overflow,
1687          * pass the stack end address to the stack initialization
1688          * function as well. */
1689         #if ( portHAS_STACK_OVERFLOW_CHECKING == 1 )
1690         {
1691             #if ( portSTACK_GROWTH < 0 )
1692             {
1693                 pxNewTCB->pxTopOfStack = pxPortInitialiseStack( pxTopOfStack, pxNewTCB->pxStack, pxTaskCode, pvParameters, xRunPrivileged, &( pxNewTCB->xMPUSettings ) );
1694             }
1695             #else /* portSTACK_GROWTH */
1696             {
1697                 pxNewTCB->pxTopOfStack = pxPortInitialiseStack( pxTopOfStack, pxNewTCB->pxEndOfStack, pxTaskCode, pvParameters, xRunPrivileged, &( pxNewTCB->xMPUSettings ) );
1698             }
1699             #endif /* portSTACK_GROWTH */
1700         }
1701         #else /* portHAS_STACK_OVERFLOW_CHECKING */
1702         {
1703             pxNewTCB->pxTopOfStack = pxPortInitialiseStack( pxTopOfStack, pxTaskCode, pvParameters, xRunPrivileged, &( pxNewTCB->xMPUSettings ) );
1704         }
1705         #endif /* portHAS_STACK_OVERFLOW_CHECKING */
1706     }
1707     #else /* portUSING_MPU_WRAPPERS */
1708     {
1709         /* If the port has capability to detect stack overflow,
1710          * pass the stack end address to the stack initialization
1711          * function as well. */
1712         #if ( portHAS_STACK_OVERFLOW_CHECKING == 1 )
1713         {
1714             #if ( portSTACK_GROWTH < 0 )
1715             {
1716                 pxNewTCB->pxTopOfStack = pxPortInitialiseStack( pxTopOfStack, pxNewTCB->pxStack, pxTaskCode, pvParameters );
1717             }
1718             #else /* portSTACK_GROWTH */
1719             {
1720                 pxNewTCB->pxTopOfStack = pxPortInitialiseStack( pxTopOfStack, pxNewTCB->pxEndOfStack, pxTaskCode, pvParameters );
1721             }
1722             #endif /* portSTACK_GROWTH */
1723         }
1724         #else /* portHAS_STACK_OVERFLOW_CHECKING */
1725         {
1726             pxNewTCB->pxTopOfStack = pxPortInitialiseStack( pxTopOfStack, pxTaskCode, pvParameters );
1727         }
1728         #endif /* portHAS_STACK_OVERFLOW_CHECKING */
1729     }
1730     #endif /* portUSING_MPU_WRAPPERS */
1731
1732     /* Initialize task state and task attributes. */
1733     #if ( configNUMBER_OF_CORES > 1 )
1734     {
1735         pxNewTCB->xTaskRunState = taskTASK_NOT_RUNNING;
1736
1737         /* Is this an idle task? */
1738         if( ( ( TaskFunction_t ) pxTaskCode == ( TaskFunction_t ) prvIdleTask ) || ( ( TaskFunction_t ) pxTaskCode == ( TaskFunction_t ) prvMinimalIdleTask ) )
1739         {
1740             pxNewTCB->uxTaskAttributes |= taskATTRIBUTE_IS_IDLE;
1741         }
1742     }
1743     #endif /* #if ( configNUMBER_OF_CORES > 1 ) */
1744
1745     if( pxCreatedTask != NULL )
1746     {
1747         /* Pass the handle out in an anonymous way.  The handle can be used to
1748          * change the created task's priority, delete the created task, etc.*/
1749         *pxCreatedTask = ( TaskHandle_t ) pxNewTCB;
1750     }
1751     else
1752     {
1753         mtCOVERAGE_TEST_MARKER();
1754     }
1755 }
1756 /*-----------------------------------------------------------*/
1757
1758 #if ( configNUMBER_OF_CORES == 1 )
1759
1760     static void prvAddNewTaskToReadyList( TCB_t * pxNewTCB )
1761     {
1762         /* Ensure interrupts don't access the task lists while the lists are being
1763          * updated. */
1764         taskENTER_CRITICAL();
1765         {
1766             uxCurrentNumberOfTasks++;
1767
1768             if( pxCurrentTCB == NULL )
1769             {
1770                 /* There are no other tasks, or all the other tasks are in
1771                  * the suspended state - make this the current task. */
1772                 pxCurrentTCB = pxNewTCB;
1773
1774                 if( uxCurrentNumberOfTasks == ( UBaseType_t ) 1 )
1775                 {
1776                     /* This is the first task to be created so do the preliminary
1777                      * initialisation required.  We will not recover if this call
1778                      * fails, but we will report the failure. */
1779                     prvInitialiseTaskLists();
1780                 }
1781                 else
1782                 {
1783                     mtCOVERAGE_TEST_MARKER();
1784                 }
1785             }
1786             else
1787             {
1788                 /* If the scheduler is not already running, make this task the
1789                  * current task if it is the highest priority task to be created
1790                  * so far. */
1791                 if( xSchedulerRunning == pdFALSE )
1792                 {
1793                     if( pxCurrentTCB->uxPriority <= pxNewTCB->uxPriority )
1794                     {
1795                         pxCurrentTCB = pxNewTCB;
1796                     }
1797                     else
1798                     {
1799                         mtCOVERAGE_TEST_MARKER();
1800                     }
1801                 }
1802                 else
1803                 {
1804                     mtCOVERAGE_TEST_MARKER();
1805                 }
1806             }
1807
1808             uxTaskNumber++;
1809
1810             #if ( configUSE_TRACE_FACILITY == 1 )
1811             {
1812                 /* Add a counter into the TCB for tracing only. */
1813                 pxNewTCB->uxTCBNumber = uxTaskNumber;
1814             }
1815             #endif /* configUSE_TRACE_FACILITY */
1816             traceTASK_CREATE( pxNewTCB );
1817
1818             prvAddTaskToReadyList( pxNewTCB );
1819
1820             portSETUP_TCB( pxNewTCB );
1821         }
1822         taskEXIT_CRITICAL();
1823
1824         if( xSchedulerRunning != pdFALSE )
1825         {
1826             /* If the created task is of a higher priority than the current task
1827              * then it should run now. */
1828             taskYIELD_ANY_CORE_IF_USING_PREEMPTION( pxNewTCB );
1829         }
1830         else
1831         {
1832             mtCOVERAGE_TEST_MARKER();
1833         }
1834     }
1835
1836 #else /* #if ( configNUMBER_OF_CORES == 1 ) */
1837
1838     static void prvAddNewTaskToReadyList( TCB_t * pxNewTCB )
1839     {
1840         /* Ensure interrupts don't access the task lists while the lists are being
1841          * updated. */
1842         taskENTER_CRITICAL();
1843         {
1844             uxCurrentNumberOfTasks++;
1845
1846             if( xSchedulerRunning == pdFALSE )
1847             {
1848                 if( uxCurrentNumberOfTasks == ( UBaseType_t ) 1 )
1849                 {
1850                     /* This is the first task to be created so do the preliminary
1851                      * initialisation required.  We will not recover if this call
1852                      * fails, but we will report the failure. */
1853                     prvInitialiseTaskLists();
1854                 }
1855                 else
1856                 {
1857                     mtCOVERAGE_TEST_MARKER();
1858                 }
1859
1860                 if( ( pxNewTCB->uxTaskAttributes & taskATTRIBUTE_IS_IDLE ) != 0U )
1861                 {
1862                     BaseType_t xCoreID;
1863
1864                     /* Check if a core is free. */
1865                     for( xCoreID = ( BaseType_t ) 0; xCoreID < ( BaseType_t ) configNUMBER_OF_CORES; xCoreID++ )
1866                     {
1867                         if( pxCurrentTCBs[ xCoreID ] == NULL )
1868                         {
1869                             pxNewTCB->xTaskRunState = xCoreID;
1870                             pxCurrentTCBs[ xCoreID ] = pxNewTCB;
1871                             break;
1872                         }
1873                         else
1874                         {
1875                             mtCOVERAGE_TEST_MARKER();
1876                         }
1877                     }
1878                 }
1879                 else
1880                 {
1881                     mtCOVERAGE_TEST_MARKER();
1882                 }
1883             }
1884
1885             uxTaskNumber++;
1886
1887             #if ( configUSE_TRACE_FACILITY == 1 )
1888             {
1889                 /* Add a counter into the TCB for tracing only. */
1890                 pxNewTCB->uxTCBNumber = uxTaskNumber;
1891             }
1892             #endif /* configUSE_TRACE_FACILITY */
1893             traceTASK_CREATE( pxNewTCB );
1894
1895             prvAddTaskToReadyList( pxNewTCB );
1896
1897             portSETUP_TCB( pxNewTCB );
1898
1899             if( xSchedulerRunning != pdFALSE )
1900             {
1901                 /* If the created task is of a higher priority than another
1902                  * currently running task and preemption is on then it should
1903                  * run now. */
1904                 taskYIELD_ANY_CORE_IF_USING_PREEMPTION( pxNewTCB );
1905             }
1906             else
1907             {
1908                 mtCOVERAGE_TEST_MARKER();
1909             }
1910         }
1911         taskEXIT_CRITICAL();
1912     }
1913
1914 #endif /* #if ( configNUMBER_OF_CORES == 1 ) */
1915 /*-----------------------------------------------------------*/
1916
1917 #if ( INCLUDE_vTaskDelete == 1 )
1918
1919     void vTaskDelete( TaskHandle_t xTaskToDelete )
1920     {
1921         TCB_t * pxTCB;
1922
1923         traceENTER_vTaskDelete( xTaskToDelete );
1924
1925         taskENTER_CRITICAL();
1926         {
1927             /* If null is passed in here then it is the calling task that is
1928              * being deleted. */
1929             pxTCB = prvGetTCBFromHandle( xTaskToDelete );
1930
1931             /* Remove task from the ready/delayed list. */
1932             if( uxListRemove( &( pxTCB->xStateListItem ) ) == ( UBaseType_t ) 0 )
1933             {
1934                 taskRESET_READY_PRIORITY( pxTCB->uxPriority );
1935             }
1936             else
1937             {
1938                 mtCOVERAGE_TEST_MARKER();
1939             }
1940
1941             /* Is the task waiting on an event also? */
1942             if( listLIST_ITEM_CONTAINER( &( pxTCB->xEventListItem ) ) != NULL )
1943             {
1944                 ( void ) uxListRemove( &( pxTCB->xEventListItem ) );
1945             }
1946             else
1947             {
1948                 mtCOVERAGE_TEST_MARKER();
1949             }
1950
1951             /* Increment the uxTaskNumber also so kernel aware debuggers can
1952              * detect that the task lists need re-generating.  This is done before
1953              * portPRE_TASK_DELETE_HOOK() as in the Windows port that macro will
1954              * not return. */
1955             uxTaskNumber++;
1956
1957             /* If the task is running (or yielding), we must add it to the
1958              * termination list so that an idle task can delete it when it is
1959              * no longer running. */
1960             if( taskTASK_IS_RUNNING_OR_SCHEDULED_TO_YIELD( pxTCB ) != pdFALSE )
1961             {
1962                 /* A running task or a task which is scheduled to yield is being
1963                  * deleted. This cannot complete when the task is still running
1964                  * on a core, as a context switch to another task is required.
1965                  * Place the task in the termination list. The idle task will check
1966                  * the termination list and free up any memory allocated by the
1967                  * scheduler for the TCB and stack of the deleted task. */
1968                 vListInsertEnd( &xTasksWaitingTermination, &( pxTCB->xStateListItem ) );
1969
1970                 /* Increment the ucTasksDeleted variable so the idle task knows
1971                  * there is a task that has been deleted and that it should therefore
1972                  * check the xTasksWaitingTermination list. */
1973                 ++uxDeletedTasksWaitingCleanUp;
1974
1975                 /* Call the delete hook before portPRE_TASK_DELETE_HOOK() as
1976                  * portPRE_TASK_DELETE_HOOK() does not return in the Win32 port. */
1977                 traceTASK_DELETE( pxTCB );
1978
1979                 /* The pre-delete hook is primarily for the Windows simulator,
1980                  * in which Windows specific clean up operations are performed,
1981                  * after which it is not possible to yield away from this task -
1982                  * hence xYieldPending is used to latch that a context switch is
1983                  * required. */
1984                 #if ( configNUMBER_OF_CORES == 1 )
1985                     portPRE_TASK_DELETE_HOOK( pxTCB, &( xYieldPendings[ 0 ] ) );
1986                 #else
1987                     portPRE_TASK_DELETE_HOOK( pxTCB, &( xYieldPendings[ pxTCB->xTaskRunState ] ) );
1988                 #endif
1989             }
1990             else
1991             {
1992                 --uxCurrentNumberOfTasks;
1993                 traceTASK_DELETE( pxTCB );
1994
1995                 /* Reset the next expected unblock time in case it referred to
1996                  * the task that has just been deleted. */
1997                 prvResetNextTaskUnblockTime();
1998             }
1999         }
2000
2001         #if ( configNUMBER_OF_CORES == 1 )
2002         {
2003             taskEXIT_CRITICAL();
2004
2005             /* If the task is not deleting itself, call prvDeleteTCB from outside of
2006              * critical section. If a task deletes itself, prvDeleteTCB is called
2007              * from prvCheckTasksWaitingTermination which is called from Idle task. */
2008             if( pxTCB != pxCurrentTCB )
2009             {
2010                 prvDeleteTCB( pxTCB );
2011             }
2012
2013             /* Force a reschedule if it is the currently running task that has just
2014              * been deleted. */
2015             if( xSchedulerRunning != pdFALSE )
2016             {
2017                 if( pxTCB == pxCurrentTCB )
2018                 {
2019                     configASSERT( uxSchedulerSuspended == 0 );
2020                     portYIELD_WITHIN_API();
2021                 }
2022                 else
2023                 {
2024                     mtCOVERAGE_TEST_MARKER();
2025                 }
2026             }
2027         }
2028         #else /* #if ( configNUMBER_OF_CORES == 1 ) */
2029         {
2030             /* If a running task is not deleting itself, call prvDeleteTCB. If a running
2031              * task deletes itself, prvDeleteTCB is called from prvCheckTasksWaitingTermination
2032              * which is called from Idle task. */
2033             if( pxTCB->xTaskRunState == taskTASK_NOT_RUNNING )
2034             {
2035                 prvDeleteTCB( pxTCB );
2036             }
2037
2038             /* Force a reschedule if the task that has just been deleted was running. */
2039             if( ( xSchedulerRunning != pdFALSE ) && ( taskTASK_IS_RUNNING( pxTCB ) == pdTRUE ) )
2040             {
2041                 if( pxTCB->xTaskRunState == ( BaseType_t ) portGET_CORE_ID() )
2042                 {
2043                     configASSERT( uxSchedulerSuspended == 0 );
2044                     vTaskYieldWithinAPI();
2045                 }
2046                 else
2047                 {
2048                     prvYieldCore( pxTCB->xTaskRunState );
2049                 }
2050             }
2051
2052             taskEXIT_CRITICAL();
2053         }
2054         #endif /* #if ( configNUMBER_OF_CORES == 1 ) */
2055
2056         traceRETURN_vTaskDelete();
2057     }
2058
2059 #endif /* INCLUDE_vTaskDelete */
2060 /*-----------------------------------------------------------*/
2061
2062 #if ( INCLUDE_xTaskDelayUntil == 1 )
2063
2064     BaseType_t xTaskDelayUntil( TickType_t * const pxPreviousWakeTime,
2065                                 const TickType_t xTimeIncrement )
2066     {
2067         TickType_t xTimeToWake;
2068         BaseType_t xAlreadyYielded, xShouldDelay = pdFALSE;
2069
2070         traceENTER_xTaskDelayUntil( pxPreviousWakeTime, xTimeIncrement );
2071
2072         configASSERT( pxPreviousWakeTime );
2073         configASSERT( ( xTimeIncrement > 0U ) );
2074
2075         vTaskSuspendAll();
2076         {
2077             /* Minor optimisation.  The tick count cannot change in this
2078              * block. */
2079             const TickType_t xConstTickCount = xTickCount;
2080
2081             configASSERT( uxSchedulerSuspended == 1U );
2082
2083             /* Generate the tick time at which the task wants to wake. */
2084             xTimeToWake = *pxPreviousWakeTime + xTimeIncrement;
2085
2086             if( xConstTickCount < *pxPreviousWakeTime )
2087             {
2088                 /* The tick count has overflowed since this function was
2089                  * lasted called.  In this case the only time we should ever
2090                  * actually delay is if the wake time has also  overflowed,
2091                  * and the wake time is greater than the tick time.  When this
2092                  * is the case it is as if neither time had overflowed. */
2093                 if( ( xTimeToWake < *pxPreviousWakeTime ) && ( xTimeToWake > xConstTickCount ) )
2094                 {
2095                     xShouldDelay = pdTRUE;
2096                 }
2097                 else
2098                 {
2099                     mtCOVERAGE_TEST_MARKER();
2100                 }
2101             }
2102             else
2103             {
2104                 /* The tick time has not overflowed.  In this case we will
2105                  * delay if either the wake time has overflowed, and/or the
2106                  * tick time is less than the wake time. */
2107                 if( ( xTimeToWake < *pxPreviousWakeTime ) || ( xTimeToWake > xConstTickCount ) )
2108                 {
2109                     xShouldDelay = pdTRUE;
2110                 }
2111                 else
2112                 {
2113                     mtCOVERAGE_TEST_MARKER();
2114                 }
2115             }
2116
2117             /* Update the wake time ready for the next call. */
2118             *pxPreviousWakeTime = xTimeToWake;
2119
2120             if( xShouldDelay != pdFALSE )
2121             {
2122                 traceTASK_DELAY_UNTIL( xTimeToWake );
2123
2124                 /* prvAddCurrentTaskToDelayedList() needs the block time, not
2125                  * the time to wake, so subtract the current tick count. */
2126                 prvAddCurrentTaskToDelayedList( xTimeToWake - xConstTickCount, pdFALSE );
2127             }
2128             else
2129             {
2130                 mtCOVERAGE_TEST_MARKER();
2131             }
2132         }
2133         xAlreadyYielded = xTaskResumeAll();
2134
2135         /* Force a reschedule if xTaskResumeAll has not already done so, we may
2136          * have put ourselves to sleep. */
2137         if( xAlreadyYielded == pdFALSE )
2138         {
2139             #if ( configNUMBER_OF_CORES == 1 )
2140                 portYIELD_WITHIN_API();
2141             #else
2142                 vTaskYieldWithinAPI();
2143             #endif
2144         }
2145         else
2146         {
2147             mtCOVERAGE_TEST_MARKER();
2148         }
2149
2150         traceRETURN_xTaskDelayUntil( xShouldDelay );
2151
2152         return xShouldDelay;
2153     }
2154
2155 #endif /* INCLUDE_xTaskDelayUntil */
2156 /*-----------------------------------------------------------*/
2157
2158 #if ( INCLUDE_vTaskDelay == 1 )
2159
2160     void vTaskDelay( const TickType_t xTicksToDelay )
2161     {
2162         BaseType_t xAlreadyYielded = pdFALSE;
2163
2164         traceENTER_vTaskDelay( xTicksToDelay );
2165
2166         /* A delay time of zero just forces a reschedule. */
2167         if( xTicksToDelay > ( TickType_t ) 0U )
2168         {
2169             vTaskSuspendAll();
2170             {
2171                 configASSERT( uxSchedulerSuspended == 1U );
2172
2173                 traceTASK_DELAY();
2174
2175                 /* A task that is removed from the event list while the
2176                  * scheduler is suspended will not get placed in the ready
2177                  * list or removed from the blocked list until the scheduler
2178                  * is resumed.
2179                  *
2180                  * This task cannot be in an event list as it is the currently
2181                  * executing task. */
2182                 prvAddCurrentTaskToDelayedList( xTicksToDelay, pdFALSE );
2183             }
2184             xAlreadyYielded = xTaskResumeAll();
2185         }
2186         else
2187         {
2188             mtCOVERAGE_TEST_MARKER();
2189         }
2190
2191         /* Force a reschedule if xTaskResumeAll has not already done so, we may
2192          * have put ourselves to sleep. */
2193         if( xAlreadyYielded == pdFALSE )
2194         {
2195             #if ( configNUMBER_OF_CORES == 1 )
2196                 portYIELD_WITHIN_API();
2197             #else
2198                 vTaskYieldWithinAPI();
2199             #endif
2200         }
2201         else
2202         {
2203             mtCOVERAGE_TEST_MARKER();
2204         }
2205
2206         traceRETURN_vTaskDelay();
2207     }
2208
2209 #endif /* INCLUDE_vTaskDelay */
2210 /*-----------------------------------------------------------*/
2211
2212 #if ( ( INCLUDE_eTaskGetState == 1 ) || ( configUSE_TRACE_FACILITY == 1 ) || ( INCLUDE_xTaskAbortDelay == 1 ) )
2213
2214     eTaskState eTaskGetState( TaskHandle_t xTask )
2215     {
2216         eTaskState eReturn;
2217         List_t const * pxStateList;
2218         List_t const * pxEventList;
2219         List_t const * pxDelayedList;
2220         List_t const * pxOverflowedDelayedList;
2221         const TCB_t * const pxTCB = xTask;
2222
2223         traceENTER_eTaskGetState( xTask );
2224
2225         configASSERT( pxTCB );
2226
2227         #if ( configNUMBER_OF_CORES == 1 )
2228             if( pxTCB == pxCurrentTCB )
2229             {
2230                 /* The task calling this function is querying its own state. */
2231                 eReturn = eRunning;
2232             }
2233             else
2234         #endif
2235         {
2236             taskENTER_CRITICAL();
2237             {
2238                 pxStateList = listLIST_ITEM_CONTAINER( &( pxTCB->xStateListItem ) );
2239                 pxEventList = listLIST_ITEM_CONTAINER( &( pxTCB->xEventListItem ) );
2240                 pxDelayedList = pxDelayedTaskList;
2241                 pxOverflowedDelayedList = pxOverflowDelayedTaskList;
2242             }
2243             taskEXIT_CRITICAL();
2244
2245             if( pxEventList == &xPendingReadyList )
2246             {
2247                 /* The task has been placed on the pending ready list, so its
2248                  * state is eReady regardless of what list the task's state list
2249                  * item is currently placed on. */
2250                 eReturn = eReady;
2251             }
2252             else if( ( pxStateList == pxDelayedList ) || ( pxStateList == pxOverflowedDelayedList ) )
2253             {
2254                 /* The task being queried is referenced from one of the Blocked
2255                  * lists. */
2256                 eReturn = eBlocked;
2257             }
2258
2259             #if ( INCLUDE_vTaskSuspend == 1 )
2260                 else if( pxStateList == &xSuspendedTaskList )
2261                 {
2262                     /* The task being queried is referenced from the suspended
2263                      * list.  Is it genuinely suspended or is it blocked
2264                      * indefinitely? */
2265                     if( listLIST_ITEM_CONTAINER( &( pxTCB->xEventListItem ) ) == NULL )
2266                     {
2267                         #if ( configUSE_TASK_NOTIFICATIONS == 1 )
2268                         {
2269                             BaseType_t x;
2270
2271                             /* The task does not appear on the event list item of
2272                              * and of the RTOS objects, but could still be in the
2273                              * blocked state if it is waiting on its notification
2274                              * rather than waiting on an object.  If not, is
2275                              * suspended. */
2276                             eReturn = eSuspended;
2277
2278                             for( x = ( BaseType_t ) 0; x < ( BaseType_t ) configTASK_NOTIFICATION_ARRAY_ENTRIES; x++ )
2279                             {
2280                                 if( pxTCB->ucNotifyState[ x ] == taskWAITING_NOTIFICATION )
2281                                 {
2282                                     eReturn = eBlocked;
2283                                     break;
2284                                 }
2285                             }
2286                         }
2287                         #else /* if ( configUSE_TASK_NOTIFICATIONS == 1 ) */
2288                         {
2289                             eReturn = eSuspended;
2290                         }
2291                         #endif /* if ( configUSE_TASK_NOTIFICATIONS == 1 ) */
2292                     }
2293                     else
2294                     {
2295                         eReturn = eBlocked;
2296                     }
2297                 }
2298             #endif /* if ( INCLUDE_vTaskSuspend == 1 ) */
2299
2300             #if ( INCLUDE_vTaskDelete == 1 )
2301                 else if( ( pxStateList == &xTasksWaitingTermination ) || ( pxStateList == NULL ) )
2302                 {
2303                     /* The task being queried is referenced from the deleted
2304                      * tasks list, or it is not referenced from any lists at
2305                      * all. */
2306                     eReturn = eDeleted;
2307                 }
2308             #endif
2309
2310             else /*lint !e525 Negative indentation is intended to make use of pre-processor clearer. */
2311             {
2312                 #if ( configNUMBER_OF_CORES == 1 )
2313                 {
2314                     /* If the task is not in any other state, it must be in the
2315                      * Ready (including pending ready) state. */
2316                     eReturn = eReady;
2317                 }
2318                 #else /* #if ( configNUMBER_OF_CORES == 1 ) */
2319                 {
2320                     if( taskTASK_IS_RUNNING( pxTCB ) == pdTRUE )
2321                     {
2322                         /* Is it actively running on a core? */
2323                         eReturn = eRunning;
2324                     }
2325                     else
2326                     {
2327                         /* If the task is not in any other state, it must be in the
2328                          * Ready (including pending ready) state. */
2329                         eReturn = eReady;
2330                     }
2331                 }
2332                 #endif /* #if ( configNUMBER_OF_CORES == 1 ) */
2333             }
2334         }
2335
2336         traceRETURN_eTaskGetState( eReturn );
2337
2338         return eReturn;
2339     } /*lint !e818 xTask cannot be a pointer to const because it is a typedef. */
2340
2341 #endif /* INCLUDE_eTaskGetState */
2342 /*-----------------------------------------------------------*/
2343
2344 #if ( INCLUDE_uxTaskPriorityGet == 1 )
2345
2346     UBaseType_t uxTaskPriorityGet( const TaskHandle_t xTask )
2347     {
2348         TCB_t const * pxTCB;
2349         UBaseType_t uxReturn;
2350
2351         traceENTER_uxTaskPriorityGet( xTask );
2352
2353         taskENTER_CRITICAL();
2354         {
2355             /* If null is passed in here then it is the priority of the task
2356              * that called uxTaskPriorityGet() that is being queried. */
2357             pxTCB = prvGetTCBFromHandle( xTask );
2358             uxReturn = pxTCB->uxPriority;
2359         }
2360         taskEXIT_CRITICAL();
2361
2362         traceRETURN_uxTaskPriorityGet( uxReturn );
2363
2364         return uxReturn;
2365     }
2366
2367 #endif /* INCLUDE_uxTaskPriorityGet */
2368 /*-----------------------------------------------------------*/
2369
2370 #if ( INCLUDE_uxTaskPriorityGet == 1 )
2371
2372     UBaseType_t uxTaskPriorityGetFromISR( const TaskHandle_t xTask )
2373     {
2374         TCB_t const * pxTCB;
2375         UBaseType_t uxReturn;
2376         UBaseType_t uxSavedInterruptStatus;
2377
2378         traceENTER_uxTaskPriorityGetFromISR( xTask );
2379
2380         /* RTOS ports that support interrupt nesting have the concept of a
2381          * maximum  system call (or maximum API call) interrupt priority.
2382          * Interrupts that are  above the maximum system call priority are keep
2383          * permanently enabled, even when the RTOS kernel is in a critical section,
2384          * but cannot make any calls to FreeRTOS API functions.  If configASSERT()
2385          * is defined in FreeRTOSConfig.h then
2386          * portASSERT_IF_INTERRUPT_PRIORITY_INVALID() will result in an assertion
2387          * failure if a FreeRTOS API function is called from an interrupt that has
2388          * been assigned a priority above the configured maximum system call
2389          * priority.  Only FreeRTOS functions that end in FromISR can be called
2390          * from interrupts  that have been assigned a priority at or (logically)
2391          * below the maximum system call interrupt priority.  FreeRTOS maintains a
2392          * separate interrupt safe API to ensure interrupt entry is as fast and as
2393          * simple as possible.  More information (albeit Cortex-M specific) is
2394          * provided on the following link:
2395          * https://www.FreeRTOS.org/RTOS-Cortex-M3-M4.html */
2396         portASSERT_IF_INTERRUPT_PRIORITY_INVALID();
2397
2398         uxSavedInterruptStatus = taskENTER_CRITICAL_FROM_ISR();
2399         {
2400             /* If null is passed in here then it is the priority of the calling
2401              * task that is being queried. */
2402             pxTCB = prvGetTCBFromHandle( xTask );
2403             uxReturn = pxTCB->uxPriority;
2404         }
2405         taskEXIT_CRITICAL_FROM_ISR( uxSavedInterruptStatus );
2406
2407         traceRETURN_uxTaskPriorityGetFromISR( uxReturn );
2408
2409         return uxReturn;
2410     }
2411
2412 #endif /* INCLUDE_uxTaskPriorityGet */
2413 /*-----------------------------------------------------------*/
2414
2415 #if ( INCLUDE_vTaskPrioritySet == 1 )
2416
2417     void vTaskPrioritySet( TaskHandle_t xTask,
2418                            UBaseType_t uxNewPriority )
2419     {
2420         TCB_t * pxTCB;
2421         UBaseType_t uxCurrentBasePriority, uxPriorityUsedOnEntry;
2422         BaseType_t xYieldRequired = pdFALSE;
2423
2424         traceENTER_vTaskPrioritySet( xTask, uxNewPriority );
2425
2426         #if ( configNUMBER_OF_CORES > 1 )
2427             BaseType_t xYieldForTask = pdFALSE;
2428         #endif
2429
2430         configASSERT( uxNewPriority < configMAX_PRIORITIES );
2431
2432         /* Ensure the new priority is valid. */
2433         if( uxNewPriority >= ( UBaseType_t ) configMAX_PRIORITIES )
2434         {
2435             uxNewPriority = ( UBaseType_t ) configMAX_PRIORITIES - ( UBaseType_t ) 1U;
2436         }
2437         else
2438         {
2439             mtCOVERAGE_TEST_MARKER();
2440         }
2441
2442         taskENTER_CRITICAL();
2443         {
2444             /* If null is passed in here then it is the priority of the calling
2445              * task that is being changed. */
2446             pxTCB = prvGetTCBFromHandle( xTask );
2447
2448             traceTASK_PRIORITY_SET( pxTCB, uxNewPriority );
2449
2450             #if ( configUSE_MUTEXES == 1 )
2451             {
2452                 uxCurrentBasePriority = pxTCB->uxBasePriority;
2453             }
2454             #else
2455             {
2456                 uxCurrentBasePriority = pxTCB->uxPriority;
2457             }
2458             #endif
2459
2460             if( uxCurrentBasePriority != uxNewPriority )
2461             {
2462                 /* The priority change may have readied a task of higher
2463                  * priority than a running task. */
2464                 if( uxNewPriority > uxCurrentBasePriority )
2465                 {
2466                     #if ( configNUMBER_OF_CORES == 1 )
2467                     {
2468                         if( pxTCB != pxCurrentTCB )
2469                         {
2470                             /* The priority of a task other than the currently
2471                              * running task is being raised.  Is the priority being
2472                              * raised above that of the running task? */
2473                             if( uxNewPriority > pxCurrentTCB->uxPriority )
2474                             {
2475                                 xYieldRequired = pdTRUE;
2476                             }
2477                             else
2478                             {
2479                                 mtCOVERAGE_TEST_MARKER();
2480                             }
2481                         }
2482                         else
2483                         {
2484                             /* The priority of the running task is being raised,
2485                              * but the running task must already be the highest
2486                              * priority task able to run so no yield is required. */
2487                         }
2488                     }
2489                     #else /* #if ( configNUMBER_OF_CORES == 1 ) */
2490                     {
2491                         /* The priority of a task is being raised so
2492                          * perform a yield for this task later. */
2493                         xYieldForTask = pdTRUE;
2494                     }
2495                     #endif /* #if ( configNUMBER_OF_CORES == 1 ) */
2496                 }
2497                 else if( taskTASK_IS_RUNNING( pxTCB ) == pdTRUE )
2498                 {
2499                     /* Setting the priority of a running task down means
2500                      * there may now be another task of higher priority that
2501                      * is ready to execute. */
2502                     #if ( configUSE_TASK_PREEMPTION_DISABLE == 1 )
2503                         if( pxTCB->xPreemptionDisable == pdFALSE )
2504                     #endif
2505                     {
2506                         xYieldRequired = pdTRUE;
2507                     }
2508                 }
2509                 else
2510                 {
2511                     /* Setting the priority of any other task down does not
2512                      * require a yield as the running task must be above the
2513                      * new priority of the task being modified. */
2514                 }
2515
2516                 /* Remember the ready list the task might be referenced from
2517                  * before its uxPriority member is changed so the
2518                  * taskRESET_READY_PRIORITY() macro can function correctly. */
2519                 uxPriorityUsedOnEntry = pxTCB->uxPriority;
2520
2521                 #if ( configUSE_MUTEXES == 1 )
2522                 {
2523                     /* Only change the priority being used if the task is not
2524                      * currently using an inherited priority or the new priority
2525                      * is bigger than the inherited priority. */
2526                     if( ( pxTCB->uxBasePriority == pxTCB->uxPriority ) || ( uxNewPriority > pxTCB->uxPriority ) )
2527                     {
2528                         pxTCB->uxPriority = uxNewPriority;
2529                     }
2530                     else
2531                     {
2532                         mtCOVERAGE_TEST_MARKER();
2533                     }
2534
2535                     /* The base priority gets set whatever. */
2536                     pxTCB->uxBasePriority = uxNewPriority;
2537                 }
2538                 #else /* if ( configUSE_MUTEXES == 1 ) */
2539                 {
2540                     pxTCB->uxPriority = uxNewPriority;
2541                 }
2542                 #endif /* if ( configUSE_MUTEXES == 1 ) */
2543
2544                 /* Only reset the event list item value if the value is not
2545                  * being used for anything else. */
2546                 if( ( listGET_LIST_ITEM_VALUE( &( pxTCB->xEventListItem ) ) & taskEVENT_LIST_ITEM_VALUE_IN_USE ) == 0UL )
2547                 {
2548                     listSET_LIST_ITEM_VALUE( &( pxTCB->xEventListItem ), ( ( TickType_t ) configMAX_PRIORITIES - ( TickType_t ) uxNewPriority ) ); /*lint !e961 MISRA exception as the casts are only redundant for some ports. */
2549                 }
2550                 else
2551                 {
2552                     mtCOVERAGE_TEST_MARKER();
2553                 }
2554
2555                 /* If the task is in the blocked or suspended list we need do
2556                  * nothing more than change its priority variable. However, if
2557                  * the task is in a ready list it needs to be removed and placed
2558                  * in the list appropriate to its new priority. */
2559                 if( listIS_CONTAINED_WITHIN( &( pxReadyTasksLists[ uxPriorityUsedOnEntry ] ), &( pxTCB->xStateListItem ) ) != pdFALSE )
2560                 {
2561                     /* The task is currently in its ready list - remove before
2562                      * adding it to its new ready list.  As we are in a critical
2563                      * section we can do this even if the scheduler is suspended. */
2564                     if( uxListRemove( &( pxTCB->xStateListItem ) ) == ( UBaseType_t ) 0 )
2565                     {
2566                         /* It is known that the task is in its ready list so
2567                          * there is no need to check again and the port level
2568                          * reset macro can be called directly. */
2569                         portRESET_READY_PRIORITY( uxPriorityUsedOnEntry, uxTopReadyPriority );
2570                     }
2571                     else
2572                     {
2573                         mtCOVERAGE_TEST_MARKER();
2574                     }
2575
2576                     prvAddTaskToReadyList( pxTCB );
2577                 }
2578                 else
2579                 {
2580                     #if ( configNUMBER_OF_CORES == 1 )
2581                     {
2582                         mtCOVERAGE_TEST_MARKER();
2583                     }
2584                     #else
2585                     {
2586                         /* It's possible that xYieldForTask was already set to pdTRUE because
2587                          * its priority is being raised. However, since it is not in a ready list
2588                          * we don't actually need to yield for it. */
2589                         xYieldForTask = pdFALSE;
2590                     }
2591                     #endif
2592                 }
2593
2594                 if( xYieldRequired != pdFALSE )
2595                 {
2596                     /* The running task priority is set down. Request the task to yield. */
2597                     taskYIELD_TASK_CORE_IF_USING_PREEMPTION( pxTCB );
2598                 }
2599                 else
2600                 {
2601                     #if ( configNUMBER_OF_CORES > 1 )
2602                         if( xYieldForTask != pdFALSE )
2603                         {
2604                             /* The priority of the task is being raised. If a running
2605                              * task has priority lower than this task, it should yield
2606                              * for this task. */
2607                             taskYIELD_ANY_CORE_IF_USING_PREEMPTION( pxTCB );
2608                         }
2609                         else
2610                     #endif /* if ( configNUMBER_OF_CORES > 1 ) */
2611                     {
2612                         mtCOVERAGE_TEST_MARKER();
2613                     }
2614                 }
2615
2616                 /* Remove compiler warning about unused variables when the port
2617                  * optimised task selection is not being used. */
2618                 ( void ) uxPriorityUsedOnEntry;
2619             }
2620         }
2621         taskEXIT_CRITICAL();
2622
2623         traceRETURN_vTaskPrioritySet();
2624     }
2625
2626 #endif /* INCLUDE_vTaskPrioritySet */
2627 /*-----------------------------------------------------------*/
2628
2629 #if ( ( configNUMBER_OF_CORES > 1 ) && ( configUSE_CORE_AFFINITY == 1 ) )
2630     void vTaskCoreAffinitySet( const TaskHandle_t xTask,
2631                                UBaseType_t uxCoreAffinityMask )
2632     {
2633         TCB_t * pxTCB;
2634         BaseType_t xCoreID;
2635         UBaseType_t uxPrevCoreAffinityMask;
2636
2637         #if ( configUSE_PREEMPTION == 1 )
2638             UBaseType_t uxPrevNotAllowedCores;
2639         #endif
2640
2641         traceENTER_vTaskCoreAffinitySet( xTask, uxCoreAffinityMask );
2642
2643         taskENTER_CRITICAL();
2644         {
2645             pxTCB = prvGetTCBFromHandle( xTask );
2646
2647             uxPrevCoreAffinityMask = pxTCB->uxCoreAffinityMask;
2648             pxTCB->uxCoreAffinityMask = uxCoreAffinityMask;
2649
2650             if( xSchedulerRunning != pdFALSE )
2651             {
2652                 if( taskTASK_IS_RUNNING( pxTCB ) == pdTRUE )
2653                 {
2654                     xCoreID = ( BaseType_t ) pxTCB->xTaskRunState;
2655
2656                     /* If the task can no longer run on the core it was running,
2657                      * request the core to yield. */
2658                     if( ( uxCoreAffinityMask & ( ( UBaseType_t ) 1U << ( UBaseType_t ) xCoreID ) ) == 0U )
2659                     {
2660                         prvYieldCore( xCoreID );
2661                     }
2662                 }
2663                 else
2664                 {
2665                     #if ( configUSE_PREEMPTION == 1 )
2666                     {
2667                         /* Calculate the cores on which this task was not allowed to
2668                          * run previously. */
2669                         uxPrevNotAllowedCores = ( ~uxPrevCoreAffinityMask ) & ( ( 1U << configNUMBER_OF_CORES ) - 1U );
2670
2671                         /* Does the new core mask enables this task to run on any of the
2672                          * previously not allowed cores? If yes, check if this task can be
2673                          * scheduled on any of those cores. */
2674                         if( ( uxPrevNotAllowedCores & uxCoreAffinityMask ) != 0U )
2675                         {
2676                             prvYieldForTask( pxTCB );
2677                         }
2678                     }
2679                     #else /* #if( configUSE_PREEMPTION == 1 ) */
2680                     {
2681                         mtCOVERAGE_TEST_MARKER();
2682                     }
2683                     #endif /* #if( configUSE_PREEMPTION == 1 ) */
2684                 }
2685             }
2686         }
2687         taskEXIT_CRITICAL();
2688
2689         traceRETURN_vTaskCoreAffinitySet();
2690     }
2691 #endif /* #if ( ( configNUMBER_OF_CORES > 1 ) && ( configUSE_CORE_AFFINITY == 1 ) ) */
2692 /*-----------------------------------------------------------*/
2693
2694 #if ( ( configNUMBER_OF_CORES > 1 ) && ( configUSE_CORE_AFFINITY == 1 ) )
2695     UBaseType_t vTaskCoreAffinityGet( ConstTaskHandle_t xTask )
2696     {
2697         const TCB_t * pxTCB;
2698         UBaseType_t uxCoreAffinityMask;
2699
2700         traceENTER_vTaskCoreAffinityGet( xTask );
2701
2702         taskENTER_CRITICAL();
2703         {
2704             pxTCB = prvGetTCBFromHandle( xTask );
2705             uxCoreAffinityMask = pxTCB->uxCoreAffinityMask;
2706         }
2707         taskEXIT_CRITICAL();
2708
2709         traceRETURN_vTaskCoreAffinityGet( uxCoreAffinityMask );
2710
2711         return uxCoreAffinityMask;
2712     }
2713 #endif /* #if ( ( configNUMBER_OF_CORES > 1 ) && ( configUSE_CORE_AFFINITY == 1 ) ) */
2714
2715 /*-----------------------------------------------------------*/
2716
2717 #if ( configUSE_TASK_PREEMPTION_DISABLE == 1 )
2718
2719     void vTaskPreemptionDisable( const TaskHandle_t xTask )
2720     {
2721         TCB_t * pxTCB;
2722
2723         traceENTER_vTaskPreemptionDisable( xTask );
2724
2725         taskENTER_CRITICAL();
2726         {
2727             pxTCB = prvGetTCBFromHandle( xTask );
2728
2729             pxTCB->xPreemptionDisable = pdTRUE;
2730         }
2731         taskEXIT_CRITICAL();
2732
2733         traceRETURN_vTaskPreemptionDisable();
2734     }
2735
2736 #endif /* #if ( configUSE_TASK_PREEMPTION_DISABLE == 1 ) */
2737 /*-----------------------------------------------------------*/
2738
2739 #if ( configUSE_TASK_PREEMPTION_DISABLE == 1 )
2740
2741     void vTaskPreemptionEnable( const TaskHandle_t xTask )
2742     {
2743         TCB_t * pxTCB;
2744         BaseType_t xCoreID;
2745
2746         traceENTER_vTaskPreemptionEnable( xTask );
2747
2748         taskENTER_CRITICAL();
2749         {
2750             pxTCB = prvGetTCBFromHandle( xTask );
2751
2752             pxTCB->xPreemptionDisable = pdFALSE;
2753
2754             if( xSchedulerRunning != pdFALSE )
2755             {
2756                 if( taskTASK_IS_RUNNING( pxTCB ) == pdTRUE )
2757                 {
2758                     xCoreID = ( BaseType_t ) pxTCB->xTaskRunState;
2759                     prvYieldCore( xCoreID );
2760                 }
2761             }
2762         }
2763         taskEXIT_CRITICAL();
2764
2765         traceRETURN_vTaskPreemptionEnable();
2766     }
2767
2768 #endif /* #if ( configUSE_TASK_PREEMPTION_DISABLE == 1 ) */
2769 /*-----------------------------------------------------------*/
2770
2771 #if ( INCLUDE_vTaskSuspend == 1 )
2772
2773     void vTaskSuspend( TaskHandle_t xTaskToSuspend )
2774     {
2775         TCB_t * pxTCB;
2776
2777         #if ( configNUMBER_OF_CORES > 1 )
2778             BaseType_t xTaskRunningOnCore;
2779         #endif
2780
2781         traceENTER_vTaskSuspend( xTaskToSuspend );
2782
2783         taskENTER_CRITICAL();
2784         {
2785             /* If null is passed in here then it is the running task that is
2786              * being suspended. */
2787             pxTCB = prvGetTCBFromHandle( xTaskToSuspend );
2788
2789             traceTASK_SUSPEND( pxTCB );
2790
2791             #if ( configNUMBER_OF_CORES > 1 )
2792                 xTaskRunningOnCore = pxTCB->xTaskRunState;
2793             #endif
2794
2795             /* Remove task from the ready/delayed list and place in the
2796              * suspended list. */
2797             if( uxListRemove( &( pxTCB->xStateListItem ) ) == ( UBaseType_t ) 0 )
2798             {
2799                 taskRESET_READY_PRIORITY( pxTCB->uxPriority );
2800             }
2801             else
2802             {
2803                 mtCOVERAGE_TEST_MARKER();
2804             }
2805
2806             /* Is the task waiting on an event also? */
2807             if( listLIST_ITEM_CONTAINER( &( pxTCB->xEventListItem ) ) != NULL )
2808             {
2809                 ( void ) uxListRemove( &( pxTCB->xEventListItem ) );
2810             }
2811             else
2812             {
2813                 mtCOVERAGE_TEST_MARKER();
2814             }
2815
2816             vListInsertEnd( &xSuspendedTaskList, &( pxTCB->xStateListItem ) );
2817
2818             #if ( configUSE_TASK_NOTIFICATIONS == 1 )
2819             {
2820                 BaseType_t x;
2821
2822                 for( x = ( BaseType_t ) 0; x < ( BaseType_t ) configTASK_NOTIFICATION_ARRAY_ENTRIES; x++ )
2823                 {
2824                     if( pxTCB->ucNotifyState[ x ] == taskWAITING_NOTIFICATION )
2825                     {
2826                         /* The task was blocked to wait for a notification, but is
2827                          * now suspended, so no notification was received. */
2828                         pxTCB->ucNotifyState[ x ] = taskNOT_WAITING_NOTIFICATION;
2829                     }
2830                 }
2831             }
2832             #endif /* if ( configUSE_TASK_NOTIFICATIONS == 1 ) */
2833         }
2834
2835         #if ( configNUMBER_OF_CORES == 1 )
2836         {
2837             taskEXIT_CRITICAL();
2838
2839             if( xSchedulerRunning != pdFALSE )
2840             {
2841                 /* Reset the next expected unblock time in case it referred to the
2842                  * task that is now in the Suspended state. */
2843                 taskENTER_CRITICAL();
2844                 {
2845                     prvResetNextTaskUnblockTime();
2846                 }
2847                 taskEXIT_CRITICAL();
2848             }
2849             else
2850             {
2851                 mtCOVERAGE_TEST_MARKER();
2852             }
2853
2854             if( pxTCB == pxCurrentTCB )
2855             {
2856                 if( xSchedulerRunning != pdFALSE )
2857                 {
2858                     /* The current task has just been suspended. */
2859                     configASSERT( uxSchedulerSuspended == 0 );
2860                     portYIELD_WITHIN_API();
2861                 }
2862                 else
2863                 {
2864                     /* The scheduler is not running, but the task that was pointed
2865                      * to by pxCurrentTCB has just been suspended and pxCurrentTCB
2866                      * must be adjusted to point to a different task. */
2867                     if( listCURRENT_LIST_LENGTH( &xSuspendedTaskList ) == uxCurrentNumberOfTasks ) /*lint !e931 Right has no side effect, just volatile. */
2868                     {
2869                         /* No other tasks are ready, so set pxCurrentTCB back to
2870                          * NULL so when the next task is created pxCurrentTCB will
2871                          * be set to point to it no matter what its relative priority
2872                          * is. */
2873                         pxCurrentTCB = NULL;
2874                     }
2875                     else
2876                     {
2877                         vTaskSwitchContext();
2878                     }
2879                 }
2880             }
2881             else
2882             {
2883                 mtCOVERAGE_TEST_MARKER();
2884             }
2885         }
2886         #else /* #if ( configNUMBER_OF_CORES == 1 ) */
2887         {
2888             if( xSchedulerRunning != pdFALSE )
2889             {
2890                 /* Reset the next expected unblock time in case it referred to the
2891                  * task that is now in the Suspended state. */
2892                 prvResetNextTaskUnblockTime();
2893             }
2894             else
2895             {
2896                 mtCOVERAGE_TEST_MARKER();
2897             }
2898
2899             if( taskTASK_IS_RUNNING( pxTCB ) == pdTRUE )
2900             {
2901                 if( xSchedulerRunning != pdFALSE )
2902                 {
2903                     if( xTaskRunningOnCore == ( BaseType_t ) portGET_CORE_ID() )
2904                     {
2905                         /* The current task has just been suspended. */
2906                         configASSERT( uxSchedulerSuspended == 0 );
2907                         vTaskYieldWithinAPI();
2908                     }
2909                     else
2910                     {
2911                         prvYieldCore( xTaskRunningOnCore );
2912                     }
2913                 }
2914                 else
2915                 {
2916                     /* This code path is not possible because only Idle tasks are
2917                      * assigned a core before the scheduler is started ( i.e.
2918                      * taskTASK_IS_RUNNING is only true for idle tasks before
2919                      * the scheduler is started ) and idle tasks cannot be
2920                      * suspended. */
2921                     mtCOVERAGE_TEST_MARKER();
2922                 }
2923             }
2924             else
2925             {
2926                 mtCOVERAGE_TEST_MARKER();
2927             }
2928
2929             taskEXIT_CRITICAL();
2930         }
2931         #endif /* #if ( configNUMBER_OF_CORES == 1 ) */
2932
2933         traceRETURN_vTaskSuspend();
2934     }
2935
2936 #endif /* INCLUDE_vTaskSuspend */
2937 /*-----------------------------------------------------------*/
2938
2939 #if ( INCLUDE_vTaskSuspend == 1 )
2940
2941     static BaseType_t prvTaskIsTaskSuspended( const TaskHandle_t xTask )
2942     {
2943         BaseType_t xReturn = pdFALSE;
2944         const TCB_t * const pxTCB = xTask;
2945
2946         /* Accesses xPendingReadyList so must be called from a critical
2947          * section. */
2948
2949         /* It does not make sense to check if the calling task is suspended. */
2950         configASSERT( xTask );
2951
2952         /* Is the task being resumed actually in the suspended list? */
2953         if( listIS_CONTAINED_WITHIN( &xSuspendedTaskList, &( pxTCB->xStateListItem ) ) != pdFALSE )
2954         {
2955             /* Has the task already been resumed from within an ISR? */
2956             if( listIS_CONTAINED_WITHIN( &xPendingReadyList, &( pxTCB->xEventListItem ) ) == pdFALSE )
2957             {
2958                 /* Is it in the suspended list because it is in the Suspended
2959                  * state, or because is is blocked with no timeout? */
2960                 if( listIS_CONTAINED_WITHIN( NULL, &( pxTCB->xEventListItem ) ) != pdFALSE ) /*lint !e961.  The cast is only redundant when NULL is used. */
2961                 {
2962                     xReturn = pdTRUE;
2963                 }
2964                 else
2965                 {
2966                     mtCOVERAGE_TEST_MARKER();
2967                 }
2968             }
2969             else
2970             {
2971                 mtCOVERAGE_TEST_MARKER();
2972             }
2973         }
2974         else
2975         {
2976             mtCOVERAGE_TEST_MARKER();
2977         }
2978
2979         return xReturn;
2980     } /*lint !e818 xTask cannot be a pointer to const because it is a typedef. */
2981
2982 #endif /* INCLUDE_vTaskSuspend */
2983 /*-----------------------------------------------------------*/
2984
2985 #if ( INCLUDE_vTaskSuspend == 1 )
2986
2987     void vTaskResume( TaskHandle_t xTaskToResume )
2988     {
2989         TCB_t * const pxTCB = xTaskToResume;
2990
2991         traceENTER_vTaskResume( xTaskToResume );
2992
2993         /* It does not make sense to resume the calling task. */
2994         configASSERT( xTaskToResume );
2995
2996         #if ( configNUMBER_OF_CORES == 1 )
2997
2998             /* The parameter cannot be NULL as it is impossible to resume the
2999              * currently executing task. */
3000             if( ( pxTCB != pxCurrentTCB ) && ( pxTCB != NULL ) )
3001         #else
3002
3003             /* The parameter cannot be NULL as it is impossible to resume the
3004              * currently executing task. It is also impossible to resume a task
3005              * that is actively running on another core but it is not safe
3006              * to check their run state here. Therefore, we get into a critical
3007              * section and check if the task is actually suspended or not. */
3008             if( pxTCB != NULL )
3009         #endif
3010         {
3011             taskENTER_CRITICAL();
3012             {
3013                 if( prvTaskIsTaskSuspended( pxTCB ) != pdFALSE )
3014                 {
3015                     traceTASK_RESUME( pxTCB );
3016
3017                     /* The ready list can be accessed even if the scheduler is
3018                      * suspended because this is inside a critical section. */
3019                     ( void ) uxListRemove( &( pxTCB->xStateListItem ) );
3020                     prvAddTaskToReadyList( pxTCB );
3021
3022                     /* This yield may not cause the task just resumed to run,
3023                      * but will leave the lists in the correct state for the
3024                      * next yield. */
3025                     taskYIELD_ANY_CORE_IF_USING_PREEMPTION( pxTCB );
3026                 }
3027                 else
3028                 {
3029                     mtCOVERAGE_TEST_MARKER();
3030                 }
3031             }
3032             taskEXIT_CRITICAL();
3033         }
3034         else
3035         {
3036             mtCOVERAGE_TEST_MARKER();
3037         }
3038
3039         traceRETURN_vTaskResume();
3040     }
3041
3042 #endif /* INCLUDE_vTaskSuspend */
3043
3044 /*-----------------------------------------------------------*/
3045
3046 #if ( ( INCLUDE_xTaskResumeFromISR == 1 ) && ( INCLUDE_vTaskSuspend == 1 ) )
3047
3048     BaseType_t xTaskResumeFromISR( TaskHandle_t xTaskToResume )
3049     {
3050         BaseType_t xYieldRequired = pdFALSE;
3051         TCB_t * const pxTCB = xTaskToResume;
3052         UBaseType_t uxSavedInterruptStatus;
3053
3054         traceENTER_xTaskResumeFromISR( xTaskToResume );
3055
3056         configASSERT( xTaskToResume );
3057
3058         /* RTOS ports that support interrupt nesting have the concept of a
3059          * maximum  system call (or maximum API call) interrupt priority.
3060          * Interrupts that are  above the maximum system call priority are keep
3061          * permanently enabled, even when the RTOS kernel is in a critical section,
3062          * but cannot make any calls to FreeRTOS API functions.  If configASSERT()
3063          * is defined in FreeRTOSConfig.h then
3064          * portASSERT_IF_INTERRUPT_PRIORITY_INVALID() will result in an assertion
3065          * failure if a FreeRTOS API function is called from an interrupt that has
3066          * been assigned a priority above the configured maximum system call
3067          * priority.  Only FreeRTOS functions that end in FromISR can be called
3068          * from interrupts  that have been assigned a priority at or (logically)
3069          * below the maximum system call interrupt priority.  FreeRTOS maintains a
3070          * separate interrupt safe API to ensure interrupt entry is as fast and as
3071          * simple as possible.  More information (albeit Cortex-M specific) is
3072          * provided on the following link:
3073          * https://www.FreeRTOS.org/RTOS-Cortex-M3-M4.html */
3074         portASSERT_IF_INTERRUPT_PRIORITY_INVALID();
3075
3076         uxSavedInterruptStatus = taskENTER_CRITICAL_FROM_ISR();
3077         {
3078             if( prvTaskIsTaskSuspended( pxTCB ) != pdFALSE )
3079             {
3080                 traceTASK_RESUME_FROM_ISR( pxTCB );
3081
3082                 /* Check the ready lists can be accessed. */
3083                 if( uxSchedulerSuspended == ( UBaseType_t ) 0U )
3084                 {
3085                     #if ( configNUMBER_OF_CORES == 1 )
3086                     {
3087                         /* Ready lists can be accessed so move the task from the
3088                          * suspended list to the ready list directly. */
3089                         if( pxTCB->uxPriority > pxCurrentTCB->uxPriority )
3090                         {
3091                             xYieldRequired = pdTRUE;
3092
3093                             /* Mark that a yield is pending in case the user is not
3094                              * using the return value to initiate a context switch
3095                              * from the ISR using portYIELD_FROM_ISR. */
3096                             xYieldPendings[ 0 ] = pdTRUE;
3097                         }
3098                         else
3099                         {
3100                             mtCOVERAGE_TEST_MARKER();
3101                         }
3102                     }
3103                     #endif /* #if ( configNUMBER_OF_CORES == 1 ) */
3104
3105                     ( void ) uxListRemove( &( pxTCB->xStateListItem ) );
3106                     prvAddTaskToReadyList( pxTCB );
3107                 }
3108                 else
3109                 {
3110                     /* The delayed or ready lists cannot be accessed so the task
3111                      * is held in the pending ready list until the scheduler is
3112                      * unsuspended. */
3113                     vListInsertEnd( &( xPendingReadyList ), &( pxTCB->xEventListItem ) );
3114                 }
3115
3116                 #if ( ( configNUMBER_OF_CORES > 1 ) && ( configUSE_PREEMPTION == 1 ) )
3117                 {
3118                     prvYieldForTask( pxTCB );
3119
3120                     if( xYieldPendings[ portGET_CORE_ID() ] != pdFALSE )
3121                     {
3122                         xYieldRequired = pdTRUE;
3123                     }
3124                 }
3125                 #endif /* #if ( ( configNUMBER_OF_CORES > 1 ) && ( configUSE_PREEMPTION == 1 ) ) */
3126             }
3127             else
3128             {
3129                 mtCOVERAGE_TEST_MARKER();
3130             }
3131         }
3132         taskEXIT_CRITICAL_FROM_ISR( uxSavedInterruptStatus );
3133
3134         traceRETURN_xTaskResumeFromISR( xYieldRequired );
3135
3136         return xYieldRequired;
3137     }
3138
3139 #endif /* ( ( INCLUDE_xTaskResumeFromISR == 1 ) && ( INCLUDE_vTaskSuspend == 1 ) ) */
3140 /*-----------------------------------------------------------*/
3141
3142 static BaseType_t prvCreateIdleTasks( void )
3143 {
3144     BaseType_t xReturn = pdPASS;
3145
3146     #if ( configNUMBER_OF_CORES == 1 )
3147     {
3148         /* Add the idle task at the lowest priority. */
3149         #if ( configSUPPORT_STATIC_ALLOCATION == 1 )
3150         {
3151             StaticTask_t * pxIdleTaskTCBBuffer = NULL;
3152             StackType_t * pxIdleTaskStackBuffer = NULL;
3153             uint32_t ulIdleTaskStackSize;
3154
3155             /* The Idle task is created using user provided RAM - obtain the
3156              * address of the RAM then create the idle task. */
3157             vApplicationGetIdleTaskMemory( &pxIdleTaskTCBBuffer, &pxIdleTaskStackBuffer, &ulIdleTaskStackSize );
3158             xIdleTaskHandles[ 0 ] = xTaskCreateStatic( prvIdleTask,
3159                                                        configIDLE_TASK_NAME,
3160                                                        ulIdleTaskStackSize,
3161                                                        ( void * ) NULL,       /*lint !e961.  The cast is not redundant for all compilers. */
3162                                                        portPRIVILEGE_BIT,     /* In effect ( tskIDLE_PRIORITY | portPRIVILEGE_BIT ), but tskIDLE_PRIORITY is zero. */
3163                                                        pxIdleTaskStackBuffer,
3164                                                        pxIdleTaskTCBBuffer ); /*lint !e961 MISRA exception, justified as it is not a redundant explicit cast to all supported compilers. */
3165
3166             if( xIdleTaskHandles[ 0 ] != NULL )
3167             {
3168                 xReturn = pdPASS;
3169             }
3170             else
3171             {
3172                 xReturn = pdFAIL;
3173             }
3174         }
3175         #else /* if ( configSUPPORT_STATIC_ALLOCATION == 1 ) */
3176         {
3177             /* The Idle task is being created using dynamically allocated RAM. */
3178             xReturn = xTaskCreate( prvIdleTask,
3179                                    configIDLE_TASK_NAME,
3180                                    configMINIMAL_STACK_SIZE,
3181                                    ( void * ) NULL,
3182                                    portPRIVILEGE_BIT,        /* In effect ( tskIDLE_PRIORITY | portPRIVILEGE_BIT ), but tskIDLE_PRIORITY is zero. */
3183                                    &xIdleTaskHandles[ 0 ] ); /*lint !e961 MISRA exception, justified as it is not a redundant explicit cast to all supported compilers. */
3184         }
3185         #endif /* configSUPPORT_STATIC_ALLOCATION */
3186     }
3187     #else /* #if ( configNUMBER_OF_CORES == 1 ) */
3188     {
3189         BaseType_t xCoreID;
3190         char cIdleName[ configMAX_TASK_NAME_LEN ];
3191
3192         /* Add each idle task at the lowest priority. */
3193         for( xCoreID = ( BaseType_t ) 0; xCoreID < ( BaseType_t ) configNUMBER_OF_CORES; xCoreID++ )
3194         {
3195             BaseType_t x;
3196
3197             if( xReturn == pdFAIL )
3198             {
3199                 break;
3200             }
3201             else
3202             {
3203                 mtCOVERAGE_TEST_MARKER();
3204             }
3205
3206             for( x = ( BaseType_t ) 0; x < ( BaseType_t ) configMAX_TASK_NAME_LEN; x++ )
3207             {
3208                 cIdleName[ x ] = configIDLE_TASK_NAME[ x ];
3209
3210                 /* Don't copy all configMAX_TASK_NAME_LEN if the string is shorter than
3211                  * configMAX_TASK_NAME_LEN characters just in case the memory after the
3212                  * string is not accessible (extremely unlikely). */
3213                 if( cIdleName[ x ] == ( char ) 0x00 )
3214                 {
3215                     break;
3216                 }
3217                 else
3218                 {
3219                     mtCOVERAGE_TEST_MARKER();
3220                 }
3221             }
3222
3223             /* Append the idle task number to the end of the name if there is space. */
3224             if( x < ( BaseType_t ) configMAX_TASK_NAME_LEN )
3225             {
3226                 cIdleName[ x ] = ( char ) ( xCoreID + '0' );
3227                 x++;
3228
3229                 /* And append a null character if there is space. */
3230                 if( x < ( BaseType_t ) configMAX_TASK_NAME_LEN )
3231                 {
3232                     cIdleName[ x ] = '\0';
3233                 }
3234                 else
3235                 {
3236                     mtCOVERAGE_TEST_MARKER();
3237                 }
3238             }
3239             else
3240             {
3241                 mtCOVERAGE_TEST_MARKER();
3242             }
3243
3244             #if ( configSUPPORT_STATIC_ALLOCATION == 1 )
3245             {
3246                 if( xCoreID == 0 )
3247                 {
3248                     StaticTask_t * pxIdleTaskTCBBuffer = NULL;
3249                     StackType_t * pxIdleTaskStackBuffer = NULL;
3250                     uint32_t ulIdleTaskStackSize;
3251
3252                     /* The Idle task is created using user provided RAM - obtain the
3253                      * address of the RAM then create the idle task. */
3254                     vApplicationGetIdleTaskMemory( &pxIdleTaskTCBBuffer, &pxIdleTaskStackBuffer, &ulIdleTaskStackSize );
3255                     xIdleTaskHandles[ xCoreID ] = xTaskCreateStatic( prvIdleTask,
3256                                                                      cIdleName,
3257                                                                      ulIdleTaskStackSize,
3258                                                                      ( void * ) NULL,       /*lint !e961.  The cast is not redundant for all compilers. */
3259                                                                      portPRIVILEGE_BIT,     /* In effect ( tskIDLE_PRIORITY | portPRIVILEGE_BIT ), but tskIDLE_PRIORITY is zero. */
3260                                                                      pxIdleTaskStackBuffer,
3261                                                                      pxIdleTaskTCBBuffer ); /*lint !e961 MISRA exception, justified as it is not a redundant explicit cast to all supported compilers. */
3262                 }
3263                 else
3264                 {
3265                     xIdleTaskHandles[ xCoreID ] = xTaskCreateStatic( prvMinimalIdleTask,
3266                                                                      cIdleName,
3267                                                                      configMINIMAL_STACK_SIZE,
3268                                                                      ( void * ) NULL,                   /*lint !e961.  The cast is not redundant for all compilers. */
3269                                                                      portPRIVILEGE_BIT,                 /* In effect ( tskIDLE_PRIORITY | portPRIVILEGE_BIT ), but tskIDLE_PRIORITY is zero. */
3270                                                                      xIdleTaskStackBuffers[ xCoreID - 1 ],
3271                                                                      &xIdleTCBBuffers[ xCoreID - 1 ] ); /*lint !e961 MISRA exception, justified as it is not a redundant explicit cast to all supported compilers. */
3272                 }
3273
3274                 if( xIdleTaskHandles[ xCoreID ] != NULL )
3275                 {
3276                     xReturn = pdPASS;
3277                 }
3278                 else
3279                 {
3280                     xReturn = pdFAIL;
3281                 }
3282             }
3283             #else /* if ( configSUPPORT_STATIC_ALLOCATION == 1 ) */
3284             {
3285                 if( xCoreID == 0 )
3286                 {
3287                     /* The Idle task is being created using dynamically allocated RAM. */
3288                     xReturn = xTaskCreate( prvIdleTask,
3289                                            cIdleName,
3290                                            configMINIMAL_STACK_SIZE,
3291                                            ( void * ) NULL,
3292                                            portPRIVILEGE_BIT,              /* In effect ( tskIDLE_PRIORITY | portPRIVILEGE_BIT ), but tskIDLE_PRIORITY is zero. */
3293                                            &xIdleTaskHandles[ xCoreID ] ); /*lint !e961 MISRA exception, justified as it is not a redundant explicit cast to all supported compilers. */
3294                 }
3295                 else
3296                 {
3297                     xReturn = xTaskCreate( prvMinimalIdleTask,
3298                                            cIdleName,
3299                                            configMINIMAL_STACK_SIZE,
3300                                            ( void * ) NULL,
3301                                            portPRIVILEGE_BIT,              /* In effect ( tskIDLE_PRIORITY | portPRIVILEGE_BIT ), but tskIDLE_PRIORITY is zero. */
3302                                            &xIdleTaskHandles[ xCoreID ] ); /*lint !e961 MISRA exception, justified as it is not a redundant explicit cast to all supported compilers. */
3303                 }
3304             }
3305             #endif /* configSUPPORT_STATIC_ALLOCATION */
3306         }
3307     }
3308     #endif /* #if ( configNUMBER_OF_CORES == 1 ) */
3309
3310     return xReturn;
3311 }
3312
3313 /*-----------------------------------------------------------*/
3314
3315 void vTaskStartScheduler( void )
3316 {
3317     BaseType_t xReturn;
3318
3319     traceENTER_vTaskStartScheduler();
3320
3321     #if ( configUSE_CORE_AFFINITY == 1 ) && ( configNUMBER_OF_CORES > 1 )
3322     {
3323         /* Sanity check that the UBaseType_t must have greater than or equal to
3324          * the number of bits as confNUMBER_OF_CORES. */
3325         configASSERT( ( sizeof( UBaseType_t ) * taskBITS_PER_BYTE ) >= configNUMBER_OF_CORES );
3326     }
3327     #endif /* #if ( configUSE_CORE_AFFINITY == 1 ) && ( configNUMBER_OF_CORES > 1 ) */
3328
3329     xReturn = prvCreateIdleTasks();
3330
3331     #if ( configUSE_TIMERS == 1 )
3332     {
3333         if( xReturn == pdPASS )
3334         {
3335             xReturn = xTimerCreateTimerTask();
3336         }
3337         else
3338         {
3339             mtCOVERAGE_TEST_MARKER();
3340         }
3341     }
3342     #endif /* configUSE_TIMERS */
3343
3344     if( xReturn == pdPASS )
3345     {
3346         /* freertos_tasks_c_additions_init() should only be called if the user
3347          * definable macro FREERTOS_TASKS_C_ADDITIONS_INIT() is defined, as that is
3348          * the only macro called by the function. */
3349         #ifdef FREERTOS_TASKS_C_ADDITIONS_INIT
3350         {
3351             freertos_tasks_c_additions_init();
3352         }
3353         #endif
3354
3355         /* Interrupts are turned off here, to ensure a tick does not occur
3356          * before or during the call to xPortStartScheduler().  The stacks of
3357          * the created tasks contain a status word with interrupts switched on
3358          * so interrupts will automatically get re-enabled when the first task
3359          * starts to run. */
3360         portDISABLE_INTERRUPTS();
3361
3362         #if ( configUSE_C_RUNTIME_TLS_SUPPORT == 1 )
3363         {
3364             /* Switch C-Runtime's TLS Block to point to the TLS
3365              * block specific to the task that will run first. */
3366             configSET_TLS_BLOCK( pxCurrentTCB->xTLSBlock );
3367         }
3368         #endif
3369
3370         xNextTaskUnblockTime = portMAX_DELAY;
3371         xSchedulerRunning = pdTRUE;
3372         xTickCount = ( TickType_t ) configINITIAL_TICK_COUNT;
3373
3374         /* If configGENERATE_RUN_TIME_STATS is defined then the following
3375          * macro must be defined to configure the timer/counter used to generate
3376          * the run time counter time base.   NOTE:  If configGENERATE_RUN_TIME_STATS
3377          * is set to 0 and the following line fails to build then ensure you do not
3378          * have portCONFIGURE_TIMER_FOR_RUN_TIME_STATS() defined in your
3379          * FreeRTOSConfig.h file. */
3380         portCONFIGURE_TIMER_FOR_RUN_TIME_STATS();
3381
3382         traceTASK_SWITCHED_IN();
3383
3384         /* Setting up the timer tick is hardware specific and thus in the
3385          * portable interface. */
3386         xPortStartScheduler();
3387
3388         /* In most cases, xPortStartScheduler() will not return. If it
3389          * returns pdTRUE then there was not enough heap memory available
3390          * to create either the Idle or the Timer task. If it returned
3391          * pdFALSE, then the application called xTaskEndScheduler().
3392          * Most ports don't implement xTaskEndScheduler() as there is
3393          * nothing to return to. */
3394     }
3395     else
3396     {
3397         /* This line will only be reached if the kernel could not be started,
3398          * because there was not enough FreeRTOS heap to create the idle task
3399          * or the timer task. */
3400         configASSERT( xReturn != errCOULD_NOT_ALLOCATE_REQUIRED_MEMORY );
3401     }
3402
3403     /* Prevent compiler warnings if INCLUDE_xTaskGetIdleTaskHandle is set to 0,
3404      * meaning xIdleTaskHandles are not used anywhere else. */
3405     ( void ) xIdleTaskHandles;
3406
3407     /* OpenOCD makes use of uxTopUsedPriority for thread debugging. Prevent uxTopUsedPriority
3408      * from getting optimized out as it is no longer used by the kernel. */
3409     ( void ) uxTopUsedPriority;
3410
3411     traceRETURN_vTaskStartScheduler();
3412 }
3413 /*-----------------------------------------------------------*/
3414
3415 void vTaskEndScheduler( void )
3416 {
3417     traceENTER_vTaskEndScheduler();
3418
3419     /* Stop the scheduler interrupts and call the portable scheduler end
3420      * routine so the original ISRs can be restored if necessary.  The port
3421      * layer must ensure interrupts enable  bit is left in the correct state. */
3422     portDISABLE_INTERRUPTS();
3423     xSchedulerRunning = pdFALSE;
3424     vPortEndScheduler();
3425
3426     traceRETURN_vTaskEndScheduler();
3427 }
3428 /*----------------------------------------------------------*/
3429
3430 void vTaskSuspendAll( void )
3431 {
3432     traceENTER_vTaskSuspendAll();
3433
3434     #if ( configNUMBER_OF_CORES == 1 )
3435     {
3436         /* A critical section is not required as the variable is of type
3437          * BaseType_t.  Please read Richard Barry's reply in the following link to a
3438          * post in the FreeRTOS support forum before reporting this as a bug! -
3439          * https://goo.gl/wu4acr */
3440
3441         /* portSOFTWARE_BARRIER() is only implemented for emulated/simulated ports that
3442          * do not otherwise exhibit real time behaviour. */
3443         portSOFTWARE_BARRIER();
3444
3445         /* The scheduler is suspended if uxSchedulerSuspended is non-zero.  An increment
3446          * is used to allow calls to vTaskSuspendAll() to nest. */
3447         ++uxSchedulerSuspended;
3448
3449         /* Enforces ordering for ports and optimised compilers that may otherwise place
3450          * the above increment elsewhere. */
3451         portMEMORY_BARRIER();
3452     }
3453     #else /* #if ( configNUMBER_OF_CORES == 1 ) */
3454     {
3455         UBaseType_t ulState;
3456
3457         /* This must only be called from within a task. */
3458         portASSERT_IF_IN_ISR();
3459
3460         if( xSchedulerRunning != pdFALSE )
3461         {
3462             /* Writes to uxSchedulerSuspended must be protected by both the task AND ISR locks.
3463              * We must disable interrupts before we grab the locks in the event that this task is
3464              * interrupted and switches context before incrementing uxSchedulerSuspended.
3465              * It is safe to re-enable interrupts after releasing the ISR lock and incrementing
3466              * uxSchedulerSuspended since that will prevent context switches. */
3467             ulState = portSET_INTERRUPT_MASK();
3468
3469             /* portSOFRWARE_BARRIER() is only implemented for emulated/simulated ports that
3470              * do not otherwise exhibit real time behaviour. */
3471             portSOFTWARE_BARRIER();
3472
3473             portGET_TASK_LOCK();
3474
3475             /* uxSchedulerSuspended is increased after prvCheckForRunStateChange. The
3476              * purpose is to prevent altering the variable when fromISR APIs are readying
3477              * it. */
3478             if( uxSchedulerSuspended == 0U )
3479             {
3480                 if( portGET_CRITICAL_NESTING_COUNT() == 0U )
3481                 {
3482                     prvCheckForRunStateChange();
3483                 }
3484                 else
3485                 {
3486                     mtCOVERAGE_TEST_MARKER();
3487                 }
3488             }
3489             else
3490             {
3491                 mtCOVERAGE_TEST_MARKER();
3492             }
3493
3494             portGET_ISR_LOCK();
3495
3496             /* The scheduler is suspended if uxSchedulerSuspended is non-zero. An increment
3497              * is used to allow calls to vTaskSuspendAll() to nest. */
3498             ++uxSchedulerSuspended;
3499             portRELEASE_ISR_LOCK();
3500
3501             portCLEAR_INTERRUPT_MASK( ulState );
3502         }
3503         else
3504         {
3505             mtCOVERAGE_TEST_MARKER();
3506         }
3507     }
3508     #endif /* #if ( configNUMBER_OF_CORES == 1 ) */
3509
3510     traceRETURN_vTaskSuspendAll();
3511 }
3512
3513 /*----------------------------------------------------------*/
3514
3515 #if ( configUSE_TICKLESS_IDLE != 0 )
3516
3517     static TickType_t prvGetExpectedIdleTime( void )
3518     {
3519         TickType_t xReturn;
3520         UBaseType_t uxHigherPriorityReadyTasks = pdFALSE;
3521
3522         /* uxHigherPriorityReadyTasks takes care of the case where
3523          * configUSE_PREEMPTION is 0, so there may be tasks above the idle priority
3524          * task that are in the Ready state, even though the idle task is
3525          * running. */
3526         #if ( configUSE_PORT_OPTIMISED_TASK_SELECTION == 0 )
3527         {
3528             if( uxTopReadyPriority > tskIDLE_PRIORITY )
3529             {
3530                 uxHigherPriorityReadyTasks = pdTRUE;
3531             }
3532         }
3533         #else
3534         {
3535             const UBaseType_t uxLeastSignificantBit = ( UBaseType_t ) 0x01;
3536
3537             /* When port optimised task selection is used the uxTopReadyPriority
3538              * variable is used as a bit map.  If bits other than the least
3539              * significant bit are set then there are tasks that have a priority
3540              * above the idle priority that are in the Ready state.  This takes
3541              * care of the case where the co-operative scheduler is in use. */
3542             if( uxTopReadyPriority > uxLeastSignificantBit )
3543             {
3544                 uxHigherPriorityReadyTasks = pdTRUE;
3545             }
3546         }
3547         #endif /* if ( configUSE_PORT_OPTIMISED_TASK_SELECTION == 0 ) */
3548
3549         if( pxCurrentTCB->uxPriority > tskIDLE_PRIORITY )
3550         {
3551             xReturn = 0;
3552         }
3553         else if( listCURRENT_LIST_LENGTH( &( pxReadyTasksLists[ tskIDLE_PRIORITY ] ) ) > 1 )
3554         {
3555             /* There are other idle priority tasks in the ready state.  If
3556              * time slicing is used then the very next tick interrupt must be
3557              * processed. */
3558             xReturn = 0;
3559         }
3560         else if( uxHigherPriorityReadyTasks != pdFALSE )
3561         {
3562             /* There are tasks in the Ready state that have a priority above the
3563              * idle priority.  This path can only be reached if
3564              * configUSE_PREEMPTION is 0. */
3565             xReturn = 0;
3566         }
3567         else
3568         {
3569             xReturn = xNextTaskUnblockTime - xTickCount;
3570         }
3571
3572         return xReturn;
3573     }
3574
3575 #endif /* configUSE_TICKLESS_IDLE */
3576 /*----------------------------------------------------------*/
3577
3578 BaseType_t xTaskResumeAll( void )
3579 {
3580     TCB_t * pxTCB = NULL;
3581     BaseType_t xAlreadyYielded = pdFALSE;
3582
3583     traceENTER_xTaskResumeAll();
3584
3585     #if ( configNUMBER_OF_CORES > 1 )
3586         if( xSchedulerRunning != pdFALSE )
3587     #endif
3588     {
3589         /* It is possible that an ISR caused a task to be removed from an event
3590          * list while the scheduler was suspended.  If this was the case then the
3591          * removed task will have been added to the xPendingReadyList.  Once the
3592          * scheduler has been resumed it is safe to move all the pending ready
3593          * tasks from this list into their appropriate ready list. */
3594         taskENTER_CRITICAL();
3595         {
3596             BaseType_t xCoreID;
3597             xCoreID = ( BaseType_t ) portGET_CORE_ID();
3598
3599             /* If uxSchedulerSuspended is zero then this function does not match a
3600              * previous call to vTaskSuspendAll(). */
3601             configASSERT( uxSchedulerSuspended != 0U );
3602
3603             --uxSchedulerSuspended;
3604             portRELEASE_TASK_LOCK();
3605
3606             if( uxSchedulerSuspended == ( UBaseType_t ) 0U )
3607             {
3608                 if( uxCurrentNumberOfTasks > ( UBaseType_t ) 0U )
3609                 {
3610                     /* Move any readied tasks from the pending list into the
3611                      * appropriate ready list. */
3612                     while( listLIST_IS_EMPTY( &xPendingReadyList ) == pdFALSE )
3613                     {
3614                         pxTCB = listGET_OWNER_OF_HEAD_ENTRY( ( &xPendingReadyList ) ); /*lint !e9079 void * is used as this macro is used with timers too.  Alignment is known to be fine as the type of the pointer stored and retrieved is the same. */
3615                         listREMOVE_ITEM( &( pxTCB->xEventListItem ) );
3616                         portMEMORY_BARRIER();
3617                         listREMOVE_ITEM( &( pxTCB->xStateListItem ) );
3618                         prvAddTaskToReadyList( pxTCB );
3619
3620                         #if ( configNUMBER_OF_CORES == 1 )
3621                         {
3622                             /* If the moved task has a priority higher than the current
3623                              * task then a yield must be performed. */
3624                             if( pxTCB->uxPriority > pxCurrentTCB->uxPriority )
3625                             {
3626                                 xYieldPendings[ xCoreID ] = pdTRUE;
3627                             }
3628                             else
3629                             {
3630                                 mtCOVERAGE_TEST_MARKER();
3631                             }
3632                         }
3633                         #else /* #if ( configNUMBER_OF_CORES == 1 ) */
3634                         {
3635                             /* All appropriate tasks yield at the moment a task is added to xPendingReadyList.
3636                              * If the current core yielded then vTaskSwitchContext() has already been called
3637                              * which sets xYieldPendings for the current core to pdTRUE. */
3638                         }
3639                         #endif /* #if ( configNUMBER_OF_CORES == 1 ) */
3640                     }
3641
3642                     if( pxTCB != NULL )
3643                     {
3644                         /* A task was unblocked while the scheduler was suspended,
3645                          * which may have prevented the next unblock time from being
3646                          * re-calculated, in which case re-calculate it now.  Mainly
3647                          * important for low power tickless implementations, where
3648                          * this can prevent an unnecessary exit from low power
3649                          * state. */
3650                         prvResetNextTaskUnblockTime();
3651                     }
3652
3653                     /* If any ticks occurred while the scheduler was suspended then
3654                      * they should be processed now.  This ensures the tick count does
3655                      * not  slip, and that any delayed tasks are resumed at the correct
3656                      * time.
3657                      *
3658                      * It should be safe to call xTaskIncrementTick here from any core
3659                      * since we are in a critical section and xTaskIncrementTick itself
3660                      * protects itself within a critical section. Suspending the scheduler
3661                      * from any core causes xTaskIncrementTick to increment uxPendedCounts. */
3662                     {
3663                         TickType_t xPendedCounts = xPendedTicks; /* Non-volatile copy. */
3664
3665                         if( xPendedCounts > ( TickType_t ) 0U )
3666                         {
3667                             do
3668                             {
3669                                 if( xTaskIncrementTick() != pdFALSE )
3670                                 {
3671                                     /* Other cores are interrupted from
3672                                      * within xTaskIncrementTick(). */
3673                                     xYieldPendings[ xCoreID ] = pdTRUE;
3674                                 }
3675                                 else
3676                                 {
3677                                     mtCOVERAGE_TEST_MARKER();
3678                                 }
3679
3680                                 --xPendedCounts;
3681                             } while( xPendedCounts > ( TickType_t ) 0U );
3682
3683                             xPendedTicks = 0;
3684                         }
3685                         else
3686                         {
3687                             mtCOVERAGE_TEST_MARKER();
3688                         }
3689                     }
3690
3691                     if( xYieldPendings[ xCoreID ] != pdFALSE )
3692                     {
3693                         #if ( configUSE_PREEMPTION != 0 )
3694                         {
3695                             xAlreadyYielded = pdTRUE;
3696                         }
3697                         #endif /* #if ( configUSE_PREEMPTION != 0 ) */
3698
3699                         #if ( configNUMBER_OF_CORES == 1 )
3700                         {
3701                             taskYIELD_TASK_CORE_IF_USING_PREEMPTION( pxCurrentTCB );
3702                         }
3703                         #endif /* #if ( configNUMBER_OF_CORES == 1 ) */
3704                     }
3705                     else
3706                     {
3707                         mtCOVERAGE_TEST_MARKER();
3708                     }
3709                 }
3710             }
3711             else
3712             {
3713                 mtCOVERAGE_TEST_MARKER();
3714             }
3715         }
3716         taskEXIT_CRITICAL();
3717     }
3718
3719     traceRETURN_xTaskResumeAll( xAlreadyYielded );
3720
3721     return xAlreadyYielded;
3722 }
3723 /*-----------------------------------------------------------*/
3724
3725 TickType_t xTaskGetTickCount( void )
3726 {
3727     TickType_t xTicks;
3728
3729     traceENTER_xTaskGetTickCount();
3730
3731     /* Critical section required if running on a 16 bit processor. */
3732     portTICK_TYPE_ENTER_CRITICAL();
3733     {
3734         xTicks = xTickCount;
3735     }
3736     portTICK_TYPE_EXIT_CRITICAL();
3737
3738     traceRETURN_xTaskGetTickCount( xTicks );
3739
3740     return xTicks;
3741 }
3742 /*-----------------------------------------------------------*/
3743
3744 TickType_t xTaskGetTickCountFromISR( void )
3745 {
3746     TickType_t xReturn;
3747     UBaseType_t uxSavedInterruptStatus;
3748
3749     traceENTER_xTaskGetTickCountFromISR();
3750
3751     /* RTOS ports that support interrupt nesting have the concept of a maximum
3752      * system call (or maximum API call) interrupt priority.  Interrupts that are
3753      * above the maximum system call priority are kept permanently enabled, even
3754      * when the RTOS kernel is in a critical section, but cannot make any calls to
3755      * FreeRTOS API functions.  If configASSERT() is defined in FreeRTOSConfig.h
3756      * then portASSERT_IF_INTERRUPT_PRIORITY_INVALID() will result in an assertion
3757      * failure if a FreeRTOS API function is called from an interrupt that has been
3758      * assigned a priority above the configured maximum system call priority.
3759      * Only FreeRTOS functions that end in FromISR can be called from interrupts
3760      * that have been assigned a priority at or (logically) below the maximum
3761      * system call  interrupt priority.  FreeRTOS maintains a separate interrupt
3762      * safe API to ensure interrupt entry is as fast and as simple as possible.
3763      * More information (albeit Cortex-M specific) is provided on the following
3764      * link: https://www.FreeRTOS.org/RTOS-Cortex-M3-M4.html */
3765     portASSERT_IF_INTERRUPT_PRIORITY_INVALID();
3766
3767     uxSavedInterruptStatus = portTICK_TYPE_SET_INTERRUPT_MASK_FROM_ISR();
3768     {
3769         xReturn = xTickCount;
3770     }
3771     portTICK_TYPE_CLEAR_INTERRUPT_MASK_FROM_ISR( uxSavedInterruptStatus );
3772
3773     traceRETURN_xTaskGetTickCountFromISR( xReturn );
3774
3775     return xReturn;
3776 }
3777 /*-----------------------------------------------------------*/
3778
3779 UBaseType_t uxTaskGetNumberOfTasks( void )
3780 {
3781     traceENTER_uxTaskGetNumberOfTasks();
3782
3783     /* A critical section is not required because the variables are of type
3784      * BaseType_t. */
3785     traceRETURN_uxTaskGetNumberOfTasks( uxCurrentNumberOfTasks );
3786
3787     return uxCurrentNumberOfTasks;
3788 }
3789 /*-----------------------------------------------------------*/
3790
3791 char * pcTaskGetName( TaskHandle_t xTaskToQuery ) /*lint !e971 Unqualified char types are allowed for strings and single characters only. */
3792 {
3793     TCB_t * pxTCB;
3794
3795     traceENTER_pcTaskGetName( xTaskToQuery );
3796
3797     /* If null is passed in here then the name of the calling task is being
3798      * queried. */
3799     pxTCB = prvGetTCBFromHandle( xTaskToQuery );
3800     configASSERT( pxTCB );
3801
3802     traceRETURN_pcTaskGetName( &( pxTCB->pcTaskName[ 0 ] ) );
3803
3804     return &( pxTCB->pcTaskName[ 0 ] );
3805 }
3806 /*-----------------------------------------------------------*/
3807
3808 #if ( INCLUDE_xTaskGetHandle == 1 )
3809
3810     #if ( configNUMBER_OF_CORES == 1 )
3811         static TCB_t * prvSearchForNameWithinSingleList( List_t * pxList,
3812                                                          const char pcNameToQuery[] )
3813         {
3814             TCB_t * pxNextTCB;
3815             TCB_t * pxFirstTCB;
3816             TCB_t * pxReturn = NULL;
3817             UBaseType_t x;
3818             char cNextChar;
3819             BaseType_t xBreakLoop;
3820
3821             /* This function is called with the scheduler suspended. */
3822
3823             if( listCURRENT_LIST_LENGTH( pxList ) > ( UBaseType_t ) 0 )
3824             {
3825                 listGET_OWNER_OF_NEXT_ENTRY( pxFirstTCB, pxList ); /*lint !e9079 void * is used as this macro is used with timers too.  Alignment is known to be fine as the type of the pointer stored and retrieved is the same. */
3826
3827                 do
3828                 {
3829                     listGET_OWNER_OF_NEXT_ENTRY( pxNextTCB, pxList ); /*lint !e9079 void * is used as this macro is used with timers too.  Alignment is known to be fine as the type of the pointer stored and retrieved is the same. */
3830
3831                     /* Check each character in the name looking for a match or
3832                      * mismatch. */
3833                     xBreakLoop = pdFALSE;
3834
3835                     for( x = ( UBaseType_t ) 0; x < ( UBaseType_t ) configMAX_TASK_NAME_LEN; x++ )
3836                     {
3837                         cNextChar = pxNextTCB->pcTaskName[ x ];
3838
3839                         if( cNextChar != pcNameToQuery[ x ] )
3840                         {
3841                             /* Characters didn't match. */
3842                             xBreakLoop = pdTRUE;
3843                         }
3844                         else if( cNextChar == ( char ) 0x00 )
3845                         {
3846                             /* Both strings terminated, a match must have been
3847                              * found. */
3848                             pxReturn = pxNextTCB;
3849                             xBreakLoop = pdTRUE;
3850                         }
3851                         else
3852                         {
3853                             mtCOVERAGE_TEST_MARKER();
3854                         }
3855
3856                         if( xBreakLoop != pdFALSE )
3857                         {
3858                             break;
3859                         }
3860                     }
3861
3862                     if( pxReturn != NULL )
3863                     {
3864                         /* The handle has been found. */
3865                         break;
3866                     }
3867                 } while( pxNextTCB != pxFirstTCB );
3868             }
3869             else
3870             {
3871                 mtCOVERAGE_TEST_MARKER();
3872             }
3873
3874             return pxReturn;
3875         }
3876     #else /* if ( configNUMBER_OF_CORES == 1 ) */
3877         static TCB_t * prvSearchForNameWithinSingleList( List_t * pxList,
3878                                                          const char pcNameToQuery[] )
3879         {
3880             TCB_t * pxReturn = NULL;
3881             UBaseType_t x;
3882             char cNextChar;
3883             BaseType_t xBreakLoop;
3884             const ListItem_t * pxEndMarker = listGET_END_MARKER( pxList );
3885             ListItem_t * pxIterator;
3886
3887             /* This function is called with the scheduler suspended. */
3888
3889             if( listCURRENT_LIST_LENGTH( pxList ) > ( UBaseType_t ) 0 )
3890             {
3891                 for( pxIterator = listGET_HEAD_ENTRY( pxList ); pxIterator != pxEndMarker; pxIterator = listGET_NEXT( pxIterator ) )
3892                 {
3893                     TCB_t * pxTCB = listGET_LIST_ITEM_OWNER( pxIterator );
3894
3895                     /* Check each character in the name looking for a match or
3896                      * mismatch. */
3897                     xBreakLoop = pdFALSE;
3898
3899                     for( x = ( UBaseType_t ) 0; x < ( UBaseType_t ) configMAX_TASK_NAME_LEN; x++ )
3900                     {
3901                         cNextChar = pxTCB->pcTaskName[ x ];
3902
3903                         if( cNextChar != pcNameToQuery[ x ] )
3904                         {
3905                             /* Characters didn't match. */
3906                             xBreakLoop = pdTRUE;
3907                         }
3908                         else if( cNextChar == ( char ) 0x00 )
3909                         {
3910                             /* Both strings terminated, a match must have been
3911                              * found. */
3912                             pxReturn = pxTCB;
3913                             xBreakLoop = pdTRUE;
3914                         }
3915                         else
3916                         {
3917                             mtCOVERAGE_TEST_MARKER();
3918                         }
3919
3920                         if( xBreakLoop != pdFALSE )
3921                         {
3922                             break;
3923                         }
3924                     }
3925
3926                     if( pxReturn != NULL )
3927                     {
3928                         /* The handle has been found. */
3929                         break;
3930                     }
3931                 }
3932             }
3933             else
3934             {
3935                 mtCOVERAGE_TEST_MARKER();
3936             }
3937
3938             return pxReturn;
3939         }
3940     #endif /* #if ( configNUMBER_OF_CORES == 1 ) */
3941
3942 #endif /* INCLUDE_xTaskGetHandle */
3943 /*-----------------------------------------------------------*/
3944
3945 #if ( INCLUDE_xTaskGetHandle == 1 )
3946
3947     TaskHandle_t xTaskGetHandle( const char * pcNameToQuery ) /*lint !e971 Unqualified char types are allowed for strings and single characters only. */
3948     {
3949         UBaseType_t uxQueue = configMAX_PRIORITIES;
3950         TCB_t * pxTCB;
3951
3952         traceENTER_xTaskGetHandle( pcNameToQuery );
3953
3954         /* Task names will be truncated to configMAX_TASK_NAME_LEN - 1 bytes. */
3955         configASSERT( strlen( pcNameToQuery ) < configMAX_TASK_NAME_LEN );
3956
3957         vTaskSuspendAll();
3958         {
3959             /* Search the ready lists. */
3960             do
3961             {
3962                 uxQueue--;
3963                 pxTCB = prvSearchForNameWithinSingleList( ( List_t * ) &( pxReadyTasksLists[ uxQueue ] ), pcNameToQuery );
3964
3965                 if( pxTCB != NULL )
3966                 {
3967                     /* Found the handle. */
3968                     break;
3969                 }
3970             } while( uxQueue > ( UBaseType_t ) tskIDLE_PRIORITY ); /*lint !e961 MISRA exception as the casts are only redundant for some ports. */
3971
3972             /* Search the delayed lists. */
3973             if( pxTCB == NULL )
3974             {
3975                 pxTCB = prvSearchForNameWithinSingleList( ( List_t * ) pxDelayedTaskList, pcNameToQuery );
3976             }
3977
3978             if( pxTCB == NULL )
3979             {
3980                 pxTCB = prvSearchForNameWithinSingleList( ( List_t * ) pxOverflowDelayedTaskList, pcNameToQuery );
3981             }
3982
3983             #if ( INCLUDE_vTaskSuspend == 1 )
3984             {
3985                 if( pxTCB == NULL )
3986                 {
3987                     /* Search the suspended list. */
3988                     pxTCB = prvSearchForNameWithinSingleList( &xSuspendedTaskList, pcNameToQuery );
3989                 }
3990             }
3991             #endif
3992
3993             #if ( INCLUDE_vTaskDelete == 1 )
3994             {
3995                 if( pxTCB == NULL )
3996                 {
3997                     /* Search the deleted list. */
3998                     pxTCB = prvSearchForNameWithinSingleList( &xTasksWaitingTermination, pcNameToQuery );
3999                 }
4000             }
4001             #endif
4002         }
4003         ( void ) xTaskResumeAll();
4004
4005         traceRETURN_xTaskGetHandle( pxTCB );
4006
4007         return pxTCB;
4008     }
4009
4010 #endif /* INCLUDE_xTaskGetHandle */
4011 /*-----------------------------------------------------------*/
4012
4013 #if ( configSUPPORT_STATIC_ALLOCATION == 1 )
4014
4015     BaseType_t xTaskGetStaticBuffers( TaskHandle_t xTask,
4016                                       StackType_t ** ppuxStackBuffer,
4017                                       StaticTask_t ** ppxTaskBuffer )
4018     {
4019         BaseType_t xReturn;
4020         TCB_t * pxTCB;
4021
4022         traceENTER_xTaskGetStaticBuffers( xTask, ppuxStackBuffer, ppxTaskBuffer );
4023
4024         configASSERT( ppuxStackBuffer != NULL );
4025         configASSERT( ppxTaskBuffer != NULL );
4026
4027         pxTCB = prvGetTCBFromHandle( xTask );
4028
4029         #if ( tskSTATIC_AND_DYNAMIC_ALLOCATION_POSSIBLE == 1 )
4030         {
4031             if( pxTCB->ucStaticallyAllocated == tskSTATICALLY_ALLOCATED_STACK_AND_TCB )
4032             {
4033                 *ppuxStackBuffer = pxTCB->pxStack;
4034                 *ppxTaskBuffer = ( StaticTask_t * ) pxTCB;
4035                 xReturn = pdTRUE;
4036             }
4037             else if( pxTCB->ucStaticallyAllocated == tskSTATICALLY_ALLOCATED_STACK_ONLY )
4038             {
4039                 *ppuxStackBuffer = pxTCB->pxStack;
4040                 *ppxTaskBuffer = NULL;
4041                 xReturn = pdTRUE;
4042             }
4043             else
4044             {
4045                 xReturn = pdFALSE;
4046             }
4047         }
4048         #else /* tskSTATIC_AND_DYNAMIC_ALLOCATION_POSSIBLE == 1 */
4049         {
4050             *ppuxStackBuffer = pxTCB->pxStack;
4051             *ppxTaskBuffer = ( StaticTask_t * ) pxTCB;
4052             xReturn = pdTRUE;
4053         }
4054         #endif /* tskSTATIC_AND_DYNAMIC_ALLOCATION_POSSIBLE == 1 */
4055
4056         traceRETURN_xTaskGetStaticBuffers( xReturn );
4057
4058         return xReturn;
4059     }
4060
4061 #endif /* configSUPPORT_STATIC_ALLOCATION */
4062 /*-----------------------------------------------------------*/
4063
4064 #if ( configUSE_TRACE_FACILITY == 1 )
4065
4066     UBaseType_t uxTaskGetSystemState( TaskStatus_t * const pxTaskStatusArray,
4067                                       const UBaseType_t uxArraySize,
4068                                       configRUN_TIME_COUNTER_TYPE * const pulTotalRunTime )
4069     {
4070         UBaseType_t uxTask = 0, uxQueue = configMAX_PRIORITIES;
4071
4072         traceENTER_uxTaskGetSystemState( pxTaskStatusArray, uxArraySize, pulTotalRunTime );
4073
4074         vTaskSuspendAll();
4075         {
4076             /* Is there a space in the array for each task in the system? */
4077             if( uxArraySize >= uxCurrentNumberOfTasks )
4078             {
4079                 /* Fill in an TaskStatus_t structure with information on each
4080                  * task in the Ready state. */
4081                 do
4082                 {
4083                     uxQueue--;
4084                     uxTask = ( UBaseType_t ) ( uxTask + prvListTasksWithinSingleList( &( pxTaskStatusArray[ uxTask ] ), &( pxReadyTasksLists[ uxQueue ] ), eReady ) );
4085                 } while( uxQueue > ( UBaseType_t ) tskIDLE_PRIORITY ); /*lint !e961 MISRA exception as the casts are only redundant for some ports. */
4086
4087                 /* Fill in an TaskStatus_t structure with information on each
4088                  * task in the Blocked state. */
4089                 uxTask = ( UBaseType_t ) ( uxTask + prvListTasksWithinSingleList( &( pxTaskStatusArray[ uxTask ] ), ( List_t * ) pxDelayedTaskList, eBlocked ) );
4090                 uxTask = ( UBaseType_t ) ( uxTask + prvListTasksWithinSingleList( &( pxTaskStatusArray[ uxTask ] ), ( List_t * ) pxOverflowDelayedTaskList, eBlocked ) );
4091
4092                 #if ( INCLUDE_vTaskDelete == 1 )
4093                 {
4094                     /* Fill in an TaskStatus_t structure with information on
4095                      * each task that has been deleted but not yet cleaned up. */
4096                     uxTask = ( UBaseType_t ) ( uxTask + prvListTasksWithinSingleList( &( pxTaskStatusArray[ uxTask ] ), &xTasksWaitingTermination, eDeleted ) );
4097                 }
4098                 #endif
4099
4100                 #if ( INCLUDE_vTaskSuspend == 1 )
4101                 {
4102                     /* Fill in an TaskStatus_t structure with information on
4103                      * each task in the Suspended state. */
4104                     uxTask = ( UBaseType_t ) ( uxTask + prvListTasksWithinSingleList( &( pxTaskStatusArray[ uxTask ] ), &xSuspendedTaskList, eSuspended ) );
4105                 }
4106                 #endif
4107
4108                 #if ( configGENERATE_RUN_TIME_STATS == 1 )
4109                 {
4110                     if( pulTotalRunTime != NULL )
4111                     {
4112                         #ifdef portALT_GET_RUN_TIME_COUNTER_VALUE
4113                             portALT_GET_RUN_TIME_COUNTER_VALUE( ( *pulTotalRunTime ) );
4114                         #else
4115                             *pulTotalRunTime = ( configRUN_TIME_COUNTER_TYPE ) portGET_RUN_TIME_COUNTER_VALUE();
4116                         #endif
4117                     }
4118                 }
4119                 #else /* if ( configGENERATE_RUN_TIME_STATS == 1 ) */
4120                 {
4121                     if( pulTotalRunTime != NULL )
4122                     {
4123                         *pulTotalRunTime = 0;
4124                     }
4125                 }
4126                 #endif /* if ( configGENERATE_RUN_TIME_STATS == 1 ) */
4127             }
4128             else
4129             {
4130                 mtCOVERAGE_TEST_MARKER();
4131             }
4132         }
4133         ( void ) xTaskResumeAll();
4134
4135         traceRETURN_uxTaskGetSystemState( uxTask );
4136
4137         return uxTask;
4138     }
4139
4140 #endif /* configUSE_TRACE_FACILITY */
4141 /*----------------------------------------------------------*/
4142
4143 #if ( INCLUDE_xTaskGetIdleTaskHandle == 1 )
4144
4145 /* SMP_TODO : This function returns only idle task handle for core 0.
4146  * Consider to add another function to return the idle task handles. */
4147     TaskHandle_t xTaskGetIdleTaskHandle( void )
4148     {
4149         traceENTER_xTaskGetIdleTaskHandle();
4150
4151         /* If xTaskGetIdleTaskHandle() is called before the scheduler has been
4152          * started, then xIdleTaskHandles will be NULL. */
4153         configASSERT( ( xIdleTaskHandles[ 0 ] != NULL ) );
4154
4155         traceRETURN_xTaskGetIdleTaskHandle( xIdleTaskHandles[ 0 ] );
4156
4157         return xIdleTaskHandles[ 0 ];
4158     }
4159
4160 #endif /* INCLUDE_xTaskGetIdleTaskHandle */
4161 /*----------------------------------------------------------*/
4162
4163 /* This conditional compilation should use inequality to 0, not equality to 1.
4164  * This is to ensure vTaskStepTick() is available when user defined low power mode
4165  * implementations require configUSE_TICKLESS_IDLE to be set to a value other than
4166  * 1. */
4167 #if ( configUSE_TICKLESS_IDLE != 0 )
4168
4169     void vTaskStepTick( TickType_t xTicksToJump )
4170     {
4171         traceENTER_vTaskStepTick( xTicksToJump );
4172
4173         /* Correct the tick count value after a period during which the tick
4174          * was suppressed.  Note this does *not* call the tick hook function for
4175          * each stepped tick. */
4176         configASSERT( ( xTickCount + xTicksToJump ) <= xNextTaskUnblockTime );
4177
4178         if( ( xTickCount + xTicksToJump ) == xNextTaskUnblockTime )
4179         {
4180             /* Arrange for xTickCount to reach xNextTaskUnblockTime in
4181              * xTaskIncrementTick() when the scheduler resumes.  This ensures
4182              * that any delayed tasks are resumed at the correct time. */
4183             configASSERT( uxSchedulerSuspended != ( UBaseType_t ) 0U );
4184             configASSERT( xTicksToJump != ( TickType_t ) 0 );
4185
4186             /* Prevent the tick interrupt modifying xPendedTicks simultaneously. */
4187             taskENTER_CRITICAL();
4188             {
4189                 xPendedTicks++;
4190             }
4191             taskEXIT_CRITICAL();
4192             xTicksToJump--;
4193         }
4194         else
4195         {
4196             mtCOVERAGE_TEST_MARKER();
4197         }
4198
4199         xTickCount += xTicksToJump;
4200
4201         traceINCREASE_TICK_COUNT( xTicksToJump );
4202         traceRETURN_vTaskStepTick();
4203     }
4204
4205 #endif /* configUSE_TICKLESS_IDLE */
4206 /*----------------------------------------------------------*/
4207
4208 BaseType_t xTaskCatchUpTicks( TickType_t xTicksToCatchUp )
4209 {
4210     BaseType_t xYieldOccurred;
4211
4212     traceENTER_xTaskCatchUpTicks( xTicksToCatchUp );
4213
4214     /* Must not be called with the scheduler suspended as the implementation
4215      * relies on xPendedTicks being wound down to 0 in xTaskResumeAll(). */
4216     configASSERT( uxSchedulerSuspended == ( UBaseType_t ) 0U );
4217
4218     /* Use xPendedTicks to mimic xTicksToCatchUp number of ticks occurring when
4219      * the scheduler is suspended so the ticks are executed in xTaskResumeAll(). */
4220     vTaskSuspendAll();
4221
4222     /* Prevent the tick interrupt modifying xPendedTicks simultaneously. */
4223     taskENTER_CRITICAL();
4224     {
4225         xPendedTicks += xTicksToCatchUp;
4226     }
4227     taskEXIT_CRITICAL();
4228     xYieldOccurred = xTaskResumeAll();
4229
4230     traceRETURN_xTaskCatchUpTicks( xYieldOccurred );
4231
4232     return xYieldOccurred;
4233 }
4234 /*----------------------------------------------------------*/
4235
4236 #if ( INCLUDE_xTaskAbortDelay == 1 )
4237
4238     BaseType_t xTaskAbortDelay( TaskHandle_t xTask )
4239     {
4240         TCB_t * pxTCB = xTask;
4241         BaseType_t xReturn;
4242
4243         traceENTER_xTaskAbortDelay( xTask );
4244
4245         configASSERT( pxTCB );
4246
4247         vTaskSuspendAll();
4248         {
4249             /* A task can only be prematurely removed from the Blocked state if
4250              * it is actually in the Blocked state. */
4251             if( eTaskGetState( xTask ) == eBlocked )
4252             {
4253                 xReturn = pdPASS;
4254
4255                 /* Remove the reference to the task from the blocked list.  An
4256                  * interrupt won't touch the xStateListItem because the
4257                  * scheduler is suspended. */
4258                 ( void ) uxListRemove( &( pxTCB->xStateListItem ) );
4259
4260                 /* Is the task waiting on an event also?  If so remove it from
4261                  * the event list too.  Interrupts can touch the event list item,
4262                  * even though the scheduler is suspended, so a critical section
4263                  * is used. */
4264                 taskENTER_CRITICAL();
4265                 {
4266                     if( listLIST_ITEM_CONTAINER( &( pxTCB->xEventListItem ) ) != NULL )
4267                     {
4268                         ( void ) uxListRemove( &( pxTCB->xEventListItem ) );
4269
4270                         /* This lets the task know it was forcibly removed from the
4271                          * blocked state so it should not re-evaluate its block time and
4272                          * then block again. */
4273                         pxTCB->ucDelayAborted = pdTRUE;
4274                     }
4275                     else
4276                     {
4277                         mtCOVERAGE_TEST_MARKER();
4278                     }
4279                 }
4280                 taskEXIT_CRITICAL();
4281
4282                 /* Place the unblocked task into the appropriate ready list. */
4283                 prvAddTaskToReadyList( pxTCB );
4284
4285                 /* A task being unblocked cannot cause an immediate context
4286                  * switch if preemption is turned off. */
4287                 #if ( configUSE_PREEMPTION == 1 )
4288                 {
4289                     #if ( configNUMBER_OF_CORES == 1 )
4290                     {
4291                         /* Preemption is on, but a context switch should only be
4292                          * performed if the unblocked task has a priority that is
4293                          * higher than the currently executing task. */
4294                         if( pxTCB->uxPriority > pxCurrentTCB->uxPriority )
4295                         {
4296                             /* Pend the yield to be performed when the scheduler
4297                              * is unsuspended. */
4298                             xYieldPendings[ 0 ] = pdTRUE;
4299                         }
4300                         else
4301                         {
4302                             mtCOVERAGE_TEST_MARKER();
4303                         }
4304                     }
4305                     #else /* #if ( configNUMBER_OF_CORES == 1 ) */
4306                     {
4307                         taskENTER_CRITICAL();
4308                         {
4309                             prvYieldForTask( pxTCB );
4310                         }
4311                         taskEXIT_CRITICAL();
4312                     }
4313                     #endif /* #if ( configNUMBER_OF_CORES == 1 ) */
4314                 }
4315                 #endif /* #if ( configUSE_PREEMPTION == 1 ) */
4316             }
4317             else
4318             {
4319                 xReturn = pdFAIL;
4320             }
4321         }
4322         ( void ) xTaskResumeAll();
4323
4324         traceRETURN_xTaskAbortDelay( xReturn );
4325
4326         return xReturn;
4327     }
4328
4329 #endif /* INCLUDE_xTaskAbortDelay */
4330 /*----------------------------------------------------------*/
4331
4332 BaseType_t xTaskIncrementTick( void )
4333 {
4334     TCB_t * pxTCB;
4335     TickType_t xItemValue;
4336     BaseType_t xSwitchRequired = pdFALSE;
4337
4338     traceENTER_xTaskIncrementTick();
4339
4340     #if ( configUSE_PREEMPTION == 1 ) && ( configNUMBER_OF_CORES > 1 )
4341     BaseType_t xYieldRequiredForCore[ configNUMBER_OF_CORES ] = { pdFALSE };
4342     #endif /* #if ( configUSE_PREEMPTION == 1 ) && ( configNUMBER_OF_CORES > 1 ) */
4343
4344     /* Called by the portable layer each time a tick interrupt occurs.
4345      * Increments the tick then checks to see if the new tick value will cause any
4346      * tasks to be unblocked. */
4347     traceTASK_INCREMENT_TICK( xTickCount );
4348
4349     /* Tick increment should occur on every kernel timer event. Core 0 has the
4350      * responsibility to increment the tick, or increment the pended ticks if the
4351      * scheduler is suspended.  If pended ticks is greater than zero, the core that
4352      * calls xTaskResumeAll has the responsibility to increment the tick. */
4353     if( uxSchedulerSuspended == ( UBaseType_t ) 0U )
4354     {
4355         /* Minor optimisation.  The tick count cannot change in this
4356          * block. */
4357         const TickType_t xConstTickCount = xTickCount + ( TickType_t ) 1;
4358
4359         /* Increment the RTOS tick, switching the delayed and overflowed
4360          * delayed lists if it wraps to 0. */
4361         xTickCount = xConstTickCount;
4362
4363         if( xConstTickCount == ( TickType_t ) 0U ) /*lint !e774 'if' does not always evaluate to false as it is looking for an overflow. */
4364         {
4365             taskSWITCH_DELAYED_LISTS();
4366         }
4367         else
4368         {
4369             mtCOVERAGE_TEST_MARKER();
4370         }
4371
4372         /* See if this tick has made a timeout expire.  Tasks are stored in
4373          * the  queue in the order of their wake time - meaning once one task
4374          * has been found whose block time has not expired there is no need to
4375          * look any further down the list. */
4376         if( xConstTickCount >= xNextTaskUnblockTime )
4377         {
4378             for( ; ; )
4379             {
4380                 if( listLIST_IS_EMPTY( pxDelayedTaskList ) != pdFALSE )
4381                 {
4382                     /* The delayed list is empty.  Set xNextTaskUnblockTime
4383                      * to the maximum possible value so it is extremely
4384                      * unlikely that the
4385                      * if( xTickCount >= xNextTaskUnblockTime ) test will pass
4386                      * next time through. */
4387                     xNextTaskUnblockTime = portMAX_DELAY; /*lint !e961 MISRA exception as the casts are only redundant for some ports. */
4388                     break;
4389                 }
4390                 else
4391                 {
4392                     /* The delayed list is not empty, get the value of the
4393                      * item at the head of the delayed list.  This is the time
4394                      * at which the task at the head of the delayed list must
4395                      * be removed from the Blocked state. */
4396                     pxTCB = listGET_OWNER_OF_HEAD_ENTRY( pxDelayedTaskList ); /*lint !e9079 void * is used as this macro is used with timers and co-routines too.  Alignment is known to be fine as the type of the pointer stored and retrieved is the same. */
4397                     xItemValue = listGET_LIST_ITEM_VALUE( &( pxTCB->xStateListItem ) );
4398
4399                     if( xConstTickCount < xItemValue )
4400                     {
4401                         /* It is not time to unblock this item yet, but the
4402                          * item value is the time at which the task at the head
4403                          * of the blocked list must be removed from the Blocked
4404                          * state -  so record the item value in
4405                          * xNextTaskUnblockTime. */
4406                         xNextTaskUnblockTime = xItemValue;
4407                         break; /*lint !e9011 Code structure here is deemed easier to understand with multiple breaks. */
4408                     }
4409                     else
4410                     {
4411                         mtCOVERAGE_TEST_MARKER();
4412                     }
4413
4414                     /* It is time to remove the item from the Blocked state. */
4415                     listREMOVE_ITEM( &( pxTCB->xStateListItem ) );
4416
4417                     /* Is the task waiting on an event also?  If so remove
4418                      * it from the event list. */
4419                     if( listLIST_ITEM_CONTAINER( &( pxTCB->xEventListItem ) ) != NULL )
4420                     {
4421                         listREMOVE_ITEM( &( pxTCB->xEventListItem ) );
4422                     }
4423                     else
4424                     {
4425                         mtCOVERAGE_TEST_MARKER();
4426                     }
4427
4428                     /* Place the unblocked task into the appropriate ready
4429                      * list. */
4430                     prvAddTaskToReadyList( pxTCB );
4431
4432                     /* A task being unblocked cannot cause an immediate
4433                      * context switch if preemption is turned off. */
4434                     #if ( configUSE_PREEMPTION == 1 )
4435                     {
4436                         #if ( configNUMBER_OF_CORES == 1 )
4437                         {
4438                             /* Preemption is on, but a context switch should
4439                              * only be performed if the unblocked task's
4440                              * priority is higher than the currently executing
4441                              * task.
4442                              * The case of equal priority tasks sharing
4443                              * processing time (which happens when both
4444                              * preemption and time slicing are on) is
4445                              * handled below.*/
4446                             if( pxTCB->uxPriority > pxCurrentTCB->uxPriority )
4447                             {
4448                                 xSwitchRequired = pdTRUE;
4449                             }
4450                             else
4451                             {
4452                                 mtCOVERAGE_TEST_MARKER();
4453                             }
4454                         }
4455                         #else /* #if( configNUMBER_OF_CORES == 1 ) */
4456                         {
4457                             prvYieldForTask( pxTCB );
4458                         }
4459                         #endif /* #if( configNUMBER_OF_CORES == 1 ) */
4460                     }
4461                     #endif /* #if ( configUSE_PREEMPTION == 1 ) */
4462                 }
4463             }
4464         }
4465
4466         /* Tasks of equal priority to the currently running task will share
4467          * processing time (time slice) if preemption is on, and the application
4468          * writer has not explicitly turned time slicing off. */
4469         #if ( ( configUSE_PREEMPTION == 1 ) && ( configUSE_TIME_SLICING == 1 ) )
4470         {
4471             #if ( configNUMBER_OF_CORES == 1 )
4472             {
4473                 if( listCURRENT_LIST_LENGTH( &( pxReadyTasksLists[ pxCurrentTCB->uxPriority ] ) ) > ( UBaseType_t ) 1 )
4474                 {
4475                     xSwitchRequired = pdTRUE;
4476                 }
4477                 else
4478                 {
4479                     mtCOVERAGE_TEST_MARKER();
4480                 }
4481             }
4482             #else /* #if ( configNUMBER_OF_CORES == 1 ) */
4483             {
4484                 BaseType_t xCoreID;
4485
4486                 for( xCoreID = 0; xCoreID < ( ( BaseType_t ) configNUMBER_OF_CORES ); xCoreID++ )
4487                 {
4488                     if( listCURRENT_LIST_LENGTH( &( pxReadyTasksLists[ pxCurrentTCBs[ xCoreID ]->uxPriority ] ) ) > 1 )
4489                     {
4490                         xYieldRequiredForCore[ xCoreID ] = pdTRUE;
4491                     }
4492                     else
4493                     {
4494                         mtCOVERAGE_TEST_MARKER();
4495                     }
4496                 }
4497             }
4498             #endif /* #if ( configNUMBER_OF_CORES == 1 ) */
4499         }
4500         #endif /* #if ( ( configUSE_PREEMPTION == 1 ) && ( configUSE_TIME_SLICING == 1 ) ) */
4501
4502         #if ( configUSE_TICK_HOOK == 1 )
4503         {
4504             /* Guard against the tick hook being called when the pended tick
4505              * count is being unwound (when the scheduler is being unlocked). */
4506             if( xPendedTicks == ( TickType_t ) 0 )
4507             {
4508                 vApplicationTickHook();
4509             }
4510             else
4511             {
4512                 mtCOVERAGE_TEST_MARKER();
4513             }
4514         }
4515         #endif /* configUSE_TICK_HOOK */
4516
4517         #if ( configUSE_PREEMPTION == 1 )
4518         {
4519             #if ( configNUMBER_OF_CORES == 1 )
4520             {
4521                 /* For single core the core ID is always 0. */
4522                 if( xYieldPendings[ 0 ] != pdFALSE )
4523                 {
4524                     xSwitchRequired = pdTRUE;
4525                 }
4526                 else
4527                 {
4528                     mtCOVERAGE_TEST_MARKER();
4529                 }
4530             }
4531             #else /* #if ( configNUMBER_OF_CORES == 1 ) */
4532             {
4533                 BaseType_t xCoreID, xCurrentCoreID;
4534                 xCurrentCoreID = ( BaseType_t ) portGET_CORE_ID();
4535
4536                 for( xCoreID = 0; xCoreID < ( BaseType_t ) configNUMBER_OF_CORES; xCoreID++ )
4537                 {
4538                     #if ( configUSE_TASK_PREEMPTION_DISABLE == 1 )
4539                         if( pxCurrentTCBs[ xCoreID ]->xPreemptionDisable == pdFALSE )
4540                     #endif
4541                     {
4542                         if( ( xYieldRequiredForCore[ xCoreID ] != pdFALSE ) || ( xYieldPendings[ xCoreID ] != pdFALSE ) )
4543                         {
4544                             if( xCoreID == xCurrentCoreID )
4545                             {
4546                                 xSwitchRequired = pdTRUE;
4547                             }
4548                             else
4549                             {
4550                                 prvYieldCore( xCoreID );
4551                             }
4552                         }
4553                         else
4554                         {
4555                             mtCOVERAGE_TEST_MARKER();
4556                         }
4557                     }
4558                 }
4559             }
4560             #endif /* #if ( configNUMBER_OF_CORES == 1 ) */
4561         }
4562         #endif /* #if ( configUSE_PREEMPTION == 1 ) */
4563     }
4564     else
4565     {
4566         ++xPendedTicks;
4567
4568         /* The tick hook gets called at regular intervals, even if the
4569          * scheduler is locked. */
4570         #if ( configUSE_TICK_HOOK == 1 )
4571         {
4572             vApplicationTickHook();
4573         }
4574         #endif
4575     }
4576
4577     traceRETURN_xTaskIncrementTick( xSwitchRequired );
4578
4579     return xSwitchRequired;
4580 }
4581 /*-----------------------------------------------------------*/
4582
4583 #if ( configUSE_APPLICATION_TASK_TAG == 1 )
4584
4585     void vTaskSetApplicationTaskTag( TaskHandle_t xTask,
4586                                      TaskHookFunction_t pxHookFunction )
4587     {
4588         TCB_t * xTCB;
4589
4590         traceENTER_vTaskSetApplicationTaskTag( xTask, pxHookFunction );
4591
4592         /* If xTask is NULL then it is the task hook of the calling task that is
4593          * getting set. */
4594         if( xTask == NULL )
4595         {
4596             xTCB = ( TCB_t * ) pxCurrentTCB;
4597         }
4598         else
4599         {
4600             xTCB = xTask;
4601         }
4602
4603         /* Save the hook function in the TCB.  A critical section is required as
4604          * the value can be accessed from an interrupt. */
4605         taskENTER_CRITICAL();
4606         {
4607             xTCB->pxTaskTag = pxHookFunction;
4608         }
4609         taskEXIT_CRITICAL();
4610
4611         traceRETURN_vTaskSetApplicationTaskTag();
4612     }
4613
4614 #endif /* configUSE_APPLICATION_TASK_TAG */
4615 /*-----------------------------------------------------------*/
4616
4617 #if ( configUSE_APPLICATION_TASK_TAG == 1 )
4618
4619     TaskHookFunction_t xTaskGetApplicationTaskTag( TaskHandle_t xTask )
4620     {
4621         TCB_t * pxTCB;
4622         TaskHookFunction_t xReturn;
4623
4624         traceENTER_xTaskGetApplicationTaskTag( xTask );
4625
4626         /* If xTask is NULL then set the calling task's hook. */
4627         pxTCB = prvGetTCBFromHandle( xTask );
4628
4629         /* Save the hook function in the TCB.  A critical section is required as
4630          * the value can be accessed from an interrupt. */
4631         taskENTER_CRITICAL();
4632         {
4633             xReturn = pxTCB->pxTaskTag;
4634         }
4635         taskEXIT_CRITICAL();
4636
4637         traceRETURN_xTaskGetApplicationTaskTag( xReturn );
4638
4639         return xReturn;
4640     }
4641
4642 #endif /* configUSE_APPLICATION_TASK_TAG */
4643 /*-----------------------------------------------------------*/
4644
4645 #if ( configUSE_APPLICATION_TASK_TAG == 1 )
4646
4647     TaskHookFunction_t xTaskGetApplicationTaskTagFromISR( TaskHandle_t xTask )
4648     {
4649         TCB_t * pxTCB;
4650         TaskHookFunction_t xReturn;
4651         UBaseType_t uxSavedInterruptStatus;
4652
4653         traceENTER_xTaskGetApplicationTaskTagFromISR( xTask );
4654
4655         /* If xTask is NULL then set the calling task's hook. */
4656         pxTCB = prvGetTCBFromHandle( xTask );
4657
4658         /* Save the hook function in the TCB.  A critical section is required as
4659          * the value can be accessed from an interrupt. */
4660         uxSavedInterruptStatus = taskENTER_CRITICAL_FROM_ISR();
4661         {
4662             xReturn = pxTCB->pxTaskTag;
4663         }
4664         taskEXIT_CRITICAL_FROM_ISR( uxSavedInterruptStatus );
4665
4666         traceRETURN_xTaskGetApplicationTaskTagFromISR( xReturn );
4667
4668         return xReturn;
4669     }
4670
4671 #endif /* configUSE_APPLICATION_TASK_TAG */
4672 /*-----------------------------------------------------------*/
4673
4674 #if ( configUSE_APPLICATION_TASK_TAG == 1 )
4675
4676     BaseType_t xTaskCallApplicationTaskHook( TaskHandle_t xTask,
4677                                              void * pvParameter )
4678     {
4679         TCB_t * xTCB;
4680         BaseType_t xReturn;
4681
4682         traceENTER_xTaskCallApplicationTaskHook( xTask, pvParameter );
4683
4684         /* If xTask is NULL then we are calling our own task hook. */
4685         if( xTask == NULL )
4686         {
4687             xTCB = pxCurrentTCB;
4688         }
4689         else
4690         {
4691             xTCB = xTask;
4692         }
4693
4694         if( xTCB->pxTaskTag != NULL )
4695         {
4696             xReturn = xTCB->pxTaskTag( pvParameter );
4697         }
4698         else
4699         {
4700             xReturn = pdFAIL;
4701         }
4702
4703         traceRETURN_xTaskCallApplicationTaskHook( xReturn );
4704
4705         return xReturn;
4706     }
4707
4708 #endif /* configUSE_APPLICATION_TASK_TAG */
4709 /*-----------------------------------------------------------*/
4710
4711 #if ( configNUMBER_OF_CORES == 1 )
4712     void vTaskSwitchContext( void )
4713     {
4714         traceENTER_vTaskSwitchContext();
4715
4716         if( uxSchedulerSuspended != ( UBaseType_t ) 0U )
4717         {
4718             /* The scheduler is currently suspended - do not allow a context
4719              * switch. */
4720             xYieldPendings[ 0 ] = pdTRUE;
4721         }
4722         else
4723         {
4724             xYieldPendings[ 0 ] = pdFALSE;
4725             traceTASK_SWITCHED_OUT();
4726
4727             #if ( configGENERATE_RUN_TIME_STATS == 1 )
4728             {
4729                 #ifdef portALT_GET_RUN_TIME_COUNTER_VALUE
4730                     portALT_GET_RUN_TIME_COUNTER_VALUE( ulTotalRunTime[ 0 ] );
4731                 #else
4732                     ulTotalRunTime[ 0 ] = portGET_RUN_TIME_COUNTER_VALUE();
4733                 #endif
4734
4735                 /* Add the amount of time the task has been running to the
4736                  * accumulated time so far.  The time the task started running was
4737                  * stored in ulTaskSwitchedInTime.  Note that there is no overflow
4738                  * protection here so count values are only valid until the timer
4739                  * overflows.  The guard against negative values is to protect
4740                  * against suspect run time stat counter implementations - which
4741                  * are provided by the application, not the kernel. */
4742                 if( ulTotalRunTime[ 0 ] > ulTaskSwitchedInTime[ 0 ] )
4743                 {
4744                     pxCurrentTCB->ulRunTimeCounter += ( ulTotalRunTime[ 0 ] - ulTaskSwitchedInTime[ 0 ] );
4745                 }
4746                 else
4747                 {
4748                     mtCOVERAGE_TEST_MARKER();
4749                 }
4750
4751                 ulTaskSwitchedInTime[ 0 ] = ulTotalRunTime[ 0 ];
4752             }
4753             #endif /* configGENERATE_RUN_TIME_STATS */
4754
4755             /* Check for stack overflow, if configured. */
4756             taskCHECK_FOR_STACK_OVERFLOW();
4757
4758             /* Before the currently running task is switched out, save its errno. */
4759             #if ( configUSE_POSIX_ERRNO == 1 )
4760             {
4761                 pxCurrentTCB->iTaskErrno = FreeRTOS_errno;
4762             }
4763             #endif
4764
4765             /* Select a new task to run using either the generic C or port
4766              * optimised asm code. */
4767             taskSELECT_HIGHEST_PRIORITY_TASK(); /*lint !e9079 void * is used as this macro is used with timers too.  Alignment is known to be fine as the type of the pointer stored and retrieved is the same. */
4768             traceTASK_SWITCHED_IN();
4769
4770             /* After the new task is switched in, update the global errno. */
4771             #if ( configUSE_POSIX_ERRNO == 1 )
4772             {
4773                 FreeRTOS_errno = pxCurrentTCB->iTaskErrno;
4774             }
4775             #endif
4776
4777             #if ( configUSE_C_RUNTIME_TLS_SUPPORT == 1 )
4778             {
4779                 /* Switch C-Runtime's TLS Block to point to the TLS
4780                  * Block specific to this task. */
4781                 configSET_TLS_BLOCK( pxCurrentTCB->xTLSBlock );
4782             }
4783             #endif
4784         }
4785
4786         traceRETURN_vTaskSwitchContext();
4787     }
4788 #else /* if ( configNUMBER_OF_CORES == 1 ) */
4789     void vTaskSwitchContext( BaseType_t xCoreID )
4790     {
4791         traceENTER_vTaskSwitchContext();
4792
4793         /* Acquire both locks:
4794          * - The ISR lock protects the ready list from simultaneous access by
4795          *   both other ISRs and tasks.
4796          * - We also take the task lock to pause here in case another core has
4797          *   suspended the scheduler. We don't want to simply set xYieldPending
4798          *   and move on if another core suspended the scheduler. We should only
4799          *   do that if the current core has suspended the scheduler. */
4800
4801         portGET_TASK_LOCK(); /* Must always acquire the task lock first. */
4802         portGET_ISR_LOCK();
4803         {
4804             /* vTaskSwitchContext() must never be called from within a critical section.
4805              * This is not necessarily true for single core FreeRTOS, but it is for this
4806              * SMP port. */
4807             configASSERT( portGET_CRITICAL_NESTING_COUNT() == 0 );
4808
4809             if( uxSchedulerSuspended != ( UBaseType_t ) 0U )
4810             {
4811                 /* The scheduler is currently suspended - do not allow a context
4812                  * switch. */
4813                 xYieldPendings[ xCoreID ] = pdTRUE;
4814             }
4815             else
4816             {
4817                 xYieldPendings[ xCoreID ] = pdFALSE;
4818                 traceTASK_SWITCHED_OUT();
4819
4820                 #if ( configGENERATE_RUN_TIME_STATS == 1 )
4821                 {
4822                     #ifdef portALT_GET_RUN_TIME_COUNTER_VALUE
4823                         portALT_GET_RUN_TIME_COUNTER_VALUE( ulTotalRunTime[ xCoreID ] );
4824                     #else
4825                         ulTotalRunTime[ xCoreID ] = portGET_RUN_TIME_COUNTER_VALUE();
4826                     #endif
4827
4828                     /* Add the amount of time the task has been running to the
4829                      * accumulated time so far.  The time the task started running was
4830                      * stored in ulTaskSwitchedInTime.  Note that there is no overflow
4831                      * protection here so count values are only valid until the timer
4832                      * overflows.  The guard against negative values is to protect
4833                      * against suspect run time stat counter implementations - which
4834                      * are provided by the application, not the kernel. */
4835                     if( ulTotalRunTime[ xCoreID ] > ulTaskSwitchedInTime[ xCoreID ] )
4836                     {
4837                         pxCurrentTCB->ulRunTimeCounter += ( ulTotalRunTime[ xCoreID ] - ulTaskSwitchedInTime[ xCoreID ] );
4838                     }
4839                     else
4840                     {
4841                         mtCOVERAGE_TEST_MARKER();
4842                     }
4843
4844                     ulTaskSwitchedInTime[ xCoreID ] = ulTotalRunTime[ xCoreID ];
4845                 }
4846                 #endif /* configGENERATE_RUN_TIME_STATS */
4847
4848                 /* Check for stack overflow, if configured. */
4849                 taskCHECK_FOR_STACK_OVERFLOW();
4850
4851                 /* Before the currently running task is switched out, save its errno. */
4852                 #if ( configUSE_POSIX_ERRNO == 1 )
4853                 {
4854                     pxCurrentTCB->iTaskErrno = FreeRTOS_errno;
4855                 }
4856                 #endif
4857
4858                 /* Select a new task to run. */
4859                 taskSELECT_HIGHEST_PRIORITY_TASK( xCoreID );
4860                 traceTASK_SWITCHED_IN();
4861
4862                 /* After the new task is switched in, update the global errno. */
4863                 #if ( configUSE_POSIX_ERRNO == 1 )
4864                 {
4865                     FreeRTOS_errno = pxCurrentTCB->iTaskErrno;
4866                 }
4867                 #endif
4868
4869                 #if ( configUSE_C_RUNTIME_TLS_SUPPORT == 1 )
4870                 {
4871                     /* Switch C-Runtime's TLS Block to point to the TLS
4872                      * Block specific to this task. */
4873                     configSET_TLS_BLOCK( pxCurrentTCB->xTLSBlock );
4874                 }
4875                 #endif
4876             }
4877         }
4878         portRELEASE_ISR_LOCK();
4879         portRELEASE_TASK_LOCK();
4880
4881         traceRETURN_vTaskSwitchContext();
4882     }
4883 #endif /* if ( configNUMBER_OF_CORES > 1 ) */
4884 /*-----------------------------------------------------------*/
4885
4886 void vTaskPlaceOnEventList( List_t * const pxEventList,
4887                             const TickType_t xTicksToWait )
4888 {
4889     traceENTER_vTaskPlaceOnEventList( pxEventList, xTicksToWait );
4890
4891     configASSERT( pxEventList );
4892
4893     /* THIS FUNCTION MUST BE CALLED WITH EITHER INTERRUPTS DISABLED OR THE
4894      * SCHEDULER SUSPENDED AND THE QUEUE BEING ACCESSED LOCKED. */
4895
4896     /* Place the event list item of the TCB in the appropriate event list.
4897      * This is placed in the list in priority order so the highest priority task
4898      * is the first to be woken by the event.
4899      *
4900      * Note: Lists are sorted in ascending order by ListItem_t.xItemValue.
4901      * Normally, the xItemValue of a TCB's ListItem_t members is:
4902      *      xItemValue = ( configMAX_PRIORITIES - uxPriority )
4903      * Therefore, the event list is sorted in descending priority order.
4904      *
4905      * The queue that contains the event list is locked, preventing
4906      * simultaneous access from interrupts. */
4907     vListInsert( pxEventList, &( pxCurrentTCB->xEventListItem ) );
4908
4909     prvAddCurrentTaskToDelayedList( xTicksToWait, pdTRUE );
4910
4911     traceRETURN_vTaskPlaceOnEventList();
4912 }
4913 /*-----------------------------------------------------------*/
4914
4915 void vTaskPlaceOnUnorderedEventList( List_t * pxEventList,
4916                                      const TickType_t xItemValue,
4917                                      const TickType_t xTicksToWait )
4918 {
4919     traceENTER_vTaskPlaceOnUnorderedEventList( pxEventList, xItemValue, xTicksToWait );
4920
4921     configASSERT( pxEventList );
4922
4923     /* THIS FUNCTION MUST BE CALLED WITH THE SCHEDULER SUSPENDED.  It is used by
4924      * the event groups implementation. */
4925     configASSERT( uxSchedulerSuspended != ( UBaseType_t ) 0U );
4926
4927     /* Store the item value in the event list item.  It is safe to access the
4928      * event list item here as interrupts won't access the event list item of a
4929      * task that is not in the Blocked state. */
4930     listSET_LIST_ITEM_VALUE( &( pxCurrentTCB->xEventListItem ), xItemValue | taskEVENT_LIST_ITEM_VALUE_IN_USE );
4931
4932     /* Place the event list item of the TCB at the end of the appropriate event
4933      * list.  It is safe to access the event list here because it is part of an
4934      * event group implementation - and interrupts don't access event groups
4935      * directly (instead they access them indirectly by pending function calls to
4936      * the task level). */
4937     listINSERT_END( pxEventList, &( pxCurrentTCB->xEventListItem ) );
4938
4939     prvAddCurrentTaskToDelayedList( xTicksToWait, pdTRUE );
4940
4941     traceRETURN_vTaskPlaceOnUnorderedEventList();
4942 }
4943 /*-----------------------------------------------------------*/
4944
4945 #if ( configUSE_TIMERS == 1 )
4946
4947     void vTaskPlaceOnEventListRestricted( List_t * const pxEventList,
4948                                           TickType_t xTicksToWait,
4949                                           const BaseType_t xWaitIndefinitely )
4950     {
4951         traceENTER_vTaskPlaceOnEventListRestricted( pxEventList, xTicksToWait, xWaitIndefinitely );
4952
4953         configASSERT( pxEventList );
4954
4955         /* This function should not be called by application code hence the
4956          * 'Restricted' in its name.  It is not part of the public API.  It is
4957          * designed for use by kernel code, and has special calling requirements -
4958          * it should be called with the scheduler suspended. */
4959
4960
4961         /* Place the event list item of the TCB in the appropriate event list.
4962          * In this case it is assume that this is the only task that is going to
4963          * be waiting on this event list, so the faster vListInsertEnd() function
4964          * can be used in place of vListInsert. */
4965         listINSERT_END( pxEventList, &( pxCurrentTCB->xEventListItem ) );
4966
4967         /* If the task should block indefinitely then set the block time to a
4968          * value that will be recognised as an indefinite delay inside the
4969          * prvAddCurrentTaskToDelayedList() function. */
4970         if( xWaitIndefinitely != pdFALSE )
4971         {
4972             xTicksToWait = portMAX_DELAY;
4973         }
4974
4975         traceTASK_DELAY_UNTIL( ( xTickCount + xTicksToWait ) );
4976         prvAddCurrentTaskToDelayedList( xTicksToWait, xWaitIndefinitely );
4977
4978         traceRETURN_vTaskPlaceOnEventListRestricted();
4979     }
4980
4981 #endif /* configUSE_TIMERS */
4982 /*-----------------------------------------------------------*/
4983
4984 BaseType_t xTaskRemoveFromEventList( const List_t * const pxEventList )
4985 {
4986     TCB_t * pxUnblockedTCB;
4987     BaseType_t xReturn;
4988
4989     traceENTER_xTaskRemoveFromEventList( pxEventList );
4990
4991     /* THIS FUNCTION MUST BE CALLED FROM A CRITICAL SECTION.  It can also be
4992      * called from a critical section within an ISR. */
4993
4994     /* The event list is sorted in priority order, so the first in the list can
4995      * be removed as it is known to be the highest priority.  Remove the TCB from
4996      * the delayed list, and add it to the ready list.
4997      *
4998      * If an event is for a queue that is locked then this function will never
4999      * get called - the lock count on the queue will get modified instead.  This
5000      * means exclusive access to the event list is guaranteed here.
5001      *
5002      * This function assumes that a check has already been made to ensure that
5003      * pxEventList is not empty. */
5004     pxUnblockedTCB = listGET_OWNER_OF_HEAD_ENTRY( pxEventList ); /*lint !e9079 void * is used as this macro is used with timers and co-routines too.  Alignment is known to be fine as the type of the pointer stored and retrieved is the same. */
5005     configASSERT( pxUnblockedTCB );
5006     listREMOVE_ITEM( &( pxUnblockedTCB->xEventListItem ) );
5007
5008     if( uxSchedulerSuspended == ( UBaseType_t ) 0U )
5009     {
5010         listREMOVE_ITEM( &( pxUnblockedTCB->xStateListItem ) );
5011         prvAddTaskToReadyList( pxUnblockedTCB );
5012
5013         #if ( configUSE_TICKLESS_IDLE != 0 )
5014         {
5015             /* If a task is blocked on a kernel object then xNextTaskUnblockTime
5016              * might be set to the blocked task's time out time.  If the task is
5017              * unblocked for a reason other than a timeout xNextTaskUnblockTime is
5018              * normally left unchanged, because it is automatically reset to a new
5019              * value when the tick count equals xNextTaskUnblockTime.  However if
5020              * tickless idling is used it might be more important to enter sleep mode
5021              * at the earliest possible time - so reset xNextTaskUnblockTime here to
5022              * ensure it is updated at the earliest possible time. */
5023             prvResetNextTaskUnblockTime();
5024         }
5025         #endif
5026     }
5027     else
5028     {
5029         /* The delayed and ready lists cannot be accessed, so hold this task
5030          * pending until the scheduler is resumed. */
5031         listINSERT_END( &( xPendingReadyList ), &( pxUnblockedTCB->xEventListItem ) );
5032     }
5033
5034     #if ( configNUMBER_OF_CORES == 1 )
5035     {
5036         if( pxUnblockedTCB->uxPriority > pxCurrentTCB->uxPriority )
5037         {
5038             /* Return true if the task removed from the event list has a higher
5039              * priority than the calling task.  This allows the calling task to know if
5040              * it should force a context switch now. */
5041             xReturn = pdTRUE;
5042
5043             /* Mark that a yield is pending in case the user is not using the
5044              * "xHigherPriorityTaskWoken" parameter to an ISR safe FreeRTOS function. */
5045             xYieldPendings[ 0 ] = pdTRUE;
5046         }
5047         else
5048         {
5049             xReturn = pdFALSE;
5050         }
5051     }
5052     #else /* #if ( configNUMBER_OF_CORES == 1 ) */
5053     {
5054         xReturn = pdFALSE;
5055
5056         #if ( configUSE_PREEMPTION == 1 )
5057         {
5058             prvYieldForTask( pxUnblockedTCB );
5059
5060             if( xYieldPendings[ portGET_CORE_ID() ] != pdFALSE )
5061             {
5062                 xReturn = pdTRUE;
5063             }
5064         }
5065         #endif /* #if ( configUSE_PREEMPTION == 1 ) */
5066     }
5067     #endif /* #if ( configNUMBER_OF_CORES == 1 ) */
5068
5069     traceRETURN_xTaskRemoveFromEventList( xReturn );
5070     return xReturn;
5071 }
5072 /*-----------------------------------------------------------*/
5073
5074 void vTaskRemoveFromUnorderedEventList( ListItem_t * pxEventListItem,
5075                                         const TickType_t xItemValue )
5076 {
5077     TCB_t * pxUnblockedTCB;
5078
5079     traceENTER_vTaskRemoveFromUnorderedEventList( pxEventListItem, xItemValue );
5080
5081     /* THIS FUNCTION MUST BE CALLED WITH THE SCHEDULER SUSPENDED.  It is used by
5082      * the event flags implementation. */
5083     configASSERT( uxSchedulerSuspended != ( UBaseType_t ) 0U );
5084
5085     /* Store the new item value in the event list. */
5086     listSET_LIST_ITEM_VALUE( pxEventListItem, xItemValue | taskEVENT_LIST_ITEM_VALUE_IN_USE );
5087
5088     /* Remove the event list form the event flag.  Interrupts do not access
5089      * event flags. */
5090     pxUnblockedTCB = listGET_LIST_ITEM_OWNER( pxEventListItem ); /*lint !e9079 void * is used as this macro is used with timers and co-routines too.  Alignment is known to be fine as the type of the pointer stored and retrieved is the same. */
5091     configASSERT( pxUnblockedTCB );
5092     listREMOVE_ITEM( pxEventListItem );
5093
5094     #if ( configUSE_TICKLESS_IDLE != 0 )
5095     {
5096         /* If a task is blocked on a kernel object then xNextTaskUnblockTime
5097          * might be set to the blocked task's time out time.  If the task is
5098          * unblocked for a reason other than a timeout xNextTaskUnblockTime is
5099          * normally left unchanged, because it is automatically reset to a new
5100          * value when the tick count equals xNextTaskUnblockTime.  However if
5101          * tickless idling is used it might be more important to enter sleep mode
5102          * at the earliest possible time - so reset xNextTaskUnblockTime here to
5103          * ensure it is updated at the earliest possible time. */
5104         prvResetNextTaskUnblockTime();
5105     }
5106     #endif
5107
5108     /* Remove the task from the delayed list and add it to the ready list.  The
5109      * scheduler is suspended so interrupts will not be accessing the ready
5110      * lists. */
5111     listREMOVE_ITEM( &( pxUnblockedTCB->xStateListItem ) );
5112     prvAddTaskToReadyList( pxUnblockedTCB );
5113
5114     #if ( configNUMBER_OF_CORES == 1 )
5115     {
5116         if( pxUnblockedTCB->uxPriority > pxCurrentTCB->uxPriority )
5117         {
5118             /* The unblocked task has a priority above that of the calling task, so
5119              * a context switch is required.  This function is called with the
5120              * scheduler suspended so xYieldPending is set so the context switch
5121              * occurs immediately that the scheduler is resumed (unsuspended). */
5122             xYieldPendings[ 0 ] = pdTRUE;
5123         }
5124     }
5125     #else /* #if ( configNUMBER_OF_CORES == 1 ) */
5126     {
5127         #if ( configUSE_PREEMPTION == 1 )
5128         {
5129             taskENTER_CRITICAL();
5130             {
5131                 prvYieldForTask( pxUnblockedTCB );
5132             }
5133             taskEXIT_CRITICAL();
5134         }
5135         #endif
5136     }
5137     #endif /* #if ( configNUMBER_OF_CORES == 1 ) */
5138
5139     traceRETURN_vTaskRemoveFromUnorderedEventList();
5140 }
5141 /*-----------------------------------------------------------*/
5142
5143 void vTaskSetTimeOutState( TimeOut_t * const pxTimeOut )
5144 {
5145     traceENTER_vTaskSetTimeOutState( pxTimeOut );
5146
5147     configASSERT( pxTimeOut );
5148     taskENTER_CRITICAL();
5149     {
5150         pxTimeOut->xOverflowCount = xNumOfOverflows;
5151         pxTimeOut->xTimeOnEntering = xTickCount;
5152     }
5153     taskEXIT_CRITICAL();
5154
5155     traceRETURN_vTaskSetTimeOutState();
5156 }
5157 /*-----------------------------------------------------------*/
5158
5159 void vTaskInternalSetTimeOutState( TimeOut_t * const pxTimeOut )
5160 {
5161     traceENTER_vTaskInternalSetTimeOutState( pxTimeOut );
5162
5163     /* For internal use only as it does not use a critical section. */
5164     pxTimeOut->xOverflowCount = xNumOfOverflows;
5165     pxTimeOut->xTimeOnEntering = xTickCount;
5166
5167     traceRETURN_vTaskInternalSetTimeOutState();
5168 }
5169 /*-----------------------------------------------------------*/
5170
5171 BaseType_t xTaskCheckForTimeOut( TimeOut_t * const pxTimeOut,
5172                                  TickType_t * const pxTicksToWait )
5173 {
5174     BaseType_t xReturn;
5175
5176     traceENTER_xTaskCheckForTimeOut( pxTimeOut, pxTicksToWait );
5177
5178     configASSERT( pxTimeOut );
5179     configASSERT( pxTicksToWait );
5180
5181     taskENTER_CRITICAL();
5182     {
5183         /* Minor optimisation.  The tick count cannot change in this block. */
5184         const TickType_t xConstTickCount = xTickCount;
5185         const TickType_t xElapsedTime = xConstTickCount - pxTimeOut->xTimeOnEntering;
5186
5187         #if ( INCLUDE_xTaskAbortDelay == 1 )
5188             if( pxCurrentTCB->ucDelayAborted != ( uint8_t ) pdFALSE )
5189             {
5190                 /* The delay was aborted, which is not the same as a time out,
5191                  * but has the same result. */
5192                 pxCurrentTCB->ucDelayAborted = pdFALSE;
5193                 xReturn = pdTRUE;
5194             }
5195             else
5196         #endif
5197
5198         #if ( INCLUDE_vTaskSuspend == 1 )
5199             if( *pxTicksToWait == portMAX_DELAY )
5200             {
5201                 /* If INCLUDE_vTaskSuspend is set to 1 and the block time
5202                  * specified is the maximum block time then the task should block
5203                  * indefinitely, and therefore never time out. */
5204                 xReturn = pdFALSE;
5205             }
5206             else
5207         #endif
5208
5209         if( ( xNumOfOverflows != pxTimeOut->xOverflowCount ) && ( xConstTickCount >= pxTimeOut->xTimeOnEntering ) ) /*lint !e525 Indentation preferred as is to make code within pre-processor directives clearer. */
5210         {
5211             /* The tick count is greater than the time at which
5212              * vTaskSetTimeout() was called, but has also overflowed since
5213              * vTaskSetTimeOut() was called.  It must have wrapped all the way
5214              * around and gone past again. This passed since vTaskSetTimeout()
5215              * was called. */
5216             xReturn = pdTRUE;
5217             *pxTicksToWait = ( TickType_t ) 0;
5218         }
5219         else if( xElapsedTime < *pxTicksToWait ) /*lint !e961 Explicit casting is only redundant with some compilers, whereas others require it to prevent integer conversion errors. */
5220         {
5221             /* Not a genuine timeout. Adjust parameters for time remaining. */
5222             *pxTicksToWait -= xElapsedTime;
5223             vTaskInternalSetTimeOutState( pxTimeOut );
5224             xReturn = pdFALSE;
5225         }
5226         else
5227         {
5228             *pxTicksToWait = ( TickType_t ) 0;
5229             xReturn = pdTRUE;
5230         }
5231     }
5232     taskEXIT_CRITICAL();
5233
5234     traceRETURN_xTaskCheckForTimeOut( xReturn );
5235
5236     return xReturn;
5237 }
5238 /*-----------------------------------------------------------*/
5239
5240 void vTaskMissedYield( void )
5241 {
5242     traceENTER_vTaskMissedYield();
5243
5244     /* Must be called from within a critical section. */
5245     xYieldPendings[ portGET_CORE_ID() ] = pdTRUE;
5246
5247     traceRETURN_vTaskMissedYield();
5248 }
5249 /*-----------------------------------------------------------*/
5250
5251 #if ( configUSE_TRACE_FACILITY == 1 )
5252
5253     UBaseType_t uxTaskGetTaskNumber( TaskHandle_t xTask )
5254     {
5255         UBaseType_t uxReturn;
5256         TCB_t const * pxTCB;
5257
5258         traceENTER_uxTaskGetTaskNumber( xTask );
5259
5260         if( xTask != NULL )
5261         {
5262             pxTCB = xTask;
5263             uxReturn = pxTCB->uxTaskNumber;
5264         }
5265         else
5266         {
5267             uxReturn = 0U;
5268         }
5269
5270         traceRETURN_uxTaskGetTaskNumber( uxReturn );
5271
5272         return uxReturn;
5273     }
5274
5275 #endif /* configUSE_TRACE_FACILITY */
5276 /*-----------------------------------------------------------*/
5277
5278 #if ( configUSE_TRACE_FACILITY == 1 )
5279
5280     void vTaskSetTaskNumber( TaskHandle_t xTask,
5281                              const UBaseType_t uxHandle )
5282     {
5283         TCB_t * pxTCB;
5284
5285         traceENTER_vTaskSetTaskNumber( xTask, uxHandle );
5286
5287         if( xTask != NULL )
5288         {
5289             pxTCB = xTask;
5290             pxTCB->uxTaskNumber = uxHandle;
5291         }
5292
5293         traceRETURN_vTaskSetTaskNumber();
5294     }
5295
5296 #endif /* configUSE_TRACE_FACILITY */
5297 /*-----------------------------------------------------------*/
5298
5299 /*
5300  * -----------------------------------------------------------
5301  * The MinimalIdle task.
5302  * ----------------------------------------------------------
5303  *
5304  * The minimal idle task is used for all the additional cores in a SMP
5305  * system. There must be only 1 idle task and the rest are minimal idle
5306  * tasks.
5307  *
5308  * The portTASK_FUNCTION() macro is used to allow port/compiler specific
5309  * language extensions.  The equivalent prototype for this function is:
5310  *
5311  * void prvMinimalIdleTask( void *pvParameters );
5312  */
5313
5314 #if ( configNUMBER_OF_CORES > 1 )
5315     static portTASK_FUNCTION( prvMinimalIdleTask, pvParameters )
5316     {
5317         ( void ) pvParameters;
5318
5319         taskYIELD();
5320
5321         for( ; INFINITE_LOOP(); )
5322         {
5323             #if ( configUSE_PREEMPTION == 0 )
5324             {
5325                 /* If we are not using preemption we keep forcing a task switch to
5326                  * see if any other task has become available.  If we are using
5327                  * preemption we don't need to do this as any task becoming available
5328                  * will automatically get the processor anyway. */
5329                 taskYIELD();
5330             }
5331             #endif /* configUSE_PREEMPTION */
5332
5333             #if ( ( configUSE_PREEMPTION == 1 ) && ( configIDLE_SHOULD_YIELD == 1 ) )
5334             {
5335                 /* When using preemption tasks of equal priority will be
5336                  * timesliced.  If a task that is sharing the idle priority is ready
5337                  * to run then the idle task should yield before the end of the
5338                  * timeslice.
5339                  *
5340                  * A critical region is not required here as we are just reading from
5341                  * the list, and an occasional incorrect value will not matter.  If
5342                  * the ready list at the idle priority contains one more task than the
5343                  * number of idle tasks, which is equal to the configured numbers of cores
5344                  * then a task other than the idle task is ready to execute. */
5345                 if( listCURRENT_LIST_LENGTH( &( pxReadyTasksLists[ tskIDLE_PRIORITY ] ) ) > ( UBaseType_t ) configNUMBER_OF_CORES )
5346                 {
5347                     taskYIELD();
5348                 }
5349                 else
5350                 {
5351                     mtCOVERAGE_TEST_MARKER();
5352                 }
5353             }
5354             #endif /* ( ( configUSE_PREEMPTION == 1 ) && ( configIDLE_SHOULD_YIELD == 1 ) ) */
5355
5356             #if ( configUSE_MINIMAL_IDLE_HOOK == 1 )
5357             {
5358                 /* Call the user defined function from within the idle task.  This
5359                  * allows the application designer to add background functionality
5360                  * without the overhead of a separate task.
5361                  *
5362                  * This hook is intended to manage core activity such as disabling cores that go idle.
5363                  *
5364                  * NOTE: vApplicationMinimalIdleHook() MUST NOT, UNDER ANY CIRCUMSTANCES,
5365                  * CALL A FUNCTION THAT MIGHT BLOCK. */
5366                 vApplicationMinimalIdleHook();
5367             }
5368             #endif /* configUSE_MINIMAL_IDLE_HOOK */
5369         }
5370     }
5371 #endif /* #if ( configNUMBER_OF_CORES > 1 ) */
5372
5373 /*
5374  * -----------------------------------------------------------
5375  * The Idle task.
5376  * ----------------------------------------------------------
5377  *
5378  * The portTASK_FUNCTION() macro is used to allow port/compiler specific
5379  * language extensions.  The equivalent prototype for this function is:
5380  *
5381  * void prvIdleTask( void *pvParameters );
5382  *
5383  */
5384
5385 static portTASK_FUNCTION( prvIdleTask, pvParameters )
5386 {
5387     /* Stop warnings. */
5388     ( void ) pvParameters;
5389
5390     /** THIS IS THE RTOS IDLE TASK - WHICH IS CREATED AUTOMATICALLY WHEN THE
5391      * SCHEDULER IS STARTED. **/
5392
5393     /* In case a task that has a secure context deletes itself, in which case
5394      * the idle task is responsible for deleting the task's secure context, if
5395      * any. */
5396     portALLOCATE_SECURE_CONTEXT( configMINIMAL_SECURE_STACK_SIZE );
5397
5398     #if ( configNUMBER_OF_CORES > 1 )
5399     {
5400         /* SMP all cores start up in the idle task. This initial yield gets the application
5401          * tasks started. */
5402         taskYIELD();
5403     }
5404     #endif /* #if ( configNUMBER_OF_CORES > 1 ) */
5405
5406     for( ; INFINITE_LOOP(); )
5407     {
5408         /* See if any tasks have deleted themselves - if so then the idle task
5409          * is responsible for freeing the deleted task's TCB and stack. */
5410         prvCheckTasksWaitingTermination();
5411
5412         #if ( configUSE_PREEMPTION == 0 )
5413         {
5414             /* If we are not using preemption we keep forcing a task switch to
5415              * see if any other task has become available.  If we are using
5416              * preemption we don't need to do this as any task becoming available
5417              * will automatically get the processor anyway. */
5418             taskYIELD();
5419         }
5420         #endif /* configUSE_PREEMPTION */
5421
5422         #if ( ( configUSE_PREEMPTION == 1 ) && ( configIDLE_SHOULD_YIELD == 1 ) )
5423         {
5424             /* When using preemption tasks of equal priority will be
5425              * timesliced.  If a task that is sharing the idle priority is ready
5426              * to run then the idle task should yield before the end of the
5427              * timeslice.
5428              *
5429              * A critical region is not required here as we are just reading from
5430              * the list, and an occasional incorrect value will not matter.  If
5431              * the ready list at the idle priority contains one more task than the
5432              * number of idle tasks, which is equal to the configured numbers of cores
5433              * then a task other than the idle task is ready to execute. */
5434             if( listCURRENT_LIST_LENGTH( &( pxReadyTasksLists[ tskIDLE_PRIORITY ] ) ) > ( UBaseType_t ) configNUMBER_OF_CORES )
5435             {
5436                 taskYIELD();
5437             }
5438             else
5439             {
5440                 mtCOVERAGE_TEST_MARKER();
5441             }
5442         }
5443         #endif /* ( ( configUSE_PREEMPTION == 1 ) && ( configIDLE_SHOULD_YIELD == 1 ) ) */
5444
5445         #if ( configUSE_IDLE_HOOK == 1 )
5446         {
5447             /* Call the user defined function from within the idle task. */
5448             vApplicationIdleHook();
5449         }
5450         #endif /* configUSE_IDLE_HOOK */
5451
5452         /* This conditional compilation should use inequality to 0, not equality
5453          * to 1.  This is to ensure portSUPPRESS_TICKS_AND_SLEEP() is called when
5454          * user defined low power mode  implementations require
5455          * configUSE_TICKLESS_IDLE to be set to a value other than 1. */
5456         #if ( configUSE_TICKLESS_IDLE != 0 )
5457         {
5458             TickType_t xExpectedIdleTime;
5459
5460             /* It is not desirable to suspend then resume the scheduler on
5461              * each iteration of the idle task.  Therefore, a preliminary
5462              * test of the expected idle time is performed without the
5463              * scheduler suspended.  The result here is not necessarily
5464              * valid. */
5465             xExpectedIdleTime = prvGetExpectedIdleTime();
5466
5467             if( xExpectedIdleTime >= configEXPECTED_IDLE_TIME_BEFORE_SLEEP )
5468             {
5469                 vTaskSuspendAll();
5470                 {
5471                     /* Now the scheduler is suspended, the expected idle
5472                      * time can be sampled again, and this time its value can
5473                      * be used. */
5474                     configASSERT( xNextTaskUnblockTime >= xTickCount );
5475                     xExpectedIdleTime = prvGetExpectedIdleTime();
5476
5477                     /* Define the following macro to set xExpectedIdleTime to 0
5478                      * if the application does not want
5479                      * portSUPPRESS_TICKS_AND_SLEEP() to be called. */
5480                     configPRE_SUPPRESS_TICKS_AND_SLEEP_PROCESSING( xExpectedIdleTime );
5481
5482                     if( xExpectedIdleTime >= configEXPECTED_IDLE_TIME_BEFORE_SLEEP )
5483                     {
5484                         traceLOW_POWER_IDLE_BEGIN();
5485                         portSUPPRESS_TICKS_AND_SLEEP( xExpectedIdleTime );
5486                         traceLOW_POWER_IDLE_END();
5487                     }
5488                     else
5489                     {
5490                         mtCOVERAGE_TEST_MARKER();
5491                     }
5492                 }
5493                 ( void ) xTaskResumeAll();
5494             }
5495             else
5496             {
5497                 mtCOVERAGE_TEST_MARKER();
5498             }
5499         }
5500         #endif /* configUSE_TICKLESS_IDLE */
5501
5502         #if ( ( configNUMBER_OF_CORES > 1 ) && ( configUSE_MINIMAL_IDLE_HOOK == 1 ) )
5503         {
5504             /* Call the user defined function from within the idle task.  This
5505              * allows the application designer to add background functionality
5506              * without the overhead of a separate task.
5507              *
5508              * This hook is intended to manage core activity such as disabling cores that go idle.
5509              *
5510              * NOTE: vApplicationMinimalIdleHook() MUST NOT, UNDER ANY CIRCUMSTANCES,
5511              * CALL A FUNCTION THAT MIGHT BLOCK. */
5512             vApplicationMinimalIdleHook();
5513         }
5514         #endif /* #if ( ( configNUMBER_OF_CORES > 1 ) && ( configUSE_MINIMAL_IDLE_HOOK == 1 ) ) */
5515     }
5516 }
5517 /*-----------------------------------------------------------*/
5518
5519 #if ( configUSE_TICKLESS_IDLE != 0 )
5520
5521     eSleepModeStatus eTaskConfirmSleepModeStatus( void )
5522     {
5523         #if ( INCLUDE_vTaskSuspend == 1 )
5524             /* The idle task exists in addition to the application tasks. */
5525             const UBaseType_t uxNonApplicationTasks = 1;
5526         #endif /* INCLUDE_vTaskSuspend */
5527
5528         traceENTER_eTaskConfirmSleepModeStatus();
5529
5530         eSleepModeStatus eReturn = eStandardSleep;
5531
5532         /* This function must be called from a critical section. */
5533
5534         if( listCURRENT_LIST_LENGTH( &xPendingReadyList ) != 0 )
5535         {
5536             /* A task was made ready while the scheduler was suspended. */
5537             eReturn = eAbortSleep;
5538         }
5539         else if( xYieldPendings[ portGET_CORE_ID() ] != pdFALSE )
5540         {
5541             /* A yield was pended while the scheduler was suspended. */
5542             eReturn = eAbortSleep;
5543         }
5544         else if( xPendedTicks != 0 )
5545         {
5546             /* A tick interrupt has already occurred but was held pending
5547              * because the scheduler is suspended. */
5548             eReturn = eAbortSleep;
5549         }
5550
5551         #if ( INCLUDE_vTaskSuspend == 1 )
5552             else if( listCURRENT_LIST_LENGTH( &xSuspendedTaskList ) == ( uxCurrentNumberOfTasks - uxNonApplicationTasks ) )
5553             {
5554                 /* If all the tasks are in the suspended list (which might mean they
5555                  * have an infinite block time rather than actually being suspended)
5556                  * then it is safe to turn all clocks off and just wait for external
5557                  * interrupts. */
5558                 eReturn = eNoTasksWaitingTimeout;
5559             }
5560         #endif /* INCLUDE_vTaskSuspend */
5561         else
5562         {
5563             mtCOVERAGE_TEST_MARKER();
5564         }
5565
5566         traceRETURN_eTaskConfirmSleepModeStatus( eReturn );
5567
5568         return eReturn;
5569     }
5570
5571 #endif /* configUSE_TICKLESS_IDLE */
5572 /*-----------------------------------------------------------*/
5573
5574 #if ( configNUM_THREAD_LOCAL_STORAGE_POINTERS != 0 )
5575
5576     void vTaskSetThreadLocalStoragePointer( TaskHandle_t xTaskToSet,
5577                                             BaseType_t xIndex,
5578                                             void * pvValue )
5579     {
5580         TCB_t * pxTCB;
5581
5582         traceENTER_vTaskSetThreadLocalStoragePointer( xTaskToSet, xIndex, pvValue );
5583
5584         if( ( xIndex >= 0 ) &&
5585             ( xIndex < ( BaseType_t ) configNUM_THREAD_LOCAL_STORAGE_POINTERS ) )
5586         {
5587             pxTCB = prvGetTCBFromHandle( xTaskToSet );
5588             configASSERT( pxTCB != NULL );
5589             pxTCB->pvThreadLocalStoragePointers[ xIndex ] = pvValue;
5590         }
5591
5592         traceRETURN_vTaskSetThreadLocalStoragePointer();
5593     }
5594
5595 #endif /* configNUM_THREAD_LOCAL_STORAGE_POINTERS */
5596 /*-----------------------------------------------------------*/
5597
5598 #if ( configNUM_THREAD_LOCAL_STORAGE_POINTERS != 0 )
5599
5600     void * pvTaskGetThreadLocalStoragePointer( TaskHandle_t xTaskToQuery,
5601                                                BaseType_t xIndex )
5602     {
5603         void * pvReturn = NULL;
5604         TCB_t * pxTCB;
5605
5606         traceENTER_pvTaskGetThreadLocalStoragePointer( xTaskToQuery, xIndex );
5607
5608         if( ( xIndex >= 0 ) &&
5609             ( xIndex < ( BaseType_t ) configNUM_THREAD_LOCAL_STORAGE_POINTERS ) )
5610         {
5611             pxTCB = prvGetTCBFromHandle( xTaskToQuery );
5612             pvReturn = pxTCB->pvThreadLocalStoragePointers[ xIndex ];
5613         }
5614         else
5615         {
5616             pvReturn = NULL;
5617         }
5618
5619         traceRETURN_pvTaskGetThreadLocalStoragePointer( pvReturn );
5620
5621         return pvReturn;
5622     }
5623
5624 #endif /* configNUM_THREAD_LOCAL_STORAGE_POINTERS */
5625 /*-----------------------------------------------------------*/
5626
5627 #if ( portUSING_MPU_WRAPPERS == 1 )
5628
5629     void vTaskAllocateMPURegions( TaskHandle_t xTaskToModify,
5630                                   const MemoryRegion_t * const pxRegions )
5631     {
5632         TCB_t * pxTCB;
5633
5634         traceENTER_vTaskAllocateMPURegions( xTaskToModify, pxRegions );
5635
5636         /* If null is passed in here then we are modifying the MPU settings of
5637          * the calling task. */
5638         pxTCB = prvGetTCBFromHandle( xTaskToModify );
5639
5640         vPortStoreTaskMPUSettings( &( pxTCB->xMPUSettings ), pxRegions, NULL, 0 );
5641
5642         traceRETURN_vTaskAllocateMPURegions();
5643     }
5644
5645 #endif /* portUSING_MPU_WRAPPERS */
5646 /*-----------------------------------------------------------*/
5647
5648 static void prvInitialiseTaskLists( void )
5649 {
5650     UBaseType_t uxPriority;
5651
5652     for( uxPriority = ( UBaseType_t ) 0U; uxPriority < ( UBaseType_t ) configMAX_PRIORITIES; uxPriority++ )
5653     {
5654         vListInitialise( &( pxReadyTasksLists[ uxPriority ] ) );
5655     }
5656
5657     vListInitialise( &xDelayedTaskList1 );
5658     vListInitialise( &xDelayedTaskList2 );
5659     vListInitialise( &xPendingReadyList );
5660
5661     #if ( INCLUDE_vTaskDelete == 1 )
5662     {
5663         vListInitialise( &xTasksWaitingTermination );
5664     }
5665     #endif /* INCLUDE_vTaskDelete */
5666
5667     #if ( INCLUDE_vTaskSuspend == 1 )
5668     {
5669         vListInitialise( &xSuspendedTaskList );
5670     }
5671     #endif /* INCLUDE_vTaskSuspend */
5672
5673     /* Start with pxDelayedTaskList using list1 and the pxOverflowDelayedTaskList
5674      * using list2. */
5675     pxDelayedTaskList = &xDelayedTaskList1;
5676     pxOverflowDelayedTaskList = &xDelayedTaskList2;
5677 }
5678 /*-----------------------------------------------------------*/
5679
5680 static void prvCheckTasksWaitingTermination( void )
5681 {
5682     /** THIS FUNCTION IS CALLED FROM THE RTOS IDLE TASK **/
5683
5684     #if ( INCLUDE_vTaskDelete == 1 )
5685     {
5686         TCB_t * pxTCB;
5687
5688         /* uxDeletedTasksWaitingCleanUp is used to prevent taskENTER_CRITICAL()
5689          * being called too often in the idle task. */
5690         while( uxDeletedTasksWaitingCleanUp > ( UBaseType_t ) 0U )
5691         {
5692             #if ( configNUMBER_OF_CORES == 1 )
5693             {
5694                 taskENTER_CRITICAL();
5695                 {
5696                     {
5697                         pxTCB = listGET_OWNER_OF_HEAD_ENTRY( ( &xTasksWaitingTermination ) ); /*lint !e9079 void * is used as this macro is used with timers too.  Alignment is known to be fine as the type of the pointer stored and retrieved is the same. */
5698                         ( void ) uxListRemove( &( pxTCB->xStateListItem ) );
5699                         --uxCurrentNumberOfTasks;
5700                         --uxDeletedTasksWaitingCleanUp;
5701                     }
5702                 }
5703                 taskEXIT_CRITICAL();
5704
5705                 prvDeleteTCB( pxTCB );
5706             }
5707             #else /* #if( configNUMBER_OF_CORES == 1 ) */
5708             {
5709                 pxTCB = NULL;
5710
5711                 taskENTER_CRITICAL();
5712                 {
5713                     /* For SMP, multiple idles can be running simultaneously
5714                      * and we need to check that other idles did not cleanup while we were
5715                      * waiting to enter the critical section. */
5716                     if( uxDeletedTasksWaitingCleanUp > ( UBaseType_t ) 0U )
5717                     {
5718                         pxTCB = listGET_OWNER_OF_HEAD_ENTRY( ( &xTasksWaitingTermination ) ); /*lint !e9079 void * is used as this macro is used with timers too.  Alignment is known to be fine as the type of the pointer stored and retrieved is the same. */
5719
5720                         if( pxTCB->xTaskRunState == taskTASK_NOT_RUNNING )
5721                         {
5722                             ( void ) uxListRemove( &( pxTCB->xStateListItem ) );
5723                             --uxCurrentNumberOfTasks;
5724                             --uxDeletedTasksWaitingCleanUp;
5725                         }
5726                         else
5727                         {
5728                             /* The TCB to be deleted still has not yet been switched out
5729                              * by the scheduler, so we will just exit this loop early and
5730                              * try again next time. */
5731                             taskEXIT_CRITICAL();
5732                             break;
5733                         }
5734                     }
5735                 }
5736                 taskEXIT_CRITICAL();
5737
5738                 if( pxTCB != NULL )
5739                 {
5740                     prvDeleteTCB( pxTCB );
5741                 }
5742             }
5743             #endif /* #if( configNUMBER_OF_CORES == 1 ) */
5744         }
5745     }
5746     #endif /* INCLUDE_vTaskDelete */
5747 }
5748 /*-----------------------------------------------------------*/
5749
5750 #if ( configUSE_TRACE_FACILITY == 1 )
5751
5752     void vTaskGetInfo( TaskHandle_t xTask,
5753                        TaskStatus_t * pxTaskStatus,
5754                        BaseType_t xGetFreeStackSpace,
5755                        eTaskState eState )
5756     {
5757         TCB_t * pxTCB;
5758
5759         traceENTER_vTaskGetInfo( xTask, pxTaskStatus, xGetFreeStackSpace, eState );
5760
5761         /* xTask is NULL then get the state of the calling task. */
5762         pxTCB = prvGetTCBFromHandle( xTask );
5763
5764         pxTaskStatus->xHandle = pxTCB;
5765         pxTaskStatus->pcTaskName = ( const char * ) &( pxTCB->pcTaskName[ 0 ] );
5766         pxTaskStatus->uxCurrentPriority = pxTCB->uxPriority;
5767         pxTaskStatus->pxStackBase = pxTCB->pxStack;
5768         #if ( ( portSTACK_GROWTH > 0 ) || ( configRECORD_STACK_HIGH_ADDRESS == 1 ) )
5769             pxTaskStatus->pxTopOfStack = ( StackType_t * ) pxTCB->pxTopOfStack;
5770             pxTaskStatus->pxEndOfStack = pxTCB->pxEndOfStack;
5771         #endif
5772         pxTaskStatus->xTaskNumber = pxTCB->uxTCBNumber;
5773
5774         #if ( ( configUSE_CORE_AFFINITY == 1 ) && ( configNUMBER_OF_CORES > 1 ) )
5775         {
5776             pxTaskStatus->uxCoreAffinityMask = pxTCB->uxCoreAffinityMask;
5777         }
5778         #endif
5779
5780         #if ( configUSE_MUTEXES == 1 )
5781         {
5782             pxTaskStatus->uxBasePriority = pxTCB->uxBasePriority;
5783         }
5784         #else
5785         {
5786             pxTaskStatus->uxBasePriority = 0;
5787         }
5788         #endif
5789
5790         #if ( configGENERATE_RUN_TIME_STATS == 1 )
5791         {
5792             pxTaskStatus->ulRunTimeCounter = pxTCB->ulRunTimeCounter;
5793         }
5794         #else
5795         {
5796             pxTaskStatus->ulRunTimeCounter = ( configRUN_TIME_COUNTER_TYPE ) 0;
5797         }
5798         #endif
5799
5800         /* Obtaining the task state is a little fiddly, so is only done if the
5801          * value of eState passed into this function is eInvalid - otherwise the
5802          * state is just set to whatever is passed in. */
5803         if( eState != eInvalid )
5804         {
5805             if( taskTASK_IS_RUNNING( pxTCB ) == pdTRUE )
5806             {
5807                 pxTaskStatus->eCurrentState = eRunning;
5808             }
5809             else
5810             {
5811                 pxTaskStatus->eCurrentState = eState;
5812
5813                 #if ( INCLUDE_vTaskSuspend == 1 )
5814                 {
5815                     /* If the task is in the suspended list then there is a
5816                      *  chance it is actually just blocked indefinitely - so really
5817                      *  it should be reported as being in the Blocked state. */
5818                     if( eState == eSuspended )
5819                     {
5820                         vTaskSuspendAll();
5821                         {
5822                             if( listLIST_ITEM_CONTAINER( &( pxTCB->xEventListItem ) ) != NULL )
5823                             {
5824                                 pxTaskStatus->eCurrentState = eBlocked;
5825                             }
5826                         }
5827                         ( void ) xTaskResumeAll();
5828                     }
5829                 }
5830                 #endif /* INCLUDE_vTaskSuspend */
5831
5832                 /* Tasks can be in pending ready list and other state list at the
5833                  * same time. These tasks are in ready state no matter what state
5834                  * list the task is in. */
5835                 taskENTER_CRITICAL();
5836                 {
5837                     if( listIS_CONTAINED_WITHIN( &xPendingReadyList, &( pxTCB->xEventListItem ) ) != pdFALSE )
5838                     {
5839                         pxTaskStatus->eCurrentState = eReady;
5840                     }
5841                 }
5842                 taskEXIT_CRITICAL();
5843             }
5844         }
5845         else
5846         {
5847             pxTaskStatus->eCurrentState = eTaskGetState( pxTCB );
5848         }
5849
5850         /* Obtaining the stack space takes some time, so the xGetFreeStackSpace
5851          * parameter is provided to allow it to be skipped. */
5852         if( xGetFreeStackSpace != pdFALSE )
5853         {
5854             #if ( portSTACK_GROWTH > 0 )
5855             {
5856                 pxTaskStatus->usStackHighWaterMark = prvTaskCheckFreeStackSpace( ( uint8_t * ) pxTCB->pxEndOfStack );
5857             }
5858             #else
5859             {
5860                 pxTaskStatus->usStackHighWaterMark = prvTaskCheckFreeStackSpace( ( uint8_t * ) pxTCB->pxStack );
5861             }
5862             #endif
5863         }
5864         else
5865         {
5866             pxTaskStatus->usStackHighWaterMark = 0;
5867         }
5868
5869         traceRETURN_vTaskGetInfo();
5870     }
5871
5872 #endif /* configUSE_TRACE_FACILITY */
5873 /*-----------------------------------------------------------*/
5874
5875 #if ( configUSE_TRACE_FACILITY == 1 )
5876
5877     static UBaseType_t prvListTasksWithinSingleList( TaskStatus_t * pxTaskStatusArray,
5878                                                      List_t * pxList,
5879                                                      eTaskState eState )
5880     {
5881         configLIST_VOLATILE TCB_t * pxNextTCB;
5882         configLIST_VOLATILE TCB_t * pxFirstTCB;
5883         UBaseType_t uxTask = 0;
5884
5885         if( listCURRENT_LIST_LENGTH( pxList ) > ( UBaseType_t ) 0 )
5886         {
5887             listGET_OWNER_OF_NEXT_ENTRY( pxFirstTCB, pxList ); /*lint !e9079 void * is used as this macro is used with timers and co-routines too.  Alignment is known to be fine as the type of the pointer stored and retrieved is the same. */
5888
5889             /* Populate an TaskStatus_t structure within the
5890              * pxTaskStatusArray array for each task that is referenced from
5891              * pxList.  See the definition of TaskStatus_t in task.h for the
5892              * meaning of each TaskStatus_t structure member. */
5893             do
5894             {
5895                 listGET_OWNER_OF_NEXT_ENTRY( pxNextTCB, pxList ); /*lint !e9079 void * is used as this macro is used with timers and co-routines too.  Alignment is known to be fine as the type of the pointer stored and retrieved is the same. */
5896                 vTaskGetInfo( ( TaskHandle_t ) pxNextTCB, &( pxTaskStatusArray[ uxTask ] ), pdTRUE, eState );
5897                 uxTask++;
5898             } while( pxNextTCB != pxFirstTCB );
5899         }
5900         else
5901         {
5902             mtCOVERAGE_TEST_MARKER();
5903         }
5904
5905         return uxTask;
5906     }
5907
5908 #endif /* configUSE_TRACE_FACILITY */
5909 /*-----------------------------------------------------------*/
5910
5911 #if ( ( configUSE_TRACE_FACILITY == 1 ) || ( INCLUDE_uxTaskGetStackHighWaterMark == 1 ) || ( INCLUDE_uxTaskGetStackHighWaterMark2 == 1 ) )
5912
5913     static configSTACK_DEPTH_TYPE prvTaskCheckFreeStackSpace( const uint8_t * pucStackByte )
5914     {
5915         uint32_t ulCount = 0U;
5916
5917         while( *pucStackByte == ( uint8_t ) tskSTACK_FILL_BYTE )
5918         {
5919             pucStackByte -= portSTACK_GROWTH;
5920             ulCount++;
5921         }
5922
5923         ulCount /= ( uint32_t ) sizeof( StackType_t ); /*lint !e961 Casting is not redundant on smaller architectures. */
5924
5925         return ( configSTACK_DEPTH_TYPE ) ulCount;
5926     }
5927
5928 #endif /* ( ( configUSE_TRACE_FACILITY == 1 ) || ( INCLUDE_uxTaskGetStackHighWaterMark == 1 ) || ( INCLUDE_uxTaskGetStackHighWaterMark2 == 1 ) ) */
5929 /*-----------------------------------------------------------*/
5930
5931 #if ( INCLUDE_uxTaskGetStackHighWaterMark2 == 1 )
5932
5933 /* uxTaskGetStackHighWaterMark() and uxTaskGetStackHighWaterMark2() are the
5934  * same except for their return type.  Using configSTACK_DEPTH_TYPE allows the
5935  * user to determine the return type.  It gets around the problem of the value
5936  * overflowing on 8-bit types without breaking backward compatibility for
5937  * applications that expect an 8-bit return type. */
5938     configSTACK_DEPTH_TYPE uxTaskGetStackHighWaterMark2( TaskHandle_t xTask )
5939     {
5940         TCB_t * pxTCB;
5941         uint8_t * pucEndOfStack;
5942         configSTACK_DEPTH_TYPE uxReturn;
5943
5944         traceENTER_uxTaskGetStackHighWaterMark2( xTask );
5945
5946         /* uxTaskGetStackHighWaterMark() and uxTaskGetStackHighWaterMark2() are
5947          * the same except for their return type.  Using configSTACK_DEPTH_TYPE
5948          * allows the user to determine the return type.  It gets around the
5949          * problem of the value overflowing on 8-bit types without breaking
5950          * backward compatibility for applications that expect an 8-bit return
5951          * type. */
5952
5953         pxTCB = prvGetTCBFromHandle( xTask );
5954
5955         #if portSTACK_GROWTH < 0
5956         {
5957             pucEndOfStack = ( uint8_t * ) pxTCB->pxStack;
5958         }
5959         #else
5960         {
5961             pucEndOfStack = ( uint8_t * ) pxTCB->pxEndOfStack;
5962         }
5963         #endif
5964
5965         uxReturn = prvTaskCheckFreeStackSpace( pucEndOfStack );
5966
5967         traceRETURN_uxTaskGetStackHighWaterMark2( uxReturn );
5968
5969         return uxReturn;
5970     }
5971
5972 #endif /* INCLUDE_uxTaskGetStackHighWaterMark2 */
5973 /*-----------------------------------------------------------*/
5974
5975 #if ( INCLUDE_uxTaskGetStackHighWaterMark == 1 )
5976
5977     UBaseType_t uxTaskGetStackHighWaterMark( TaskHandle_t xTask )
5978     {
5979         TCB_t * pxTCB;
5980         uint8_t * pucEndOfStack;
5981         UBaseType_t uxReturn;
5982
5983         traceENTER_uxTaskGetStackHighWaterMark( xTask );
5984
5985         pxTCB = prvGetTCBFromHandle( xTask );
5986
5987         #if portSTACK_GROWTH < 0
5988         {
5989             pucEndOfStack = ( uint8_t * ) pxTCB->pxStack;
5990         }
5991         #else
5992         {
5993             pucEndOfStack = ( uint8_t * ) pxTCB->pxEndOfStack;
5994         }
5995         #endif
5996
5997         uxReturn = ( UBaseType_t ) prvTaskCheckFreeStackSpace( pucEndOfStack );
5998
5999         traceRETURN_uxTaskGetStackHighWaterMark( uxReturn );
6000
6001         return uxReturn;
6002     }
6003
6004 #endif /* INCLUDE_uxTaskGetStackHighWaterMark */
6005 /*-----------------------------------------------------------*/
6006
6007 #if ( INCLUDE_vTaskDelete == 1 )
6008
6009     static void prvDeleteTCB( TCB_t * pxTCB )
6010     {
6011         /* This call is required specifically for the TriCore port.  It must be
6012          * above the vPortFree() calls.  The call is also used by ports/demos that
6013          * want to allocate and clean RAM statically. */
6014         portCLEAN_UP_TCB( pxTCB );
6015
6016         #if ( configUSE_C_RUNTIME_TLS_SUPPORT == 1 )
6017         {
6018             /* Free up the memory allocated for the task's TLS Block. */
6019             configDEINIT_TLS_BLOCK( pxTCB->xTLSBlock );
6020         }
6021         #endif
6022
6023         #if ( ( configSUPPORT_DYNAMIC_ALLOCATION == 1 ) && ( configSUPPORT_STATIC_ALLOCATION == 0 ) && ( portUSING_MPU_WRAPPERS == 0 ) )
6024         {
6025             /* The task can only have been allocated dynamically - free both
6026              * the stack and TCB. */
6027             vPortFreeStack( pxTCB->pxStack );
6028             vPortFree( pxTCB );
6029         }
6030         #elif ( tskSTATIC_AND_DYNAMIC_ALLOCATION_POSSIBLE != 0 ) /*lint !e731 !e9029 Macro has been consolidated for readability reasons. */
6031         {
6032             /* The task could have been allocated statically or dynamically, so
6033              * check what was statically allocated before trying to free the
6034              * memory. */
6035             if( pxTCB->ucStaticallyAllocated == tskDYNAMICALLY_ALLOCATED_STACK_AND_TCB )
6036             {
6037                 /* Both the stack and TCB were allocated dynamically, so both
6038                  * must be freed. */
6039                 vPortFreeStack( pxTCB->pxStack );
6040                 vPortFree( pxTCB );
6041             }
6042             else if( pxTCB->ucStaticallyAllocated == tskSTATICALLY_ALLOCATED_STACK_ONLY )
6043             {
6044                 /* Only the stack was statically allocated, so the TCB is the
6045                  * only memory that must be freed. */
6046                 vPortFree( pxTCB );
6047             }
6048             else
6049             {
6050                 /* Neither the stack nor the TCB were allocated dynamically, so
6051                  * nothing needs to be freed. */
6052                 configASSERT( pxTCB->ucStaticallyAllocated == tskSTATICALLY_ALLOCATED_STACK_AND_TCB );
6053                 mtCOVERAGE_TEST_MARKER();
6054             }
6055         }
6056         #endif /* configSUPPORT_DYNAMIC_ALLOCATION */
6057     }
6058
6059 #endif /* INCLUDE_vTaskDelete */
6060 /*-----------------------------------------------------------*/
6061
6062 static void prvResetNextTaskUnblockTime( void )
6063 {
6064     if( listLIST_IS_EMPTY( pxDelayedTaskList ) != pdFALSE )
6065     {
6066         /* The new current delayed list is empty.  Set xNextTaskUnblockTime to
6067          * the maximum possible value so it is  extremely unlikely that the
6068          * if( xTickCount >= xNextTaskUnblockTime ) test will pass until
6069          * there is an item in the delayed list. */
6070         xNextTaskUnblockTime = portMAX_DELAY;
6071     }
6072     else
6073     {
6074         /* The new current delayed list is not empty, get the value of
6075          * the item at the head of the delayed list.  This is the time at
6076          * which the task at the head of the delayed list should be removed
6077          * from the Blocked state. */
6078         xNextTaskUnblockTime = listGET_ITEM_VALUE_OF_HEAD_ENTRY( pxDelayedTaskList );
6079     }
6080 }
6081 /*-----------------------------------------------------------*/
6082
6083 #if ( ( INCLUDE_xTaskGetCurrentTaskHandle == 1 ) || ( configUSE_MUTEXES == 1 ) ) || ( configNUMBER_OF_CORES > 1 )
6084
6085     #if ( configNUMBER_OF_CORES == 1 )
6086         TaskHandle_t xTaskGetCurrentTaskHandle( void )
6087         {
6088             TaskHandle_t xReturn;
6089
6090             traceENTER_xTaskGetCurrentTaskHandle();
6091
6092             /* A critical section is not required as this is not called from
6093              * an interrupt and the current TCB will always be the same for any
6094              * individual execution thread. */
6095             xReturn = pxCurrentTCB;
6096
6097             traceRETURN_xTaskGetCurrentTaskHandle( xReturn );
6098
6099             return xReturn;
6100         }
6101     #else /* #if ( configNUMBER_OF_CORES == 1 ) */
6102         TaskHandle_t xTaskGetCurrentTaskHandle( void )
6103         {
6104             TaskHandle_t xReturn;
6105             UBaseType_t uxSavedInterruptStatus;
6106
6107             traceENTER_xTaskGetCurrentTaskHandle();
6108
6109             uxSavedInterruptStatus = portSET_INTERRUPT_MASK();
6110             {
6111                 xReturn = pxCurrentTCBs[ portGET_CORE_ID() ];
6112             }
6113             portCLEAR_INTERRUPT_MASK( uxSavedInterruptStatus );
6114
6115             traceRETURN_xTaskGetCurrentTaskHandle( xReturn );
6116
6117             return xReturn;
6118         }
6119
6120         TaskHandle_t xTaskGetCurrentTaskHandleCPU( BaseType_t xCoreID )
6121         {
6122             TaskHandle_t xReturn = NULL;
6123
6124             traceENTER_xTaskGetCurrentTaskHandleCPU( xCoreID );
6125
6126             if( taskVALID_CORE_ID( xCoreID ) != pdFALSE )
6127             {
6128                 xReturn = pxCurrentTCBs[ xCoreID ];
6129             }
6130
6131             traceRETURN_xTaskGetCurrentTaskHandleCPU( xReturn );
6132
6133             return xReturn;
6134         }
6135     #endif /* #if ( configNUMBER_OF_CORES == 1 ) */
6136
6137 #endif /* ( ( INCLUDE_xTaskGetCurrentTaskHandle == 1 ) || ( configUSE_MUTEXES == 1 ) ) */
6138 /*-----------------------------------------------------------*/
6139
6140 #if ( ( INCLUDE_xTaskGetSchedulerState == 1 ) || ( configUSE_TIMERS == 1 ) )
6141
6142     BaseType_t xTaskGetSchedulerState( void )
6143     {
6144         BaseType_t xReturn;
6145
6146         traceENTER_xTaskGetSchedulerState();
6147
6148         if( xSchedulerRunning == pdFALSE )
6149         {
6150             xReturn = taskSCHEDULER_NOT_STARTED;
6151         }
6152         else
6153         {
6154             #if ( configNUMBER_OF_CORES > 1 )
6155                 taskENTER_CRITICAL();
6156             #endif
6157             {
6158                 if( uxSchedulerSuspended == ( UBaseType_t ) 0U )
6159                 {
6160                     xReturn = taskSCHEDULER_RUNNING;
6161                 }
6162                 else
6163                 {
6164                     xReturn = taskSCHEDULER_SUSPENDED;
6165                 }
6166             }
6167             #if ( configNUMBER_OF_CORES > 1 )
6168                 taskEXIT_CRITICAL();
6169             #endif
6170         }
6171
6172         traceRETURN_xTaskGetSchedulerState( xReturn );
6173
6174         return xReturn;
6175     }
6176
6177 #endif /* ( ( INCLUDE_xTaskGetSchedulerState == 1 ) || ( configUSE_TIMERS == 1 ) ) */
6178 /*-----------------------------------------------------------*/
6179
6180 #if ( configUSE_MUTEXES == 1 )
6181
6182     BaseType_t xTaskPriorityInherit( TaskHandle_t const pxMutexHolder )
6183     {
6184         TCB_t * const pxMutexHolderTCB = pxMutexHolder;
6185         BaseType_t xReturn = pdFALSE;
6186
6187         traceENTER_xTaskPriorityInherit( pxMutexHolder );
6188
6189         /* If the mutex is taken by an interrupt, the mutex holder is NULL. Priority
6190          * inheritance is not applied in this scenario. */
6191         if( pxMutexHolder != NULL )
6192         {
6193             /* If the holder of the mutex has a priority below the priority of
6194              * the task attempting to obtain the mutex then it will temporarily
6195              * inherit the priority of the task attempting to obtain the mutex. */
6196             if( pxMutexHolderTCB->uxPriority < pxCurrentTCB->uxPriority )
6197             {
6198                 /* Adjust the mutex holder state to account for its new
6199                  * priority.  Only reset the event list item value if the value is
6200                  * not being used for anything else. */
6201                 if( ( listGET_LIST_ITEM_VALUE( &( pxMutexHolderTCB->xEventListItem ) ) & taskEVENT_LIST_ITEM_VALUE_IN_USE ) == 0UL )
6202                 {
6203                     listSET_LIST_ITEM_VALUE( &( pxMutexHolderTCB->xEventListItem ), ( TickType_t ) configMAX_PRIORITIES - ( TickType_t ) pxCurrentTCB->uxPriority ); /*lint !e961 MISRA exception as the casts are only redundant for some ports. */
6204                 }
6205                 else
6206                 {
6207                     mtCOVERAGE_TEST_MARKER();
6208                 }
6209
6210                 /* If the task being modified is in the ready state it will need
6211                  * to be moved into a new list. */
6212                 if( listIS_CONTAINED_WITHIN( &( pxReadyTasksLists[ pxMutexHolderTCB->uxPriority ] ), &( pxMutexHolderTCB->xStateListItem ) ) != pdFALSE )
6213                 {
6214                     if( uxListRemove( &( pxMutexHolderTCB->xStateListItem ) ) == ( UBaseType_t ) 0 )
6215                     {
6216                         /* It is known that the task is in its ready list so
6217                          * there is no need to check again and the port level
6218                          * reset macro can be called directly. */
6219                         portRESET_READY_PRIORITY( pxMutexHolderTCB->uxPriority, uxTopReadyPriority );
6220                     }
6221                     else
6222                     {
6223                         mtCOVERAGE_TEST_MARKER();
6224                     }
6225
6226                     /* Inherit the priority before being moved into the new list. */
6227                     pxMutexHolderTCB->uxPriority = pxCurrentTCB->uxPriority;
6228                     prvAddTaskToReadyList( pxMutexHolderTCB );
6229                     #if ( configNUMBER_OF_CORES > 1 )
6230                     {
6231                         /* The priority of the task is raised. Yield for this task
6232                          * if it is not running. */
6233                         if( taskTASK_IS_RUNNING( pxMutexHolderTCB ) != pdTRUE )
6234                         {
6235                             prvYieldForTask( pxMutexHolderTCB );
6236                         }
6237                     }
6238                     #endif /* if ( configNUMBER_OF_CORES > 1 ) */
6239                 }
6240                 else
6241                 {
6242                     /* Just inherit the priority. */
6243                     pxMutexHolderTCB->uxPriority = pxCurrentTCB->uxPriority;
6244                 }
6245
6246                 traceTASK_PRIORITY_INHERIT( pxMutexHolderTCB, pxCurrentTCB->uxPriority );
6247
6248                 /* Inheritance occurred. */
6249                 xReturn = pdTRUE;
6250             }
6251             else
6252             {
6253                 if( pxMutexHolderTCB->uxBasePriority < pxCurrentTCB->uxPriority )
6254                 {
6255                     /* The base priority of the mutex holder is lower than the
6256                      * priority of the task attempting to take the mutex, but the
6257                      * current priority of the mutex holder is not lower than the
6258                      * priority of the task attempting to take the mutex.
6259                      * Therefore the mutex holder must have already inherited a
6260                      * priority, but inheritance would have occurred if that had
6261                      * not been the case. */
6262                     xReturn = pdTRUE;
6263                 }
6264                 else
6265                 {
6266                     mtCOVERAGE_TEST_MARKER();
6267                 }
6268             }
6269         }
6270         else
6271         {
6272             mtCOVERAGE_TEST_MARKER();
6273         }
6274
6275         traceRETURN_xTaskPriorityInherit( xReturn );
6276
6277         return xReturn;
6278     }
6279
6280 #endif /* configUSE_MUTEXES */
6281 /*-----------------------------------------------------------*/
6282
6283 #if ( configUSE_MUTEXES == 1 )
6284
6285     BaseType_t xTaskPriorityDisinherit( TaskHandle_t const pxMutexHolder )
6286     {
6287         TCB_t * const pxTCB = pxMutexHolder;
6288         BaseType_t xReturn = pdFALSE;
6289
6290         traceENTER_xTaskPriorityDisinherit( pxMutexHolder );
6291
6292         if( pxMutexHolder != NULL )
6293         {
6294             /* A task can only have an inherited priority if it holds the mutex.
6295              * If the mutex is held by a task then it cannot be given from an
6296              * interrupt, and if a mutex is given by the holding task then it must
6297              * be the running state task. */
6298             configASSERT( pxTCB == pxCurrentTCB );
6299             configASSERT( pxTCB->uxMutexesHeld );
6300             ( pxTCB->uxMutexesHeld )--;
6301
6302             /* Has the holder of the mutex inherited the priority of another
6303              * task? */
6304             if( pxTCB->uxPriority != pxTCB->uxBasePriority )
6305             {
6306                 /* Only disinherit if no other mutexes are held. */
6307                 if( pxTCB->uxMutexesHeld == ( UBaseType_t ) 0 )
6308                 {
6309                     /* A task can only have an inherited priority if it holds
6310                      * the mutex.  If the mutex is held by a task then it cannot be
6311                      * given from an interrupt, and if a mutex is given by the
6312                      * holding task then it must be the running state task.  Remove
6313                      * the holding task from the ready list. */
6314                     if( uxListRemove( &( pxTCB->xStateListItem ) ) == ( UBaseType_t ) 0 )
6315                     {
6316                         portRESET_READY_PRIORITY( pxTCB->uxPriority, uxTopReadyPriority );
6317                     }
6318                     else
6319                     {
6320                         mtCOVERAGE_TEST_MARKER();
6321                     }
6322
6323                     /* Disinherit the priority before adding the task into the
6324                      * new  ready list. */
6325                     traceTASK_PRIORITY_DISINHERIT( pxTCB, pxTCB->uxBasePriority );
6326                     pxTCB->uxPriority = pxTCB->uxBasePriority;
6327
6328                     /* Reset the event list item value.  It cannot be in use for
6329                      * any other purpose if this task is running, and it must be
6330                      * running to give back the mutex. */
6331                     listSET_LIST_ITEM_VALUE( &( pxTCB->xEventListItem ), ( TickType_t ) configMAX_PRIORITIES - ( TickType_t ) pxTCB->uxPriority ); /*lint !e961 MISRA exception as the casts are only redundant for some ports. */
6332                     prvAddTaskToReadyList( pxTCB );
6333                     #if ( configNUMBER_OF_CORES > 1 )
6334                     {
6335                         /* The priority of the task is dropped. Yield the core on
6336                          * which the task is running. */
6337                         if( taskTASK_IS_RUNNING( pxTCB ) == pdTRUE )
6338                         {
6339                             prvYieldCore( pxTCB->xTaskRunState );
6340                         }
6341                     }
6342                     #endif /* if ( configNUMBER_OF_CORES > 1 ) */
6343
6344                     /* Return true to indicate that a context switch is required.
6345                      * This is only actually required in the corner case whereby
6346                      * multiple mutexes were held and the mutexes were given back
6347                      * in an order different to that in which they were taken.
6348                      * If a context switch did not occur when the first mutex was
6349                      * returned, even if a task was waiting on it, then a context
6350                      * switch should occur when the last mutex is returned whether
6351                      * a task is waiting on it or not. */
6352                     xReturn = pdTRUE;
6353                 }
6354                 else
6355                 {
6356                     mtCOVERAGE_TEST_MARKER();
6357                 }
6358             }
6359             else
6360             {
6361                 mtCOVERAGE_TEST_MARKER();
6362             }
6363         }
6364         else
6365         {
6366             mtCOVERAGE_TEST_MARKER();
6367         }
6368
6369         traceRETURN_xTaskPriorityDisinherit( xReturn );
6370
6371         return xReturn;
6372     }
6373
6374 #endif /* configUSE_MUTEXES */
6375 /*-----------------------------------------------------------*/
6376
6377 #if ( configUSE_MUTEXES == 1 )
6378
6379     void vTaskPriorityDisinheritAfterTimeout( TaskHandle_t const pxMutexHolder,
6380                                               UBaseType_t uxHighestPriorityWaitingTask )
6381     {
6382         TCB_t * const pxTCB = pxMutexHolder;
6383         UBaseType_t uxPriorityUsedOnEntry, uxPriorityToUse;
6384         const UBaseType_t uxOnlyOneMutexHeld = ( UBaseType_t ) 1;
6385
6386         traceENTER_vTaskPriorityDisinheritAfterTimeout( pxMutexHolder, uxHighestPriorityWaitingTask );
6387
6388         if( pxMutexHolder != NULL )
6389         {
6390             /* If pxMutexHolder is not NULL then the holder must hold at least
6391              * one mutex. */
6392             configASSERT( pxTCB->uxMutexesHeld );
6393
6394             /* Determine the priority to which the priority of the task that
6395              * holds the mutex should be set.  This will be the greater of the
6396              * holding task's base priority and the priority of the highest
6397              * priority task that is waiting to obtain the mutex. */
6398             if( pxTCB->uxBasePriority < uxHighestPriorityWaitingTask )
6399             {
6400                 uxPriorityToUse = uxHighestPriorityWaitingTask;
6401             }
6402             else
6403             {
6404                 uxPriorityToUse = pxTCB->uxBasePriority;
6405             }
6406
6407             /* Does the priority need to change? */
6408             if( pxTCB->uxPriority != uxPriorityToUse )
6409             {
6410                 /* Only disinherit if no other mutexes are held.  This is a
6411                  * simplification in the priority inheritance implementation.  If
6412                  * the task that holds the mutex is also holding other mutexes then
6413                  * the other mutexes may have caused the priority inheritance. */
6414                 if( pxTCB->uxMutexesHeld == uxOnlyOneMutexHeld )
6415                 {
6416                     /* If a task has timed out because it already holds the
6417                      * mutex it was trying to obtain then it cannot of inherited
6418                      * its own priority. */
6419                     configASSERT( pxTCB != pxCurrentTCB );
6420
6421                     /* Disinherit the priority, remembering the previous
6422                      * priority to facilitate determining the subject task's
6423                      * state. */
6424                     traceTASK_PRIORITY_DISINHERIT( pxTCB, uxPriorityToUse );
6425                     uxPriorityUsedOnEntry = pxTCB->uxPriority;
6426                     pxTCB->uxPriority = uxPriorityToUse;
6427
6428                     /* Only reset the event list item value if the value is not
6429                      * being used for anything else. */
6430                     if( ( listGET_LIST_ITEM_VALUE( &( pxTCB->xEventListItem ) ) & taskEVENT_LIST_ITEM_VALUE_IN_USE ) == 0UL )
6431                     {
6432                         listSET_LIST_ITEM_VALUE( &( pxTCB->xEventListItem ), ( TickType_t ) configMAX_PRIORITIES - ( TickType_t ) uxPriorityToUse ); /*lint !e961 MISRA exception as the casts are only redundant for some ports. */
6433                     }
6434                     else
6435                     {
6436                         mtCOVERAGE_TEST_MARKER();
6437                     }
6438
6439                     /* If the running task is not the task that holds the mutex
6440                      * then the task that holds the mutex could be in either the
6441                      * Ready, Blocked or Suspended states.  Only remove the task
6442                      * from its current state list if it is in the Ready state as
6443                      * the task's priority is going to change and there is one
6444                      * Ready list per priority. */
6445                     if( listIS_CONTAINED_WITHIN( &( pxReadyTasksLists[ uxPriorityUsedOnEntry ] ), &( pxTCB->xStateListItem ) ) != pdFALSE )
6446                     {
6447                         if( uxListRemove( &( pxTCB->xStateListItem ) ) == ( UBaseType_t ) 0 )
6448                         {
6449                             /* It is known that the task is in its ready list so
6450                              * there is no need to check again and the port level
6451                              * reset macro can be called directly. */
6452                             portRESET_READY_PRIORITY( pxTCB->uxPriority, uxTopReadyPriority );
6453                         }
6454                         else
6455                         {
6456                             mtCOVERAGE_TEST_MARKER();
6457                         }
6458
6459                         prvAddTaskToReadyList( pxTCB );
6460                         #if ( configNUMBER_OF_CORES > 1 )
6461                         {
6462                             /* The priority of the task is dropped. Yield the core on
6463                              * which the task is running. */
6464                             if( taskTASK_IS_RUNNING( pxTCB ) == pdTRUE )
6465                             {
6466                                 prvYieldCore( pxTCB->xTaskRunState );
6467                             }
6468                         }
6469                         #endif /* if ( configNUMBER_OF_CORES > 1 ) */
6470                     }
6471                     else
6472                     {
6473                         mtCOVERAGE_TEST_MARKER();
6474                     }
6475                 }
6476                 else
6477                 {
6478                     mtCOVERAGE_TEST_MARKER();
6479                 }
6480             }
6481             else
6482             {
6483                 mtCOVERAGE_TEST_MARKER();
6484             }
6485         }
6486         else
6487         {
6488             mtCOVERAGE_TEST_MARKER();
6489         }
6490
6491         traceRETURN_vTaskPriorityDisinheritAfterTimeout();
6492     }
6493
6494 #endif /* configUSE_MUTEXES */
6495 /*-----------------------------------------------------------*/
6496
6497 #if ( configNUMBER_OF_CORES > 1 )
6498
6499 /* If not in a critical section then yield immediately.
6500  * Otherwise set xYieldPendings to true to wait to
6501  * yield until exiting the critical section.
6502  */
6503     void vTaskYieldWithinAPI( void )
6504     {
6505         traceENTER_vTaskYieldWithinAPI();
6506
6507         if( portGET_CRITICAL_NESTING_COUNT() == 0U )
6508         {
6509             portYIELD();
6510         }
6511         else
6512         {
6513             xYieldPendings[ portGET_CORE_ID() ] = pdTRUE;
6514         }
6515
6516         traceRETURN_vTaskYieldWithinAPI();
6517     }
6518 #endif /* #if ( configNUMBER_OF_CORES > 1 ) */
6519
6520 /*-----------------------------------------------------------*/
6521
6522 #if ( ( portCRITICAL_NESTING_IN_TCB == 1 ) && ( configNUMBER_OF_CORES == 1 ) )
6523
6524     void vTaskEnterCritical( void )
6525     {
6526         traceENTER_vTaskEnterCritical();
6527
6528         portDISABLE_INTERRUPTS();
6529
6530         if( xSchedulerRunning != pdFALSE )
6531         {
6532             ( pxCurrentTCB->uxCriticalNesting )++;
6533
6534             /* This is not the interrupt safe version of the enter critical
6535              * function so  assert() if it is being called from an interrupt
6536              * context.  Only API functions that end in "FromISR" can be used in an
6537              * interrupt.  Only assert if the critical nesting count is 1 to
6538              * protect against recursive calls if the assert function also uses a
6539              * critical section. */
6540             if( pxCurrentTCB->uxCriticalNesting == 1 )
6541             {
6542                 portASSERT_IF_IN_ISR();
6543             }
6544         }
6545         else
6546         {
6547             mtCOVERAGE_TEST_MARKER();
6548         }
6549
6550         traceRETURN_vTaskEnterCritical();
6551     }
6552
6553 #endif /* #if ( ( portCRITICAL_NESTING_IN_TCB == 1 ) && ( configNUMBER_OF_CORES == 1 ) ) */
6554 /*-----------------------------------------------------------*/
6555
6556 #if ( configNUMBER_OF_CORES > 1 )
6557
6558     void vTaskEnterCritical( void )
6559     {
6560         traceENTER_vTaskEnterCritical();
6561
6562         portDISABLE_INTERRUPTS();
6563
6564         if( xSchedulerRunning != pdFALSE )
6565         {
6566             if( portGET_CRITICAL_NESTING_COUNT() == 0U )
6567             {
6568                 portGET_TASK_LOCK();
6569                 portGET_ISR_LOCK();
6570             }
6571
6572             portINCREMENT_CRITICAL_NESTING_COUNT();
6573
6574             /* This is not the interrupt safe version of the enter critical
6575              * function so  assert() if it is being called from an interrupt
6576              * context.  Only API functions that end in "FromISR" can be used in an
6577              * interrupt.  Only assert if the critical nesting count is 1 to
6578              * protect against recursive calls if the assert function also uses a
6579              * critical section. */
6580             if( portGET_CRITICAL_NESTING_COUNT() == 1U )
6581             {
6582                 portASSERT_IF_IN_ISR();
6583
6584                 if( uxSchedulerSuspended == 0U )
6585                 {
6586                     /* The only time there would be a problem is if this is called
6587                      * before a context switch and vTaskExitCritical() is called
6588                      * after pxCurrentTCB changes. Therefore this should not be
6589                      * used within vTaskSwitchContext(). */
6590                     prvCheckForRunStateChange();
6591                 }
6592             }
6593         }
6594         else
6595         {
6596             mtCOVERAGE_TEST_MARKER();
6597         }
6598
6599         traceRETURN_vTaskEnterCritical();
6600     }
6601
6602 #endif /* #if ( configNUMBER_OF_CORES > 1 ) */
6603
6604 /*-----------------------------------------------------------*/
6605
6606 #if ( configNUMBER_OF_CORES > 1 )
6607
6608     UBaseType_t vTaskEnterCriticalFromISR( void )
6609     {
6610         UBaseType_t uxSavedInterruptStatus = 0;
6611
6612         traceENTER_vTaskEnterCriticalFromISR();
6613
6614         if( xSchedulerRunning != pdFALSE )
6615         {
6616             uxSavedInterruptStatus = portSET_INTERRUPT_MASK_FROM_ISR();
6617
6618             if( portGET_CRITICAL_NESTING_COUNT() == 0U )
6619             {
6620                 portGET_ISR_LOCK();
6621             }
6622
6623             portINCREMENT_CRITICAL_NESTING_COUNT();
6624         }
6625         else
6626         {
6627             mtCOVERAGE_TEST_MARKER();
6628         }
6629
6630         traceRETURN_vTaskEnterCriticalFromISR( uxSavedInterruptStatus );
6631
6632         return uxSavedInterruptStatus;
6633     }
6634
6635 #endif /* #if ( configNUMBER_OF_CORES > 1 ) */
6636 /*-----------------------------------------------------------*/
6637
6638 #if ( ( portCRITICAL_NESTING_IN_TCB == 1 ) && ( configNUMBER_OF_CORES == 1 ) )
6639
6640     void vTaskExitCritical( void )
6641     {
6642         traceENTER_vTaskExitCritical();
6643
6644         if( xSchedulerRunning != pdFALSE )
6645         {
6646             /* If pxCurrentTCB->uxCriticalNesting is zero then this function
6647              * does not match a previous call to vTaskEnterCritical(). */
6648             configASSERT( pxCurrentTCB->uxCriticalNesting > 0U );
6649
6650             /* This function should not be called in ISR. Use vTaskExitCriticalFromISR
6651              * to exit critical section from ISR. */
6652             portASSERT_IF_IN_ISR();
6653
6654             if( pxCurrentTCB->uxCriticalNesting > 0U )
6655             {
6656                 ( pxCurrentTCB->uxCriticalNesting )--;
6657
6658                 if( pxCurrentTCB->uxCriticalNesting == 0U )
6659                 {
6660                     portENABLE_INTERRUPTS();
6661                 }
6662                 else
6663                 {
6664                     mtCOVERAGE_TEST_MARKER();
6665                 }
6666             }
6667             else
6668             {
6669                 mtCOVERAGE_TEST_MARKER();
6670             }
6671         }
6672         else
6673         {
6674             mtCOVERAGE_TEST_MARKER();
6675         }
6676
6677         traceRETURN_vTaskExitCritical();
6678     }
6679
6680 #endif /* #if ( ( portCRITICAL_NESTING_IN_TCB == 1 ) && ( configNUMBER_OF_CORES == 1 ) ) */
6681 /*-----------------------------------------------------------*/
6682
6683 #if ( configNUMBER_OF_CORES > 1 )
6684
6685     void vTaskExitCritical( void )
6686     {
6687         traceENTER_vTaskExitCritical();
6688
6689         if( xSchedulerRunning != pdFALSE )
6690         {
6691             /* If critical nesting count is zero then this function
6692              * does not match a previous call to vTaskEnterCritical(). */
6693             configASSERT( portGET_CRITICAL_NESTING_COUNT() > 0U );
6694
6695             /* This function should not be called in ISR. Use vTaskExitCriticalFromISR
6696              * to exit critical section from ISR. */
6697             portASSERT_IF_IN_ISR();
6698
6699             if( portGET_CRITICAL_NESTING_COUNT() > 0U )
6700             {
6701                 portDECREMENT_CRITICAL_NESTING_COUNT();
6702
6703                 if( portGET_CRITICAL_NESTING_COUNT() == 0U )
6704                 {
6705                     BaseType_t xYieldCurrentTask;
6706
6707                     /* Get the xYieldPending stats inside the critical section. */
6708                     xYieldCurrentTask = xYieldPendings[ portGET_CORE_ID() ];
6709
6710                     portRELEASE_ISR_LOCK();
6711                     portRELEASE_TASK_LOCK();
6712                     portENABLE_INTERRUPTS();
6713
6714                     /* When a task yields in a critical section it just sets
6715                      * xYieldPending to true. So now that we have exited the
6716                      * critical section check if xYieldPending is true, and
6717                      * if so yield. */
6718                     if( xYieldCurrentTask != pdFALSE )
6719                     {
6720                         portYIELD();
6721                     }
6722                 }
6723                 else
6724                 {
6725                     mtCOVERAGE_TEST_MARKER();
6726                 }
6727             }
6728             else
6729             {
6730                 mtCOVERAGE_TEST_MARKER();
6731             }
6732         }
6733         else
6734         {
6735             mtCOVERAGE_TEST_MARKER();
6736         }
6737
6738         traceRETURN_vTaskExitCritical();
6739     }
6740
6741 #endif /* #if ( configNUMBER_OF_CORES > 1 ) */
6742 /*-----------------------------------------------------------*/
6743
6744 #if ( configNUMBER_OF_CORES > 1 )
6745
6746     void vTaskExitCriticalFromISR( UBaseType_t uxSavedInterruptStatus )
6747     {
6748         traceENTER_vTaskExitCriticalFromISR( uxSavedInterruptStatus );
6749
6750         if( xSchedulerRunning != pdFALSE )
6751         {
6752             /* If critical nesting count is zero then this function
6753              * does not match a previous call to vTaskEnterCritical(). */
6754             configASSERT( portGET_CRITICAL_NESTING_COUNT() > 0U );
6755
6756             if( portGET_CRITICAL_NESTING_COUNT() > 0U )
6757             {
6758                 portDECREMENT_CRITICAL_NESTING_COUNT();
6759
6760                 if( portGET_CRITICAL_NESTING_COUNT() == 0U )
6761                 {
6762                     portRELEASE_ISR_LOCK();
6763                     portCLEAR_INTERRUPT_MASK_FROM_ISR( uxSavedInterruptStatus );
6764                 }
6765                 else
6766                 {
6767                     mtCOVERAGE_TEST_MARKER();
6768                 }
6769             }
6770             else
6771             {
6772                 mtCOVERAGE_TEST_MARKER();
6773             }
6774         }
6775         else
6776         {
6777             mtCOVERAGE_TEST_MARKER();
6778         }
6779
6780         traceRETURN_vTaskExitCriticalFromISR();
6781     }
6782
6783 #endif /* #if ( configNUMBER_OF_CORES > 1 ) */
6784 /*-----------------------------------------------------------*/
6785
6786 #if ( configUSE_STATS_FORMATTING_FUNCTIONS > 0 )
6787
6788     static char * prvWriteNameToBuffer( char * pcBuffer,
6789                                         const char * pcTaskName )
6790     {
6791         size_t x;
6792
6793         /* Start by copying the entire string. */
6794         ( void ) strcpy( pcBuffer, pcTaskName );
6795
6796         /* Pad the end of the string with spaces to ensure columns line up when
6797          * printed out. */
6798         for( x = strlen( pcBuffer ); x < ( size_t ) ( ( size_t ) configMAX_TASK_NAME_LEN - 1U ); x++ )
6799         {
6800             pcBuffer[ x ] = ' ';
6801         }
6802
6803         /* Terminate. */
6804         pcBuffer[ x ] = ( char ) 0x00;
6805
6806         /* Return the new end of string. */
6807         return &( pcBuffer[ x ] );
6808     }
6809
6810 #endif /* ( configUSE_STATS_FORMATTING_FUNCTIONS > 0 ) */
6811 /*-----------------------------------------------------------*/
6812
6813 #if ( ( configUSE_TRACE_FACILITY == 1 ) && ( configUSE_STATS_FORMATTING_FUNCTIONS > 0 ) )
6814
6815     void vTaskList( char * pcWriteBuffer )
6816     {
6817         TaskStatus_t * pxTaskStatusArray;
6818         UBaseType_t uxArraySize, x;
6819         char cStatus;
6820
6821         traceENTER_vTaskList( pcWriteBuffer );
6822
6823         /*
6824          * PLEASE NOTE:
6825          *
6826          * This function is provided for convenience only, and is used by many
6827          * of the demo applications.  Do not consider it to be part of the
6828          * scheduler.
6829          *
6830          * vTaskList() calls uxTaskGetSystemState(), then formats part of the
6831          * uxTaskGetSystemState() output into a human readable table that
6832          * displays task: names, states, priority, stack usage and task number.
6833          * Stack usage specified as the number of unused StackType_t words stack can hold
6834          * on top of stack - not the number of bytes.
6835          *
6836          * vTaskList() has a dependency on the sprintf() C library function that
6837          * might bloat the code size, use a lot of stack, and provide different
6838          * results on different platforms.  An alternative, tiny, third party,
6839          * and limited functionality implementation of sprintf() is provided in
6840          * many of the FreeRTOS/Demo sub-directories in a file called
6841          * printf-stdarg.c (note printf-stdarg.c does not provide a full
6842          * snprintf() implementation!).
6843          *
6844          * It is recommended that production systems call uxTaskGetSystemState()
6845          * directly to get access to raw stats data, rather than indirectly
6846          * through a call to vTaskList().
6847          */
6848
6849
6850         /* Make sure the write buffer does not contain a string. */
6851         *pcWriteBuffer = ( char ) 0x00;
6852
6853         /* Take a snapshot of the number of tasks in case it changes while this
6854          * function is executing. */
6855         uxArraySize = uxCurrentNumberOfTasks;
6856
6857         /* Allocate an array index for each task.  NOTE!  if
6858          * configSUPPORT_DYNAMIC_ALLOCATION is set to 0 then pvPortMalloc() will
6859          * equate to NULL. */
6860         pxTaskStatusArray = pvPortMalloc( uxCurrentNumberOfTasks * sizeof( TaskStatus_t ) ); /*lint !e9079 All values returned by pvPortMalloc() have at least the alignment required by the MCU's stack and this allocation allocates a struct that has the alignment requirements of a pointer. */
6861
6862         if( pxTaskStatusArray != NULL )
6863         {
6864             /* Generate the (binary) data. */
6865             uxArraySize = uxTaskGetSystemState( pxTaskStatusArray, uxArraySize, NULL );
6866
6867             /* Create a human readable table from the binary data. */
6868             for( x = 0; x < uxArraySize; x++ )
6869             {
6870                 switch( pxTaskStatusArray[ x ].eCurrentState )
6871                 {
6872                     case eRunning:
6873                         cStatus = tskRUNNING_CHAR;
6874                         break;
6875
6876                     case eReady:
6877                         cStatus = tskREADY_CHAR;
6878                         break;
6879
6880                     case eBlocked:
6881                         cStatus = tskBLOCKED_CHAR;
6882                         break;
6883
6884                     case eSuspended:
6885                         cStatus = tskSUSPENDED_CHAR;
6886                         break;
6887
6888                     case eDeleted:
6889                         cStatus = tskDELETED_CHAR;
6890                         break;
6891
6892                     case eInvalid: /* Fall through. */
6893                     default:       /* Should not get here, but it is included
6894                                     * to prevent static checking errors. */
6895                         cStatus = ( char ) 0x00;
6896                         break;
6897                 }
6898
6899                 /* Write the task name to the string, padding with spaces so it
6900                  * can be printed in tabular form more easily. */
6901                 pcWriteBuffer = prvWriteNameToBuffer( pcWriteBuffer, pxTaskStatusArray[ x ].pcTaskName );
6902
6903                 /* Write the rest of the string. */
6904                 sprintf( pcWriteBuffer, "\t%c\t%u\t%u\t%u\r\n", cStatus, ( unsigned int ) pxTaskStatusArray[ x ].uxCurrentPriority, ( unsigned int ) pxTaskStatusArray[ x ].usStackHighWaterMark, ( unsigned int ) pxTaskStatusArray[ x ].xTaskNumber ); /*lint !e586 sprintf() allowed as this is compiled with many compilers and this is a utility function only - not part of the core kernel implementation. */
6905                 pcWriteBuffer += strlen( pcWriteBuffer );                                                                                                                                                                                                /*lint !e9016 Pointer arithmetic ok on char pointers especially as in this case where it best denotes the intent of the code. */
6906             }
6907
6908             /* Free the array again.  NOTE!  If configSUPPORT_DYNAMIC_ALLOCATION
6909              * is 0 then vPortFree() will be #defined to nothing. */
6910             vPortFree( pxTaskStatusArray );
6911         }
6912         else
6913         {
6914             mtCOVERAGE_TEST_MARKER();
6915         }
6916
6917         traceRETURN_vTaskList();
6918     }
6919
6920 #endif /* ( ( configUSE_TRACE_FACILITY == 1 ) && ( configUSE_STATS_FORMATTING_FUNCTIONS > 0 ) ) */
6921 /*----------------------------------------------------------*/
6922
6923 #if ( ( configGENERATE_RUN_TIME_STATS == 1 ) && ( configUSE_STATS_FORMATTING_FUNCTIONS > 0 ) && ( configUSE_TRACE_FACILITY == 1 ) )
6924
6925     void vTaskGetRunTimeStats( char * pcWriteBuffer )
6926     {
6927         TaskStatus_t * pxTaskStatusArray;
6928         UBaseType_t uxArraySize, x;
6929         configRUN_TIME_COUNTER_TYPE ulTotalTime, ulStatsAsPercentage;
6930
6931         traceENTER_vTaskGetRunTimeStats( pcWriteBuffer );
6932
6933         /*
6934          * PLEASE NOTE:
6935          *
6936          * This function is provided for convenience only, and is used by many
6937          * of the demo applications.  Do not consider it to be part of the
6938          * scheduler.
6939          *
6940          * vTaskGetRunTimeStats() calls uxTaskGetSystemState(), then formats part
6941          * of the uxTaskGetSystemState() output into a human readable table that
6942          * displays the amount of time each task has spent in the Running state
6943          * in both absolute and percentage terms.
6944          *
6945          * vTaskGetRunTimeStats() has a dependency on the sprintf() C library
6946          * function that might bloat the code size, use a lot of stack, and
6947          * provide different results on different platforms.  An alternative,
6948          * tiny, third party, and limited functionality implementation of
6949          * sprintf() is provided in many of the FreeRTOS/Demo sub-directories in
6950          * a file called printf-stdarg.c (note printf-stdarg.c does not provide
6951          * a full snprintf() implementation!).
6952          *
6953          * It is recommended that production systems call uxTaskGetSystemState()
6954          * directly to get access to raw stats data, rather than indirectly
6955          * through a call to vTaskGetRunTimeStats().
6956          */
6957
6958         /* Make sure the write buffer does not contain a string. */
6959         *pcWriteBuffer = ( char ) 0x00;
6960
6961         /* Take a snapshot of the number of tasks in case it changes while this
6962          * function is executing. */
6963         uxArraySize = uxCurrentNumberOfTasks;
6964
6965         /* Allocate an array index for each task.  NOTE!  If
6966          * configSUPPORT_DYNAMIC_ALLOCATION is set to 0 then pvPortMalloc() will
6967          * equate to NULL. */
6968         pxTaskStatusArray = pvPortMalloc( uxCurrentNumberOfTasks * sizeof( TaskStatus_t ) ); /*lint !e9079 All values returned by pvPortMalloc() have at least the alignment required by the MCU's stack and this allocation allocates a struct that has the alignment requirements of a pointer. */
6969
6970         if( pxTaskStatusArray != NULL )
6971         {
6972             /* Generate the (binary) data. */
6973             uxArraySize = uxTaskGetSystemState( pxTaskStatusArray, uxArraySize, &ulTotalTime );
6974
6975             /* For percentage calculations. */
6976             ulTotalTime /= 100UL;
6977
6978             /* Avoid divide by zero errors. */
6979             if( ulTotalTime > 0UL )
6980             {
6981                 /* Create a human readable table from the binary data. */
6982                 for( x = 0; x < uxArraySize; x++ )
6983                 {
6984                     /* What percentage of the total run time has the task used?
6985                      * This will always be rounded down to the nearest integer.
6986                      * ulTotalRunTime has already been divided by 100. */
6987                     ulStatsAsPercentage = pxTaskStatusArray[ x ].ulRunTimeCounter / ulTotalTime;
6988
6989                     /* Write the task name to the string, padding with
6990                      * spaces so it can be printed in tabular form more
6991                      * easily. */
6992                     pcWriteBuffer = prvWriteNameToBuffer( pcWriteBuffer, pxTaskStatusArray[ x ].pcTaskName );
6993
6994                     if( ulStatsAsPercentage > 0UL )
6995                     {
6996                         #ifdef portLU_PRINTF_SPECIFIER_REQUIRED
6997                         {
6998                             sprintf( pcWriteBuffer, "\t%lu\t\t%lu%%\r\n", pxTaskStatusArray[ x ].ulRunTimeCounter, ulStatsAsPercentage );
6999                         }
7000                         #else
7001                         {
7002                             /* sizeof( int ) == sizeof( long ) so a smaller
7003                              * printf() library can be used. */
7004                             sprintf( pcWriteBuffer, "\t%u\t\t%u%%\r\n", ( unsigned int ) pxTaskStatusArray[ x ].ulRunTimeCounter, ( unsigned int ) ulStatsAsPercentage ); /*lint !e586 sprintf() allowed as this is compiled with many compilers and this is a utility function only - not part of the core kernel implementation. */
7005                         }
7006                         #endif
7007                     }
7008                     else
7009                     {
7010                         /* If the percentage is zero here then the task has
7011                          * consumed less than 1% of the total run time. */
7012                         #ifdef portLU_PRINTF_SPECIFIER_REQUIRED
7013                         {
7014                             sprintf( pcWriteBuffer, "\t%lu\t\t<1%%\r\n", pxTaskStatusArray[ x ].ulRunTimeCounter );
7015                         }
7016                         #else
7017                         {
7018                             /* sizeof( int ) == sizeof( long ) so a smaller
7019                              * printf() library can be used. */
7020                             sprintf( pcWriteBuffer, "\t%u\t\t<1%%\r\n", ( unsigned int ) pxTaskStatusArray[ x ].ulRunTimeCounter ); /*lint !e586 sprintf() allowed as this is compiled with many compilers and this is a utility function only - not part of the core kernel implementation. */
7021                         }
7022                         #endif
7023                     }
7024
7025                     pcWriteBuffer += strlen( pcWriteBuffer ); /*lint !e9016 Pointer arithmetic ok on char pointers especially as in this case where it best denotes the intent of the code. */
7026                 }
7027             }
7028             else
7029             {
7030                 mtCOVERAGE_TEST_MARKER();
7031             }
7032
7033             /* Free the array again.  NOTE!  If configSUPPORT_DYNAMIC_ALLOCATION
7034              * is 0 then vPortFree() will be #defined to nothing. */
7035             vPortFree( pxTaskStatusArray );
7036         }
7037         else
7038         {
7039             mtCOVERAGE_TEST_MARKER();
7040         }
7041
7042         traceRETURN_vTaskGetRunTimeStats();
7043     }
7044
7045 #endif /* ( ( configGENERATE_RUN_TIME_STATS == 1 ) && ( configUSE_STATS_FORMATTING_FUNCTIONS > 0 ) ) */
7046 /*-----------------------------------------------------------*/
7047
7048 TickType_t uxTaskResetEventItemValue( void )
7049 {
7050     TickType_t uxReturn;
7051
7052     traceENTER_uxTaskResetEventItemValue();
7053
7054     uxReturn = listGET_LIST_ITEM_VALUE( &( pxCurrentTCB->xEventListItem ) );
7055
7056     /* Reset the event list item to its normal value - so it can be used with
7057      * queues and semaphores. */
7058     listSET_LIST_ITEM_VALUE( &( pxCurrentTCB->xEventListItem ), ( ( TickType_t ) configMAX_PRIORITIES - ( TickType_t ) pxCurrentTCB->uxPriority ) ); /*lint !e961 MISRA exception as the casts are only redundant for some ports. */
7059
7060     traceRETURN_uxTaskResetEventItemValue( uxReturn );
7061
7062     return uxReturn;
7063 }
7064 /*-----------------------------------------------------------*/
7065
7066 #if ( configUSE_MUTEXES == 1 )
7067
7068     TaskHandle_t pvTaskIncrementMutexHeldCount( void )
7069     {
7070         TCB_t * pxTCB;
7071
7072         traceENTER_pvTaskIncrementMutexHeldCount();
7073
7074         pxTCB = pxCurrentTCB;
7075
7076         /* If xSemaphoreCreateMutex() is called before any tasks have been created
7077          * then pxCurrentTCB will be NULL. */
7078         if( pxTCB != NULL )
7079         {
7080             ( pxTCB->uxMutexesHeld )++;
7081         }
7082
7083         traceRETURN_pvTaskIncrementMutexHeldCount( pxTCB );
7084
7085         return pxTCB;
7086     }
7087
7088 #endif /* configUSE_MUTEXES */
7089 /*-----------------------------------------------------------*/
7090
7091 #if ( configUSE_TASK_NOTIFICATIONS == 1 )
7092
7093     uint32_t ulTaskGenericNotifyTake( UBaseType_t uxIndexToWaitOn,
7094                                       BaseType_t xClearCountOnExit,
7095                                       TickType_t xTicksToWait )
7096     {
7097         uint32_t ulReturn;
7098
7099         traceENTER_ulTaskGenericNotifyTake( uxIndexToWaitOn, xClearCountOnExit, xTicksToWait );
7100
7101         configASSERT( uxIndexToWaitOn < configTASK_NOTIFICATION_ARRAY_ENTRIES );
7102
7103         taskENTER_CRITICAL();
7104         {
7105             /* Only block if the notification count is not already non-zero. */
7106             if( pxCurrentTCB->ulNotifiedValue[ uxIndexToWaitOn ] == 0UL )
7107             {
7108                 /* Mark this task as waiting for a notification. */
7109                 pxCurrentTCB->ucNotifyState[ uxIndexToWaitOn ] = taskWAITING_NOTIFICATION;
7110
7111                 if( xTicksToWait > ( TickType_t ) 0 )
7112                 {
7113                     prvAddCurrentTaskToDelayedList( xTicksToWait, pdTRUE );
7114                     traceTASK_NOTIFY_TAKE_BLOCK( uxIndexToWaitOn );
7115
7116                     /* All ports are written to allow a yield in a critical
7117                      * section (some will yield immediately, others wait until the
7118                      * critical section exits) - but it is not something that
7119                      * application code should ever do. */
7120                     #if ( configNUMBER_OF_CORES == 1 )
7121                     {
7122                         portYIELD_WITHIN_API();
7123                     }
7124                     #else
7125                     {
7126                         vTaskYieldWithinAPI();
7127                     }
7128                     #endif
7129                 }
7130                 else
7131                 {
7132                     mtCOVERAGE_TEST_MARKER();
7133                 }
7134             }
7135             else
7136             {
7137                 mtCOVERAGE_TEST_MARKER();
7138             }
7139         }
7140         taskEXIT_CRITICAL();
7141
7142         taskENTER_CRITICAL();
7143         {
7144             traceTASK_NOTIFY_TAKE( uxIndexToWaitOn );
7145             ulReturn = pxCurrentTCB->ulNotifiedValue[ uxIndexToWaitOn ];
7146
7147             if( ulReturn != 0UL )
7148             {
7149                 if( xClearCountOnExit != pdFALSE )
7150                 {
7151                     pxCurrentTCB->ulNotifiedValue[ uxIndexToWaitOn ] = 0UL;
7152                 }
7153                 else
7154                 {
7155                     pxCurrentTCB->ulNotifiedValue[ uxIndexToWaitOn ] = ulReturn - ( uint32_t ) 1;
7156                 }
7157             }
7158             else
7159             {
7160                 mtCOVERAGE_TEST_MARKER();
7161             }
7162
7163             pxCurrentTCB->ucNotifyState[ uxIndexToWaitOn ] = taskNOT_WAITING_NOTIFICATION;
7164         }
7165         taskEXIT_CRITICAL();
7166
7167         traceRETURN_ulTaskGenericNotifyTake( ulReturn );
7168
7169         return ulReturn;
7170     }
7171
7172 #endif /* configUSE_TASK_NOTIFICATIONS */
7173 /*-----------------------------------------------------------*/
7174
7175 #if ( configUSE_TASK_NOTIFICATIONS == 1 )
7176
7177     BaseType_t xTaskGenericNotifyWait( UBaseType_t uxIndexToWaitOn,
7178                                        uint32_t ulBitsToClearOnEntry,
7179                                        uint32_t ulBitsToClearOnExit,
7180                                        uint32_t * pulNotificationValue,
7181                                        TickType_t xTicksToWait )
7182     {
7183         BaseType_t xReturn;
7184
7185         traceENTER_xTaskGenericNotifyWait( uxIndexToWaitOn, ulBitsToClearOnEntry, ulBitsToClearOnExit, pulNotificationValue, xTicksToWait );
7186
7187         configASSERT( uxIndexToWaitOn < configTASK_NOTIFICATION_ARRAY_ENTRIES );
7188
7189         taskENTER_CRITICAL();
7190         {
7191             /* Only block if a notification is not already pending. */
7192             if( pxCurrentTCB->ucNotifyState[ uxIndexToWaitOn ] != taskNOTIFICATION_RECEIVED )
7193             {
7194                 /* Clear bits in the task's notification value as bits may get
7195                  * set  by the notifying task or interrupt.  This can be used to
7196                  * clear the value to zero. */
7197                 pxCurrentTCB->ulNotifiedValue[ uxIndexToWaitOn ] &= ~ulBitsToClearOnEntry;
7198
7199                 /* Mark this task as waiting for a notification. */
7200                 pxCurrentTCB->ucNotifyState[ uxIndexToWaitOn ] = taskWAITING_NOTIFICATION;
7201
7202                 if( xTicksToWait > ( TickType_t ) 0 )
7203                 {
7204                     prvAddCurrentTaskToDelayedList( xTicksToWait, pdTRUE );
7205                     traceTASK_NOTIFY_WAIT_BLOCK( uxIndexToWaitOn );
7206
7207                     /* All ports are written to allow a yield in a critical
7208                      * section (some will yield immediately, others wait until the
7209                      * critical section exits) - but it is not something that
7210                      * application code should ever do. */
7211                     #if ( configNUMBER_OF_CORES == 1 )
7212                     {
7213                         portYIELD_WITHIN_API();
7214                     }
7215                     #else
7216                     {
7217                         vTaskYieldWithinAPI();
7218                     }
7219                     #endif
7220                 }
7221                 else
7222                 {
7223                     mtCOVERAGE_TEST_MARKER();
7224                 }
7225             }
7226             else
7227             {
7228                 mtCOVERAGE_TEST_MARKER();
7229             }
7230         }
7231         taskEXIT_CRITICAL();
7232
7233         taskENTER_CRITICAL();
7234         {
7235             traceTASK_NOTIFY_WAIT( uxIndexToWaitOn );
7236
7237             if( pulNotificationValue != NULL )
7238             {
7239                 /* Output the current notification value, which may or may not
7240                  * have changed. */
7241                 *pulNotificationValue = pxCurrentTCB->ulNotifiedValue[ uxIndexToWaitOn ];
7242             }
7243
7244             /* If ucNotifyValue is set then either the task never entered the
7245              * blocked state (because a notification was already pending) or the
7246              * task unblocked because of a notification.  Otherwise the task
7247              * unblocked because of a timeout. */
7248             if( pxCurrentTCB->ucNotifyState[ uxIndexToWaitOn ] != taskNOTIFICATION_RECEIVED )
7249             {
7250                 /* A notification was not received. */
7251                 xReturn = pdFALSE;
7252             }
7253             else
7254             {
7255                 /* A notification was already pending or a notification was
7256                  * received while the task was waiting. */
7257                 pxCurrentTCB->ulNotifiedValue[ uxIndexToWaitOn ] &= ~ulBitsToClearOnExit;
7258                 xReturn = pdTRUE;
7259             }
7260
7261             pxCurrentTCB->ucNotifyState[ uxIndexToWaitOn ] = taskNOT_WAITING_NOTIFICATION;
7262         }
7263         taskEXIT_CRITICAL();
7264
7265         traceRETURN_xTaskGenericNotifyWait( xReturn );
7266
7267         return xReturn;
7268     }
7269
7270 #endif /* configUSE_TASK_NOTIFICATIONS */
7271 /*-----------------------------------------------------------*/
7272
7273 #if ( configUSE_TASK_NOTIFICATIONS == 1 )
7274
7275     BaseType_t xTaskGenericNotify( TaskHandle_t xTaskToNotify,
7276                                    UBaseType_t uxIndexToNotify,
7277                                    uint32_t ulValue,
7278                                    eNotifyAction eAction,
7279                                    uint32_t * pulPreviousNotificationValue )
7280     {
7281         TCB_t * pxTCB;
7282         BaseType_t xReturn = pdPASS;
7283         uint8_t ucOriginalNotifyState;
7284
7285         traceENTER_xTaskGenericNotify( xTaskToNotify, uxIndexToNotify, ulValue, eAction, pulPreviousNotificationValue );
7286
7287         configASSERT( uxIndexToNotify < configTASK_NOTIFICATION_ARRAY_ENTRIES );
7288         configASSERT( xTaskToNotify );
7289         pxTCB = xTaskToNotify;
7290
7291         taskENTER_CRITICAL();
7292         {
7293             if( pulPreviousNotificationValue != NULL )
7294             {
7295                 *pulPreviousNotificationValue = pxTCB->ulNotifiedValue[ uxIndexToNotify ];
7296             }
7297
7298             ucOriginalNotifyState = pxTCB->ucNotifyState[ uxIndexToNotify ];
7299
7300             pxTCB->ucNotifyState[ uxIndexToNotify ] = taskNOTIFICATION_RECEIVED;
7301
7302             switch( eAction )
7303             {
7304                 case eSetBits:
7305                     pxTCB->ulNotifiedValue[ uxIndexToNotify ] |= ulValue;
7306                     break;
7307
7308                 case eIncrement:
7309                     ( pxTCB->ulNotifiedValue[ uxIndexToNotify ] )++;
7310                     break;
7311
7312                 case eSetValueWithOverwrite:
7313                     pxTCB->ulNotifiedValue[ uxIndexToNotify ] = ulValue;
7314                     break;
7315
7316                 case eSetValueWithoutOverwrite:
7317
7318                     if( ucOriginalNotifyState != taskNOTIFICATION_RECEIVED )
7319                     {
7320                         pxTCB->ulNotifiedValue[ uxIndexToNotify ] = ulValue;
7321                     }
7322                     else
7323                     {
7324                         /* The value could not be written to the task. */
7325                         xReturn = pdFAIL;
7326                     }
7327
7328                     break;
7329
7330                 case eNoAction:
7331
7332                     /* The task is being notified without its notify value being
7333                      * updated. */
7334                     break;
7335
7336                 default:
7337
7338                     /* Should not get here if all enums are handled.
7339                      * Artificially force an assert by testing a value the
7340                      * compiler can't assume is const. */
7341                     configASSERT( xTickCount == ( TickType_t ) 0 );
7342
7343                     break;
7344             }
7345
7346             traceTASK_NOTIFY( uxIndexToNotify );
7347
7348             /* If the task is in the blocked state specifically to wait for a
7349              * notification then unblock it now. */
7350             if( ucOriginalNotifyState == taskWAITING_NOTIFICATION )
7351             {
7352                 listREMOVE_ITEM( &( pxTCB->xStateListItem ) );
7353                 prvAddTaskToReadyList( pxTCB );
7354
7355                 /* The task should not have been on an event list. */
7356                 configASSERT( listLIST_ITEM_CONTAINER( &( pxTCB->xEventListItem ) ) == NULL );
7357
7358                 #if ( configUSE_TICKLESS_IDLE != 0 )
7359                 {
7360                     /* If a task is blocked waiting for a notification then
7361                      * xNextTaskUnblockTime might be set to the blocked task's time
7362                      * out time.  If the task is unblocked for a reason other than
7363                      * a timeout xNextTaskUnblockTime is normally left unchanged,
7364                      * because it will automatically get reset to a new value when
7365                      * the tick count equals xNextTaskUnblockTime.  However if
7366                      * tickless idling is used it might be more important to enter
7367                      * sleep mode at the earliest possible time - so reset
7368                      * xNextTaskUnblockTime here to ensure it is updated at the
7369                      * earliest possible time. */
7370                     prvResetNextTaskUnblockTime();
7371                 }
7372                 #endif
7373
7374                 /* Check if the notified task has a priority above the currently
7375                  * executing task. */
7376                 taskYIELD_ANY_CORE_IF_USING_PREEMPTION( pxTCB );
7377             }
7378             else
7379             {
7380                 mtCOVERAGE_TEST_MARKER();
7381             }
7382         }
7383         taskEXIT_CRITICAL();
7384
7385         traceRETURN_xTaskGenericNotify( xReturn );
7386
7387         return xReturn;
7388     }
7389
7390 #endif /* configUSE_TASK_NOTIFICATIONS */
7391 /*-----------------------------------------------------------*/
7392
7393 #if ( configUSE_TASK_NOTIFICATIONS == 1 )
7394
7395     BaseType_t xTaskGenericNotifyFromISR( TaskHandle_t xTaskToNotify,
7396                                           UBaseType_t uxIndexToNotify,
7397                                           uint32_t ulValue,
7398                                           eNotifyAction eAction,
7399                                           uint32_t * pulPreviousNotificationValue,
7400                                           BaseType_t * pxHigherPriorityTaskWoken )
7401     {
7402         TCB_t * pxTCB;
7403         uint8_t ucOriginalNotifyState;
7404         BaseType_t xReturn = pdPASS;
7405         UBaseType_t uxSavedInterruptStatus;
7406
7407         traceENTER_xTaskGenericNotifyFromISR( xTaskToNotify, uxIndexToNotify, ulValue, eAction, pulPreviousNotificationValue, pxHigherPriorityTaskWoken );
7408
7409         configASSERT( xTaskToNotify );
7410         configASSERT( uxIndexToNotify < configTASK_NOTIFICATION_ARRAY_ENTRIES );
7411
7412         /* RTOS ports that support interrupt nesting have the concept of a
7413          * maximum  system call (or maximum API call) interrupt priority.
7414          * Interrupts that are  above the maximum system call priority are keep
7415          * permanently enabled, even when the RTOS kernel is in a critical section,
7416          * but cannot make any calls to FreeRTOS API functions.  If configASSERT()
7417          * is defined in FreeRTOSConfig.h then
7418          * portASSERT_IF_INTERRUPT_PRIORITY_INVALID() will result in an assertion
7419          * failure if a FreeRTOS API function is called from an interrupt that has
7420          * been assigned a priority above the configured maximum system call
7421          * priority.  Only FreeRTOS functions that end in FromISR can be called
7422          * from interrupts  that have been assigned a priority at or (logically)
7423          * below the maximum system call interrupt priority.  FreeRTOS maintains a
7424          * separate interrupt safe API to ensure interrupt entry is as fast and as
7425          * simple as possible.  More information (albeit Cortex-M specific) is
7426          * provided on the following link:
7427          * https://www.FreeRTOS.org/RTOS-Cortex-M3-M4.html */
7428         portASSERT_IF_INTERRUPT_PRIORITY_INVALID();
7429
7430         pxTCB = xTaskToNotify;
7431
7432         uxSavedInterruptStatus = taskENTER_CRITICAL_FROM_ISR();
7433         {
7434             if( pulPreviousNotificationValue != NULL )
7435             {
7436                 *pulPreviousNotificationValue = pxTCB->ulNotifiedValue[ uxIndexToNotify ];
7437             }
7438
7439             ucOriginalNotifyState = pxTCB->ucNotifyState[ uxIndexToNotify ];
7440             pxTCB->ucNotifyState[ uxIndexToNotify ] = taskNOTIFICATION_RECEIVED;
7441
7442             switch( eAction )
7443             {
7444                 case eSetBits:
7445                     pxTCB->ulNotifiedValue[ uxIndexToNotify ] |= ulValue;
7446                     break;
7447
7448                 case eIncrement:
7449                     ( pxTCB->ulNotifiedValue[ uxIndexToNotify ] )++;
7450                     break;
7451
7452                 case eSetValueWithOverwrite:
7453                     pxTCB->ulNotifiedValue[ uxIndexToNotify ] = ulValue;
7454                     break;
7455
7456                 case eSetValueWithoutOverwrite:
7457
7458                     if( ucOriginalNotifyState != taskNOTIFICATION_RECEIVED )
7459                     {
7460                         pxTCB->ulNotifiedValue[ uxIndexToNotify ] = ulValue;
7461                     }
7462                     else
7463                     {
7464                         /* The value could not be written to the task. */
7465                         xReturn = pdFAIL;
7466                     }
7467
7468                     break;
7469
7470                 case eNoAction:
7471
7472                     /* The task is being notified without its notify value being
7473                      * updated. */
7474                     break;
7475
7476                 default:
7477
7478                     /* Should not get here if all enums are handled.
7479                      * Artificially force an assert by testing a value the
7480                      * compiler can't assume is const. */
7481                     configASSERT( xTickCount == ( TickType_t ) 0 );
7482                     break;
7483             }
7484
7485             traceTASK_NOTIFY_FROM_ISR( uxIndexToNotify );
7486
7487             /* If the task is in the blocked state specifically to wait for a
7488              * notification then unblock it now. */
7489             if( ucOriginalNotifyState == taskWAITING_NOTIFICATION )
7490             {
7491                 /* The task should not have been on an event list. */
7492                 configASSERT( listLIST_ITEM_CONTAINER( &( pxTCB->xEventListItem ) ) == NULL );
7493
7494                 if( uxSchedulerSuspended == ( UBaseType_t ) 0U )
7495                 {
7496                     listREMOVE_ITEM( &( pxTCB->xStateListItem ) );
7497                     prvAddTaskToReadyList( pxTCB );
7498                 }
7499                 else
7500                 {
7501                     /* The delayed and ready lists cannot be accessed, so hold
7502                      * this task pending until the scheduler is resumed. */
7503                     listINSERT_END( &( xPendingReadyList ), &( pxTCB->xEventListItem ) );
7504                 }
7505
7506                 #if ( configNUMBER_OF_CORES == 1 )
7507                 {
7508                     if( pxTCB->uxPriority > pxCurrentTCB->uxPriority )
7509                     {
7510                         /* The notified task has a priority above the currently
7511                          * executing task so a yield is required. */
7512                         if( pxHigherPriorityTaskWoken != NULL )
7513                         {
7514                             *pxHigherPriorityTaskWoken = pdTRUE;
7515                         }
7516
7517                         /* Mark that a yield is pending in case the user is not
7518                          * using the "xHigherPriorityTaskWoken" parameter to an ISR
7519                          * safe FreeRTOS function. */
7520                         xYieldPendings[ 0 ] = pdTRUE;
7521                     }
7522                     else
7523                     {
7524                         mtCOVERAGE_TEST_MARKER();
7525                     }
7526                 }
7527                 #else /* #if ( configNUMBER_OF_CORES == 1 ) */
7528                 {
7529                     #if ( configUSE_PREEMPTION == 1 )
7530                     {
7531                         prvYieldForTask( pxTCB );
7532
7533                         if( xYieldPendings[ portGET_CORE_ID() ] == pdTRUE )
7534                         {
7535                             if( pxHigherPriorityTaskWoken != NULL )
7536                             {
7537                                 *pxHigherPriorityTaskWoken = pdTRUE;
7538                             }
7539                         }
7540                     }
7541                     #endif /* if ( configUSE_PREEMPTION == 1 ) */
7542                 }
7543                 #endif /* #if ( configNUMBER_OF_CORES == 1 ) */
7544             }
7545         }
7546         taskEXIT_CRITICAL_FROM_ISR( uxSavedInterruptStatus );
7547
7548         traceRETURN_xTaskGenericNotifyFromISR( xReturn );
7549
7550         return xReturn;
7551     }
7552
7553 #endif /* configUSE_TASK_NOTIFICATIONS */
7554 /*-----------------------------------------------------------*/
7555
7556 #if ( configUSE_TASK_NOTIFICATIONS == 1 )
7557
7558     void vTaskGenericNotifyGiveFromISR( TaskHandle_t xTaskToNotify,
7559                                         UBaseType_t uxIndexToNotify,
7560                                         BaseType_t * pxHigherPriorityTaskWoken )
7561     {
7562         TCB_t * pxTCB;
7563         uint8_t ucOriginalNotifyState;
7564         UBaseType_t uxSavedInterruptStatus;
7565
7566         traceENTER_vTaskGenericNotifyGiveFromISR( xTaskToNotify, uxIndexToNotify, pxHigherPriorityTaskWoken );
7567
7568         configASSERT( xTaskToNotify );
7569         configASSERT( uxIndexToNotify < configTASK_NOTIFICATION_ARRAY_ENTRIES );
7570
7571         /* RTOS ports that support interrupt nesting have the concept of a
7572          * maximum  system call (or maximum API call) interrupt priority.
7573          * Interrupts that are  above the maximum system call priority are keep
7574          * permanently enabled, even when the RTOS kernel is in a critical section,
7575          * but cannot make any calls to FreeRTOS API functions.  If configASSERT()
7576          * is defined in FreeRTOSConfig.h then
7577          * portASSERT_IF_INTERRUPT_PRIORITY_INVALID() will result in an assertion
7578          * failure if a FreeRTOS API function is called from an interrupt that has
7579          * been assigned a priority above the configured maximum system call
7580          * priority.  Only FreeRTOS functions that end in FromISR can be called
7581          * from interrupts  that have been assigned a priority at or (logically)
7582          * below the maximum system call interrupt priority.  FreeRTOS maintains a
7583          * separate interrupt safe API to ensure interrupt entry is as fast and as
7584          * simple as possible.  More information (albeit Cortex-M specific) is
7585          * provided on the following link:
7586          * https://www.FreeRTOS.org/RTOS-Cortex-M3-M4.html */
7587         portASSERT_IF_INTERRUPT_PRIORITY_INVALID();
7588
7589         pxTCB = xTaskToNotify;
7590
7591         uxSavedInterruptStatus = taskENTER_CRITICAL_FROM_ISR();
7592         {
7593             ucOriginalNotifyState = pxTCB->ucNotifyState[ uxIndexToNotify ];
7594             pxTCB->ucNotifyState[ uxIndexToNotify ] = taskNOTIFICATION_RECEIVED;
7595
7596             /* 'Giving' is equivalent to incrementing a count in a counting
7597              * semaphore. */
7598             ( pxTCB->ulNotifiedValue[ uxIndexToNotify ] )++;
7599
7600             traceTASK_NOTIFY_GIVE_FROM_ISR( uxIndexToNotify );
7601
7602             /* If the task is in the blocked state specifically to wait for a
7603              * notification then unblock it now. */
7604             if( ucOriginalNotifyState == taskWAITING_NOTIFICATION )
7605             {
7606                 /* The task should not have been on an event list. */
7607                 configASSERT( listLIST_ITEM_CONTAINER( &( pxTCB->xEventListItem ) ) == NULL );
7608
7609                 if( uxSchedulerSuspended == ( UBaseType_t ) 0U )
7610                 {
7611                     listREMOVE_ITEM( &( pxTCB->xStateListItem ) );
7612                     prvAddTaskToReadyList( pxTCB );
7613                 }
7614                 else
7615                 {
7616                     /* The delayed and ready lists cannot be accessed, so hold
7617                      * this task pending until the scheduler is resumed. */
7618                     listINSERT_END( &( xPendingReadyList ), &( pxTCB->xEventListItem ) );
7619                 }
7620
7621                 #if ( configNUMBER_OF_CORES == 1 )
7622                 {
7623                     if( pxTCB->uxPriority > pxCurrentTCB->uxPriority )
7624                     {
7625                         /* The notified task has a priority above the currently
7626                          * executing task so a yield is required. */
7627                         if( pxHigherPriorityTaskWoken != NULL )
7628                         {
7629                             *pxHigherPriorityTaskWoken = pdTRUE;
7630                         }
7631
7632                         /* Mark that a yield is pending in case the user is not
7633                          * using the "xHigherPriorityTaskWoken" parameter in an ISR
7634                          * safe FreeRTOS function. */
7635                         xYieldPendings[ 0 ] = pdTRUE;
7636                     }
7637                     else
7638                     {
7639                         mtCOVERAGE_TEST_MARKER();
7640                     }
7641                 }
7642                 #else /* #if ( configNUMBER_OF_CORES == 1 ) */
7643                 {
7644                     #if ( configUSE_PREEMPTION == 1 )
7645                     {
7646                         prvYieldForTask( pxTCB );
7647
7648                         if( xYieldPendings[ portGET_CORE_ID() ] == pdTRUE )
7649                         {
7650                             if( pxHigherPriorityTaskWoken != NULL )
7651                             {
7652                                 *pxHigherPriorityTaskWoken = pdTRUE;
7653                             }
7654                         }
7655                     }
7656                     #endif /* #if ( configUSE_PREEMPTION == 1 ) */
7657                 }
7658                 #endif /* #if ( configNUMBER_OF_CORES == 1 ) */
7659             }
7660         }
7661         taskEXIT_CRITICAL_FROM_ISR( uxSavedInterruptStatus );
7662
7663         traceRETURN_vTaskGenericNotifyGiveFromISR();
7664     }
7665
7666 #endif /* configUSE_TASK_NOTIFICATIONS */
7667 /*-----------------------------------------------------------*/
7668
7669 #if ( configUSE_TASK_NOTIFICATIONS == 1 )
7670
7671     BaseType_t xTaskGenericNotifyStateClear( TaskHandle_t xTask,
7672                                              UBaseType_t uxIndexToClear )
7673     {
7674         TCB_t * pxTCB;
7675         BaseType_t xReturn;
7676
7677         traceENTER_xTaskGenericNotifyStateClear( xTask, uxIndexToClear );
7678
7679         configASSERT( uxIndexToClear < configTASK_NOTIFICATION_ARRAY_ENTRIES );
7680
7681         /* If null is passed in here then it is the calling task that is having
7682          * its notification state cleared. */
7683         pxTCB = prvGetTCBFromHandle( xTask );
7684
7685         taskENTER_CRITICAL();
7686         {
7687             if( pxTCB->ucNotifyState[ uxIndexToClear ] == taskNOTIFICATION_RECEIVED )
7688             {
7689                 pxTCB->ucNotifyState[ uxIndexToClear ] = taskNOT_WAITING_NOTIFICATION;
7690                 xReturn = pdPASS;
7691             }
7692             else
7693             {
7694                 xReturn = pdFAIL;
7695             }
7696         }
7697         taskEXIT_CRITICAL();
7698
7699         traceRETURN_xTaskGenericNotifyStateClear( xReturn );
7700
7701         return xReturn;
7702     }
7703
7704 #endif /* configUSE_TASK_NOTIFICATIONS */
7705 /*-----------------------------------------------------------*/
7706
7707 #if ( configUSE_TASK_NOTIFICATIONS == 1 )
7708
7709     uint32_t ulTaskGenericNotifyValueClear( TaskHandle_t xTask,
7710                                             UBaseType_t uxIndexToClear,
7711                                             uint32_t ulBitsToClear )
7712     {
7713         TCB_t * pxTCB;
7714         uint32_t ulReturn;
7715
7716         traceENTER_ulTaskGenericNotifyValueClear( xTask, uxIndexToClear, ulBitsToClear );
7717
7718         configASSERT( uxIndexToClear < configTASK_NOTIFICATION_ARRAY_ENTRIES );
7719
7720         /* If null is passed in here then it is the calling task that is having
7721          * its notification state cleared. */
7722         pxTCB = prvGetTCBFromHandle( xTask );
7723
7724         taskENTER_CRITICAL();
7725         {
7726             /* Return the notification as it was before the bits were cleared,
7727              * then clear the bit mask. */
7728             ulReturn = pxTCB->ulNotifiedValue[ uxIndexToClear ];
7729             pxTCB->ulNotifiedValue[ uxIndexToClear ] &= ~ulBitsToClear;
7730         }
7731         taskEXIT_CRITICAL();
7732
7733         traceRETURN_ulTaskGenericNotifyValueClear( ulReturn );
7734
7735         return ulReturn;
7736     }
7737
7738 #endif /* configUSE_TASK_NOTIFICATIONS */
7739 /*-----------------------------------------------------------*/
7740
7741 #if ( configGENERATE_RUN_TIME_STATS == 1 )
7742
7743     configRUN_TIME_COUNTER_TYPE ulTaskGetRunTimeCounter( const TaskHandle_t xTask )
7744     {
7745         TCB_t * pxTCB;
7746
7747         traceENTER_ulTaskGetRunTimeCounter( xTask );
7748
7749         pxTCB = prvGetTCBFromHandle( xTask );
7750
7751         traceRETURN_ulTaskGetRunTimeCounter( pxTCB->ulRunTimeCounter );
7752
7753         return pxTCB->ulRunTimeCounter;
7754     }
7755
7756 #endif /* if ( configGENERATE_RUN_TIME_STATS == 1 ) */
7757 /*-----------------------------------------------------------*/
7758
7759 #if ( configGENERATE_RUN_TIME_STATS == 1 )
7760
7761     configRUN_TIME_COUNTER_TYPE ulTaskGetRunTimePercent( const TaskHandle_t xTask )
7762     {
7763         TCB_t * pxTCB;
7764         configRUN_TIME_COUNTER_TYPE ulTotalTime, ulReturn;
7765
7766         traceENTER_ulTaskGetRunTimePercent( xTask );
7767
7768         ulTotalTime = ( configRUN_TIME_COUNTER_TYPE ) portGET_RUN_TIME_COUNTER_VALUE();
7769
7770         /* For percentage calculations. */
7771         ulTotalTime /= ( configRUN_TIME_COUNTER_TYPE ) 100;
7772
7773         /* Avoid divide by zero errors. */
7774         if( ulTotalTime > ( configRUN_TIME_COUNTER_TYPE ) 0 )
7775         {
7776             pxTCB = prvGetTCBFromHandle( xTask );
7777             ulReturn = pxTCB->ulRunTimeCounter / ulTotalTime;
7778         }
7779         else
7780         {
7781             ulReturn = 0;
7782         }
7783
7784         traceRETURN_ulTaskGetRunTimePercent( ulReturn );
7785
7786         return ulReturn;
7787     }
7788
7789 #endif /* if ( configGENERATE_RUN_TIME_STATS == 1 ) */
7790 /*-----------------------------------------------------------*/
7791
7792 #if ( ( configGENERATE_RUN_TIME_STATS == 1 ) && ( INCLUDE_xTaskGetIdleTaskHandle == 1 ) )
7793
7794     configRUN_TIME_COUNTER_TYPE ulTaskGetIdleRunTimeCounter( void )
7795     {
7796         configRUN_TIME_COUNTER_TYPE ulReturn = 0;
7797         BaseType_t i;
7798
7799         traceENTER_ulTaskGetIdleRunTimeCounter();
7800
7801         for( i = 0; i < ( BaseType_t ) configNUMBER_OF_CORES; i++ )
7802         {
7803             ulReturn += xIdleTaskHandles[ i ]->ulRunTimeCounter;
7804         }
7805
7806         traceRETURN_ulTaskGetIdleRunTimeCounter( ulReturn );
7807
7808         return ulReturn;
7809     }
7810
7811 #endif /* if ( ( configGENERATE_RUN_TIME_STATS == 1 ) && ( INCLUDE_xTaskGetIdleTaskHandle == 1 ) ) */
7812 /*-----------------------------------------------------------*/
7813
7814 #if ( ( configGENERATE_RUN_TIME_STATS == 1 ) && ( INCLUDE_xTaskGetIdleTaskHandle == 1 ) )
7815
7816     configRUN_TIME_COUNTER_TYPE ulTaskGetIdleRunTimePercent( void )
7817     {
7818         configRUN_TIME_COUNTER_TYPE ulTotalTime, ulReturn;
7819         configRUN_TIME_COUNTER_TYPE ulRunTimeCounter = 0;
7820         BaseType_t i;
7821
7822         traceENTER_ulTaskGetIdleRunTimePercent();
7823
7824         ulTotalTime = portGET_RUN_TIME_COUNTER_VALUE() * configNUMBER_OF_CORES;
7825
7826         /* For percentage calculations. */
7827         ulTotalTime /= ( configRUN_TIME_COUNTER_TYPE ) 100;
7828
7829         /* Avoid divide by zero errors. */
7830         if( ulTotalTime > ( configRUN_TIME_COUNTER_TYPE ) 0 )
7831         {
7832             for( i = 0; i < ( BaseType_t ) configNUMBER_OF_CORES; i++ )
7833             {
7834                 ulRunTimeCounter += xIdleTaskHandles[ i ]->ulRunTimeCounter;
7835             }
7836
7837             ulReturn = ulRunTimeCounter / ulTotalTime;
7838         }
7839         else
7840         {
7841             ulReturn = 0;
7842         }
7843
7844         traceRETURN_ulTaskGetIdleRunTimePercent( ulReturn );
7845
7846         return ulReturn;
7847     }
7848
7849 #endif /* if ( ( configGENERATE_RUN_TIME_STATS == 1 ) && ( INCLUDE_xTaskGetIdleTaskHandle == 1 ) ) */
7850 /*-----------------------------------------------------------*/
7851
7852 static void prvAddCurrentTaskToDelayedList( TickType_t xTicksToWait,
7853                                             const BaseType_t xCanBlockIndefinitely )
7854 {
7855     TickType_t xTimeToWake;
7856     const TickType_t xConstTickCount = xTickCount;
7857
7858     #if ( INCLUDE_xTaskAbortDelay == 1 )
7859     {
7860         /* About to enter a delayed list, so ensure the ucDelayAborted flag is
7861          * reset to pdFALSE so it can be detected as having been set to pdTRUE
7862          * when the task leaves the Blocked state. */
7863         pxCurrentTCB->ucDelayAborted = pdFALSE;
7864     }
7865     #endif
7866
7867     /* Remove the task from the ready list before adding it to the blocked list
7868      * as the same list item is used for both lists. */
7869     if( uxListRemove( &( pxCurrentTCB->xStateListItem ) ) == ( UBaseType_t ) 0 )
7870     {
7871         /* The current task must be in a ready list, so there is no need to
7872          * check, and the port reset macro can be called directly. */
7873         portRESET_READY_PRIORITY( pxCurrentTCB->uxPriority, uxTopReadyPriority ); /*lint !e931 pxCurrentTCB cannot change as it is the calling task.  pxCurrentTCB->uxPriority and uxTopReadyPriority cannot change as called with scheduler suspended or in a critical section. */
7874     }
7875     else
7876     {
7877         mtCOVERAGE_TEST_MARKER();
7878     }
7879
7880     #if ( INCLUDE_vTaskSuspend == 1 )
7881     {
7882         if( ( xTicksToWait == portMAX_DELAY ) && ( xCanBlockIndefinitely != pdFALSE ) )
7883         {
7884             /* Add the task to the suspended task list instead of a delayed task
7885              * list to ensure it is not woken by a timing event.  It will block
7886              * indefinitely. */
7887             listINSERT_END( &xSuspendedTaskList, &( pxCurrentTCB->xStateListItem ) );
7888         }
7889         else
7890         {
7891             /* Calculate the time at which the task should be woken if the event
7892              * does not occur.  This may overflow but this doesn't matter, the
7893              * kernel will manage it correctly. */
7894             xTimeToWake = xConstTickCount + xTicksToWait;
7895
7896             /* The list item will be inserted in wake time order. */
7897             listSET_LIST_ITEM_VALUE( &( pxCurrentTCB->xStateListItem ), xTimeToWake );
7898
7899             if( xTimeToWake < xConstTickCount )
7900             {
7901                 /* Wake time has overflowed.  Place this item in the overflow
7902                  * list. */
7903                 traceMOVED_TASK_TO_OVERFLOW_DELAYED_LIST();
7904                 vListInsert( pxOverflowDelayedTaskList, &( pxCurrentTCB->xStateListItem ) );
7905             }
7906             else
7907             {
7908                 /* The wake time has not overflowed, so the current block list
7909                  * is used. */
7910                 traceMOVED_TASK_TO_DELAYED_LIST();
7911                 vListInsert( pxDelayedTaskList, &( pxCurrentTCB->xStateListItem ) );
7912
7913                 /* If the task entering the blocked state was placed at the
7914                  * head of the list of blocked tasks then xNextTaskUnblockTime
7915                  * needs to be updated too. */
7916                 if( xTimeToWake < xNextTaskUnblockTime )
7917                 {
7918                     xNextTaskUnblockTime = xTimeToWake;
7919                 }
7920                 else
7921                 {
7922                     mtCOVERAGE_TEST_MARKER();
7923                 }
7924             }
7925         }
7926     }
7927     #else /* INCLUDE_vTaskSuspend */
7928     {
7929         /* Calculate the time at which the task should be woken if the event
7930          * does not occur.  This may overflow but this doesn't matter, the kernel
7931          * will manage it correctly. */
7932         xTimeToWake = xConstTickCount + xTicksToWait;
7933
7934         /* The list item will be inserted in wake time order. */
7935         listSET_LIST_ITEM_VALUE( &( pxCurrentTCB->xStateListItem ), xTimeToWake );
7936
7937         if( xTimeToWake < xConstTickCount )
7938         {
7939             traceMOVED_TASK_TO_OVERFLOW_DELAYED_LIST();
7940             /* Wake time has overflowed.  Place this item in the overflow list. */
7941             vListInsert( pxOverflowDelayedTaskList, &( pxCurrentTCB->xStateListItem ) );
7942         }
7943         else
7944         {
7945             traceMOVED_TASK_TO_DELAYED_LIST();
7946             /* The wake time has not overflowed, so the current block list is used. */
7947             vListInsert( pxDelayedTaskList, &( pxCurrentTCB->xStateListItem ) );
7948
7949             /* If the task entering the blocked state was placed at the head of the
7950              * list of blocked tasks then xNextTaskUnblockTime needs to be updated
7951              * too. */
7952             if( xTimeToWake < xNextTaskUnblockTime )
7953             {
7954                 xNextTaskUnblockTime = xTimeToWake;
7955             }
7956             else
7957             {
7958                 mtCOVERAGE_TEST_MARKER();
7959             }
7960         }
7961
7962         /* Avoid compiler warning when INCLUDE_vTaskSuspend is not 1. */
7963         ( void ) xCanBlockIndefinitely;
7964     }
7965     #endif /* INCLUDE_vTaskSuspend */
7966 }
7967 /*-----------------------------------------------------------*/
7968
7969 #if ( portUSING_MPU_WRAPPERS == 1 )
7970
7971     xMPU_SETTINGS * xTaskGetMPUSettings( TaskHandle_t xTask )
7972     {
7973         TCB_t * pxTCB;
7974
7975         traceENTER_xTaskGetMPUSettings( xTask );
7976
7977         pxTCB = prvGetTCBFromHandle( xTask );
7978
7979         traceRETURN_xTaskGetMPUSettings( &( pxTCB->xMPUSettings ) );
7980
7981         return &( pxTCB->xMPUSettings );
7982     }
7983
7984 #endif /* portUSING_MPU_WRAPPERS */
7985 /*-----------------------------------------------------------*/
7986
7987 /* Code below here allows additional code to be inserted into this source file,
7988  * especially where access to file scope functions and data is needed (for example
7989  * when performing module tests). */
7990
7991 #ifdef FREERTOS_MODULE_TEST
7992     #include "tasks_test_access_functions.h"
7993 #endif
7994
7995
7996 #if ( configINCLUDE_FREERTOS_TASK_C_ADDITIONS_H == 1 )
7997
7998     #include "freertos_tasks_c_additions.h"
7999
8000     #ifdef FREERTOS_TASKS_C_ADDITIONS_INIT
8001         static void freertos_tasks_c_additions_init( void )
8002         {
8003             FREERTOS_TASKS_C_ADDITIONS_INIT();
8004         }
8005     #endif
8006
8007 #endif /* if ( configINCLUDE_FREERTOS_TASK_C_ADDITIONS_H == 1 ) */
8008 /*-----------------------------------------------------------*/
8009
8010 #if ( ( configSUPPORT_STATIC_ALLOCATION == 1 ) && ( configKERNEL_PROVIDED_STATIC_MEMORY == 1 ) && ( portUSING_MPU_WRAPPERS == 0 ) )
8011
8012 /*
8013  * This is the kernel provided implementation of vApplicationGetIdleTaskMemory()
8014  * to provide the memory that is used by the Idle task. It is used when
8015  * configKERNEL_PROVIDED_STATIC_MEMORY is set to 1. The application can provide
8016  * it's own implementation of vApplicationGetIdleTaskMemory by setting
8017  * configKERNEL_PROVIDED_STATIC_MEMORY to 0 or leaving it undefined.
8018  */
8019     void vApplicationGetIdleTaskMemory( StaticTask_t ** ppxIdleTaskTCBBuffer,
8020                                         StackType_t ** ppxIdleTaskStackBuffer,
8021                                         uint32_t * pulIdleTaskStackSize )
8022     {
8023         static StaticTask_t xIdleTaskTCB;
8024         static StackType_t uxIdleTaskStack[ configMINIMAL_STACK_SIZE ];
8025
8026         *ppxIdleTaskTCBBuffer = &( xIdleTaskTCB );
8027         *ppxIdleTaskStackBuffer = &( uxIdleTaskStack[ 0 ] );
8028         *pulIdleTaskStackSize = configMINIMAL_STACK_SIZE;
8029     }
8030
8031 #endif /* #if ( ( configSUPPORT_STATIC_ALLOCATION == 1 ) && ( configKERNEL_PROVIDED_STATIC_MEMORY == 1 ) && ( portUSING_MPU_WRAPPERS == 0 ) ) */
8032 /*-----------------------------------------------------------*/
8033
8034 #if ( ( configSUPPORT_STATIC_ALLOCATION == 1 ) && ( configKERNEL_PROVIDED_STATIC_MEMORY == 1 ) && ( portUSING_MPU_WRAPPERS == 0 ) )
8035
8036 /*
8037  * This is the kernel provided implementation of vApplicationGetTimerTaskMemory()
8038  * to provide the memory that is used by the Timer service task. It is used when
8039  * configKERNEL_PROVIDED_STATIC_MEMORY is set to 1. The application can provide
8040  * it's own implementation of vApplicationGetTimerTaskMemory by setting
8041  * configKERNEL_PROVIDED_STATIC_MEMORY to 0 or leaving it undefined.
8042  */
8043     void vApplicationGetTimerTaskMemory( StaticTask_t ** ppxTimerTaskTCBBuffer,
8044                                          StackType_t ** ppxTimerTaskStackBuffer,
8045                                          uint32_t * pulTimerTaskStackSize )
8046     {
8047         static StaticTask_t xTimerTaskTCB;
8048         static StackType_t uxTimerTaskStack[ configTIMER_TASK_STACK_DEPTH ];
8049
8050         *ppxTimerTaskTCBBuffer = &( xTimerTaskTCB );
8051         *ppxTimerTaskStackBuffer = &( uxTimerTaskStack[ 0 ] );
8052         *pulTimerTaskStackSize = configTIMER_TASK_STACK_DEPTH;
8053     }
8054
8055 #endif /* #if ( ( configSUPPORT_STATIC_ALLOCATION == 1 ) && ( configKERNEL_PROVIDED_STATIC_MEMORY == 1 ) && ( portUSING_MPU_WRAPPERS == 0 ) ) */
8056 /*-----------------------------------------------------------*/