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