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