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