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