]> begriffs open source - freertos/blob - tasks.c
RP2040: Fix compiler warning and comment (#509)
[freertos] / tasks.c
1 /*
2  * FreeRTOS SMP Kernel V202110.00
3  * Copyright (C) 2020 Amazon.com, Inc. or its affiliates.  All Rights Reserved.
4  *
5  * Permission is hereby granted, free of charge, to any person obtaining a copy of
6  * this software and associated documentation files (the "Software"), to deal in
7  * the Software without restriction, including without limitation the rights to
8  * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
9  * the Software, and to permit persons to whom the Software is furnished to do so,
10  * subject to the following conditions:
11  *
12  * The above copyright notice and this permission notice shall be included in all
13  * copies or substantial portions of the Software.
14  *
15  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
17  * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
18  * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
19  * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
20  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
21  *
22  * https://www.FreeRTOS.org
23  * https://github.com/FreeRTOS
24  *
25  */
26
27 /* Standard includes. */
28 #include <stdlib.h>
29 #include <string.h>
30
31 /* Defining MPU_WRAPPERS_INCLUDED_FROM_API_FILE prevents task.h from redefining
32  * all the API functions to use the MPU wrappers.  That should only be done when
33  * task.h is included from an application file. */
34 #define MPU_WRAPPERS_INCLUDED_FROM_API_FILE
35
36 /* FreeRTOS includes. */
37 #include "FreeRTOS.h"
38 #include "task.h"
39 #include "timers.h"
40 #include "stack_macros.h"
41
42 /* Lint e9021, e961 and e750 are suppressed as a MISRA exception justified
43  * because the MPU ports require MPU_WRAPPERS_INCLUDED_FROM_API_FILE to be defined
44  * for the header files above, but not in this file, in order to generate the
45  * correct privileged Vs unprivileged linkage and placement. */
46 #undef MPU_WRAPPERS_INCLUDED_FROM_API_FILE /*lint !e961 !e750 !e9021. */
47
48 /* Set configUSE_STATS_FORMATTING_FUNCTIONS to 2 to include the stats formatting
49  * functions but without including stdio.h here. */
50 #if ( configUSE_STATS_FORMATTING_FUNCTIONS == 1 )
51
52 /* At the bottom of this file are two optional functions that can be used
53  * to generate human readable text from the raw data generated by the
54  * uxTaskGetSystemState() function.  Note the formatting functions are provided
55  * for convenience only, and are NOT considered part of the kernel. */
56     #include <stdio.h>
57 #endif /* configUSE_STATS_FORMATTING_FUNCTIONS == 1 ) */
58
59 #if ( configUSE_PREEMPTION == 0 )
60
61 /* If the cooperative scheduler is being used then a yield should not be
62  * performed just because a higher priority task has been woken. */
63     #define taskYIELD_IF_USING_PREEMPTION()
64 #else
65     #define taskYIELD_IF_USING_PREEMPTION()    vTaskYieldWithinAPI()
66 #endif
67
68 /* Values that can be assigned to the ucNotifyState member of the TCB. */
69 #define taskNOT_WAITING_NOTIFICATION              ( ( uint8_t ) 0 ) /* Must be zero as it is the initialised value. */
70 #define taskWAITING_NOTIFICATION                  ( ( uint8_t ) 1 )
71 #define taskNOTIFICATION_RECEIVED                 ( ( uint8_t ) 2 )
72
73 /*
74  * The value used to fill the stack of a task when the task is created.  This
75  * is used purely for checking the high water mark for tasks.
76  */
77 #define tskSTACK_FILL_BYTE                        ( 0xa5U )
78
79 /* Bits used to record how a task's stack and TCB were allocated. */
80 #define tskDYNAMICALLY_ALLOCATED_STACK_AND_TCB    ( ( uint8_t ) 0 )
81 #define tskSTATICALLY_ALLOCATED_STACK_ONLY        ( ( uint8_t ) 1 )
82 #define tskSTATICALLY_ALLOCATED_STACK_AND_TCB     ( ( uint8_t ) 2 )
83
84 /* If any of the following are set then task stacks are filled with a known
85  * value so the high water mark can be determined.  If none of the following are
86  * set then don't fill the stack so there is no unnecessary dependency on memset. */
87 #if ( ( configCHECK_FOR_STACK_OVERFLOW > 1 ) || ( configUSE_TRACE_FACILITY == 1 ) || ( INCLUDE_uxTaskGetStackHighWaterMark == 1 ) || ( INCLUDE_uxTaskGetStackHighWaterMark2 == 1 ) )
88     #define tskSET_NEW_STACKS_TO_KNOWN_VALUE    1
89 #else
90     #define tskSET_NEW_STACKS_TO_KNOWN_VALUE    0
91 #endif
92
93 /*
94  * Macros used by vListTask to indicate which state a task is in.
95  */
96 #define tskRUNNING_CHAR      ( 'X' )
97 #define tskBLOCKED_CHAR      ( 'B' )
98 #define tskREADY_CHAR        ( 'R' )
99 #define tskDELETED_CHAR      ( 'D' )
100 #define tskSUSPENDED_CHAR    ( 'S' )
101
102 /*
103  * Some kernel aware debuggers require the data the debugger needs access to to
104  * be global, rather than file scope.
105  */
106 #ifdef portREMOVE_STATIC_QUALIFIER
107     #define static
108 #endif
109
110 /* The name allocated to the Idle task.  This can be overridden by defining
111  * configIDLE_TASK_NAME in FreeRTOSConfig.h. */
112 #ifndef configIDLE_TASK_NAME
113     #define configIDLE_TASK_NAME    "IDLE"
114 #endif
115
116 #if ( configUSE_PORT_OPTIMISED_TASK_SELECTION == 0 )
117
118 /* If configUSE_PORT_OPTIMISED_TASK_SELECTION is 0 then task selection is
119  * performed in a generic way that is not optimised to any particular
120  * microcontroller architecture. */
121
122 /* uxTopReadyPriority holds the priority of the highest priority ready
123  * state task. */
124     #define taskRECORD_READY_PRIORITY( uxPriority ) \
125     {                                               \
126         if( ( uxPriority ) > uxTopReadyPriority )   \
127         {                                           \
128             uxTopReadyPriority = ( uxPriority );    \
129         }                                           \
130     } /* taskRECORD_READY_PRIORITY */
131
132     /*-----------------------------------------------------------*/
133
134 /* Define away taskRESET_READY_PRIORITY() and portRESET_READY_PRIORITY() as
135  * they are only required when a port optimised method of task selection is
136  * being used. */
137     #define taskRESET_READY_PRIORITY( uxPriority )
138     #define portRESET_READY_PRIORITY( uxPriority, uxTopReadyPriority )
139
140 #else /* configUSE_PORT_OPTIMISED_TASK_SELECTION */
141
142     #error configUSE_PORT_OPTIMISED_TASK_SELECTION not yet supported in SMP
143
144 /* If configUSE_PORT_OPTIMISED_TASK_SELECTION is 1 then task selection is
145  * performed in a way that is tailored to the particular microcontroller
146  * architecture being used. */
147
148 /* A port optimised version is provided.  Call the port defined macros. */
149     #define taskRECORD_READY_PRIORITY( uxPriority )    portRECORD_READY_PRIORITY( uxPriority, uxTopReadyPriority )
150
151     /*-----------------------------------------------------------*/
152
153 /* A port optimised version is provided, call it only if the TCB being reset
154  * is being referenced from a ready list.  If it is referenced from a delayed
155  * or suspended list then it won't be in a ready list. */
156     #define taskRESET_READY_PRIORITY( uxPriority )                                                     \
157     {                                                                                                  \
158         if( listCURRENT_LIST_LENGTH( &( pxReadyTasksLists[ ( uxPriority ) ] ) ) == ( UBaseType_t ) 0 ) \
159         {                                                                                              \
160             portRESET_READY_PRIORITY( ( uxPriority ), ( uxTopReadyPriority ) );                        \
161         }                                                                                              \
162     }
163
164 #endif /* configUSE_PORT_OPTIMISED_TASK_SELECTION */
165
166 /*-----------------------------------------------------------*/
167
168 /* pxDelayedTaskList and pxOverflowDelayedTaskList are switched when the tick
169  * count overflows. */
170 #define taskSWITCH_DELAYED_LISTS()                                                \
171     {                                                                             \
172         List_t * pxTemp;                                                          \
173                                                                                   \
174         /* The delayed tasks list should be empty when the lists are switched. */ \
175         configASSERT( ( listLIST_IS_EMPTY( pxDelayedTaskList ) ) );               \
176                                                                                   \
177         pxTemp = pxDelayedTaskList;                                               \
178         pxDelayedTaskList = pxOverflowDelayedTaskList;                            \
179         pxOverflowDelayedTaskList = pxTemp;                                       \
180         xNumOfOverflows++;                                                        \
181         prvResetNextTaskUnblockTime();                                            \
182     }
183
184 /*-----------------------------------------------------------*/
185
186 /*
187  * Place the task represented by pxTCB into the appropriate ready list for
188  * the task.  It is inserted at the end of the list.
189  */
190 #define prvAddTaskToReadyList( pxTCB )                                                                 \
191     traceMOVED_TASK_TO_READY_STATE( pxTCB );                                                           \
192     taskRECORD_READY_PRIORITY( ( pxTCB )->uxPriority );                                                \
193     vListInsertEnd( &( pxReadyTasksLists[ ( pxTCB )->uxPriority ] ), &( ( pxTCB )->xStateListItem ) ); \
194     tracePOST_MOVED_TASK_TO_READY_STATE( pxTCB )
195 /*-----------------------------------------------------------*/
196
197 /*
198  * Several functions take a TaskHandle_t parameter that can optionally be NULL,
199  * where NULL is used to indicate that the handle of the currently executing
200  * task should be used in place of the parameter.  This macro simply checks to
201  * see if the parameter is NULL and returns a pointer to the appropriate TCB.
202  */
203 #define prvGetTCBFromHandle( pxHandle )    ( ( ( pxHandle ) == NULL ) ? pxCurrentTCB : ( pxHandle ) )
204
205 /* The item value of the event list item is normally used to hold the priority
206  * of the task to which it belongs (coded to allow it to be held in reverse
207  * priority order).  However, it is occasionally borrowed for other purposes.  It
208  * is important its value is not updated due to a task priority change while it is
209  * being used for another purpose.  The following bit definition is used to inform
210  * the scheduler that the value should not be changed - in which case it is the
211  * responsibility of whichever module is using the value to ensure it gets set back
212  * to its original value when it is released. */
213 #if ( configUSE_16_BIT_TICKS == 1 )
214     #define taskEVENT_LIST_ITEM_VALUE_IN_USE    0x8000U
215 #else
216     #define taskEVENT_LIST_ITEM_VALUE_IN_USE    0x80000000UL
217 #endif
218
219 /* Indicates that the task is not actively running on any core. */
220 #define taskTASK_NOT_RUNNING    ( TaskRunning_t ) ( -1 )
221
222 /* Indicates that the task is actively running but scheduled to yield. */
223 #define taskTASK_YIELDING       ( TaskRunning_t ) ( -2 )
224
225 /* Returns pdTRUE if the task is actively running and not scheduled to yield. */
226 #define taskTASK_IS_RUNNING( xTaskRunState )    ( ( 0 <= xTaskRunState ) && ( xTaskRunState < configNUM_CORES ) )
227
228 typedef BaseType_t TaskRunning_t;
229
230 /*
231  * Task control block.  A task control block (TCB) is allocated for each task,
232  * and stores task state information, including a pointer to the task's context
233  * (the task's run time environment, including register values)
234  */
235 typedef struct tskTaskControlBlock       /* The old naming convention is used to prevent breaking kernel aware debuggers. */
236 {
237     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. */
238
239     #if ( portUSING_MPU_WRAPPERS == 1 )
240         xMPU_SETTINGS xMPUSettings; /*< The MPU settings are defined as part of the port layer.  THIS MUST BE THE SECOND MEMBER OF THE TCB STRUCT. */
241     #endif
242
243     #if ( configUSE_CORE_AFFINITY == 1 && configNUM_CORES > 1 )
244         UBaseType_t uxCoreAffinityMask; /*< Used to link the task to certain cores.  UBaseType_t must have >= the same number of bits as SMP confNUM_CORES */
245     #endif
246
247     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 ). */
248     ListItem_t xEventListItem;                  /*< Used to reference a task from an event list. */
249     UBaseType_t uxPriority;                     /*< The priority of the task.  0 is the lowest priority. */
250     StackType_t * pxStack;                      /*< Points to the start of the stack. */
251     volatile TaskRunning_t xTaskRunState;       /*< Used to identify the core the task is running on, if any. */
252     BaseType_t xIsIdle;                         /*< Used to identify the idle tasks. */
253     char pcTaskName[ configMAX_TASK_NAME_LEN ]; /*< Descriptive name given to the task when created.  Facilitates debugging only. */ /*lint !e971 Unqualified char types are allowed for strings and single characters only. */
254
255     #if ( configUSE_TASK_PREEMPTION_DISABLE == 1 )
256         BaseType_t xPreemptionDisable; /*< Used to prevent the task from being preempted */
257     #endif
258
259     #if ( ( portSTACK_GROWTH > 0 ) || ( configRECORD_STACK_HIGH_ADDRESS == 1 ) )
260         StackType_t * pxEndOfStack; /*< Points to the highest valid address for the stack. */
261     #endif
262
263     #if ( portCRITICAL_NESTING_IN_TCB == 1 )
264         UBaseType_t uxCriticalNesting; /*< Holds the critical section nesting depth for ports that do not maintain their own count in the port layer. */
265     #endif
266
267     #if ( configUSE_TRACE_FACILITY == 1 )
268         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. */
269         UBaseType_t uxTaskNumber; /*< Stores a number specifically for use by third party trace code. */
270     #endif
271
272     #if ( configUSE_MUTEXES == 1 )
273         UBaseType_t uxBasePriority; /*< The priority last assigned to the task - used by the priority inheritance mechanism. */
274         UBaseType_t uxMutexesHeld;
275     #endif
276
277     #if ( configUSE_APPLICATION_TASK_TAG == 1 )
278         TaskHookFunction_t pxTaskTag;
279     #endif
280
281     #if ( configNUM_THREAD_LOCAL_STORAGE_POINTERS > 0 )
282         void * pvThreadLocalStoragePointers[ configNUM_THREAD_LOCAL_STORAGE_POINTERS ];
283     #endif
284
285     #if ( configGENERATE_RUN_TIME_STATS == 1 )
286         uint32_t ulRunTimeCounter; /*< Stores the amount of time the task has spent in the Running state. */
287     #endif
288
289     #if ( configUSE_NEWLIB_REENTRANT == 1 )
290         /* Allocate a Newlib reent structure that is specific to this task.
291          * Note Newlib support has been included by popular demand, but is not
292          * used by the FreeRTOS maintainers themselves.  FreeRTOS is not
293          * responsible for resulting newlib operation.  User must be familiar with
294          * newlib and must provide system-wide implementations of the necessary
295          * stubs. Be warned that (at the time of writing) the current newlib design
296          * implements a system-wide malloc() that must be provided with locks.
297          *
298          * See the third party link http://www.nadler.com/embedded/newlibAndFreeRTOS.html
299          * for additional information. */
300         struct  _reent xNewLib_reent;
301     #endif
302
303     #if ( configUSE_TASK_NOTIFICATIONS == 1 )
304         volatile uint32_t ulNotifiedValue[ configTASK_NOTIFICATION_ARRAY_ENTRIES ];
305         volatile uint8_t ucNotifyState[ configTASK_NOTIFICATION_ARRAY_ENTRIES ];
306     #endif
307
308     /* See the comments in FreeRTOS.h with the definition of
309      * tskSTATIC_AND_DYNAMIC_ALLOCATION_POSSIBLE. */
310     #if ( tskSTATIC_AND_DYNAMIC_ALLOCATION_POSSIBLE != 0 ) /*lint !e731 !e9029 Macro has been consolidated for readability reasons. */
311         uint8_t ucStaticallyAllocated;                     /*< Set to pdTRUE if the task is a statically allocated to ensure no attempt is made to free the memory. */
312     #endif
313
314     #if ( INCLUDE_xTaskAbortDelay == 1 )
315         uint8_t ucDelayAborted;
316     #endif
317
318     #if ( configUSE_POSIX_ERRNO == 1 )
319         int iTaskErrno;
320     #endif
321 } tskTCB;
322
323 /* The old tskTCB name is maintained above then typedefed to the new TCB_t name
324  * below to enable the use of older kernel aware debuggers. */
325 typedef tskTCB TCB_t;
326
327 /*lint -save -e956 A manual analysis and inspection has been used to determine
328  * which static variables must be declared volatile. */
329 PRIVILEGED_DATA TCB_t * volatile pxCurrentTCBs[ configNUM_CORES ] = { NULL };
330 #define pxCurrentTCB    xTaskGetCurrentTaskHandle()
331
332 /* Lists for ready and blocked tasks. --------------------
333  * xDelayedTaskList1 and xDelayedTaskList2 could be moved to function scope but
334  * doing so breaks some kernel aware debuggers and debuggers that rely on removing
335  * the static qualifier. */
336 PRIVILEGED_DATA static List_t pxReadyTasksLists[ configMAX_PRIORITIES ]; /*< Prioritised ready tasks. */
337 PRIVILEGED_DATA static List_t xDelayedTaskList1;                         /*< Delayed tasks. */
338 PRIVILEGED_DATA static List_t xDelayedTaskList2;                         /*< Delayed tasks (two lists are used - one for delays that have overflowed the current tick count. */
339 PRIVILEGED_DATA static List_t * volatile pxDelayedTaskList;              /*< Points to the delayed task list currently being used. */
340 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. */
341 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. */
342
343 #if ( INCLUDE_vTaskDelete == 1 )
344
345     PRIVILEGED_DATA static List_t xTasksWaitingTermination; /*< Tasks that have been deleted - but their memory not yet freed. */
346     PRIVILEGED_DATA static volatile UBaseType_t uxDeletedTasksWaitingCleanUp = ( UBaseType_t ) 0U;
347
348 #endif
349
350 #if ( INCLUDE_vTaskSuspend == 1 )
351
352     PRIVILEGED_DATA static List_t xSuspendedTaskList; /*< Tasks that are currently suspended. */
353
354 #endif
355
356 /* Global POSIX errno. Its value is changed upon context switching to match
357  * the errno of the currently running task. */
358 #if ( configUSE_POSIX_ERRNO == 1 )
359     int FreeRTOS_errno = 0;
360 #endif
361
362 /* Other file private variables. --------------------------------*/
363 PRIVILEGED_DATA static volatile UBaseType_t uxCurrentNumberOfTasks = ( UBaseType_t ) 0U;
364 PRIVILEGED_DATA static volatile TickType_t xTickCount = ( TickType_t ) configINITIAL_TICK_COUNT;
365 PRIVILEGED_DATA static volatile UBaseType_t uxTopReadyPriority = tskIDLE_PRIORITY;
366 PRIVILEGED_DATA static volatile BaseType_t xSchedulerRunning = pdFALSE;
367 PRIVILEGED_DATA static volatile TickType_t xPendedTicks = ( TickType_t ) 0U;
368 PRIVILEGED_DATA static volatile BaseType_t xYieldPendings[ configNUM_CORES ] = { pdFALSE };
369 PRIVILEGED_DATA static volatile BaseType_t xNumOfOverflows = ( BaseType_t ) 0;
370 PRIVILEGED_DATA static UBaseType_t uxTaskNumber = ( UBaseType_t ) 0U;
371 PRIVILEGED_DATA static volatile TickType_t xNextTaskUnblockTime = ( TickType_t ) 0U; /* Initialised to portMAX_DELAY before the scheduler starts. */
372 PRIVILEGED_DATA static TaskHandle_t xIdleTaskHandle[ configNUM_CORES ] = { NULL };   /*< Holds the handle of the idle task.  The idle task is created automatically when the scheduler is started. */
373
374 #define xYieldPending    prvGetCurrentYieldPending()
375
376 /* Improve support for OpenOCD. The kernel tracks Ready tasks via priority lists.
377  * For tracking the state of remote threads, OpenOCD uses uxTopUsedPriority
378  * to determine the number of priority lists to read back from the remote target. */
379 const volatile UBaseType_t uxTopUsedPriority = configMAX_PRIORITIES - 1U;
380
381 /* Context switches are held pending while the scheduler is suspended.  Also,
382  * interrupts must not manipulate the xStateListItem of a TCB, or any of the
383  * lists the xStateListItem can be referenced from, if the scheduler is suspended.
384  * If an interrupt needs to unblock a task while the scheduler is suspended then it
385  * moves the task's event list item into the xPendingReadyList, ready for the
386  * kernel to move the task from the pending ready list into the real ready list
387  * when the scheduler is unsuspended.  The pending ready list itself can only be
388  * accessed from a critical section.
389  *
390  * Updates to uxSchedulerSuspended must be protected by both the task and ISR locks and
391  * must not be done by an ISR. Reads must be protected by either lock and may be done by
392  * either an ISR or a task. */
393 PRIVILEGED_DATA static volatile UBaseType_t uxSchedulerSuspended = ( UBaseType_t ) pdFALSE;
394
395 #if ( configGENERATE_RUN_TIME_STATS == 1 )
396
397 /* Do not move these variables to function scope as doing so prevents the
398  * code working with debuggers that need to remove the static qualifier. */
399     PRIVILEGED_DATA static uint32_t ulTaskSwitchedInTime = 0UL;    /*< Holds the value of a timer/counter the last time a task was switched in. */
400     PRIVILEGED_DATA static volatile uint32_t ulTotalRunTime = 0UL; /*< Holds the total amount of execution time as defined by the run time counter clock. */
401
402 #endif
403
404 /*lint -restore */
405
406 /*-----------------------------------------------------------*/
407
408 /* File private functions. --------------------------------*/
409
410 /*
411  * Creates the idle tasks during scheduler start
412  */
413 static BaseType_t prvCreateIdleTasks( void );
414
415 /*
416  * Returns the yield pending count for the calling core.
417  */
418 static BaseType_t prvGetCurrentYieldPending( void );
419
420 /*
421  * Checks to see if another task moved the current task out of the ready
422  * list while it was waiting to enter a critical section and yields if so.
423  */
424 static void prvCheckForRunStateChange( void );
425
426 /*
427  * Yields the given core.
428  */
429 static void prvYieldCore( BaseType_t xCoreID );
430
431 /*
432  * Yields a core, or cores if multiple priorities are not allowed to run
433  * simultaneously, to allow the task pxTCB to run.
434  */
435 static void prvYieldForTask( TCB_t * pxTCB,
436                              const BaseType_t xPreemptEqualPriority );
437
438 /*
439  * Selects the highest priority available task
440  */
441 static BaseType_t prvSelectHighestPriorityTask( const BaseType_t xCoreID );
442
443 /**
444  * Utility task that simply returns pdTRUE if the task referenced by xTask is
445  * currently in the Suspended state, or pdFALSE if the task referenced by xTask
446  * is in any other state.
447  */
448 #if ( INCLUDE_vTaskSuspend == 1 )
449
450     static BaseType_t prvTaskIsTaskSuspended( const TaskHandle_t xTask ) PRIVILEGED_FUNCTION;
451
452 #endif /* INCLUDE_vTaskSuspend */
453
454 /*
455  * Utility to ready all the lists used by the scheduler.  This is called
456  * automatically upon the creation of the first task.
457  */
458 static void prvInitialiseTaskLists( void ) PRIVILEGED_FUNCTION;
459
460 /*
461  * The idle task, which as all tasks is implemented as a never ending loop.
462  * The idle task is automatically created and added to the ready lists upon
463  * creation of the first user task.
464  *
465  */
466 static portTASK_FUNCTION_PROTO( prvIdleTask, pvParameters ) PRIVILEGED_FUNCTION;
467 #if ( configNUM_CORES > 1 )
468     static portTASK_FUNCTION_PROTO( prvMinimalIdleTask, pvParameters ) PRIVILEGED_FUNCTION;
469 #endif
470
471 /*
472  * Utility to free all memory allocated by the scheduler to hold a TCB,
473  * including the stack pointed to by the TCB.
474  *
475  * This does not free memory allocated by the task itself (i.e. memory
476  * allocated by calls to pvPortMalloc from within the tasks application code).
477  */
478 #if ( INCLUDE_vTaskDelete == 1 )
479
480     static void prvDeleteTCB( TCB_t * pxTCB ) PRIVILEGED_FUNCTION;
481
482 #endif
483
484 /*
485  * Used only by the idle task.  This checks to see if anything has been placed
486  * in the list of tasks waiting to be deleted.  If so the task is cleaned up
487  * and its TCB deleted.
488  */
489 static void prvCheckTasksWaitingTermination( void ) PRIVILEGED_FUNCTION;
490
491 /*
492  * The currently executing task is entering the Blocked state.  Add the task to
493  * either the current or the overflow delayed task list.
494  */
495 static void prvAddCurrentTaskToDelayedList( TickType_t xTicksToWait,
496                                             const BaseType_t xCanBlockIndefinitely ) PRIVILEGED_FUNCTION;
497
498 /*
499  * Fills an TaskStatus_t structure with information on each task that is
500  * referenced from the pxList list (which may be a ready list, a delayed list,
501  * a suspended list, etc.).
502  *
503  * THIS FUNCTION IS INTENDED FOR DEBUGGING ONLY, AND SHOULD NOT BE CALLED FROM
504  * NORMAL APPLICATION CODE.
505  */
506 #if ( configUSE_TRACE_FACILITY == 1 )
507
508     static UBaseType_t prvListTasksWithinSingleList( TaskStatus_t * pxTaskStatusArray,
509                                                      List_t * pxList,
510                                                      eTaskState eState ) PRIVILEGED_FUNCTION;
511
512 #endif
513
514 /*
515  * Searches pxList for a task with name pcNameToQuery - returning a handle to
516  * the task if it is found, or NULL if the task is not found.
517  */
518 #if ( INCLUDE_xTaskGetHandle == 1 )
519
520     static TCB_t * prvSearchForNameWithinSingleList( List_t * pxList,
521                                                      const char pcNameToQuery[] ) PRIVILEGED_FUNCTION;
522
523 #endif
524
525 /*
526  * When a task is created, the stack of the task is filled with a known value.
527  * This function determines the 'high water mark' of the task stack by
528  * determining how much of the stack remains at the original preset value.
529  */
530 #if ( ( configUSE_TRACE_FACILITY == 1 ) || ( INCLUDE_uxTaskGetStackHighWaterMark == 1 ) || ( INCLUDE_uxTaskGetStackHighWaterMark2 == 1 ) )
531
532     static configSTACK_DEPTH_TYPE prvTaskCheckFreeStackSpace( const uint8_t * pucStackByte ) PRIVILEGED_FUNCTION;
533
534 #endif
535
536 /*
537  * Return the amount of time, in ticks, that will pass before the kernel will
538  * next move a task from the Blocked state to the Running state.
539  *
540  * This conditional compilation should use inequality to 0, not equality to 1.
541  * This is to ensure portSUPPRESS_TICKS_AND_SLEEP() can be called when user
542  * defined low power mode implementations require configUSE_TICKLESS_IDLE to be
543  * set to a value other than 1.
544  */
545 #if ( configUSE_TICKLESS_IDLE != 0 )
546
547     static TickType_t prvGetExpectedIdleTime( void ) PRIVILEGED_FUNCTION;
548
549 #endif
550
551 /*
552  * Set xNextTaskUnblockTime to the time at which the next Blocked state task
553  * will exit the Blocked state.
554  */
555 static void prvResetNextTaskUnblockTime( void ) PRIVILEGED_FUNCTION;
556
557 #if ( ( configUSE_TRACE_FACILITY == 1 ) && ( configUSE_STATS_FORMATTING_FUNCTIONS > 0 ) )
558
559 /*
560  * Helper function used to pad task names with spaces when printing out
561  * human readable tables of task information.
562  */
563     static char * prvWriteNameToBuffer( char * pcBuffer,
564                                         const char * pcTaskName ) PRIVILEGED_FUNCTION;
565
566 #endif
567
568 /*
569  * Called after a Task_t structure has been allocated either statically or
570  * dynamically to fill in the structure's members.
571  */
572 static void prvInitialiseNewTask( TaskFunction_t pxTaskCode,
573                                   const char * const pcName, /*lint !e971 Unqualified char types are allowed for strings and single characters only. */
574                                   const uint32_t ulStackDepth,
575                                   void * const pvParameters,
576                                   UBaseType_t uxPriority,
577                                   TaskHandle_t * const pxCreatedTask,
578                                   TCB_t * pxNewTCB,
579                                   const MemoryRegion_t * const xRegions ) PRIVILEGED_FUNCTION;
580
581 /*
582  * Called after a new task has been created and initialised to place the task
583  * under the control of the scheduler.
584  */
585 static void prvAddNewTaskToReadyList( TCB_t * pxNewTCB ) PRIVILEGED_FUNCTION;
586
587 /*
588  * freertos_tasks_c_additions_init() should only be called if the user definable
589  * macro FREERTOS_TASKS_C_ADDITIONS_INIT() is defined, as that is the only macro
590  * called by the function.
591  */
592 #ifdef FREERTOS_TASKS_C_ADDITIONS_INIT
593
594     static void freertos_tasks_c_additions_init( void ) PRIVILEGED_FUNCTION;
595
596 #endif
597
598 /*-----------------------------------------------------------*/
599
600 static BaseType_t prvGetCurrentYieldPending( void )
601 {
602     BaseType_t xReturn;
603     UBaseType_t ulState;
604
605     ulState = portDISABLE_INTERRUPTS();
606     xReturn = xYieldPendings[ portGET_CORE_ID() ];
607     portRESTORE_INTERRUPTS( ulState );
608
609     return xReturn;
610 }
611
612 /*-----------------------------------------------------------*/
613
614 static void prvCheckForRunStateChange( void )
615 {
616     UBaseType_t uxPrevCriticalNesting;
617     UBaseType_t uxPrevSchedulerSuspended;
618     TCB_t * pxThisTCB;
619
620     /* This should be skipped when entering a critical section within
621      * an ISR. If the task on the current core is no longer running, then
622      * vTaskSwitchContext() probably should be run before returning, but
623      * we don't have a way to force that to happen from here. */
624     if( portCHECK_IF_IN_ISR() == pdFALSE )
625     {
626         /* This function is always called with interrupts disabled
627          * so this is safe. */
628         pxThisTCB = pxCurrentTCBs[ portGET_CORE_ID() ];
629
630         while( pxThisTCB->xTaskRunState == taskTASK_YIELDING )
631         {
632             /* We are only here if we just entered a critical section
633             * or if we just suspended the scheduler, and another task
634             * has requested that we yield.
635             *
636             * This is slightly complicated since we need to save and restore
637             * the suspension and critical nesting counts, as well as release
638             * and reacquire the correct locks. And then do it all over again
639             * if our state changed again during the reacquisition. */
640
641             uxPrevCriticalNesting = pxThisTCB->uxCriticalNesting;
642             uxPrevSchedulerSuspended = uxSchedulerSuspended;
643
644             /* this must only be called the first time we enter into a critical
645              * section, otherwise it could context switch in the middle of a
646              * critical section. */
647             configASSERT( uxPrevCriticalNesting + uxPrevSchedulerSuspended == 1U );
648
649             uxSchedulerSuspended = 0U;
650
651             if( uxPrevCriticalNesting > 0U )
652             {
653                 pxThisTCB->uxCriticalNesting = 0U;
654                 portRELEASE_ISR_LOCK();
655                 portRELEASE_TASK_LOCK();
656             }
657             else
658             {
659                 /* uxPrevSchedulerSuspended must be 1 */
660                 portRELEASE_TASK_LOCK();
661             }
662
663             portMEMORY_BARRIER();
664             configASSERT( pxThisTCB->xTaskRunState == taskTASK_YIELDING );
665
666             portENABLE_INTERRUPTS();
667
668             /* Enabling interrupts should cause this core to immediately
669              * service the pending interrupt and yield. If the run state is still
670              * yielding here then that is a problem. */
671             configASSERT( pxThisTCB->xTaskRunState != taskTASK_YIELDING );
672
673             portDISABLE_INTERRUPTS();
674             portGET_TASK_LOCK();
675             portGET_ISR_LOCK();
676             pxCurrentTCB->uxCriticalNesting = uxPrevCriticalNesting;
677             uxSchedulerSuspended = uxPrevSchedulerSuspended;
678
679             if( uxPrevCriticalNesting == 0U )
680             {
681                 /* uxPrevSchedulerSuspended must be 1 */
682                 configASSERT( uxPrevSchedulerSuspended != ( UBaseType_t ) pdFALSE );
683                 portRELEASE_ISR_LOCK();
684             }
685         }
686     }
687 }
688
689 /*-----------------------------------------------------------*/
690
691 static void prvYieldCore( BaseType_t xCoreID )
692 {
693     /* This must be called from a critical section and
694      * xCoreID must be valid. */
695
696     if( portCHECK_IF_IN_ISR() && ( xCoreID == portGET_CORE_ID() ) )
697     {
698         xYieldPendings[ xCoreID ] = pdTRUE;
699     }
700     else if( pxCurrentTCBs[ xCoreID ]->xTaskRunState != taskTASK_YIELDING )
701     {
702         if( xCoreID == portGET_CORE_ID() )
703         {
704             xYieldPendings[ xCoreID ] = pdTRUE;
705         }
706         #if ( configNUM_CORES > 1 )
707             else
708             {
709                 portYIELD_CORE( xCoreID );
710                 pxCurrentTCBs[ xCoreID ]->xTaskRunState = taskTASK_YIELDING;
711             }
712         #endif
713     }
714 }
715
716 /*-----------------------------------------------------------*/
717
718 static void prvYieldForTask( TCB_t * pxTCB,
719                              const BaseType_t xPreemptEqualPriority )
720 {
721     BaseType_t xLowestPriority;
722     BaseType_t xTaskPriority;
723     BaseType_t xLowestPriorityCore = -1;
724     BaseType_t xYieldCount = 0;
725     BaseType_t x;
726     TaskRunning_t xTaskRunState;
727
728     /* THIS FUNCTION MUST BE CALLED FROM A CRITICAL SECTION */
729
730     configASSERT( pxCurrentTCB->uxCriticalNesting > 0U );
731
732     #if ( ( configRUN_MULTIPLE_PRIORITIES == 0 ) && ( configNUM_CORES > 1 ) )
733         {
734             /* No task should yield for this one if it is a lower priority
735              * than priority level of currently ready tasks. */
736             if( pxTCB->uxPriority < uxTopReadyPriority )
737             {
738                 return;
739             }
740         }
741     #endif
742
743     xLowestPriority = ( BaseType_t ) pxTCB->uxPriority;
744
745     if( xPreemptEqualPriority == pdFALSE )
746     {
747         /* xLowestPriority will be decremented to -1 if the priority of pxTCB
748          * is 0. This is ok as we will give system idle tasks a priority of -1 below. */
749         --xLowestPriority;
750     }
751
752     for( x = ( BaseType_t ) 0; x < ( BaseType_t ) configNUM_CORES; x++ )
753     {
754         /* System idle tasks are being assigned a priority of tskIDLE_PRIORITY - 1 here */
755         xTaskPriority = ( BaseType_t ) pxCurrentTCBs[ x ]->uxPriority - pxCurrentTCBs[ x ]->xIsIdle;
756         xTaskRunState = pxCurrentTCBs[ x ]->xTaskRunState;
757
758         if( ( taskTASK_IS_RUNNING( xTaskRunState ) != pdFALSE ) && ( xYieldPendings[ x ] == pdFALSE ) )
759         {
760             if( xTaskPriority <= xLowestPriority )
761             {
762                 #if ( configNUM_CORES > 1 )
763                     #if ( configUSE_CORE_AFFINITY == 1 )
764                         if( ( pxTCB->uxCoreAffinityMask & ( 1 << x ) ) != 0 )
765                     #endif
766                 #endif
767                 {
768                     #if ( configUSE_TASK_PREEMPTION_DISABLE == 1 )
769                         if( pxCurrentTCBs[ x ]->xPreemptionDisable == pdFALSE )
770                     #endif
771                     {
772                         xLowestPriority = xTaskPriority;
773                         xLowestPriorityCore = x;
774                     }
775                 }
776             }
777             else
778             {
779                 mtCOVERAGE_TEST_MARKER();
780             }
781
782             #if ( ( configRUN_MULTIPLE_PRIORITIES == 0 ) && ( configNUM_CORES > 1 ) ) && 1
783                 {
784                     /* Yield all currently running non-idle tasks with a priority lower than
785                      * the task that needs to run. */
786                     if( ( ( BaseType_t ) tskIDLE_PRIORITY - 1 < xTaskPriority ) && ( xTaskPriority < ( BaseType_t ) pxTCB->uxPriority ) )
787                     {
788                         prvYieldCore( x );
789                         xYieldCount++;
790                     }
791                     else
792                     {
793                         mtCOVERAGE_TEST_MARKER();
794                     }
795                 }
796             #endif /* if ( ( configRUN_MULTIPLE_PRIORITIES == 0 ) && ( configNUM_CORES > 1 ) ) && 1 */
797         }
798         else
799         {
800             mtCOVERAGE_TEST_MARKER();
801         }
802     }
803
804     if( ( xYieldCount == 0 ) && taskVALID_CORE_ID( xLowestPriorityCore ) )
805     {
806         prvYieldCore( xLowestPriorityCore );
807         xYieldCount++;
808     }
809
810     #if ( ( configRUN_MULTIPLE_PRIORITIES == 0 ) && ( configNUM_CORES > 1 ) )
811         /* Verify that the calling core always yields to higher priority tasks */
812         if( !pxCurrentTCBs[ portGET_CORE_ID() ]->xIsIdle && ( pxTCB->uxPriority > pxCurrentTCBs[ portGET_CORE_ID() ]->uxPriority ) )
813         {
814             configASSERT( xYieldPendings[ portGET_CORE_ID() ] == pdTRUE || taskTASK_IS_RUNNING( pxCurrentTCBs[ portGET_CORE_ID() ]->xTaskRunState ) == pdFALSE );
815         }
816     #endif
817 }
818 /*-----------------------------------------------------------*/
819
820 #if ( configUSE_PORT_OPTIMISED_TASK_SELECTION == 0 )
821
822     static BaseType_t prvSelectHighestPriorityTask( const BaseType_t xCoreID )
823     {
824         UBaseType_t uxCurrentPriority = uxTopReadyPriority;
825         BaseType_t xTaskScheduled = pdFALSE;
826         BaseType_t xDecrementTopPriority = pdTRUE;
827
828         #if ( ( configNUM_CORES > 1 ) && ( configUSE_CORE_AFFINITY == 1 ) )
829             TCB_t * pxPreviousTCB = NULL;
830         #endif
831         #if ( ( configRUN_MULTIPLE_PRIORITIES == 0 ) && ( configNUM_CORES > 1 ) )
832             BaseType_t xPriorityDropped = pdFALSE;
833         #endif
834
835         while( xTaskScheduled == pdFALSE )
836         {
837             #if ( ( configRUN_MULTIPLE_PRIORITIES == 0 ) && ( configNUM_CORES > 1 ) )
838                 {
839                     if( uxCurrentPriority < uxTopReadyPriority )
840                     {
841                         /* We can't schedule any tasks, other than idle, that have a
842                          * priority lower than the priority of a task currently running
843                          * on another core. */
844                         uxCurrentPriority = tskIDLE_PRIORITY;
845                     }
846                 }
847             #endif
848
849             if( listLIST_IS_EMPTY( &( pxReadyTasksLists[ uxCurrentPriority ] ) ) == pdFALSE )
850             {
851                 List_t * const pxReadyList = &( pxReadyTasksLists[ uxCurrentPriority ] );
852                 ListItem_t * pxLastTaskItem = pxReadyList->pxIndex->pxPrevious;
853                 ListItem_t * pxTaskItem = pxLastTaskItem;
854
855                 if( ( void * ) pxLastTaskItem == ( void * ) &( pxReadyList->xListEnd ) )
856                 {
857                     pxLastTaskItem = pxLastTaskItem->pxPrevious;
858                 }
859
860                 /* The ready task list for uxCurrentPriority is not empty, so uxTopReadyPriority
861                  * must not be decremented any further */
862                 xDecrementTopPriority = pdFALSE;
863
864                 do
865                 {
866                     TCB_t * pxTCB;
867
868                     pxTaskItem = pxTaskItem->pxNext;
869
870                     if( ( void * ) pxTaskItem == ( void * ) &( pxReadyList->xListEnd ) )
871                     {
872                         pxTaskItem = pxTaskItem->pxNext;
873                     }
874
875                     pxTCB = pxTaskItem->pvOwner;
876
877                     /*debug_printf("Attempting to schedule %s on core %d\n", pxTCB->pcTaskName, portGET_CORE_ID() ); */
878
879                     #if ( ( configRUN_MULTIPLE_PRIORITIES == 0 ) && ( configNUM_CORES > 1 ) )
880                         {
881                             /* When falling back to the idle priority because only one priority
882                              * level is allowed to run at a time, we should ONLY schedule the true
883                              * idle tasks, not user tasks at the idle priority. */
884                             if( uxCurrentPriority < uxTopReadyPriority )
885                             {
886                                 if( pxTCB->xIsIdle == pdFALSE )
887                                 {
888                                     continue;
889                                 }
890                             }
891                         }
892                     #endif /* if ( ( configRUN_MULTIPLE_PRIORITIES == 0 ) && ( configNUM_CORES > 1 ) ) */
893
894                     if( pxTCB->xTaskRunState == taskTASK_NOT_RUNNING )
895                     {
896                         #if ( configNUM_CORES > 1 )
897                             #if ( configUSE_CORE_AFFINITY == 1 )
898                                 if( ( pxTCB->uxCoreAffinityMask & ( 1 << xCoreID ) ) != 0 )
899                             #endif
900                         #endif
901                         {
902                             /* If the task is not being executed by any core swap it in */
903                             pxCurrentTCBs[ xCoreID ]->xTaskRunState = taskTASK_NOT_RUNNING;
904                             #if ( ( configNUM_CORES > 1 ) && ( configUSE_CORE_AFFINITY == 1 ) )
905                                 pxPreviousTCB = pxCurrentTCBs[ xCoreID ];
906                             #endif
907                             pxTCB->xTaskRunState = ( TaskRunning_t ) xCoreID;
908                             pxCurrentTCBs[ xCoreID ] = pxTCB;
909                             xTaskScheduled = pdTRUE;
910                         }
911                     }
912                     else if( pxTCB == pxCurrentTCBs[ xCoreID ] )
913                     {
914                         configASSERT( ( pxTCB->xTaskRunState == xCoreID ) || ( pxTCB->xTaskRunState == taskTASK_YIELDING ) );
915                         #if ( configNUM_CORES > 1 )
916                             #if ( configUSE_CORE_AFFINITY == 1 )
917                                 if( ( pxTCB->uxCoreAffinityMask & ( 1 << xCoreID ) ) != 0 )
918                             #endif
919                         #endif
920                         {
921                             /* The task is already running on this core, mark it as scheduled */
922                             pxTCB->xTaskRunState = ( TaskRunning_t ) xCoreID;
923                             xTaskScheduled = pdTRUE;
924                         }
925                     }
926
927                     if( xTaskScheduled != pdFALSE )
928                     {
929                         /* Once a task has been selected to run on this core,
930                          * move it to the end of the ready task list. */
931                         uxListRemove( pxTaskItem );
932                         vListInsertEnd( pxReadyList, pxTaskItem );
933                         break;
934                     }
935                 } while( pxTaskItem != pxLastTaskItem );
936             }
937             else
938             {
939                 if( xDecrementTopPriority != pdFALSE )
940                 {
941                     uxTopReadyPriority--;
942                     #if ( ( configRUN_MULTIPLE_PRIORITIES == 0 ) && ( configNUM_CORES > 1 ) )
943                         {
944                             xPriorityDropped = pdTRUE;
945                         }
946                     #endif
947                 }
948             }
949
950             /* This function can get called by vTaskSuspend() before the scheduler is started.
951              * In that case, since the idle tasks have not yet been created it is possible that we
952              * won't find a new task to schedule. Return pdFALSE in this case. */
953             if( ( xSchedulerRunning == pdFALSE ) && ( uxCurrentPriority == tskIDLE_PRIORITY ) && ( xTaskScheduled == pdFALSE ) )
954             {
955                 return pdFALSE;
956             }
957
958             configASSERT( ( uxCurrentPriority > tskIDLE_PRIORITY ) || ( xTaskScheduled == pdTRUE ) );
959             uxCurrentPriority--;
960         }
961
962         configASSERT( taskTASK_IS_RUNNING( pxCurrentTCBs[ xCoreID ]->xTaskRunState ) );
963
964         #if ( ( configRUN_MULTIPLE_PRIORITIES == 0 ) && ( configNUM_CORES > 1 ) )
965             if( xPriorityDropped != pdFALSE )
966             {
967                 /* There may be several ready tasks that were being prevented from running because there was
968                  * a higher priority task running. Now that the last of the higher priority tasks is no longer
969                  * running, make sure all the other idle tasks yield. */
970                 UBaseType_t x;
971
972                 for( x = ( BaseType_t ) 0; x < ( BaseType_t ) configNUM_CORES; x++ )
973                 {
974                     if( pxCurrentTCBs[ x ]->xIsIdle != pdFALSE )
975                     {
976                         prvYieldCore( x );
977                     }
978                 }
979             }
980         #endif /* if ( ( configRUN_MULTIPLE_PRIORITIES == 0 ) && ( configNUM_CORES > 1 ) ) */
981
982         #if ( configNUM_CORES > 1 )
983             #if ( configUSE_CORE_AFFINITY == 1 )
984                 if( ( pxPreviousTCB != NULL ) && ( listIS_CONTAINED_WITHIN( &( pxReadyTasksLists[ pxPreviousTCB->uxPriority ] ), &( pxPreviousTCB->xStateListItem ) ) != pdFALSE ) )
985                 {
986                     /* A ready task was just bumped off this core. Look at the cores it can run from
987                      * from to see if it is able to run on any of them */
988                     UBaseType_t uxCoreMap = pxPreviousTCB->uxCoreAffinityMask;
989                     BaseType_t xLowestPriority = pxPreviousTCB->uxPriority - pxPreviousTCB->xIsIdle;
990                     BaseType_t xLowestPriorityCore = -1;
991
992                     if( ( uxCoreMap & ( 1 << xCoreID ) ) != 0 )
993                     {
994                         /* The ready task that was removed from this core is not excluded from it.
995                          * Only look at the intersection of the cores the removed task is allowed to run
996                          * on with the cores that the new task is excluded from. It is possible that the
997                          * new task was only placed onto this core because it is excluded from another.
998                          * Check to see if the previous task could run on one of those cores. */
999                         uxCoreMap &= ~( pxCurrentTCBs[ xCoreID ]->uxCoreAffinityMask );
1000                     }
1001                     else
1002                     {
1003                         /* The ready task that was removed from this core is excluded from it. */
1004                     }
1005
1006                     uxCoreMap &= ( ( 1 << configNUM_CORES ) - 1 );
1007
1008                     while( uxCoreMap != 0 )
1009                     {
1010                         int uxCore = 31UL - ( uint32_t ) __builtin_clz( uxCoreMap );
1011
1012                         configASSERT( taskVALID_CORE_ID( uxCore ) );
1013
1014                         uxCoreMap &= ~( 1 << uxCore );
1015
1016                         BaseType_t xTaskPriority = ( BaseType_t ) pxCurrentTCBs[ uxCore ]->uxPriority - pxCurrentTCBs[ uxCore ]->xIsIdle;
1017
1018                         if( ( xTaskPriority < xLowestPriority ) && ( taskTASK_IS_RUNNING( pxCurrentTCBs[ uxCore ]->xTaskRunState ) != pdFALSE ) && ( xYieldPendings[ uxCore ] == pdFALSE ) )
1019                         {
1020                             #if ( configUSE_TASK_PREEMPTION_DISABLE == 1 )
1021                                 if( pxCurrentTCBs[ uxCore ]->xPreemptionDisable == pdFALSE )
1022                             #endif
1023                             {
1024                                 xLowestPriority = xTaskPriority;
1025                                 xLowestPriorityCore = uxCore;
1026                             }
1027                         }
1028                     }
1029
1030                     if( taskVALID_CORE_ID( xLowestPriorityCore ) )
1031                     {
1032                         prvYieldCore( xLowestPriorityCore );
1033                     }
1034                 }
1035             #endif /* if ( configUSE_CORE_AFFINITY == 1 ) */
1036         #endif /* if ( configNUM_CORES > 1 ) */
1037
1038         return pdTRUE;
1039     }
1040
1041 #else /* configUSE_PORT_OPTIMISED_TASK_SELECTION */
1042
1043     static void prvSelectHighestPriorityTask( BaseType_t xCoreID )
1044     {
1045         UBaseType_t uxTopPriority;
1046
1047         /* Find the highest priority list that contains ready tasks. */
1048         portGET_HIGHEST_PRIORITY( uxTopPriority, uxTopReadyPriority );
1049         configASSERT( listCURRENT_LIST_LENGTH( &( pxReadyTasksLists[ uxTopPriority ] ) ) > 0 );
1050         listGET_OWNER_OF_NEXT_ENTRY( pxCurrentTCB, &( pxReadyTasksLists[ uxTopPriority ] ) );
1051     }
1052
1053 #endif /* configUSE_PORT_OPTIMISED_TASK_SELECTION */
1054 /*-----------------------------------------------------------*/
1055
1056 #if ( configSUPPORT_STATIC_ALLOCATION == 1 )
1057
1058     TaskHandle_t xTaskCreateStatic( TaskFunction_t pxTaskCode,
1059                                     const char * const pcName, /*lint !e971 Unqualified char types are allowed for strings and single characters only. */
1060                                     const uint32_t ulStackDepth,
1061                                     void * const pvParameters,
1062                                     UBaseType_t uxPriority,
1063                                     StackType_t * const puxStackBuffer,
1064                                     StaticTask_t * const pxTaskBuffer )
1065     #if ( ( configNUM_CORES > 1 ) && ( configUSE_CORE_AFFINITY == 1 ) )
1066         {
1067             return xTaskCreateStaticAffinitySet(pxTaskCode, pcName, ulStackDepth, pvParameters, uxPriority, puxStackBuffer, pxTaskBuffer, tskNO_AFFINITY);
1068         }
1069
1070         TaskHandle_t xTaskCreateStaticAffinitySet( TaskFunction_t pxTaskCode,
1071                                                    const char * const pcName,   /*lint !e971 Unqualified char types are allowed for strings and single characters only. */
1072                                                    const uint32_t ulStackDepth,
1073                                                    void * const pvParameters,
1074                                                    UBaseType_t uxPriority,
1075                                                    StackType_t * const puxStackBuffer,
1076                                                    StaticTask_t * const pxTaskBuffer,
1077                                                    UBaseType_t uxCoreAffinityMask )
1078     #endif /* ( configNUM_CORES > 1 ) && ( configUSE_CORE_AFFINITY == 1 ) */
1079     {
1080         TCB_t * pxNewTCB;
1081         TaskHandle_t xReturn;
1082
1083         configASSERT( puxStackBuffer != NULL );
1084         configASSERT( pxTaskBuffer != NULL );
1085
1086         #if ( configASSERT_DEFINED == 1 )
1087             {
1088                 /* Sanity check that the size of the structure used to declare a
1089                  * variable of type StaticTask_t equals the size of the real task
1090                  * structure. */
1091                 volatile size_t xSize = sizeof( StaticTask_t );
1092                 configASSERT( xSize == sizeof( TCB_t ) );
1093                 ( void ) xSize; /* Prevent lint warning when configASSERT() is not used. */
1094             }
1095         #endif /* configASSERT_DEFINED */
1096
1097         if( ( pxTaskBuffer != NULL ) && ( puxStackBuffer != NULL ) )
1098         {
1099             /* The memory used for the task's TCB and stack are passed into this
1100              * function - use them. */
1101             pxNewTCB = ( TCB_t * ) pxTaskBuffer; /*lint !e740 !e9087 Unusual cast is ok as the structures are designed to have the same alignment, and the size is checked by an assert. */
1102             pxNewTCB->pxStack = ( StackType_t * ) puxStackBuffer;
1103
1104             #if ( tskSTATIC_AND_DYNAMIC_ALLOCATION_POSSIBLE != 0 ) /*lint !e731 !e9029 Macro has been consolidated for readability reasons. */
1105                 {
1106                     /* Tasks can be created statically or dynamically, so note this
1107                      * task was created statically in case the task is later deleted. */
1108                     pxNewTCB->ucStaticallyAllocated = tskSTATICALLY_ALLOCATED_STACK_AND_TCB;
1109                 }
1110             #endif /* tskSTATIC_AND_DYNAMIC_ALLOCATION_POSSIBLE */
1111
1112             prvInitialiseNewTask( pxTaskCode, pcName, ulStackDepth, pvParameters, uxPriority, &xReturn, pxNewTCB, NULL );
1113
1114             #if ( ( configNUM_CORES > 1 ) && ( configUSE_CORE_AFFINITY == 1 ) )
1115                 {
1116                     /* Set the task's affinity before scheduling it */
1117                     pxNewTCB->uxCoreAffinityMask = uxCoreAffinityMask;
1118                 }
1119             #endif
1120
1121             prvAddNewTaskToReadyList( pxNewTCB );
1122         }
1123         else
1124         {
1125             xReturn = NULL;
1126         }
1127
1128         return xReturn;
1129     }
1130
1131 #endif /* SUPPORT_STATIC_ALLOCATION */
1132 /*-----------------------------------------------------------*/
1133
1134 #if ( ( portUSING_MPU_WRAPPERS == 1 ) && ( configSUPPORT_STATIC_ALLOCATION == 1 ) )
1135
1136     BaseType_t xTaskCreateRestrictedStatic( const TaskParameters_t * const pxTaskDefinition,
1137                                             TaskHandle_t * pxCreatedTask )
1138     #if ( ( configNUM_CORES > 1 ) && ( configUSE_CORE_AFFINITY == 1 ) )
1139         {
1140             return xTaskCreateRestrictedStaticAffinitySet( pxTaskDefinition, tskNO_AFFINITY, pxCreatedTask );
1141         }
1142
1143         BaseType_t xTaskCreateRestrictedStaticAffinitySet( const TaskParameters_t * const pxTaskDefinition,
1144                                                            UBaseType_t uxCoreAffinityMask,
1145                                                            TaskHandle_t * pxCreatedTask )
1146     #endif /* ( configNUM_CORES > 1 ) && ( configUSE_CORE_AFFINITY == 1 ) */
1147     {
1148         TCB_t * pxNewTCB;
1149         BaseType_t xReturn = errCOULD_NOT_ALLOCATE_REQUIRED_MEMORY;
1150
1151         configASSERT( pxTaskDefinition->puxStackBuffer != NULL );
1152         configASSERT( pxTaskDefinition->pxTaskBuffer != NULL );
1153
1154         if( ( pxTaskDefinition->puxStackBuffer != NULL ) && ( pxTaskDefinition->pxTaskBuffer != NULL ) )
1155         {
1156             /* Allocate space for the TCB.  Where the memory comes from depends
1157              * on the implementation of the port malloc function and whether or
1158              * not static allocation is being used. */
1159             pxNewTCB = ( TCB_t * ) pxTaskDefinition->pxTaskBuffer;
1160
1161             /* Store the stack location in the TCB. */
1162             pxNewTCB->pxStack = pxTaskDefinition->puxStackBuffer;
1163
1164             #if ( tskSTATIC_AND_DYNAMIC_ALLOCATION_POSSIBLE != 0 )
1165                 {
1166                     /* Tasks can be created statically or dynamically, so note this
1167                      * task was created statically in case the task is later deleted. */
1168                     pxNewTCB->ucStaticallyAllocated = tskSTATICALLY_ALLOCATED_STACK_AND_TCB;
1169                 }
1170             #endif /* tskSTATIC_AND_DYNAMIC_ALLOCATION_POSSIBLE */
1171
1172             prvInitialiseNewTask( pxTaskDefinition->pvTaskCode,
1173                                   pxTaskDefinition->pcName,
1174                                   ( uint32_t ) pxTaskDefinition->usStackDepth,
1175                                   pxTaskDefinition->pvParameters,
1176                                   pxTaskDefinition->uxPriority,
1177                                   pxCreatedTask, pxNewTCB,
1178                                   pxTaskDefinition->xRegions );
1179
1180             #if ( ( configNUM_CORES > 1 ) && ( configUSE_CORE_AFFINITY == 1 ) )
1181                 {
1182                     /* Set the task's affinity before scheduling it */
1183                     pxNewTCB->uxCoreAffinityMask = uxCoreAffinityMask;
1184                 }
1185             #endif
1186
1187             prvAddNewTaskToReadyList( pxNewTCB );
1188             xReturn = pdPASS;
1189         }
1190
1191         return xReturn;
1192     }
1193
1194 #endif /* ( portUSING_MPU_WRAPPERS == 1 ) && ( configSUPPORT_STATIC_ALLOCATION == 1 ) */
1195 /*-----------------------------------------------------------*/
1196
1197 #if ( ( portUSING_MPU_WRAPPERS == 1 ) && ( configSUPPORT_DYNAMIC_ALLOCATION == 1 ) )
1198
1199     BaseType_t xTaskCreateRestricted( const TaskParameters_t * const pxTaskDefinition,
1200                                       TaskHandle_t * pxCreatedTask )
1201     #if ( ( configNUM_CORES > 1 ) && ( configUSE_CORE_AFFINITY == 1 ) )
1202         {
1203             return xTaskCreateRestrictedAffinitySet( pxTaskDefinition, tskNO_AFFINITY, pxCreatedTask );
1204         }
1205
1206         BaseType_t xTaskCreateRestrictedAffinitySet( const TaskParameters_t * const pxTaskDefinition,
1207                                                      UBaseType_t uxCoreAffinityMask,
1208                                                      TaskHandle_t * pxCreatedTask )
1209     #endif /* ( configNUM_CORES > 1 ) && ( configUSE_CORE_AFFINITY == 1 ) */
1210     {
1211         TCB_t * pxNewTCB;
1212         BaseType_t xReturn = errCOULD_NOT_ALLOCATE_REQUIRED_MEMORY;
1213
1214         configASSERT( pxTaskDefinition->puxStackBuffer );
1215
1216         if( pxTaskDefinition->puxStackBuffer != NULL )
1217         {
1218             /* Allocate space for the TCB.  Where the memory comes from depends
1219              * on the implementation of the port malloc function and whether or
1220              * not static allocation is being used. */
1221             pxNewTCB = ( TCB_t * ) pvPortMalloc( sizeof( TCB_t ) );
1222
1223             if( pxNewTCB != NULL )
1224             {
1225                 /* Store the stack location in the TCB. */
1226                 pxNewTCB->pxStack = pxTaskDefinition->puxStackBuffer;
1227
1228                 #if ( tskSTATIC_AND_DYNAMIC_ALLOCATION_POSSIBLE != 0 )
1229                     {
1230                         /* Tasks can be created statically or dynamically, so note
1231                          * this task had a statically allocated stack in case it is
1232                          * later deleted.  The TCB was allocated dynamically. */
1233                         pxNewTCB->ucStaticallyAllocated = tskSTATICALLY_ALLOCATED_STACK_ONLY;
1234                     }
1235                 #endif /* tskSTATIC_AND_DYNAMIC_ALLOCATION_POSSIBLE */
1236
1237                 prvInitialiseNewTask( pxTaskDefinition->pvTaskCode,
1238                                       pxTaskDefinition->pcName,
1239                                       ( uint32_t ) pxTaskDefinition->usStackDepth,
1240                                       pxTaskDefinition->pvParameters,
1241                                       pxTaskDefinition->uxPriority,
1242                                       pxCreatedTask, pxNewTCB,
1243                                       pxTaskDefinition->xRegions );
1244
1245                 #if ( ( configNUM_CORES > 1 ) && ( configUSE_CORE_AFFINITY == 1 ) )
1246                     {
1247                         /* Set the task's affinity before scheduling it */
1248                         pxNewTCB->uxCoreAffinityMask = uxCoreAffinityMask;
1249                     }
1250                 #endif
1251
1252                 prvAddNewTaskToReadyList( pxNewTCB );
1253                 xReturn = pdPASS;
1254             }
1255         }
1256
1257         return xReturn;
1258     }
1259
1260 #endif /* portUSING_MPU_WRAPPERS */
1261 /*-----------------------------------------------------------*/
1262
1263 #if ( configSUPPORT_DYNAMIC_ALLOCATION == 1 )
1264
1265     BaseType_t xTaskCreate( TaskFunction_t pxTaskCode,
1266                             const char * const pcName, /*lint !e971 Unqualified char types are allowed for strings and single characters only. */
1267                             const configSTACK_DEPTH_TYPE usStackDepth,
1268                             void * const pvParameters,
1269                             UBaseType_t uxPriority,
1270                             TaskHandle_t * const pxCreatedTask )
1271     #if ( ( configNUM_CORES > 1 ) && ( configUSE_CORE_AFFINITY == 1 ) )
1272         {
1273             return xTaskCreateAffinitySet(pxTaskCode, pcName, usStackDepth, pvParameters, uxPriority, tskNO_AFFINITY, pxCreatedTask);
1274         }
1275
1276         BaseType_t xTaskCreateAffinitySet( TaskFunction_t pxTaskCode,
1277                                            const char * const pcName,     /*lint !e971 Unqualified char types are allowed for strings and single characters only. */
1278                                            const configSTACK_DEPTH_TYPE usStackDepth,
1279                                            void * const pvParameters,
1280                                            UBaseType_t uxPriority,
1281                                            UBaseType_t uxCoreAffinityMask,
1282                                            TaskHandle_t * const pxCreatedTask )
1283     #endif /* ( configNUM_CORES > 1 ) && ( configUSE_CORE_AFFINITY == 1 ) */
1284     {
1285         TCB_t * pxNewTCB;
1286         BaseType_t xReturn;
1287
1288         /* If the stack grows down then allocate the stack then the TCB so the stack
1289          * does not grow into the TCB.  Likewise if the stack grows up then allocate
1290          * the TCB then the stack. */
1291         #if ( portSTACK_GROWTH > 0 )
1292             {
1293                 /* Allocate space for the TCB.  Where the memory comes from depends on
1294                  * the implementation of the port malloc function and whether or not static
1295                  * allocation is being used. */
1296                 pxNewTCB = ( TCB_t * ) pvPortMalloc( sizeof( TCB_t ) );
1297
1298                 if( pxNewTCB != NULL )
1299                 {
1300                     /* Allocate space for the stack used by the task being created.
1301                      * The base of the stack memory stored in the TCB so the task can
1302                      * be deleted later if required. */
1303                     pxNewTCB->pxStack = ( StackType_t * ) pvPortMallocStack( ( ( ( size_t ) usStackDepth ) * sizeof( StackType_t ) ) ); /*lint !e961 MISRA exception as the casts are only redundant for some ports. */
1304
1305                     if( pxNewTCB->pxStack == NULL )
1306                     {
1307                         /* Could not allocate the stack.  Delete the allocated TCB. */
1308                         vPortFree( pxNewTCB );
1309                         pxNewTCB = NULL;
1310                     }
1311                 }
1312             }
1313         #else /* portSTACK_GROWTH */
1314             {
1315                 StackType_t * pxStack;
1316
1317                 /* Allocate space for the stack used by the task being created. */
1318                 pxStack = pvPortMallocStack( ( ( ( size_t ) usStackDepth ) * sizeof( StackType_t ) ) ); /*lint !e9079 All values returned by pvPortMalloc() have at least the alignment required by the MCU's stack and this allocation is the stack. */
1319
1320                 if( pxStack != NULL )
1321                 {
1322                     /* Allocate space for the TCB. */
1323                     pxNewTCB = ( TCB_t * ) pvPortMalloc( sizeof( TCB_t ) ); /*lint !e9087 !e9079 All values returned by pvPortMalloc() have at least the alignment required by the MCU's stack, and the first member of TCB_t is always a pointer to the task's stack. */
1324
1325                     if( pxNewTCB != NULL )
1326                     {
1327                         /* Store the stack location in the TCB. */
1328                         pxNewTCB->pxStack = pxStack;
1329                     }
1330                     else
1331                     {
1332                         /* The stack cannot be used as the TCB was not created.  Free
1333                          * it again. */
1334                         vPortFreeStack( pxStack );
1335                     }
1336                 }
1337                 else
1338                 {
1339                     pxNewTCB = NULL;
1340                 }
1341             }
1342         #endif /* portSTACK_GROWTH */
1343
1344         if( pxNewTCB != NULL )
1345         {
1346             #if ( tskSTATIC_AND_DYNAMIC_ALLOCATION_POSSIBLE != 0 ) /*lint !e9029 !e731 Macro has been consolidated for readability reasons. */
1347                 {
1348                     /* Tasks can be created statically or dynamically, so note this
1349                      * task was created dynamically in case it is later deleted. */
1350                     pxNewTCB->ucStaticallyAllocated = tskDYNAMICALLY_ALLOCATED_STACK_AND_TCB;
1351                 }
1352             #endif /* tskSTATIC_AND_DYNAMIC_ALLOCATION_POSSIBLE */
1353
1354             prvInitialiseNewTask( pxTaskCode, pcName, ( uint32_t ) usStackDepth, pvParameters, uxPriority, pxCreatedTask, pxNewTCB, NULL );
1355
1356             #if ( ( configNUM_CORES > 1 ) && ( configUSE_CORE_AFFINITY == 1 ) )
1357                 {
1358                     /* Set the task's affinity before scheduling it */
1359                     pxNewTCB->uxCoreAffinityMask = uxCoreAffinityMask;
1360                 }
1361             #endif
1362
1363             prvAddNewTaskToReadyList( pxNewTCB );
1364             xReturn = pdPASS;
1365         }
1366         else
1367         {
1368             xReturn = errCOULD_NOT_ALLOCATE_REQUIRED_MEMORY;
1369         }
1370
1371         return xReturn;
1372     }
1373
1374 #endif /* configSUPPORT_DYNAMIC_ALLOCATION */
1375 /*-----------------------------------------------------------*/
1376
1377 static void prvInitialiseNewTask( TaskFunction_t pxTaskCode,
1378                                   const char * const pcName, /*lint !e971 Unqualified char types are allowed for strings and single characters only. */
1379                                   const uint32_t ulStackDepth,
1380                                   void * const pvParameters,
1381                                   UBaseType_t uxPriority,
1382                                   TaskHandle_t * const pxCreatedTask,
1383                                   TCB_t * pxNewTCB,
1384                                   const MemoryRegion_t * const xRegions )
1385 {
1386     StackType_t * pxTopOfStack;
1387     UBaseType_t x;
1388
1389     #if ( portUSING_MPU_WRAPPERS == 1 )
1390         /* Should the task be created in privileged mode? */
1391         BaseType_t xRunPrivileged;
1392
1393         if( ( uxPriority & portPRIVILEGE_BIT ) != 0U )
1394         {
1395             xRunPrivileged = pdTRUE;
1396         }
1397         else
1398         {
1399             xRunPrivileged = pdFALSE;
1400         }
1401         uxPriority &= ~portPRIVILEGE_BIT;
1402     #endif /* portUSING_MPU_WRAPPERS == 1 */
1403
1404     /* Avoid dependency on memset() if it is not required. */
1405     #if ( tskSET_NEW_STACKS_TO_KNOWN_VALUE == 1 )
1406         {
1407             /* Fill the stack with a known value to assist debugging. */
1408             ( void ) memset( pxNewTCB->pxStack, ( int ) tskSTACK_FILL_BYTE, ( size_t ) ulStackDepth * sizeof( StackType_t ) );
1409         }
1410     #endif /* tskSET_NEW_STACKS_TO_KNOWN_VALUE */
1411
1412     /* Calculate the top of stack address.  This depends on whether the stack
1413      * grows from high memory to low (as per the 80x86) or vice versa.
1414      * portSTACK_GROWTH is used to make the result positive or negative as required
1415      * by the port. */
1416     #if ( portSTACK_GROWTH < 0 )
1417         {
1418             pxTopOfStack = &( pxNewTCB->pxStack[ ulStackDepth - ( uint32_t ) 1 ] );
1419             pxTopOfStack = ( StackType_t * ) ( ( ( portPOINTER_SIZE_TYPE ) pxTopOfStack ) & ( ~( ( portPOINTER_SIZE_TYPE ) portBYTE_ALIGNMENT_MASK ) ) ); /*lint !e923 !e9033 !e9078 MISRA exception.  Avoiding casts between pointers and integers is not practical.  Size differences accounted for using portPOINTER_SIZE_TYPE type.  Checked by assert(). */
1420
1421             /* Check the alignment of the calculated top of stack is correct. */
1422             configASSERT( ( ( ( portPOINTER_SIZE_TYPE ) pxTopOfStack & ( portPOINTER_SIZE_TYPE ) portBYTE_ALIGNMENT_MASK ) == 0UL ) );
1423
1424             #if ( configRECORD_STACK_HIGH_ADDRESS == 1 )
1425                 {
1426                     /* Also record the stack's high address, which may assist
1427                      * debugging. */
1428                     pxNewTCB->pxEndOfStack = pxTopOfStack;
1429                 }
1430             #endif /* configRECORD_STACK_HIGH_ADDRESS */
1431         }
1432     #else /* portSTACK_GROWTH */
1433         {
1434             pxTopOfStack = pxNewTCB->pxStack;
1435
1436             /* Check the alignment of the stack buffer is correct. */
1437             configASSERT( ( ( ( portPOINTER_SIZE_TYPE ) pxNewTCB->pxStack & ( portPOINTER_SIZE_TYPE ) portBYTE_ALIGNMENT_MASK ) == 0UL ) );
1438
1439             /* The other extreme of the stack space is required if stack checking is
1440              * performed. */
1441             pxNewTCB->pxEndOfStack = pxNewTCB->pxStack + ( ulStackDepth - ( uint32_t ) 1 );
1442         }
1443     #endif /* portSTACK_GROWTH */
1444
1445     /* Store the task name in the TCB. */
1446     if( pcName != NULL )
1447     {
1448         for( x = ( UBaseType_t ) 0; x < ( UBaseType_t ) configMAX_TASK_NAME_LEN; x++ )
1449         {
1450             pxNewTCB->pcTaskName[ x ] = pcName[ x ];
1451
1452             /* Don't copy all configMAX_TASK_NAME_LEN if the string is shorter than
1453              * configMAX_TASK_NAME_LEN characters just in case the memory after the
1454              * string is not accessible (extremely unlikely). */
1455             if( pcName[ x ] == ( char ) 0x00 )
1456             {
1457                 break;
1458             }
1459             else
1460             {
1461                 mtCOVERAGE_TEST_MARKER();
1462             }
1463         }
1464
1465         /* Ensure the name string is terminated in the case that the string length
1466          * was greater or equal to configMAX_TASK_NAME_LEN. */
1467         pxNewTCB->pcTaskName[ configMAX_TASK_NAME_LEN - 1 ] = '\0';
1468     }
1469     else
1470     {
1471         /* The task has not been given a name, so just ensure there is a NULL
1472          * terminator when it is read out. */
1473         pxNewTCB->pcTaskName[ 0 ] = 0x00;
1474     }
1475
1476     /* This is used as an array index so must ensure it's not too large.  First
1477      * remove the privilege bit if one is present. */
1478     if( uxPriority >= ( UBaseType_t ) configMAX_PRIORITIES )
1479     {
1480         uxPriority = ( UBaseType_t ) configMAX_PRIORITIES - ( UBaseType_t ) 1U;
1481     }
1482     else
1483     {
1484         mtCOVERAGE_TEST_MARKER();
1485     }
1486
1487     pxNewTCB->uxPriority = uxPriority;
1488     #if ( configUSE_MUTEXES == 1 )
1489         {
1490             pxNewTCB->uxBasePriority = uxPriority;
1491             pxNewTCB->uxMutexesHeld = 0;
1492         }
1493     #endif /* configUSE_MUTEXES */
1494
1495     vListInitialiseItem( &( pxNewTCB->xStateListItem ) );
1496     vListInitialiseItem( &( pxNewTCB->xEventListItem ) );
1497
1498     /* Set the pxNewTCB as a link back from the ListItem_t.  This is so we can get
1499      * back to  the containing TCB from a generic item in a list. */
1500     listSET_LIST_ITEM_OWNER( &( pxNewTCB->xStateListItem ), pxNewTCB );
1501
1502     /* Event lists are always in priority order. */
1503     listSET_LIST_ITEM_VALUE( &( pxNewTCB->xEventListItem ), ( TickType_t ) configMAX_PRIORITIES - ( TickType_t ) uxPriority ); /*lint !e961 MISRA exception as the casts are only redundant for some ports. */
1504     listSET_LIST_ITEM_OWNER( &( pxNewTCB->xEventListItem ), pxNewTCB );
1505
1506     #if ( portCRITICAL_NESTING_IN_TCB == 1 )
1507         {
1508             pxNewTCB->uxCriticalNesting = ( UBaseType_t ) 0U;
1509         }
1510     #endif /* portCRITICAL_NESTING_IN_TCB */
1511
1512     #if ( configUSE_APPLICATION_TASK_TAG == 1 )
1513         {
1514             pxNewTCB->pxTaskTag = NULL;
1515         }
1516     #endif /* configUSE_APPLICATION_TASK_TAG */
1517
1518     #if ( configGENERATE_RUN_TIME_STATS == 1 )
1519         {
1520             pxNewTCB->ulRunTimeCounter = 0UL;
1521         }
1522     #endif /* configGENERATE_RUN_TIME_STATS */
1523
1524     #if ( portUSING_MPU_WRAPPERS == 1 )
1525         {
1526             vPortStoreTaskMPUSettings( &( pxNewTCB->xMPUSettings ), xRegions, pxNewTCB->pxStack, ulStackDepth );
1527         }
1528     #else
1529         {
1530             /* Avoid compiler warning about unreferenced parameter. */
1531             ( void ) xRegions;
1532         }
1533     #endif
1534
1535     #if ( configNUM_THREAD_LOCAL_STORAGE_POINTERS != 0 )
1536         {
1537             memset( ( void * ) &( pxNewTCB->pvThreadLocalStoragePointers[ 0 ] ), 0x00, sizeof( pxNewTCB->pvThreadLocalStoragePointers ) );
1538         }
1539     #endif
1540
1541     #if ( configUSE_TASK_NOTIFICATIONS == 1 )
1542         {
1543             memset( ( void * ) &( pxNewTCB->ulNotifiedValue[ 0 ] ), 0x00, sizeof( pxNewTCB->ulNotifiedValue ) );
1544             memset( ( void * ) &( pxNewTCB->ucNotifyState[ 0 ] ), 0x00, sizeof( pxNewTCB->ucNotifyState ) );
1545         }
1546     #endif
1547
1548     #if ( configUSE_NEWLIB_REENTRANT == 1 )
1549         {
1550             /* Initialise this task's Newlib reent structure.
1551              * See the third party link http://www.nadler.com/embedded/newlibAndFreeRTOS.html
1552              * for additional information. */
1553             _REENT_INIT_PTR( ( &( pxNewTCB->xNewLib_reent ) ) );
1554         }
1555     #endif
1556
1557     #if ( INCLUDE_xTaskAbortDelay == 1 )
1558         {
1559             pxNewTCB->ucDelayAborted = pdFALSE;
1560         }
1561     #endif
1562
1563     #if ( configNUM_CORES > 1 )
1564         #if ( configUSE_CORE_AFFINITY == 1 )
1565             {
1566                 pxNewTCB->uxCoreAffinityMask = tskNO_AFFINITY;
1567             }
1568         #endif
1569     #endif
1570     #if ( configUSE_TASK_PREEMPTION_DISABLE == 1 )
1571         {
1572             pxNewTCB->xPreemptionDisable = 0;
1573         }
1574     #endif
1575
1576     /* Initialize the TCB stack to look as if the task was already running,
1577      * but had been interrupted by the scheduler.  The return address is set
1578      * to the start of the task function. Once the stack has been initialised
1579      * the top of stack variable is updated. */
1580     #if ( portUSING_MPU_WRAPPERS == 1 )
1581         {
1582             /* If the port has capability to detect stack overflow,
1583              * pass the stack end address to the stack initialization
1584              * function as well. */
1585             #if ( portHAS_STACK_OVERFLOW_CHECKING == 1 )
1586                 {
1587                     #if ( portSTACK_GROWTH < 0 )
1588                         {
1589                             pxNewTCB->pxTopOfStack = pxPortInitialiseStack( pxTopOfStack, pxNewTCB->pxStack, pxTaskCode, pvParameters, xRunPrivileged );
1590                         }
1591                     #else /* portSTACK_GROWTH */
1592                         {
1593                             pxNewTCB->pxTopOfStack = pxPortInitialiseStack( pxTopOfStack, pxNewTCB->pxEndOfStack, pxTaskCode, pvParameters, xRunPrivileged );
1594                         }
1595                     #endif /* portSTACK_GROWTH */
1596                 }
1597             #else /* portHAS_STACK_OVERFLOW_CHECKING */
1598                 {
1599                     pxNewTCB->pxTopOfStack = pxPortInitialiseStack( pxTopOfStack, pxTaskCode, pvParameters, xRunPrivileged );
1600                 }
1601             #endif /* portHAS_STACK_OVERFLOW_CHECKING */
1602         }
1603     #else /* portUSING_MPU_WRAPPERS */
1604         {
1605             /* If the port has capability to detect stack overflow,
1606              * pass the stack end address to the stack initialization
1607              * function as well. */
1608             #if ( portHAS_STACK_OVERFLOW_CHECKING == 1 )
1609                 {
1610                     #if ( portSTACK_GROWTH < 0 )
1611                         {
1612                             pxNewTCB->pxTopOfStack = pxPortInitialiseStack( pxTopOfStack, pxNewTCB->pxStack, pxTaskCode, pvParameters );
1613                         }
1614                     #else /* portSTACK_GROWTH */
1615                         {
1616                             pxNewTCB->pxTopOfStack = pxPortInitialiseStack( pxTopOfStack, pxNewTCB->pxEndOfStack, pxTaskCode, pvParameters );
1617                         }
1618                     #endif /* portSTACK_GROWTH */
1619                 }
1620             #else /* portHAS_STACK_OVERFLOW_CHECKING */
1621                 {
1622                     pxNewTCB->pxTopOfStack = pxPortInitialiseStack( pxTopOfStack, pxTaskCode, pvParameters );
1623                 }
1624             #endif /* portHAS_STACK_OVERFLOW_CHECKING */
1625         }
1626     #endif /* portUSING_MPU_WRAPPERS */
1627
1628     /* Initialize to not running */
1629     pxNewTCB->xTaskRunState = taskTASK_NOT_RUNNING;
1630
1631     /* Is this an idle task? */
1632     if( pxTaskCode == prvIdleTask )
1633     {
1634         pxNewTCB->xIsIdle = pdTRUE;
1635     }
1636
1637     #if ( configNUM_CORES > 1 )
1638         else if( pxTaskCode == prvMinimalIdleTask )
1639         {
1640             pxNewTCB->xIsIdle = pdTRUE;
1641         }
1642     #endif
1643     else
1644     {
1645         pxNewTCB->xIsIdle = pdFALSE;
1646     }
1647
1648     if( pxCreatedTask != NULL )
1649     {
1650         /* Pass the handle out in an anonymous way.  The handle can be used to
1651          * change the created task's priority, delete the created task, etc.*/
1652         *pxCreatedTask = ( TaskHandle_t ) pxNewTCB;
1653     }
1654     else
1655     {
1656         mtCOVERAGE_TEST_MARKER();
1657     }
1658 }
1659 /*-----------------------------------------------------------*/
1660
1661 static void prvAddNewTaskToReadyList( TCB_t * pxNewTCB )
1662 {
1663     /* Ensure interrupts don't access the task lists while the lists are being
1664      * updated. */
1665     taskENTER_CRITICAL();
1666     {
1667         uxCurrentNumberOfTasks++;
1668
1669         if( xSchedulerRunning == pdFALSE )
1670         {
1671             if( uxCurrentNumberOfTasks == ( UBaseType_t ) 1 )
1672             {
1673                 /* This is the first task to be created so do the preliminary
1674                  * initialisation required.  We will not recover if this call
1675                  * fails, but we will report the failure. */
1676                 prvInitialiseTaskLists();
1677             }
1678             else
1679             {
1680                 mtCOVERAGE_TEST_MARKER();
1681             }
1682
1683             if( pxNewTCB->xIsIdle != pdFALSE )
1684             {
1685                 BaseType_t xCoreID;
1686
1687                 /* Check if a core is free. */
1688                 for( xCoreID = ( UBaseType_t ) 0; xCoreID < ( UBaseType_t ) configNUM_CORES; xCoreID++ )
1689                 {
1690                     if( pxCurrentTCBs[ xCoreID ] == NULL )
1691                     {
1692                         pxNewTCB->xTaskRunState = xCoreID;
1693                         pxCurrentTCBs[ xCoreID ] = pxNewTCB;
1694                         break;
1695                     }
1696                 }
1697             }
1698         }
1699         else
1700         {
1701             mtCOVERAGE_TEST_MARKER();
1702         }
1703
1704         uxTaskNumber++;
1705
1706         #if ( configUSE_TRACE_FACILITY == 1 )
1707             {
1708                 /* Add a counter into the TCB for tracing only. */
1709                 pxNewTCB->uxTCBNumber = uxTaskNumber;
1710             }
1711         #endif /* configUSE_TRACE_FACILITY */
1712         traceTASK_CREATE( pxNewTCB );
1713
1714         prvAddTaskToReadyList( pxNewTCB );
1715
1716         portSETUP_TCB( pxNewTCB );
1717
1718         if( xSchedulerRunning != pdFALSE )
1719         {
1720             /* If the created task is of a higher priority than another
1721              * currently running task and preemption is on then it should
1722              * run now. */
1723             #if ( configUSE_PREEMPTION == 1 )
1724                 prvYieldForTask( pxNewTCB, pdFALSE );
1725             #endif
1726         }
1727         else
1728         {
1729             mtCOVERAGE_TEST_MARKER();
1730         }
1731     }
1732     taskEXIT_CRITICAL();
1733 }
1734 /*-----------------------------------------------------------*/
1735
1736 #if ( INCLUDE_vTaskDelete == 1 )
1737
1738     void vTaskDelete( TaskHandle_t xTaskToDelete )
1739     {
1740         TCB_t * pxTCB;
1741         TaskRunning_t xTaskRunningOnCore;
1742
1743         taskENTER_CRITICAL();
1744         {
1745             /* If null is passed in here then it is the calling task that is
1746              * being deleted. */
1747             pxTCB = prvGetTCBFromHandle( xTaskToDelete );
1748
1749             xTaskRunningOnCore = pxTCB->xTaskRunState;
1750
1751             /* Remove task from the ready/delayed list. */
1752             if( uxListRemove( &( pxTCB->xStateListItem ) ) == ( UBaseType_t ) 0 )
1753             {
1754                 taskRESET_READY_PRIORITY( pxTCB->uxPriority );
1755             }
1756             else
1757             {
1758                 mtCOVERAGE_TEST_MARKER();
1759             }
1760
1761             /* Is the task waiting on an event also? */
1762             if( listLIST_ITEM_CONTAINER( &( pxTCB->xEventListItem ) ) != NULL )
1763             {
1764                 ( void ) uxListRemove( &( pxTCB->xEventListItem ) );
1765             }
1766             else
1767             {
1768                 mtCOVERAGE_TEST_MARKER();
1769             }
1770
1771             /* Increment the uxTaskNumber also so kernel aware debuggers can
1772              * detect that the task lists need re-generating.  This is done before
1773              * portPRE_TASK_DELETE_HOOK() as in the Windows port that macro will
1774              * not return. */
1775             uxTaskNumber++;
1776
1777             /* If the task is running (or yielding), we must add it to the
1778              * termination list so that an idle task can delete it when it is
1779              * no longer running. */
1780             if( xTaskRunningOnCore != taskTASK_NOT_RUNNING )
1781             {
1782                 /* A running task is being deleted.  This cannot complete within the
1783                  * task itself, as a context switch to another task is required.
1784                  * Place the task in the termination list.  The idle task will
1785                  * check the termination list and free up any memory allocated by
1786                  * the scheduler for the TCB and stack of the deleted task. */
1787                 vListInsertEnd( &xTasksWaitingTermination, &( pxTCB->xStateListItem ) );
1788
1789                 /* Increment the ucTasksDeleted variable so the idle task knows
1790                  * there is a task that has been deleted and that it should therefore
1791                  * check the xTasksWaitingTermination list. */
1792                 ++uxDeletedTasksWaitingCleanUp;
1793
1794                 /* Call the delete hook before portPRE_TASK_DELETE_HOOK() as
1795                  * portPRE_TASK_DELETE_HOOK() does not return in the Win32 port. */
1796                 traceTASK_DELETE( pxTCB );
1797
1798                 /* The pre-delete hook is primarily for the Windows simulator,
1799                  * in which Windows specific clean up operations are performed,
1800                  * after which it is not possible to yield away from this task -
1801                  * hence xYieldPending is used to latch that a context switch is
1802                  * required. */
1803                 portPRE_TASK_DELETE_HOOK( pxTCB, &xYieldPendings[ pxTCB->xTaskRunState ] );
1804             }
1805             else
1806             {
1807                 --uxCurrentNumberOfTasks;
1808                 traceTASK_DELETE( pxTCB );
1809                 prvDeleteTCB( pxTCB );
1810
1811                 /* Reset the next expected unblock time in case it referred to
1812                  * the task that has just been deleted. */
1813                 prvResetNextTaskUnblockTime();
1814             }
1815
1816             /* Force a reschedule if the task that has just been deleted was running. */
1817             if( ( xSchedulerRunning != pdFALSE ) && ( taskTASK_IS_RUNNING( xTaskRunningOnCore ) ) )
1818             {
1819                 BaseType_t xCoreID;
1820
1821                 xCoreID = portGET_CORE_ID();
1822
1823                 if( xTaskRunningOnCore == xCoreID )
1824                 {
1825                     configASSERT( uxSchedulerSuspended == 0 );
1826                     vTaskYieldWithinAPI();
1827                 }
1828                 else
1829                 {
1830                     prvYieldCore( xTaskRunningOnCore );
1831                 }
1832             }
1833         }
1834         taskEXIT_CRITICAL();
1835     }
1836
1837 #endif /* INCLUDE_vTaskDelete */
1838 /*-----------------------------------------------------------*/
1839
1840 #if ( INCLUDE_xTaskDelayUntil == 1 )
1841
1842     BaseType_t xTaskDelayUntil( TickType_t * const pxPreviousWakeTime,
1843                                 const TickType_t xTimeIncrement )
1844     {
1845         TickType_t xTimeToWake;
1846         BaseType_t xAlreadyYielded, xShouldDelay = pdFALSE;
1847
1848         configASSERT( pxPreviousWakeTime );
1849         configASSERT( ( xTimeIncrement > 0U ) );
1850
1851         vTaskSuspendAll();
1852         {
1853             configASSERT( uxSchedulerSuspended == 1 );
1854
1855             /* Minor optimisation.  The tick count cannot change in this
1856              * block. */
1857             const TickType_t xConstTickCount = xTickCount;
1858
1859             /* Generate the tick time at which the task wants to wake. */
1860             xTimeToWake = *pxPreviousWakeTime + xTimeIncrement;
1861
1862             if( xConstTickCount < *pxPreviousWakeTime )
1863             {
1864                 /* The tick count has overflowed since this function was
1865                  * lasted called.  In this case the only time we should ever
1866                  * actually delay is if the wake time has also  overflowed,
1867                  * and the wake time is greater than the tick time.  When this
1868                  * is the case it is as if neither time had overflowed. */
1869                 if( ( xTimeToWake < *pxPreviousWakeTime ) && ( xTimeToWake > xConstTickCount ) )
1870                 {
1871                     xShouldDelay = pdTRUE;
1872                 }
1873                 else
1874                 {
1875                     mtCOVERAGE_TEST_MARKER();
1876                 }
1877             }
1878             else
1879             {
1880                 /* The tick time has not overflowed.  In this case we will
1881                  * delay if either the wake time has overflowed, and/or the
1882                  * tick time is less than the wake time. */
1883                 if( ( xTimeToWake < *pxPreviousWakeTime ) || ( xTimeToWake > xConstTickCount ) )
1884                 {
1885                     xShouldDelay = pdTRUE;
1886                 }
1887                 else
1888                 {
1889                     mtCOVERAGE_TEST_MARKER();
1890                 }
1891             }
1892
1893             /* Update the wake time ready for the next call. */
1894             *pxPreviousWakeTime = xTimeToWake;
1895
1896             if( xShouldDelay != pdFALSE )
1897             {
1898                 traceTASK_DELAY_UNTIL( xTimeToWake );
1899
1900                 /* prvAddCurrentTaskToDelayedList() needs the block time, not
1901                  * the time to wake, so subtract the current tick count. */
1902                 prvAddCurrentTaskToDelayedList( xTimeToWake - xConstTickCount, pdFALSE );
1903             }
1904             else
1905             {
1906                 mtCOVERAGE_TEST_MARKER();
1907             }
1908         }
1909         xAlreadyYielded = xTaskResumeAll();
1910
1911         /* Force a reschedule if xTaskResumeAll has not already done so, we may
1912          * have put ourselves to sleep. */
1913         if( xAlreadyYielded == pdFALSE )
1914         {
1915             vTaskYieldWithinAPI();
1916         }
1917         else
1918         {
1919             mtCOVERAGE_TEST_MARKER();
1920         }
1921
1922         return xShouldDelay;
1923     }
1924
1925 #endif /* INCLUDE_xTaskDelayUntil */
1926 /*-----------------------------------------------------------*/
1927
1928 #if ( INCLUDE_vTaskDelay == 1 )
1929
1930     void vTaskDelay( const TickType_t xTicksToDelay )
1931     {
1932         BaseType_t xAlreadyYielded = pdFALSE;
1933
1934         /* A delay time of zero just forces a reschedule. */
1935         if( xTicksToDelay > ( TickType_t ) 0U )
1936         {
1937             vTaskSuspendAll();
1938             {
1939                 configASSERT( uxSchedulerSuspended == 1 );
1940                 traceTASK_DELAY();
1941
1942                 /* A task that is removed from the event list while the
1943                  * scheduler is suspended will not get placed in the ready
1944                  * list or removed from the blocked list until the scheduler
1945                  * is resumed.
1946                  *
1947                  * This task cannot be in an event list as it is the currently
1948                  * executing task. */
1949                 prvAddCurrentTaskToDelayedList( xTicksToDelay, pdFALSE );
1950             }
1951             xAlreadyYielded = xTaskResumeAll();
1952         }
1953         else
1954         {
1955             mtCOVERAGE_TEST_MARKER();
1956         }
1957
1958         /* Force a reschedule if xTaskResumeAll has not already done so, we may
1959          * have put ourselves to sleep. */
1960         if( xAlreadyYielded == pdFALSE )
1961         {
1962             vTaskYieldWithinAPI();
1963         }
1964         else
1965         {
1966             mtCOVERAGE_TEST_MARKER();
1967         }
1968     }
1969
1970 #endif /* INCLUDE_vTaskDelay */
1971 /*-----------------------------------------------------------*/
1972
1973 #if ( ( INCLUDE_eTaskGetState == 1 ) || ( configUSE_TRACE_FACILITY == 1 ) || ( INCLUDE_xTaskAbortDelay == 1 ) )
1974
1975     eTaskState eTaskGetState( TaskHandle_t xTask )
1976     {
1977         eTaskState eReturn;
1978         List_t const * pxStateList, * pxDelayedList, * pxOverflowedDelayedList;
1979         const TCB_t * const pxTCB = xTask;
1980
1981         configASSERT( pxTCB );
1982
1983         taskENTER_CRITICAL();
1984         {
1985             pxStateList = listLIST_ITEM_CONTAINER( &( pxTCB->xStateListItem ) );
1986             pxDelayedList = pxDelayedTaskList;
1987             pxOverflowedDelayedList = pxOverflowDelayedTaskList;
1988         }
1989         taskEXIT_CRITICAL();
1990
1991         if( ( pxStateList == pxDelayedList ) || ( pxStateList == pxOverflowedDelayedList ) )
1992         {
1993             /* The task being queried is referenced from one of the Blocked
1994              * lists. */
1995             eReturn = eBlocked;
1996         }
1997
1998         #if ( INCLUDE_vTaskSuspend == 1 )
1999             else if( pxStateList == &xSuspendedTaskList )
2000             {
2001                 /* The task being queried is referenced from the suspended
2002                  * list.  Is it genuinely suspended or is it blocked
2003                  * indefinitely? */
2004                 if( listLIST_ITEM_CONTAINER( &( pxTCB->xEventListItem ) ) == NULL )
2005                 {
2006                     #if ( configUSE_TASK_NOTIFICATIONS == 1 )
2007                         {
2008                             BaseType_t x;
2009
2010                             /* The task does not appear on the event list item of
2011                              * and of the RTOS objects, but could still be in the
2012                              * blocked state if it is waiting on its notification
2013                              * rather than waiting on an object.  If not, is
2014                              * suspended. */
2015                             eReturn = eSuspended;
2016
2017                             for( x = 0; x < configTASK_NOTIFICATION_ARRAY_ENTRIES; x++ )
2018                             {
2019                                 if( pxTCB->ucNotifyState[ x ] == taskWAITING_NOTIFICATION )
2020                                 {
2021                                     eReturn = eBlocked;
2022                                     break;
2023                                 }
2024                             }
2025                         }
2026                     #else /* if ( configUSE_TASK_NOTIFICATIONS == 1 ) */
2027                         {
2028                             eReturn = eSuspended;
2029                         }
2030                     #endif /* if ( configUSE_TASK_NOTIFICATIONS == 1 ) */
2031                 }
2032                 else
2033                 {
2034                     eReturn = eBlocked;
2035                 }
2036             }
2037         #endif /* if ( INCLUDE_vTaskSuspend == 1 ) */
2038
2039         #if ( INCLUDE_vTaskDelete == 1 )
2040             else if( ( pxStateList == &xTasksWaitingTermination ) || ( pxStateList == NULL ) )
2041             {
2042                 /* The task being queried is referenced from the deleted
2043                  * tasks list, or it is not referenced from any lists at
2044                  * all. */
2045                 eReturn = eDeleted;
2046             }
2047         #endif
2048
2049         else /*lint !e525 Negative indentation is intended to make use of pre-processor clearer. */
2050         {
2051             /* If the task is not in any other state, it must be in the
2052              * Ready (including pending ready) state. */
2053             if( taskTASK_IS_RUNNING( pxTCB->xTaskRunState ) )
2054             {
2055                 /* Is it actively running on a core? */
2056                 eReturn = eRunning;
2057             }
2058             else
2059             {
2060                 eReturn = eReady;
2061             }
2062         }
2063
2064         return eReturn;
2065     } /*lint !e818 xTask cannot be a pointer to const because it is a typedef. */
2066
2067 #endif /* INCLUDE_eTaskGetState */
2068 /*-----------------------------------------------------------*/
2069
2070 #if ( INCLUDE_uxTaskPriorityGet == 1 )
2071
2072     UBaseType_t uxTaskPriorityGet( const TaskHandle_t xTask )
2073     {
2074         TCB_t const * pxTCB;
2075         UBaseType_t uxReturn;
2076
2077         taskENTER_CRITICAL();
2078         {
2079             /* If null is passed in here then it is the priority of the task
2080              * that called uxTaskPriorityGet() that is being queried. */
2081             pxTCB = prvGetTCBFromHandle( xTask );
2082             uxReturn = pxTCB->uxPriority;
2083         }
2084         taskEXIT_CRITICAL();
2085
2086         return uxReturn;
2087     }
2088
2089 #endif /* INCLUDE_uxTaskPriorityGet */
2090 /*-----------------------------------------------------------*/
2091
2092 #if ( INCLUDE_uxTaskPriorityGet == 1 )
2093
2094     UBaseType_t uxTaskPriorityGetFromISR( const TaskHandle_t xTask )
2095     {
2096         TCB_t const * pxTCB;
2097         UBaseType_t uxReturn, uxSavedInterruptState;
2098
2099         /* RTOS ports that support interrupt nesting have the concept of a
2100          * maximum  system call (or maximum API call) interrupt priority.
2101          * Interrupts that are  above the maximum system call priority are keep
2102          * permanently enabled, even when the RTOS kernel is in a critical section,
2103          * but cannot make any calls to FreeRTOS API functions.  If configASSERT()
2104          * is defined in FreeRTOSConfig.h then
2105          * portASSERT_IF_INTERRUPT_PRIORITY_INVALID() will result in an assertion
2106          * failure if a FreeRTOS API function is called from an interrupt that has
2107          * been assigned a priority above the configured maximum system call
2108          * priority.  Only FreeRTOS functions that end in FromISR can be called
2109          * from interrupts  that have been assigned a priority at or (logically)
2110          * below the maximum system call interrupt priority.  FreeRTOS maintains a
2111          * separate interrupt safe API to ensure interrupt entry is as fast and as
2112          * simple as possible.  More information (albeit Cortex-M specific) is
2113          * provided on the following link:
2114          * https://www.FreeRTOS.org/RTOS-Cortex-M3-M4.html */
2115         portASSERT_IF_INTERRUPT_PRIORITY_INVALID();
2116
2117         uxSavedInterruptState = portSET_INTERRUPT_MASK_FROM_ISR();
2118         {
2119             /* If null is passed in here then it is the priority of the calling
2120              * task that is being queried. */
2121             pxTCB = prvGetTCBFromHandle( xTask );
2122             uxReturn = pxTCB->uxPriority;
2123         }
2124         portCLEAR_INTERRUPT_MASK_FROM_ISR( uxSavedInterruptState );
2125
2126         return uxReturn;
2127     }
2128
2129 #endif /* INCLUDE_uxTaskPriorityGet */
2130 /*-----------------------------------------------------------*/
2131
2132 #if ( INCLUDE_vTaskPrioritySet == 1 )
2133
2134     void vTaskPrioritySet( TaskHandle_t xTask,
2135                            UBaseType_t uxNewPriority )
2136     {
2137         TCB_t * pxTCB;
2138         UBaseType_t uxCurrentBasePriority, uxPriorityUsedOnEntry;
2139         BaseType_t xYieldRequired = pdFALSE;
2140         BaseType_t xYieldForTask = pdFALSE;
2141         BaseType_t xCoreID;
2142
2143         configASSERT( ( uxNewPriority < configMAX_PRIORITIES ) );
2144
2145         /* Ensure the new priority is valid. */
2146         if( uxNewPriority >= ( UBaseType_t ) configMAX_PRIORITIES )
2147         {
2148             uxNewPriority = ( UBaseType_t ) configMAX_PRIORITIES - ( UBaseType_t ) 1U;
2149         }
2150         else
2151         {
2152             mtCOVERAGE_TEST_MARKER();
2153         }
2154
2155         taskENTER_CRITICAL();
2156         {
2157             /* If null is passed in here then it is the priority of the calling
2158              * task that is being changed. */
2159             pxTCB = prvGetTCBFromHandle( xTask );
2160
2161             traceTASK_PRIORITY_SET( pxTCB, uxNewPriority );
2162
2163             #if ( configUSE_MUTEXES == 1 )
2164                 {
2165                     uxCurrentBasePriority = pxTCB->uxBasePriority;
2166                 }
2167             #else
2168                 {
2169                     uxCurrentBasePriority = pxTCB->uxPriority;
2170                 }
2171             #endif
2172
2173             if( uxCurrentBasePriority != uxNewPriority )
2174             {
2175                 /* The priority change may have readied a task of higher
2176                  * priority than a running task. */
2177                 if( uxNewPriority > uxCurrentBasePriority )
2178                 {
2179                     /* The priority of a task is being raised so
2180                      * perform a yield for this task later. */
2181                     xYieldForTask = pdTRUE;
2182                 }
2183                 else if( taskTASK_IS_RUNNING( pxTCB->xTaskRunState ) )
2184                 {
2185                     /* Setting the priority of a running task down means
2186                      * there may now be another task of higher priority that
2187                      * is ready to execute. */
2188                     #if ( configUSE_TASK_PREEMPTION_DISABLE == 1 )
2189                         if( pxTCB->xPreemptionDisable == pdFALSE )
2190                     #endif
2191                     {
2192                         xCoreID = ( BaseType_t ) pxTCB->xTaskRunState;
2193                         xYieldRequired = pdTRUE;
2194                     }
2195                 }
2196                 else
2197                 {
2198                     /* Setting the priority of any other task down does not
2199                      * require a yield as the running task must be above the
2200                      * new priority of the task being modified. */
2201                 }
2202
2203                 /* Remember the ready list the task might be referenced from
2204                  * before its uxPriority member is changed so the
2205                  * taskRESET_READY_PRIORITY() macro can function correctly. */
2206                 uxPriorityUsedOnEntry = pxTCB->uxPriority;
2207
2208                 #if ( configUSE_MUTEXES == 1 )
2209                     {
2210                         /* Only change the priority being used if the task is not
2211                          * currently using an inherited priority. */
2212                         if( pxTCB->uxBasePriority == pxTCB->uxPriority )
2213                         {
2214                             pxTCB->uxPriority = uxNewPriority;
2215                         }
2216                         else
2217                         {
2218                             mtCOVERAGE_TEST_MARKER();
2219                         }
2220
2221                         /* The base priority gets set whatever. */
2222                         pxTCB->uxBasePriority = uxNewPriority;
2223                     }
2224                 #else /* if ( configUSE_MUTEXES == 1 ) */
2225                     {
2226                         pxTCB->uxPriority = uxNewPriority;
2227                     }
2228                 #endif /* if ( configUSE_MUTEXES == 1 ) */
2229
2230                 /* Only reset the event list item value if the value is not
2231                  * being used for anything else. */
2232                 if( ( listGET_LIST_ITEM_VALUE( &( pxTCB->xEventListItem ) ) & taskEVENT_LIST_ITEM_VALUE_IN_USE ) == 0UL )
2233                 {
2234                     listSET_LIST_ITEM_VALUE( &( pxTCB->xEventListItem ), ( ( TickType_t ) configMAX_PRIORITIES - ( TickType_t ) uxNewPriority ) ); /*lint !e961 MISRA exception as the casts are only redundant for some ports. */
2235                 }
2236                 else
2237                 {
2238                     mtCOVERAGE_TEST_MARKER();
2239                 }
2240
2241                 /* If the task is in the blocked or suspended list we need do
2242                  * nothing more than change its priority variable. However, if
2243                  * the task is in a ready list it needs to be removed and placed
2244                  * in the list appropriate to its new priority. */
2245                 if( listIS_CONTAINED_WITHIN( &( pxReadyTasksLists[ uxPriorityUsedOnEntry ] ), &( pxTCB->xStateListItem ) ) != pdFALSE )
2246                 {
2247                     /* The task is currently in its ready list - remove before
2248                      * adding it to its new ready list.  As we are in a critical
2249                      * section we can do this even if the scheduler is suspended. */
2250                     if( uxListRemove( &( pxTCB->xStateListItem ) ) == ( UBaseType_t ) 0 )
2251                     {
2252                         /* It is known that the task is in its ready list so
2253                          * there is no need to check again and the port level
2254                          * reset macro can be called directly. */
2255                         portRESET_READY_PRIORITY( uxPriorityUsedOnEntry, uxTopReadyPriority );
2256                     }
2257                     else
2258                     {
2259                         mtCOVERAGE_TEST_MARKER();
2260                     }
2261
2262                     prvAddTaskToReadyList( pxTCB );
2263                 }
2264                 else
2265                 {
2266                     /* It's possible that xYieldForTask was already set to pdTRUE because
2267                      * its priority is being raised. However, since it is not in a ready list
2268                      * we don't actually need to yield for it. */
2269                     xYieldForTask = pdFALSE;
2270                 }
2271
2272                 #if ( configUSE_PREEMPTION == 1 )
2273                     if( xYieldRequired != pdFALSE )
2274                     {
2275                         prvYieldCore( xCoreID );
2276                     }
2277                     else if( xYieldForTask != pdFALSE )
2278                     {
2279                         prvYieldForTask( pxTCB, pdTRUE );
2280                     }
2281                     else
2282                     {
2283                         mtCOVERAGE_TEST_MARKER();
2284                     }
2285                 #endif /* if ( configUSE_PREEMPTION == 1 ) */
2286
2287                 /* Remove compiler warning about unused variables when the port
2288                  * optimised task selection is not being used. */
2289                 ( void ) uxPriorityUsedOnEntry;
2290             }
2291         }
2292         taskEXIT_CRITICAL();
2293     }
2294
2295 #endif /* INCLUDE_vTaskPrioritySet */
2296 /*-----------------------------------------------------------*/
2297
2298 #if ( configNUM_CORES > 1 )
2299     #if ( configUSE_CORE_AFFINITY == 1 )
2300
2301         void vTaskCoreAffinitySet( const TaskHandle_t xTask,
2302                                    UBaseType_t uxCoreAffinityMask )
2303         {
2304             TCB_t * pxTCB;
2305             BaseType_t xCoreID;
2306
2307             taskENTER_CRITICAL();
2308             {
2309                 pxTCB = prvGetTCBFromHandle( xTask );
2310
2311                 pxTCB->uxCoreAffinityMask = uxCoreAffinityMask;
2312
2313                 if( xSchedulerRunning != pdFALSE )
2314                 {
2315                     if( taskTASK_IS_RUNNING( pxTCB->xTaskRunState ) )
2316                     {
2317                         xCoreID = ( BaseType_t ) pxTCB->xTaskRunState;
2318
2319                         if( ( uxCoreAffinityMask & ( 1 << xCoreID ) ) == 0 )
2320                         {
2321                             prvYieldCore( xCoreID );
2322                         }
2323                     }
2324                 }
2325             }
2326             taskEXIT_CRITICAL();
2327         }
2328
2329     #endif /* configUSE_CORE_AFFINITY */
2330 #endif /* if ( configNUM_CORES > 1 ) */
2331 /*-----------------------------------------------------------*/
2332
2333 #if ( configNUM_CORES > 1 )
2334     #if ( configUSE_CORE_AFFINITY == 1 )
2335
2336         UBaseType_t vTaskCoreAffinityGet( const TaskHandle_t xTask )
2337         {
2338             TCB_t * pxTCB;
2339             UBaseType_t uxCoreAffinityMask;
2340
2341             taskENTER_CRITICAL();
2342             {
2343                 pxTCB = prvGetTCBFromHandle( xTask );
2344                 uxCoreAffinityMask = pxTCB->uxCoreAffinityMask;
2345             }
2346             taskEXIT_CRITICAL();
2347
2348             return uxCoreAffinityMask;
2349         }
2350
2351     #endif /* configUSE_CORE_AFFINITY */
2352 #endif /* if ( configNUM_CORES > 1 ) */
2353
2354 /*-----------------------------------------------------------*/
2355
2356 #if ( configUSE_TASK_PREEMPTION_DISABLE == 1 )
2357
2358     void vTaskPreemptionDisable( const TaskHandle_t xTask )
2359     {
2360         TCB_t * pxTCB;
2361
2362         taskENTER_CRITICAL();
2363         {
2364             pxTCB = prvGetTCBFromHandle( xTask );
2365
2366             pxTCB->xPreemptionDisable = pdTRUE;
2367         }
2368         taskEXIT_CRITICAL();
2369     }
2370
2371 #endif /* configUSE_TASK_PREEMPTION_DISABLE */
2372 /*-----------------------------------------------------------*/
2373
2374 #if ( configUSE_TASK_PREEMPTION_DISABLE == 1 )
2375
2376     void vTaskPreemptionEnable( const TaskHandle_t xTask )
2377     {
2378         TCB_t * pxTCB;
2379         BaseType_t xCoreID;
2380
2381         taskENTER_CRITICAL();
2382         {
2383             pxTCB = prvGetTCBFromHandle( xTask );
2384
2385             pxTCB->xPreemptionDisable = pdFALSE;
2386
2387             if( xSchedulerRunning != pdFALSE )
2388             {
2389                 if( taskTASK_IS_RUNNING( pxTCB->xTaskRunState ) )
2390                 {
2391                     xCoreID = ( BaseType_t ) pxTCB->xTaskRunState;
2392                     prvYieldCore( xCoreID );
2393                 }
2394             }
2395         }
2396         taskEXIT_CRITICAL();
2397     }
2398
2399 #endif /* configUSE_TASK_PREEMPTION_DISABLE */
2400 /*-----------------------------------------------------------*/
2401
2402 #if ( INCLUDE_vTaskSuspend == 1 )
2403
2404     void vTaskSuspend( TaskHandle_t xTaskToSuspend )
2405     {
2406         TCB_t * pxTCB;
2407         TaskRunning_t xTaskRunningOnCore;
2408
2409         taskENTER_CRITICAL();
2410         {
2411             /* If null is passed in here then it is the running task that is
2412              * being suspended. */
2413             pxTCB = prvGetTCBFromHandle( xTaskToSuspend );
2414
2415             traceTASK_SUSPEND( pxTCB );
2416
2417             xTaskRunningOnCore = pxTCB->xTaskRunState;
2418
2419             /* Remove task from the ready/delayed list and place in the
2420              * suspended list. */
2421             if( uxListRemove( &( pxTCB->xStateListItem ) ) == ( UBaseType_t ) 0 )
2422             {
2423                 taskRESET_READY_PRIORITY( pxTCB->uxPriority );
2424             }
2425             else
2426             {
2427                 mtCOVERAGE_TEST_MARKER();
2428             }
2429
2430             /* Is the task waiting on an event also? */
2431             if( listLIST_ITEM_CONTAINER( &( pxTCB->xEventListItem ) ) != NULL )
2432             {
2433                 ( void ) uxListRemove( &( pxTCB->xEventListItem ) );
2434             }
2435             else
2436             {
2437                 mtCOVERAGE_TEST_MARKER();
2438             }
2439
2440             vListInsertEnd( &xSuspendedTaskList, &( pxTCB->xStateListItem ) );
2441
2442             #if ( configUSE_TASK_NOTIFICATIONS == 1 )
2443                 {
2444                     BaseType_t x;
2445
2446                     for( x = 0; x < configTASK_NOTIFICATION_ARRAY_ENTRIES; x++ )
2447                     {
2448                         if( pxTCB->ucNotifyState[ x ] == taskWAITING_NOTIFICATION )
2449                         {
2450                             /* The task was blocked to wait for a notification, but is
2451                              * now suspended, so no notification was received. */
2452                             pxTCB->ucNotifyState[ x ] = taskNOT_WAITING_NOTIFICATION;
2453                         }
2454                     }
2455                 }
2456             #endif /* if ( configUSE_TASK_NOTIFICATIONS == 1 ) */
2457
2458             if( xSchedulerRunning != pdFALSE )
2459             {
2460                 /* Reset the next expected unblock time in case it referred to the
2461                  * task that is now in the Suspended state. */
2462                 prvResetNextTaskUnblockTime();
2463             }
2464             else
2465             {
2466                 mtCOVERAGE_TEST_MARKER();
2467             }
2468
2469             if( taskTASK_IS_RUNNING( xTaskRunningOnCore ) )
2470             {
2471                 if( xSchedulerRunning != pdFALSE )
2472                 {
2473                     if( xTaskRunningOnCore == portGET_CORE_ID() )
2474                     {
2475                         /* The current task has just been suspended. */
2476                         configASSERT( uxSchedulerSuspended == 0 );
2477                         vTaskYieldWithinAPI();
2478                     }
2479                     else
2480                     {
2481                         prvYieldCore( xTaskRunningOnCore );
2482                     }
2483
2484                     taskEXIT_CRITICAL();
2485                 }
2486                 else
2487                 {
2488                     taskEXIT_CRITICAL();
2489
2490                     configASSERT( pxTCB == pxCurrentTCBs[ xTaskRunningOnCore ] );
2491
2492                     /* The scheduler is not running, but the task that was pointed
2493                      * to by pxCurrentTCB has just been suspended and pxCurrentTCB
2494                      * must be adjusted to point to a different task. */
2495                     if( listCURRENT_LIST_LENGTH( &xSuspendedTaskList ) == uxCurrentNumberOfTasks ) /*lint !e931 Right has no side effect, just volatile. */
2496                     {
2497                         /* No other tasks are ready, so set the core's TCB back to
2498                          * NULL so when the next task is created the core's TCB will
2499                          * be able to be set to point to it no matter what its relative
2500                          * priority is. */
2501                         pxTCB->xTaskRunState = taskTASK_NOT_RUNNING;
2502                         pxCurrentTCBs[ xTaskRunningOnCore ] = NULL;
2503                     }
2504                     else
2505                     {
2506                         /* Attempt to switch in a new task. This could fail since the idle tasks
2507                          * haven't been created yet. If it does then set the core's TCB back to
2508                          * NULL. */
2509                         if( prvSelectHighestPriorityTask( xTaskRunningOnCore ) == pdFALSE )
2510                         {
2511                             pxTCB->xTaskRunState = taskTASK_NOT_RUNNING;
2512                             pxCurrentTCBs[ xTaskRunningOnCore ] = NULL;
2513                         }
2514                     }
2515                 }
2516             }
2517             else
2518             {
2519                 taskEXIT_CRITICAL();
2520             }
2521         } /* taskEXIT_CRITICAL() - already exited in one of three cases above */
2522     }
2523
2524 #endif /* INCLUDE_vTaskSuspend */
2525 /*-----------------------------------------------------------*/
2526
2527 #if ( INCLUDE_vTaskSuspend == 1 )
2528
2529     static BaseType_t prvTaskIsTaskSuspended( const TaskHandle_t xTask )
2530     {
2531         BaseType_t xReturn = pdFALSE;
2532         const TCB_t * const pxTCB = xTask;
2533
2534         /* Accesses xPendingReadyList so must be called from a critical section. */
2535
2536         /* It does not make sense to check if the calling task is suspended. */
2537         configASSERT( xTask );
2538
2539         /* Is the task being resumed actually in the suspended list? */
2540         if( listIS_CONTAINED_WITHIN( &xSuspendedTaskList, &( pxTCB->xStateListItem ) ) != pdFALSE )
2541         {
2542             /* Has the task already been resumed from within an ISR? */
2543             if( listIS_CONTAINED_WITHIN( &xPendingReadyList, &( pxTCB->xEventListItem ) ) == pdFALSE )
2544             {
2545                 /* Is it in the suspended list because it is in the Suspended
2546                  * state, or because is is blocked with no timeout? */
2547                 if( listIS_CONTAINED_WITHIN( NULL, &( pxTCB->xEventListItem ) ) != pdFALSE ) /*lint !e961.  The cast is only redundant when NULL is used. */
2548                 {
2549                     xReturn = pdTRUE;
2550                 }
2551                 else
2552                 {
2553                     mtCOVERAGE_TEST_MARKER();
2554                 }
2555             }
2556             else
2557             {
2558                 mtCOVERAGE_TEST_MARKER();
2559             }
2560         }
2561         else
2562         {
2563             mtCOVERAGE_TEST_MARKER();
2564         }
2565
2566         return xReturn;
2567     } /*lint !e818 xTask cannot be a pointer to const because it is a typedef. */
2568
2569 #endif /* INCLUDE_vTaskSuspend */
2570 /*-----------------------------------------------------------*/
2571
2572 #if ( INCLUDE_vTaskSuspend == 1 )
2573
2574     void vTaskResume( TaskHandle_t xTaskToResume )
2575     {
2576         TCB_t * const pxTCB = xTaskToResume;
2577
2578         /* It does not make sense to resume the calling task. */
2579         configASSERT( xTaskToResume );
2580
2581         /* The parameter cannot be NULL as it is impossible to resume the
2582          * currently executing task. It is also impossible to resume a task
2583          * that is actively running on another core but it is too dangerous
2584          * to check their run state here. Safer to get into a critical section
2585          * and check if it is actually suspended or not below. */
2586         if( pxTCB != NULL )
2587         {
2588             taskENTER_CRITICAL();
2589             {
2590                 if( prvTaskIsTaskSuspended( pxTCB ) != pdFALSE )
2591                 {
2592                     traceTASK_RESUME( pxTCB );
2593
2594                     /* The ready list can be accessed even if the scheduler is
2595                      * suspended because this is inside a critical section. */
2596                     ( void ) uxListRemove( &( pxTCB->xStateListItem ) );
2597                     prvAddTaskToReadyList( pxTCB );
2598
2599                     /* A higher priority task may have just been resumed. */
2600                     #if ( configUSE_PREEMPTION == 1 )
2601                         {
2602                             prvYieldForTask( pxTCB, pdTRUE );
2603                         }
2604                     #endif
2605                 }
2606                 else
2607                 {
2608                     mtCOVERAGE_TEST_MARKER();
2609                 }
2610             }
2611             taskEXIT_CRITICAL();
2612         }
2613         else
2614         {
2615             mtCOVERAGE_TEST_MARKER();
2616         }
2617     }
2618
2619 #endif /* INCLUDE_vTaskSuspend */
2620
2621 /*-----------------------------------------------------------*/
2622
2623 #if ( ( INCLUDE_xTaskResumeFromISR == 1 ) && ( INCLUDE_vTaskSuspend == 1 ) )
2624
2625     BaseType_t xTaskResumeFromISR( TaskHandle_t xTaskToResume )
2626     {
2627         BaseType_t xYieldRequired = pdFALSE;
2628         TCB_t * const pxTCB = xTaskToResume;
2629         UBaseType_t uxSavedInterruptStatus;
2630
2631         configASSERT( xTaskToResume );
2632
2633         /* RTOS ports that support interrupt nesting have the concept of a
2634          * maximum  system call (or maximum API call) interrupt priority.
2635          * Interrupts that are  above the maximum system call priority are keep
2636          * permanently enabled, even when the RTOS kernel is in a critical section,
2637          * but cannot make any calls to FreeRTOS API functions.  If configASSERT()
2638          * is defined in FreeRTOSConfig.h then
2639          * portASSERT_IF_INTERRUPT_PRIORITY_INVALID() will result in an assertion
2640          * failure if a FreeRTOS API function is called from an interrupt that has
2641          * been assigned a priority above the configured maximum system call
2642          * priority.  Only FreeRTOS functions that end in FromISR can be called
2643          * from interrupts  that have been assigned a priority at or (logically)
2644          * below the maximum system call interrupt priority.  FreeRTOS maintains a
2645          * separate interrupt safe API to ensure interrupt entry is as fast and as
2646          * simple as possible.  More information (albeit Cortex-M specific) is
2647          * provided on the following link:
2648          * https://www.FreeRTOS.org/RTOS-Cortex-M3-M4.html */
2649         portASSERT_IF_INTERRUPT_PRIORITY_INVALID();
2650
2651         uxSavedInterruptStatus = portSET_INTERRUPT_MASK_FROM_ISR();
2652         {
2653             if( prvTaskIsTaskSuspended( pxTCB ) != pdFALSE )
2654             {
2655                 traceTASK_RESUME_FROM_ISR( pxTCB );
2656
2657                 /* Check the ready lists can be accessed. */
2658                 if( uxSchedulerSuspended == ( UBaseType_t ) pdFALSE )
2659                 {
2660                     /* Ready lists can be accessed so move the task from the
2661                      * suspended list to the ready list directly. */
2662
2663                     ( void ) uxListRemove( &( pxTCB->xStateListItem ) );
2664                     prvAddTaskToReadyList( pxTCB );
2665                 }
2666                 else
2667                 {
2668                     /* The delayed or ready lists cannot be accessed so the task
2669                      * is held in the pending ready list until the scheduler is
2670                      * unsuspended. */
2671                     vListInsertEnd( &( xPendingReadyList ), &( pxTCB->xEventListItem ) );
2672                 }
2673
2674                 #if ( configUSE_PREEMPTION == 1 )
2675                     prvYieldForTask( pxTCB, pdTRUE );
2676
2677                     if( xYieldPendings[ portGET_CORE_ID() ] != pdFALSE )
2678                     {
2679                         xYieldRequired = pdTRUE;
2680                     }
2681                 #endif
2682             }
2683             else
2684             {
2685                 mtCOVERAGE_TEST_MARKER();
2686             }
2687         }
2688         portCLEAR_INTERRUPT_MASK_FROM_ISR( uxSavedInterruptStatus );
2689
2690         return xYieldRequired;
2691     }
2692
2693 #endif /* ( ( INCLUDE_xTaskResumeFromISR == 1 ) && ( INCLUDE_vTaskSuspend == 1 ) ) */
2694 /*-----------------------------------------------------------*/
2695
2696 static BaseType_t prvCreateIdleTasks( void )
2697 {
2698     BaseType_t xReturn = pdPASS;
2699     BaseType_t xCoreID;
2700     char cIdleName[ configMAX_TASK_NAME_LEN ];
2701
2702     /* Add each idle task at the lowest priority. */
2703     for( xCoreID = ( BaseType_t ) 0; xCoreID < ( BaseType_t ) configNUM_CORES; xCoreID++ )
2704     {
2705         BaseType_t x;
2706
2707         if( xReturn == pdFAIL )
2708         {
2709             break;
2710         }
2711         else
2712         {
2713             mtCOVERAGE_TEST_MARKER();
2714         }
2715
2716         for( x = ( BaseType_t ) 0; x < ( BaseType_t ) configMAX_TASK_NAME_LEN; x++ )
2717         {
2718             cIdleName[ x ] = configIDLE_TASK_NAME[ x ];
2719
2720             /* Don't copy all configMAX_TASK_NAME_LEN if the string is shorter than
2721              * configMAX_TASK_NAME_LEN characters just in case the memory after the
2722              * string is not accessible (extremely unlikely). */
2723             if( cIdleName[ x ] == ( char ) 0x00 )
2724             {
2725                 break;
2726             }
2727             else
2728             {
2729                 mtCOVERAGE_TEST_MARKER();
2730             }
2731         }
2732
2733         /* Append the idle task number to the end of the name if there is space */
2734         if( x < configMAX_TASK_NAME_LEN )
2735         {
2736             cIdleName[ x++ ] = xCoreID + '0';
2737
2738             /* And append a null character if there is space */
2739             if( x < configMAX_TASK_NAME_LEN )
2740             {
2741                 cIdleName[ x ] = '\0';
2742             }
2743             else
2744             {
2745                 mtCOVERAGE_TEST_MARKER();
2746             }
2747         }
2748         else
2749         {
2750             mtCOVERAGE_TEST_MARKER();
2751         }
2752
2753         #if ( configSUPPORT_STATIC_ALLOCATION == 1 )
2754             {
2755                 if( xCoreID == 0 )
2756                 {
2757                     StaticTask_t * pxIdleTaskTCBBuffer = NULL;
2758                     StackType_t * pxIdleTaskStackBuffer = NULL;
2759                     uint32_t ulIdleTaskStackSize;
2760
2761                     /* The Idle task is created using user provided RAM - obtain the
2762                      * address of the RAM then create the idle task. */
2763                     vApplicationGetIdleTaskMemory( &pxIdleTaskTCBBuffer, &pxIdleTaskStackBuffer, &ulIdleTaskStackSize );
2764                     xIdleTaskHandle[ xCoreID ] = xTaskCreateStatic( prvIdleTask,
2765                                                                     cIdleName,
2766                                                                     ulIdleTaskStackSize,
2767                                                                     ( void * ) NULL,       /*lint !e961.  The cast is not redundant for all compilers. */
2768                                                                     portPRIVILEGE_BIT,     /* In effect ( tskIDLE_PRIORITY | portPRIVILEGE_BIT ), but tskIDLE_PRIORITY is zero. */
2769                                                                     pxIdleTaskStackBuffer,
2770                                                                     pxIdleTaskTCBBuffer ); /*lint !e961 MISRA exception, justified as it is not a redundant explicit cast to all supported compilers. */
2771                 }
2772
2773                 #if ( configNUM_CORES > 1 )
2774                     else
2775                     {
2776                         static StaticTask_t xIdleTCBBuffers[ configNUM_CORES - 1 ];
2777                         static StackType_t xIdleTaskStackBuffers[ configNUM_CORES - 1 ][ configMINIMAL_STACK_SIZE ];
2778
2779                         xIdleTaskHandle[ xCoreID ] = xTaskCreateStatic( prvMinimalIdleTask,
2780                                                                         cIdleName,
2781                                                                         configMINIMAL_STACK_SIZE,
2782                                                                         ( void * ) NULL,                   /*lint !e961.  The cast is not redundant for all compilers. */
2783                                                                         portPRIVILEGE_BIT,                 /* In effect ( tskIDLE_PRIORITY | portPRIVILEGE_BIT ), but tskIDLE_PRIORITY is zero. */
2784                                                                         xIdleTaskStackBuffers[ xCoreID - 1 ],
2785                                                                         &xIdleTCBBuffers[ xCoreID - 1 ] ); /*lint !e961 MISRA exception, justified as it is not a redundant explicit cast to all supported compilers. */
2786                     }
2787                 #endif /* if ( configNUM_CORES > 1 ) */
2788
2789                 if( xIdleTaskHandle[ xCoreID ] != NULL )
2790                 {
2791                     xReturn = pdPASS;
2792                 }
2793                 else
2794                 {
2795                     xReturn = pdFAIL;
2796                 }
2797             }
2798         #else /* if ( configSUPPORT_STATIC_ALLOCATION == 1 ) */
2799             {
2800                 if( xCoreID == 0 )
2801                 {
2802                     /* The Idle task is being created using dynamically allocated RAM. */
2803                     xReturn = xTaskCreate( prvIdleTask,
2804                                            cIdleName,
2805                                            configMINIMAL_STACK_SIZE,
2806                                            ( void * ) NULL,
2807                                            portPRIVILEGE_BIT,             /* In effect ( tskIDLE_PRIORITY | portPRIVILEGE_BIT ), but tskIDLE_PRIORITY is zero. */
2808                                            &xIdleTaskHandle[ xCoreID ] ); /*lint !e961 MISRA exception, justified as it is not a redundant explicit cast to all supported compilers. */
2809                 }
2810
2811                 #if ( configNUM_CORES > 1 )
2812                     else
2813                     {
2814                         xReturn = xTaskCreate( prvMinimalIdleTask,
2815                                                cIdleName,
2816                                                configMINIMAL_STACK_SIZE,
2817                                                ( void * ) NULL,
2818                                                portPRIVILEGE_BIT,             /* In effect ( tskIDLE_PRIORITY | portPRIVILEGE_BIT ), but tskIDLE_PRIORITY is zero. */
2819                                                &xIdleTaskHandle[ xCoreID ] ); /*lint !e961 MISRA exception, justified as it is not a redundant explicit cast to all supported compilers. */
2820                     }
2821                 #endif
2822             }
2823         #endif /* configSUPPORT_STATIC_ALLOCATION */
2824     }
2825
2826     return xReturn;
2827 }
2828
2829 void vTaskStartScheduler( void )
2830 {
2831     BaseType_t xReturn;
2832
2833     #if ( configUSE_TIMERS == 1 )
2834         {
2835             xReturn = xTimerCreateTimerTask();
2836         }
2837     #endif /* configUSE_TIMERS */
2838
2839     xReturn = prvCreateIdleTasks();
2840
2841     if( xReturn == pdPASS )
2842     {
2843         /* freertos_tasks_c_additions_init() should only be called if the user
2844          * definable macro FREERTOS_TASKS_C_ADDITIONS_INIT() is defined, as that is
2845          * the only macro called by the function. */
2846         #ifdef FREERTOS_TASKS_C_ADDITIONS_INIT
2847             {
2848                 freertos_tasks_c_additions_init();
2849             }
2850         #endif
2851
2852         /* Interrupts are turned off here, to ensure a tick does not occur
2853          * before or during the call to xPortStartScheduler().  The stacks of
2854          * the created tasks contain a status word with interrupts switched on
2855          * so interrupts will automatically get re-enabled when the first task
2856          * starts to run. */
2857         portDISABLE_INTERRUPTS();
2858
2859         #if ( ( configUSE_NEWLIB_REENTRANT == 1 ) && ( configNEWLIB_REENTRANT_IS_DYNAMIC == 0 ) )
2860             {
2861                 /* Switch Newlib's _impure_ptr variable to point to the _reent
2862                  * structure specific to the task that will run first.
2863                  * See the third party link http://www.nadler.com/embedded/newlibAndFreeRTOS.html
2864                  * for additional information.
2865                  *
2866                  * Note: Updating the _impure_ptr is not required when Newlib is compiled with
2867                  * __DYNAMIC_REENT__ enabled. The port should provide __getreent() instead. */
2868                 _impure_ptr = &( pxCurrentTCB->xNewLib_reent );
2869             }
2870         #endif /* ( configUSE_NEWLIB_REENTRANT == 1 ) && ( configNEWLIB_REENTRANT_IS_DYNAMIC == 0 ) */
2871
2872         xNextTaskUnblockTime = portMAX_DELAY;
2873         xSchedulerRunning = pdTRUE;
2874         xTickCount = ( TickType_t ) configINITIAL_TICK_COUNT;
2875
2876         /* If configGENERATE_RUN_TIME_STATS is defined then the following
2877          * macro must be defined to configure the timer/counter used to generate
2878          * the run time counter time base.   NOTE:  If configGENERATE_RUN_TIME_STATS
2879          * is set to 0 and the following line fails to build then ensure you do not
2880          * have portCONFIGURE_TIMER_FOR_RUN_TIME_STATS() defined in your
2881          * FreeRTOSConfig.h file. */
2882         portCONFIGURE_TIMER_FOR_RUN_TIME_STATS();
2883
2884         traceTASK_SWITCHED_IN();
2885
2886         /* Setting up the timer tick is hardware specific and thus in the
2887          * portable interface. */
2888         if( xPortStartScheduler() != pdFALSE )
2889         {
2890             /* Should not reach here as if the scheduler is running the
2891              * function will not return. */
2892         }
2893         else
2894         {
2895             /* Should only reach here if a task calls xTaskEndScheduler(). */
2896         }
2897     }
2898     else
2899     {
2900         /* This line will only be reached if the kernel could not be started,
2901          * because there was not enough FreeRTOS heap to create the idle task
2902          * or the timer task. */
2903         configASSERT( xReturn != errCOULD_NOT_ALLOCATE_REQUIRED_MEMORY );
2904     }
2905
2906     /* Prevent compiler warnings if INCLUDE_xTaskGetIdleTaskHandle is set to 0,
2907      * meaning xIdleTaskHandle is not used anywhere else. */
2908     ( void ) xIdleTaskHandle;
2909
2910     /* OpenOCD makes use of uxTopUsedPriority for thread debugging. Prevent uxTopUsedPriority
2911      * from getting optimized out as it is no longer used by the kernel. */
2912     ( void ) uxTopUsedPriority;
2913 }
2914 /*-----------------------------------------------------------*/
2915
2916 void vTaskEndScheduler( void )
2917 {
2918     /* Stop the scheduler interrupts and call the portable scheduler end
2919      * routine so the original ISRs can be restored if necessary.  The port
2920      * layer must ensure interrupts enable  bit is left in the correct state. */
2921     portDISABLE_INTERRUPTS();
2922     xSchedulerRunning = pdFALSE;
2923     vPortEndScheduler();
2924 }
2925 /*----------------------------------------------------------*/
2926
2927 void vTaskSuspendAll( void )
2928 {
2929     UBaseType_t ulState;
2930
2931     /* This must only be called from within a task */
2932     portASSERT_IF_IN_ISR();
2933
2934     if( xSchedulerRunning != pdFALSE )
2935     {
2936         /* writes to uxSchedulerSuspended must be protected by both the task AND ISR locks.
2937          * We must disable interrupts before we grab the locks in the event that this task is
2938          * interrupted and switches context before incrementing uxSchedulerSuspended.
2939          * It is safe to re-enable interrupts after releasing the ISR lock and incrementing
2940          * uxSchedulerSuspended since that will prevent context switches. */
2941         ulState = portDISABLE_INTERRUPTS();
2942
2943         /* portSOFRWARE_BARRIER() is only implemented for emulated/simulated ports that
2944          * do not otherwise exhibit real time behaviour. */
2945         portSOFTWARE_BARRIER();
2946
2947         portGET_TASK_LOCK();
2948         portGET_ISR_LOCK();
2949
2950         /* The scheduler is suspended if uxSchedulerSuspended is non-zero.  An increment
2951          * is used to allow calls to vTaskSuspendAll() to nest. */
2952         ++uxSchedulerSuspended;
2953         portRELEASE_ISR_LOCK();
2954
2955         if( ( uxSchedulerSuspended == 1U ) && ( pxCurrentTCB->uxCriticalNesting == 0U ) )
2956         {
2957             prvCheckForRunStateChange();
2958         }
2959
2960         portRESTORE_INTERRUPTS( ulState );
2961     }
2962     else
2963     {
2964         mtCOVERAGE_TEST_MARKER();
2965     }
2966 }
2967 /*----------------------------------------------------------*/
2968
2969 #if ( configUSE_TICKLESS_IDLE != 0 )
2970
2971     static TickType_t prvGetExpectedIdleTime( void )
2972     {
2973         TickType_t xReturn;
2974         UBaseType_t uxHigherPriorityReadyTasks = pdFALSE;
2975
2976         /* uxHigherPriorityReadyTasks takes care of the case where
2977          * configUSE_PREEMPTION is 0, so there may be tasks above the idle priority
2978          * task that are in the Ready state, even though the idle task is
2979          * running. */
2980         #if ( configUSE_PORT_OPTIMISED_TASK_SELECTION == 0 )
2981             {
2982                 if( uxTopReadyPriority > tskIDLE_PRIORITY )
2983                 {
2984                     uxHigherPriorityReadyTasks = pdTRUE;
2985                 }
2986             }
2987         #else
2988             {
2989                 const UBaseType_t uxLeastSignificantBit = ( UBaseType_t ) 0x01;
2990
2991                 /* When port optimised task selection is used the uxTopReadyPriority
2992                  * variable is used as a bit map.  If bits other than the least
2993                  * significant bit are set then there are tasks that have a priority
2994                  * above the idle priority that are in the Ready state.  This takes
2995                  * care of the case where the co-operative scheduler is in use. */
2996                 if( uxTopReadyPriority > uxLeastSignificantBit )
2997                 {
2998                     uxHigherPriorityReadyTasks = pdTRUE;
2999                 }
3000             }
3001         #endif /* if ( configUSE_PORT_OPTIMISED_TASK_SELECTION == 0 ) */
3002
3003         if( pxCurrentTCB->uxPriority > tskIDLE_PRIORITY )
3004         {
3005             xReturn = 0;
3006         }
3007         else if( listCURRENT_LIST_LENGTH( &( pxReadyTasksLists[ tskIDLE_PRIORITY ] ) ) > 1 )
3008         {
3009             /* There are other idle priority tasks in the ready state.  If
3010              * time slicing is used then the very next tick interrupt must be
3011              * processed. */
3012             xReturn = 0;
3013         }
3014         else if( uxHigherPriorityReadyTasks != pdFALSE )
3015         {
3016             /* There are tasks in the Ready state that have a priority above the
3017              * idle priority.  This path can only be reached if
3018              * configUSE_PREEMPTION is 0. */
3019             xReturn = 0;
3020         }
3021         else
3022         {
3023             xReturn = xNextTaskUnblockTime - xTickCount;
3024         }
3025
3026         return xReturn;
3027     }
3028
3029 #endif /* configUSE_TICKLESS_IDLE */
3030 /*----------------------------------------------------------*/
3031
3032 BaseType_t xTaskResumeAll( void )
3033 {
3034     TCB_t * pxTCB = NULL;
3035     BaseType_t xAlreadyYielded = pdFALSE;
3036
3037     if( xSchedulerRunning != pdFALSE )
3038     {
3039         /* It is possible that an ISR caused a task to be removed from an event
3040          * list while the scheduler was suspended.  If this was the case then the
3041          * removed task will have been added to the xPendingReadyList.  Once the
3042          * scheduler has been resumed it is safe to move all the pending ready
3043          * tasks from this list into their appropriate ready list. */
3044         taskENTER_CRITICAL();
3045         {
3046             BaseType_t xCoreID;
3047
3048             xCoreID = portGET_CORE_ID();
3049
3050             /* If uxSchedulerSuspended is zero then this function does not match a
3051              * previous call to vTaskSuspendAll(). */
3052             configASSERT( uxSchedulerSuspended );
3053
3054             --uxSchedulerSuspended;
3055             portRELEASE_TASK_LOCK();
3056
3057             if( uxSchedulerSuspended == ( UBaseType_t ) pdFALSE )
3058             {
3059                 if( uxCurrentNumberOfTasks > ( UBaseType_t ) 0U )
3060                 {
3061                     /* Move any readied tasks from the pending list into the
3062                      * appropriate ready list. */
3063                     while( listLIST_IS_EMPTY( &xPendingReadyList ) == pdFALSE )
3064                     {
3065                         pxTCB = listGET_OWNER_OF_HEAD_ENTRY( ( &xPendingReadyList ) ); /*lint !e9079 void * is used as this macro is used with timers and co-routines too.  Alignment is known to be fine as the type of the pointer stored and retrieved is the same. */
3066                         ( void ) uxListRemove( &( pxTCB->xEventListItem ) );
3067                         ( void ) uxListRemove( &( pxTCB->xStateListItem ) );
3068                         prvAddTaskToReadyList( pxTCB );
3069
3070                         /* All appropriate tasks yield at the moment a task is added to xPendingReadyList.
3071                          * If the current core yielded then vTaskSwitchContext() has already been called
3072                          * which sets xYieldPendings for the current core to pdTRUE. */
3073                     }
3074
3075                     if( pxTCB != NULL )
3076                     {
3077                         /* A task was unblocked while the scheduler was suspended,
3078                          * which may have prevented the next unblock time from being
3079                          * re-calculated, in which case re-calculate it now.  Mainly
3080                          * important for low power tickless implementations, where
3081                          * this can prevent an unnecessary exit from low power
3082                          * state. */
3083                         prvResetNextTaskUnblockTime();
3084                     }
3085
3086                     /* If any ticks occurred while the scheduler was suspended then
3087                      * they should be processed now.  This ensures the tick count does
3088                      * not      slip, and that any delayed tasks are resumed at the correct
3089                      * time.
3090                      *
3091                      * It should be safe to call xTaskIncrementTick here from any core
3092                      * since we are in a critical section and xTaskIncrementTick itself
3093                      * protects itself within a critical section. Suspending the scheduler
3094                      * from any core causes xTaskIncrementTick to increment uxPendedCounts.*/
3095                     {
3096                         TickType_t xPendedCounts = xPendedTicks; /* Non-volatile copy. */
3097
3098                         if( xPendedCounts > ( TickType_t ) 0U )
3099                         {
3100                             do
3101                             {
3102                                 if( xTaskIncrementTick() != pdFALSE )
3103                                 {
3104                                     /* other cores are interrupted from
3105                                      * within xTaskIncrementTick(). */
3106                                     xYieldPendings[ xCoreID ] = pdTRUE;
3107                                 }
3108                                 else
3109                                 {
3110                                     mtCOVERAGE_TEST_MARKER();
3111                                 }
3112
3113                                 --xPendedCounts;
3114                             } while( xPendedCounts > ( TickType_t ) 0U );
3115
3116                             xPendedTicks = 0;
3117                         }
3118                         else
3119                         {
3120                             mtCOVERAGE_TEST_MARKER();
3121                         }
3122                     }
3123
3124                     if( xYieldPendings[ xCoreID ] != pdFALSE )
3125                     {
3126                         /* If xYieldPendings is true then taskEXIT_CRITICAL()
3127                          * will yield, so make sure we return true to let the
3128                          * caller know a yield has already happened. */
3129                         xAlreadyYielded = pdTRUE;
3130                     }
3131                 }
3132             }
3133             else
3134             {
3135                 mtCOVERAGE_TEST_MARKER();
3136             }
3137         }
3138         taskEXIT_CRITICAL();
3139     }
3140     else
3141     {
3142         mtCOVERAGE_TEST_MARKER();
3143     }
3144
3145     return xAlreadyYielded;
3146 }
3147 /*-----------------------------------------------------------*/
3148
3149 TickType_t xTaskGetTickCount( void )
3150 {
3151     TickType_t xTicks;
3152
3153     /* Critical section required if running on a 16 bit processor. */
3154     portTICK_TYPE_ENTER_CRITICAL();
3155     {
3156         xTicks = xTickCount;
3157     }
3158     portTICK_TYPE_EXIT_CRITICAL();
3159
3160     return xTicks;
3161 }
3162 /*-----------------------------------------------------------*/
3163
3164 TickType_t xTaskGetTickCountFromISR( void )
3165 {
3166     TickType_t xReturn;
3167     UBaseType_t uxSavedInterruptStatus;
3168
3169     /* RTOS ports that support interrupt nesting have the concept of a maximum
3170      * system call (or maximum API call) interrupt priority.  Interrupts that are
3171      * above the maximum system call priority are kept permanently enabled, even
3172      * when the RTOS kernel is in a critical section, but cannot make any calls to
3173      * FreeRTOS API functions.  If configASSERT() is defined in FreeRTOSConfig.h
3174      * then portASSERT_IF_INTERRUPT_PRIORITY_INVALID() will result in an assertion
3175      * failure if a FreeRTOS API function is called from an interrupt that has been
3176      * assigned a priority above the configured maximum system call priority.
3177      * Only FreeRTOS functions that end in FromISR can be called from interrupts
3178      * that have been assigned a priority at or (logically) below the maximum
3179      * system call  interrupt priority.  FreeRTOS maintains a separate interrupt
3180      * safe API to ensure interrupt entry is as fast and as simple as possible.
3181      * More information (albeit Cortex-M specific) is provided on the following
3182      * link: https://www.FreeRTOS.org/RTOS-Cortex-M3-M4.html */
3183     portASSERT_IF_INTERRUPT_PRIORITY_INVALID();
3184
3185     uxSavedInterruptStatus = portTICK_TYPE_SET_INTERRUPT_MASK_FROM_ISR();
3186     {
3187         xReturn = xTickCount;
3188     }
3189     portTICK_TYPE_CLEAR_INTERRUPT_MASK_FROM_ISR( uxSavedInterruptStatus );
3190
3191     return xReturn;
3192 }
3193 /*-----------------------------------------------------------*/
3194
3195 UBaseType_t uxTaskGetNumberOfTasks( void )
3196 {
3197     /* A critical section is not required because the variables are of type
3198      * BaseType_t. */
3199     return uxCurrentNumberOfTasks;
3200 }
3201 /*-----------------------------------------------------------*/
3202
3203 char * pcTaskGetName( TaskHandle_t xTaskToQuery ) /*lint !e971 Unqualified char types are allowed for strings and single characters only. */
3204 {
3205     TCB_t * pxTCB;
3206
3207     /* If null is passed in here then the name of the calling task is being
3208      * queried. */
3209     pxTCB = prvGetTCBFromHandle( xTaskToQuery );
3210     configASSERT( pxTCB );
3211     return &( pxTCB->pcTaskName[ 0 ] );
3212 }
3213 /*-----------------------------------------------------------*/
3214
3215 #if ( INCLUDE_xTaskGetHandle == 1 )
3216
3217     static TCB_t * prvSearchForNameWithinSingleList( List_t * pxList,
3218                                                      const char pcNameToQuery[] )
3219     {
3220         TCB_t * pxNextTCB, * pxFirstTCB, * pxReturn = NULL;
3221         UBaseType_t x;
3222         char cNextChar;
3223         BaseType_t xBreakLoop;
3224
3225         /* This function is called with the scheduler suspended. */
3226
3227         if( listCURRENT_LIST_LENGTH( pxList ) > ( UBaseType_t ) 0 )
3228         {
3229             listGET_OWNER_OF_NEXT_ENTRY( pxFirstTCB, pxList ); /*lint !e9079 void * is used as this macro is used with timers and co-routines too.  Alignment is known to be fine as the type of the pointer stored and retrieved is the same. */
3230
3231             do
3232             {
3233                 listGET_OWNER_OF_NEXT_ENTRY( pxNextTCB, pxList ); /*lint !e9079 void * is used as this macro is used with timers and co-routines too.  Alignment is known to be fine as the type of the pointer stored and retrieved is the same. */
3234
3235                 /* Check each character in the name looking for a match or
3236                  * mismatch. */
3237                 xBreakLoop = pdFALSE;
3238
3239                 for( x = ( UBaseType_t ) 0; x < ( UBaseType_t ) configMAX_TASK_NAME_LEN; x++ )
3240                 {
3241                     cNextChar = pxNextTCB->pcTaskName[ x ];
3242
3243                     if( cNextChar != pcNameToQuery[ x ] )
3244                     {
3245                         /* Characters didn't match. */
3246                         xBreakLoop = pdTRUE;
3247                     }
3248                     else if( cNextChar == ( char ) 0x00 )
3249                     {
3250                         /* Both strings terminated, a match must have been
3251                          * found. */
3252                         pxReturn = pxNextTCB;
3253                         xBreakLoop = pdTRUE;
3254                     }
3255                     else
3256                     {
3257                         mtCOVERAGE_TEST_MARKER();
3258                     }
3259
3260                     if( xBreakLoop != pdFALSE )
3261                     {
3262                         break;
3263                     }
3264                 }
3265
3266                 if( pxReturn != NULL )
3267                 {
3268                     /* The handle has been found. */
3269                     break;
3270                 }
3271             } while( pxNextTCB != pxFirstTCB );
3272         }
3273         else
3274         {
3275             mtCOVERAGE_TEST_MARKER();
3276         }
3277
3278         return pxReturn;
3279     }
3280
3281 #endif /* INCLUDE_xTaskGetHandle */
3282 /*-----------------------------------------------------------*/
3283
3284 #if ( INCLUDE_xTaskGetHandle == 1 )
3285
3286     TaskHandle_t xTaskGetHandle( const char * pcNameToQuery ) /*lint !e971 Unqualified char types are allowed for strings and single characters only. */
3287     {
3288         UBaseType_t uxQueue = configMAX_PRIORITIES;
3289         TCB_t * pxTCB;
3290
3291         /* Task names will be truncated to configMAX_TASK_NAME_LEN - 1 bytes. */
3292         configASSERT( strlen( pcNameToQuery ) < configMAX_TASK_NAME_LEN );
3293
3294         vTaskSuspendAll();
3295         {
3296             /* Search the ready lists. */
3297             do
3298             {
3299                 uxQueue--;
3300                 pxTCB = prvSearchForNameWithinSingleList( ( List_t * ) &( pxReadyTasksLists[ uxQueue ] ), pcNameToQuery );
3301
3302                 if( pxTCB != NULL )
3303                 {
3304                     /* Found the handle. */
3305                     break;
3306                 }
3307             } while( uxQueue > ( UBaseType_t ) tskIDLE_PRIORITY ); /*lint !e961 MISRA exception as the casts are only redundant for some ports. */
3308
3309             /* Search the delayed lists. */
3310             if( pxTCB == NULL )
3311             {
3312                 pxTCB = prvSearchForNameWithinSingleList( ( List_t * ) pxDelayedTaskList, pcNameToQuery );
3313             }
3314
3315             if( pxTCB == NULL )
3316             {
3317                 pxTCB = prvSearchForNameWithinSingleList( ( List_t * ) pxOverflowDelayedTaskList, pcNameToQuery );
3318             }
3319
3320             #if ( INCLUDE_vTaskSuspend == 1 )
3321                 {
3322                     if( pxTCB == NULL )
3323                     {
3324                         /* Search the suspended list. */
3325                         pxTCB = prvSearchForNameWithinSingleList( &xSuspendedTaskList, pcNameToQuery );
3326                     }
3327                 }
3328             #endif
3329
3330             #if ( INCLUDE_vTaskDelete == 1 )
3331                 {
3332                     if( pxTCB == NULL )
3333                     {
3334                         /* Search the deleted list. */
3335                         pxTCB = prvSearchForNameWithinSingleList( &xTasksWaitingTermination, pcNameToQuery );
3336                     }
3337                 }
3338             #endif
3339         }
3340         ( void ) xTaskResumeAll();
3341
3342         return pxTCB;
3343     }
3344
3345 #endif /* INCLUDE_xTaskGetHandle */
3346 /*-----------------------------------------------------------*/
3347
3348 #if ( configUSE_TRACE_FACILITY == 1 )
3349
3350     UBaseType_t uxTaskGetSystemState( TaskStatus_t * const pxTaskStatusArray,
3351                                       const UBaseType_t uxArraySize,
3352                                       uint32_t * const pulTotalRunTime )
3353     {
3354         UBaseType_t uxTask = 0, uxQueue = configMAX_PRIORITIES;
3355
3356         vTaskSuspendAll();
3357         {
3358             /* Is there a space in the array for each task in the system? */
3359             if( uxArraySize >= uxCurrentNumberOfTasks )
3360             {
3361                 /* Fill in an TaskStatus_t structure with information on each
3362                  * task in the Ready state. */
3363                 do
3364                 {
3365                     uxQueue--;
3366                     uxTask += prvListTasksWithinSingleList( &( pxTaskStatusArray[ uxTask ] ), &( pxReadyTasksLists[ uxQueue ] ), eReady );
3367                 } while( uxQueue > ( UBaseType_t ) tskIDLE_PRIORITY ); /*lint !e961 MISRA exception as the casts are only redundant for some ports. */
3368
3369                 /* Fill in an TaskStatus_t structure with information on each
3370                  * task in the Blocked state. */
3371                 uxTask += prvListTasksWithinSingleList( &( pxTaskStatusArray[ uxTask ] ), ( List_t * ) pxDelayedTaskList, eBlocked );
3372                 uxTask += prvListTasksWithinSingleList( &( pxTaskStatusArray[ uxTask ] ), ( List_t * ) pxOverflowDelayedTaskList, eBlocked );
3373
3374                 #if ( INCLUDE_vTaskDelete == 1 )
3375                     {
3376                         /* Fill in an TaskStatus_t structure with information on
3377                          * each task that has been deleted but not yet cleaned up. */
3378                         uxTask += prvListTasksWithinSingleList( &( pxTaskStatusArray[ uxTask ] ), &xTasksWaitingTermination, eDeleted );
3379                     }
3380                 #endif
3381
3382                 #if ( INCLUDE_vTaskSuspend == 1 )
3383                     {
3384                         /* Fill in an TaskStatus_t structure with information on
3385                          * each task in the Suspended state. */
3386                         uxTask += prvListTasksWithinSingleList( &( pxTaskStatusArray[ uxTask ] ), &xSuspendedTaskList, eSuspended );
3387                     }
3388                 #endif
3389
3390                 #if ( configGENERATE_RUN_TIME_STATS == 1 )
3391                     {
3392                         if( pulTotalRunTime != NULL )
3393                         {
3394                             #ifdef portALT_GET_RUN_TIME_COUNTER_VALUE
3395                                 portALT_GET_RUN_TIME_COUNTER_VALUE( ( *pulTotalRunTime ) );
3396                             #else
3397                                 *pulTotalRunTime = portGET_RUN_TIME_COUNTER_VALUE();
3398                             #endif
3399                         }
3400                     }
3401                 #else /* if ( configGENERATE_RUN_TIME_STATS == 1 ) */
3402                     {
3403                         if( pulTotalRunTime != NULL )
3404                         {
3405                             *pulTotalRunTime = 0;
3406                         }
3407                     }
3408                 #endif /* if ( configGENERATE_RUN_TIME_STATS == 1 ) */
3409             }
3410             else
3411             {
3412                 mtCOVERAGE_TEST_MARKER();
3413             }
3414         }
3415         ( void ) xTaskResumeAll();
3416
3417         return uxTask;
3418     }
3419
3420 #endif /* configUSE_TRACE_FACILITY */
3421 /*----------------------------------------------------------*/
3422
3423 #if ( INCLUDE_xTaskGetIdleTaskHandle == 1 )
3424
3425     TaskHandle_t * xTaskGetIdleTaskHandle( void )
3426     {
3427         /* If xTaskGetIdleTaskHandle() is called before the scheduler has been
3428          * started, then xIdleTaskHandle will be NULL. */
3429         configASSERT( ( xIdleTaskHandle != NULL ) );
3430         return &( xIdleTaskHandle[ 0 ] );
3431     }
3432
3433 #endif /* INCLUDE_xTaskGetIdleTaskHandle */
3434 /*----------------------------------------------------------*/
3435
3436 /* This conditional compilation should use inequality to 0, not equality to 1.
3437  * This is to ensure vTaskStepTick() is available when user defined low power mode
3438  * implementations require configUSE_TICKLESS_IDLE to be set to a value other than
3439  * 1. */
3440 #if ( configUSE_TICKLESS_IDLE != 0 )
3441
3442     void vTaskStepTick( const TickType_t xTicksToJump )
3443     {
3444         /* Correct the tick count value after a period during which the tick
3445          * was suppressed.  Note this does *not* call the tick hook function for
3446          * each stepped tick. */
3447         configASSERT( ( xTickCount + xTicksToJump ) <= xNextTaskUnblockTime );
3448         xTickCount += xTicksToJump;
3449         traceINCREASE_TICK_COUNT( xTicksToJump );
3450     }
3451
3452 #endif /* configUSE_TICKLESS_IDLE */
3453 /*----------------------------------------------------------*/
3454
3455 BaseType_t xTaskCatchUpTicks( TickType_t xTicksToCatchUp )
3456 {
3457     BaseType_t xYieldOccurred;
3458
3459     /* Must not be called with the scheduler suspended as the implementation
3460      * relies on xPendedTicks being wound down to 0 in xTaskResumeAll(). */
3461     configASSERT( uxSchedulerSuspended == 0 );
3462
3463     /* Use xPendedTicks to mimic xTicksToCatchUp number of ticks occurring when
3464      * the scheduler is suspended so the ticks are executed in xTaskResumeAll(). */
3465     vTaskSuspendAll();
3466     xPendedTicks += xTicksToCatchUp;
3467     xYieldOccurred = xTaskResumeAll();
3468
3469     return xYieldOccurred;
3470 }
3471 /*----------------------------------------------------------*/
3472
3473 #if ( INCLUDE_xTaskAbortDelay == 1 )
3474
3475     BaseType_t xTaskAbortDelay( TaskHandle_t xTask )
3476     {
3477         TCB_t * pxTCB = xTask;
3478         BaseType_t xReturn;
3479
3480         configASSERT( pxTCB );
3481
3482         vTaskSuspendAll();
3483         {
3484             /* A task can only be prematurely removed from the Blocked state if
3485              * it is actually in the Blocked state. */
3486             if( eTaskGetState( xTask ) == eBlocked )
3487             {
3488                 xReturn = pdPASS;
3489
3490                 /* Remove the reference to the task from the blocked list.  An
3491                  * interrupt won't touch the xStateListItem because the
3492                  * scheduler is suspended. */
3493                 ( void ) uxListRemove( &( pxTCB->xStateListItem ) );
3494
3495                 /* Is the task waiting on an event also?  If so remove it from
3496                  * the event list too.  Interrupts can touch the event list item,
3497                  * even though the scheduler is suspended, so a critical section
3498                  * is used. */
3499                 taskENTER_CRITICAL();
3500                 {
3501                     if( listLIST_ITEM_CONTAINER( &( pxTCB->xEventListItem ) ) != NULL )
3502                     {
3503                         ( void ) uxListRemove( &( pxTCB->xEventListItem ) );
3504
3505                         /* This lets the task know it was forcibly removed from the
3506                          * blocked state so it should not re-evaluate its block time and
3507                          * then block again. */
3508                         pxTCB->ucDelayAborted = pdTRUE;
3509                     }
3510                     else
3511                     {
3512                         mtCOVERAGE_TEST_MARKER();
3513                     }
3514                 }
3515                 taskEXIT_CRITICAL();
3516
3517                 /* Place the unblocked task into the appropriate ready list. */
3518                 prvAddTaskToReadyList( pxTCB );
3519
3520                 /* A task being unblocked cannot cause an immediate context
3521                  * switch if preemption is turned off. */
3522                 #if ( configUSE_PREEMPTION == 1 )
3523                     {
3524                         taskENTER_CRITICAL();
3525                         {
3526                             prvYieldForTask( pxTCB, pdFALSE );
3527                         }
3528                         taskEXIT_CRITICAL();
3529                     }
3530                 #endif /* configUSE_PREEMPTION */
3531             }
3532             else
3533             {
3534                 xReturn = pdFAIL;
3535             }
3536         }
3537         ( void ) xTaskResumeAll();
3538
3539         return xReturn;
3540     }
3541
3542 #endif /* INCLUDE_xTaskAbortDelay */
3543 /*----------------------------------------------------------*/
3544
3545 BaseType_t xTaskIncrementTick( void )
3546 {
3547     TCB_t * pxTCB;
3548     TickType_t xItemValue;
3549     BaseType_t xSwitchRequired = pdFALSE;
3550
3551     #if ( configUSE_PREEMPTION == 1 )
3552         UBaseType_t x;
3553         BaseType_t xCoreYieldList[ configNUM_CORES ] = { pdFALSE };
3554     #endif /* configUSE_PREEMPTION */
3555
3556     taskENTER_CRITICAL();
3557     {
3558         /* Called by the portable layer each time a tick interrupt occurs.
3559          * Increments the tick then checks to see if the new tick value will cause any
3560          * tasks to be unblocked. */
3561         traceTASK_INCREMENT_TICK( xTickCount );
3562
3563         /* Tick increment should occur on every kernel timer event. Core 0 has the
3564          * responsibility to increment the tick, or increment the pended ticks if the
3565          * scheduler is suspended.  If pended ticks is greater than zero, the core that
3566          * calls xTaskResumeAll has the responsibility to increment the tick. */
3567         if( uxSchedulerSuspended == ( UBaseType_t ) pdFALSE )
3568         {
3569             /* Minor optimisation.  The tick count cannot change in this
3570              * block. */
3571             const TickType_t xConstTickCount = xTickCount + ( TickType_t ) 1;
3572
3573             /* Increment the RTOS tick, switching the delayed and overflowed
3574              * delayed lists if it wraps to 0. */
3575             xTickCount = xConstTickCount;
3576
3577             if( xConstTickCount == ( TickType_t ) 0U ) /*lint !e774 'if' does not always evaluate to false as it is looking for an overflow. */
3578             {
3579                 taskSWITCH_DELAYED_LISTS();
3580             }
3581             else
3582             {
3583                 mtCOVERAGE_TEST_MARKER();
3584             }
3585
3586             /* See if this tick has made a timeout expire.  Tasks are stored in
3587              * the      queue in the order of their wake time - meaning once one task
3588              * has been found whose block time has not expired there is no need to
3589              * look any further down the list. */
3590             if( xConstTickCount >= xNextTaskUnblockTime )
3591             {
3592                 for( ; ; )
3593                 {
3594                     if( listLIST_IS_EMPTY( pxDelayedTaskList ) != pdFALSE )
3595                     {
3596                         /* The delayed list is empty.  Set xNextTaskUnblockTime
3597                          * to the maximum possible value so it is extremely
3598                          * unlikely that the
3599                          * if( xTickCount >= xNextTaskUnblockTime ) test will pass
3600                          * next time through. */
3601                         xNextTaskUnblockTime = portMAX_DELAY; /*lint !e961 MISRA exception as the casts are only redundant for some ports. */
3602                         break;
3603                     }
3604                     else
3605                     {
3606                         /* The delayed list is not empty, get the value of the
3607                          * item at the head of the delayed list.  This is the time
3608                          * at which the task at the head of the delayed list must
3609                          * be removed from the Blocked state. */
3610                         pxTCB = listGET_OWNER_OF_HEAD_ENTRY( pxDelayedTaskList ); /*lint !e9079 void * is used as this macro is used with timers and co-routines too.  Alignment is known to be fine as the type of the pointer stored and retrieved is the same. */
3611                         xItemValue = listGET_LIST_ITEM_VALUE( &( pxTCB->xStateListItem ) );
3612
3613                         if( xConstTickCount < xItemValue )
3614                         {
3615                             /* It is not time to unblock this item yet, but the
3616                              * item value is the time at which the task at the head
3617                              * of the blocked list must be removed from the Blocked
3618                              * state -  so record the item value in
3619                              * xNextTaskUnblockTime. */
3620                             xNextTaskUnblockTime = xItemValue;
3621                             break; /*lint !e9011 Code structure here is deemed easier to understand with multiple breaks. */
3622                         }
3623                         else
3624                         {
3625                             mtCOVERAGE_TEST_MARKER();
3626                         }
3627
3628                         /* It is time to remove the item from the Blocked state. */
3629                         ( void ) uxListRemove( &( pxTCB->xStateListItem ) );
3630
3631                         /* Is the task waiting on an event also?  If so remove
3632                          * it from the event list. */
3633                         if( listLIST_ITEM_CONTAINER( &( pxTCB->xEventListItem ) ) != NULL )
3634                         {
3635                             ( void ) uxListRemove( &( pxTCB->xEventListItem ) );
3636                         }
3637                         else
3638                         {
3639                             mtCOVERAGE_TEST_MARKER();
3640                         }
3641
3642                         /* Place the unblocked task into the appropriate ready
3643                          * list. */
3644                         prvAddTaskToReadyList( pxTCB );
3645
3646                         /* A task being unblocked cannot cause an immediate
3647                          * context switch if preemption is turned off. */
3648                         #if ( configUSE_PREEMPTION == 1 )
3649                             {
3650                                 prvYieldForTask( pxTCB, pdTRUE );
3651                             }
3652                         #endif /* configUSE_PREEMPTION */
3653                     }
3654                 }
3655             }
3656
3657             /* Tasks of equal priority to the currently running task will share
3658              * processing time (time slice) if preemption is on, and the application
3659              * writer has not explicitly turned time slicing off. */
3660             #if ( ( configUSE_PREEMPTION == 1 ) && ( configUSE_TIME_SLICING == 1 ) )
3661                 {
3662                     /* TODO: If there are fewer "non-IDLE" READY tasks than cores, do not
3663                      * force a context switch that would just shuffle tasks around cores */
3664                     /* TODO: There are certainly better ways of doing this that would reduce
3665                      * the number of interrupts and also potentially help prevent tasks from
3666                      * moving between cores as often. This, however, works for now. */
3667                     for( x = ( UBaseType_t ) 0; x < ( UBaseType_t ) configNUM_CORES; x++ )
3668                     {
3669                         if( listCURRENT_LIST_LENGTH( &( pxReadyTasksLists[ pxCurrentTCBs[ x ]->uxPriority ] ) ) > ( UBaseType_t ) 1 )
3670                         {
3671                             xCoreYieldList[ x ] = pdTRUE;
3672                         }
3673                         else
3674                         {
3675                             mtCOVERAGE_TEST_MARKER();
3676                         }
3677                     }
3678                 }
3679             #endif /* ( ( configUSE_PREEMPTION == 1 ) && ( configUSE_TIME_SLICING == 1 ) ) */
3680
3681             #if ( configUSE_TICK_HOOK == 1 )
3682                 {
3683                     /* Guard against the tick hook being called when the pended tick
3684                      * count is being unwound (when the scheduler is being unlocked). */
3685                     if( xPendedTicks == ( TickType_t ) 0 )
3686                     {
3687                         vApplicationTickHook();
3688                     }
3689                     else
3690                     {
3691                         mtCOVERAGE_TEST_MARKER();
3692                     }
3693                 }
3694             #endif /* configUSE_TICK_HOOK */
3695
3696             #if ( configUSE_PREEMPTION == 1 )
3697                 {
3698                     for( x = ( UBaseType_t ) 0; x < ( UBaseType_t ) configNUM_CORES; x++ )
3699                     {
3700                         if( xYieldPendings[ x ] != pdFALSE )
3701                         {
3702                             xCoreYieldList[ x ] = pdTRUE;
3703                         }
3704                         else
3705                         {
3706                             mtCOVERAGE_TEST_MARKER();
3707                         }
3708                     }
3709                 }
3710             #endif /* configUSE_PREEMPTION */
3711
3712             #if ( configUSE_PREEMPTION == 1 )
3713                 {
3714                     BaseType_t xCoreID;
3715
3716                     xCoreID = portGET_CORE_ID();
3717
3718                     for( x = ( UBaseType_t ) 0; x < ( UBaseType_t ) configNUM_CORES; x++ )
3719                     {
3720                         #if ( configUSE_TASK_PREEMPTION_DISABLE == 1 )
3721                             if( pxCurrentTCBs[ x ]->xPreemptionDisable == pdFALSE )
3722                         #endif
3723                         {
3724                             if( xCoreYieldList[ x ] != pdFALSE )
3725                             {
3726                                 if( x == xCoreID )
3727                                 {
3728                                     xSwitchRequired = pdTRUE;
3729                                 }
3730                                 else
3731                                 {
3732                                     prvYieldCore( x );
3733                                 }
3734                             }
3735                             else
3736                             {
3737                                 mtCOVERAGE_TEST_MARKER();
3738                             }
3739                         }
3740                     }
3741                 }
3742             #endif /* configUSE_PREEMPTION */
3743         }
3744         else
3745         {
3746             ++xPendedTicks;
3747
3748             /* The tick hook gets called at regular intervals, even if the
3749              * scheduler is locked. */
3750             #if ( configUSE_TICK_HOOK == 1 )
3751                 {
3752                     vApplicationTickHook();
3753                 }
3754             #endif
3755         }
3756     }
3757     taskEXIT_CRITICAL();
3758
3759     return xSwitchRequired;
3760 }
3761 /*-----------------------------------------------------------*/
3762
3763 #if ( configUSE_APPLICATION_TASK_TAG == 1 )
3764
3765     void vTaskSetApplicationTaskTag( TaskHandle_t xTask,
3766                                      TaskHookFunction_t pxHookFunction )
3767     {
3768         TCB_t * xTCB;
3769
3770         /* If xTask is NULL then it is the task hook of the calling task that is
3771          * getting set. */
3772         if( xTask == NULL )
3773         {
3774             xTCB = ( TCB_t * ) pxCurrentTCB;
3775         }
3776         else
3777         {
3778             xTCB = xTask;
3779         }
3780
3781         /* Save the hook function in the TCB.  A critical section is required as
3782          * the value can be accessed from an interrupt. */
3783         taskENTER_CRITICAL();
3784         {
3785             xTCB->pxTaskTag = pxHookFunction;
3786         }
3787         taskEXIT_CRITICAL();
3788     }
3789
3790 #endif /* configUSE_APPLICATION_TASK_TAG */
3791 /*-----------------------------------------------------------*/
3792
3793 #if ( configUSE_APPLICATION_TASK_TAG == 1 )
3794
3795     TaskHookFunction_t xTaskGetApplicationTaskTag( TaskHandle_t xTask )
3796     {
3797         TCB_t * pxTCB;
3798         TaskHookFunction_t xReturn;
3799
3800         /* If xTask is NULL then set the calling task's hook. */
3801         pxTCB = prvGetTCBFromHandle( xTask );
3802
3803         /* Save the hook function in the TCB.  A critical section is required as
3804          * the value can be accessed from an interrupt. */
3805         taskENTER_CRITICAL();
3806         {
3807             xReturn = pxTCB->pxTaskTag;
3808         }
3809         taskEXIT_CRITICAL();
3810
3811         return xReturn;
3812     }
3813
3814 #endif /* configUSE_APPLICATION_TASK_TAG */
3815 /*-----------------------------------------------------------*/
3816
3817 #if ( configUSE_APPLICATION_TASK_TAG == 1 )
3818
3819     TaskHookFunction_t xTaskGetApplicationTaskTagFromISR( TaskHandle_t xTask )
3820     {
3821         TCB_t * pxTCB;
3822         TaskHookFunction_t xReturn;
3823         UBaseType_t uxSavedInterruptStatus;
3824
3825         /* If xTask is NULL then set the calling task's hook. */
3826         pxTCB = prvGetTCBFromHandle( xTask );
3827
3828         /* Save the hook function in the TCB.  A critical section is required as
3829          * the value can be accessed from an interrupt. */
3830         uxSavedInterruptStatus = portSET_INTERRUPT_MASK_FROM_ISR();
3831         {
3832             xReturn = pxTCB->pxTaskTag;
3833         }
3834         portCLEAR_INTERRUPT_MASK_FROM_ISR( uxSavedInterruptStatus );
3835
3836         return xReturn;
3837     }
3838
3839 #endif /* configUSE_APPLICATION_TASK_TAG */
3840 /*-----------------------------------------------------------*/
3841
3842 #if ( configUSE_APPLICATION_TASK_TAG == 1 )
3843
3844     BaseType_t xTaskCallApplicationTaskHook( TaskHandle_t xTask,
3845                                              void * pvParameter )
3846     {
3847         TCB_t * xTCB;
3848         BaseType_t xReturn;
3849
3850         /* If xTask is NULL then we are calling our own task hook. */
3851         if( xTask == NULL )
3852         {
3853             xTCB = pxCurrentTCB;
3854         }
3855         else
3856         {
3857             xTCB = xTask;
3858         }
3859
3860         if( xTCB->pxTaskTag != NULL )
3861         {
3862             xReturn = xTCB->pxTaskTag( pvParameter );
3863         }
3864         else
3865         {
3866             xReturn = pdFAIL;
3867         }
3868
3869         return xReturn;
3870     }
3871
3872 #endif /* configUSE_APPLICATION_TASK_TAG */
3873 /*-----------------------------------------------------------*/
3874
3875 void vTaskSwitchContext( BaseType_t xCoreID )
3876 {
3877     /* Acquire both locks:
3878      * - The ISR lock protects the ready list from simultaneous access by
3879      *  both other ISRs and tasks.
3880      * - We also take the task lock to pause here in case another core has
3881      *  suspended the scheduler. We don't want to simply set xYieldPending
3882      *  and move on if another core suspended the scheduler. We should only
3883      *  do that if the current core has suspended the scheduler. */
3884
3885     portGET_TASK_LOCK(); /* Must always acquire the task lock first */
3886     portGET_ISR_LOCK();
3887     {
3888         /* vTaskSwitchContext() must never be called from within a critical section.
3889          * This is not necessarily true for vanilla FreeRTOS, but it is for this SMP port. */
3890         configASSERT( pxCurrentTCB->uxCriticalNesting == 0 );
3891
3892         if( uxSchedulerSuspended != ( UBaseType_t ) pdFALSE )
3893         {
3894             /* The scheduler is currently suspended - do not allow a context
3895              * switch. */
3896             xYieldPendings[ xCoreID ] = pdTRUE;
3897         }
3898         else
3899         {
3900             xYieldPendings[ xCoreID ] = pdFALSE;
3901             traceTASK_SWITCHED_OUT();
3902
3903             #if ( configGENERATE_RUN_TIME_STATS == 1 )
3904                 {
3905                     #ifdef portALT_GET_RUN_TIME_COUNTER_VALUE
3906                         portALT_GET_RUN_TIME_COUNTER_VALUE( ulTotalRunTime );
3907                     #else
3908                         ulTotalRunTime = portGET_RUN_TIME_COUNTER_VALUE();
3909                     #endif
3910
3911                     /* Add the amount of time the task has been running to the
3912                      * accumulated time so far.  The time the task started running was
3913                      * stored in ulTaskSwitchedInTime.  Note that there is no overflow
3914                      * protection here so count values are only valid until the timer
3915                      * overflows.  The guard against negative values is to protect
3916                      * against suspect run time stat counter implementations - which
3917                      * are provided by the application, not the kernel. */
3918                     if( ulTotalRunTime > ulTaskSwitchedInTime )
3919                     {
3920                         pxCurrentTCB->ulRunTimeCounter += ( ulTotalRunTime - ulTaskSwitchedInTime );
3921                     }
3922                     else
3923                     {
3924                         mtCOVERAGE_TEST_MARKER();
3925                     }
3926
3927                     ulTaskSwitchedInTime = ulTotalRunTime;
3928                 }
3929             #endif /* configGENERATE_RUN_TIME_STATS */
3930
3931             /* Check for stack overflow, if configured. */
3932             taskCHECK_FOR_STACK_OVERFLOW();
3933
3934             /* Before the currently running task is switched out, save its errno. */
3935             #if ( configUSE_POSIX_ERRNO == 1 )
3936                 {
3937                     pxCurrentTCB->iTaskErrno = FreeRTOS_errno;
3938                 }
3939             #endif
3940
3941             /* Select a new task to run using either the generic C or port
3942              * optimised asm code. */
3943             ( void ) prvSelectHighestPriorityTask( xCoreID );
3944             traceTASK_SWITCHED_IN();
3945
3946             /* After the new task is switched in, update the global errno. */
3947             #if ( configUSE_POSIX_ERRNO == 1 )
3948                 {
3949                     FreeRTOS_errno = pxCurrentTCB->iTaskErrno;
3950                 }
3951             #endif
3952
3953             #if ( ( configUSE_NEWLIB_REENTRANT == 1 ) && ( configNEWLIB_REENTRANT_IS_DYNAMIC == 0 ) )
3954                 {
3955                     /* Switch Newlib's _impure_ptr variable to point to the _reent
3956                      * structure specific to this task.
3957                      * See the third party link http://www.nadler.com/embedded/newlibAndFreeRTOS.html
3958                      * for additional information.
3959                      *
3960                      * Note: Updating the _impure_ptr is not required when Newlib is compiled with
3961                      * __DYNAMIC_REENT__ enabled. The the port should provide __getreent() instead. */
3962                     _impure_ptr = &( pxCurrentTCB->xNewLib_reent );
3963                 }
3964             #endif /* ( configUSE_NEWLIB_REENTRANT == 1 ) && ( configNEWLIB_REENTRANT_IS_DYNAMIC == 0 ) */
3965         }
3966     }
3967     portRELEASE_ISR_LOCK();
3968     portRELEASE_TASK_LOCK();
3969 }
3970 /*-----------------------------------------------------------*/
3971
3972 void vTaskPlaceOnEventList( List_t * const pxEventList,
3973                             const TickType_t xTicksToWait )
3974 {
3975     configASSERT( pxEventList );
3976
3977     /* THIS FUNCTION MUST BE CALLED WITH EITHER INTERRUPTS DISABLED OR THE
3978      * SCHEDULER SUSPENDED AND THE QUEUE BEING ACCESSED LOCKED. */
3979
3980     /* Place the event list item of the TCB in the appropriate event list.
3981      * This is placed in the list in priority order so the highest priority task
3982      * is the first to be woken by the event.  The queue that contains the event
3983      * list is locked, preventing simultaneous access from interrupts. */
3984     vListInsert( pxEventList, &( pxCurrentTCB->xEventListItem ) );
3985
3986     prvAddCurrentTaskToDelayedList( xTicksToWait, pdTRUE );
3987 }
3988 /*-----------------------------------------------------------*/
3989
3990 void vTaskPlaceOnUnorderedEventList( List_t * pxEventList,
3991                                      const TickType_t xItemValue,
3992                                      const TickType_t xTicksToWait )
3993 {
3994     configASSERT( pxEventList );
3995
3996     /* THIS FUNCTION MUST BE CALLED WITH THE SCHEDULER SUSPENDED.  It is used by
3997      * the event groups implementation. */
3998     configASSERT( uxSchedulerSuspended != 0 );
3999
4000     /* Store the item value in the event list item.  It is safe to access the
4001      * event list item here as interrupts won't access the event list item of a
4002      * task that is not in the Blocked state. */
4003     listSET_LIST_ITEM_VALUE( &( pxCurrentTCB->xEventListItem ), xItemValue | taskEVENT_LIST_ITEM_VALUE_IN_USE );
4004
4005     /* Place the event list item of the TCB at the end of the appropriate event
4006      * list.  It is safe to access the event list here because it is part of an
4007      * event group implementation - and interrupts don't access event groups
4008      * directly (instead they access them indirectly by pending function calls to
4009      * the task level). */
4010     vListInsertEnd( pxEventList, &( pxCurrentTCB->xEventListItem ) );
4011
4012     prvAddCurrentTaskToDelayedList( xTicksToWait, pdTRUE );
4013 }
4014 /*-----------------------------------------------------------*/
4015
4016 #if ( configUSE_TIMERS == 1 )
4017
4018     void vTaskPlaceOnEventListRestricted( List_t * const pxEventList,
4019                                           TickType_t xTicksToWait,
4020                                           const BaseType_t xWaitIndefinitely )
4021     {
4022         configASSERT( pxEventList );
4023
4024         /* This function should not be called by application code hence the
4025          * 'Restricted' in its name.  It is not part of the public API.  It is
4026          * designed for use by kernel code, and has special calling requirements -
4027          * it should be called with the scheduler suspended. */
4028
4029
4030         /* Place the event list item of the TCB in the appropriate event list.
4031          * In this case it is assume that this is the only task that is going to
4032          * be waiting on this event list, so the faster vListInsertEnd() function
4033          * can be used in place of vListInsert. */
4034         vListInsertEnd( pxEventList, &( pxCurrentTCB->xEventListItem ) );
4035
4036         /* If the task should block indefinitely then set the block time to a
4037          * value that will be recognised as an indefinite delay inside the
4038          * prvAddCurrentTaskToDelayedList() function. */
4039         if( xWaitIndefinitely != pdFALSE )
4040         {
4041             xTicksToWait = portMAX_DELAY;
4042         }
4043
4044         traceTASK_DELAY_UNTIL( ( xTickCount + xTicksToWait ) );
4045         prvAddCurrentTaskToDelayedList( xTicksToWait, xWaitIndefinitely );
4046     }
4047
4048 #endif /* configUSE_TIMERS */
4049 /*-----------------------------------------------------------*/
4050
4051 BaseType_t xTaskRemoveFromEventList( const List_t * const pxEventList )
4052 {
4053     TCB_t * pxUnblockedTCB;
4054     BaseType_t xReturn;
4055
4056     /* THIS FUNCTION MUST BE CALLED FROM A CRITICAL SECTION.  It can also be
4057      * called from a critical section within an ISR. */
4058
4059     /* The event list is sorted in priority order, so the first in the list can
4060      * be removed as it is known to be the highest priority.  Remove the TCB from
4061      * the delayed list, and add it to the ready list.
4062      *
4063      * If an event is for a queue that is locked then this function will never
4064      * get called - the lock count on the queue will get modified instead.  This
4065      * means exclusive access to the event list is guaranteed here.
4066      *
4067      * This function assumes that a check has already been made to ensure that
4068      * pxEventList is not empty. */
4069     pxUnblockedTCB = listGET_OWNER_OF_HEAD_ENTRY( pxEventList ); /*lint !e9079 void * is used as this macro is used with timers and co-routines too.  Alignment is known to be fine as the type of the pointer stored and retrieved is the same. */
4070     configASSERT( pxUnblockedTCB );
4071     ( void ) uxListRemove( &( pxUnblockedTCB->xEventListItem ) );
4072
4073     if( uxSchedulerSuspended == ( UBaseType_t ) pdFALSE )
4074     {
4075         ( void ) uxListRemove( &( pxUnblockedTCB->xStateListItem ) );
4076         prvAddTaskToReadyList( pxUnblockedTCB );
4077
4078         #if ( configUSE_TICKLESS_IDLE != 0 )
4079             {
4080                 /* If a task is blocked on a kernel object then xNextTaskUnblockTime
4081                  * might be set to the blocked task's time out time.  If the task is
4082                  * unblocked for a reason other than a timeout xNextTaskUnblockTime is
4083                  * normally left unchanged, because it is automatically reset to a new
4084                  * value when the tick count equals xNextTaskUnblockTime.  However if
4085                  * tickless idling is used it might be more important to enter sleep mode
4086                  * at the earliest possible time - so reset xNextTaskUnblockTime here to
4087                  * ensure it is updated at the earliest possible time. */
4088                 prvResetNextTaskUnblockTime();
4089             }
4090         #endif
4091     }
4092     else
4093     {
4094         /* The delayed and ready lists cannot be accessed, so hold this task
4095          * pending until the scheduler is resumed. */
4096         vListInsertEnd( &( xPendingReadyList ), &( pxUnblockedTCB->xEventListItem ) );
4097     }
4098
4099     xReturn = pdFALSE;
4100     #if ( configUSE_PREEMPTION == 1 )
4101         prvYieldForTask( pxUnblockedTCB, pdFALSE );
4102
4103         if( xYieldPendings[ portGET_CORE_ID() ] != pdFALSE )
4104         {
4105             xReturn = pdTRUE;
4106         }
4107     #endif
4108
4109     return xReturn;
4110 }
4111 /*-----------------------------------------------------------*/
4112
4113 void vTaskRemoveFromUnorderedEventList( ListItem_t * pxEventListItem,
4114                                         const TickType_t xItemValue )
4115 {
4116     TCB_t * pxUnblockedTCB;
4117
4118     /* THIS FUNCTION MUST BE CALLED WITH THE SCHEDULER SUSPENDED.  It is used by
4119      * the event flags implementation. */
4120     configASSERT( uxSchedulerSuspended != pdFALSE );
4121
4122     /* Store the new item value in the event list. */
4123     listSET_LIST_ITEM_VALUE( pxEventListItem, xItemValue | taskEVENT_LIST_ITEM_VALUE_IN_USE );
4124
4125     /* Remove the event list form the event flag.  Interrupts do not access
4126      * event flags. */
4127     pxUnblockedTCB = listGET_LIST_ITEM_OWNER( pxEventListItem ); /*lint !e9079 void * is used as this macro is used with timers and co-routines too.  Alignment is known to be fine as the type of the pointer stored and retrieved is the same. */
4128     configASSERT( pxUnblockedTCB );
4129     ( void ) uxListRemove( pxEventListItem );
4130
4131     #if ( configUSE_TICKLESS_IDLE != 0 )
4132         {
4133             /* If a task is blocked on a kernel object then xNextTaskUnblockTime
4134              * might be set to the blocked task's time out time.  If the task is
4135              * unblocked for a reason other than a timeout xNextTaskUnblockTime is
4136              * normally left unchanged, because it is automatically reset to a new
4137              * value when the tick count equals xNextTaskUnblockTime.  However if
4138              * tickless idling is used it might be more important to enter sleep mode
4139              * at the earliest possible time - so reset xNextTaskUnblockTime here to
4140              * ensure it is updated at the earliest possible time. */
4141             prvResetNextTaskUnblockTime();
4142         }
4143     #endif
4144
4145     /* Remove the task from the delayed list and add it to the ready list.  The
4146      * scheduler is suspended so interrupts will not be accessing the ready
4147      * lists. */
4148     ( void ) uxListRemove( &( pxUnblockedTCB->xStateListItem ) );
4149     prvAddTaskToReadyList( pxUnblockedTCB );
4150
4151     #if ( configUSE_PREEMPTION == 1 )
4152         taskENTER_CRITICAL();
4153         {
4154             prvYieldForTask( pxUnblockedTCB, pdFALSE );
4155         }
4156         taskEXIT_CRITICAL();
4157     #endif
4158 }
4159 /*-----------------------------------------------------------*/
4160
4161 void vTaskSetTimeOutState( TimeOut_t * const pxTimeOut )
4162 {
4163     configASSERT( pxTimeOut );
4164     taskENTER_CRITICAL();
4165     {
4166         pxTimeOut->xOverflowCount = xNumOfOverflows;
4167         pxTimeOut->xTimeOnEntering = xTickCount;
4168     }
4169     taskEXIT_CRITICAL();
4170 }
4171 /*-----------------------------------------------------------*/
4172
4173 void vTaskInternalSetTimeOutState( TimeOut_t * const pxTimeOut )
4174 {
4175     /* For internal use only as it does not use a critical section. */
4176     pxTimeOut->xOverflowCount = xNumOfOverflows;
4177     pxTimeOut->xTimeOnEntering = xTickCount;
4178 }
4179 /*-----------------------------------------------------------*/
4180
4181 BaseType_t xTaskCheckForTimeOut( TimeOut_t * const pxTimeOut,
4182                                  TickType_t * const pxTicksToWait )
4183 {
4184     BaseType_t xReturn;
4185
4186     configASSERT( pxTimeOut );
4187     configASSERT( pxTicksToWait );
4188
4189     taskENTER_CRITICAL();
4190     {
4191         /* Minor optimisation.  The tick count cannot change in this block. */
4192         const TickType_t xConstTickCount = xTickCount;
4193         const TickType_t xElapsedTime = xConstTickCount - pxTimeOut->xTimeOnEntering;
4194
4195         #if ( INCLUDE_xTaskAbortDelay == 1 )
4196             if( pxCurrentTCB->ucDelayAborted != ( uint8_t ) pdFALSE )
4197             {
4198                 /* The delay was aborted, which is not the same as a time out,
4199                  * but has the same result. */
4200                 pxCurrentTCB->ucDelayAborted = pdFALSE;
4201                 xReturn = pdTRUE;
4202             }
4203             else
4204         #endif
4205
4206         #if ( INCLUDE_vTaskSuspend == 1 )
4207             if( *pxTicksToWait == portMAX_DELAY )
4208             {
4209                 /* If INCLUDE_vTaskSuspend is set to 1 and the block time
4210                  * specified is the maximum block time then the task should block
4211                  * indefinitely, and therefore never time out. */
4212                 xReturn = pdFALSE;
4213             }
4214             else
4215         #endif
4216
4217         if( ( xNumOfOverflows != pxTimeOut->xOverflowCount ) && ( xConstTickCount >= pxTimeOut->xTimeOnEntering ) ) /*lint !e525 Indentation preferred as is to make code within pre-processor directives clearer. */
4218         {
4219             /* The tick count is greater than the time at which
4220              * vTaskSetTimeout() was called, but has also overflowed since
4221              * vTaskSetTimeOut() was called.  It must have wrapped all the way
4222              * around and gone past again. This passed since vTaskSetTimeout()
4223              * was called. */
4224             xReturn = pdTRUE;
4225             *pxTicksToWait = ( TickType_t ) 0;
4226         }
4227         else if( xElapsedTime < *pxTicksToWait ) /*lint !e961 Explicit casting is only redundant with some compilers, whereas others require it to prevent integer conversion errors. */
4228         {
4229             /* Not a genuine timeout. Adjust parameters for time remaining. */
4230             *pxTicksToWait -= xElapsedTime;
4231             vTaskInternalSetTimeOutState( pxTimeOut );
4232             xReturn = pdFALSE;
4233         }
4234         else
4235         {
4236             *pxTicksToWait = ( TickType_t ) 0;
4237             xReturn = pdTRUE;
4238         }
4239     }
4240     taskEXIT_CRITICAL();
4241
4242     return xReturn;
4243 }
4244 /*-----------------------------------------------------------*/
4245
4246 void vTaskMissedYield( void )
4247 {
4248     /* Must be called from within a critical section */
4249     xYieldPendings[ portGET_CORE_ID() ] = pdTRUE;
4250 }
4251 /*-----------------------------------------------------------*/
4252
4253 #if ( configUSE_TRACE_FACILITY == 1 )
4254
4255     UBaseType_t uxTaskGetTaskNumber( TaskHandle_t xTask )
4256     {
4257         UBaseType_t uxReturn;
4258         TCB_t const * pxTCB;
4259
4260         if( xTask != NULL )
4261         {
4262             pxTCB = xTask;
4263             uxReturn = pxTCB->uxTaskNumber;
4264         }
4265         else
4266         {
4267             uxReturn = 0U;
4268         }
4269
4270         return uxReturn;
4271     }
4272
4273 #endif /* configUSE_TRACE_FACILITY */
4274 /*-----------------------------------------------------------*/
4275
4276 #if ( configUSE_TRACE_FACILITY == 1 )
4277
4278     void vTaskSetTaskNumber( TaskHandle_t xTask,
4279                              const UBaseType_t uxHandle )
4280     {
4281         TCB_t * pxTCB;
4282
4283         if( xTask != NULL )
4284         {
4285             pxTCB = xTask;
4286             pxTCB->uxTaskNumber = uxHandle;
4287         }
4288     }
4289
4290 #endif /* configUSE_TRACE_FACILITY */
4291
4292 /*
4293  * -----------------------------------------------------------
4294  * The MinimalIdle task.
4295  * ----------------------------------------------------------
4296  *
4297  * The minimal idle task is used for all the additional Cores in a SMP system.
4298  * There must be only 1 idle task and the rest are minimal idle tasks.
4299  *
4300  * @todo additional conditional compiles to remove this function.
4301  */
4302
4303 #if ( configNUM_CORES > 1 )
4304     static portTASK_FUNCTION( prvMinimalIdleTask, pvParameters )
4305     {
4306         taskYIELD();
4307
4308         for( ; ; )
4309         {
4310             #if ( configUSE_PREEMPTION == 0 )
4311                 {
4312                     /* If we are not using preemption we keep forcing a task switch to
4313                      * see if any other task has become available.  If we are using
4314                      * preemption we don't need to do this as any task becoming available
4315                      * will automatically get the processor anyway. */
4316                     taskYIELD();
4317                 }
4318             #endif /* configUSE_PREEMPTION */
4319
4320             #if ( ( configUSE_PREEMPTION == 1 ) && ( configIDLE_SHOULD_YIELD == 1 ) )
4321                 {
4322                     /* When using preemption tasks of equal priority will be
4323                      * timesliced.  If a task that is sharing the idle priority is ready
4324                      * to run then the idle task should yield before the end of the
4325                      * timeslice.
4326                      *
4327                      * A critical region is not required here as we are just reading from
4328                      * the list, and an occasional incorrect value will not matter.  If
4329                      * the ready list at the idle priority contains one more task than the
4330                      * number of idle tasks, which is equal to the configured numbers of cores
4331                      * then a task other than the idle task is ready to execute. */
4332                     if( listCURRENT_LIST_LENGTH( &( pxReadyTasksLists[ tskIDLE_PRIORITY ] ) ) > ( UBaseType_t ) configNUM_CORES )
4333                     {
4334                         taskYIELD();
4335                     }
4336                     else
4337                     {
4338                         mtCOVERAGE_TEST_MARKER();
4339                     }
4340                 }
4341             #endif /* ( ( configUSE_PREEMPTION == 1 ) && ( configIDLE_SHOULD_YIELD == 1 ) ) */
4342
4343             #if ( configUSE_MINIMAL_IDLE_HOOK == 1 )
4344                 {
4345                     extern void vApplicationMinimalIdleHook( void );
4346
4347                     /* Call the user defined function from within the idle task.  This
4348                      * allows the application designer to add background functionality
4349                      * without the overhead of a separate task.
4350                      *
4351                      * This hook is intended to manage core activity such as disabling cores that go idle.
4352                      *
4353                      * NOTE: vApplicationMinimalIdleHook() MUST NOT, UNDER ANY CIRCUMSTANCES,
4354                      * CALL A FUNCTION THAT MIGHT BLOCK. */
4355                     vApplicationMinimalIdleHook();
4356                 }
4357             #endif /* configUSE_MINIMAL_IDLE_HOOK */
4358         }
4359     }
4360 #endif /* if ( configNUM_CORES > 1 ) */
4361
4362 /*
4363  * -----------------------------------------------------------
4364  * The Idle task.
4365  * ----------------------------------------------------------
4366  *
4367  *
4368  */
4369 static portTASK_FUNCTION( prvIdleTask, pvParameters )
4370 {
4371     /* Stop warnings. */
4372     ( void ) pvParameters;
4373
4374     /** THIS IS THE RTOS IDLE TASK - WHICH IS CREATED AUTOMATICALLY WHEN THE
4375      * SCHEDULER IS STARTED. **/
4376
4377     /* In case a task that has a secure context deletes itself, in which case
4378      * the idle task is responsible for deleting the task's secure context, if
4379      * any. */
4380     portALLOCATE_SECURE_CONTEXT( configMINIMAL_SECURE_STACK_SIZE );
4381
4382     /* All cores start up in the idle task. This initial yield gets the application
4383      * tasks started. */
4384     taskYIELD();
4385
4386     for( ; ; )
4387     {
4388         /* See if any tasks have deleted themselves - if so then the idle task
4389          * is responsible for freeing the deleted task's TCB and stack. */
4390         prvCheckTasksWaitingTermination();
4391
4392         #if ( configUSE_PREEMPTION == 0 )
4393             {
4394                 /* If we are not using preemption we keep forcing a task switch to
4395                  * see if any other task has become available.  If we are using
4396                  * preemption we don't need to do this as any task becoming available
4397                  * will automatically get the processor anyway. */
4398                 taskYIELD();
4399             }
4400         #endif /* configUSE_PREEMPTION */
4401
4402         #if ( ( configUSE_PREEMPTION == 1 ) && ( configIDLE_SHOULD_YIELD == 1 ) )
4403             {
4404                 /* When using preemption tasks of equal priority will be
4405                  * timesliced.  If a task that is sharing the idle priority is ready
4406                  * to run then the idle task should yield before the end of the
4407                  * timeslice.
4408                  *
4409                  * A critical region is not required here as we are just reading from
4410                  * the list, and an occasional incorrect value will not matter.  If
4411                  * the ready list at the idle priority contains one more task than the
4412                  * number of idle tasks, which is equal to the configured numbers of cores
4413                  * then a task other than the idle task is ready to execute. */
4414                 if( listCURRENT_LIST_LENGTH( &( pxReadyTasksLists[ tskIDLE_PRIORITY ] ) ) > ( UBaseType_t ) configNUM_CORES )
4415                 {
4416                     taskYIELD();
4417                 }
4418                 else
4419                 {
4420                     mtCOVERAGE_TEST_MARKER();
4421                 }
4422             }
4423         #endif /* ( ( configUSE_PREEMPTION == 1 ) && ( configIDLE_SHOULD_YIELD == 1 ) ) */
4424
4425         #if ( configUSE_IDLE_HOOK == 1 )
4426             {
4427                 extern void vApplicationIdleHook( void );
4428
4429                 /* Call the user defined function from within the idle task.  This
4430                  * allows the application designer to add background functionality
4431                  * without the overhead of a separate task.
4432                  *
4433                  * NOTE: vApplicationIdleHook() MUST NOT, UNDER ANY CIRCUMSTANCES,
4434                  * CALL A FUNCTION THAT MIGHT BLOCK. */
4435                 vApplicationIdleHook();
4436             }
4437         #endif /* configUSE_IDLE_HOOK */
4438
4439         /* This conditional compilation should use inequality to 0, not equality
4440          * to 1.  This is to ensure portSUPPRESS_TICKS_AND_SLEEP() is called when
4441          * user defined low power mode  implementations require
4442          * configUSE_TICKLESS_IDLE to be set to a value other than 1. */
4443         #if ( configUSE_TICKLESS_IDLE != 0 )
4444             {
4445                 TickType_t xExpectedIdleTime;
4446
4447                 /* It is not desirable to suspend then resume the scheduler on
4448                  * each iteration of the idle task.  Therefore, a preliminary
4449                  * test of the expected idle time is performed without the
4450                  * scheduler suspended.  The result here is not necessarily
4451                  * valid. */
4452                 xExpectedIdleTime = prvGetExpectedIdleTime();
4453
4454                 if( xExpectedIdleTime >= configEXPECTED_IDLE_TIME_BEFORE_SLEEP )
4455                 {
4456                     vTaskSuspendAll();
4457                     {
4458                         /* Now the scheduler is suspended, the expected idle
4459                          * time can be sampled again, and this time its value can
4460                          * be used. */
4461                         configASSERT( xNextTaskUnblockTime >= xTickCount );
4462                         xExpectedIdleTime = prvGetExpectedIdleTime();
4463
4464                         /* Define the following macro to set xExpectedIdleTime to 0
4465                          * if the application does not want
4466                          * portSUPPRESS_TICKS_AND_SLEEP() to be called. */
4467                         configPRE_SUPPRESS_TICKS_AND_SLEEP_PROCESSING( xExpectedIdleTime );
4468
4469                         if( xExpectedIdleTime >= configEXPECTED_IDLE_TIME_BEFORE_SLEEP )
4470                         {
4471                             traceLOW_POWER_IDLE_BEGIN();
4472                             portSUPPRESS_TICKS_AND_SLEEP( xExpectedIdleTime );
4473                             traceLOW_POWER_IDLE_END();
4474                         }
4475                         else
4476                         {
4477                             mtCOVERAGE_TEST_MARKER();
4478                         }
4479                     }
4480                     ( void ) xTaskResumeAll();
4481                 }
4482                 else
4483                 {
4484                     mtCOVERAGE_TEST_MARKER();
4485                 }
4486             }
4487         #endif /* configUSE_TICKLESS_IDLE */
4488
4489         #if ( configUSE_MINIMAL_IDLE_HOOK == 1 )
4490             {
4491                 extern void vApplicationMinimalIdleHook( void );
4492
4493                 /* Call the user defined function from within the idle task.  This
4494                  * allows the application designer to add background functionality
4495                  * without the overhead of a separate task.
4496                  *
4497                  * This hook is intended to manage core activity such as disabling cores that go idle.
4498                  *
4499                  * NOTE: vApplicationMinimalIdleHook() MUST NOT, UNDER ANY CIRCUMSTANCES,
4500                  * CALL A FUNCTION THAT MIGHT BLOCK. */
4501                 vApplicationMinimalIdleHook();
4502             }
4503         #endif /* configUSE_MINIMAL_IDLE_HOOK */
4504     }
4505 }
4506 /*-----------------------------------------------------------*/
4507
4508 #if ( configUSE_TICKLESS_IDLE != 0 )
4509
4510     eSleepModeStatus eTaskConfirmSleepModeStatus( void )
4511     {
4512         /* The idle task exists in addition to the application tasks. */
4513         const UBaseType_t uxNonApplicationTasks = 1;
4514         eSleepModeStatus eReturn = eStandardSleep;
4515
4516         /* This function must be called from a critical section. */
4517
4518         if( listCURRENT_LIST_LENGTH( &xPendingReadyList ) != 0 )
4519         {
4520             /* A task was made ready while the scheduler was suspended. */
4521             eReturn = eAbortSleep;
4522         }
4523         else if( xYieldPending != pdFALSE )
4524         {
4525             /* A yield was pended while the scheduler was suspended. */
4526             eReturn = eAbortSleep;
4527         }
4528         else if( xPendedTicks != 0 )
4529         {
4530             /* A tick interrupt has already occurred but was held pending
4531              * because the scheduler is suspended. */
4532             eReturn = eAbortSleep;
4533         }
4534         else
4535         {
4536             /* If all the tasks are in the suspended list (which might mean they
4537              * have an infinite block time rather than actually being suspended)
4538              * then it is safe to turn all clocks off and just wait for external
4539              * interrupts. */
4540             if( listCURRENT_LIST_LENGTH( &xSuspendedTaskList ) == ( uxCurrentNumberOfTasks - uxNonApplicationTasks ) )
4541             {
4542                 eReturn = eNoTasksWaitingTimeout;
4543             }
4544             else
4545             {
4546                 mtCOVERAGE_TEST_MARKER();
4547             }
4548         }
4549
4550         return eReturn;
4551     }
4552
4553 #endif /* configUSE_TICKLESS_IDLE */
4554 /*-----------------------------------------------------------*/
4555
4556 #if ( configNUM_THREAD_LOCAL_STORAGE_POINTERS != 0 )
4557
4558     void vTaskSetThreadLocalStoragePointer( TaskHandle_t xTaskToSet,
4559                                             BaseType_t xIndex,
4560                                             void * pvValue )
4561     {
4562         TCB_t * pxTCB;
4563
4564         if( xIndex < configNUM_THREAD_LOCAL_STORAGE_POINTERS )
4565         {
4566             pxTCB = prvGetTCBFromHandle( xTaskToSet );
4567             configASSERT( pxTCB != NULL );
4568             pxTCB->pvThreadLocalStoragePointers[ xIndex ] = pvValue;
4569         }
4570     }
4571
4572 #endif /* configNUM_THREAD_LOCAL_STORAGE_POINTERS */
4573 /*-----------------------------------------------------------*/
4574
4575 #if ( configNUM_THREAD_LOCAL_STORAGE_POINTERS != 0 )
4576
4577     void * pvTaskGetThreadLocalStoragePointer( TaskHandle_t xTaskToQuery,
4578                                                BaseType_t xIndex )
4579     {
4580         void * pvReturn = NULL;
4581         TCB_t * pxTCB;
4582
4583         if( xIndex < configNUM_THREAD_LOCAL_STORAGE_POINTERS )
4584         {
4585             pxTCB = prvGetTCBFromHandle( xTaskToQuery );
4586             pvReturn = pxTCB->pvThreadLocalStoragePointers[ xIndex ];
4587         }
4588         else
4589         {
4590             pvReturn = NULL;
4591         }
4592
4593         return pvReturn;
4594     }
4595
4596 #endif /* configNUM_THREAD_LOCAL_STORAGE_POINTERS */
4597 /*-----------------------------------------------------------*/
4598
4599 #if ( portUSING_MPU_WRAPPERS == 1 )
4600
4601     void vTaskAllocateMPURegions( TaskHandle_t xTaskToModify,
4602                                   const MemoryRegion_t * const xRegions )
4603     {
4604         TCB_t * pxTCB;
4605
4606         /* If null is passed in here then we are modifying the MPU settings of
4607          * the calling task. */
4608         pxTCB = prvGetTCBFromHandle( xTaskToModify );
4609
4610         vPortStoreTaskMPUSettings( &( pxTCB->xMPUSettings ), xRegions, NULL, 0 );
4611     }
4612
4613 #endif /* portUSING_MPU_WRAPPERS */
4614 /*-----------------------------------------------------------*/
4615
4616 static void prvInitialiseTaskLists( void )
4617 {
4618     UBaseType_t uxPriority;
4619
4620     for( uxPriority = ( UBaseType_t ) 0U; uxPriority < ( UBaseType_t ) configMAX_PRIORITIES; uxPriority++ )
4621     {
4622         vListInitialise( &( pxReadyTasksLists[ uxPriority ] ) );
4623     }
4624
4625     vListInitialise( &xDelayedTaskList1 );
4626     vListInitialise( &xDelayedTaskList2 );
4627     vListInitialise( &xPendingReadyList );
4628
4629     #if ( INCLUDE_vTaskDelete == 1 )
4630         {
4631             vListInitialise( &xTasksWaitingTermination );
4632         }
4633     #endif /* INCLUDE_vTaskDelete */
4634
4635     #if ( INCLUDE_vTaskSuspend == 1 )
4636         {
4637             vListInitialise( &xSuspendedTaskList );
4638         }
4639     #endif /* INCLUDE_vTaskSuspend */
4640
4641     /* Start with pxDelayedTaskList using list1 and the pxOverflowDelayedTaskList
4642      * using list2. */
4643     pxDelayedTaskList = &xDelayedTaskList1;
4644     pxOverflowDelayedTaskList = &xDelayedTaskList2;
4645 }
4646 /*-----------------------------------------------------------*/
4647
4648 static void prvCheckTasksWaitingTermination( void )
4649 {
4650     /** THIS FUNCTION IS CALLED FROM THE RTOS IDLE TASK **/
4651
4652     #if ( INCLUDE_vTaskDelete == 1 )
4653         {
4654             TCB_t * pxTCB;
4655
4656             /* uxDeletedTasksWaitingCleanUp is used to prevent taskENTER_CRITICAL()
4657              * being called too often in the idle task. */
4658             while( uxDeletedTasksWaitingCleanUp > ( UBaseType_t ) 0U )
4659             {
4660                 taskENTER_CRITICAL();
4661                 {
4662                     /* Since we are SMP, multiple idles can be running simultaneously
4663                      * and we need to check that other idles did not cleanup while we were
4664                      * waiting to enter the critical section */
4665                     if( uxDeletedTasksWaitingCleanUp > ( UBaseType_t ) 0U )
4666                     {
4667                         pxTCB = listGET_OWNER_OF_HEAD_ENTRY( ( &xTasksWaitingTermination ) ); /*lint !e9079 void * is used as this macro is used with timers and co-routines too.  Alignment is known to be fine as the type of the pointer stored and retrieved is the same. */
4668
4669                         if( pxTCB->xTaskRunState == taskTASK_NOT_RUNNING )
4670                         {
4671                             ( void ) uxListRemove( &( pxTCB->xStateListItem ) );
4672                             --uxCurrentNumberOfTasks;
4673                             --uxDeletedTasksWaitingCleanUp;
4674                             prvDeleteTCB( pxTCB );
4675                         }
4676                         else
4677                         {
4678                             /* The TCB to be deleted still has not yet been switched out
4679                              * by the scheduler, so we will just exit this loop early and
4680                              * try again next time. */
4681                             taskEXIT_CRITICAL();
4682                             break;
4683                         }
4684                     }
4685                 }
4686                 taskEXIT_CRITICAL();
4687             }
4688         }
4689     #endif /* INCLUDE_vTaskDelete */
4690 }
4691 /*-----------------------------------------------------------*/
4692
4693 #if ( configUSE_TRACE_FACILITY == 1 )
4694
4695     void vTaskGetInfo( TaskHandle_t xTask,
4696                        TaskStatus_t * pxTaskStatus,
4697                        BaseType_t xGetFreeStackSpace,
4698                        eTaskState eState )
4699     {
4700         TCB_t * pxTCB;
4701
4702         /* xTask is NULL then get the state of the calling task. */
4703         pxTCB = prvGetTCBFromHandle( xTask );
4704
4705         pxTaskStatus->xHandle = ( TaskHandle_t ) pxTCB;
4706         pxTaskStatus->pcTaskName = ( const char * ) &( pxTCB->pcTaskName[ 0 ] );
4707         pxTaskStatus->uxCurrentPriority = pxTCB->uxPriority;
4708         pxTaskStatus->pxStackBase = pxTCB->pxStack;
4709         pxTaskStatus->xTaskNumber = pxTCB->uxTCBNumber;
4710
4711         #if ( configUSE_MUTEXES == 1 )
4712             {
4713                 pxTaskStatus->uxBasePriority = pxTCB->uxBasePriority;
4714             }
4715         #else
4716             {
4717                 pxTaskStatus->uxBasePriority = 0;
4718             }
4719         #endif
4720
4721         #if ( configGENERATE_RUN_TIME_STATS == 1 )
4722             {
4723                 pxTaskStatus->ulRunTimeCounter = pxTCB->ulRunTimeCounter;
4724             }
4725         #else
4726             {
4727                 pxTaskStatus->ulRunTimeCounter = 0;
4728             }
4729         #endif
4730
4731         /* Obtaining the task state is a little fiddly, so is only done if the
4732          * value of eState passed into this function is eInvalid - otherwise the
4733          * state is just set to whatever is passed in. */
4734         if( eState != eInvalid )
4735         {
4736             if( taskTASK_IS_RUNNING( pxTCB->xTaskRunState ) )
4737             {
4738                 pxTaskStatus->eCurrentState = eRunning;
4739             }
4740             else
4741             {
4742                 pxTaskStatus->eCurrentState = eState;
4743
4744                 #if ( INCLUDE_vTaskSuspend == 1 )
4745                     {
4746                         /* If the task is in the suspended list then there is a
4747                          *  chance it is actually just blocked indefinitely - so really
4748                          *  it should be reported as being in the Blocked state. */
4749                         if( eState == eSuspended )
4750                         {
4751                             vTaskSuspendAll();
4752                             {
4753                                 if( listLIST_ITEM_CONTAINER( &( pxTCB->xEventListItem ) ) != NULL )
4754                                 {
4755                                     pxTaskStatus->eCurrentState = eBlocked;
4756                                 }
4757                             }
4758                             ( void ) xTaskResumeAll();
4759                         }
4760                     }
4761                 #endif /* INCLUDE_vTaskSuspend */
4762             }
4763         }
4764         else
4765         {
4766             pxTaskStatus->eCurrentState = eTaskGetState( pxTCB );
4767         }
4768
4769         /* Obtaining the stack space takes some time, so the xGetFreeStackSpace
4770          * parameter is provided to allow it to be skipped. */
4771         if( xGetFreeStackSpace != pdFALSE )
4772         {
4773             #if ( portSTACK_GROWTH > 0 )
4774                 {
4775                     pxTaskStatus->usStackHighWaterMark = prvTaskCheckFreeStackSpace( ( uint8_t * ) pxTCB->pxEndOfStack );
4776                 }
4777             #else
4778                 {
4779                     pxTaskStatus->usStackHighWaterMark = prvTaskCheckFreeStackSpace( ( uint8_t * ) pxTCB->pxStack );
4780                 }
4781             #endif
4782         }
4783         else
4784         {
4785             pxTaskStatus->usStackHighWaterMark = 0;
4786         }
4787     }
4788
4789 #endif /* configUSE_TRACE_FACILITY */
4790 /*-----------------------------------------------------------*/
4791
4792 #if ( configUSE_TRACE_FACILITY == 1 )
4793
4794     static UBaseType_t prvListTasksWithinSingleList( TaskStatus_t * pxTaskStatusArray,
4795                                                      List_t * pxList,
4796                                                      eTaskState eState )
4797     {
4798         configLIST_VOLATILE TCB_t * pxNextTCB, * pxFirstTCB;
4799         UBaseType_t uxTask = 0;
4800
4801         if( listCURRENT_LIST_LENGTH( pxList ) > ( UBaseType_t ) 0 )
4802         {
4803             listGET_OWNER_OF_NEXT_ENTRY( pxFirstTCB, pxList ); /*lint !e9079 void * is used as this macro is used with timers and co-routines too.  Alignment is known to be fine as the type of the pointer stored and retrieved is the same. */
4804
4805             /* Populate an TaskStatus_t structure within the
4806              * pxTaskStatusArray array for each task that is referenced from
4807              * pxList.  See the definition of TaskStatus_t in task.h for the
4808              * meaning of each TaskStatus_t structure member. */
4809             do
4810             {
4811                 listGET_OWNER_OF_NEXT_ENTRY( pxNextTCB, pxList ); /*lint !e9079 void * is used as this macro is used with timers and co-routines too.  Alignment is known to be fine as the type of the pointer stored and retrieved is the same. */
4812                 vTaskGetInfo( ( TaskHandle_t ) pxNextTCB, &( pxTaskStatusArray[ uxTask ] ), pdTRUE, eState );
4813                 uxTask++;
4814             } while( pxNextTCB != pxFirstTCB );
4815         }
4816         else
4817         {
4818             mtCOVERAGE_TEST_MARKER();
4819         }
4820
4821         return uxTask;
4822     }
4823
4824 #endif /* configUSE_TRACE_FACILITY */
4825 /*-----------------------------------------------------------*/
4826
4827 #if ( ( configUSE_TRACE_FACILITY == 1 ) || ( INCLUDE_uxTaskGetStackHighWaterMark == 1 ) || ( INCLUDE_uxTaskGetStackHighWaterMark2 == 1 ) )
4828
4829     static configSTACK_DEPTH_TYPE prvTaskCheckFreeStackSpace( const uint8_t * pucStackByte )
4830     {
4831         uint32_t ulCount = 0U;
4832
4833         while( *pucStackByte == ( uint8_t ) tskSTACK_FILL_BYTE )
4834         {
4835             pucStackByte -= portSTACK_GROWTH;
4836             ulCount++;
4837         }
4838
4839         ulCount /= ( uint32_t ) sizeof( StackType_t ); /*lint !e961 Casting is not redundant on smaller architectures. */
4840
4841         return ( configSTACK_DEPTH_TYPE ) ulCount;
4842     }
4843
4844 #endif /* ( ( configUSE_TRACE_FACILITY == 1 ) || ( INCLUDE_uxTaskGetStackHighWaterMark == 1 ) || ( INCLUDE_uxTaskGetStackHighWaterMark2 == 1 ) ) */
4845 /*-----------------------------------------------------------*/
4846
4847 #if ( INCLUDE_uxTaskGetStackHighWaterMark2 == 1 )
4848
4849 /* uxTaskGetStackHighWaterMark() and uxTaskGetStackHighWaterMark2() are the
4850  * same except for their return type.  Using configSTACK_DEPTH_TYPE allows the
4851  * user to determine the return type.  It gets around the problem of the value
4852  * overflowing on 8-bit types without breaking backward compatibility for
4853  * applications that expect an 8-bit return type. */
4854     configSTACK_DEPTH_TYPE uxTaskGetStackHighWaterMark2( TaskHandle_t xTask )
4855     {
4856         TCB_t * pxTCB;
4857         uint8_t * pucEndOfStack;
4858         configSTACK_DEPTH_TYPE uxReturn;
4859
4860         /* uxTaskGetStackHighWaterMark() and uxTaskGetStackHighWaterMark2() are
4861          * the same except for their return type.  Using configSTACK_DEPTH_TYPE
4862          * allows the user to determine the return type.  It gets around the
4863          * problem of the value overflowing on 8-bit types without breaking
4864          * backward compatibility for applications that expect an 8-bit return
4865          * type. */
4866
4867         pxTCB = prvGetTCBFromHandle( xTask );
4868
4869         #if portSTACK_GROWTH < 0
4870             {
4871                 pucEndOfStack = ( uint8_t * ) pxTCB->pxStack;
4872             }
4873         #else
4874             {
4875                 pucEndOfStack = ( uint8_t * ) pxTCB->pxEndOfStack;
4876             }
4877         #endif
4878
4879         uxReturn = prvTaskCheckFreeStackSpace( pucEndOfStack );
4880
4881         return uxReturn;
4882     }
4883
4884 #endif /* INCLUDE_uxTaskGetStackHighWaterMark2 */
4885 /*-----------------------------------------------------------*/
4886
4887 #if ( INCLUDE_uxTaskGetStackHighWaterMark == 1 )
4888
4889     UBaseType_t uxTaskGetStackHighWaterMark( TaskHandle_t xTask )
4890     {
4891         TCB_t * pxTCB;
4892         uint8_t * pucEndOfStack;
4893         UBaseType_t uxReturn;
4894
4895         pxTCB = prvGetTCBFromHandle( xTask );
4896
4897         #if portSTACK_GROWTH < 0
4898             {
4899                 pucEndOfStack = ( uint8_t * ) pxTCB->pxStack;
4900             }
4901         #else
4902             {
4903                 pucEndOfStack = ( uint8_t * ) pxTCB->pxEndOfStack;
4904             }
4905         #endif
4906
4907         uxReturn = ( UBaseType_t ) prvTaskCheckFreeStackSpace( pucEndOfStack );
4908
4909         return uxReturn;
4910     }
4911
4912 #endif /* INCLUDE_uxTaskGetStackHighWaterMark */
4913 /*-----------------------------------------------------------*/
4914
4915 #if ( INCLUDE_vTaskDelete == 1 )
4916
4917     static void prvDeleteTCB( TCB_t * pxTCB )
4918     {
4919         /* This call is required specifically for the TriCore port.  It must be
4920          * above the vPortFree() calls.  The call is also used by ports/demos that
4921          * want to allocate and clean RAM statically. */
4922         portCLEAN_UP_TCB( pxTCB );
4923
4924         /* Free up the memory allocated by the scheduler for the task.  It is up
4925          * to the task to free any memory allocated at the application level.
4926          * See the third party link http://www.nadler.com/embedded/newlibAndFreeRTOS.html
4927          * for additional information. */
4928         #if ( configUSE_NEWLIB_REENTRANT == 1 )
4929             {
4930                 _reclaim_reent( &( pxTCB->xNewLib_reent ) );
4931             }
4932         #endif /* configUSE_NEWLIB_REENTRANT */
4933
4934         #if ( ( configSUPPORT_DYNAMIC_ALLOCATION == 1 ) && ( configSUPPORT_STATIC_ALLOCATION == 0 ) && ( portUSING_MPU_WRAPPERS == 0 ) )
4935             {
4936                 /* The task can only have been allocated dynamically - free both
4937                  * the stack and TCB. */
4938                 vPortFreeStack( pxTCB->pxStack );
4939                 vPortFree( pxTCB );
4940             }
4941         #elif ( tskSTATIC_AND_DYNAMIC_ALLOCATION_POSSIBLE != 0 ) /*lint !e731 !e9029 Macro has been consolidated for readability reasons. */
4942             {
4943                 /* The task could have been allocated statically or dynamically, so
4944                  * check what was statically allocated before trying to free the
4945                  * memory. */
4946                 if( pxTCB->ucStaticallyAllocated == tskDYNAMICALLY_ALLOCATED_STACK_AND_TCB )
4947                 {
4948                     /* Both the stack and TCB were allocated dynamically, so both
4949                      * must be freed. */
4950                     vPortFreeStack( pxTCB->pxStack );
4951                     vPortFree( pxTCB );
4952                 }
4953                 else if( pxTCB->ucStaticallyAllocated == tskSTATICALLY_ALLOCATED_STACK_ONLY )
4954                 {
4955                     /* Only the stack was statically allocated, so the TCB is the
4956                      * only memory that must be freed. */
4957                     vPortFree( pxTCB );
4958                 }
4959                 else
4960                 {
4961                     /* Neither the stack nor the TCB were allocated dynamically, so
4962                      * nothing needs to be freed. */
4963                     configASSERT( pxTCB->ucStaticallyAllocated == tskSTATICALLY_ALLOCATED_STACK_AND_TCB );
4964                     mtCOVERAGE_TEST_MARKER();
4965                 }
4966             }
4967         #endif /* configSUPPORT_DYNAMIC_ALLOCATION */
4968     }
4969
4970 #endif /* INCLUDE_vTaskDelete */
4971 /*-----------------------------------------------------------*/
4972
4973 static void prvResetNextTaskUnblockTime( void )
4974 {
4975     if( listLIST_IS_EMPTY( pxDelayedTaskList ) != pdFALSE )
4976     {
4977         /* The new current delayed list is empty.  Set xNextTaskUnblockTime to
4978          * the maximum possible value so it is  extremely unlikely that the
4979          * if( xTickCount >= xNextTaskUnblockTime ) test will pass until
4980          * there is an item in the delayed list. */
4981         xNextTaskUnblockTime = portMAX_DELAY;
4982     }
4983     else
4984     {
4985         /* The new current delayed list is not empty, get the value of
4986          * the item at the head of the delayed list.  This is the time at
4987          * which the task at the head of the delayed list should be removed
4988          * from the Blocked state. */
4989         xNextTaskUnblockTime = listGET_ITEM_VALUE_OF_HEAD_ENTRY( pxDelayedTaskList );
4990     }
4991 }
4992 /*-----------------------------------------------------------*/
4993
4994 #if ( ( INCLUDE_xTaskGetCurrentTaskHandle == 1 ) || ( configUSE_MUTEXES == 1 ) )
4995
4996     TaskHandle_t xTaskGetCurrentTaskHandle( void )
4997     {
4998         TaskHandle_t xReturn;
4999         uint32_t ulState;
5000
5001         ulState = portDISABLE_INTERRUPTS();
5002         xReturn = pxCurrentTCBs[ portGET_CORE_ID() ];
5003         portRESTORE_INTERRUPTS( ulState );
5004
5005         return xReturn;
5006     }
5007
5008     TaskHandle_t xTaskGetCurrentTaskHandleCPU( UBaseType_t xCoreID )
5009     {
5010         TaskHandle_t xReturn = NULL;
5011
5012         if( taskVALID_CORE_ID( xCoreID ) != pdFALSE )
5013         {
5014             xReturn = pxCurrentTCBs[ xCoreID ];
5015         }
5016
5017         return xReturn;
5018     }
5019
5020 #endif /* ( ( INCLUDE_xTaskGetCurrentTaskHandle == 1 ) || ( configUSE_MUTEXES == 1 ) ) */
5021 /*-----------------------------------------------------------*/
5022
5023 #if ( ( INCLUDE_xTaskGetSchedulerState == 1 ) || ( configUSE_TIMERS == 1 ) )
5024
5025     BaseType_t xTaskGetSchedulerState( void )
5026     {
5027         BaseType_t xReturn;
5028
5029         if( xSchedulerRunning == pdFALSE )
5030         {
5031             xReturn = taskSCHEDULER_NOT_STARTED;
5032         }
5033         else
5034         {
5035             taskENTER_CRITICAL();
5036             {
5037                 if( uxSchedulerSuspended == ( UBaseType_t ) pdFALSE )
5038                 {
5039                     xReturn = taskSCHEDULER_RUNNING;
5040                 }
5041                 else
5042                 {
5043                     xReturn = taskSCHEDULER_SUSPENDED;
5044                 }
5045             }
5046             taskEXIT_CRITICAL();
5047         }
5048
5049         return xReturn;
5050     }
5051
5052 #endif /* ( ( INCLUDE_xTaskGetSchedulerState == 1 ) || ( configUSE_TIMERS == 1 ) ) */
5053 /*-----------------------------------------------------------*/
5054
5055 #if ( configUSE_MUTEXES == 1 )
5056
5057     BaseType_t xTaskPriorityInherit( TaskHandle_t const pxMutexHolder )
5058     {
5059         TCB_t * const pxMutexHolderTCB = pxMutexHolder;
5060         BaseType_t xReturn = pdFALSE;
5061
5062         /* If the mutex was given back by an interrupt while the queue was
5063          * locked then the mutex holder might now be NULL.  _RB_ Is this still
5064          * needed as interrupts can no longer use mutexes? */
5065         if( pxMutexHolder != NULL )
5066         {
5067             /* If the holder of the mutex has a priority below the priority of
5068              * the task attempting to obtain the mutex then it will temporarily
5069              * inherit the priority of the task attempting to obtain the mutex. */
5070             if( pxMutexHolderTCB->uxPriority < pxCurrentTCB->uxPriority )
5071             {
5072                 /* Adjust the mutex holder state to account for its new
5073                  * priority.  Only reset the event list item value if the value is
5074                  * not being used for anything else. */
5075                 if( ( listGET_LIST_ITEM_VALUE( &( pxMutexHolderTCB->xEventListItem ) ) & taskEVENT_LIST_ITEM_VALUE_IN_USE ) == 0UL )
5076                 {
5077                     listSET_LIST_ITEM_VALUE( &( pxMutexHolderTCB->xEventListItem ), ( TickType_t ) configMAX_PRIORITIES - ( TickType_t ) pxCurrentTCB->uxPriority ); /*lint !e961 MISRA exception as the casts are only redundant for some ports. */
5078                 }
5079                 else
5080                 {
5081                     mtCOVERAGE_TEST_MARKER();
5082                 }
5083
5084                 /* If the task being modified is in the ready state it will need
5085                  * to be moved into a new list. */
5086                 if( listIS_CONTAINED_WITHIN( &( pxReadyTasksLists[ pxMutexHolderTCB->uxPriority ] ), &( pxMutexHolderTCB->xStateListItem ) ) != pdFALSE )
5087                 {
5088                     if( uxListRemove( &( pxMutexHolderTCB->xStateListItem ) ) == ( UBaseType_t ) 0 )
5089                     {
5090                         /* It is known that the task is in its ready list so
5091                          * there is no need to check again and the port level
5092                          * reset macro can be called directly. */
5093                         portRESET_READY_PRIORITY( pxMutexHolderTCB->uxPriority, uxTopReadyPriority );
5094                     }
5095                     else
5096                     {
5097                         mtCOVERAGE_TEST_MARKER();
5098                     }
5099
5100                     /* Inherit the priority before being moved into the new list. */
5101                     pxMutexHolderTCB->uxPriority = pxCurrentTCB->uxPriority;
5102                     prvAddTaskToReadyList( pxMutexHolderTCB );
5103                 }
5104                 else
5105                 {
5106                     /* Just inherit the priority. */
5107                     pxMutexHolderTCB->uxPriority = pxCurrentTCB->uxPriority;
5108                 }
5109
5110                 traceTASK_PRIORITY_INHERIT( pxMutexHolderTCB, pxCurrentTCB->uxPriority );
5111
5112                 /* Inheritance occurred. */
5113                 xReturn = pdTRUE;
5114             }
5115             else
5116             {
5117                 if( pxMutexHolderTCB->uxBasePriority < pxCurrentTCB->uxPriority )
5118                 {
5119                     /* The base priority of the mutex holder is lower than the
5120                      * priority of the task attempting to take the mutex, but the
5121                      * current priority of the mutex holder is not lower than the
5122                      * priority of the task attempting to take the mutex.
5123                      * Therefore the mutex holder must have already inherited a
5124                      * priority, but inheritance would have occurred if that had
5125                      * not been the case. */
5126                     xReturn = pdTRUE;
5127                 }
5128                 else
5129                 {
5130                     mtCOVERAGE_TEST_MARKER();
5131                 }
5132             }
5133         }
5134         else
5135         {
5136             mtCOVERAGE_TEST_MARKER();
5137         }
5138
5139         return xReturn;
5140     }
5141
5142 #endif /* configUSE_MUTEXES */
5143 /*-----------------------------------------------------------*/
5144
5145 #if ( configUSE_MUTEXES == 1 )
5146
5147     BaseType_t xTaskPriorityDisinherit( TaskHandle_t const pxMutexHolder )
5148     {
5149         TCB_t * const pxTCB = pxMutexHolder;
5150         BaseType_t xReturn = pdFALSE;
5151
5152         if( pxMutexHolder != NULL )
5153         {
5154             /* A task can only have an inherited priority if it holds the mutex.
5155              * If the mutex is held by a task then it cannot be given from an
5156              * interrupt, and if a mutex is given by the holding task then it must
5157              * be the running state task. */
5158             configASSERT( pxTCB == pxCurrentTCB );
5159             configASSERT( pxTCB->uxMutexesHeld );
5160             ( pxTCB->uxMutexesHeld )--;
5161
5162             /* Has the holder of the mutex inherited the priority of another
5163              * task? */
5164             if( pxTCB->uxPriority != pxTCB->uxBasePriority )
5165             {
5166                 /* Only disinherit if no other mutexes are held. */
5167                 if( pxTCB->uxMutexesHeld == ( UBaseType_t ) 0 )
5168                 {
5169                     /* A task can only have an inherited priority if it holds
5170                      * the mutex.  If the mutex is held by a task then it cannot be
5171                      * given from an interrupt, and if a mutex is given by the
5172                      * holding task then it must be the running state task.  Remove
5173                      * the holding task from the ready list. */
5174                     if( uxListRemove( &( pxTCB->xStateListItem ) ) == ( UBaseType_t ) 0 )
5175                     {
5176                         portRESET_READY_PRIORITY( pxTCB->uxPriority, uxTopReadyPriority );
5177                     }
5178                     else
5179                     {
5180                         mtCOVERAGE_TEST_MARKER();
5181                     }
5182
5183                     /* Disinherit the priority before adding the task into the
5184                      * new  ready list. */
5185                     traceTASK_PRIORITY_DISINHERIT( pxTCB, pxTCB->uxBasePriority );
5186                     pxTCB->uxPriority = pxTCB->uxBasePriority;
5187
5188                     /* Reset the event list item value.  It cannot be in use for
5189                      * any other purpose if this task is running, and it must be
5190                      * running to give back the mutex. */
5191                     listSET_LIST_ITEM_VALUE( &( pxTCB->xEventListItem ), ( TickType_t ) configMAX_PRIORITIES - ( TickType_t ) pxTCB->uxPriority ); /*lint !e961 MISRA exception as the casts are only redundant for some ports. */
5192                     prvAddTaskToReadyList( pxTCB );
5193
5194                     /* Return true to indicate that a context switch is required.
5195                      * This is only actually required in the corner case whereby
5196                      * multiple mutexes were held and the mutexes were given back
5197                      * in an order different to that in which they were taken.
5198                      * If a context switch did not occur when the first mutex was
5199                      * returned, even if a task was waiting on it, then a context
5200                      * switch should occur when the last mutex is returned whether
5201                      * a task is waiting on it or not. */
5202                     xReturn = pdTRUE;
5203                 }
5204                 else
5205                 {
5206                     mtCOVERAGE_TEST_MARKER();
5207                 }
5208             }
5209             else
5210             {
5211                 mtCOVERAGE_TEST_MARKER();
5212             }
5213         }
5214         else
5215         {
5216             mtCOVERAGE_TEST_MARKER();
5217         }
5218
5219         return xReturn;
5220     }
5221
5222 #endif /* configUSE_MUTEXES */
5223 /*-----------------------------------------------------------*/
5224
5225 #if ( configUSE_MUTEXES == 1 )
5226
5227     void vTaskPriorityDisinheritAfterTimeout( TaskHandle_t const pxMutexHolder,
5228                                               UBaseType_t uxHighestPriorityWaitingTask )
5229     {
5230         TCB_t * const pxTCB = pxMutexHolder;
5231         UBaseType_t uxPriorityUsedOnEntry, uxPriorityToUse;
5232         const UBaseType_t uxOnlyOneMutexHeld = ( UBaseType_t ) 1;
5233
5234         if( pxMutexHolder != NULL )
5235         {
5236             /* If pxMutexHolder is not NULL then the holder must hold at least
5237              * one mutex. */
5238             configASSERT( pxTCB->uxMutexesHeld );
5239
5240             /* Determine the priority to which the priority of the task that
5241              * holds the mutex should be set.  This will be the greater of the
5242              * holding task's base priority and the priority of the highest
5243              * priority task that is waiting to obtain the mutex. */
5244             if( pxTCB->uxBasePriority < uxHighestPriorityWaitingTask )
5245             {
5246                 uxPriorityToUse = uxHighestPriorityWaitingTask;
5247             }
5248             else
5249             {
5250                 uxPriorityToUse = pxTCB->uxBasePriority;
5251             }
5252
5253             /* Does the priority need to change? */
5254             if( pxTCB->uxPriority != uxPriorityToUse )
5255             {
5256                 /* Only disinherit if no other mutexes are held.  This is a
5257                  * simplification in the priority inheritance implementation.  If
5258                  * the task that holds the mutex is also holding other mutexes then
5259                  * the other mutexes may have caused the priority inheritance. */
5260                 if( pxTCB->uxMutexesHeld == uxOnlyOneMutexHeld )
5261                 {
5262                     /* If a task has timed out because it already holds the
5263                      * mutex it was trying to obtain then it cannot of inherited
5264                      * its own priority. */
5265                     configASSERT( pxTCB != pxCurrentTCB );
5266
5267                     /* Disinherit the priority, remembering the previous
5268                      * priority to facilitate determining the subject task's
5269                      * state. */
5270                     traceTASK_PRIORITY_DISINHERIT( pxTCB, uxPriorityToUse );
5271                     uxPriorityUsedOnEntry = pxTCB->uxPriority;
5272                     pxTCB->uxPriority = uxPriorityToUse;
5273
5274                     /* Only reset the event list item value if the value is not
5275                      * being used for anything else. */
5276                     if( ( listGET_LIST_ITEM_VALUE( &( pxTCB->xEventListItem ) ) & taskEVENT_LIST_ITEM_VALUE_IN_USE ) == 0UL )
5277                     {
5278                         listSET_LIST_ITEM_VALUE( &( pxTCB->xEventListItem ), ( TickType_t ) configMAX_PRIORITIES - ( TickType_t ) uxPriorityToUse ); /*lint !e961 MISRA exception as the casts are only redundant for some ports. */
5279                     }
5280                     else
5281                     {
5282                         mtCOVERAGE_TEST_MARKER();
5283                     }
5284
5285                     /* If the running task is not the task that holds the mutex
5286                      * then the task that holds the mutex could be in either the
5287                      * Ready, Blocked or Suspended states.  Only remove the task
5288                      * from its current state list if it is in the Ready state as
5289                      * the task's priority is going to change and there is one
5290                      * Ready list per priority. */
5291                     if( listIS_CONTAINED_WITHIN( &( pxReadyTasksLists[ uxPriorityUsedOnEntry ] ), &( pxTCB->xStateListItem ) ) != pdFALSE )
5292                     {
5293                         if( uxListRemove( &( pxTCB->xStateListItem ) ) == ( UBaseType_t ) 0 )
5294                         {
5295                             /* It is known that the task is in its ready list so
5296                              * there is no need to check again and the port level
5297                              * reset macro can be called directly. */
5298                             portRESET_READY_PRIORITY( pxTCB->uxPriority, uxTopReadyPriority );
5299                         }
5300                         else
5301                         {
5302                             mtCOVERAGE_TEST_MARKER();
5303                         }
5304
5305                         prvAddTaskToReadyList( pxTCB );
5306                     }
5307                     else
5308                     {
5309                         mtCOVERAGE_TEST_MARKER();
5310                     }
5311                 }
5312                 else
5313                 {
5314                     mtCOVERAGE_TEST_MARKER();
5315                 }
5316             }
5317             else
5318             {
5319                 mtCOVERAGE_TEST_MARKER();
5320             }
5321         }
5322         else
5323         {
5324             mtCOVERAGE_TEST_MARKER();
5325         }
5326     }
5327
5328 #endif /* configUSE_MUTEXES */
5329 /*-----------------------------------------------------------*/
5330
5331 /*
5332  * If not in a critical section then yield immediately.
5333  * Otherwise set xYieldPending to true to wait to
5334  * yield until exiting the critical section.
5335  */
5336 void vTaskYieldWithinAPI( void )
5337 {
5338     if( pxCurrentTCB->uxCriticalNesting == 0U )
5339     {
5340         portYIELD();
5341     }
5342     else
5343     {
5344         xYieldPendings[ portGET_CORE_ID() ] = pdTRUE;
5345     }
5346 }
5347 /*-----------------------------------------------------------*/
5348
5349 #if ( portCRITICAL_NESTING_IN_TCB == 1 )
5350
5351     void vTaskEnterCritical( void )
5352     {
5353         portDISABLE_INTERRUPTS();
5354
5355         if( xSchedulerRunning != pdFALSE )
5356         {
5357             if( pxCurrentTCB->uxCriticalNesting == 0U )
5358             {
5359                 if( portCHECK_IF_IN_ISR() == pdFALSE )
5360                 {
5361                     portGET_TASK_LOCK();
5362                 }
5363
5364                 portGET_ISR_LOCK();
5365             }
5366
5367             ( pxCurrentTCB->uxCriticalNesting )++;
5368
5369             /* This should now be interrupt safe. The only time there would be
5370              * a problem is if this is called before a context switch and
5371              * vTaskExitCritical() is called after pxCurrentTCB changes. Therefore
5372              * this should not be used within vTaskSwitchContext(). */
5373
5374             if( ( uxSchedulerSuspended == 0U ) && ( pxCurrentTCB->uxCriticalNesting == 1U ) )
5375             {
5376                 prvCheckForRunStateChange();
5377             }
5378         }
5379         else
5380         {
5381             mtCOVERAGE_TEST_MARKER();
5382         }
5383     }
5384
5385 #endif /* portCRITICAL_NESTING_IN_TCB */
5386 /*-----------------------------------------------------------*/
5387
5388 #if ( portCRITICAL_NESTING_IN_TCB == 1 )
5389
5390     void vTaskExitCritical( void )
5391     {
5392         if( xSchedulerRunning != pdFALSE )
5393         {
5394             /* If pxCurrentTCB->uxCriticalNesting is zero then this function
5395              * does not match a previous call to vTaskEnterCritical(). */
5396             configASSERT( pxCurrentTCB->uxCriticalNesting > 0U );
5397
5398             if( pxCurrentTCB->uxCriticalNesting > 0U )
5399             {
5400                 ( pxCurrentTCB->uxCriticalNesting )--;
5401
5402                 if( pxCurrentTCB->uxCriticalNesting == 0U )
5403                 {
5404                     portRELEASE_ISR_LOCK();
5405
5406                     if( portCHECK_IF_IN_ISR() == pdFALSE )
5407                     {
5408                         portRELEASE_TASK_LOCK();
5409                         portENABLE_INTERRUPTS();
5410
5411                         /* When a task yields in a critical section it just sets
5412                          * xYieldPending to true. So now that we have exited the
5413                          * critical section check if xYieldPending is true, and
5414                          * if so yield. */
5415                         if( xYieldPending != pdFALSE )
5416                         {
5417                             portYIELD();
5418                         }
5419                     }
5420                     else
5421                     {
5422                         /* In an ISR we don't hold the task lock and don't
5423                          * need to yield. Yield will happen if necessary when
5424                          * the application ISR calls portEND_SWITCHING_ISR() */
5425                         mtCOVERAGE_TEST_MARKER();
5426                     }
5427                 }
5428                 else
5429                 {
5430                     mtCOVERAGE_TEST_MARKER();
5431                 }
5432             }
5433             else
5434             {
5435                 mtCOVERAGE_TEST_MARKER();
5436             }
5437         }
5438         else
5439         {
5440             mtCOVERAGE_TEST_MARKER();
5441         }
5442     }
5443
5444 #endif /* portCRITICAL_NESTING_IN_TCB */
5445 /*-----------------------------------------------------------*/
5446
5447 #if ( ( configUSE_TRACE_FACILITY == 1 ) && ( configUSE_STATS_FORMATTING_FUNCTIONS > 0 ) )
5448
5449     static char * prvWriteNameToBuffer( char * pcBuffer,
5450                                         const char * pcTaskName )
5451     {
5452         size_t x;
5453
5454         /* Start by copying the entire string. */
5455         strcpy( pcBuffer, pcTaskName );
5456
5457         /* Pad the end of the string with spaces to ensure columns line up when
5458          * printed out. */
5459         for( x = strlen( pcBuffer ); x < ( size_t ) ( configMAX_TASK_NAME_LEN - 1 ); x++ )
5460         {
5461             pcBuffer[ x ] = ' ';
5462         }
5463
5464         /* Terminate. */
5465         pcBuffer[ x ] = ( char ) 0x00;
5466
5467         /* Return the new end of string. */
5468         return &( pcBuffer[ x ] );
5469     }
5470
5471 #endif /* ( configUSE_TRACE_FACILITY == 1 ) && ( configUSE_STATS_FORMATTING_FUNCTIONS > 0 ) */
5472 /*-----------------------------------------------------------*/
5473
5474 #if ( ( configUSE_TRACE_FACILITY == 1 ) && ( configUSE_STATS_FORMATTING_FUNCTIONS > 0 ) && ( configSUPPORT_DYNAMIC_ALLOCATION == 1 ) )
5475
5476     void vTaskList( char * pcWriteBuffer )
5477     {
5478         TaskStatus_t * pxTaskStatusArray;
5479         UBaseType_t uxArraySize, x;
5480         char cStatus;
5481
5482         /*
5483          * PLEASE NOTE:
5484          *
5485          * This function is provided for convenience only, and is used by many
5486          * of the demo applications.  Do not consider it to be part of the
5487          * scheduler.
5488          *
5489          * vTaskList() calls uxTaskGetSystemState(), then formats part of the
5490          * uxTaskGetSystemState() output into a human readable table that
5491          * displays task: names, states, priority, stack usage and task number.
5492          * Stack usage specified as the number of unused StackType_t words stack can hold
5493          * on top of stack - not the number of bytes.
5494          *
5495          * vTaskList() has a dependency on the sprintf() C library function that
5496          * might bloat the code size, use a lot of stack, and provide different
5497          * results on different platforms.  An alternative, tiny, third party,
5498          * and limited functionality implementation of sprintf() is provided in
5499          * many of the FreeRTOS/Demo sub-directories in a file called
5500          * printf-stdarg.c (note printf-stdarg.c does not provide a full
5501          * snprintf() implementation!).
5502          *
5503          * It is recommended that production systems call uxTaskGetSystemState()
5504          * directly to get access to raw stats data, rather than indirectly
5505          * through a call to vTaskList().
5506          */
5507
5508
5509         /* Make sure the write buffer does not contain a string. */
5510         *pcWriteBuffer = ( char ) 0x00;
5511
5512         /* Take a snapshot of the number of tasks in case it changes while this
5513          * function is executing. */
5514         uxArraySize = uxCurrentNumberOfTasks;
5515
5516         /* Allocate an array index for each task.  NOTE!  if
5517          * configSUPPORT_DYNAMIC_ALLOCATION is set to 0 then pvPortMalloc() will
5518          * equate to NULL. */
5519         pxTaskStatusArray = pvPortMalloc( uxCurrentNumberOfTasks * sizeof( TaskStatus_t ) ); /*lint !e9079 All values returned by pvPortMalloc() have at least the alignment required by the MCU's stack and this allocation allocates a struct that has the alignment requirements of a pointer. */
5520
5521         if( pxTaskStatusArray != NULL )
5522         {
5523             /* Generate the (binary) data. */
5524             uxArraySize = uxTaskGetSystemState( pxTaskStatusArray, uxArraySize, NULL );
5525
5526             /* Create a human readable table from the binary data. */
5527             for( x = 0; x < uxArraySize; x++ )
5528             {
5529                 switch( pxTaskStatusArray[ x ].eCurrentState )
5530                 {
5531                     case eRunning:
5532                         cStatus = tskRUNNING_CHAR;
5533                         break;
5534
5535                     case eReady:
5536                         cStatus = tskREADY_CHAR;
5537                         break;
5538
5539                     case eBlocked:
5540                         cStatus = tskBLOCKED_CHAR;
5541                         break;
5542
5543                     case eSuspended:
5544                         cStatus = tskSUSPENDED_CHAR;
5545                         break;
5546
5547                     case eDeleted:
5548                         cStatus = tskDELETED_CHAR;
5549                         break;
5550
5551                     case eInvalid: /* Fall through. */
5552                     default:       /* Should not get here, but it is included
5553                                     * to prevent static checking errors. */
5554                         cStatus = ( char ) 0x00;
5555                         break;
5556                 }
5557
5558                 /* Write the task name to the string, padding with spaces so it
5559                  * can be printed in tabular form more easily. */
5560                 pcWriteBuffer = prvWriteNameToBuffer( pcWriteBuffer, pxTaskStatusArray[ x ].pcTaskName );
5561
5562                 /* Write the rest of the string. */
5563                 sprintf( pcWriteBuffer, "\t%c\t%u\t%u\t%u\r\n", cStatus, ( unsigned int ) pxTaskStatusArray[ x ].uxCurrentPriority, ( unsigned int ) pxTaskStatusArray[ x ].usStackHighWaterMark, ( unsigned int ) pxTaskStatusArray[ x ].xTaskNumber ); /*lint !e586 sprintf() allowed as this is compiled with many compilers and this is a utility function only - not part of the core kernel implementation. */
5564                 pcWriteBuffer += strlen( pcWriteBuffer );                                                                                                                                                                                                /*lint !e9016 Pointer arithmetic ok on char pointers especially as in this case where it best denotes the intent of the code. */
5565             }
5566
5567             /* Free the array again.  NOTE!  If configSUPPORT_DYNAMIC_ALLOCATION
5568              * is 0 then vPortFree() will be #defined to nothing. */
5569             vPortFree( pxTaskStatusArray );
5570         }
5571         else
5572         {
5573             mtCOVERAGE_TEST_MARKER();
5574         }
5575     }
5576
5577 #endif /* ( ( configUSE_TRACE_FACILITY == 1 ) && ( configUSE_STATS_FORMATTING_FUNCTIONS > 0 ) && ( configSUPPORT_DYNAMIC_ALLOCATION == 1 ) ) */
5578 /*----------------------------------------------------------*/
5579
5580 #if ( ( configGENERATE_RUN_TIME_STATS == 1 ) && ( configUSE_STATS_FORMATTING_FUNCTIONS > 0 ) && ( configSUPPORT_DYNAMIC_ALLOCATION == 1 ) )
5581
5582     void vTaskGetRunTimeStats( char * pcWriteBuffer )
5583     {
5584         TaskStatus_t * pxTaskStatusArray;
5585         UBaseType_t uxArraySize, x;
5586         uint32_t ulTotalTime, ulStatsAsPercentage;
5587
5588         #if ( configUSE_TRACE_FACILITY != 1 )
5589             {
5590                 #error configUSE_TRACE_FACILITY must also be set to 1 in FreeRTOSConfig.h to use vTaskGetRunTimeStats().
5591             }
5592         #endif
5593
5594         /*
5595          * PLEASE NOTE:
5596          *
5597          * This function is provided for convenience only, and is used by many
5598          * of the demo applications.  Do not consider it to be part of the
5599          * scheduler.
5600          *
5601          * vTaskGetRunTimeStats() calls uxTaskGetSystemState(), then formats part
5602          * of the uxTaskGetSystemState() output into a human readable table that
5603          * displays the amount of time each task has spent in the Running state
5604          * in both absolute and percentage terms.
5605          *
5606          * vTaskGetRunTimeStats() has a dependency on the sprintf() C library
5607          * function that might bloat the code size, use a lot of stack, and
5608          * provide different results on different platforms.  An alternative,
5609          * tiny, third party, and limited functionality implementation of
5610          * sprintf() is provided in many of the FreeRTOS/Demo sub-directories in
5611          * a file called printf-stdarg.c (note printf-stdarg.c does not provide
5612          * a full snprintf() implementation!).
5613          *
5614          * It is recommended that production systems call uxTaskGetSystemState()
5615          * directly to get access to raw stats data, rather than indirectly
5616          * through a call to vTaskGetRunTimeStats().
5617          */
5618
5619         /* Make sure the write buffer does not contain a string. */
5620         *pcWriteBuffer = ( char ) 0x00;
5621
5622         /* Take a snapshot of the number of tasks in case it changes while this
5623          * function is executing. */
5624         uxArraySize = uxCurrentNumberOfTasks;
5625
5626         /* Allocate an array index for each task.  NOTE!  If
5627          * configSUPPORT_DYNAMIC_ALLOCATION is set to 0 then pvPortMalloc() will
5628          * equate to NULL. */
5629         pxTaskStatusArray = pvPortMalloc( uxCurrentNumberOfTasks * sizeof( TaskStatus_t ) ); /*lint !e9079 All values returned by pvPortMalloc() have at least the alignment required by the MCU's stack and this allocation allocates a struct that has the alignment requirements of a pointer. */
5630
5631         if( pxTaskStatusArray != NULL )
5632         {
5633             /* Generate the (binary) data. */
5634             uxArraySize = uxTaskGetSystemState( pxTaskStatusArray, uxArraySize, &ulTotalTime );
5635
5636             /* For percentage calculations. */
5637             ulTotalTime /= 100UL;
5638
5639             /* Avoid divide by zero errors. */
5640             if( ulTotalTime > 0UL )
5641             {
5642                 /* Create a human readable table from the binary data. */
5643                 for( x = 0; x < uxArraySize; x++ )
5644                 {
5645                     /* What percentage of the total run time has the task used?
5646                      * This will always be rounded down to the nearest integer.
5647                      * ulTotalRunTimeDiv100 has already been divided by 100. */
5648                     ulStatsAsPercentage = pxTaskStatusArray[ x ].ulRunTimeCounter / ulTotalTime;
5649
5650                     /* Write the task name to the string, padding with
5651                      * spaces so it can be printed in tabular form more
5652                      * easily. */
5653                     pcWriteBuffer = prvWriteNameToBuffer( pcWriteBuffer, pxTaskStatusArray[ x ].pcTaskName );
5654
5655                     if( ulStatsAsPercentage > 0UL )
5656                     {
5657                         #ifdef portLU_PRINTF_SPECIFIER_REQUIRED
5658                             {
5659                                 sprintf( pcWriteBuffer, "\t%lu\t\t%lu%%\r\n", pxTaskStatusArray[ x ].ulRunTimeCounter, ulStatsAsPercentage );
5660                             }
5661                         #else
5662                             {
5663                                 /* sizeof( int ) == sizeof( long ) so a smaller
5664                                  * printf() library can be used. */
5665                                 sprintf( pcWriteBuffer, "\t%u\t\t%u%%\r\n", ( unsigned int ) pxTaskStatusArray[ x ].ulRunTimeCounter, ( unsigned int ) ulStatsAsPercentage ); /*lint !e586 sprintf() allowed as this is compiled with many compilers and this is a utility function only - not part of the core kernel implementation. */
5666                             }
5667                         #endif
5668                     }
5669                     else
5670                     {
5671                         /* If the percentage is zero here then the task has
5672                          * consumed less than 1% of the total run time. */
5673                         #ifdef portLU_PRINTF_SPECIFIER_REQUIRED
5674                             {
5675                                 sprintf( pcWriteBuffer, "\t%lu\t\t<1%%\r\n", pxTaskStatusArray[ x ].ulRunTimeCounter );
5676                             }
5677                         #else
5678                             {
5679                                 /* sizeof( int ) == sizeof( long ) so a smaller
5680                                  * printf() library can be used. */
5681                                 sprintf( pcWriteBuffer, "\t%u\t\t<1%%\r\n", ( unsigned int ) pxTaskStatusArray[ x ].ulRunTimeCounter ); /*lint !e586 sprintf() allowed as this is compiled with many compilers and this is a utility function only - not part of the core kernel implementation. */
5682                             }
5683                         #endif
5684                     }
5685
5686                     pcWriteBuffer += strlen( pcWriteBuffer ); /*lint !e9016 Pointer arithmetic ok on char pointers especially as in this case where it best denotes the intent of the code. */
5687                 }
5688             }
5689             else
5690             {
5691                 mtCOVERAGE_TEST_MARKER();
5692             }
5693
5694             /* Free the array again.  NOTE!  If configSUPPORT_DYNAMIC_ALLOCATION
5695              * is 0 then vPortFree() will be #defined to nothing. */
5696             vPortFree( pxTaskStatusArray );
5697         }
5698         else
5699         {
5700             mtCOVERAGE_TEST_MARKER();
5701         }
5702     }
5703
5704 #endif /* ( ( configGENERATE_RUN_TIME_STATS == 1 ) && ( configUSE_STATS_FORMATTING_FUNCTIONS > 0 ) && ( configSUPPORT_STATIC_ALLOCATION == 1 ) ) */
5705 /*-----------------------------------------------------------*/
5706
5707 TickType_t uxTaskResetEventItemValue( void )
5708 {
5709     TickType_t uxReturn;
5710
5711     uxReturn = listGET_LIST_ITEM_VALUE( &( pxCurrentTCB->xEventListItem ) );
5712
5713     /* Reset the event list item to its normal value - so it can be used with
5714      * queues and semaphores. */
5715     listSET_LIST_ITEM_VALUE( &( pxCurrentTCB->xEventListItem ), ( ( TickType_t ) configMAX_PRIORITIES - ( TickType_t ) pxCurrentTCB->uxPriority ) ); /*lint !e961 MISRA exception as the casts are only redundant for some ports. */
5716
5717     return uxReturn;
5718 }
5719 /*-----------------------------------------------------------*/
5720
5721 #if ( configUSE_MUTEXES == 1 )
5722
5723     TaskHandle_t pvTaskIncrementMutexHeldCount( void )
5724     {
5725         /* If xSemaphoreCreateMutex() is called before any tasks have been created
5726          * then pxCurrentTCB will be NULL. */
5727         if( pxCurrentTCB != NULL )
5728         {
5729             ( pxCurrentTCB->uxMutexesHeld )++;
5730         }
5731
5732         return pxCurrentTCB;
5733     }
5734
5735 #endif /* configUSE_MUTEXES */
5736 /*-----------------------------------------------------------*/
5737
5738 #if ( configUSE_TASK_NOTIFICATIONS == 1 )
5739
5740     uint32_t ulTaskGenericNotifyTake( UBaseType_t uxIndexToWait,
5741                                       BaseType_t xClearCountOnExit,
5742                                       TickType_t xTicksToWait )
5743     {
5744         uint32_t ulReturn;
5745
5746         configASSERT( uxIndexToWait < configTASK_NOTIFICATION_ARRAY_ENTRIES );
5747
5748         taskENTER_CRITICAL();
5749         {
5750             /* Only block if the notification count is not already non-zero. */
5751             if( pxCurrentTCB->ulNotifiedValue[ uxIndexToWait ] == 0UL )
5752             {
5753                 /* Mark this task as waiting for a notification. */
5754                 pxCurrentTCB->ucNotifyState[ uxIndexToWait ] = taskWAITING_NOTIFICATION;
5755
5756                 if( xTicksToWait > ( TickType_t ) 0 )
5757                 {
5758                     prvAddCurrentTaskToDelayedList( xTicksToWait, pdTRUE );
5759                     traceTASK_NOTIFY_TAKE_BLOCK( uxIndexToWait );
5760
5761                     /* All ports are written to allow a yield in a critical
5762                      * section (some will yield immediately, others wait until the
5763                      * critical section exits) - but it is not something that
5764                      * application code should ever do. */
5765                     vTaskYieldWithinAPI();
5766                 }
5767                 else
5768                 {
5769                     mtCOVERAGE_TEST_MARKER();
5770                 }
5771             }
5772             else
5773             {
5774                 mtCOVERAGE_TEST_MARKER();
5775             }
5776         }
5777         taskEXIT_CRITICAL();
5778
5779         taskENTER_CRITICAL();
5780         {
5781             traceTASK_NOTIFY_TAKE( uxIndexToWait );
5782             ulReturn = pxCurrentTCB->ulNotifiedValue[ uxIndexToWait ];
5783
5784             if( ulReturn != 0UL )
5785             {
5786                 if( xClearCountOnExit != pdFALSE )
5787                 {
5788                     pxCurrentTCB->ulNotifiedValue[ uxIndexToWait ] = 0UL;
5789                 }
5790                 else
5791                 {
5792                     pxCurrentTCB->ulNotifiedValue[ uxIndexToWait ] = ulReturn - ( uint32_t ) 1;
5793                 }
5794             }
5795             else
5796             {
5797                 mtCOVERAGE_TEST_MARKER();
5798             }
5799
5800             pxCurrentTCB->ucNotifyState[ uxIndexToWait ] = taskNOT_WAITING_NOTIFICATION;
5801         }
5802         taskEXIT_CRITICAL();
5803
5804         return ulReturn;
5805     }
5806
5807 #endif /* configUSE_TASK_NOTIFICATIONS */
5808 /*-----------------------------------------------------------*/
5809
5810 #if ( configUSE_TASK_NOTIFICATIONS == 1 )
5811
5812     BaseType_t xTaskGenericNotifyWait( UBaseType_t uxIndexToWait,
5813                                        uint32_t ulBitsToClearOnEntry,
5814                                        uint32_t ulBitsToClearOnExit,
5815                                        uint32_t * pulNotificationValue,
5816                                        TickType_t xTicksToWait )
5817     {
5818         BaseType_t xReturn;
5819
5820         configASSERT( uxIndexToWait < configTASK_NOTIFICATION_ARRAY_ENTRIES );
5821
5822         taskENTER_CRITICAL();
5823         {
5824             /* Only block if a notification is not already pending. */
5825             if( pxCurrentTCB->ucNotifyState[ uxIndexToWait ] != taskNOTIFICATION_RECEIVED )
5826             {
5827                 /* Clear bits in the task's notification value as bits may get
5828                  * set  by the notifying task or interrupt.  This can be used to
5829                  * clear the value to zero. */
5830                 pxCurrentTCB->ulNotifiedValue[ uxIndexToWait ] &= ~ulBitsToClearOnEntry;
5831
5832                 /* Mark this task as waiting for a notification. */
5833                 pxCurrentTCB->ucNotifyState[ uxIndexToWait ] = taskWAITING_NOTIFICATION;
5834
5835                 if( xTicksToWait > ( TickType_t ) 0 )
5836                 {
5837                     prvAddCurrentTaskToDelayedList( xTicksToWait, pdTRUE );
5838                     traceTASK_NOTIFY_WAIT_BLOCK( uxIndexToWait );
5839
5840                     /* All ports are written to allow a yield in a critical
5841                      * section (some will yield immediately, others wait until the
5842                      * critical section exits) - but it is not something that
5843                      * application code should ever do. */
5844                     vTaskYieldWithinAPI();
5845                 }
5846                 else
5847                 {
5848                     mtCOVERAGE_TEST_MARKER();
5849                 }
5850             }
5851             else
5852             {
5853                 mtCOVERAGE_TEST_MARKER();
5854             }
5855         }
5856         taskEXIT_CRITICAL();
5857
5858         taskENTER_CRITICAL();
5859         {
5860             traceTASK_NOTIFY_WAIT( uxIndexToWait );
5861
5862             if( pulNotificationValue != NULL )
5863             {
5864                 /* Output the current notification value, which may or may not
5865                  * have changed. */
5866                 *pulNotificationValue = pxCurrentTCB->ulNotifiedValue[ uxIndexToWait ];
5867             }
5868
5869             /* If ucNotifyValue is set then either the task never entered the
5870              * blocked state (because a notification was already pending) or the
5871              * task unblocked because of a notification.  Otherwise the task
5872              * unblocked because of a timeout. */
5873             if( pxCurrentTCB->ucNotifyState[ uxIndexToWait ] != taskNOTIFICATION_RECEIVED )
5874             {
5875                 /* A notification was not received. */
5876                 xReturn = pdFALSE;
5877             }
5878             else
5879             {
5880                 /* A notification was already pending or a notification was
5881                  * received while the task was waiting. */
5882                 pxCurrentTCB->ulNotifiedValue[ uxIndexToWait ] &= ~ulBitsToClearOnExit;
5883                 xReturn = pdTRUE;
5884             }
5885
5886             pxCurrentTCB->ucNotifyState[ uxIndexToWait ] = taskNOT_WAITING_NOTIFICATION;
5887         }
5888         taskEXIT_CRITICAL();
5889
5890         return xReturn;
5891     }
5892
5893 #endif /* configUSE_TASK_NOTIFICATIONS */
5894 /*-----------------------------------------------------------*/
5895
5896 #if ( configUSE_TASK_NOTIFICATIONS == 1 )
5897
5898     BaseType_t xTaskGenericNotify( TaskHandle_t xTaskToNotify,
5899                                    UBaseType_t uxIndexToNotify,
5900                                    uint32_t ulValue,
5901                                    eNotifyAction eAction,
5902                                    uint32_t * pulPreviousNotificationValue )
5903     {
5904         TCB_t * pxTCB;
5905         BaseType_t xReturn = pdPASS;
5906         uint8_t ucOriginalNotifyState;
5907
5908         configASSERT( uxIndexToNotify < configTASK_NOTIFICATION_ARRAY_ENTRIES );
5909         configASSERT( xTaskToNotify );
5910         pxTCB = xTaskToNotify;
5911
5912         taskENTER_CRITICAL();
5913         {
5914             if( pulPreviousNotificationValue != NULL )
5915             {
5916                 *pulPreviousNotificationValue = pxTCB->ulNotifiedValue[ uxIndexToNotify ];
5917             }
5918
5919             ucOriginalNotifyState = pxTCB->ucNotifyState[ uxIndexToNotify ];
5920
5921             pxTCB->ucNotifyState[ uxIndexToNotify ] = taskNOTIFICATION_RECEIVED;
5922
5923             switch( eAction )
5924             {
5925                 case eSetBits:
5926                     pxTCB->ulNotifiedValue[ uxIndexToNotify ] |= ulValue;
5927                     break;
5928
5929                 case eIncrement:
5930                     ( pxTCB->ulNotifiedValue[ uxIndexToNotify ] )++;
5931                     break;
5932
5933                 case eSetValueWithOverwrite:
5934                     pxTCB->ulNotifiedValue[ uxIndexToNotify ] = ulValue;
5935                     break;
5936
5937                 case eSetValueWithoutOverwrite:
5938
5939                     if( ucOriginalNotifyState != taskNOTIFICATION_RECEIVED )
5940                     {
5941                         pxTCB->ulNotifiedValue[ uxIndexToNotify ] = ulValue;
5942                     }
5943                     else
5944                     {
5945                         /* The value could not be written to the task. */
5946                         xReturn = pdFAIL;
5947                     }
5948
5949                     break;
5950
5951                 case eNoAction:
5952
5953                     /* The task is being notified without its notify value being
5954                      * updated. */
5955                     break;
5956
5957                 default:
5958
5959                     /* Should not get here if all enums are handled.
5960                      * Artificially force an assert by testing a value the
5961                      * compiler can't assume is const. */
5962                     configASSERT( xTickCount == ( TickType_t ) 0 );
5963
5964                     break;
5965             }
5966
5967             traceTASK_NOTIFY( uxIndexToNotify );
5968
5969             /* If the task is in the blocked state specifically to wait for a
5970              * notification then unblock it now. */
5971             if( ucOriginalNotifyState == taskWAITING_NOTIFICATION )
5972             {
5973                 ( void ) uxListRemove( &( pxTCB->xStateListItem ) );
5974                 prvAddTaskToReadyList( pxTCB );
5975
5976                 /* The task should not have been on an event list. */
5977                 configASSERT( listLIST_ITEM_CONTAINER( &( pxTCB->xEventListItem ) ) == NULL );
5978
5979                 #if ( configUSE_TICKLESS_IDLE != 0 )
5980                     {
5981                         /* If a task is blocked waiting for a notification then
5982                          * xNextTaskUnblockTime might be set to the blocked task's time
5983                          * out time.  If the task is unblocked for a reason other than
5984                          * a timeout xNextTaskUnblockTime is normally left unchanged,
5985                          * because it will automatically get reset to a new value when
5986                          * the tick count equals xNextTaskUnblockTime.  However if
5987                          * tickless idling is used it might be more important to enter
5988                          * sleep mode at the earliest possible time - so reset
5989                          * xNextTaskUnblockTime here to ensure it is updated at the
5990                          * earliest possible time. */
5991                         prvResetNextTaskUnblockTime();
5992                     }
5993                 #endif
5994
5995                 #if ( configUSE_PREEMPTION == 1 )
5996                     {
5997                         prvYieldForTask( pxTCB, pdFALSE );
5998                     }
5999                 #endif
6000             }
6001             else
6002             {
6003                 mtCOVERAGE_TEST_MARKER();
6004             }
6005         }
6006         taskEXIT_CRITICAL();
6007
6008         return xReturn;
6009     }
6010
6011 #endif /* configUSE_TASK_NOTIFICATIONS */
6012 /*-----------------------------------------------------------*/
6013
6014 #if ( configUSE_TASK_NOTIFICATIONS == 1 )
6015
6016     BaseType_t xTaskGenericNotifyFromISR( TaskHandle_t xTaskToNotify,
6017                                           UBaseType_t uxIndexToNotify,
6018                                           uint32_t ulValue,
6019                                           eNotifyAction eAction,
6020                                           uint32_t * pulPreviousNotificationValue,
6021                                           BaseType_t * pxHigherPriorityTaskWoken )
6022     {
6023         TCB_t * pxTCB;
6024         uint8_t ucOriginalNotifyState;
6025         BaseType_t xReturn = pdPASS;
6026         UBaseType_t uxSavedInterruptStatus;
6027
6028         configASSERT( xTaskToNotify );
6029         configASSERT( uxIndexToNotify < configTASK_NOTIFICATION_ARRAY_ENTRIES );
6030
6031         /* RTOS ports that support interrupt nesting have the concept of a
6032          * maximum  system call (or maximum API call) interrupt priority.
6033          * Interrupts that are  above the maximum system call priority are keep
6034          * permanently enabled, even when the RTOS kernel is in a critical section,
6035          * but cannot make any calls to FreeRTOS API functions.  If configASSERT()
6036          * is defined in FreeRTOSConfig.h then
6037          * portASSERT_IF_INTERRUPT_PRIORITY_INVALID() will result in an assertion
6038          * failure if a FreeRTOS API function is called from an interrupt that has
6039          * been assigned a priority above the configured maximum system call
6040          * priority.  Only FreeRTOS functions that end in FromISR can be called
6041          * from interrupts  that have been assigned a priority at or (logically)
6042          * below the maximum system call interrupt priority.  FreeRTOS maintains a
6043          * separate interrupt safe API to ensure interrupt entry is as fast and as
6044          * simple as possible.  More information (albeit Cortex-M specific) is
6045          * provided on the following link:
6046          * https://www.FreeRTOS.org/RTOS-Cortex-M3-M4.html */
6047         portASSERT_IF_INTERRUPT_PRIORITY_INVALID();
6048
6049         pxTCB = xTaskToNotify;
6050
6051         uxSavedInterruptStatus = portSET_INTERRUPT_MASK_FROM_ISR();
6052         {
6053             if( pulPreviousNotificationValue != NULL )
6054             {
6055                 *pulPreviousNotificationValue = pxTCB->ulNotifiedValue[ uxIndexToNotify ];
6056             }
6057
6058             ucOriginalNotifyState = pxTCB->ucNotifyState[ uxIndexToNotify ];
6059             pxTCB->ucNotifyState[ uxIndexToNotify ] = taskNOTIFICATION_RECEIVED;
6060
6061             switch( eAction )
6062             {
6063                 case eSetBits:
6064                     pxTCB->ulNotifiedValue[ uxIndexToNotify ] |= ulValue;
6065                     break;
6066
6067                 case eIncrement:
6068                     ( pxTCB->ulNotifiedValue[ uxIndexToNotify ] )++;
6069                     break;
6070
6071                 case eSetValueWithOverwrite:
6072                     pxTCB->ulNotifiedValue[ uxIndexToNotify ] = ulValue;
6073                     break;
6074
6075                 case eSetValueWithoutOverwrite:
6076
6077                     if( ucOriginalNotifyState != taskNOTIFICATION_RECEIVED )
6078                     {
6079                         pxTCB->ulNotifiedValue[ uxIndexToNotify ] = ulValue;
6080                     }
6081                     else
6082                     {
6083                         /* The value could not be written to the task. */
6084                         xReturn = pdFAIL;
6085                     }
6086
6087                     break;
6088
6089                 case eNoAction:
6090
6091                     /* The task is being notified without its notify value being
6092                      * updated. */
6093                     break;
6094
6095                 default:
6096
6097                     /* Should not get here if all enums are handled.
6098                      * Artificially force an assert by testing a value the
6099                      * compiler can't assume is const. */
6100                     configASSERT( xTickCount == ( TickType_t ) 0 );
6101                     break;
6102             }
6103
6104             traceTASK_NOTIFY_FROM_ISR( uxIndexToNotify );
6105
6106             /* If the task is in the blocked state specifically to wait for a
6107              * notification then unblock it now. */
6108             if( ucOriginalNotifyState == taskWAITING_NOTIFICATION )
6109             {
6110                 /* The task should not have been on an event list. */
6111                 configASSERT( listLIST_ITEM_CONTAINER( &( pxTCB->xEventListItem ) ) == NULL );
6112
6113                 if( uxSchedulerSuspended == ( UBaseType_t ) pdFALSE )
6114                 {
6115                     ( void ) uxListRemove( &( pxTCB->xStateListItem ) );
6116                     prvAddTaskToReadyList( pxTCB );
6117                 }
6118                 else
6119                 {
6120                     /* The delayed and ready lists cannot be accessed, so hold
6121                      * this task pending until the scheduler is resumed. */
6122                     vListInsertEnd( &( xPendingReadyList ), &( pxTCB->xEventListItem ) );
6123                 }
6124
6125                 #if ( configUSE_PREEMPTION == 1 )
6126                     prvYieldForTask( pxTCB, pdFALSE );
6127
6128                     if( xYieldPendings[ portGET_CORE_ID() ] == pdTRUE )
6129                     {
6130                         if( pxHigherPriorityTaskWoken != NULL )
6131                         {
6132                             *pxHigherPriorityTaskWoken = pdTRUE;
6133                         }
6134                     }
6135                 #endif
6136             }
6137         }
6138         portCLEAR_INTERRUPT_MASK_FROM_ISR( uxSavedInterruptStatus );
6139
6140         return xReturn;
6141     }
6142
6143 #endif /* configUSE_TASK_NOTIFICATIONS */
6144 /*-----------------------------------------------------------*/
6145
6146 #if ( configUSE_TASK_NOTIFICATIONS == 1 )
6147
6148     void vTaskGenericNotifyGiveFromISR( TaskHandle_t xTaskToNotify,
6149                                         UBaseType_t uxIndexToNotify,
6150                                         BaseType_t * pxHigherPriorityTaskWoken )
6151     {
6152         TCB_t * pxTCB;
6153         uint8_t ucOriginalNotifyState;
6154         UBaseType_t uxSavedInterruptStatus;
6155
6156         configASSERT( xTaskToNotify );
6157         configASSERT( uxIndexToNotify < configTASK_NOTIFICATION_ARRAY_ENTRIES );
6158
6159         /* RTOS ports that support interrupt nesting have the concept of a
6160          * maximum  system call (or maximum API call) interrupt priority.
6161          * Interrupts that are  above the maximum system call priority are keep
6162          * permanently enabled, even when the RTOS kernel is in a critical section,
6163          * but cannot make any calls to FreeRTOS API functions.  If configASSERT()
6164          * is defined in FreeRTOSConfig.h then
6165          * portASSERT_IF_INTERRUPT_PRIORITY_INVALID() will result in an assertion
6166          * failure if a FreeRTOS API function is called from an interrupt that has
6167          * been assigned a priority above the configured maximum system call
6168          * priority.  Only FreeRTOS functions that end in FromISR can be called
6169          * from interrupts  that have been assigned a priority at or (logically)
6170          * below the maximum system call interrupt priority.  FreeRTOS maintains a
6171          * separate interrupt safe API to ensure interrupt entry is as fast and as
6172          * simple as possible.  More information (albeit Cortex-M specific) is
6173          * provided on the following link:
6174          * https://www.FreeRTOS.org/RTOS-Cortex-M3-M4.html */
6175         portASSERT_IF_INTERRUPT_PRIORITY_INVALID();
6176
6177         pxTCB = xTaskToNotify;
6178
6179         uxSavedInterruptStatus = portSET_INTERRUPT_MASK_FROM_ISR();
6180         {
6181             ucOriginalNotifyState = pxTCB->ucNotifyState[ uxIndexToNotify ];
6182             pxTCB->ucNotifyState[ uxIndexToNotify ] = taskNOTIFICATION_RECEIVED;
6183
6184             /* 'Giving' is equivalent to incrementing a count in a counting
6185              * semaphore. */
6186             ( pxTCB->ulNotifiedValue[ uxIndexToNotify ] )++;
6187
6188             traceTASK_NOTIFY_GIVE_FROM_ISR( uxIndexToNotify );
6189
6190             /* If the task is in the blocked state specifically to wait for a
6191              * notification then unblock it now. */
6192             if( ucOriginalNotifyState == taskWAITING_NOTIFICATION )
6193             {
6194                 /* The task should not have been on an event list. */
6195                 configASSERT( listLIST_ITEM_CONTAINER( &( pxTCB->xEventListItem ) ) == NULL );
6196
6197                 if( uxSchedulerSuspended == ( UBaseType_t ) pdFALSE )
6198                 {
6199                     ( void ) uxListRemove( &( pxTCB->xStateListItem ) );
6200                     prvAddTaskToReadyList( pxTCB );
6201                 }
6202                 else
6203                 {
6204                     /* The delayed and ready lists cannot be accessed, so hold
6205                      * this task pending until the scheduler is resumed. */
6206                     vListInsertEnd( &( xPendingReadyList ), &( pxTCB->xEventListItem ) );
6207                 }
6208
6209                 #if ( configUSE_PREEMPTION == 1 )
6210                     prvYieldForTask( pxTCB, pdFALSE );
6211
6212                     if( xYieldPendings[ portGET_CORE_ID() ] == pdTRUE )
6213                     {
6214                         if( pxHigherPriorityTaskWoken != NULL )
6215                         {
6216                             *pxHigherPriorityTaskWoken = pdTRUE;
6217                         }
6218                     }
6219                 #endif
6220             }
6221         }
6222         portCLEAR_INTERRUPT_MASK_FROM_ISR( uxSavedInterruptStatus );
6223     }
6224
6225 #endif /* configUSE_TASK_NOTIFICATIONS */
6226 /*-----------------------------------------------------------*/
6227
6228 #if ( configUSE_TASK_NOTIFICATIONS == 1 )
6229
6230     BaseType_t xTaskGenericNotifyStateClear( TaskHandle_t xTask,
6231                                              UBaseType_t uxIndexToClear )
6232     {
6233         TCB_t * pxTCB;
6234         BaseType_t xReturn;
6235
6236         configASSERT( uxIndexToClear < configTASK_NOTIFICATION_ARRAY_ENTRIES );
6237
6238         /* If null is passed in here then it is the calling task that is having
6239          * its notification state cleared. */
6240         pxTCB = prvGetTCBFromHandle( xTask );
6241
6242         taskENTER_CRITICAL();
6243         {
6244             if( pxTCB->ucNotifyState[ uxIndexToClear ] == taskNOTIFICATION_RECEIVED )
6245             {
6246                 pxTCB->ucNotifyState[ uxIndexToClear ] = taskNOT_WAITING_NOTIFICATION;
6247                 xReturn = pdPASS;
6248             }
6249             else
6250             {
6251                 xReturn = pdFAIL;
6252             }
6253         }
6254         taskEXIT_CRITICAL();
6255
6256         return xReturn;
6257     }
6258
6259 #endif /* configUSE_TASK_NOTIFICATIONS */
6260 /*-----------------------------------------------------------*/
6261
6262 #if ( configUSE_TASK_NOTIFICATIONS == 1 )
6263
6264     uint32_t ulTaskGenericNotifyValueClear( TaskHandle_t xTask,
6265                                             UBaseType_t uxIndexToClear,
6266                                             uint32_t ulBitsToClear )
6267     {
6268         TCB_t * pxTCB;
6269         uint32_t ulReturn;
6270
6271         /* If null is passed in here then it is the calling task that is having
6272          * its notification state cleared. */
6273         pxTCB = prvGetTCBFromHandle( xTask );
6274
6275         taskENTER_CRITICAL();
6276         {
6277             /* Return the notification as it was before the bits were cleared,
6278              * then clear the bit mask. */
6279             ulReturn = pxTCB->ulNotifiedValue[ uxIndexToClear ];
6280             pxTCB->ulNotifiedValue[ uxIndexToClear ] &= ~ulBitsToClear;
6281         }
6282         taskEXIT_CRITICAL();
6283
6284         return ulReturn;
6285     }
6286
6287 #endif /* configUSE_TASK_NOTIFICATIONS */
6288 /*-----------------------------------------------------------*/
6289
6290 #if ( ( configGENERATE_RUN_TIME_STATS == 1 ) && ( INCLUDE_xTaskGetIdleTaskHandle == 1 ) )
6291
6292     uint32_t ulTaskGetIdleRunTimeCounter( void )
6293     {
6294         uint32_t ulReturn = 0;
6295
6296         for( BaseType_t i = 0; i < configNUM_CORES; i++ )
6297         {
6298             ulReturn += xIdleTaskHandle[ i ]->ulRunTimeCounter;
6299         }
6300
6301         return ulReturn;
6302     }
6303
6304 #endif /* if ( ( configGENERATE_RUN_TIME_STATS == 1 ) && ( INCLUDE_xTaskGetIdleTaskHandle == 1 ) ) */
6305 /*-----------------------------------------------------------*/
6306
6307 static void prvAddCurrentTaskToDelayedList( TickType_t xTicksToWait,
6308                                             const BaseType_t xCanBlockIndefinitely )
6309 {
6310     TickType_t xTimeToWake;
6311     const TickType_t xConstTickCount = xTickCount;
6312
6313     #if ( INCLUDE_xTaskAbortDelay == 1 )
6314         {
6315             /* About to enter a delayed list, so ensure the ucDelayAborted flag is
6316              * reset to pdFALSE so it can be detected as having been set to pdTRUE
6317              * when the task leaves the Blocked state. */
6318             pxCurrentTCB->ucDelayAborted = pdFALSE;
6319         }
6320     #endif
6321
6322     /* Remove the task from the ready list before adding it to the blocked list
6323      * as the same list item is used for both lists. */
6324     if( uxListRemove( &( pxCurrentTCB->xStateListItem ) ) == ( UBaseType_t ) 0 )
6325     {
6326         /* The current task must be in a ready list, so there is no need to
6327          * check, and the port reset macro can be called directly. */
6328         portRESET_READY_PRIORITY( pxCurrentTCB->uxPriority, uxTopReadyPriority ); /*lint !e931 pxCurrentTCB cannot change as it is the calling task.  pxCurrentTCB->uxPriority and uxTopReadyPriority cannot change as called with scheduler suspended or in a critical section. */
6329     }
6330     else
6331     {
6332         mtCOVERAGE_TEST_MARKER();
6333     }
6334
6335     #if ( INCLUDE_vTaskSuspend == 1 )
6336         {
6337             if( ( xTicksToWait == portMAX_DELAY ) && ( xCanBlockIndefinitely != pdFALSE ) )
6338             {
6339                 /* Add the task to the suspended task list instead of a delayed task
6340                  * list to ensure it is not woken by a timing event.  It will block
6341                  * indefinitely. */
6342                 vListInsertEnd( &xSuspendedTaskList, &( pxCurrentTCB->xStateListItem ) );
6343             }
6344             else
6345             {
6346                 /* Calculate the time at which the task should be woken if the event
6347                  * does not occur.  This may overflow but this doesn't matter, the
6348                  * kernel will manage it correctly. */
6349                 xTimeToWake = xConstTickCount + xTicksToWait;
6350
6351                 /* The list item will be inserted in wake time order. */
6352                 listSET_LIST_ITEM_VALUE( &( pxCurrentTCB->xStateListItem ), xTimeToWake );
6353
6354                 if( xTimeToWake < xConstTickCount )
6355                 {
6356                     /* Wake time has overflowed.  Place this item in the overflow
6357                      * list. */
6358                     vListInsert( pxOverflowDelayedTaskList, &( pxCurrentTCB->xStateListItem ) );
6359                 }
6360                 else
6361                 {
6362                     /* The wake time has not overflowed, so the current block list
6363                      * is used. */
6364                     vListInsert( pxDelayedTaskList, &( pxCurrentTCB->xStateListItem ) );
6365
6366                     /* If the task entering the blocked state was placed at the
6367                      * head of the list of blocked tasks then xNextTaskUnblockTime
6368                      * needs to be updated too. */
6369                     if( xTimeToWake < xNextTaskUnblockTime )
6370                     {
6371                         xNextTaskUnblockTime = xTimeToWake;
6372                     }
6373                     else
6374                     {
6375                         mtCOVERAGE_TEST_MARKER();
6376                     }
6377                 }
6378             }
6379         }
6380     #else /* INCLUDE_vTaskSuspend */
6381         {
6382             /* Calculate the time at which the task should be woken if the event
6383              * does not occur.  This may overflow but this doesn't matter, the kernel
6384              * will manage it correctly. */
6385             xTimeToWake = xConstTickCount + xTicksToWait;
6386
6387             /* The list item will be inserted in wake time order. */
6388             listSET_LIST_ITEM_VALUE( &( pxCurrentTCB->xStateListItem ), xTimeToWake );
6389
6390             if( xTimeToWake < xConstTickCount )
6391             {
6392                 /* Wake time has overflowed.  Place this item in the overflow list. */
6393                 vListInsert( pxOverflowDelayedTaskList, &( pxCurrentTCB->xStateListItem ) );
6394             }
6395             else
6396             {
6397                 /* The wake time has not overflowed, so the current block list is used. */
6398                 vListInsert( pxDelayedTaskList, &( pxCurrentTCB->xStateListItem ) );
6399
6400                 /* If the task entering the blocked state was placed at the head of the
6401                  * list of blocked tasks then xNextTaskUnblockTime needs to be updated
6402                  * too. */
6403                 if( xTimeToWake < xNextTaskUnblockTime )
6404                 {
6405                     xNextTaskUnblockTime = xTimeToWake;
6406                 }
6407                 else
6408                 {
6409                     mtCOVERAGE_TEST_MARKER();
6410                 }
6411             }
6412
6413             /* Avoid compiler warning when INCLUDE_vTaskSuspend is not 1. */
6414             ( void ) xCanBlockIndefinitely;
6415         }
6416     #endif /* INCLUDE_vTaskSuspend */
6417 }
6418
6419 /* Code below here allows additional code to be inserted into this source file,
6420  * especially where access to file scope functions and data is needed (for example
6421  * when performing module tests). */
6422
6423 #ifdef FREERTOS_MODULE_TEST
6424     #include "tasks_test_access_functions.h"
6425 #endif
6426
6427
6428 #if ( configINCLUDE_FREERTOS_TASK_C_ADDITIONS_H == 1 )
6429
6430     #include "freertos_tasks_c_additions.h"
6431
6432     #ifdef FREERTOS_TASKS_C_ADDITIONS_INIT
6433         static void freertos_tasks_c_additions_init( void )
6434         {
6435             FREERTOS_TASKS_C_ADDITIONS_INIT();
6436         }
6437     #endif
6438
6439 #endif /* if ( configINCLUDE_FREERTOS_TASK_C_ADDITIONS_H == 1 ) */