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