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