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