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