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