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