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