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