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