]> begriffs open source - freertos/blob - tasks.c
Fixed static allocation internal array
[freertos] / tasks.c
1 /*
2  * FreeRTOS Kernel V10.4.3
3  * Copyright (C) 2020 Amazon.com, Inc. or its affiliates.  All Rights Reserved.
4  *
5  * Permission is hereby granted, free of charge, to any person obtaining a copy of
6  * this software and associated documentation files (the "Software"), to deal in
7  * the Software without restriction, including without limitation the rights to
8  * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
9  * the Software, and to permit persons to whom the Software is furnished to do so,
10  * subject to the following conditions:
11  *
12  * The above copyright notice and this permission notice shall be included in all
13  * copies or substantial portions of the Software.
14  *
15  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
17  * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
18  * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
19  * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
20  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
21  *
22  * https://www.FreeRTOS.org
23  * https://github.com/FreeRTOS
24  *
25  */
26
27 /* Standard includes. */
28 #include <stdlib.h>
29 #include <string.h>
30
31 /* Defining MPU_WRAPPERS_INCLUDED_FROM_API_FILE prevents task.h from redefining
32  * all the API functions to use the MPU wrappers.  That should only be done when
33  * task.h is included from an application file. */
34 #define MPU_WRAPPERS_INCLUDED_FROM_API_FILE
35
36 #define DEBUG_UNIT    FREERTOS_TASKS
37
38 /* FreeRTOS includes. */
39 #include "FreeRTOS.h"
40 #include "task.h"
41 #include "timers.h"
42 #include "stack_macros.h"
43
44 /* Lint e9021, e961 and e750 are suppressed as a MISRA exception justified
45  * because the MPU ports require MPU_WRAPPERS_INCLUDED_FROM_API_FILE to be defined
46  * for the header files above, but not in this file, in order to generate the
47  * correct privileged Vs unprivileged linkage and placement. */
48 #undef MPU_WRAPPERS_INCLUDED_FROM_API_FILE /*lint !e961 !e750 !e9021. */
49
50 /* Set configUSE_STATS_FORMATTING_FUNCTIONS to 2 to include the stats formatting
51  * functions but without including stdio.h here. */
52 #if ( configUSE_STATS_FORMATTING_FUNCTIONS == 1 )
53
54 /* At the bottom of this file are two optional functions that can be used
55  * to generate human readable text from the raw data generated by the
56  * uxTaskGetSystemState() function.  Note the formatting functions are provided
57  * for convenience only, and are NOT considered part of the kernel. */
58     #include <stdio.h>
59 #endif /* configUSE_STATS_FORMATTING_FUNCTIONS == 1 ) */
60
61 #if ( configUSE_PREEMPTION == 0 )
62
63 /* If the cooperative scheduler is being used then a yield should not be
64  * performed just because a higher priority task has been woken. */
65     #define taskYIELD_IF_USING_PREEMPTION()
66 #else
67     #define taskYIELD_IF_USING_PREEMPTION()    vTaskYieldWithinAPI()
68 #endif
69
70 /* Values that can be assigned to the ucNotifyState member of the TCB. */
71 #define taskNOT_WAITING_NOTIFICATION              ( ( uint8_t ) 0 ) /* Must be zero as it is the initialised value. */
72 #define taskWAITING_NOTIFICATION                  ( ( uint8_t ) 1 )
73 #define taskNOTIFICATION_RECEIVED                 ( ( uint8_t ) 2 )
74
75 /*
76  * The value used to fill the stack of a task when the task is created.  This
77  * is used purely for checking the high water mark for tasks.
78  */
79 #define tskSTACK_FILL_BYTE                        ( 0xa5U )
80
81 /* Bits used to record how a task's stack and TCB were allocated. */
82 #define tskDYNAMICALLY_ALLOCATED_STACK_AND_TCB    ( ( uint8_t ) 0 )
83 #define tskSTATICALLY_ALLOCATED_STACK_ONLY        ( ( uint8_t ) 1 )
84 #define tskSTATICALLY_ALLOCATED_STACK_AND_TCB     ( ( uint8_t ) 2 )
85
86 /* If any of the following are set then task stacks are filled with a known
87  * value so the high water mark can be determined.  If none of the following are
88  * set then don't fill the stack so there is no unnecessary dependency on memset. */
89 #if ( ( configCHECK_FOR_STACK_OVERFLOW > 1 ) || ( configUSE_TRACE_FACILITY == 1 ) || ( INCLUDE_uxTaskGetStackHighWaterMark == 1 ) || ( INCLUDE_uxTaskGetStackHighWaterMark2 == 1 ) )
90     #define tskSET_NEW_STACKS_TO_KNOWN_VALUE    1
91 #else
92     #define tskSET_NEW_STACKS_TO_KNOWN_VALUE    0
93 #endif
94
95 /*
96  * Macros used by vListTask to indicate which state a task is in.
97  */
98 #define tskRUNNING_CHAR      ( 'X' )
99 #define tskBLOCKED_CHAR      ( 'B' )
100 #define tskREADY_CHAR        ( 'R' )
101 #define tskDELETED_CHAR      ( 'D' )
102 #define tskSUSPENDED_CHAR    ( 'S' )
103
104 /*
105  * Some kernel aware debuggers require the data the debugger needs access to to
106  * be global, rather than file scope.
107  */
108 #ifdef portREMOVE_STATIC_QUALIFIER
109     #define static
110 #endif
111
112 /* The name allocated to the Idle task.  This can be overridden by defining
113  * configIDLE_TASK_NAME in FreeRTOSConfig.h. */
114 #ifndef configIDLE_TASK_NAME
115     #define configIDLE_TASK_NAME    "IDLE"
116 #endif
117
118 #if ( configUSE_PORT_OPTIMISED_TASK_SELECTION == 0 )
119
120 /* If configUSE_PORT_OPTIMISED_TASK_SELECTION is 0 then task selection is
121  * performed in a generic way that is not optimised to any particular
122  * microcontroller architecture. */
123
124 /* uxTopReadyPriority holds the priority of the highest priority ready
125  * state task. */
126     #define taskRECORD_READY_PRIORITY( uxPriority ) \
127     {                                               \
128         if( ( uxPriority ) > uxTopReadyPriority )   \
129         {                                           \
130             uxTopReadyPriority = ( uxPriority );    \
131         }                                           \
132     } /* taskRECORD_READY_PRIORITY */
133
134     /*-----------------------------------------------------------*/
135
136 /* Define away taskRESET_READY_PRIORITY() and portRESET_READY_PRIORITY() as
137  * they are only required when a port optimised method of task selection is
138  * being used. */
139     #define taskRESET_READY_PRIORITY( uxPriority )
140     #define portRESET_READY_PRIORITY( uxPriority, uxTopReadyPriority )
141
142 #else /* configUSE_PORT_OPTIMISED_TASK_SELECTION */
143
144     #error configUSE_PORT_OPTIMISED_TASK_SELECTION not yet supported in SMP
145
146 /* If configUSE_PORT_OPTIMISED_TASK_SELECTION is 1 then task selection is
147  * performed in a way that is tailored to the particular microcontroller
148  * architecture being used. */
149
150 /* A port optimised version is provided.  Call the port defined macros. */
151     #define taskRECORD_READY_PRIORITY( uxPriority )    portRECORD_READY_PRIORITY( uxPriority, uxTopReadyPriority )
152
153         /*-----------------------------------------------------------*/
154
155 /* A port optimised version is provided, call it only if the TCB being reset
156  * is being referenced from a ready list.  If it is referenced from a delayed
157  * or suspended list then it won't be in a ready list. */
158     #define taskRESET_READY_PRIORITY( uxPriority )                                                     \
159     {                                                                                                  \
160         if( listCURRENT_LIST_LENGTH( &( pxReadyTasksLists[ ( uxPriority ) ] ) ) == ( UBaseType_t ) 0 ) \
161         {                                                                                              \
162             portRESET_READY_PRIORITY( ( uxPriority ), ( uxTopReadyPriority ) );                        \
163         }                                                                                              \
164     }
165
166 #endif /* configUSE_PORT_OPTIMISED_TASK_SELECTION */
167
168 /*-----------------------------------------------------------*/
169
170 /* pxDelayedTaskList and pxOverflowDelayedTaskList are switched when the tick
171  * count overflows. */
172 #define taskSWITCH_DELAYED_LISTS()                                                \
173     {                                                                             \
174         List_t * pxTemp;                                                          \
175                                                                                   \
176         /* The delayed tasks list should be empty when the lists are switched. */ \
177         configASSERT( ( listLIST_IS_EMPTY( pxDelayedTaskList ) ) );               \
178                                                                                   \
179         pxTemp = pxDelayedTaskList;                                               \
180         pxDelayedTaskList = pxOverflowDelayedTaskList;                            \
181         pxOverflowDelayedTaskList = pxTemp;                                       \
182         xNumOfOverflows++;                                                        \
183         prvResetNextTaskUnblockTime();                                            \
184     }
185
186 /*-----------------------------------------------------------*/
187
188 /*
189  * Place the task represented by pxTCB into the appropriate ready list for
190  * the task.  It is inserted at the end of the list.
191  */
192 #define prvAddTaskToReadyList( pxTCB )                                                                 \
193     traceMOVED_TASK_TO_READY_STATE( pxTCB );                                                           \
194     taskRECORD_READY_PRIORITY( ( pxTCB )->uxPriority );                                                \
195     vListInsertEnd( &( pxReadyTasksLists[ ( pxTCB )->uxPriority ] ), &( ( pxTCB )->xStateListItem ) ); \
196     tracePOST_MOVED_TASK_TO_READY_STATE( pxTCB )
197 /*-----------------------------------------------------------*/
198
199 /*
200  * Several functions take a TaskHandle_t parameter that can optionally be NULL,
201  * where NULL is used to indicate that the handle of the currently executing
202  * task should be used in place of the parameter.  This macro simply checks to
203  * see if the parameter is NULL and returns a pointer to the appropriate TCB.
204  */
205 #define prvGetTCBFromHandle( pxHandle )    ( ( ( pxHandle ) == NULL ) ? pxCurrentTCB : ( pxHandle ) )
206
207 /* The item value of the event list item is normally used to hold the priority
208  * of the task to which it belongs (coded to allow it to be held in reverse
209  * priority order).  However, it is occasionally borrowed for other purposes.  It
210  * is important its value is not updated due to a task priority change while it is
211  * being used for another purpose.  The following bit definition is used to inform
212  * the scheduler that the value should not be changed - in which case it is the
213  * responsibility of whichever module is using the value to ensure it gets set back
214  * to its original value when it is released. */
215 #if ( configUSE_16_BIT_TICKS == 1 )
216     #define taskEVENT_LIST_ITEM_VALUE_IN_USE    0x8000U
217 #else
218     #define taskEVENT_LIST_ITEM_VALUE_IN_USE    0x80000000UL
219 #endif
220
221 /* Indicates that the task is not actively running on any core. */
222 #define taskTASK_NOT_RUNNING    ( TaskRunning_t ) ( -1 )
223
224 /* Indicates that the task is actively running but scheduled to yield. */
225 #define taskTASK_YIELDING       ( TaskRunning_t ) ( -2 )
226
227 /* Returns pdTRUE if the task is actively running and not scheduled to yield. */
228 #define taskTASK_IS_RUNNING( xTaskRunState )    ( ( 0 <= xTaskRunState ) && ( xTaskRunState < configNUM_CORES ) )
229
230 typedef BaseType_t TaskRunning_t;
231
232 /*
233  * Task control block.  A task control block (TCB) is allocated for each task,
234  * and stores task state information, including a pointer to the task's context
235  * (the task's run time environment, including register values)
236  */
237 typedef struct tskTaskControlBlock       /* The old naming convention is used to prevent breaking kernel aware debuggers. */
238 {
239     volatile StackType_t * pxTopOfStack; /*< Points to the location of the last item placed on the tasks stack.  THIS MUST BE THE FIRST MEMBER OF THE TCB STRUCT. */
240
241     #if ( portUSING_MPU_WRAPPERS == 1 )
242         xMPU_SETTINGS xMPUSettings; /*< The MPU settings are defined as part of the port layer.  THIS MUST BE THE SECOND MEMBER OF THE TCB STRUCT. */
243     #endif
244
245     ListItem_t xStateListItem;                  /*< The list that the state list item of a task is reference from denotes the state of that task (Ready, Blocked, Suspended ). */
246     ListItem_t xEventListItem;                  /*< Used to reference a task from an event list. */
247     UBaseType_t uxPriority;                     /*< The priority of the task.  0 is the lowest priority. */
248     StackType_t * pxStack;                      /*< Points to the start of the stack. */
249     volatile TaskRunning_t xTaskRunState;       /*< Used to identify the core the task is running on, if any. */
250     BaseType_t xIsIdle;                         /*< Used to identify the idle tasks. */
251     char pcTaskName[ configMAX_TASK_NAME_LEN ]; /*< Descriptive name given to the task when created.  Facilitates debugging only. */ /*lint !e971 Unqualified char types are allowed for strings and single characters only. */
252
253     #if ( configUSE_TASK_PREEMPTION_DISABLE == 1 )
254         BaseType_t xPreemptionDisable; /*< Used to prevent the task from being preempted */
255     #endif
256
257     #if ( configUSE_CORE_AFFINITY == 1 && configNUM_CORES > 1 )
258         UBaseType_t uxCoreAffinityMask; /*< Used to link the task to certain cores.  UBaseType_t must have >= the same number of bits as SMP confNUM_CORES */
259         UBaseType_t uxCoreAffinityInheritanceMask; /*< Used to allow a task to inherit the affinity of its parent */
260     #endif
261
262     #if ( ( portSTACK_GROWTH > 0 ) || ( configRECORD_STACK_HIGH_ADDRESS == 1 ) )
263         StackType_t * pxEndOfStack; /*< Points to the highest valid address for the stack. */
264     #endif
265
266     #if ( portCRITICAL_NESTING_IN_TCB == 1 )
267         UBaseType_t uxCriticalNesting; /*< Holds the critical section nesting depth for ports that do not maintain their own count in the port layer. */
268     #endif
269
270     #if ( configUSE_TRACE_FACILITY == 1 )
271         UBaseType_t uxTCBNumber;  /*< Stores a number that increments each time a TCB is created.  It allows debuggers to determine when a task has been deleted and then recreated. */
272         UBaseType_t uxTaskNumber; /*< Stores a number specifically for use by third party trace code. */
273     #endif
274
275     #if ( configUSE_MUTEXES == 1 )
276         UBaseType_t uxBasePriority; /*< The priority last assigned to the task - used by the priority inheritance mechanism. */
277         UBaseType_t uxMutexesHeld;
278     #endif
279
280     #if ( configUSE_APPLICATION_TASK_TAG == 1 )
281         TaskHookFunction_t pxTaskTag;
282     #endif
283
284     #if ( configNUM_THREAD_LOCAL_STORAGE_POINTERS > 0 )
285         void * pvThreadLocalStoragePointers[ configNUM_THREAD_LOCAL_STORAGE_POINTERS ];
286     #endif
287
288     #if ( configGENERATE_RUN_TIME_STATS == 1 )
289         uint32_t ulRunTimeCounter; /*< Stores the amount of time the task has spent in the Running state. */
290     #endif
291
292     #if ( configUSE_NEWLIB_REENTRANT == 1 )
293
294         /* Allocate a Newlib reent structure that is specific to this task.
295          * Note Newlib support has been included by popular demand, but is not
296          * used by the FreeRTOS maintainers themselves.  FreeRTOS is not
297          * responsible for resulting newlib operation.  User must be familiar with
298          * newlib and must provide system-wide implementations of the necessary
299          * stubs. Be warned that (at the time of writing) the current newlib design
300          * implements a system-wide malloc() that must be provided with locks.
301          *
302          * See the third party link http://www.nadler.com/embedded/newlibAndFreeRTOS.html
303          * for additional information. */
304         struct  _reent xNewLib_reent;
305     #endif
306
307     #if ( configUSE_TASK_NOTIFICATIONS == 1 )
308         volatile uint32_t ulNotifiedValue[ configTASK_NOTIFICATION_ARRAY_ENTRIES ];
309         volatile uint8_t ucNotifyState[ configTASK_NOTIFICATION_ARRAY_ENTRIES ];
310     #endif
311
312     /* See the comments in FreeRTOS.h with the definition of
313      * tskSTATIC_AND_DYNAMIC_ALLOCATION_POSSIBLE. */
314     #if ( tskSTATIC_AND_DYNAMIC_ALLOCATION_POSSIBLE != 0 ) /*lint !e731 !e9029 Macro has been consolidated for readability reasons. */
315         uint8_t ucStaticallyAllocated;                     /*< Set to pdTRUE if the task is a statically allocated to ensure no attempt is made to free the memory. */
316     #endif
317
318     #if ( INCLUDE_xTaskAbortDelay == 1 )
319         uint8_t ucDelayAborted;
320     #endif
321
322     #if ( configUSE_POSIX_ERRNO == 1 )
323         int iTaskErrno;
324     #endif
325 }  tskTCB;
326
327 /* The old tskTCB name is maintained above then typedefed to the new TCB_t name
328  * below to enable the use of older kernel aware debuggers. */
329 typedef tskTCB TCB_t;
330
331 /*lint -save -e956 A manual analysis and inspection has been used to determine
332  * which static variables must be declared volatile. */
333 PRIVILEGED_DATA TCB_t * volatile pxCurrentTCBs[ configNUM_CORES ] = { NULL };
334 #define pxCurrentTCB    xTaskGetCurrentTaskHandle()
335
336 /* Lists for ready and blocked tasks. --------------------
337  * xDelayedTaskList1 and xDelayedTaskList2 could be moved to function scope but
338  * doing so breaks some kernel aware debuggers and debuggers that rely on removing
339  * the static qualifier. */
340 PRIVILEGED_DATA static List_t pxReadyTasksLists[ configMAX_PRIORITIES ]; /*< Prioritised ready tasks. */
341 PRIVILEGED_DATA static List_t xDelayedTaskList1;                         /*< Delayed tasks. */
342 PRIVILEGED_DATA static List_t xDelayedTaskList2;                         /*< Delayed tasks (two lists are used - one for delays that have overflowed the current tick count. */
343 PRIVILEGED_DATA static List_t * volatile pxDelayedTaskList;              /*< Points to the delayed task list currently being used. */
344 PRIVILEGED_DATA static List_t * volatile pxOverflowDelayedTaskList;      /*< Points to the delayed task list currently being used to hold tasks that have overflowed the current tick count. */
345 PRIVILEGED_DATA static List_t xPendingReadyList;                         /*< Tasks that have been readied while the scheduler was suspended.  They will be moved to the ready list when the scheduler is resumed. */
346
347 #if ( INCLUDE_vTaskDelete == 1 )
348
349     PRIVILEGED_DATA static List_t xTasksWaitingTermination; /*< Tasks that have been deleted - but their memory not yet freed. */
350     PRIVILEGED_DATA static volatile UBaseType_t uxDeletedTasksWaitingCleanUp = ( UBaseType_t ) 0U;
351
352 #endif
353
354 #if ( INCLUDE_vTaskSuspend == 1 )
355
356     PRIVILEGED_DATA static List_t xSuspendedTaskList; /*< Tasks that are currently suspended. */
357
358 #endif
359
360 /* Global POSIX errno. Its value is changed upon context switching to match
361  * the errno of the currently running task. */
362 #if ( configUSE_POSIX_ERRNO == 1 )
363     int FreeRTOS_errno = 0;
364 #endif
365
366 /* Other file private variables. --------------------------------*/
367 PRIVILEGED_DATA static volatile UBaseType_t uxCurrentNumberOfTasks = ( UBaseType_t ) 0U;
368 PRIVILEGED_DATA static volatile TickType_t xTickCount = ( TickType_t ) configINITIAL_TICK_COUNT;
369 PRIVILEGED_DATA static volatile UBaseType_t uxTopReadyPriority = tskIDLE_PRIORITY;
370 PRIVILEGED_DATA static volatile BaseType_t xSchedulerRunning = pdFALSE;
371 PRIVILEGED_DATA static volatile TickType_t xPendedTicks = ( TickType_t ) 0U;
372 PRIVILEGED_DATA static volatile BaseType_t xYieldPendings[ configNUM_CORES ] = { pdFALSE };
373 PRIVILEGED_DATA static volatile BaseType_t xNumOfOverflows = ( BaseType_t ) 0;
374 PRIVILEGED_DATA static UBaseType_t uxTaskNumber = ( UBaseType_t ) 0U;
375 PRIVILEGED_DATA static volatile TickType_t xNextTaskUnblockTime = ( TickType_t ) 0U; /* Initialised to portMAX_DELAY before the scheduler starts. */
376 PRIVILEGED_DATA static TaskHandle_t xIdleTaskHandle[ configNUM_CORES ] = { NULL };   /*< Holds the handle of the idle task.  The idle task is created automatically when the scheduler is started. */
377
378 #define xYieldPending    prvGetCurrentYieldPending()
379
380 /* Improve support for OpenOCD. The kernel tracks Ready tasks via priority lists.
381  * For tracking the state of remote threads, OpenOCD uses uxTopUsedPriority
382  * to determine the number of priority lists to read back from the remote target. */
383 const volatile UBaseType_t uxTopUsedPriority = configMAX_PRIORITIES - 1U;
384
385 /* Context switches are held pending while the scheduler is suspended.  Also,
386  * interrupts must not manipulate the xStateListItem of a TCB, or any of the
387  * lists the xStateListItem can be referenced from, if the scheduler is suspended.
388  * If an interrupt needs to unblock a task while the scheduler is suspended then it
389  * moves the task's event list item into the xPendingReadyList, ready for the
390  * kernel to move the task from the pending ready list into the real ready list
391  * when the scheduler is unsuspended.  The pending ready list itself can only be
392  * accessed from a critical section.
393  *
394  * Updates to uxSchedulerSuspended must be protected by both the task and ISR locks and
395  * must not be done by an ISR. Reads must be protected by either lock and may be done by
396  * either an ISR or a task. */
397 PRIVILEGED_DATA static volatile UBaseType_t uxSchedulerSuspended = ( UBaseType_t ) pdFALSE;
398
399 #if ( configGENERATE_RUN_TIME_STATS == 1 )
400
401 /* Do not move these variables to function scope as doing so prevents the
402  * code working with debuggers that need to remove the static qualifier. */
403     PRIVILEGED_DATA static uint32_t ulTaskSwitchedInTime = 0UL;    /*< Holds the value of a timer/counter the last time a task was switched in. */
404     PRIVILEGED_DATA static volatile uint32_t ulTotalRunTime = 0UL; /*< Holds the total amount of execution time as defined by the run time counter clock. */
405
406 #endif
407
408 /*lint -restore */
409
410 /*-----------------------------------------------------------*/
411
412 /* File private functions. --------------------------------*/
413
414 /*
415  * Creates the idle tasks during scheduler start
416  */
417 static BaseType_t prvCreateIdleTasks( void );
418
419 /*
420  * Returns the yield pending count for the calling core.
421  */
422 static BaseType_t prvGetCurrentYieldPending( void );
423
424 /*
425  * Checks to see if another task moved the current task out of the ready
426  * list while it was waiting to enter a critical section and yields if so.
427  */
428 static void prvCheckForRunStateChange( void );
429
430 /*
431  * Yields the given core.
432  */
433 static void prvYieldCore( BaseType_t xCoreID );
434
435 /*
436  * Yields a core, or cores if multiple priorities are not allowed to run
437  * simultaneously, to allow the task pxTCB to run.
438  */
439 static void prvYieldForTask( TCB_t * pxTCB,
440                              const BaseType_t xPreemptEqualPriority );
441
442 /*
443  * Selects the highest priority available task
444  */
445 static BaseType_t prvSelectHighestPriorityTask( const BaseType_t xCoreID );
446
447 /**
448  * Utility task that simply returns pdTRUE if the task referenced by xTask is
449  * currently in the Suspended state, or pdFALSE if the task referenced by xTask
450  * is in any other state.
451  */
452 #if ( INCLUDE_vTaskSuspend == 1 )
453
454     static BaseType_t prvTaskIsTaskSuspended( const TaskHandle_t xTask ) PRIVILEGED_FUNCTION;
455
456 #endif /* INCLUDE_vTaskSuspend */
457
458 /*
459  * Utility to ready all the lists used by the scheduler.  This is called
460  * automatically upon the creation of the first task.
461  */
462 static void prvInitialiseTaskLists( void ) PRIVILEGED_FUNCTION;
463
464 /*
465  * The idle task, which as all tasks is implemented as a never ending loop.
466  * The idle task is automatically created and added to the ready lists upon
467  * creation of the first user task.
468  *
469  */
470 static portTASK_FUNCTION_PROTO( prvIdleTask, pvParameters ) PRIVILEGED_FUNCTION;
471 #if ( configNUM_CORES > 1 )
472 static portTASK_FUNCTION_PROTO( prvMinimalIdleTask, pvParameters ) PRIVILEGED_FUNCTION;
473 #endif
474
475 /*
476  * Utility to free all memory allocated by the scheduler to hold a TCB,
477  * including the stack pointed to by the TCB.
478  *
479  * This does not free memory allocated by the task itself (i.e. memory
480  * allocated by calls to pvPortMalloc from within the tasks application code).
481  */
482 #if ( INCLUDE_vTaskDelete == 1 )
483
484     static void prvDeleteTCB( TCB_t * pxTCB ) PRIVILEGED_FUNCTION;
485
486 #endif
487
488 /*
489  * Used only by the idle task.  This checks to see if anything has been placed
490  * in the list of tasks waiting to be deleted.  If so the task is cleaned up
491  * and its TCB deleted.
492  */
493 static void prvCheckTasksWaitingTermination( void ) PRIVILEGED_FUNCTION;
494
495 /*
496  * The currently executing task is entering the Blocked state.  Add the task to
497  * either the current or the overflow delayed task list.
498  */
499 static void prvAddCurrentTaskToDelayedList( TickType_t xTicksToWait,
500                                             const BaseType_t xCanBlockIndefinitely ) PRIVILEGED_FUNCTION;
501
502 /*
503  * Fills an TaskStatus_t structure with information on each task that is
504  * referenced from the pxList list (which may be a ready list, a delayed list,
505  * a suspended list, etc.).
506  *
507  * THIS FUNCTION IS INTENDED FOR DEBUGGING ONLY, AND SHOULD NOT BE CALLED FROM
508  * NORMAL APPLICATION CODE.
509  */
510 #if ( configUSE_TRACE_FACILITY == 1 )
511
512     static UBaseType_t prvListTasksWithinSingleList( TaskStatus_t * pxTaskStatusArray,
513                                                      List_t * pxList,
514                                                      eTaskState eState ) PRIVILEGED_FUNCTION;
515
516 #endif
517
518 /*
519  * Searches pxList for a task with name pcNameToQuery - returning a handle to
520  * the task if it is found, or NULL if the task is not found.
521  */
522 #if ( INCLUDE_xTaskGetHandle == 1 )
523
524     static TCB_t * prvSearchForNameWithinSingleList( List_t * pxList,
525                                                      const char pcNameToQuery[] ) PRIVILEGED_FUNCTION;
526
527 #endif
528
529 /*
530  * When a task is created, the stack of the task is filled with a known value.
531  * This function determines the 'high water mark' of the task stack by
532  * determining how much of the stack remains at the original preset value.
533  */
534 #if ( ( configUSE_TRACE_FACILITY == 1 ) || ( INCLUDE_uxTaskGetStackHighWaterMark == 1 ) || ( INCLUDE_uxTaskGetStackHighWaterMark2 == 1 ) )
535
536     static configSTACK_DEPTH_TYPE prvTaskCheckFreeStackSpace( const uint8_t * pucStackByte ) PRIVILEGED_FUNCTION;
537
538 #endif
539
540 /*
541  * Return the amount of time, in ticks, that will pass before the kernel will
542  * next move a task from the Blocked state to the Running state.
543  *
544  * This conditional compilation should use inequality to 0, not equality to 1.
545  * This is to ensure portSUPPRESS_TICKS_AND_SLEEP() can be called when user
546  * defined low power mode implementations require configUSE_TICKLESS_IDLE to be
547  * set to a value other than 1.
548  */
549 #if ( configUSE_TICKLESS_IDLE != 0 )
550
551     static TickType_t prvGetExpectedIdleTime( void ) PRIVILEGED_FUNCTION;
552
553 #endif
554
555 /*
556  * Set xNextTaskUnblockTime to the time at which the next Blocked state task
557  * will exit the Blocked state.
558  */
559 static void prvResetNextTaskUnblockTime( void ) PRIVILEGED_FUNCTION;
560
561 #if ( ( configUSE_TRACE_FACILITY == 1 ) && ( configUSE_STATS_FORMATTING_FUNCTIONS > 0 ) )
562
563 /*
564  * Helper function used to pad task names with spaces when printing out
565  * human readable tables of task information.
566  */
567     static char * prvWriteNameToBuffer( char * pcBuffer,
568                                         const char * pcTaskName ) PRIVILEGED_FUNCTION;
569
570 #endif
571
572 /*
573  * Called after a Task_t structure has been allocated either statically or
574  * dynamically to fill in the structure's members.
575  */
576 static void prvInitialiseNewTask( TaskFunction_t pxTaskCode,
577                                   const char * const pcName, /*lint !e971 Unqualified char types are allowed for strings and single characters only. */
578                                   const uint32_t ulStackDepth,
579                                   void * const pvParameters,
580                                   UBaseType_t uxPriority,
581                                   TaskHandle_t * const pxCreatedTask,
582                                   TCB_t * pxNewTCB,
583                                   const MemoryRegion_t * const xRegions ) PRIVILEGED_FUNCTION;
584
585 /*
586  * Called after a new task has been created and initialised to place the task
587  * under the control of the scheduler.
588  */
589 static void prvAddNewTaskToReadyList( TCB_t * pxNewTCB ) PRIVILEGED_FUNCTION;
590
591 /*
592  * freertos_tasks_c_additions_init() should only be called if the user definable
593  * macro FREERTOS_TASKS_C_ADDITIONS_INIT() is defined, as that is the only macro
594  * called by the function.
595  */
596 #ifdef FREERTOS_TASKS_C_ADDITIONS_INIT
597
598     static void freertos_tasks_c_additions_init( void ) PRIVILEGED_FUNCTION;
599
600 #endif
601
602 /*-----------------------------------------------------------*/
603
604 static BaseType_t prvGetCurrentYieldPending( void )
605 {
606     BaseType_t xReturn;
607     UBaseType_t ulState;
608
609     ulState = portDISABLE_INTERRUPTS();
610     xReturn = xYieldPendings[ portGET_CORE_ID() ];
611     portRESTORE_INTERRUPTS( ulState );
612
613     return xReturn;
614 }
615
616 /*-----------------------------------------------------------*/
617
618 static void prvCheckForRunStateChange( void )
619 {
620     UBaseType_t uxPrevCriticalNesting;
621     UBaseType_t uxPrevSchedulerSuspended;
622     TCB_t * pxThisTCB;
623
624     /* This should be skipped when entering a critical section within
625      * an ISR. If the task on the current core is no longer running, then
626      * vTaskSwitchContext() probably should be run before returning, but
627      * we don't have a way to force that to happen from here. */
628     if( portCHECK_IF_IN_ISR() == pdFALSE )
629     {
630         /* This function is always called with interrupts disabled
631          * so this is safe. */
632         pxThisTCB = pxCurrentTCBs[ portGET_CORE_ID() ];
633
634         while( pxThisTCB->xTaskRunState == taskTASK_YIELDING )
635         {
636             /* We are only here if we just entered a critical section
637              * or if we just suspended the scheduler, and another task
638              * has requested that we yield.
639              *
640              * This is slightly complicated since we need to save and restore
641              * the suspension and critical nesting counts, as well as release
642              * and reacquire the correct locks. And then do it all over again
643              * if our state changed again during the reacquisition. */
644
645             uxPrevCriticalNesting = pxThisTCB->uxCriticalNesting;
646             uxPrevSchedulerSuspended = uxSchedulerSuspended;
647
648             /* this must only be called the first time we enter into a critical
649              * section, otherwise it could context switch in the middle of a
650              * critical section. */
651             configASSERT( uxPrevCriticalNesting + uxPrevSchedulerSuspended == 1U );
652
653             uxSchedulerSuspended = 0U;
654
655             if( uxPrevCriticalNesting > 0U )
656             {
657                 pxThisTCB->uxCriticalNesting = 0U;
658                 portRELEASE_ISR_LOCK();
659                 portRELEASE_TASK_LOCK();
660             }
661             else
662             {
663                 /* uxPrevSchedulerSuspended must be 1 */
664                 portRELEASE_TASK_LOCK();
665             }
666
667             portMEMORY_BARRIER();
668             configASSERT( pxThisTCB->xTaskRunState == taskTASK_YIELDING );
669
670             portENABLE_INTERRUPTS();
671
672             /* Enabling interrupts should cause this core to immediately
673              * service the pending interrupt and yield. If the run state is still
674              * yielding here then that is a problem. */
675             configASSERT( pxThisTCB->xTaskRunState != taskTASK_YIELDING );
676
677             portDISABLE_INTERRUPTS();
678             portGET_TASK_LOCK();
679             portGET_ISR_LOCK();
680             pxCurrentTCB->uxCriticalNesting = uxPrevCriticalNesting;
681             uxSchedulerSuspended = uxPrevSchedulerSuspended;
682
683             if( uxPrevCriticalNesting == 0U )
684             {
685                 /* uxPrevSchedulerSuspended must be 1 */
686                 configASSERT( uxPrevSchedulerSuspended != ( UBaseType_t ) pdFALSE );
687                 portRELEASE_ISR_LOCK();
688             }
689         }
690     }
691 }
692
693 /*-----------------------------------------------------------*/
694
695 static void prvYieldCore( BaseType_t xCoreID )
696 {
697     /* This must be called from a critical section and
698      * xCoreID must be valid. */
699
700     if( portCHECK_IF_IN_ISR() && ( xCoreID == portGET_CORE_ID() ) )
701     {
702         xYieldPendings[ xCoreID ] = pdTRUE;
703     }
704     else if( pxCurrentTCBs[ xCoreID ]->xTaskRunState != taskTASK_YIELDING )
705     {
706         if( xCoreID == portGET_CORE_ID() )
707         {
708             xYieldPendings[ xCoreID ] = pdTRUE;
709         }
710         else
711         {
712             portYIELD_CORE( xCoreID );
713             pxCurrentTCBs[ xCoreID ]->xTaskRunState = taskTASK_YIELDING;
714         }
715     }
716 }
717
718 /*-----------------------------------------------------------*/
719
720 static void prvYieldForTask( TCB_t * pxTCB,
721                              const BaseType_t xPreemptEqualPriority )
722 {
723     BaseType_t xLowestPriority;
724     BaseType_t xTaskPriority;
725     BaseType_t xLowestPriorityCore = -1;
726     BaseType_t xYieldCount = 0;
727     BaseType_t x;
728     TaskRunning_t xTaskRunState;
729
730     /* THIS FUNCTION MUST BE CALLED FROM A CRITICAL SECTION */
731
732     configASSERT( pxCurrentTCB->uxCriticalNesting > 0U );
733
734     #if ( ( configRUN_MULTIPLE_PRIORITIES == 0 ) && ( configNUM_CORES > 1 ) )
735         {
736             /* No task should yield for this one if it is a lower priority
737              * than priority level of currently ready tasks. */
738             if( pxTCB->uxPriority < uxTopReadyPriority )
739             {
740                 return;
741             }
742         }
743     #endif
744
745     xLowestPriority = ( BaseType_t ) pxTCB->uxPriority;
746
747     if( xPreemptEqualPriority == pdFALSE )
748     {
749         /* xLowestPriority will be decremented to -1 if the priority of pxTCB
750          * is 0. This is ok as we will give system idle tasks a priority of -1 below. */
751         --xLowestPriority;
752     }
753
754     for( x = ( BaseType_t ) 0; x < ( BaseType_t ) configNUM_CORES; x++ )
755     {
756         /* System idle tasks are being assigned a priority of tskIDLE_PRIORITY - 1 here */
757         xTaskPriority = ( BaseType_t ) pxCurrentTCBs[ x ]->uxPriority - pxCurrentTCBs[ x ]->xIsIdle;
758         xTaskRunState = pxCurrentTCBs[ x ]->xTaskRunState;
759
760         if( ( taskTASK_IS_RUNNING( xTaskRunState ) != pdFALSE ) && ( xYieldPendings[ x ] == pdFALSE ) )
761         {
762             if( xTaskPriority <= xLowestPriority )
763             {
764                 #if( configNUM_CORES > 1 )
765                 #if ( configUSE_CORE_AFFINITY == 1 )
766                     if( ( pxTCB->uxCoreAffinityMask & ( 1 << x ) ) != 0 )
767                 #endif
768                 #endif
769                 {
770                     #if ( configUSE_TASK_PREEMPTION_DISABLE == 1 )
771                         if( pxCurrentTCBs[ x ]->xPreemptionDisable == pdFALSE )
772                     #endif
773                     {
774                         xLowestPriority = xTaskPriority;
775                         xLowestPriorityCore = x;
776                     }
777                 }
778             }
779             else
780             {
781                 mtCOVERAGE_TEST_MARKER();
782             }
783
784             #if ( ( configRUN_MULTIPLE_PRIORITIES == 0 ) && ( configNUM_CORES > 1 ) ) && 1
785                 {
786                     /* Yield all currently running non-idle tasks with a priority lower than
787                      * the task that needs to run. */
788                     if( ( ( BaseType_t ) tskIDLE_PRIORITY - 1 < xTaskPriority ) && ( xTaskPriority < ( BaseType_t ) pxTCB->uxPriority ) )
789                     {
790                         prvYieldCore( x );
791                         xYieldCount++;
792                     }
793                     else
794                     {
795                         mtCOVERAGE_TEST_MARKER();
796                     }
797                 }
798             #endif /* if ( ( configRUN_MULTIPLE_PRIORITIES == 0 ) && ( configNUM_CORES > 1 ) ) && 1 */
799         }
800         else
801         {
802             mtCOVERAGE_TEST_MARKER();
803         }
804     }
805
806     if( ( xYieldCount == 0 ) && taskVALID_CORE_ID( xLowestPriorityCore ) )
807     {
808         prvYieldCore( xLowestPriorityCore );
809         xYieldCount++;
810     }
811
812     #if ( ( configRUN_MULTIPLE_PRIORITIES == 0 ) && ( configNUM_CORES > 1 ) )
813         /* Verify that the calling core always yields to higher priority tasks */
814         if( !pxCurrentTCBs[ portGET_CORE_ID() ]->xIsIdle && ( pxTCB->uxPriority > pxCurrentTCBs[ portGET_CORE_ID() ]->uxPriority ) )
815         {
816             configASSERT( xYieldPendings[ portGET_CORE_ID() ] == pdTRUE || taskTASK_IS_RUNNING( pxCurrentTCBs[ portGET_CORE_ID() ]->xTaskRunState ) == pdFALSE );
817         }
818     #endif
819 }
820 /*-----------------------------------------------------------*/
821
822 #if ( configUSE_PORT_OPTIMISED_TASK_SELECTION == 0 )
823
824     static BaseType_t prvSelectHighestPriorityTask( const BaseType_t xCoreID )
825     {
826         UBaseType_t uxCurrentPriority = uxTopReadyPriority;
827         BaseType_t xTaskScheduled = pdFALSE;
828         BaseType_t xDecrementTopPriority = pdTRUE;
829
830         #if ( configUSE_CORE_AFFINITY == 1 )
831             TCB_t * pxPreviousTCB = NULL;
832         #endif
833         #if ( ( configRUN_MULTIPLE_PRIORITIES == 0 ) && ( configNUM_CORES > 1 ) )
834             BaseType_t xPriorityDropped = pdFALSE;
835         #endif
836
837         while( xTaskScheduled == pdFALSE )
838         {
839             #if ( ( configRUN_MULTIPLE_PRIORITIES == 0 ) && ( configNUM_CORES > 1 ) )
840                 {
841                     if( uxCurrentPriority < uxTopReadyPriority )
842                     {
843                         /* We can't schedule any tasks, other than idle, that have a
844                          * priority lower than the priority of a task currently running
845                          * on another core. */
846                         uxCurrentPriority = tskIDLE_PRIORITY;
847                     }
848                 }
849             #endif
850
851             if( listLIST_IS_EMPTY( &( pxReadyTasksLists[ uxCurrentPriority ] ) ) == pdFALSE )
852             {
853                 List_t * const pxReadyList = &( pxReadyTasksLists[ uxCurrentPriority ] );
854                 ListItem_t * pxLastTaskItem = pxReadyList->pxIndex->pxPrevious;
855                 ListItem_t * pxTaskItem = pxLastTaskItem;
856
857                 if( ( void * ) pxLastTaskItem == ( void * ) &( pxReadyList->xListEnd ) )
858                 {
859                     pxLastTaskItem = pxLastTaskItem->pxPrevious;
860                 }
861
862                 /* The ready task list for uxCurrentPriority is not empty, so uxTopReadyPriority
863                  * must not be decremented any further */
864                 xDecrementTopPriority = pdFALSE;
865
866                 do
867                 {
868                     TCB_t * pxTCB;
869
870                     pxTaskItem = pxTaskItem->pxNext;
871
872                     if( ( void * ) pxTaskItem == ( void * ) &( pxReadyList->xListEnd ) )
873                     {
874                         pxTaskItem = pxTaskItem->pxNext;
875                     }
876
877                     pxTCB = pxTaskItem->pvOwner;
878
879                     /*debug_printf("Attempting to schedule %s on core %d\n", pxTCB->pcTaskName, portGET_CORE_ID() ); */
880
881                     #if ( ( configRUN_MULTIPLE_PRIORITIES == 0 ) && ( configNUM_CORES > 1 ) )
882                         {
883                             /* When falling back to the idle priority because only one priority
884                              * level is allowed to run at a time, we should ONLY schedule the true
885                              * idle tasks, not user tasks at the idle priority. */
886                             if( uxCurrentPriority < uxTopReadyPriority )
887                             {
888                                 if( pxTCB->xIsIdle == pdFALSE )
889                                 {
890                                     continue;
891                                 }
892                             }
893                         }
894                     #endif /* if ( ( configRUN_MULTIPLE_PRIORITIES == 0 ) && ( configNUM_CORES > 1 ) ) */
895
896                     if( pxTCB->xTaskRunState == taskTASK_NOT_RUNNING )
897                     {
898                         #if ( configNUM_CORES > 1 )
899                         #if ( configUSE_CORE_AFFINITY == 1 )
900                             if( ( pxTCB->uxCoreAffinityMask & ( 1 << xCoreID ) ) != 0 )
901                         #endif
902                         #endif
903                         {
904                             /* If the task is not being executed by any core swap it in */
905                             pxCurrentTCBs[ xCoreID ]->xTaskRunState = taskTASK_NOT_RUNNING;
906                             #if ( configUSE_CORE_AFFINITY == 1 )
907                                 pxPreviousTCB = pxCurrentTCBs[ xCoreID ];
908                             #endif
909                             pxTCB->xTaskRunState = ( TaskRunning_t ) xCoreID;
910                             pxCurrentTCBs[ xCoreID ] = pxTCB;
911                             xTaskScheduled = pdTRUE;
912                         }
913                     }
914                     else if( pxTCB == pxCurrentTCBs[ xCoreID ] )
915                     {
916                         configASSERT( ( pxTCB->xTaskRunState == xCoreID ) || ( pxTCB->xTaskRunState == taskTASK_YIELDING ) );
917                         #if( configNUM_CORES > 1 )
918                         #if ( configUSE_CORE_AFFINITY == 1 )
919                             if( ( pxTCB->uxCoreAffinityMask & ( 1 << xCoreID ) ) != 0 )
920                         #endif
921                         #endif
922                         {
923                             /* The task is already running on this core, mark it as scheduled */
924                             pxTCB->xTaskRunState = ( TaskRunning_t ) xCoreID;
925                             xTaskScheduled = pdTRUE;
926                         }
927                     }
928
929                     if( xTaskScheduled != pdFALSE )
930                     {
931                         /* Once a task has been selected to run on this core,
932                          * move it to the end of the ready task list. */
933                         uxListRemove( pxTaskItem );
934                         vListInsertEnd( pxReadyList, pxTaskItem );
935                         break;
936                     }
937                 } while( pxTaskItem != pxLastTaskItem );
938             }
939             else
940             {
941                 if( xDecrementTopPriority != pdFALSE )
942                 {
943                     uxTopReadyPriority--;
944                     #if ( ( configRUN_MULTIPLE_PRIORITIES == 0 ) && ( configNUM_CORES > 1 ) )
945                         {
946                             xPriorityDropped = pdTRUE;
947                         }
948                     #endif
949                 }
950             }
951
952             /* This function can get called by vTaskSuspend() before the scheduler is started.
953              * In that case, since the idle tasks have not yet been created it is possible that we
954              * won't find a new task to schedule. Return pdFALSE in this case. */
955             if( ( xSchedulerRunning == pdFALSE ) && ( uxCurrentPriority == tskIDLE_PRIORITY ) && ( xTaskScheduled == pdFALSE ) )
956             {
957                 return pdFALSE;
958             }
959
960             configASSERT( ( uxCurrentPriority > tskIDLE_PRIORITY ) || ( xTaskScheduled == pdTRUE ) );
961             uxCurrentPriority--;
962         }
963
964         configASSERT( taskTASK_IS_RUNNING( pxCurrentTCBs[ xCoreID ]->xTaskRunState ) );
965
966         #if ( ( configRUN_MULTIPLE_PRIORITIES == 0 ) && ( configNUM_CORES > 1 ) )
967             if( xPriorityDropped != pdFALSE )
968             {
969                 /* There may be several ready tasks that were being prevented from running because there was
970                  * a higher priority task running. Now that the last of the higher priority tasks is no longer
971                  * running, make sure all the other idle tasks yield. */
972                 UBaseType_t x;
973
974                 for( x = ( BaseType_t ) 0; x < ( BaseType_t ) configNUM_CORES; x++ )
975                 {
976                     if( pxCurrentTCBs[ x ]->xIsIdle != pdFALSE )
977                     {
978                         prvYieldCore( x );
979                     }
980                 }
981             }
982         #endif /* if ( ( configRUN_MULTIPLE_PRIORITIES == 0 ) && ( configNUM_CORES > 1 ) ) */
983
984         #if ( configNUM_CORES > 1 )
985         #if ( configUSE_CORE_AFFINITY == 1 )
986             if( ( pxPreviousTCB != NULL ) && ( listIS_CONTAINED_WITHIN( &( pxReadyTasksLists[ pxPreviousTCB->uxPriority ] ), &( pxPreviousTCB->xStateListItem ) ) != pdFALSE ) )
987             {
988                 /* A ready task was just bumped off this core. Look at the cores it can run from
989                  * from to see if it is able to run on any of them */
990                 UBaseType_t uxCoreMap = pxPreviousTCB->uxCoreAffinityMask;
991                 BaseType_t xLowestPriority = pxPreviousTCB->uxPriority - pxPreviousTCB->xIsIdle;
992                 BaseType_t xLowestPriorityCore = -1;
993
994                 if( ( uxCoreMap & ( 1 << xCoreID ) ) != 0 )
995                 {
996                     /* The ready task that was removed from this core is not excluded from it.
997                      * Only look at the intersection of the cores the removed task is allowed to run
998                      * on with the cores that the new task is excluded from. It is possible that the
999                      * new task was only placed onto this core because it is excluded from another.
1000                      * Check to see if the previous task could run on one of those cores. */
1001                     uxCoreMap &= ~( pxCurrentTCBs[ xCoreID ]->uxCoreAffinityMask );
1002                 }
1003                 else
1004                 {
1005                     /* The ready task that was removed from this core is excluded from it.
1006                      * @todo See if we can schedule it on any of the cores where it is not excluded from. */
1007                 }
1008
1009                 uxCoreMap &= ( ( 1 << configNUM_CORES ) - 1 );
1010
1011                 while( uxCoreMap != 0 )
1012                 {
1013                     int uxCore = 31UL - ( uint32_t ) __builtin_clz( uxCoreMap );
1014
1015                     xassert( taskVALID_CORE_ID( uxCore ) );
1016
1017                     uxCoreMap &= ~( 1 << uxCore );
1018
1019                     BaseType_t xTaskPriority = ( BaseType_t ) pxCurrentTCBs[ uxCore ]->uxPriority - pxCurrentTCBs[ uxCore ]->xIsIdle;
1020
1021                     if( ( xTaskPriority < xLowestPriority ) && ( taskTASK_IS_RUNNING( pxCurrentTCBs[ uxCore ]->xTaskRunState ) != pdFALSE ) && ( xYieldPendings[ uxCore ] == pdFALSE ) )
1022                     {
1023                         #if ( configUSE_TASK_PREEMPTION_DISABLE == 1 )
1024                             if( pxCurrentTCBs[ uxCore ]->xPreemptionDisable == pdFALSE )
1025                         #endif
1026                         {
1027                             xLowestPriority = xTaskPriority;
1028                             xLowestPriorityCore = uxCore;
1029                         }
1030                     }
1031                 }
1032
1033                 if( taskVALID_CORE_ID( xLowestPriorityCore ) )
1034                 {
1035                     prvYieldCore( xLowestPriorityCore );
1036                 }
1037             }
1038         #endif /* if ( configUSE_CORE_AFFINITY == 1 ) */
1039         #endif /* if ( configNUM_CORES > 1 ) */
1040
1041         return pdTRUE;
1042     }
1043
1044 #else /* configUSE_PORT_OPTIMISED_TASK_SELECTION */
1045
1046     static void prvSelectHighestPriorityTask( BaseType_t xCoreID )
1047     {
1048         UBaseType_t uxTopPriority;
1049
1050         /* Find the highest priority list that contains ready tasks. */
1051         portGET_HIGHEST_PRIORITY( uxTopPriority, uxTopReadyPriority );
1052         configASSERT( listCURRENT_LIST_LENGTH( &( pxReadyTasksLists[ uxTopPriority ] ) ) > 0 );
1053         listGET_OWNER_OF_NEXT_ENTRY( pxCurrentTCB, &( pxReadyTasksLists[ uxTopPriority ] ) );
1054     }
1055
1056 #endif /* configUSE_PORT_OPTIMISED_TASK_SELECTION */
1057 /*-----------------------------------------------------------*/
1058
1059 #if ( configSUPPORT_STATIC_ALLOCATION == 1 )
1060
1061     TaskHandle_t xTaskCreateStatic( TaskFunction_t pxTaskCode,
1062                                     const char * const pcName, /*lint !e971 Unqualified char types are allowed for strings and single characters only. */
1063                                     const uint32_t ulStackDepth,
1064                                     void * const pvParameters,
1065                                     UBaseType_t uxPriority,
1066                                     StackType_t * const puxStackBuffer,
1067                                     StaticTask_t * const pxTaskBuffer )
1068     {
1069         TCB_t * pxNewTCB;
1070         TaskHandle_t xReturn;
1071
1072         configASSERT( puxStackBuffer != NULL );
1073         configASSERT( pxTaskBuffer != NULL );
1074
1075         #if ( configASSERT_DEFINED == 1 )
1076             {
1077                 /* Sanity check that the size of the structure used to declare a
1078                  * variable of type StaticTask_t equals the size of the real task
1079                  * structure. */
1080                 volatile size_t xSize = sizeof( StaticTask_t );
1081                 configASSERT( xSize == sizeof( TCB_t ) );
1082                 ( void ) xSize; /* Prevent lint warning when configASSERT() is not used. */
1083             }
1084         #endif /* configASSERT_DEFINED */
1085
1086         if( ( pxTaskBuffer != NULL ) && ( puxStackBuffer != NULL ) )
1087         {
1088             /* The memory used for the task's TCB and stack are passed into this
1089              * function - use them. */
1090             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. */
1091             pxNewTCB->pxStack = ( StackType_t * ) puxStackBuffer;
1092
1093             #if ( tskSTATIC_AND_DYNAMIC_ALLOCATION_POSSIBLE != 0 ) /*lint !e731 !e9029 Macro has been consolidated for readability reasons. */
1094                 {
1095                     /* Tasks can be created statically or dynamically, so note this
1096                      * task was created statically in case the task is later deleted. */
1097                     pxNewTCB->ucStaticallyAllocated = tskSTATICALLY_ALLOCATED_STACK_AND_TCB;
1098                 }
1099             #endif /* tskSTATIC_AND_DYNAMIC_ALLOCATION_POSSIBLE */
1100
1101             prvInitialiseNewTask( pxTaskCode, pcName, ulStackDepth, pvParameters, uxPriority, &xReturn, pxNewTCB, NULL );
1102             prvAddNewTaskToReadyList( pxNewTCB );
1103         }
1104         else
1105         {
1106             xReturn = NULL;
1107         }
1108
1109         return xReturn;
1110     }
1111
1112 #endif /* SUPPORT_STATIC_ALLOCATION */
1113 /*-----------------------------------------------------------*/
1114
1115 #if ( ( portUSING_MPU_WRAPPERS == 1 ) && ( configSUPPORT_STATIC_ALLOCATION == 1 ) )
1116
1117     BaseType_t xTaskCreateRestrictedStatic( const TaskParameters_t * const pxTaskDefinition,
1118                                             TaskHandle_t * pxCreatedTask )
1119     {
1120         TCB_t * pxNewTCB;
1121         BaseType_t xReturn = errCOULD_NOT_ALLOCATE_REQUIRED_MEMORY;
1122
1123         configASSERT( pxTaskDefinition->puxStackBuffer != NULL );
1124         configASSERT( pxTaskDefinition->pxTaskBuffer != NULL );
1125
1126         if( ( pxTaskDefinition->puxStackBuffer != NULL ) && ( pxTaskDefinition->pxTaskBuffer != NULL ) )
1127         {
1128             /* Allocate space for the TCB.  Where the memory comes from depends
1129              * on the implementation of the port malloc function and whether or
1130              * not static allocation is being used. */
1131             pxNewTCB = ( TCB_t * ) pxTaskDefinition->pxTaskBuffer;
1132
1133             /* Store the stack location in the TCB. */
1134             pxNewTCB->pxStack = pxTaskDefinition->puxStackBuffer;
1135
1136             #if ( tskSTATIC_AND_DYNAMIC_ALLOCATION_POSSIBLE != 0 )
1137                 {
1138                     /* Tasks can be created statically or dynamically, so note this
1139                      * task was created statically in case the task is later deleted. */
1140                     pxNewTCB->ucStaticallyAllocated = tskSTATICALLY_ALLOCATED_STACK_AND_TCB;
1141                 }
1142             #endif /* tskSTATIC_AND_DYNAMIC_ALLOCATION_POSSIBLE */
1143
1144             prvInitialiseNewTask( pxTaskDefinition->pvTaskCode,
1145                                   pxTaskDefinition->pcName,
1146                                   ( uint32_t ) pxTaskDefinition->usStackDepth,
1147                                   pxTaskDefinition->pvParameters,
1148                                   pxTaskDefinition->uxPriority,
1149                                   pxCreatedTask, pxNewTCB,
1150                                   pxTaskDefinition->xRegions );
1151
1152             prvAddNewTaskToReadyList( pxNewTCB );
1153             xReturn = pdPASS;
1154         }
1155
1156         return xReturn;
1157     }
1158
1159 #endif /* ( portUSING_MPU_WRAPPERS == 1 ) && ( configSUPPORT_STATIC_ALLOCATION == 1 ) */
1160 /*-----------------------------------------------------------*/
1161
1162 #if ( ( portUSING_MPU_WRAPPERS == 1 ) && ( configSUPPORT_DYNAMIC_ALLOCATION == 1 ) )
1163
1164     BaseType_t xTaskCreateRestricted( const TaskParameters_t * const pxTaskDefinition,
1165                                       TaskHandle_t * pxCreatedTask )
1166     {
1167         TCB_t * pxNewTCB;
1168         BaseType_t xReturn = errCOULD_NOT_ALLOCATE_REQUIRED_MEMORY;
1169
1170         configASSERT( pxTaskDefinition->puxStackBuffer );
1171
1172         if( pxTaskDefinition->puxStackBuffer != NULL )
1173         {
1174             /* Allocate space for the TCB.  Where the memory comes from depends
1175              * on the implementation of the port malloc function and whether or
1176              * not static allocation is being used. */
1177             pxNewTCB = ( TCB_t * ) pvPortMalloc( sizeof( TCB_t ) );
1178
1179             if( pxNewTCB != NULL )
1180             {
1181                 /* Store the stack location in the TCB. */
1182                 pxNewTCB->pxStack = pxTaskDefinition->puxStackBuffer;
1183
1184                 #if ( tskSTATIC_AND_DYNAMIC_ALLOCATION_POSSIBLE != 0 )
1185                     {
1186                         /* Tasks can be created statically or dynamically, so note
1187                          * this task had a statically allocated stack in case it is
1188                          * later deleted.  The TCB was allocated dynamically. */
1189                         pxNewTCB->ucStaticallyAllocated = tskSTATICALLY_ALLOCATED_STACK_ONLY;
1190                     }
1191                 #endif /* tskSTATIC_AND_DYNAMIC_ALLOCATION_POSSIBLE */
1192
1193                 prvInitialiseNewTask( pxTaskDefinition->pvTaskCode,
1194                                       pxTaskDefinition->pcName,
1195                                       ( uint32_t ) pxTaskDefinition->usStackDepth,
1196                                       pxTaskDefinition->pvParameters,
1197                                       pxTaskDefinition->uxPriority,
1198                                       pxCreatedTask, pxNewTCB,
1199                                       pxTaskDefinition->xRegions );
1200
1201                 prvAddNewTaskToReadyList( pxNewTCB );
1202                 xReturn = pdPASS;
1203             }
1204         }
1205
1206         return xReturn;
1207     }
1208
1209 #endif /* portUSING_MPU_WRAPPERS */
1210 /*-----------------------------------------------------------*/
1211
1212 #if ( configSUPPORT_DYNAMIC_ALLOCATION == 1 )
1213
1214     BaseType_t xTaskCreate( TaskFunction_t pxTaskCode,
1215                             const char * const pcName, /*lint !e971 Unqualified char types are allowed for strings and single characters only. */
1216                             const configSTACK_DEPTH_TYPE usStackDepth,
1217                             void * const pvParameters,
1218                             UBaseType_t uxPriority,
1219                             TaskHandle_t * const pxCreatedTask )
1220     {
1221         TCB_t * pxNewTCB;
1222         BaseType_t xReturn;
1223
1224         /* If the stack grows down then allocate the stack then the TCB so the stack
1225          * does not grow into the TCB.  Likewise if the stack grows up then allocate
1226          * the TCB then the stack. */
1227         #if ( portSTACK_GROWTH > 0 )
1228             {
1229                 /* Allocate space for the TCB.  Where the memory comes from depends on
1230                  * the implementation of the port malloc function and whether or not static
1231                  * allocation is being used. */
1232                 pxNewTCB = ( TCB_t * ) pvPortMalloc( sizeof( TCB_t ) );
1233
1234                 if( pxNewTCB != NULL )
1235                 {
1236                     /* Allocate space for the stack used by the task being created.
1237                      * The base of the stack memory stored in the TCB so the task can
1238                      * be deleted later if required. */
1239                     pxNewTCB->pxStack = ( StackType_t * ) pvPortMallocStack( ( ( ( size_t ) usStackDepth ) * sizeof( StackType_t ) ) ); /*lint !e961 MISRA exception as the casts are only redundant for some ports. */
1240
1241                     if( pxNewTCB->pxStack == NULL )
1242                     {
1243                         /* Could not allocate the stack.  Delete the allocated TCB. */
1244                         vPortFree( pxNewTCB );
1245                         pxNewTCB = NULL;
1246                     }
1247                 }
1248             }
1249         #else /* portSTACK_GROWTH */
1250             {
1251                 StackType_t * pxStack;
1252
1253                 /* Allocate space for the stack used by the task being created. */
1254                 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. */
1255
1256                 if( pxStack != NULL )
1257                 {
1258                     /* Allocate space for the TCB. */
1259                     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. */
1260
1261                     if( pxNewTCB != NULL )
1262                     {
1263                         /* Store the stack location in the TCB. */
1264                         pxNewTCB->pxStack = pxStack;
1265                     }
1266                     else
1267                     {
1268                         /* The stack cannot be used as the TCB was not created.  Free
1269                          * it again. */
1270                         vPortFreeStack( pxStack );
1271                     }
1272                 }
1273                 else
1274                 {
1275                     pxNewTCB = NULL;
1276                 }
1277             }
1278         #endif /* portSTACK_GROWTH */
1279
1280         if( pxNewTCB != NULL )
1281         {
1282             #if ( tskSTATIC_AND_DYNAMIC_ALLOCATION_POSSIBLE != 0 ) /*lint !e9029 !e731 Macro has been consolidated for readability reasons. */
1283                 {
1284                     /* Tasks can be created statically or dynamically, so note this
1285                      * task was created dynamically in case it is later deleted. */
1286                     pxNewTCB->ucStaticallyAllocated = tskDYNAMICALLY_ALLOCATED_STACK_AND_TCB;
1287                 }
1288             #endif /* tskSTATIC_AND_DYNAMIC_ALLOCATION_POSSIBLE */
1289
1290             prvInitialiseNewTask( pxTaskCode, pcName, ( uint32_t ) usStackDepth, pvParameters, uxPriority, pxCreatedTask, pxNewTCB, NULL );
1291             prvAddNewTaskToReadyList( pxNewTCB );
1292             xReturn = pdPASS;
1293         }
1294         else
1295         {
1296             xReturn = errCOULD_NOT_ALLOCATE_REQUIRED_MEMORY;
1297         }
1298
1299         return xReturn;
1300     }
1301
1302 #endif /* configSUPPORT_DYNAMIC_ALLOCATION */
1303 /*-----------------------------------------------------------*/
1304
1305 static void prvInitialiseNewTask( TaskFunction_t pxTaskCode,
1306                                   const char * const pcName, /*lint !e971 Unqualified char types are allowed for strings and single characters only. */
1307                                   const uint32_t ulStackDepth,
1308                                   void * const pvParameters,
1309                                   UBaseType_t uxPriority,
1310                                   TaskHandle_t * const pxCreatedTask,
1311                                   TCB_t * pxNewTCB,
1312                                   const MemoryRegion_t * const xRegions )
1313 {
1314     StackType_t * pxTopOfStack;
1315     UBaseType_t x;
1316
1317     #if ( portUSING_MPU_WRAPPERS == 1 )
1318         /* Should the task be created in privileged mode? */
1319         BaseType_t xRunPrivileged;
1320
1321         if( ( uxPriority & portPRIVILEGE_BIT ) != 0U )
1322         {
1323             xRunPrivileged = pdTRUE;
1324         }
1325         else
1326         {
1327             xRunPrivileged = pdFALSE;
1328         }
1329         uxPriority &= ~portPRIVILEGE_BIT;
1330     #endif /* portUSING_MPU_WRAPPERS == 1 */
1331
1332     /* Avoid dependency on memset() if it is not required. */
1333     #if ( tskSET_NEW_STACKS_TO_KNOWN_VALUE == 1 )
1334         {
1335             /* Fill the stack with a known value to assist debugging. */
1336             ( void ) memset( pxNewTCB->pxStack, ( int ) tskSTACK_FILL_BYTE, ( size_t ) ulStackDepth * sizeof( StackType_t ) );
1337         }
1338     #endif /* tskSET_NEW_STACKS_TO_KNOWN_VALUE */
1339
1340     /* Calculate the top of stack address.  This depends on whether the stack
1341      * grows from high memory to low (as per the 80x86) or vice versa.
1342      * portSTACK_GROWTH is used to make the result positive or negative as required
1343      * by the port. */
1344     #if ( portSTACK_GROWTH < 0 )
1345         {
1346             pxTopOfStack = &( pxNewTCB->pxStack[ ulStackDepth - ( uint32_t ) 1 ] );
1347             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(). */
1348
1349             /* Check the alignment of the calculated top of stack is correct. */
1350             configASSERT( ( ( ( portPOINTER_SIZE_TYPE ) pxTopOfStack & ( portPOINTER_SIZE_TYPE ) portBYTE_ALIGNMENT_MASK ) == 0UL ) );
1351
1352             #if ( configRECORD_STACK_HIGH_ADDRESS == 1 )
1353                 {
1354                     /* Also record the stack's high address, which may assist
1355                      * debugging. */
1356                     pxNewTCB->pxEndOfStack = pxTopOfStack;
1357                 }
1358             #endif /* configRECORD_STACK_HIGH_ADDRESS */
1359         }
1360     #else /* portSTACK_GROWTH */
1361         {
1362             pxTopOfStack = pxNewTCB->pxStack;
1363
1364             /* Check the alignment of the stack buffer is correct. */
1365             configASSERT( ( ( ( portPOINTER_SIZE_TYPE ) pxNewTCB->pxStack & ( portPOINTER_SIZE_TYPE ) portBYTE_ALIGNMENT_MASK ) == 0UL ) );
1366
1367             /* The other extreme of the stack space is required if stack checking is
1368              * performed. */
1369             pxNewTCB->pxEndOfStack = pxNewTCB->pxStack + ( ulStackDepth - ( uint32_t ) 1 );
1370         }
1371     #endif /* portSTACK_GROWTH */
1372
1373     /* Store the task name in the TCB. */
1374     if( pcName != NULL )
1375     {
1376         for( x = ( UBaseType_t ) 0; x < ( UBaseType_t ) configMAX_TASK_NAME_LEN; x++ )
1377         {
1378             pxNewTCB->pcTaskName[ x ] = pcName[ x ];
1379
1380             /* Don't copy all configMAX_TASK_NAME_LEN if the string is shorter than
1381              * configMAX_TASK_NAME_LEN characters just in case the memory after the
1382              * string is not accessible (extremely unlikely). */
1383             if( pcName[ x ] == ( char ) 0x00 )
1384             {
1385                 break;
1386             }
1387             else
1388             {
1389                 mtCOVERAGE_TEST_MARKER();
1390             }
1391         }
1392
1393         /* Ensure the name string is terminated in the case that the string length
1394          * was greater or equal to configMAX_TASK_NAME_LEN. */
1395         pxNewTCB->pcTaskName[ configMAX_TASK_NAME_LEN - 1 ] = '\0';
1396     }
1397     else
1398     {
1399         /* The task has not been given a name, so just ensure there is a NULL
1400          * terminator when it is read out. */
1401         pxNewTCB->pcTaskName[ 0 ] = 0x00;
1402     }
1403
1404     /* This is used as an array index so must ensure it's not too large.  First
1405      * remove the privilege bit if one is present. */
1406     if( uxPriority >= ( UBaseType_t ) configMAX_PRIORITIES )
1407     {
1408         uxPriority = ( UBaseType_t ) configMAX_PRIORITIES - ( UBaseType_t ) 1U;
1409     }
1410     else
1411     {
1412         mtCOVERAGE_TEST_MARKER();
1413     }
1414
1415     pxNewTCB->uxPriority = uxPriority;
1416     #if ( configUSE_MUTEXES == 1 )
1417         {
1418             pxNewTCB->uxBasePriority = uxPriority;
1419             pxNewTCB->uxMutexesHeld = 0;
1420         }
1421     #endif /* configUSE_MUTEXES */
1422
1423     vListInitialiseItem( &( pxNewTCB->xStateListItem ) );
1424     vListInitialiseItem( &( pxNewTCB->xEventListItem ) );
1425
1426     /* Set the pxNewTCB as a link back from the ListItem_t.  This is so we can get
1427      * back to  the containing TCB from a generic item in a list. */
1428     listSET_LIST_ITEM_OWNER( &( pxNewTCB->xStateListItem ), pxNewTCB );
1429
1430     /* Event lists are always in priority order. */
1431     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. */
1432     listSET_LIST_ITEM_OWNER( &( pxNewTCB->xEventListItem ), pxNewTCB );
1433
1434     #if ( portCRITICAL_NESTING_IN_TCB == 1 )
1435         {
1436             pxNewTCB->uxCriticalNesting = ( UBaseType_t ) 0U;
1437         }
1438     #endif /* portCRITICAL_NESTING_IN_TCB */
1439
1440     #if ( configUSE_APPLICATION_TASK_TAG == 1 )
1441         {
1442             pxNewTCB->pxTaskTag = NULL;
1443         }
1444     #endif /* configUSE_APPLICATION_TASK_TAG */
1445
1446     #if ( configGENERATE_RUN_TIME_STATS == 1 )
1447         {
1448             pxNewTCB->ulRunTimeCounter = 0UL;
1449         }
1450     #endif /* configGENERATE_RUN_TIME_STATS */
1451
1452     #if ( portUSING_MPU_WRAPPERS == 1 )
1453         {
1454             vPortStoreTaskMPUSettings( &( pxNewTCB->xMPUSettings ), xRegions, pxNewTCB->pxStack, ulStackDepth );
1455         }
1456     #else
1457         {
1458             /* Avoid compiler warning about unreferenced parameter. */
1459             ( void ) xRegions;
1460         }
1461     #endif
1462
1463     #if ( configNUM_THREAD_LOCAL_STORAGE_POINTERS != 0 )
1464         {
1465             memset( ( void * ) &( pxNewTCB->pvThreadLocalStoragePointers[ 0 ] ), 0x00, sizeof( pxNewTCB->pvThreadLocalStoragePointers ) );
1466         }
1467     #endif
1468
1469     #if ( configUSE_TASK_NOTIFICATIONS == 1 )
1470         {
1471             memset( ( void * ) &( pxNewTCB->ulNotifiedValue[ 0 ] ), 0x00, sizeof( pxNewTCB->ulNotifiedValue ) );
1472             memset( ( void * ) &( pxNewTCB->ucNotifyState[ 0 ] ), 0x00, sizeof( pxNewTCB->ucNotifyState ) );
1473         }
1474     #endif
1475
1476     #if ( configUSE_NEWLIB_REENTRANT == 1 )
1477         {
1478             /* Initialise this task's Newlib reent structure.
1479              * See the third party link http://www.nadler.com/embedded/newlibAndFreeRTOS.html
1480              * for additional information. */
1481             _REENT_INIT_PTR( ( &( pxNewTCB->xNewLib_reent ) ) );
1482         }
1483     #endif
1484
1485     #if ( INCLUDE_xTaskAbortDelay == 1 )
1486         {
1487             pxNewTCB->ucDelayAborted = pdFALSE;
1488         }
1489     #endif
1490
1491     #if ( configNUM_CORES > 1 )
1492     #if ( configUSE_CORE_AFFINITY == 1 )
1493         {
1494             pxNewTCB->uxCoreAffinityMask = tskNO_AFFINITY;
1495         }
1496     #endif
1497     #endif
1498     #if ( configUSE_TASK_PREEMPTION_DISABLE == 1 )
1499         {
1500             pxNewTCB->xPreemptionDisable = 0;
1501         }
1502     #endif
1503
1504     /* Initialize the TCB stack to look as if the task was already running,
1505      * but had been interrupted by the scheduler.  The return address is set
1506      * to the start of the task function. Once the stack has been initialised
1507      * the top of stack variable is updated. */
1508     #if ( portUSING_MPU_WRAPPERS == 1 )
1509         {
1510             /* If the port has capability to detect stack overflow,
1511              * pass the stack end address to the stack initialization
1512              * function as well. */
1513             #if ( portHAS_STACK_OVERFLOW_CHECKING == 1 )
1514                 {
1515                     #if ( portSTACK_GROWTH < 0 )
1516                         {
1517                             pxNewTCB->pxTopOfStack = pxPortInitialiseStack( pxTopOfStack, pxNewTCB->pxStack, pxTaskCode, pvParameters, xRunPrivileged );
1518                         }
1519                     #else /* portSTACK_GROWTH */
1520                         {
1521                             pxNewTCB->pxTopOfStack = pxPortInitialiseStack( pxTopOfStack, pxNewTCB->pxEndOfStack, pxTaskCode, pvParameters, xRunPrivileged );
1522                         }
1523                     #endif /* portSTACK_GROWTH */
1524                 }
1525             #else /* portHAS_STACK_OVERFLOW_CHECKING */
1526                 {
1527                     pxNewTCB->pxTopOfStack = pxPortInitialiseStack( pxTopOfStack, pxTaskCode, pvParameters, xRunPrivileged );
1528                 }
1529             #endif /* portHAS_STACK_OVERFLOW_CHECKING */
1530         }
1531     #else /* portUSING_MPU_WRAPPERS */
1532         {
1533             /* If the port has capability to detect stack overflow,
1534              * pass the stack end address to the stack initialization
1535              * function as well. */
1536             #if ( portHAS_STACK_OVERFLOW_CHECKING == 1 )
1537                 {
1538                     #if ( portSTACK_GROWTH < 0 )
1539                         {
1540                             pxNewTCB->pxTopOfStack = pxPortInitialiseStack( pxTopOfStack, pxNewTCB->pxStack, pxTaskCode, pvParameters );
1541                         }
1542                     #else /* portSTACK_GROWTH */
1543                         {
1544                             pxNewTCB->pxTopOfStack = pxPortInitialiseStack( pxTopOfStack, pxNewTCB->pxEndOfStack, pxTaskCode, pvParameters );
1545                         }
1546                     #endif /* portSTACK_GROWTH */
1547                 }
1548             #else /* portHAS_STACK_OVERFLOW_CHECKING */
1549                 {
1550                     pxNewTCB->pxTopOfStack = pxPortInitialiseStack( pxTopOfStack, pxTaskCode, pvParameters );
1551                 }
1552             #endif /* portHAS_STACK_OVERFLOW_CHECKING */
1553         }
1554     #endif /* portUSING_MPU_WRAPPERS */
1555
1556     /* Initialize to not running */
1557     pxNewTCB->xTaskRunState = taskTASK_NOT_RUNNING;
1558
1559     /* Is this an idle task? */
1560     if(pxTaskCode == prvIdleTask)
1561     {
1562         pxNewTCB->xIsIdle = pdTRUE;
1563     }
1564     #if(configNUM_CORES > 1)
1565     else if(pxTaskCode == prvMinimalIdleTask)
1566     {
1567         pxNewTCB->xIsIdle = pdTRUE;
1568     }
1569     #endif
1570     else
1571     {
1572         pxNewTCB->xIsIdle = pdFALSE;
1573     }
1574
1575     if( pxCreatedTask != NULL )
1576     {
1577         /* Pass the handle out in an anonymous way.  The handle can be used to
1578          * change the created task's priority, delete the created task, etc.*/
1579         *pxCreatedTask = ( TaskHandle_t ) pxNewTCB;
1580     }
1581     else
1582     {
1583         mtCOVERAGE_TEST_MARKER();
1584     }
1585 }
1586 /*-----------------------------------------------------------*/
1587
1588 static void prvAddNewTaskToReadyList( TCB_t * pxNewTCB )
1589 {
1590     /* Ensure interrupts don't access the task lists while the lists are being
1591      * updated. */
1592     taskENTER_CRITICAL();
1593     {
1594         uxCurrentNumberOfTasks++;
1595
1596         if( xSchedulerRunning == pdFALSE )
1597         {
1598             if( uxCurrentNumberOfTasks == ( UBaseType_t ) 1 )
1599             {
1600                 /* This is the first task to be created so do the preliminary
1601                  * initialisation required.  We will not recover if this call
1602                  * fails, but we will report the failure. */
1603                 prvInitialiseTaskLists();
1604             }
1605             else
1606             {
1607                 mtCOVERAGE_TEST_MARKER();
1608             }
1609
1610             if( pxNewTCB->xIsIdle != pdFALSE )
1611             {
1612                 BaseType_t xCoreID;
1613
1614                 /* Check if a core is free. */
1615                 for( xCoreID = ( UBaseType_t ) 0; xCoreID < ( UBaseType_t ) configNUM_CORES; xCoreID++ )
1616                 {
1617                     if( pxCurrentTCBs[ xCoreID ] == NULL )
1618                     {
1619                         pxNewTCB->xTaskRunState = xCoreID;
1620                         /* This section of code pins the idle tasks to cores.
1621                         #if ( configUSE_CORE_AFFINITY == 1 )
1622                             {
1623                                 pxNewTCB->uxCoreAffinityMask = ( 1 << xCoreID );
1624                             }
1625                         #endif
1626                         */
1627                         pxCurrentTCBs[ xCoreID ] = pxNewTCB;
1628                         break;
1629                     }
1630                 }
1631             }
1632         }
1633         else
1634         {
1635             mtCOVERAGE_TEST_MARKER();
1636         }
1637
1638         uxTaskNumber++;
1639
1640         #if ( configUSE_TRACE_FACILITY == 1 )
1641             {
1642                 /* Add a counter into the TCB for tracing only. */
1643                 pxNewTCB->uxTCBNumber = uxTaskNumber;
1644             }
1645         #endif /* configUSE_TRACE_FACILITY */
1646         traceTASK_CREATE( pxNewTCB );
1647
1648         prvAddTaskToReadyList( pxNewTCB );
1649
1650         portSETUP_TCB( pxNewTCB );
1651
1652         if( xSchedulerRunning != pdFALSE )
1653         {
1654             /* If the created task is of a higher priority than another
1655              * currently running task and preemption is on then it should
1656              * run now. */
1657             #if ( configUSE_PREEMPTION == 1 )
1658                 prvYieldForTask( pxNewTCB, pdFALSE );
1659             #endif
1660         }
1661         else
1662         {
1663             mtCOVERAGE_TEST_MARKER();
1664         }
1665     }
1666     taskEXIT_CRITICAL();
1667 }
1668 /*-----------------------------------------------------------*/
1669
1670 #if ( INCLUDE_vTaskDelete == 1 )
1671
1672     void vTaskDelete( TaskHandle_t xTaskToDelete )
1673     {
1674         TCB_t * pxTCB;
1675         TaskRunning_t xTaskRunningOnCore;
1676
1677         taskENTER_CRITICAL();
1678         {
1679             /* If null is passed in here then it is the calling task that is
1680              * being deleted. */
1681             pxTCB = prvGetTCBFromHandle( xTaskToDelete );
1682
1683             xTaskRunningOnCore = pxTCB->xTaskRunState;
1684
1685             /* Remove task from the ready/delayed list. */
1686             if( uxListRemove( &( pxTCB->xStateListItem ) ) == ( UBaseType_t ) 0 )
1687             {
1688                 taskRESET_READY_PRIORITY( pxTCB->uxPriority );
1689             }
1690             else
1691             {
1692                 mtCOVERAGE_TEST_MARKER();
1693             }
1694
1695             /* Is the task waiting on an event also? */
1696             if( listLIST_ITEM_CONTAINER( &( pxTCB->xEventListItem ) ) != NULL )
1697             {
1698                 ( void ) uxListRemove( &( pxTCB->xEventListItem ) );
1699             }
1700             else
1701             {
1702                 mtCOVERAGE_TEST_MARKER();
1703             }
1704
1705             /* Increment the uxTaskNumber also so kernel aware debuggers can
1706              * detect that the task lists need re-generating.  This is done before
1707              * portPRE_TASK_DELETE_HOOK() as in the Windows port that macro will
1708              * not return. */
1709             uxTaskNumber++;
1710
1711             /* If the task is running (or yielding), we must add it to the
1712              * termination list so that an idle task can delete it when it is
1713              * no longer running. */
1714             if( xTaskRunningOnCore != taskTASK_NOT_RUNNING )
1715             {
1716
1717                 /* A running task is being deleted.  This cannot complete within the
1718                  * task itself, as a context switch to another task is required.
1719                  * Place the task in the termination list.  The idle task will
1720                  * check the termination list and free up any memory allocated by
1721                  * the scheduler for the TCB and stack of the deleted task. */
1722                 vListInsertEnd( &xTasksWaitingTermination, &( pxTCB->xStateListItem ) );
1723
1724                 /* Increment the ucTasksDeleted variable so the idle task knows
1725                  * there is a task that has been deleted and that it should therefore
1726                  * check the xTasksWaitingTermination list. */
1727                 ++uxDeletedTasksWaitingCleanUp;
1728
1729                 /* Call the delete hook before portPRE_TASK_DELETE_HOOK() as
1730                  * portPRE_TASK_DELETE_HOOK() does not return in the Win32 port. */
1731                 traceTASK_DELETE( pxTCB );
1732
1733                 /* The pre-delete hook is primarily for the Windows simulator,
1734                  * in which Windows specific clean up operations are performed,
1735                  * after which it is not possible to yield away from this task -
1736                  * hence xYieldPending is used to latch that a context switch is
1737                  * required. */
1738                 portPRE_TASK_DELETE_HOOK( pxTCB, &xYieldPendings[ pxTCB->xTaskRunState ] );
1739             }
1740             else
1741             {
1742                 --uxCurrentNumberOfTasks;
1743                 traceTASK_DELETE( pxTCB );
1744                 prvDeleteTCB( pxTCB );
1745
1746                 /* Reset the next expected unblock time in case it referred to
1747                  * the task that has just been deleted. */
1748                 prvResetNextTaskUnblockTime();
1749             }
1750
1751             /* Force a reschedule if the task that has just been deleted was running. */
1752             if( ( xSchedulerRunning != pdFALSE ) && ( taskTASK_IS_RUNNING( xTaskRunningOnCore ) ) )
1753             {
1754                 BaseType_t xCoreID;
1755
1756                 xCoreID = portGET_CORE_ID();
1757
1758
1759                 if( xTaskRunningOnCore == xCoreID )
1760                 {
1761                     configASSERT( uxSchedulerSuspended == 0 );
1762                     vTaskYieldWithinAPI();
1763                 }
1764                 else
1765                 {
1766                     prvYieldCore( xTaskRunningOnCore );
1767                 }
1768             }
1769         }
1770         taskEXIT_CRITICAL();
1771     }
1772
1773 #endif /* INCLUDE_vTaskDelete */
1774 /*-----------------------------------------------------------*/
1775
1776 #if ( INCLUDE_xTaskDelayUntil == 1 )
1777
1778     BaseType_t xTaskDelayUntil( TickType_t * const pxPreviousWakeTime,
1779                                 const TickType_t xTimeIncrement )
1780     {
1781         TickType_t xTimeToWake;
1782         BaseType_t xAlreadyYielded, xShouldDelay = pdFALSE;
1783
1784         configASSERT( pxPreviousWakeTime );
1785         configASSERT( ( xTimeIncrement > 0U ) );
1786
1787         vTaskSuspendAll();
1788         {
1789             configASSERT( uxSchedulerSuspended == 1 );
1790
1791             /* Minor optimisation.  The tick count cannot change in this
1792              * block. */
1793             const TickType_t xConstTickCount = xTickCount;
1794
1795             /* Generate the tick time at which the task wants to wake. */
1796             xTimeToWake = *pxPreviousWakeTime + xTimeIncrement;
1797
1798             if( xConstTickCount < *pxPreviousWakeTime )
1799             {
1800                 /* The tick count has overflowed since this function was
1801                  * lasted called.  In this case the only time we should ever
1802                  * actually delay is if the wake time has also  overflowed,
1803                  * and the wake time is greater than the tick time.  When this
1804                  * is the case it is as if neither time had overflowed. */
1805                 if( ( xTimeToWake < *pxPreviousWakeTime ) && ( xTimeToWake > xConstTickCount ) )
1806                 {
1807                     xShouldDelay = pdTRUE;
1808                 }
1809                 else
1810                 {
1811                     mtCOVERAGE_TEST_MARKER();
1812                 }
1813             }
1814             else
1815             {
1816                 /* The tick time has not overflowed.  In this case we will
1817                  * delay if either the wake time has overflowed, and/or the
1818                  * tick time is less than the wake time. */
1819                 if( ( xTimeToWake < *pxPreviousWakeTime ) || ( xTimeToWake > xConstTickCount ) )
1820                 {
1821                     xShouldDelay = pdTRUE;
1822                 }
1823                 else
1824                 {
1825                     mtCOVERAGE_TEST_MARKER();
1826                 }
1827             }
1828
1829             /* Update the wake time ready for the next call. */
1830             *pxPreviousWakeTime = xTimeToWake;
1831
1832             if( xShouldDelay != pdFALSE )
1833             {
1834                 traceTASK_DELAY_UNTIL( xTimeToWake );
1835
1836                 /* prvAddCurrentTaskToDelayedList() needs the block time, not
1837                  * the time to wake, so subtract the current tick count. */
1838                 prvAddCurrentTaskToDelayedList( xTimeToWake - xConstTickCount, pdFALSE );
1839             }
1840             else
1841             {
1842                 mtCOVERAGE_TEST_MARKER();
1843             }
1844         }
1845         xAlreadyYielded = xTaskResumeAll();
1846
1847         /* Force a reschedule if xTaskResumeAll has not already done so, we may
1848          * have put ourselves to sleep. */
1849         if( xAlreadyYielded == pdFALSE )
1850         {
1851             vTaskYieldWithinAPI();
1852         }
1853         else
1854         {
1855             mtCOVERAGE_TEST_MARKER();
1856         }
1857
1858         return xShouldDelay;
1859     }
1860
1861 #endif /* INCLUDE_xTaskDelayUntil */
1862 /*-----------------------------------------------------------*/
1863
1864 #if ( INCLUDE_vTaskDelay == 1 )
1865
1866     void vTaskDelay( const TickType_t xTicksToDelay )
1867     {
1868         BaseType_t xAlreadyYielded = pdFALSE;
1869
1870         /* A delay time of zero just forces a reschedule. */
1871         if( xTicksToDelay > ( TickType_t ) 0U )
1872         {
1873             vTaskSuspendAll();
1874             {
1875                 configASSERT( uxSchedulerSuspended == 1 );
1876                 traceTASK_DELAY();
1877
1878                 /* A task that is removed from the event list while the
1879                  * scheduler is suspended will not get placed in the ready
1880                  * list or removed from the blocked list until the scheduler
1881                  * is resumed.
1882                  *
1883                  * This task cannot be in an event list as it is the currently
1884                  * executing task. */
1885                 prvAddCurrentTaskToDelayedList( xTicksToDelay, pdFALSE );
1886             }
1887             xAlreadyYielded = xTaskResumeAll();
1888         }
1889         else
1890         {
1891             mtCOVERAGE_TEST_MARKER();
1892         }
1893
1894         /* Force a reschedule if xTaskResumeAll has not already done so, we may
1895          * have put ourselves to sleep. */
1896         if( xAlreadyYielded == pdFALSE )
1897         {
1898             vTaskYieldWithinAPI();
1899         }
1900         else
1901         {
1902             mtCOVERAGE_TEST_MARKER();
1903         }
1904     }
1905
1906 #endif /* INCLUDE_vTaskDelay */
1907 /*-----------------------------------------------------------*/
1908
1909 #if ( ( INCLUDE_eTaskGetState == 1 ) || ( configUSE_TRACE_FACILITY == 1 ) || ( INCLUDE_xTaskAbortDelay == 1 ) )
1910
1911     eTaskState eTaskGetState( TaskHandle_t xTask )
1912     {
1913         eTaskState eReturn;
1914         List_t const * pxStateList, * pxDelayedList, * pxOverflowedDelayedList;
1915         const TCB_t * const pxTCB = xTask;
1916
1917         configASSERT( pxTCB );
1918
1919         taskENTER_CRITICAL();
1920         {
1921             pxStateList = listLIST_ITEM_CONTAINER( &( pxTCB->xStateListItem ) );
1922             pxDelayedList = pxDelayedTaskList;
1923             pxOverflowedDelayedList = pxOverflowDelayedTaskList;
1924         }
1925         taskEXIT_CRITICAL();
1926
1927         if( ( pxStateList == pxDelayedList ) || ( pxStateList == pxOverflowedDelayedList ) )
1928         {
1929             /* The task being queried is referenced from one of the Blocked
1930              * lists. */
1931             eReturn = eBlocked;
1932         }
1933
1934         #if ( INCLUDE_vTaskSuspend == 1 )
1935             else if( pxStateList == &xSuspendedTaskList )
1936             {
1937                 /* The task being queried is referenced from the suspended
1938                  * list.  Is it genuinely suspended or is it blocked
1939                  * indefinitely? */
1940                 if( listLIST_ITEM_CONTAINER( &( pxTCB->xEventListItem ) ) == NULL )
1941                 {
1942                     #if ( configUSE_TASK_NOTIFICATIONS == 1 )
1943                         {
1944                             BaseType_t x;
1945
1946                             /* The task does not appear on the event list item of
1947                              * and of the RTOS objects, but could still be in the
1948                              * blocked state if it is waiting on its notification
1949                              * rather than waiting on an object.  If not, is
1950                              * suspended. */
1951                             eReturn = eSuspended;
1952
1953                             for( x = 0; x < configTASK_NOTIFICATION_ARRAY_ENTRIES; x++ )
1954                             {
1955                                 if( pxTCB->ucNotifyState[ x ] == taskWAITING_NOTIFICATION )
1956                                 {
1957                                     eReturn = eBlocked;
1958                                     break;
1959                                 }
1960                             }
1961                         }
1962                     #else  /* if ( configUSE_TASK_NOTIFICATIONS == 1 ) */
1963                         {
1964                             eReturn = eSuspended;
1965                         }
1966                     #endif /* if ( configUSE_TASK_NOTIFICATIONS == 1 ) */
1967                 }
1968                 else
1969                 {
1970                     eReturn = eBlocked;
1971                 }
1972             }
1973         #endif /* if ( INCLUDE_vTaskSuspend == 1 ) */
1974
1975         #if ( INCLUDE_vTaskDelete == 1 )
1976             else if( ( pxStateList == &xTasksWaitingTermination ) || ( pxStateList == NULL ) )
1977             {
1978                 /* The task being queried is referenced from the deleted
1979                  * tasks list, or it is not referenced from any lists at
1980                  * all. */
1981                 eReturn = eDeleted;
1982             }
1983         #endif
1984
1985         else /*lint !e525 Negative indentation is intended to make use of pre-processor clearer. */
1986         {
1987             /* If the task is not in any other state, it must be in the
1988              * Ready (including pending ready) state. */
1989             if( taskTASK_IS_RUNNING( pxTCB->xTaskRunState ) )
1990             {
1991                 /* Is it actively running on a core? */
1992                 eReturn = eRunning;
1993             }
1994             else
1995             {
1996                 eReturn = eReady;
1997             }
1998         }
1999
2000         return eReturn;
2001     } /*lint !e818 xTask cannot be a pointer to const because it is a typedef. */
2002
2003 #endif /* INCLUDE_eTaskGetState */
2004 /*-----------------------------------------------------------*/
2005
2006 #if ( INCLUDE_uxTaskPriorityGet == 1 )
2007
2008     UBaseType_t uxTaskPriorityGet( const TaskHandle_t xTask )
2009     {
2010         TCB_t const * pxTCB;
2011         UBaseType_t uxReturn;
2012
2013         taskENTER_CRITICAL();
2014         {
2015             /* If null is passed in here then it is the priority of the task
2016              * that called uxTaskPriorityGet() that is being queried. */
2017             pxTCB = prvGetTCBFromHandle( xTask );
2018             uxReturn = pxTCB->uxPriority;
2019         }
2020         taskEXIT_CRITICAL();
2021
2022         return uxReturn;
2023     }
2024
2025 #endif /* INCLUDE_uxTaskPriorityGet */
2026 /*-----------------------------------------------------------*/
2027
2028 #if ( INCLUDE_uxTaskPriorityGet == 1 )
2029
2030     UBaseType_t uxTaskPriorityGetFromISR( const TaskHandle_t xTask )
2031     {
2032         TCB_t const * pxTCB;
2033         UBaseType_t uxReturn, uxSavedInterruptState;
2034
2035         /* RTOS ports that support interrupt nesting have the concept of a
2036          * maximum  system call (or maximum API call) interrupt priority.
2037          * Interrupts that are  above the maximum system call priority are keep
2038          * permanently enabled, even when the RTOS kernel is in a critical section,
2039          * but cannot make any calls to FreeRTOS API functions.  If configASSERT()
2040          * is defined in FreeRTOSConfig.h then
2041          * portASSERT_IF_INTERRUPT_PRIORITY_INVALID() will result in an assertion
2042          * failure if a FreeRTOS API function is called from an interrupt that has
2043          * been assigned a priority above the configured maximum system call
2044          * priority.  Only FreeRTOS functions that end in FromISR can be called
2045          * from interrupts  that have been assigned a priority at or (logically)
2046          * below the maximum system call interrupt priority.  FreeRTOS maintains a
2047          * separate interrupt safe API to ensure interrupt entry is as fast and as
2048          * simple as possible.  More information (albeit Cortex-M specific) is
2049          * provided on the following link:
2050          * https://www.FreeRTOS.org/RTOS-Cortex-M3-M4.html */
2051         portASSERT_IF_INTERRUPT_PRIORITY_INVALID();
2052
2053         uxSavedInterruptState = portSET_INTERRUPT_MASK_FROM_ISR();
2054         {
2055             /* If null is passed in here then it is the priority of the calling
2056              * task that is being queried. */
2057             pxTCB = prvGetTCBFromHandle( xTask );
2058             uxReturn = pxTCB->uxPriority;
2059         }
2060         portCLEAR_INTERRUPT_MASK_FROM_ISR( uxSavedInterruptState );
2061
2062         return uxReturn;
2063     }
2064
2065 #endif /* INCLUDE_uxTaskPriorityGet */
2066 /*-----------------------------------------------------------*/
2067
2068 #if ( INCLUDE_vTaskPrioritySet == 1 )
2069
2070     void vTaskPrioritySet( TaskHandle_t xTask,
2071                            UBaseType_t uxNewPriority )
2072     {
2073         TCB_t * pxTCB;
2074         UBaseType_t uxCurrentBasePriority, uxPriorityUsedOnEntry;
2075         BaseType_t xYieldRequired = pdFALSE;
2076         BaseType_t xYieldForTask = pdFALSE;
2077         BaseType_t xCoreID;
2078
2079         configASSERT( ( uxNewPriority < configMAX_PRIORITIES ) );
2080
2081         /* Ensure the new priority is valid. */
2082         if( uxNewPriority >= ( UBaseType_t ) configMAX_PRIORITIES )
2083         {
2084             uxNewPriority = ( UBaseType_t ) configMAX_PRIORITIES - ( UBaseType_t ) 1U;
2085         }
2086         else
2087         {
2088             mtCOVERAGE_TEST_MARKER();
2089         }
2090
2091         taskENTER_CRITICAL();
2092         {
2093             /* If null is passed in here then it is the priority of the calling
2094              * task that is being changed. */
2095             pxTCB = prvGetTCBFromHandle( xTask );
2096
2097             traceTASK_PRIORITY_SET( pxTCB, uxNewPriority );
2098
2099             #if ( configUSE_MUTEXES == 1 )
2100                 {
2101                     uxCurrentBasePriority = pxTCB->uxBasePriority;
2102                 }
2103             #else
2104                 {
2105                     uxCurrentBasePriority = pxTCB->uxPriority;
2106                 }
2107             #endif
2108
2109             if( uxCurrentBasePriority != uxNewPriority )
2110             {
2111                 /* The priority change may have readied a task of higher
2112                  * priority than a running task. */
2113                 if( uxNewPriority > uxCurrentBasePriority )
2114                 {
2115                     /* The priority of a task is being raised so
2116                      * perform a yield for this task later. */
2117                     xYieldForTask = pdTRUE;
2118                 }
2119                 else if( taskTASK_IS_RUNNING( pxTCB->xTaskRunState ) )
2120                 {
2121                     /* Setting the priority of a running task down means
2122                      * there may now be another task of higher priority that
2123                      * is ready to execute. */
2124                     #if ( configUSE_TASK_PREEMPTION_DISABLE == 1 )
2125                         if( pxTCB->xPreemptionDisable == pdFALSE )
2126                     #endif
2127                     {
2128                         xCoreID = ( BaseType_t ) pxTCB->xTaskRunState;
2129                         xYieldRequired = pdTRUE;
2130                     }
2131                 }
2132                 else
2133                 {
2134                     /* Setting the priority of any other task down does not
2135                      * require a yield as the running task must be above the
2136                      * new priority of the task being modified. */
2137                 }
2138
2139                 /* Remember the ready list the task might be referenced from
2140                  * before its uxPriority member is changed so the
2141                  * taskRESET_READY_PRIORITY() macro can function correctly. */
2142                 uxPriorityUsedOnEntry = pxTCB->uxPriority;
2143
2144                 #if ( configUSE_MUTEXES == 1 )
2145                     {
2146                         /* Only change the priority being used if the task is not
2147                          * currently using an inherited priority. */
2148                         if( pxTCB->uxBasePriority == pxTCB->uxPriority )
2149                         {
2150                             pxTCB->uxPriority = uxNewPriority;
2151                         }
2152                         else
2153                         {
2154                             mtCOVERAGE_TEST_MARKER();
2155                         }
2156
2157                         /* The base priority gets set whatever. */
2158                         pxTCB->uxBasePriority = uxNewPriority;
2159                     }
2160                 #else /* if ( configUSE_MUTEXES == 1 ) */
2161                     {
2162                         pxTCB->uxPriority = uxNewPriority;
2163                     }
2164                 #endif /* if ( configUSE_MUTEXES == 1 ) */
2165
2166                 /* Only reset the event list item value if the value is not
2167                  * being used for anything else. */
2168                 if( ( listGET_LIST_ITEM_VALUE( &( pxTCB->xEventListItem ) ) & taskEVENT_LIST_ITEM_VALUE_IN_USE ) == 0UL )
2169                 {
2170                     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. */
2171                 }
2172                 else
2173                 {
2174                     mtCOVERAGE_TEST_MARKER();
2175                 }
2176
2177                 /* If the task is in the blocked or suspended list we need do
2178                  * nothing more than change its priority variable. However, if
2179                  * the task is in a ready list it needs to be removed and placed
2180                  * in the list appropriate to its new priority. */
2181                 if( listIS_CONTAINED_WITHIN( &( pxReadyTasksLists[ uxPriorityUsedOnEntry ] ), &( pxTCB->xStateListItem ) ) != pdFALSE )
2182                 {
2183                     /* The task is currently in its ready list - remove before
2184                      * adding it to its new ready list.  As we are in a critical
2185                      * section we can do this even if the scheduler is suspended. */
2186                     if( uxListRemove( &( pxTCB->xStateListItem ) ) == ( UBaseType_t ) 0 )
2187                     {
2188                         /* It is known that the task is in its ready list so
2189                          * there is no need to check again and the port level
2190                          * reset macro can be called directly. */
2191                         portRESET_READY_PRIORITY( uxPriorityUsedOnEntry, uxTopReadyPriority );
2192                     }
2193                     else
2194                     {
2195                         mtCOVERAGE_TEST_MARKER();
2196                     }
2197
2198                     prvAddTaskToReadyList( pxTCB );
2199                 }
2200                 else
2201                 {
2202                     /* It's possible that xYieldForTask was already set to pdTRUE because
2203                      * its priority is being raised. However, since it is not in a ready list
2204                      * we don't actually need to yield for it. */
2205                     xYieldForTask = pdFALSE;
2206                 }
2207
2208                 #if ( configUSE_PREEMPTION == 1 )
2209                     if( xYieldRequired != pdFALSE )
2210                     {
2211                         prvYieldCore( xCoreID );
2212                     }
2213                     else if( xYieldForTask != pdFALSE )
2214                     {
2215                         prvYieldForTask( pxTCB, pdTRUE );
2216                     }
2217                     else
2218                     {
2219                         mtCOVERAGE_TEST_MARKER();
2220                     }
2221                 #endif /* if ( configUSE_PREEMPTION == 1 ) */
2222
2223                 /* Remove compiler warning about unused variables when the port
2224                  * optimised task selection is not being used. */
2225                 ( void ) uxPriorityUsedOnEntry;
2226             }
2227         }
2228         taskEXIT_CRITICAL();
2229     }
2230
2231 #endif /* INCLUDE_vTaskPrioritySet */
2232 /*-----------------------------------------------------------*/
2233
2234 #if ( configNUM_CORES > 1 )
2235 #if ( configUSE_CORE_AFFINITY == 1 )
2236
2237     void vTaskCoreAffinitySet( const TaskHandle_t xTask,
2238                                 UBaseType_t uxCoreAffinityMask )
2239     {
2240         TCB_t * pxTCB;
2241         BaseType_t xCoreID;
2242
2243         taskENTER_CRITICAL();
2244         {
2245             pxTCB = prvGetTCBFromHandle( xTask );
2246
2247             pxTCB->uxCoreAffinityMask = uxCoreAffinityMask;
2248
2249             if( xSchedulerRunning != pdFALSE )
2250             {
2251                 if( taskTASK_IS_RUNNING( pxTCB->xTaskRunState ) )
2252                 {
2253                     xCoreID = ( BaseType_t ) pxTCB->xTaskRunState;
2254
2255                     if( ( uxCoreAffinityMask & ( 1 << xCoreID ) ) != 0 )
2256                     {
2257                         prvYieldCore( xCoreID );
2258                     }
2259                 }
2260             }
2261         }
2262         taskEXIT_CRITICAL();
2263     }
2264
2265 #endif /* configUSE_CORE_AFFINITY */
2266 #endif
2267 /*-----------------------------------------------------------*/
2268
2269 #if ( configNUM_CORES > 1 )
2270 #if ( configUSE_CORE_AFFINITY == 1 )
2271
2272     UBaseType_t vTaskCoreAffinityGet( const TaskHandle_t xTask )
2273     {
2274         TCB_t * pxTCB;
2275         UBaseType_t uxCoreAffinityMask;
2276
2277         taskENTER_CRITICAL();
2278         {
2279             pxTCB = prvGetTCBFromHandle( xTask );
2280             uxCoreAffinityMask = pxTCB->uxCoreAffinityMask;
2281         }
2282         taskEXIT_CRITICAL();
2283
2284         return uxCoreAffinityMask;
2285     }
2286
2287 #endif /* configUSE_CORE_AFFINITY */
2288 #endif
2289
2290 /*-----------------------------------------------------------*/
2291
2292 #if ( configUSE_TASK_PREEMPTION_DISABLE == 1 )
2293
2294     void vTaskPreemptionDisable( const TaskHandle_t xTask )
2295     {
2296         TCB_t * pxTCB;
2297
2298         taskENTER_CRITICAL();
2299         {
2300             pxTCB = prvGetTCBFromHandle( xTask );
2301
2302             pxTCB->xPreemptionDisable = pdTRUE;
2303         }
2304         taskEXIT_CRITICAL();
2305     }
2306
2307 #endif /* configUSE_TASK_PREEMPTION_DISABLE */
2308 /*-----------------------------------------------------------*/
2309
2310 #if ( configUSE_TASK_PREEMPTION_DISABLE == 1 )
2311
2312     void vTaskPreemptionEnable( const TaskHandle_t xTask )
2313     {
2314         TCB_t * pxTCB;
2315         BaseType_t xCoreID;
2316
2317         taskENTER_CRITICAL();
2318         {
2319             pxTCB = prvGetTCBFromHandle( xTask );
2320
2321             pxTCB->xPreemptionDisable = pdFALSE;
2322
2323             if( xSchedulerRunning != pdFALSE )
2324             {
2325                 if( taskTASK_IS_RUNNING( pxTCB->xTaskRunState ) )
2326                 {
2327                     xCoreID = ( BaseType_t ) pxTCB->xTaskRunState;
2328                     prvYieldCore( xCoreID );
2329                 }
2330             }
2331         }
2332         taskEXIT_CRITICAL();
2333     }
2334
2335 #endif /* configUSE_TASK_PREEMPTION_DISABLE */
2336 /*-----------------------------------------------------------*/
2337
2338 #if ( INCLUDE_vTaskSuspend == 1 )
2339
2340     void vTaskSuspend( TaskHandle_t xTaskToSuspend )
2341     {
2342         TCB_t * pxTCB;
2343         TaskRunning_t xTaskRunningOnCore;
2344
2345         taskENTER_CRITICAL();
2346         {
2347             /* If null is passed in here then it is the running task that is
2348              * being suspended. */
2349             pxTCB = prvGetTCBFromHandle( xTaskToSuspend );
2350
2351             traceTASK_SUSPEND( pxTCB );
2352
2353             xTaskRunningOnCore = pxTCB->xTaskRunState;
2354
2355             /* Remove task from the ready/delayed list and place in the
2356              * suspended list. */
2357             if( uxListRemove( &( pxTCB->xStateListItem ) ) == ( UBaseType_t ) 0 )
2358             {
2359                 taskRESET_READY_PRIORITY( pxTCB->uxPriority );
2360             }
2361             else
2362             {
2363                 mtCOVERAGE_TEST_MARKER();
2364             }
2365
2366             /* Is the task waiting on an event also? */
2367             if( listLIST_ITEM_CONTAINER( &( pxTCB->xEventListItem ) ) != NULL )
2368             {
2369                 ( void ) uxListRemove( &( pxTCB->xEventListItem ) );
2370             }
2371             else
2372             {
2373                 mtCOVERAGE_TEST_MARKER();
2374             }
2375
2376             vListInsertEnd( &xSuspendedTaskList, &( pxTCB->xStateListItem ) );
2377
2378             #if ( configUSE_TASK_NOTIFICATIONS == 1 )
2379                 {
2380                     BaseType_t x;
2381
2382                     for( x = 0; x < configTASK_NOTIFICATION_ARRAY_ENTRIES; x++ )
2383                     {
2384                         if( pxTCB->ucNotifyState[ x ] == taskWAITING_NOTIFICATION )
2385                         {
2386                             /* The task was blocked to wait for a notification, but is
2387                              * now suspended, so no notification was received. */
2388                             pxTCB->ucNotifyState[ x ] = taskNOT_WAITING_NOTIFICATION;
2389                         }
2390                     }
2391                 }
2392             #endif /* if ( configUSE_TASK_NOTIFICATIONS == 1 ) */
2393
2394             if( xSchedulerRunning != pdFALSE )
2395             {
2396                 /* Reset the next expected unblock time in case it referred to the
2397                  * task that is now in the Suspended state. */
2398                 prvResetNextTaskUnblockTime();
2399             }
2400             else
2401             {
2402                 mtCOVERAGE_TEST_MARKER();
2403             }
2404
2405             if( taskTASK_IS_RUNNING( xTaskRunningOnCore ) )
2406             {
2407                 if( xSchedulerRunning != pdFALSE )
2408                 {
2409                     if( xTaskRunningOnCore == portGET_CORE_ID() )
2410                     {
2411                         /* The current task has just been suspended. */
2412                         configASSERT( uxSchedulerSuspended == 0 );
2413                         vTaskYieldWithinAPI();
2414                     }
2415                     else
2416                     {
2417                         prvYieldCore( xTaskRunningOnCore );
2418                     }
2419
2420                     taskEXIT_CRITICAL();
2421                 }
2422                 else
2423                 {
2424                     taskEXIT_CRITICAL();
2425
2426                     configASSERT( pxTCB == pxCurrentTCBs[ xTaskRunningOnCore ] );
2427
2428                     /* The scheduler is not running, but the task that was pointed
2429                      * to by pxCurrentTCB has just been suspended and pxCurrentTCB
2430                      * must be adjusted to point to a different task. */
2431                     if( listCURRENT_LIST_LENGTH( &xSuspendedTaskList ) == uxCurrentNumberOfTasks ) /*lint !e931 Right has no side effect, just volatile. */
2432                     {
2433                         /* No other tasks are ready, so set the core's TCB back to
2434                          * NULL so when the next task is created the core's TCB will
2435                          * be able to be set to point to it no matter what its relative
2436                          * priority is. */
2437                         pxTCB->xTaskRunState = taskTASK_NOT_RUNNING;
2438                         pxCurrentTCBs[ xTaskRunningOnCore ] = NULL;
2439                     }
2440                     else
2441                     {
2442                         /* Attempt to switch in a new task. This could fail since the idle tasks
2443                          * haven't been created yet. If it does then set the core's TCB back to
2444                          * NULL. */
2445                         if( prvSelectHighestPriorityTask( xTaskRunningOnCore ) == pdFALSE )
2446                         {
2447                             pxTCB->xTaskRunState = taskTASK_NOT_RUNNING;
2448                             pxCurrentTCBs[ xTaskRunningOnCore ] = NULL;
2449                         }
2450                     }
2451                 }
2452             }
2453             else
2454             {
2455                 taskEXIT_CRITICAL();
2456             }
2457         } /* taskEXIT_CRITICAL() - already exited in one of three cases above */
2458     }
2459
2460 #endif /* INCLUDE_vTaskSuspend */
2461 /*-----------------------------------------------------------*/
2462
2463 #if ( INCLUDE_vTaskSuspend == 1 )
2464
2465     static BaseType_t prvTaskIsTaskSuspended( const TaskHandle_t xTask )
2466     {
2467         BaseType_t xReturn = pdFALSE;
2468         const TCB_t * const pxTCB = xTask;
2469
2470         /* Accesses xPendingReadyList so must be called from a critical section. */
2471
2472         /* It does not make sense to check if the calling task is suspended. */
2473         configASSERT( xTask );
2474
2475         /* Is the task being resumed actually in the suspended list? */
2476         if( listIS_CONTAINED_WITHIN( &xSuspendedTaskList, &( pxTCB->xStateListItem ) ) != pdFALSE )
2477         {
2478             /* Has the task already been resumed from within an ISR? */
2479             if( listIS_CONTAINED_WITHIN( &xPendingReadyList, &( pxTCB->xEventListItem ) ) == pdFALSE )
2480             {
2481                 /* Is it in the suspended list because it is in the Suspended
2482                  * state, or because is is blocked with no timeout? */
2483                 if( listIS_CONTAINED_WITHIN( NULL, &( pxTCB->xEventListItem ) ) != pdFALSE ) /*lint !e961.  The cast is only redundant when NULL is used. */
2484                 {
2485                     xReturn = pdTRUE;
2486                 }
2487                 else
2488                 {
2489                     mtCOVERAGE_TEST_MARKER();
2490                 }
2491             }
2492             else
2493             {
2494                 mtCOVERAGE_TEST_MARKER();
2495             }
2496         }
2497         else
2498         {
2499             mtCOVERAGE_TEST_MARKER();
2500         }
2501
2502         return xReturn;
2503     } /*lint !e818 xTask cannot be a pointer to const because it is a typedef. */
2504
2505 #endif /* INCLUDE_vTaskSuspend */
2506 /*-----------------------------------------------------------*/
2507
2508 #if ( INCLUDE_vTaskSuspend == 1 )
2509
2510     void vTaskResume( TaskHandle_t xTaskToResume )
2511     {
2512         TCB_t * const pxTCB = xTaskToResume;
2513
2514         /* It does not make sense to resume the calling task. */
2515         configASSERT( xTaskToResume );
2516
2517         /* The parameter cannot be NULL as it is impossible to resume the
2518          * currently executing task. It is also impossible to resume a task
2519          * that is actively running on another core but it is too dangerous
2520          * to check their run state here. Safer to get into a critical section
2521          * and check if it is actually suspended or not below. */
2522         if( pxTCB != NULL )
2523         {
2524             taskENTER_CRITICAL();
2525             {
2526                 if( prvTaskIsTaskSuspended( pxTCB ) != pdFALSE )
2527                 {
2528                     traceTASK_RESUME( pxTCB );
2529
2530                     /* The ready list can be accessed even if the scheduler is
2531                      * suspended because this is inside a critical section. */
2532                     ( void ) uxListRemove( &( pxTCB->xStateListItem ) );
2533                     prvAddTaskToReadyList( pxTCB );
2534
2535                     /* A higher priority task may have just been resumed. */
2536                     #if ( configUSE_PREEMPTION == 1 )
2537                         {
2538                             prvYieldForTask( pxTCB, pdTRUE );
2539                         }
2540                     #endif
2541                 }
2542                 else
2543                 {
2544                     mtCOVERAGE_TEST_MARKER();
2545                 }
2546             }
2547             taskEXIT_CRITICAL();
2548         }
2549         else
2550         {
2551             mtCOVERAGE_TEST_MARKER();
2552         }
2553     }
2554
2555 #endif /* INCLUDE_vTaskSuspend */
2556
2557 /*-----------------------------------------------------------*/
2558
2559 #if ( ( INCLUDE_xTaskResumeFromISR == 1 ) && ( INCLUDE_vTaskSuspend == 1 ) )
2560
2561     BaseType_t xTaskResumeFromISR( TaskHandle_t xTaskToResume )
2562     {
2563         BaseType_t xYieldRequired = pdFALSE;
2564         TCB_t * const pxTCB = xTaskToResume;
2565         UBaseType_t uxSavedInterruptStatus;
2566
2567         configASSERT( xTaskToResume );
2568
2569         /* RTOS ports that support interrupt nesting have the concept of a
2570          * maximum  system call (or maximum API call) interrupt priority.
2571          * Interrupts that are  above the maximum system call priority are keep
2572          * permanently enabled, even when the RTOS kernel is in a critical section,
2573          * but cannot make any calls to FreeRTOS API functions.  If configASSERT()
2574          * is defined in FreeRTOSConfig.h then
2575          * portASSERT_IF_INTERRUPT_PRIORITY_INVALID() will result in an assertion
2576          * failure if a FreeRTOS API function is called from an interrupt that has
2577          * been assigned a priority above the configured maximum system call
2578          * priority.  Only FreeRTOS functions that end in FromISR can be called
2579          * from interrupts  that have been assigned a priority at or (logically)
2580          * below the maximum system call interrupt priority.  FreeRTOS maintains a
2581          * separate interrupt safe API to ensure interrupt entry is as fast and as
2582          * simple as possible.  More information (albeit Cortex-M specific) is
2583          * provided on the following link:
2584          * https://www.FreeRTOS.org/RTOS-Cortex-M3-M4.html */
2585         portASSERT_IF_INTERRUPT_PRIORITY_INVALID();
2586
2587         uxSavedInterruptStatus = portSET_INTERRUPT_MASK_FROM_ISR();
2588         {
2589             if( prvTaskIsTaskSuspended( pxTCB ) != pdFALSE )
2590             {
2591                 traceTASK_RESUME_FROM_ISR( pxTCB );
2592
2593                 /* Check the ready lists can be accessed. */
2594                 if( uxSchedulerSuspended == ( UBaseType_t ) pdFALSE )
2595                 {
2596                     /* Ready lists can be accessed so move the task from the
2597                      * suspended list to the ready list directly. */
2598
2599                     ( void ) uxListRemove( &( pxTCB->xStateListItem ) );
2600                     prvAddTaskToReadyList( pxTCB );
2601                 }
2602                 else
2603                 {
2604                     /* The delayed or ready lists cannot be accessed so the task
2605                      * is held in the pending ready list until the scheduler is
2606                      * unsuspended. */
2607                     vListInsertEnd( &( xPendingReadyList ), &( pxTCB->xEventListItem ) );
2608                 }
2609
2610                 #if ( configUSE_PREEMPTION == 1 )
2611                     prvYieldForTask( pxTCB, pdTRUE );
2612
2613                     if( xYieldPendings[ portGET_CORE_ID() ] != pdFALSE )
2614                     {
2615                         xYieldRequired = pdTRUE;
2616                     }
2617                 #endif
2618             }
2619             else
2620             {
2621                 mtCOVERAGE_TEST_MARKER();
2622             }
2623         }
2624         portCLEAR_INTERRUPT_MASK_FROM_ISR( uxSavedInterruptStatus );
2625
2626         return xYieldRequired;
2627     }
2628
2629 #endif /* ( ( INCLUDE_xTaskResumeFromISR == 1 ) && ( INCLUDE_vTaskSuspend == 1 ) ) */
2630 /*-----------------------------------------------------------*/
2631
2632 static BaseType_t prvCreateIdleTasks( void )
2633 {
2634     BaseType_t xReturn = pdPASS;
2635     BaseType_t xCoreID;
2636     char cIdleName[ configMAX_TASK_NAME_LEN ];
2637
2638         /* Add each idle task at the lowest priority. */
2639     for( xCoreID = ( BaseType_t ) 0; xCoreID < ( BaseType_t ) configNUM_CORES; xCoreID++ )
2640     {
2641         BaseType_t x;
2642
2643         if( xReturn == pdFAIL )
2644         {
2645             break;
2646         }
2647         else
2648         {
2649             mtCOVERAGE_TEST_MARKER();
2650         }
2651
2652         for( x = ( BaseType_t ) 0; x < ( BaseType_t ) configMAX_TASK_NAME_LEN; x++ )
2653         {
2654             cIdleName[ x ] = configIDLE_TASK_NAME[ x ];
2655
2656             /* Don't copy all configMAX_TASK_NAME_LEN if the string is shorter than
2657              * configMAX_TASK_NAME_LEN characters just in case the memory after the
2658              * string is not accessible (extremely unlikely). */
2659             if( cIdleName[ x ] == ( char ) 0x00 )
2660             {
2661                 break;
2662             }
2663             else
2664             {
2665                 mtCOVERAGE_TEST_MARKER();
2666             }
2667         }
2668
2669         /* Append the idle task number to the end of the name if there is space */
2670         if( x < configMAX_TASK_NAME_LEN )
2671         {
2672             cIdleName[ x++ ] = xCoreID + '0';
2673
2674             /* And append a null character if there is space */
2675             if( x < configMAX_TASK_NAME_LEN )
2676             {
2677                 cIdleName[ x ] = '\0';
2678             }
2679             else
2680             {
2681                 mtCOVERAGE_TEST_MARKER();
2682             }
2683         }
2684         else
2685         {
2686             mtCOVERAGE_TEST_MARKER();
2687         }
2688
2689         #if ( configSUPPORT_STATIC_ALLOCATION == 1 )
2690             {
2691                 if(xCoreID == 0)
2692                 {
2693                     StaticTask_t * pxIdleTaskTCBBuffer = NULL;
2694                     StackType_t * pxIdleTaskStackBuffer = NULL;
2695                     uint32_t ulIdleTaskStackSize;
2696
2697                     /* The Idle task is created using user provided RAM - obtain the
2698                     * address of the RAM then create the idle task. */
2699                     vApplicationGetIdleTaskMemory( &pxIdleTaskTCBBuffer, &pxIdleTaskStackBuffer, &ulIdleTaskStackSize );
2700                     xIdleTaskHandle[ xCoreID ] = xTaskCreateStatic( prvIdleTask,
2701                                                                     cIdleName,
2702                                                                     ulIdleTaskStackSize,
2703                                                                     ( void * ) NULL,       /*lint !e961.  The cast is not redundant for all compilers. */
2704                                                                     portPRIVILEGE_BIT,     /* In effect ( tskIDLE_PRIORITY | portPRIVILEGE_BIT ), but tskIDLE_PRIORITY is zero. */
2705                                                                     pxIdleTaskStackBuffer,
2706                                                                     pxIdleTaskTCBBuffer ); /*lint !e961 MISRA exception, justified as it is not a redundant explicit cast to all supported compilers. */
2707                 }
2708                 #if( configNUM_CORES > 1)
2709                 else
2710                 {
2711                     static StaticTask_t xIdleTCBBuffers[configNUM_CORES-1];
2712                     static StackType_t xIdleTaskStackBuffers[configNUM_CORES-1][configMINIMAL_STACK_SIZE];
2713
2714                     xIdleTaskHandle[ xCoreID ] = xTaskCreateStatic( prvMinimalIdleTask,
2715                                                                     cIdleName,
2716                                                                     configMINIMAL_STACK_SIZE,
2717                                                                     ( void * ) NULL,       /*lint !e961.  The cast is not redundant for all compilers. */
2718                                                                     portPRIVILEGE_BIT,     /* In effect ( tskIDLE_PRIORITY | portPRIVILEGE_BIT ), but tskIDLE_PRIORITY is zero. */
2719                                                                     xIdleTaskStackBuffers[xCoreID-1],
2720                                                                     &xIdleTCBBuffers[xCoreID-1] ); /*lint !e961 MISRA exception, justified as it is not a redundant explicit cast to all supported compilers. */
2721                 }
2722                 #endif
2723                 if( xIdleTaskHandle[ xCoreID ] != NULL )
2724                 {
2725                     xReturn = pdPASS;
2726                 }
2727                 else
2728                 {
2729                     xReturn = pdFAIL;
2730                 }
2731             }
2732         #else  /* if ( configSUPPORT_STATIC_ALLOCATION == 1 ) */
2733             {
2734                 if(xCoreID == 0)
2735                 {
2736                 /* The Idle task is being created using dynamically allocated RAM. */
2737                 xReturn = xTaskCreate( prvIdleTask,
2738                                        cIdleName,
2739                                        configMINIMAL_STACK_SIZE,
2740                                        ( void * ) NULL,
2741                                        portPRIVILEGE_BIT,             /* In effect ( tskIDLE_PRIORITY | portPRIVILEGE_BIT ), but tskIDLE_PRIORITY is zero. */
2742                                        &xIdleTaskHandle[ xCoreID ] ); /*lint !e961 MISRA exception, justified as it is not a redundant explicit cast to all supported compilers. */
2743                 }
2744                 #if( configNUM_CORES > 1 )
2745                 else
2746                 {
2747                 xReturn = xTaskCreate( prvMinimalIdleTask,
2748                                        cIdleName,
2749                                        configMINIMAL_STACK_SIZE,
2750                                        ( void * ) NULL,
2751                                        portPRIVILEGE_BIT,             /* In effect ( tskIDLE_PRIORITY | portPRIVILEGE_BIT ), but tskIDLE_PRIORITY is zero. */
2752                                        &xIdleTaskHandle[ xCoreID ] ); /*lint !e961 MISRA exception, justified as it is not a redundant explicit cast to all supported compilers. */
2753                 }
2754                 #endif
2755             }
2756         #endif /* configSUPPORT_STATIC_ALLOCATION */
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 #if (configNUM_CORES > 1)
4229 static portTASK_FUNCTION( prvMinimalIdleTask, pvParameters )
4230 {
4231     for(;;)
4232     {
4233         #if ( configUSE_PREEMPTION == 0 )
4234             {
4235                 /* If we are not using preemption we keep forcing a task switch to
4236                  * see if any other task has become available.  If we are using
4237                  * preemption we don't need to do this as any task becoming available
4238                  * will automatically get the processor anyway. */
4239                 taskYIELD();
4240             }
4241         #endif /* configUSE_PREEMPTION */
4242
4243         #if ( ( configUSE_PREEMPTION == 1 ) && ( configIDLE_SHOULD_YIELD == 1 ) )
4244             {
4245                 /* When using preemption tasks of equal priority will be
4246                  * timesliced.  If a task that is sharing the idle priority is ready
4247                  * to run then the idle task should yield before the end of the
4248                  * timeslice.
4249                  *
4250                  * A critical region is not required here as we are just reading from
4251                  * the list, and an occasional incorrect value will not matter.  If
4252                  * the ready list at the idle priority contains one more task than the
4253                  * number of idle tasks, which is equal to the configured numbers of cores
4254                  * then a task other than the idle task is ready to execute. */
4255                 if( listCURRENT_LIST_LENGTH( &( pxReadyTasksLists[ tskIDLE_PRIORITY ] ) ) > ( UBaseType_t ) configNUM_CORES )
4256                 {
4257                     taskYIELD();
4258                 }
4259                 else
4260                 {
4261                     mtCOVERAGE_TEST_MARKER();
4262                 }
4263             }
4264         #endif /* ( ( configUSE_PREEMPTION == 1 ) && ( configIDLE_SHOULD_YIELD == 1 ) ) */
4265     }
4266 }
4267 #endif
4268 /*
4269  * -----------------------------------------------------------
4270  * The Idle task.
4271  * ----------------------------------------------------------
4272  *
4273  *
4274  */
4275 static portTASK_FUNCTION( prvIdleTask, pvParameters )
4276 {
4277     /* Stop warnings. */
4278     ( void ) pvParameters;
4279
4280     /** THIS IS THE RTOS IDLE TASK - WHICH IS CREATED AUTOMATICALLY WHEN THE
4281      * SCHEDULER IS STARTED. **/
4282
4283     /* In case a task that has a secure context deletes itself, in which case
4284      * the idle task is responsible for deleting the task's secure context, if
4285      * any. */
4286     portALLOCATE_SECURE_CONTEXT( configMINIMAL_SECURE_STACK_SIZE );
4287
4288     /* All cores start up in the idle task. This initial yield gets the application
4289      * tasks started. */
4290     taskYIELD();
4291
4292     for( ; ; )
4293     {
4294         /* See if any tasks have deleted themselves - if so then the idle task
4295          * is responsible for freeing the deleted task's TCB and stack. */
4296         prvCheckTasksWaitingTermination();
4297
4298         #if ( configUSE_PREEMPTION == 0 )
4299             {
4300                 /* If we are not using preemption we keep forcing a task switch to
4301                  * see if any other task has become available.  If we are using
4302                  * preemption we don't need to do this as any task becoming available
4303                  * will automatically get the processor anyway. */
4304                 taskYIELD();
4305             }
4306         #endif /* configUSE_PREEMPTION */
4307
4308         #if ( ( configUSE_PREEMPTION == 1 ) && ( configIDLE_SHOULD_YIELD == 1 ) )
4309             {
4310                 /* When using preemption tasks of equal priority will be
4311                  * timesliced.  If a task that is sharing the idle priority is ready
4312                  * to run then the idle task should yield before the end of the
4313                  * timeslice.
4314                  *
4315                  * A critical region is not required here as we are just reading from
4316                  * the list, and an occasional incorrect value will not matter.  If
4317                  * the ready list at the idle priority contains one more task than the
4318                  * number of idle tasks, which is equal to the configured numbers of cores
4319                  * then a task other than the idle task is ready to execute. */
4320                 if( listCURRENT_LIST_LENGTH( &( pxReadyTasksLists[ tskIDLE_PRIORITY ] ) ) > ( UBaseType_t ) configNUM_CORES )
4321                 {
4322                     taskYIELD();
4323                 }
4324                 else
4325                 {
4326                     mtCOVERAGE_TEST_MARKER();
4327                 }
4328             }
4329         #endif /* ( ( configUSE_PREEMPTION == 1 ) && ( configIDLE_SHOULD_YIELD == 1 ) ) */
4330
4331         #if ( configUSE_IDLE_HOOK == 1 )
4332             {
4333                 extern void vApplicationIdleHook( void );
4334
4335                 /* Call the user defined function from within the idle task.  This
4336                  * allows the application designer to add background functionality
4337                  * without the overhead of a separate task.
4338                  * NOTE: vApplicationIdleHook() MUST NOT, UNDER ANY CIRCUMSTANCES,
4339                  * CALL A FUNCTION THAT MIGHT BLOCK. */
4340                 vApplicationIdleHook();
4341             }
4342         #endif /* configUSE_IDLE_HOOK */
4343
4344         /* This conditional compilation should use inequality to 0, not equality
4345          * to 1.  This is to ensure portSUPPRESS_TICKS_AND_SLEEP() is called when
4346          * user defined low power mode  implementations require
4347          * configUSE_TICKLESS_IDLE to be set to a value other than 1. */
4348         #if ( configUSE_TICKLESS_IDLE != 0 )
4349             {
4350                 TickType_t xExpectedIdleTime;
4351
4352                 /* It is not desirable to suspend then resume the scheduler on
4353                  * each iteration of the idle task.  Therefore, a preliminary
4354                  * test of the expected idle time is performed without the
4355                  * scheduler suspended.  The result here is not necessarily
4356                  * valid. */
4357                 xExpectedIdleTime = prvGetExpectedIdleTime();
4358
4359                 if( xExpectedIdleTime >= configEXPECTED_IDLE_TIME_BEFORE_SLEEP )
4360                 {
4361                     vTaskSuspendAll();
4362                     {
4363                         /* Now the scheduler is suspended, the expected idle
4364                          * time can be sampled again, and this time its value can
4365                          * be used. */
4366                         configASSERT( xNextTaskUnblockTime >= xTickCount );
4367                         xExpectedIdleTime = prvGetExpectedIdleTime();
4368
4369                         /* Define the following macro to set xExpectedIdleTime to 0
4370                          * if the application does not want
4371                          * portSUPPRESS_TICKS_AND_SLEEP() to be called. */
4372                         configPRE_SUPPRESS_TICKS_AND_SLEEP_PROCESSING( xExpectedIdleTime );
4373
4374                         if( xExpectedIdleTime >= configEXPECTED_IDLE_TIME_BEFORE_SLEEP )
4375                         {
4376                             traceLOW_POWER_IDLE_BEGIN();
4377                             portSUPPRESS_TICKS_AND_SLEEP( xExpectedIdleTime );
4378                             traceLOW_POWER_IDLE_END();
4379                         }
4380                         else
4381                         {
4382                             mtCOVERAGE_TEST_MARKER();
4383                         }
4384                     }
4385                     ( void ) xTaskResumeAll();
4386                 }
4387                 else
4388                 {
4389                     mtCOVERAGE_TEST_MARKER();
4390                 }
4391             }
4392         #endif /* configUSE_TICKLESS_IDLE */
4393     }
4394 }
4395 /*-----------------------------------------------------------*/
4396
4397 #if ( configUSE_TICKLESS_IDLE != 0 )
4398
4399     eSleepModeStatus eTaskConfirmSleepModeStatus( void )
4400     {
4401         /* The idle task exists in addition to the application tasks. */
4402         const UBaseType_t uxNonApplicationTasks = 1;
4403         eSleepModeStatus eReturn = eStandardSleep;
4404
4405         /* This function must be called from a critical section. */
4406
4407         if( listCURRENT_LIST_LENGTH( &xPendingReadyList ) != 0 )
4408         {
4409             /* A task was made ready while the scheduler was suspended. */
4410             eReturn = eAbortSleep;
4411         }
4412         else if( xYieldPending != pdFALSE )
4413         {
4414             /* A yield was pended while the scheduler was suspended. */
4415             eReturn = eAbortSleep;
4416         }
4417         else if( xPendedTicks != 0 )
4418         {
4419             /* A tick interrupt has already occurred but was held pending
4420              * because the scheduler is suspended. */
4421             eReturn = eAbortSleep;
4422         }
4423         else
4424         {
4425             /* If all the tasks are in the suspended list (which might mean they
4426              * have an infinite block time rather than actually being suspended)
4427              * then it is safe to turn all clocks off and just wait for external
4428              * interrupts. */
4429             if( listCURRENT_LIST_LENGTH( &xSuspendedTaskList ) == ( uxCurrentNumberOfTasks - uxNonApplicationTasks ) )
4430             {
4431                 eReturn = eNoTasksWaitingTimeout;
4432             }
4433             else
4434             {
4435                 mtCOVERAGE_TEST_MARKER();
4436             }
4437         }
4438
4439         return eReturn;
4440     }
4441
4442 #endif /* configUSE_TICKLESS_IDLE */
4443 /*-----------------------------------------------------------*/
4444
4445 #if ( configNUM_THREAD_LOCAL_STORAGE_POINTERS != 0 )
4446
4447     void vTaskSetThreadLocalStoragePointer( TaskHandle_t xTaskToSet,
4448                                             BaseType_t xIndex,
4449                                             void * pvValue )
4450     {
4451         TCB_t * pxTCB;
4452
4453         if( xIndex < configNUM_THREAD_LOCAL_STORAGE_POINTERS )
4454         {
4455             pxTCB = prvGetTCBFromHandle( xTaskToSet );
4456             configASSERT( pxTCB != NULL );
4457             pxTCB->pvThreadLocalStoragePointers[ xIndex ] = pvValue;
4458         }
4459     }
4460
4461 #endif /* configNUM_THREAD_LOCAL_STORAGE_POINTERS */
4462 /*-----------------------------------------------------------*/
4463
4464 #if ( configNUM_THREAD_LOCAL_STORAGE_POINTERS != 0 )
4465
4466     void * pvTaskGetThreadLocalStoragePointer( TaskHandle_t xTaskToQuery,
4467                                                BaseType_t xIndex )
4468     {
4469         void * pvReturn = NULL;
4470         TCB_t * pxTCB;
4471
4472         if( xIndex < configNUM_THREAD_LOCAL_STORAGE_POINTERS )
4473         {
4474             pxTCB = prvGetTCBFromHandle( xTaskToQuery );
4475             pvReturn = pxTCB->pvThreadLocalStoragePointers[ xIndex ];
4476         }
4477         else
4478         {
4479             pvReturn = NULL;
4480         }
4481
4482         return pvReturn;
4483     }
4484
4485 #endif /* configNUM_THREAD_LOCAL_STORAGE_POINTERS */
4486 /*-----------------------------------------------------------*/
4487
4488 #if ( portUSING_MPU_WRAPPERS == 1 )
4489
4490     void vTaskAllocateMPURegions( TaskHandle_t xTaskToModify,
4491                                   const MemoryRegion_t * const xRegions )
4492     {
4493         TCB_t * pxTCB;
4494
4495         /* If null is passed in here then we are modifying the MPU settings of
4496          * the calling task. */
4497         pxTCB = prvGetTCBFromHandle( xTaskToModify );
4498
4499         vPortStoreTaskMPUSettings( &( pxTCB->xMPUSettings ), xRegions, NULL, 0 );
4500     }
4501
4502 #endif /* portUSING_MPU_WRAPPERS */
4503 /*-----------------------------------------------------------*/
4504
4505 static void prvInitialiseTaskLists( void )
4506 {
4507     UBaseType_t uxPriority;
4508
4509     for( uxPriority = ( UBaseType_t ) 0U; uxPriority < ( UBaseType_t ) configMAX_PRIORITIES; uxPriority++ )
4510     {
4511         vListInitialise( &( pxReadyTasksLists[ uxPriority ] ) );
4512     }
4513
4514     vListInitialise( &xDelayedTaskList1 );
4515     vListInitialise( &xDelayedTaskList2 );
4516     vListInitialise( &xPendingReadyList );
4517
4518     #if ( INCLUDE_vTaskDelete == 1 )
4519         {
4520             vListInitialise( &xTasksWaitingTermination );
4521         }
4522     #endif /* INCLUDE_vTaskDelete */
4523
4524     #if ( INCLUDE_vTaskSuspend == 1 )
4525         {
4526             vListInitialise( &xSuspendedTaskList );
4527         }
4528     #endif /* INCLUDE_vTaskSuspend */
4529
4530     /* Start with pxDelayedTaskList using list1 and the pxOverflowDelayedTaskList
4531      * using list2. */
4532     pxDelayedTaskList = &xDelayedTaskList1;
4533     pxOverflowDelayedTaskList = &xDelayedTaskList2;
4534 }
4535 /*-----------------------------------------------------------*/
4536
4537 static void prvCheckTasksWaitingTermination( void )
4538 {
4539     /** THIS FUNCTION IS CALLED FROM THE RTOS IDLE TASK **/
4540
4541     #if ( INCLUDE_vTaskDelete == 1 )
4542         {
4543             TCB_t * pxTCB;
4544
4545             /* uxDeletedTasksWaitingCleanUp is used to prevent taskENTER_CRITICAL()
4546              * being called too often in the idle task. */
4547             while( uxDeletedTasksWaitingCleanUp > ( UBaseType_t ) 0U )
4548             {
4549                 taskENTER_CRITICAL();
4550                 {
4551                     /* Since we are SMP, multiple idles can be running simultaneously
4552                      * and we need to check that other idles did not cleanup while we were
4553                      * waiting to enter the critical section */
4554                     if( uxDeletedTasksWaitingCleanUp > ( UBaseType_t ) 0U )
4555                     {
4556                         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. */
4557
4558                         if( pxTCB->xTaskRunState == taskTASK_NOT_RUNNING )
4559                         {
4560                             ( void ) uxListRemove( &( pxTCB->xStateListItem ) );
4561                             --uxCurrentNumberOfTasks;
4562                             --uxDeletedTasksWaitingCleanUp;
4563                             prvDeleteTCB( pxTCB );
4564                         }
4565                         else
4566                         {
4567                             /* The TCB to be deleted still has not yet been switched out
4568                              * by the scheduler, so we will just exit this loop early and
4569                              * try again next time. */
4570                             taskEXIT_CRITICAL();
4571                             break;
4572                         }
4573                     }
4574                 }
4575                 taskEXIT_CRITICAL();
4576             }
4577         }
4578     #endif /* INCLUDE_vTaskDelete */
4579 }
4580 /*-----------------------------------------------------------*/
4581
4582 #if ( configUSE_TRACE_FACILITY == 1 )
4583
4584     void vTaskGetInfo( TaskHandle_t xTask,
4585                        TaskStatus_t * pxTaskStatus,
4586                        BaseType_t xGetFreeStackSpace,
4587                        eTaskState eState )
4588     {
4589         TCB_t * pxTCB;
4590
4591         /* xTask is NULL then get the state of the calling task. */
4592         pxTCB = prvGetTCBFromHandle( xTask );
4593
4594         pxTaskStatus->xHandle = ( TaskHandle_t ) pxTCB;
4595         pxTaskStatus->pcTaskName = ( const char * ) &( pxTCB->pcTaskName[ 0 ] );
4596         pxTaskStatus->uxCurrentPriority = pxTCB->uxPriority;
4597         pxTaskStatus->pxStackBase = pxTCB->pxStack;
4598         pxTaskStatus->xTaskNumber = pxTCB->uxTCBNumber;
4599
4600         #if ( configUSE_MUTEXES == 1 )
4601             {
4602                 pxTaskStatus->uxBasePriority = pxTCB->uxBasePriority;
4603             }
4604         #else
4605             {
4606                 pxTaskStatus->uxBasePriority = 0;
4607             }
4608         #endif
4609
4610         #if ( configGENERATE_RUN_TIME_STATS == 1 )
4611             {
4612                 pxTaskStatus->ulRunTimeCounter = pxTCB->ulRunTimeCounter;
4613             }
4614         #else
4615             {
4616                 pxTaskStatus->ulRunTimeCounter = 0;
4617             }
4618         #endif
4619
4620         /* Obtaining the task state is a little fiddly, so is only done if the
4621          * value of eState passed into this function is eInvalid - otherwise the
4622          * state is just set to whatever is passed in. */
4623         if( eState != eInvalid )
4624         {
4625             if( taskTASK_IS_RUNNING( pxTCB->xTaskRunState ) )
4626             {
4627                 pxTaskStatus->eCurrentState = eRunning;
4628             }
4629             else
4630             {
4631                 pxTaskStatus->eCurrentState = eState;
4632
4633                 #if ( INCLUDE_vTaskSuspend == 1 )
4634                     {
4635                         /* If the task is in the suspended list then there is a
4636                          *  chance it is actually just blocked indefinitely - so really
4637                          *  it should be reported as being in the Blocked state. */
4638                         if( eState == eSuspended )
4639                         {
4640                             vTaskSuspendAll();
4641                             {
4642                                 if( listLIST_ITEM_CONTAINER( &( pxTCB->xEventListItem ) ) != NULL )
4643                                 {
4644                                     pxTaskStatus->eCurrentState = eBlocked;
4645                                 }
4646                             }
4647                             ( void ) xTaskResumeAll();
4648                         }
4649                     }
4650                 #endif /* INCLUDE_vTaskSuspend */
4651             }
4652         }
4653         else
4654         {
4655             pxTaskStatus->eCurrentState = eTaskGetState( pxTCB );
4656         }
4657
4658         /* Obtaining the stack space takes some time, so the xGetFreeStackSpace
4659          * parameter is provided to allow it to be skipped. */
4660         if( xGetFreeStackSpace != pdFALSE )
4661         {
4662             #if ( portSTACK_GROWTH > 0 )
4663                 {
4664                     pxTaskStatus->usStackHighWaterMark = prvTaskCheckFreeStackSpace( ( uint8_t * ) pxTCB->pxEndOfStack );
4665                 }
4666             #else
4667                 {
4668                     pxTaskStatus->usStackHighWaterMark = prvTaskCheckFreeStackSpace( ( uint8_t * ) pxTCB->pxStack );
4669                 }
4670             #endif
4671         }
4672         else
4673         {
4674             pxTaskStatus->usStackHighWaterMark = 0;
4675         }
4676     }
4677
4678 #endif /* configUSE_TRACE_FACILITY */
4679 /*-----------------------------------------------------------*/
4680
4681 #if ( configUSE_TRACE_FACILITY == 1 )
4682
4683     static UBaseType_t prvListTasksWithinSingleList( TaskStatus_t * pxTaskStatusArray,
4684                                                      List_t * pxList,
4685                                                      eTaskState eState )
4686     {
4687         configLIST_VOLATILE TCB_t * pxNextTCB, * pxFirstTCB;
4688         UBaseType_t uxTask = 0;
4689
4690         if( listCURRENT_LIST_LENGTH( pxList ) > ( UBaseType_t ) 0 )
4691         {
4692             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. */
4693
4694             /* Populate an TaskStatus_t structure within the
4695              * pxTaskStatusArray array for each task that is referenced from
4696              * pxList.  See the definition of TaskStatus_t in task.h for the
4697              * meaning of each TaskStatus_t structure member. */
4698             do
4699             {
4700                 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. */
4701                 vTaskGetInfo( ( TaskHandle_t ) pxNextTCB, &( pxTaskStatusArray[ uxTask ] ), pdTRUE, eState );
4702                 uxTask++;
4703             } while( pxNextTCB != pxFirstTCB );
4704         }
4705         else
4706         {
4707             mtCOVERAGE_TEST_MARKER();
4708         }
4709
4710         return uxTask;
4711     }
4712
4713 #endif /* configUSE_TRACE_FACILITY */
4714 /*-----------------------------------------------------------*/
4715
4716 #if ( ( configUSE_TRACE_FACILITY == 1 ) || ( INCLUDE_uxTaskGetStackHighWaterMark == 1 ) || ( INCLUDE_uxTaskGetStackHighWaterMark2 == 1 ) )
4717
4718     static configSTACK_DEPTH_TYPE prvTaskCheckFreeStackSpace( const uint8_t * pucStackByte )
4719     {
4720         uint32_t ulCount = 0U;
4721
4722         while( *pucStackByte == ( uint8_t ) tskSTACK_FILL_BYTE )
4723         {
4724             pucStackByte -= portSTACK_GROWTH;
4725             ulCount++;
4726         }
4727
4728         ulCount /= ( uint32_t ) sizeof( StackType_t ); /*lint !e961 Casting is not redundant on smaller architectures. */
4729
4730         return ( configSTACK_DEPTH_TYPE ) ulCount;
4731     }
4732
4733 #endif /* ( ( configUSE_TRACE_FACILITY == 1 ) || ( INCLUDE_uxTaskGetStackHighWaterMark == 1 ) || ( INCLUDE_uxTaskGetStackHighWaterMark2 == 1 ) ) */
4734 /*-----------------------------------------------------------*/
4735
4736 #if ( INCLUDE_uxTaskGetStackHighWaterMark2 == 1 )
4737
4738 /* uxTaskGetStackHighWaterMark() and uxTaskGetStackHighWaterMark2() are the
4739  * same except for their return type.  Using configSTACK_DEPTH_TYPE allows the
4740  * user to determine the return type.  It gets around the problem of the value
4741  * overflowing on 8-bit types without breaking backward compatibility for
4742  * applications that expect an 8-bit return type. */
4743     configSTACK_DEPTH_TYPE uxTaskGetStackHighWaterMark2( TaskHandle_t xTask )
4744     {
4745         TCB_t * pxTCB;
4746         uint8_t * pucEndOfStack;
4747         configSTACK_DEPTH_TYPE uxReturn;
4748
4749         /* uxTaskGetStackHighWaterMark() and uxTaskGetStackHighWaterMark2() are
4750          * the same except for their return type.  Using configSTACK_DEPTH_TYPE
4751          * allows the user to determine the return type.  It gets around the
4752          * problem of the value overflowing on 8-bit types without breaking
4753          * backward compatibility for applications that expect an 8-bit return
4754          * type. */
4755
4756         pxTCB = prvGetTCBFromHandle( xTask );
4757
4758         #if portSTACK_GROWTH < 0
4759             {
4760                 pucEndOfStack = ( uint8_t * ) pxTCB->pxStack;
4761             }
4762         #else
4763             {
4764                 pucEndOfStack = ( uint8_t * ) pxTCB->pxEndOfStack;
4765             }
4766         #endif
4767
4768         uxReturn = prvTaskCheckFreeStackSpace( pucEndOfStack );
4769
4770         return uxReturn;
4771     }
4772
4773 #endif /* INCLUDE_uxTaskGetStackHighWaterMark2 */
4774 /*-----------------------------------------------------------*/
4775
4776 #if ( INCLUDE_uxTaskGetStackHighWaterMark == 1 )
4777
4778     UBaseType_t uxTaskGetStackHighWaterMark( TaskHandle_t xTask )
4779     {
4780         TCB_t * pxTCB;
4781         uint8_t * pucEndOfStack;
4782         UBaseType_t uxReturn;
4783
4784         pxTCB = prvGetTCBFromHandle( xTask );
4785
4786         #if portSTACK_GROWTH < 0
4787             {
4788                 pucEndOfStack = ( uint8_t * ) pxTCB->pxStack;
4789             }
4790         #else
4791             {
4792                 pucEndOfStack = ( uint8_t * ) pxTCB->pxEndOfStack;
4793             }
4794         #endif
4795
4796         uxReturn = ( UBaseType_t ) prvTaskCheckFreeStackSpace( pucEndOfStack );
4797
4798         return uxReturn;
4799     }
4800
4801 #endif /* INCLUDE_uxTaskGetStackHighWaterMark */
4802 /*-----------------------------------------------------------*/
4803
4804 #if ( INCLUDE_vTaskDelete == 1 )
4805
4806     static void prvDeleteTCB( TCB_t * pxTCB )
4807     {
4808         /* This call is required specifically for the TriCore port.  It must be
4809          * above the vPortFree() calls.  The call is also used by ports/demos that
4810          * want to allocate and clean RAM statically. */
4811         portCLEAN_UP_TCB( pxTCB );
4812
4813         /* Free up the memory allocated by the scheduler for the task.  It is up
4814          * to the task to free any memory allocated at the application level.
4815          * See the third party link http://www.nadler.com/embedded/newlibAndFreeRTOS.html
4816          * for additional information. */
4817         #if ( configUSE_NEWLIB_REENTRANT == 1 )
4818             {
4819                 _reclaim_reent( &( pxTCB->xNewLib_reent ) );
4820             }
4821         #endif /* configUSE_NEWLIB_REENTRANT */
4822
4823         #if ( ( configSUPPORT_DYNAMIC_ALLOCATION == 1 ) && ( configSUPPORT_STATIC_ALLOCATION == 0 ) && ( portUSING_MPU_WRAPPERS == 0 ) )
4824             {
4825                 /* The task can only have been allocated dynamically - free both
4826                  * the stack and TCB. */
4827                 vPortFreeStack( pxTCB->pxStack );
4828                 vPortFree( pxTCB );
4829             }
4830         #elif ( tskSTATIC_AND_DYNAMIC_ALLOCATION_POSSIBLE != 0 ) /*lint !e731 !e9029 Macro has been consolidated for readability reasons. */
4831             {
4832                 /* The task could have been allocated statically or dynamically, so
4833                  * check what was statically allocated before trying to free the
4834                  * memory. */
4835                 if( pxTCB->ucStaticallyAllocated == tskDYNAMICALLY_ALLOCATED_STACK_AND_TCB )
4836                 {
4837                     /* Both the stack and TCB were allocated dynamically, so both
4838                      * must be freed. */
4839                     vPortFreeStack( pxTCB->pxStack );
4840                     vPortFree( pxTCB );
4841                 }
4842                 else if( pxTCB->ucStaticallyAllocated == tskSTATICALLY_ALLOCATED_STACK_ONLY )
4843                 {
4844                     /* Only the stack was statically allocated, so the TCB is the
4845                      * only memory that must be freed. */
4846                     vPortFree( pxTCB );
4847                 }
4848                 else
4849                 {
4850                     /* Neither the stack nor the TCB were allocated dynamically, so
4851                      * nothing needs to be freed. */
4852                     configASSERT( pxTCB->ucStaticallyAllocated == tskSTATICALLY_ALLOCATED_STACK_AND_TCB );
4853                     mtCOVERAGE_TEST_MARKER();
4854                 }
4855             }
4856         #endif /* configSUPPORT_DYNAMIC_ALLOCATION */
4857     }
4858
4859 #endif /* INCLUDE_vTaskDelete */
4860 /*-----------------------------------------------------------*/
4861
4862 static void prvResetNextTaskUnblockTime( void )
4863 {
4864     if( listLIST_IS_EMPTY( pxDelayedTaskList ) != pdFALSE )
4865     {
4866         /* The new current delayed list is empty.  Set xNextTaskUnblockTime to
4867          * the maximum possible value so it is  extremely unlikely that the
4868          * if( xTickCount >= xNextTaskUnblockTime ) test will pass until
4869          * there is an item in the delayed list. */
4870         xNextTaskUnblockTime = portMAX_DELAY;
4871     }
4872     else
4873     {
4874         /* The new current delayed list is not empty, get the value of
4875          * the item at the head of the delayed list.  This is the time at
4876          * which the task at the head of the delayed list should be removed
4877          * from the Blocked state. */
4878         xNextTaskUnblockTime = listGET_ITEM_VALUE_OF_HEAD_ENTRY( pxDelayedTaskList );
4879     }
4880 }
4881 /*-----------------------------------------------------------*/
4882
4883 #if ( ( INCLUDE_xTaskGetCurrentTaskHandle == 1 ) || ( configUSE_MUTEXES == 1 ) )
4884
4885     TaskHandle_t xTaskGetCurrentTaskHandle( void )
4886     {
4887         TaskHandle_t xReturn;
4888         uint32_t ulState;
4889
4890         ulState = portDISABLE_INTERRUPTS();
4891         xReturn = pxCurrentTCBs[ portGET_CORE_ID() ];
4892         portRESTORE_INTERRUPTS( ulState );
4893
4894         return xReturn;
4895     }
4896
4897     TaskHandle_t xTaskGetCurrentTaskHandleCPU( UBaseType_t xCoreID )
4898     {
4899         TaskHandle_t xReturn = NULL;
4900
4901         if( taskVALID_CORE_ID( xCoreID ) != pdFALSE )
4902         {
4903             xReturn = pxCurrentTCBs[ xCoreID ];
4904         }
4905
4906         return xReturn;
4907     }
4908
4909 #endif /* ( ( INCLUDE_xTaskGetCurrentTaskHandle == 1 ) || ( configUSE_MUTEXES == 1 ) ) */
4910 /*-----------------------------------------------------------*/
4911
4912 #if ( ( INCLUDE_xTaskGetSchedulerState == 1 ) || ( configUSE_TIMERS == 1 ) )
4913
4914     BaseType_t xTaskGetSchedulerState( void )
4915     {
4916         BaseType_t xReturn;
4917
4918         if( xSchedulerRunning == pdFALSE )
4919         {
4920             xReturn = taskSCHEDULER_NOT_STARTED;
4921         }
4922         else
4923         {
4924             taskENTER_CRITICAL();
4925             {
4926                 if( uxSchedulerSuspended == ( UBaseType_t ) pdFALSE )
4927                 {
4928                     xReturn = taskSCHEDULER_RUNNING;
4929                 }
4930                 else
4931                 {
4932                     xReturn = taskSCHEDULER_SUSPENDED;
4933                 }
4934             }
4935             taskEXIT_CRITICAL();
4936         }
4937
4938         return xReturn;
4939     }
4940
4941 #endif /* ( ( INCLUDE_xTaskGetSchedulerState == 1 ) || ( configUSE_TIMERS == 1 ) ) */
4942 /*-----------------------------------------------------------*/
4943
4944 #if ( configUSE_MUTEXES == 1 )
4945
4946     BaseType_t xTaskPriorityInherit( TaskHandle_t const pxMutexHolder )
4947     {
4948         TCB_t * const pxMutexHolderTCB = pxMutexHolder;
4949         BaseType_t xReturn = pdFALSE;
4950
4951         /* If the mutex was given back by an interrupt while the queue was
4952          * locked then the mutex holder might now be NULL.  _RB_ Is this still
4953          * needed as interrupts can no longer use mutexes? */
4954         if( pxMutexHolder != NULL )
4955         {
4956             /* If the holder of the mutex has a priority below the priority of
4957              * the task attempting to obtain the mutex then it will temporarily
4958              * inherit the priority of the task attempting to obtain the mutex. */
4959             if( pxMutexHolderTCB->uxPriority < pxCurrentTCB->uxPriority )
4960             {
4961                 /* Adjust the mutex holder state to account for its new
4962                  * priority.  Only reset the event list item value if the value is
4963                  * not being used for anything else. */
4964                 if( ( listGET_LIST_ITEM_VALUE( &( pxMutexHolderTCB->xEventListItem ) ) & taskEVENT_LIST_ITEM_VALUE_IN_USE ) == 0UL )
4965                 {
4966                     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. */
4967                 }
4968                 else
4969                 {
4970                     mtCOVERAGE_TEST_MARKER();
4971                 }
4972
4973                 /* If the task being modified is in the ready state it will need
4974                  * to be moved into a new list. */
4975                 if( listIS_CONTAINED_WITHIN( &( pxReadyTasksLists[ pxMutexHolderTCB->uxPriority ] ), &( pxMutexHolderTCB->xStateListItem ) ) != pdFALSE )
4976                 {
4977                     if( uxListRemove( &( pxMutexHolderTCB->xStateListItem ) ) == ( UBaseType_t ) 0 )
4978                     {
4979                         /* It is known that the task is in its ready list so
4980                          * there is no need to check again and the port level
4981                          * reset macro can be called directly. */
4982                         portRESET_READY_PRIORITY( pxMutexHolderTCB->uxPriority, uxTopReadyPriority );
4983                     }
4984                     else
4985                     {
4986                         mtCOVERAGE_TEST_MARKER();
4987                     }
4988
4989                     /* Inherit the priority before being moved into the new list. */
4990                     pxMutexHolderTCB->uxPriority = pxCurrentTCB->uxPriority;
4991                     prvAddTaskToReadyList( pxMutexHolderTCB );
4992                 }
4993                 else
4994                 {
4995                     /* Just inherit the priority. */
4996                     pxMutexHolderTCB->uxPriority = pxCurrentTCB->uxPriority;
4997                 }
4998
4999                 traceTASK_PRIORITY_INHERIT( pxMutexHolderTCB, pxCurrentTCB->uxPriority );
5000
5001                 /* Inheritance occurred. */
5002                 xReturn = pdTRUE;
5003             }
5004             else
5005             {
5006                 if( pxMutexHolderTCB->uxBasePriority < pxCurrentTCB->uxPriority )
5007                 {
5008                     /* The base priority of the mutex holder is lower than the
5009                      * priority of the task attempting to take the mutex, but the
5010                      * current priority of the mutex holder is not lower than the
5011                      * priority of the task attempting to take the mutex.
5012                      * Therefore the mutex holder must have already inherited a
5013                      * priority, but inheritance would have occurred if that had
5014                      * not been the case. */
5015                     xReturn = pdTRUE;
5016                 }
5017                 else
5018                 {
5019                     mtCOVERAGE_TEST_MARKER();
5020                 }
5021             }
5022         }
5023         else
5024         {
5025             mtCOVERAGE_TEST_MARKER();
5026         }
5027
5028         return xReturn;
5029     }
5030
5031 #endif /* configUSE_MUTEXES */
5032 /*-----------------------------------------------------------*/
5033
5034 #if ( configUSE_MUTEXES == 1 )
5035
5036     BaseType_t xTaskPriorityDisinherit( TaskHandle_t const pxMutexHolder )
5037     {
5038         TCB_t * const pxTCB = pxMutexHolder;
5039         BaseType_t xReturn = pdFALSE;
5040
5041         if( pxMutexHolder != NULL )
5042         {
5043             /* A task can only have an inherited priority if it holds the mutex.
5044              * If the mutex is held by a task then it cannot be given from an
5045              * interrupt, and if a mutex is given by the holding task then it must
5046              * be the running state task. */
5047             configASSERT( pxTCB == pxCurrentTCB );
5048             configASSERT( pxTCB->uxMutexesHeld );
5049             ( pxTCB->uxMutexesHeld )--;
5050
5051             /* Has the holder of the mutex inherited the priority of another
5052              * task? */
5053             if( pxTCB->uxPriority != pxTCB->uxBasePriority )
5054             {
5055                 /* Only disinherit if no other mutexes are held. */
5056                 if( pxTCB->uxMutexesHeld == ( UBaseType_t ) 0 )
5057                 {
5058                     /* A task can only have an inherited priority if it holds
5059                      * the mutex.  If the mutex is held by a task then it cannot be
5060                      * given from an interrupt, and if a mutex is given by the
5061                      * holding task then it must be the running state task.  Remove
5062                      * the holding task from the ready list. */
5063                     if( uxListRemove( &( pxTCB->xStateListItem ) ) == ( UBaseType_t ) 0 )
5064                     {
5065                         portRESET_READY_PRIORITY( pxTCB->uxPriority, uxTopReadyPriority );
5066                     }
5067                     else
5068                     {
5069                         mtCOVERAGE_TEST_MARKER();
5070                     }
5071
5072                     /* Disinherit the priority before adding the task into the
5073                      * new  ready list. */
5074                     traceTASK_PRIORITY_DISINHERIT( pxTCB, pxTCB->uxBasePriority );
5075                     pxTCB->uxPriority = pxTCB->uxBasePriority;
5076
5077                     /* Reset the event list item value.  It cannot be in use for
5078                      * any other purpose if this task is running, and it must be
5079                      * running to give back the mutex. */
5080                     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. */
5081                     prvAddTaskToReadyList( pxTCB );
5082
5083                     /* Return true to indicate that a context switch is required.
5084                      * This is only actually required in the corner case whereby
5085                      * multiple mutexes were held and the mutexes were given back
5086                      * in an order different to that in which they were taken.
5087                      * If a context switch did not occur when the first mutex was
5088                      * returned, even if a task was waiting on it, then a context
5089                      * switch should occur when the last mutex is returned whether
5090                      * a task is waiting on it or not. */
5091                     xReturn = pdTRUE;
5092                 }
5093                 else
5094                 {
5095                     mtCOVERAGE_TEST_MARKER();
5096                 }
5097             }
5098             else
5099             {
5100                 mtCOVERAGE_TEST_MARKER();
5101             }
5102         }
5103         else
5104         {
5105             mtCOVERAGE_TEST_MARKER();
5106         }
5107
5108         return xReturn;
5109     }
5110
5111 #endif /* configUSE_MUTEXES */
5112 /*-----------------------------------------------------------*/
5113
5114 #if ( configUSE_MUTEXES == 1 )
5115
5116     void vTaskPriorityDisinheritAfterTimeout( TaskHandle_t const pxMutexHolder,
5117                                               UBaseType_t uxHighestPriorityWaitingTask )
5118     {
5119         TCB_t * const pxTCB = pxMutexHolder;
5120         UBaseType_t uxPriorityUsedOnEntry, uxPriorityToUse;
5121         const UBaseType_t uxOnlyOneMutexHeld = ( UBaseType_t ) 1;
5122
5123         if( pxMutexHolder != NULL )
5124         {
5125             /* If pxMutexHolder is not NULL then the holder must hold at least
5126              * one mutex. */
5127             configASSERT( pxTCB->uxMutexesHeld );
5128
5129             /* Determine the priority to which the priority of the task that
5130              * holds the mutex should be set.  This will be the greater of the
5131              * holding task's base priority and the priority of the highest
5132              * priority task that is waiting to obtain the mutex. */
5133             if( pxTCB->uxBasePriority < uxHighestPriorityWaitingTask )
5134             {
5135                 uxPriorityToUse = uxHighestPriorityWaitingTask;
5136             }
5137             else
5138             {
5139                 uxPriorityToUse = pxTCB->uxBasePriority;
5140             }
5141
5142             /* Does the priority need to change? */
5143             if( pxTCB->uxPriority != uxPriorityToUse )
5144             {
5145                 /* Only disinherit if no other mutexes are held.  This is a
5146                  * simplification in the priority inheritance implementation.  If
5147                  * the task that holds the mutex is also holding other mutexes then
5148                  * the other mutexes may have caused the priority inheritance. */
5149                 if( pxTCB->uxMutexesHeld == uxOnlyOneMutexHeld )
5150                 {
5151                     /* If a task has timed out because it already holds the
5152                      * mutex it was trying to obtain then it cannot of inherited
5153                      * its own priority. */
5154                     configASSERT( pxTCB != pxCurrentTCB );
5155
5156                     /* Disinherit the priority, remembering the previous
5157                      * priority to facilitate determining the subject task's
5158                      * state. */
5159                     traceTASK_PRIORITY_DISINHERIT( pxTCB, uxPriorityToUse );
5160                     uxPriorityUsedOnEntry = pxTCB->uxPriority;
5161                     pxTCB->uxPriority = uxPriorityToUse;
5162
5163                     /* Only reset the event list item value if the value is not
5164                      * being used for anything else. */
5165                     if( ( listGET_LIST_ITEM_VALUE( &( pxTCB->xEventListItem ) ) & taskEVENT_LIST_ITEM_VALUE_IN_USE ) == 0UL )
5166                     {
5167                         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. */
5168                     }
5169                     else
5170                     {
5171                         mtCOVERAGE_TEST_MARKER();
5172                     }
5173
5174                     /* If the running task is not the task that holds the mutex
5175                      * then the task that holds the mutex could be in either the
5176                      * Ready, Blocked or Suspended states.  Only remove the task
5177                      * from its current state list if it is in the Ready state as
5178                      * the task's priority is going to change and there is one
5179                      * Ready list per priority. */
5180                     if( listIS_CONTAINED_WITHIN( &( pxReadyTasksLists[ uxPriorityUsedOnEntry ] ), &( pxTCB->xStateListItem ) ) != pdFALSE )
5181                     {
5182                         if( uxListRemove( &( pxTCB->xStateListItem ) ) == ( UBaseType_t ) 0 )
5183                         {
5184                             /* It is known that the task is in its ready list so
5185                              * there is no need to check again and the port level
5186                              * reset macro can be called directly. */
5187                             portRESET_READY_PRIORITY( pxTCB->uxPriority, uxTopReadyPriority );
5188                         }
5189                         else
5190                         {
5191                             mtCOVERAGE_TEST_MARKER();
5192                         }
5193
5194                         prvAddTaskToReadyList( pxTCB );
5195                     }
5196                     else
5197                     {
5198                         mtCOVERAGE_TEST_MARKER();
5199                     }
5200                 }
5201                 else
5202                 {
5203                     mtCOVERAGE_TEST_MARKER();
5204                 }
5205             }
5206             else
5207             {
5208                 mtCOVERAGE_TEST_MARKER();
5209             }
5210         }
5211         else
5212         {
5213             mtCOVERAGE_TEST_MARKER();
5214         }
5215     }
5216
5217 #endif /* configUSE_MUTEXES */
5218 /*-----------------------------------------------------------*/
5219
5220 /*
5221  * If not in a critical section then yield immediately.
5222  * Otherwise set xYieldPending to true to wait to
5223  * yield until exiting the critical section.
5224  */
5225 void vTaskYieldWithinAPI( void )
5226 {
5227     if( pxCurrentTCB->uxCriticalNesting == 0U )
5228     {
5229         portYIELD();
5230     }
5231     else
5232     {
5233         xYieldPendings[ portGET_CORE_ID() ] = pdTRUE;
5234     }
5235 }
5236 /*-----------------------------------------------------------*/
5237
5238 #if ( portCRITICAL_NESTING_IN_TCB == 1 )
5239
5240     void vTaskEnterCritical( void )
5241     {
5242         portDISABLE_INTERRUPTS();
5243
5244         if( xSchedulerRunning != pdFALSE )
5245         {
5246             if( pxCurrentTCB->uxCriticalNesting == 0U )
5247             {
5248                 if( portCHECK_IF_IN_ISR() == pdFALSE )
5249                 {
5250                     portGET_TASK_LOCK();
5251                 }
5252
5253                 portGET_ISR_LOCK();
5254             }
5255
5256             ( pxCurrentTCB->uxCriticalNesting )++;
5257
5258             /* This should now be interrupt safe. The only time there would be
5259              * a problem is if this is called before a context switch and
5260              * vTaskExitCritical() is called after pxCurrentTCB changes. Therefore
5261              * this should not be used within vTaskSwitchContext(). */
5262
5263             if( ( uxSchedulerSuspended == 0U ) && ( pxCurrentTCB->uxCriticalNesting == 1U ) )
5264             {
5265                 prvCheckForRunStateChange();
5266             }
5267         }
5268         else
5269         {
5270             mtCOVERAGE_TEST_MARKER();
5271         }
5272     }
5273
5274 #endif /* portCRITICAL_NESTING_IN_TCB */
5275 /*-----------------------------------------------------------*/
5276
5277 #if ( portCRITICAL_NESTING_IN_TCB == 1 )
5278
5279     void vTaskExitCritical( void )
5280     {
5281         if( xSchedulerRunning != pdFALSE )
5282         {
5283             /* If pxCurrentTCB->uxCriticalNesting is zero then this function
5284              * does not match a previous call to vTaskEnterCritical(). */
5285             configASSERT( pxCurrentTCB->uxCriticalNesting > 0U );
5286
5287             if( pxCurrentTCB->uxCriticalNesting > 0U )
5288             {
5289                 ( pxCurrentTCB->uxCriticalNesting )--;
5290
5291                 if( pxCurrentTCB->uxCriticalNesting == 0U )
5292                 {
5293                     portRELEASE_ISR_LOCK();
5294
5295                     if( portCHECK_IF_IN_ISR() == pdFALSE )
5296                     {
5297                         portRELEASE_TASK_LOCK();
5298                         portENABLE_INTERRUPTS();
5299
5300                         /* When a task yields in a critical section it just sets
5301                          * xYieldPending to true. So now that we have exited the
5302                          * critical section check if xYieldPending is true, and
5303                          * if so yield. */
5304                         if( xYieldPending != pdFALSE )
5305                         {
5306                             portYIELD();
5307                         }
5308                     }
5309                     else
5310                     {
5311                         /* In an ISR we don't hold the task lock and don't
5312                          * need to yield. Yield will happen if necessary when
5313                          * the application ISR calls portEND_SWITCHING_ISR() */
5314                         mtCOVERAGE_TEST_MARKER();
5315                     }
5316                 }
5317                 else
5318                 {
5319                     mtCOVERAGE_TEST_MARKER();
5320                 }
5321             }
5322             else
5323             {
5324                 mtCOVERAGE_TEST_MARKER();
5325             }
5326         }
5327         else
5328         {
5329             mtCOVERAGE_TEST_MARKER();
5330         }
5331     }
5332
5333 #endif /* portCRITICAL_NESTING_IN_TCB */
5334 /*-----------------------------------------------------------*/
5335
5336 #if ( ( configUSE_TRACE_FACILITY == 1 ) && ( configUSE_STATS_FORMATTING_FUNCTIONS > 0 ) )
5337
5338     static char * prvWriteNameToBuffer( char * pcBuffer,
5339                                         const char * pcTaskName )
5340     {
5341         size_t x;
5342
5343         /* Start by copying the entire string. */
5344         strcpy( pcBuffer, pcTaskName );
5345
5346         /* Pad the end of the string with spaces to ensure columns line up when
5347          * printed out. */
5348         for( x = strlen( pcBuffer ); x < ( size_t ) ( configMAX_TASK_NAME_LEN - 1 ); x++ )
5349         {
5350             pcBuffer[ x ] = ' ';
5351         }
5352
5353         /* Terminate. */
5354         pcBuffer[ x ] = ( char ) 0x00;
5355
5356         /* Return the new end of string. */
5357         return &( pcBuffer[ x ] );
5358     }
5359
5360 #endif /* ( configUSE_TRACE_FACILITY == 1 ) && ( configUSE_STATS_FORMATTING_FUNCTIONS > 0 ) */
5361 /*-----------------------------------------------------------*/
5362
5363 #if ( ( configUSE_TRACE_FACILITY == 1 ) && ( configUSE_STATS_FORMATTING_FUNCTIONS > 0 ) && ( configSUPPORT_DYNAMIC_ALLOCATION == 1 ) )
5364
5365     void vTaskList( char * pcWriteBuffer )
5366     {
5367         TaskStatus_t * pxTaskStatusArray;
5368         UBaseType_t uxArraySize, x;
5369         char cStatus;
5370
5371         /*
5372          * PLEASE NOTE:
5373          *
5374          * This function is provided for convenience only, and is used by many
5375          * of the demo applications.  Do not consider it to be part of the
5376          * scheduler.
5377          *
5378          * vTaskList() calls uxTaskGetSystemState(), then formats part of the
5379          * uxTaskGetSystemState() output into a human readable table that
5380          * displays task: names, states, priority, stack usage and task number.
5381          * Stack usage specified as the number of unused StackType_t words stack can hold
5382          * on top of stack - not the number of bytes.
5383          *
5384          * vTaskList() has a dependency on the sprintf() C library function that
5385          * might bloat the code size, use a lot of stack, and provide different
5386          * results on different platforms.  An alternative, tiny, third party,
5387          * and limited functionality implementation of sprintf() is provided in
5388          * many of the FreeRTOS/Demo sub-directories in a file called
5389          * printf-stdarg.c (note printf-stdarg.c does not provide a full
5390          * snprintf() implementation!).
5391          *
5392          * It is recommended that production systems call uxTaskGetSystemState()
5393          * directly to get access to raw stats data, rather than indirectly
5394          * through a call to vTaskList().
5395          */
5396
5397
5398         /* Make sure the write buffer does not contain a string. */
5399         *pcWriteBuffer = ( char ) 0x00;
5400
5401         /* Take a snapshot of the number of tasks in case it changes while this
5402          * function is executing. */
5403         uxArraySize = uxCurrentNumberOfTasks;
5404
5405         /* Allocate an array index for each task.  NOTE!  if
5406          * configSUPPORT_DYNAMIC_ALLOCATION is set to 0 then pvPortMalloc() will
5407          * equate to NULL. */
5408         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. */
5409
5410         if( pxTaskStatusArray != NULL )
5411         {
5412             /* Generate the (binary) data. */
5413             uxArraySize = uxTaskGetSystemState( pxTaskStatusArray, uxArraySize, NULL );
5414
5415             /* Create a human readable table from the binary data. */
5416             for( x = 0; x < uxArraySize; x++ )
5417             {
5418                 switch( pxTaskStatusArray[ x ].eCurrentState )
5419                 {
5420                     case eRunning:
5421                         cStatus = tskRUNNING_CHAR;
5422                         break;
5423
5424                     case eReady:
5425                         cStatus = tskREADY_CHAR;
5426                         break;
5427
5428                     case eBlocked:
5429                         cStatus = tskBLOCKED_CHAR;
5430                         break;
5431
5432                     case eSuspended:
5433                         cStatus = tskSUSPENDED_CHAR;
5434                         break;
5435
5436                     case eDeleted:
5437                         cStatus = tskDELETED_CHAR;
5438                         break;
5439
5440                     case eInvalid: /* Fall through. */
5441                     default:       /* Should not get here, but it is included
5442                                     * to prevent static checking errors. */
5443                         cStatus = ( char ) 0x00;
5444                         break;
5445                 }
5446
5447                 /* Write the task name to the string, padding with spaces so it
5448                  * can be printed in tabular form more easily. */
5449                 pcWriteBuffer = prvWriteNameToBuffer( pcWriteBuffer, pxTaskStatusArray[ x ].pcTaskName );
5450
5451                 /* Write the rest of the string. */
5452                 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. */
5453                 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. */
5454             }
5455
5456             /* Free the array again.  NOTE!  If configSUPPORT_DYNAMIC_ALLOCATION
5457              * is 0 then vPortFree() will be #defined to nothing. */
5458             vPortFree( pxTaskStatusArray );
5459         }
5460         else
5461         {
5462             mtCOVERAGE_TEST_MARKER();
5463         }
5464     }
5465
5466 #endif /* ( ( configUSE_TRACE_FACILITY == 1 ) && ( configUSE_STATS_FORMATTING_FUNCTIONS > 0 ) && ( configSUPPORT_DYNAMIC_ALLOCATION == 1 ) ) */
5467 /*----------------------------------------------------------*/
5468
5469 #if ( ( configGENERATE_RUN_TIME_STATS == 1 ) && ( configUSE_STATS_FORMATTING_FUNCTIONS > 0 ) && ( configSUPPORT_DYNAMIC_ALLOCATION == 1 ) )
5470
5471     void vTaskGetRunTimeStats( char * pcWriteBuffer )
5472     {
5473         TaskStatus_t * pxTaskStatusArray;
5474         UBaseType_t uxArraySize, x;
5475         uint32_t ulTotalTime, ulStatsAsPercentage;
5476
5477         #if ( configUSE_TRACE_FACILITY != 1 )
5478             {
5479                 #error configUSE_TRACE_FACILITY must also be set to 1 in FreeRTOSConfig.h to use vTaskGetRunTimeStats().
5480             }
5481         #endif
5482
5483         /*
5484          * PLEASE NOTE:
5485          *
5486          * This function is provided for convenience only, and is used by many
5487          * of the demo applications.  Do not consider it to be part of the
5488          * scheduler.
5489          *
5490          * vTaskGetRunTimeStats() calls uxTaskGetSystemState(), then formats part
5491          * of the uxTaskGetSystemState() output into a human readable table that
5492          * displays the amount of time each task has spent in the Running state
5493          * in both absolute and percentage terms.
5494          *
5495          * vTaskGetRunTimeStats() has a dependency on the sprintf() C library
5496          * function that might bloat the code size, use a lot of stack, and
5497          * provide different results on different platforms.  An alternative,
5498          * tiny, third party, and limited functionality implementation of
5499          * sprintf() is provided in many of the FreeRTOS/Demo sub-directories in
5500          * a file called printf-stdarg.c (note printf-stdarg.c does not provide
5501          * a full snprintf() implementation!).
5502          *
5503          * It is recommended that production systems call uxTaskGetSystemState()
5504          * directly to get access to raw stats data, rather than indirectly
5505          * through a call to vTaskGetRunTimeStats().
5506          */
5507
5508         /* Make sure the write buffer does not contain a string. */
5509         *pcWriteBuffer = ( char ) 0x00;
5510
5511         /* Take a snapshot of the number of tasks in case it changes while this
5512          * function is executing. */
5513         uxArraySize = uxCurrentNumberOfTasks;
5514
5515         /* Allocate an array index for each task.  NOTE!  If
5516          * configSUPPORT_DYNAMIC_ALLOCATION is set to 0 then pvPortMalloc() will
5517          * equate to NULL. */
5518         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. */
5519
5520         if( pxTaskStatusArray != NULL )
5521         {
5522             /* Generate the (binary) data. */
5523             uxArraySize = uxTaskGetSystemState( pxTaskStatusArray, uxArraySize, &ulTotalTime );
5524
5525             /* For percentage calculations. */
5526             ulTotalTime /= 100UL;
5527
5528             /* Avoid divide by zero errors. */
5529             if( ulTotalTime > 0UL )
5530             {
5531                 /* Create a human readable table from the binary data. */
5532                 for( x = 0; x < uxArraySize; x++ )
5533                 {
5534                     /* What percentage of the total run time has the task used?
5535                      * This will always be rounded down to the nearest integer.
5536                      * ulTotalRunTimeDiv100 has already been divided by 100. */
5537                     ulStatsAsPercentage = pxTaskStatusArray[ x ].ulRunTimeCounter / ulTotalTime;
5538
5539                     /* Write the task name to the string, padding with
5540                      * spaces so it can be printed in tabular form more
5541                      * easily. */
5542                     pcWriteBuffer = prvWriteNameToBuffer( pcWriteBuffer, pxTaskStatusArray[ x ].pcTaskName );
5543
5544                     if( ulStatsAsPercentage > 0UL )
5545                     {
5546                         #ifdef portLU_PRINTF_SPECIFIER_REQUIRED
5547                             {
5548                                 sprintf( pcWriteBuffer, "\t%lu\t\t%lu%%\r\n", pxTaskStatusArray[ x ].ulRunTimeCounter, ulStatsAsPercentage );
5549                             }
5550                         #else
5551                             {
5552                                 /* sizeof( int ) == sizeof( long ) so a smaller
5553                                  * printf() library can be used. */
5554                                 sprintf( pcWriteBuffer, "\t%u\t\t%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. */
5555                             }
5556                         #endif
5557                     }
5558                     else
5559                     {
5560                         /* If the percentage is zero here then the task has
5561                          * consumed less than 1% of the total run time. */
5562                         #ifdef portLU_PRINTF_SPECIFIER_REQUIRED
5563                             {
5564                                 sprintf( pcWriteBuffer, "\t%lu\t\t<1%%\r\n", pxTaskStatusArray[ x ].ulRunTimeCounter );
5565                             }
5566                         #else
5567                             {
5568                                 /* sizeof( int ) == sizeof( long ) so a smaller
5569                                  * printf() library can be used. */
5570                                 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. */
5571                             }
5572                         #endif
5573                     }
5574
5575                     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. */
5576                 }
5577             }
5578             else
5579             {
5580                 mtCOVERAGE_TEST_MARKER();
5581             }
5582
5583             /* Free the array again.  NOTE!  If configSUPPORT_DYNAMIC_ALLOCATION
5584              * is 0 then vPortFree() will be #defined to nothing. */
5585             vPortFree( pxTaskStatusArray );
5586         }
5587         else
5588         {
5589             mtCOVERAGE_TEST_MARKER();
5590         }
5591     }
5592
5593 #endif /* ( ( configGENERATE_RUN_TIME_STATS == 1 ) && ( configUSE_STATS_FORMATTING_FUNCTIONS > 0 ) && ( configSUPPORT_STATIC_ALLOCATION == 1 ) ) */
5594 /*-----------------------------------------------------------*/
5595
5596 TickType_t uxTaskResetEventItemValue( void )
5597 {
5598     TickType_t uxReturn;
5599
5600     uxReturn = listGET_LIST_ITEM_VALUE( &( pxCurrentTCB->xEventListItem ) );
5601
5602     /* Reset the event list item to its normal value - so it can be used with
5603      * queues and semaphores. */
5604     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. */
5605
5606     return uxReturn;
5607 }
5608 /*-----------------------------------------------------------*/
5609
5610 #if ( configUSE_MUTEXES == 1 )
5611
5612     TaskHandle_t pvTaskIncrementMutexHeldCount( void )
5613     {
5614         /* If xSemaphoreCreateMutex() is called before any tasks have been created
5615          * then pxCurrentTCB will be NULL. */
5616         if( pxCurrentTCB != NULL )
5617         {
5618             ( pxCurrentTCB->uxMutexesHeld )++;
5619         }
5620
5621         return pxCurrentTCB;
5622     }
5623
5624 #endif /* configUSE_MUTEXES */
5625 /*-----------------------------------------------------------*/
5626
5627 #if ( configUSE_TASK_NOTIFICATIONS == 1 )
5628
5629     uint32_t ulTaskGenericNotifyTake( UBaseType_t uxIndexToWait,
5630                                       BaseType_t xClearCountOnExit,
5631                                       TickType_t xTicksToWait )
5632     {
5633         uint32_t ulReturn;
5634
5635         configASSERT( uxIndexToWait < configTASK_NOTIFICATION_ARRAY_ENTRIES );
5636
5637         taskENTER_CRITICAL();
5638         {
5639             /* Only block if the notification count is not already non-zero. */
5640             if( pxCurrentTCB->ulNotifiedValue[ uxIndexToWait ] == 0UL )
5641             {
5642                 /* Mark this task as waiting for a notification. */
5643                 pxCurrentTCB->ucNotifyState[ uxIndexToWait ] = taskWAITING_NOTIFICATION;
5644
5645                 if( xTicksToWait > ( TickType_t ) 0 )
5646                 {
5647                     prvAddCurrentTaskToDelayedList( xTicksToWait, pdTRUE );
5648                     traceTASK_NOTIFY_TAKE_BLOCK( uxIndexToWait );
5649
5650                     /* All ports are written to allow a yield in a critical
5651                      * section (some will yield immediately, others wait until the
5652                      * critical section exits) - but it is not something that
5653                      * application code should ever do. */
5654                     vTaskYieldWithinAPI();
5655                 }
5656                 else
5657                 {
5658                     mtCOVERAGE_TEST_MARKER();
5659                 }
5660             }
5661             else
5662             {
5663                 mtCOVERAGE_TEST_MARKER();
5664             }
5665         }
5666         taskEXIT_CRITICAL();
5667
5668         taskENTER_CRITICAL();
5669         {
5670             traceTASK_NOTIFY_TAKE( uxIndexToWait );
5671             ulReturn = pxCurrentTCB->ulNotifiedValue[ uxIndexToWait ];
5672
5673             if( ulReturn != 0UL )
5674             {
5675                 if( xClearCountOnExit != pdFALSE )
5676                 {
5677                     pxCurrentTCB->ulNotifiedValue[ uxIndexToWait ] = 0UL;
5678                 }
5679                 else
5680                 {
5681                     pxCurrentTCB->ulNotifiedValue[ uxIndexToWait ] = ulReturn - ( uint32_t ) 1;
5682                 }
5683             }
5684             else
5685             {
5686                 mtCOVERAGE_TEST_MARKER();
5687             }
5688
5689             pxCurrentTCB->ucNotifyState[ uxIndexToWait ] = taskNOT_WAITING_NOTIFICATION;
5690         }
5691         taskEXIT_CRITICAL();
5692
5693         return ulReturn;
5694     }
5695
5696 #endif /* configUSE_TASK_NOTIFICATIONS */
5697 /*-----------------------------------------------------------*/
5698
5699 #if ( configUSE_TASK_NOTIFICATIONS == 1 )
5700
5701     BaseType_t xTaskGenericNotifyWait( UBaseType_t uxIndexToWait,
5702                                        uint32_t ulBitsToClearOnEntry,
5703                                        uint32_t ulBitsToClearOnExit,
5704                                        uint32_t * pulNotificationValue,
5705                                        TickType_t xTicksToWait )
5706     {
5707         BaseType_t xReturn;
5708
5709         configASSERT( uxIndexToWait < configTASK_NOTIFICATION_ARRAY_ENTRIES );
5710
5711         taskENTER_CRITICAL();
5712         {
5713             /* Only block if a notification is not already pending. */
5714             if( pxCurrentTCB->ucNotifyState[ uxIndexToWait ] != taskNOTIFICATION_RECEIVED )
5715             {
5716                 /* Clear bits in the task's notification value as bits may get
5717                  * set  by the notifying task or interrupt.  This can be used to
5718                  * clear the value to zero. */
5719                 pxCurrentTCB->ulNotifiedValue[ uxIndexToWait ] &= ~ulBitsToClearOnEntry;
5720
5721                 /* Mark this task as waiting for a notification. */
5722                 pxCurrentTCB->ucNotifyState[ uxIndexToWait ] = taskWAITING_NOTIFICATION;
5723
5724                 if( xTicksToWait > ( TickType_t ) 0 )
5725                 {
5726                     prvAddCurrentTaskToDelayedList( xTicksToWait, pdTRUE );
5727                     traceTASK_NOTIFY_WAIT_BLOCK( uxIndexToWait );
5728
5729                     /* All ports are written to allow a yield in a critical
5730                      * section (some will yield immediately, others wait until the
5731                      * critical section exits) - but it is not something that
5732                      * application code should ever do. */
5733                     vTaskYieldWithinAPI();
5734                 }
5735                 else
5736                 {
5737                     mtCOVERAGE_TEST_MARKER();
5738                 }
5739             }
5740             else
5741             {
5742                 mtCOVERAGE_TEST_MARKER();
5743             }
5744         }
5745         taskEXIT_CRITICAL();
5746
5747         taskENTER_CRITICAL();
5748         {
5749             traceTASK_NOTIFY_WAIT( uxIndexToWait );
5750
5751             if( pulNotificationValue != NULL )
5752             {
5753                 /* Output the current notification value, which may or may not
5754                  * have changed. */
5755                 *pulNotificationValue = pxCurrentTCB->ulNotifiedValue[ uxIndexToWait ];
5756             }
5757
5758             /* If ucNotifyValue is set then either the task never entered the
5759              * blocked state (because a notification was already pending) or the
5760              * task unblocked because of a notification.  Otherwise the task
5761              * unblocked because of a timeout. */
5762             if( pxCurrentTCB->ucNotifyState[ uxIndexToWait ] != taskNOTIFICATION_RECEIVED )
5763             {
5764                 /* A notification was not received. */
5765                 xReturn = pdFALSE;
5766             }
5767             else
5768             {
5769                 /* A notification was already pending or a notification was
5770                  * received while the task was waiting. */
5771                 pxCurrentTCB->ulNotifiedValue[ uxIndexToWait ] &= ~ulBitsToClearOnExit;
5772                 xReturn = pdTRUE;
5773             }
5774
5775             pxCurrentTCB->ucNotifyState[ uxIndexToWait ] = taskNOT_WAITING_NOTIFICATION;
5776         }
5777         taskEXIT_CRITICAL();
5778
5779         return xReturn;
5780     }
5781
5782 #endif /* configUSE_TASK_NOTIFICATIONS */
5783 /*-----------------------------------------------------------*/
5784
5785 #if ( configUSE_TASK_NOTIFICATIONS == 1 )
5786
5787     BaseType_t xTaskGenericNotify( TaskHandle_t xTaskToNotify,
5788                                    UBaseType_t uxIndexToNotify,
5789                                    uint32_t ulValue,
5790                                    eNotifyAction eAction,
5791                                    uint32_t * pulPreviousNotificationValue )
5792     {
5793         TCB_t * pxTCB;
5794         BaseType_t xReturn = pdPASS;
5795         uint8_t ucOriginalNotifyState;
5796
5797         configASSERT( uxIndexToNotify < configTASK_NOTIFICATION_ARRAY_ENTRIES );
5798         configASSERT( xTaskToNotify );
5799         pxTCB = xTaskToNotify;
5800
5801         taskENTER_CRITICAL();
5802         {
5803             if( pulPreviousNotificationValue != NULL )
5804             {
5805                 *pulPreviousNotificationValue = pxTCB->ulNotifiedValue[ uxIndexToNotify ];
5806             }
5807
5808             ucOriginalNotifyState = pxTCB->ucNotifyState[ uxIndexToNotify ];
5809
5810             pxTCB->ucNotifyState[ uxIndexToNotify ] = taskNOTIFICATION_RECEIVED;
5811
5812             switch( eAction )
5813             {
5814                 case eSetBits:
5815                     pxTCB->ulNotifiedValue[ uxIndexToNotify ] |= ulValue;
5816                     break;
5817
5818                 case eIncrement:
5819                     ( pxTCB->ulNotifiedValue[ uxIndexToNotify ] )++;
5820                     break;
5821
5822                 case eSetValueWithOverwrite:
5823                     pxTCB->ulNotifiedValue[ uxIndexToNotify ] = ulValue;
5824                     break;
5825
5826                 case eSetValueWithoutOverwrite:
5827
5828                     if( ucOriginalNotifyState != taskNOTIFICATION_RECEIVED )
5829                     {
5830                         pxTCB->ulNotifiedValue[ uxIndexToNotify ] = ulValue;
5831                     }
5832                     else
5833                     {
5834                         /* The value could not be written to the task. */
5835                         xReturn = pdFAIL;
5836                     }
5837
5838                     break;
5839
5840                 case eNoAction:
5841
5842                     /* The task is being notified without its notify value being
5843                      * updated. */
5844                     break;
5845
5846                 default:
5847
5848                     /* Should not get here if all enums are handled.
5849                      * Artificially force an assert by testing a value the
5850                      * compiler can't assume is const. */
5851                     configASSERT( xTickCount == ( TickType_t ) 0 );
5852
5853                     break;
5854             }
5855
5856             traceTASK_NOTIFY( uxIndexToNotify );
5857
5858             /* If the task is in the blocked state specifically to wait for a
5859              * notification then unblock it now. */
5860             if( ucOriginalNotifyState == taskWAITING_NOTIFICATION )
5861             {
5862                 ( void ) uxListRemove( &( pxTCB->xStateListItem ) );
5863                 prvAddTaskToReadyList( pxTCB );
5864
5865                 /* The task should not have been on an event list. */
5866                 configASSERT( listLIST_ITEM_CONTAINER( &( pxTCB->xEventListItem ) ) == NULL );
5867
5868                 #if ( configUSE_TICKLESS_IDLE != 0 )
5869                     {
5870                         /* If a task is blocked waiting for a notification then
5871                          * xNextTaskUnblockTime might be set to the blocked task's time
5872                          * out time.  If the task is unblocked for a reason other than
5873                          * a timeout xNextTaskUnblockTime is normally left unchanged,
5874                          * because it will automatically get reset to a new value when
5875                          * the tick count equals xNextTaskUnblockTime.  However if
5876                          * tickless idling is used it might be more important to enter
5877                          * sleep mode at the earliest possible time - so reset
5878                          * xNextTaskUnblockTime here to ensure it is updated at the
5879                          * earliest possible time. */
5880                         prvResetNextTaskUnblockTime();
5881                     }
5882                 #endif
5883
5884                 #if ( configUSE_PREEMPTION == 1 )
5885                     {
5886                         prvYieldForTask( pxTCB, pdFALSE );
5887                     }
5888                 #endif
5889             }
5890             else
5891             {
5892                 mtCOVERAGE_TEST_MARKER();
5893             }
5894         }
5895         taskEXIT_CRITICAL();
5896
5897         return xReturn;
5898     }
5899
5900 #endif /* configUSE_TASK_NOTIFICATIONS */
5901 /*-----------------------------------------------------------*/
5902
5903 #if ( configUSE_TASK_NOTIFICATIONS == 1 )
5904
5905     BaseType_t xTaskGenericNotifyFromISR( TaskHandle_t xTaskToNotify,
5906                                           UBaseType_t uxIndexToNotify,
5907                                           uint32_t ulValue,
5908                                           eNotifyAction eAction,
5909                                           uint32_t * pulPreviousNotificationValue,
5910                                           BaseType_t * pxHigherPriorityTaskWoken )
5911     {
5912         TCB_t * pxTCB;
5913         uint8_t ucOriginalNotifyState;
5914         BaseType_t xReturn = pdPASS;
5915         UBaseType_t uxSavedInterruptStatus;
5916
5917         configASSERT( xTaskToNotify );
5918         configASSERT( uxIndexToNotify < configTASK_NOTIFICATION_ARRAY_ENTRIES );
5919
5920         /* RTOS ports that support interrupt nesting have the concept of a
5921          * maximum  system call (or maximum API call) interrupt priority.
5922          * Interrupts that are  above the maximum system call priority are keep
5923          * permanently enabled, even when the RTOS kernel is in a critical section,
5924          * but cannot make any calls to FreeRTOS API functions.  If configASSERT()
5925          * is defined in FreeRTOSConfig.h then
5926          * portASSERT_IF_INTERRUPT_PRIORITY_INVALID() will result in an assertion
5927          * failure if a FreeRTOS API function is called from an interrupt that has
5928          * been assigned a priority above the configured maximum system call
5929          * priority.  Only FreeRTOS functions that end in FromISR can be called
5930          * from interrupts  that have been assigned a priority at or (logically)
5931          * below the maximum system call interrupt priority.  FreeRTOS maintains a
5932          * separate interrupt safe API to ensure interrupt entry is as fast and as
5933          * simple as possible.  More information (albeit Cortex-M specific) is
5934          * provided on the following link:
5935          * https://www.FreeRTOS.org/RTOS-Cortex-M3-M4.html */
5936         portASSERT_IF_INTERRUPT_PRIORITY_INVALID();
5937
5938         pxTCB = xTaskToNotify;
5939
5940         uxSavedInterruptStatus = portSET_INTERRUPT_MASK_FROM_ISR();
5941         {
5942             if( pulPreviousNotificationValue != NULL )
5943             {
5944                 *pulPreviousNotificationValue = pxTCB->ulNotifiedValue[ uxIndexToNotify ];
5945             }
5946
5947             ucOriginalNotifyState = pxTCB->ucNotifyState[ uxIndexToNotify ];
5948             pxTCB->ucNotifyState[ uxIndexToNotify ] = taskNOTIFICATION_RECEIVED;
5949
5950             switch( eAction )
5951             {
5952                 case eSetBits:
5953                     pxTCB->ulNotifiedValue[ uxIndexToNotify ] |= ulValue;
5954                     break;
5955
5956                 case eIncrement:
5957                     ( pxTCB->ulNotifiedValue[ uxIndexToNotify ] )++;
5958                     break;
5959
5960                 case eSetValueWithOverwrite:
5961                     pxTCB->ulNotifiedValue[ uxIndexToNotify ] = ulValue;
5962                     break;
5963
5964                 case eSetValueWithoutOverwrite:
5965
5966                     if( ucOriginalNotifyState != taskNOTIFICATION_RECEIVED )
5967                     {
5968                         pxTCB->ulNotifiedValue[ uxIndexToNotify ] = ulValue;
5969                     }
5970                     else
5971                     {
5972                         /* The value could not be written to the task. */
5973                         xReturn = pdFAIL;
5974                     }
5975
5976                     break;
5977
5978                 case eNoAction:
5979
5980                     /* The task is being notified without its notify value being
5981                      * updated. */
5982                     break;
5983
5984                 default:
5985
5986                     /* Should not get here if all enums are handled.
5987                      * Artificially force an assert by testing a value the
5988                      * compiler can't assume is const. */
5989                     configASSERT( xTickCount == ( TickType_t ) 0 );
5990                     break;
5991             }
5992
5993             traceTASK_NOTIFY_FROM_ISR( uxIndexToNotify );
5994
5995             /* If the task is in the blocked state specifically to wait for a
5996              * notification then unblock it now. */
5997             if( ucOriginalNotifyState == taskWAITING_NOTIFICATION )
5998             {
5999                 /* The task should not have been on an event list. */
6000                 configASSERT( listLIST_ITEM_CONTAINER( &( pxTCB->xEventListItem ) ) == NULL );
6001
6002                 if( uxSchedulerSuspended == ( UBaseType_t ) pdFALSE )
6003                 {
6004                     ( void ) uxListRemove( &( pxTCB->xStateListItem ) );
6005                     prvAddTaskToReadyList( pxTCB );
6006                 }
6007                 else
6008                 {
6009                     /* The delayed and ready lists cannot be accessed, so hold
6010                      * this task pending until the scheduler is resumed. */
6011                     vListInsertEnd( &( xPendingReadyList ), &( pxTCB->xEventListItem ) );
6012                 }
6013
6014                 #if ( configUSE_PREEMPTION == 1 )
6015                     prvYieldForTask( pxTCB, pdFALSE );
6016
6017                     if( xYieldPendings[ portGET_CORE_ID() ] == pdTRUE )
6018                     {
6019                         if( pxHigherPriorityTaskWoken != NULL )
6020                         {
6021                             *pxHigherPriorityTaskWoken = pdTRUE;
6022                         }
6023                     }
6024                 #endif
6025             }
6026         }
6027         portCLEAR_INTERRUPT_MASK_FROM_ISR( uxSavedInterruptStatus );
6028
6029         return xReturn;
6030     }
6031
6032 #endif /* configUSE_TASK_NOTIFICATIONS */
6033 /*-----------------------------------------------------------*/
6034
6035 #if ( configUSE_TASK_NOTIFICATIONS == 1 )
6036
6037     void vTaskGenericNotifyGiveFromISR( TaskHandle_t xTaskToNotify,
6038                                         UBaseType_t uxIndexToNotify,
6039                                         BaseType_t * pxHigherPriorityTaskWoken )
6040     {
6041         TCB_t * pxTCB;
6042         uint8_t ucOriginalNotifyState;
6043         UBaseType_t uxSavedInterruptStatus;
6044
6045         configASSERT( xTaskToNotify );
6046         configASSERT( uxIndexToNotify < configTASK_NOTIFICATION_ARRAY_ENTRIES );
6047
6048         /* RTOS ports that support interrupt nesting have the concept of a
6049          * maximum  system call (or maximum API call) interrupt priority.
6050          * Interrupts that are  above the maximum system call priority are keep
6051          * permanently enabled, even when the RTOS kernel is in a critical section,
6052          * but cannot make any calls to FreeRTOS API functions.  If configASSERT()
6053          * is defined in FreeRTOSConfig.h then
6054          * portASSERT_IF_INTERRUPT_PRIORITY_INVALID() will result in an assertion
6055          * failure if a FreeRTOS API function is called from an interrupt that has
6056          * been assigned a priority above the configured maximum system call
6057          * priority.  Only FreeRTOS functions that end in FromISR can be called
6058          * from interrupts  that have been assigned a priority at or (logically)
6059          * below the maximum system call interrupt priority.  FreeRTOS maintains a
6060          * separate interrupt safe API to ensure interrupt entry is as fast and as
6061          * simple as possible.  More information (albeit Cortex-M specific) is
6062          * provided on the following link:
6063          * https://www.FreeRTOS.org/RTOS-Cortex-M3-M4.html */
6064         portASSERT_IF_INTERRUPT_PRIORITY_INVALID();
6065
6066         pxTCB = xTaskToNotify;
6067
6068         uxSavedInterruptStatus = portSET_INTERRUPT_MASK_FROM_ISR();
6069         {
6070             ucOriginalNotifyState = pxTCB->ucNotifyState[ uxIndexToNotify ];
6071             pxTCB->ucNotifyState[ uxIndexToNotify ] = taskNOTIFICATION_RECEIVED;
6072
6073             /* 'Giving' is equivalent to incrementing a count in a counting
6074              * semaphore. */
6075             ( pxTCB->ulNotifiedValue[ uxIndexToNotify ] )++;
6076
6077             traceTASK_NOTIFY_GIVE_FROM_ISR( uxIndexToNotify );
6078
6079             /* If the task is in the blocked state specifically to wait for a
6080              * notification then unblock it now. */
6081             if( ucOriginalNotifyState == taskWAITING_NOTIFICATION )
6082             {
6083                 /* The task should not have been on an event list. */
6084                 configASSERT( listLIST_ITEM_CONTAINER( &( pxTCB->xEventListItem ) ) == NULL );
6085
6086                 if( uxSchedulerSuspended == ( UBaseType_t ) pdFALSE )
6087                 {
6088                     ( void ) uxListRemove( &( pxTCB->xStateListItem ) );
6089                     prvAddTaskToReadyList( pxTCB );
6090                 }
6091                 else
6092                 {
6093                     /* The delayed and ready lists cannot be accessed, so hold
6094                      * this task pending until the scheduler is resumed. */
6095                     vListInsertEnd( &( xPendingReadyList ), &( pxTCB->xEventListItem ) );
6096                 }
6097
6098                 #if ( configUSE_PREEMPTION == 1 )
6099                     prvYieldForTask( pxTCB, pdFALSE );
6100
6101                     if( xYieldPendings[ portGET_CORE_ID() ] == pdTRUE )
6102                     {
6103                         if( pxHigherPriorityTaskWoken != NULL )
6104                         {
6105                             *pxHigherPriorityTaskWoken = pdTRUE;
6106                         }
6107                     }
6108                 #endif
6109             }
6110         }
6111         portCLEAR_INTERRUPT_MASK_FROM_ISR( uxSavedInterruptStatus );
6112     }
6113
6114 #endif /* configUSE_TASK_NOTIFICATIONS */
6115 /*-----------------------------------------------------------*/
6116
6117 #if ( configUSE_TASK_NOTIFICATIONS == 1 )
6118
6119     BaseType_t xTaskGenericNotifyStateClear( TaskHandle_t xTask,
6120                                              UBaseType_t uxIndexToClear )
6121     {
6122         TCB_t * pxTCB;
6123         BaseType_t xReturn;
6124
6125         configASSERT( uxIndexToClear < configTASK_NOTIFICATION_ARRAY_ENTRIES );
6126
6127         /* If null is passed in here then it is the calling task that is having
6128          * its notification state cleared. */
6129         pxTCB = prvGetTCBFromHandle( xTask );
6130
6131         taskENTER_CRITICAL();
6132         {
6133             if( pxTCB->ucNotifyState[ uxIndexToClear ] == taskNOTIFICATION_RECEIVED )
6134             {
6135                 pxTCB->ucNotifyState[ uxIndexToClear ] = taskNOT_WAITING_NOTIFICATION;
6136                 xReturn = pdPASS;
6137             }
6138             else
6139             {
6140                 xReturn = pdFAIL;
6141             }
6142         }
6143         taskEXIT_CRITICAL();
6144
6145         return xReturn;
6146     }
6147
6148 #endif /* configUSE_TASK_NOTIFICATIONS */
6149 /*-----------------------------------------------------------*/
6150
6151 #if ( configUSE_TASK_NOTIFICATIONS == 1 )
6152
6153     uint32_t ulTaskGenericNotifyValueClear( TaskHandle_t xTask,
6154                                             UBaseType_t uxIndexToClear,
6155                                             uint32_t ulBitsToClear )
6156     {
6157         TCB_t * pxTCB;
6158         uint32_t ulReturn;
6159
6160         /* If null is passed in here then it is the calling task that is having
6161          * its notification state cleared. */
6162         pxTCB = prvGetTCBFromHandle( xTask );
6163
6164         taskENTER_CRITICAL();
6165         {
6166             /* Return the notification as it was before the bits were cleared,
6167              * then clear the bit mask. */
6168             ulReturn = pxTCB->ulNotifiedValue[ uxIndexToClear ];
6169             pxTCB->ulNotifiedValue[ uxIndexToClear ] &= ~ulBitsToClear;
6170         }
6171         taskEXIT_CRITICAL();
6172
6173         return ulReturn;
6174     }
6175
6176 #endif /* configUSE_TASK_NOTIFICATIONS */
6177 /*-----------------------------------------------------------*/
6178
6179 #if ( ( configGENERATE_RUN_TIME_STATS == 1 ) && ( INCLUDE_xTaskGetIdleTaskHandle == 1 ) )
6180
6181     uint32_t ulTaskGetIdleRunTimeCounter( void )
6182     {
6183         uint32_t ulReturn = 0;
6184
6185         for( BaseType_t i = 0; i < configNUM_CORES; i++ )
6186         {
6187             ulReturn += xIdleTaskHandle[ i ]->ulRunTimeCounter;
6188         }
6189
6190         return ulReturn;
6191     }
6192
6193 #endif /* if ( ( configGENERATE_RUN_TIME_STATS == 1 ) && ( INCLUDE_xTaskGetIdleTaskHandle == 1 ) ) */
6194 /*-----------------------------------------------------------*/
6195
6196 static void prvAddCurrentTaskToDelayedList( TickType_t xTicksToWait,
6197                                             const BaseType_t xCanBlockIndefinitely )
6198 {
6199     TickType_t xTimeToWake;
6200     const TickType_t xConstTickCount = xTickCount;
6201
6202     #if ( INCLUDE_xTaskAbortDelay == 1 )
6203         {
6204             /* About to enter a delayed list, so ensure the ucDelayAborted flag is
6205              * reset to pdFALSE so it can be detected as having been set to pdTRUE
6206              * when the task leaves the Blocked state. */
6207             pxCurrentTCB->ucDelayAborted = pdFALSE;
6208         }
6209     #endif
6210
6211     /* Remove the task from the ready list before adding it to the blocked list
6212      * as the same list item is used for both lists. */
6213     if( uxListRemove( &( pxCurrentTCB->xStateListItem ) ) == ( UBaseType_t ) 0 )
6214     {
6215         /* The current task must be in a ready list, so there is no need to
6216          * check, and the port reset macro can be called directly. */
6217         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. */
6218     }
6219     else
6220     {
6221         mtCOVERAGE_TEST_MARKER();
6222     }
6223
6224     #if ( INCLUDE_vTaskSuspend == 1 )
6225         {
6226             if( ( xTicksToWait == portMAX_DELAY ) && ( xCanBlockIndefinitely != pdFALSE ) )
6227             {
6228                 /* Add the task to the suspended task list instead of a delayed task
6229                  * list to ensure it is not woken by a timing event.  It will block
6230                  * indefinitely. */
6231                 vListInsertEnd( &xSuspendedTaskList, &( pxCurrentTCB->xStateListItem ) );
6232             }
6233             else
6234             {
6235                 /* Calculate the time at which the task should be woken if the event
6236                  * does not occur.  This may overflow but this doesn't matter, the
6237                  * kernel will manage it correctly. */
6238                 xTimeToWake = xConstTickCount + xTicksToWait;
6239
6240                 /* The list item will be inserted in wake time order. */
6241                 listSET_LIST_ITEM_VALUE( &( pxCurrentTCB->xStateListItem ), xTimeToWake );
6242
6243                 if( xTimeToWake < xConstTickCount )
6244                 {
6245                     /* Wake time has overflowed.  Place this item in the overflow
6246                      * list. */
6247                     vListInsert( pxOverflowDelayedTaskList, &( pxCurrentTCB->xStateListItem ) );
6248                 }
6249                 else
6250                 {
6251                     /* The wake time has not overflowed, so the current block list
6252                      * is used. */
6253                     vListInsert( pxDelayedTaskList, &( pxCurrentTCB->xStateListItem ) );
6254
6255                     /* If the task entering the blocked state was placed at the
6256                      * head of the list of blocked tasks then xNextTaskUnblockTime
6257                      * needs to be updated too. */
6258                     if( xTimeToWake < xNextTaskUnblockTime )
6259                     {
6260                         xNextTaskUnblockTime = xTimeToWake;
6261                     }
6262                     else
6263                     {
6264                         mtCOVERAGE_TEST_MARKER();
6265                     }
6266                 }
6267             }
6268         }
6269     #else /* INCLUDE_vTaskSuspend */
6270         {
6271             /* Calculate the time at which the task should be woken if the event
6272              * does not occur.  This may overflow but this doesn't matter, the kernel
6273              * will manage it correctly. */
6274             xTimeToWake = xConstTickCount + xTicksToWait;
6275
6276             /* The list item will be inserted in wake time order. */
6277             listSET_LIST_ITEM_VALUE( &( pxCurrentTCB->xStateListItem ), xTimeToWake );
6278
6279             if( xTimeToWake < xConstTickCount )
6280             {
6281                 /* Wake time has overflowed.  Place this item in the overflow list. */
6282                 vListInsert( pxOverflowDelayedTaskList, &( pxCurrentTCB->xStateListItem ) );
6283             }
6284             else
6285             {
6286                 /* The wake time has not overflowed, so the current block list is used. */
6287                 vListInsert( pxDelayedTaskList, &( pxCurrentTCB->xStateListItem ) );
6288
6289                 /* If the task entering the blocked state was placed at the head of the
6290                  * list of blocked tasks then xNextTaskUnblockTime needs to be updated
6291                  * too. */
6292                 if( xTimeToWake < xNextTaskUnblockTime )
6293                 {
6294                     xNextTaskUnblockTime = xTimeToWake;
6295                 }
6296                 else
6297                 {
6298                     mtCOVERAGE_TEST_MARKER();
6299                 }
6300             }
6301
6302             /* Avoid compiler warning when INCLUDE_vTaskSuspend is not 1. */
6303             ( void ) xCanBlockIndefinitely;
6304         }
6305     #endif /* INCLUDE_vTaskSuspend */
6306 }
6307
6308 /* Code below here allows additional code to be inserted into this source file,
6309  * especially where access to file scope functions and data is needed (for example
6310  * when performing module tests). */
6311
6312 #ifdef FREERTOS_MODULE_TEST
6313     #include "tasks_test_access_functions.h"
6314 #endif
6315
6316
6317 #if ( configINCLUDE_FREERTOS_TASK_C_ADDITIONS_H == 1 )
6318
6319     #include "freertos_tasks_c_additions.h"
6320
6321     #ifdef FREERTOS_TASKS_C_ADDITIONS_INIT
6322         static void freertos_tasks_c_additions_init( void )
6323         {
6324             FREERTOS_TASKS_C_ADDITIONS_INIT();
6325         }
6326     #endif
6327
6328 #endif /* if ( configINCLUDE_FREERTOS_TASK_C_ADDITIONS_H == 1 ) */