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