2 * FreeRTOS Kernel V10.4.3
3 * Copyright (C) 2020 Amazon.com, Inc. or its affiliates. All Rights Reserved.
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:
12 * The above copyright notice and this permission notice shall be included in all
13 * copies or substantial portions of the Software.
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.
22 * https://www.FreeRTOS.org
23 * https://github.com/FreeRTOS
27 /* Standard includes. */
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
36 #define DEBUG_UNIT FREERTOS_TASKS
38 /* FreeRTOS includes. */
42 #include "stack_macros.h"
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. */
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 )
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. */
59 #endif /* configUSE_STATS_FORMATTING_FUNCTIONS == 1 ) */
61 #if ( configUSE_PREEMPTION == 0 )
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()
67 #define taskYIELD_IF_USING_PREEMPTION() vTaskYieldWithinAPI()
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 )
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.
79 #define tskSTACK_FILL_BYTE ( 0xa5U )
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 )
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
92 #define tskSET_NEW_STACKS_TO_KNOWN_VALUE 0
96 * Macros used by vListTask to indicate which state a task is in.
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' )
105 * Some kernel aware debuggers require the data the debugger needs access to to
106 * be global, rather than file scope.
108 #ifdef portREMOVE_STATIC_QUALIFIER
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"
118 #if ( configUSE_PORT_OPTIMISED_TASK_SELECTION == 0 )
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. */
124 /* uxTopReadyPriority holds the priority of the highest priority ready
126 #define taskRECORD_READY_PRIORITY( uxPriority ) \
128 if( ( uxPriority ) > uxTopReadyPriority ) \
130 uxTopReadyPriority = ( uxPriority ); \
132 } /* taskRECORD_READY_PRIORITY */
134 /*-----------------------------------------------------------*/
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
139 #define taskRESET_READY_PRIORITY( uxPriority )
140 #define portRESET_READY_PRIORITY( uxPriority, uxTopReadyPriority )
142 #else /* configUSE_PORT_OPTIMISED_TASK_SELECTION */
144 #error configUSE_PORT_OPTIMISED_TASK_SELECTION not yet supported in SMP
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. */
150 /* A port optimised version is provided. Call the port defined macros. */
151 #define taskRECORD_READY_PRIORITY( uxPriority ) portRECORD_READY_PRIORITY( uxPriority, uxTopReadyPriority )
153 /*-----------------------------------------------------------*/
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 ) \
160 if( listCURRENT_LIST_LENGTH( &( pxReadyTasksLists[ ( uxPriority ) ] ) ) == ( UBaseType_t ) 0 ) \
162 portRESET_READY_PRIORITY( ( uxPriority ), ( uxTopReadyPriority ) ); \
166 #endif /* configUSE_PORT_OPTIMISED_TASK_SELECTION */
168 /*-----------------------------------------------------------*/
170 /* pxDelayedTaskList and pxOverflowDelayedTaskList are switched when the tick
171 * count overflows. */
172 #define taskSWITCH_DELAYED_LISTS() \
176 /* The delayed tasks list should be empty when the lists are switched. */ \
177 configASSERT( ( listLIST_IS_EMPTY( pxDelayedTaskList ) ) ); \
179 pxTemp = pxDelayedTaskList; \
180 pxDelayedTaskList = pxOverflowDelayedTaskList; \
181 pxOverflowDelayedTaskList = pxTemp; \
183 prvResetNextTaskUnblockTime(); \
186 /*-----------------------------------------------------------*/
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.
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 /*-----------------------------------------------------------*/
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.
205 #define prvGetTCBFromHandle( pxHandle ) ( ( ( pxHandle ) == NULL ) ? pxCurrentTCB : ( pxHandle ) )
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
218 #define taskEVENT_LIST_ITEM_VALUE_IN_USE 0x80000000UL
221 /* Indicates that the task is not actively running on any core. */
222 #define taskTASK_NOT_RUNNING ( TaskRunning_t ) ( -1 )
224 /* Indicates that the task is actively running but scheduled to yield. */
225 #define taskTASK_YIELDING ( TaskRunning_t ) ( -2 )
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 ) )
230 typedef BaseType_t TaskRunning_t;
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)
237 typedef struct tskTaskControlBlock /* The old naming convention is used to prevent breaking kernel aware debuggers. */
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. */
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. */
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. */
253 #if ( configUSE_TASK_PREEMPTION_DISABLE == 1 )
254 BaseType_t xPreemptionDisable; /*< Used to prevent the task from being preempted */
257 #if ( configUSE_CORE_EXCLUSION == 1 )
258 UBaseType_t uxCoreExclude; /*< Used to exclude the task from certain cores */
261 #if ( ( portSTACK_GROWTH > 0 ) || ( configRECORD_STACK_HIGH_ADDRESS == 1 ) )
262 StackType_t * pxEndOfStack; /*< Points to the highest valid address for the stack. */
265 #if ( portCRITICAL_NESTING_IN_TCB == 1 )
266 UBaseType_t uxCriticalNesting; /*< Holds the critical section nesting depth for ports that do not maintain their own count in the port layer. */
269 #if ( configUSE_TRACE_FACILITY == 1 )
270 UBaseType_t uxTCBNumber; /*< Stores a number that increments each time a TCB is created. It allows debuggers to determine when a task has been deleted and then recreated. */
271 UBaseType_t uxTaskNumber; /*< Stores a number specifically for use by third party trace code. */
274 #if ( configUSE_MUTEXES == 1 )
275 UBaseType_t uxBasePriority; /*< The priority last assigned to the task - used by the priority inheritance mechanism. */
276 UBaseType_t uxMutexesHeld;
279 #if ( configUSE_APPLICATION_TASK_TAG == 1 )
280 TaskHookFunction_t pxTaskTag;
283 #if ( configNUM_THREAD_LOCAL_STORAGE_POINTERS > 0 )
284 void * pvThreadLocalStoragePointers[ configNUM_THREAD_LOCAL_STORAGE_POINTERS ];
287 #if ( configGENERATE_RUN_TIME_STATS == 1 )
288 uint32_t ulRunTimeCounter; /*< Stores the amount of time the task has spent in the Running state. */
291 #if ( configUSE_NEWLIB_REENTRANT == 1 )
293 /* Allocate a Newlib reent structure that is specific to this task.
294 * Note Newlib support has been included by popular demand, but is not
295 * used by the FreeRTOS maintainers themselves. FreeRTOS is not
296 * responsible for resulting newlib operation. User must be familiar with
297 * newlib and must provide system-wide implementations of the necessary
298 * stubs. Be warned that (at the time of writing) the current newlib design
299 * implements a system-wide malloc() that must be provided with locks.
301 * See the third party link http://www.nadler.com/embedded/newlibAndFreeRTOS.html
302 * for additional information. */
303 struct _reent xNewLib_reent;
306 #if ( configUSE_TASK_NOTIFICATIONS == 1 )
307 volatile uint32_t ulNotifiedValue[ configTASK_NOTIFICATION_ARRAY_ENTRIES ];
308 volatile uint8_t ucNotifyState[ configTASK_NOTIFICATION_ARRAY_ENTRIES ];
311 /* See the comments in FreeRTOS.h with the definition of
312 * tskSTATIC_AND_DYNAMIC_ALLOCATION_POSSIBLE. */
313 #if ( tskSTATIC_AND_DYNAMIC_ALLOCATION_POSSIBLE != 0 ) /*lint !e731 !e9029 Macro has been consolidated for readability reasons. */
314 uint8_t ucStaticallyAllocated; /*< Set to pdTRUE if the task is a statically allocated to ensure no attempt is made to free the memory. */
317 #if ( INCLUDE_xTaskAbortDelay == 1 )
318 uint8_t ucDelayAborted;
321 #if ( configUSE_POSIX_ERRNO == 1 )
326 /* The old tskTCB name is maintained above then typedefed to the new TCB_t name
327 * below to enable the use of older kernel aware debuggers. */
328 typedef tskTCB TCB_t;
330 /*lint -save -e956 A manual analysis and inspection has been used to determine
331 * which static variables must be declared volatile. */
332 PRIVILEGED_DATA TCB_t * volatile pxCurrentTCBs[ configNUM_CORES ] = { NULL };
333 #define pxCurrentTCB xTaskGetCurrentTaskHandle()
335 /* Lists for ready and blocked tasks. --------------------
336 * xDelayedTaskList1 and xDelayedTaskList2 could be moved to function scope but
337 * doing so breaks some kernel aware debuggers and debuggers that rely on removing
338 * the static qualifier. */
339 PRIVILEGED_DATA static List_t pxReadyTasksLists[ configMAX_PRIORITIES ]; /*< Prioritised ready tasks. */
340 PRIVILEGED_DATA static List_t xDelayedTaskList1; /*< Delayed tasks. */
341 PRIVILEGED_DATA static List_t xDelayedTaskList2; /*< Delayed tasks (two lists are used - one for delays that have overflowed the current tick count. */
342 PRIVILEGED_DATA static List_t * volatile pxDelayedTaskList; /*< Points to the delayed task list currently being used. */
343 PRIVILEGED_DATA static List_t * volatile pxOverflowDelayedTaskList; /*< Points to the delayed task list currently being used to hold tasks that have overflowed the current tick count. */
344 PRIVILEGED_DATA static List_t xPendingReadyList; /*< Tasks that have been readied while the scheduler was suspended. They will be moved to the ready list when the scheduler is resumed. */
346 #if ( INCLUDE_vTaskDelete == 1 )
348 PRIVILEGED_DATA static List_t xTasksWaitingTermination; /*< Tasks that have been deleted - but their memory not yet freed. */
349 PRIVILEGED_DATA static volatile UBaseType_t uxDeletedTasksWaitingCleanUp = ( UBaseType_t ) 0U;
353 #if ( INCLUDE_vTaskSuspend == 1 )
355 PRIVILEGED_DATA static List_t xSuspendedTaskList; /*< Tasks that are currently suspended. */
359 /* Global POSIX errno. Its value is changed upon context switching to match
360 * the errno of the currently running task. */
361 #if ( configUSE_POSIX_ERRNO == 1 )
362 int FreeRTOS_errno = 0;
365 /* Other file private variables. --------------------------------*/
366 PRIVILEGED_DATA static volatile UBaseType_t uxCurrentNumberOfTasks = ( UBaseType_t ) 0U;
367 PRIVILEGED_DATA static volatile TickType_t xTickCount = ( TickType_t ) configINITIAL_TICK_COUNT;
368 PRIVILEGED_DATA static volatile UBaseType_t uxTopReadyPriority = tskIDLE_PRIORITY;
369 PRIVILEGED_DATA static volatile BaseType_t xSchedulerRunning = pdFALSE;
370 PRIVILEGED_DATA static volatile TickType_t xPendedTicks = ( TickType_t ) 0U;
371 PRIVILEGED_DATA static volatile BaseType_t xYieldPendings[ configNUM_CORES ] = { pdFALSE };
372 PRIVILEGED_DATA static volatile BaseType_t xNumOfOverflows = ( BaseType_t ) 0;
373 PRIVILEGED_DATA static UBaseType_t uxTaskNumber = ( UBaseType_t ) 0U;
374 PRIVILEGED_DATA static volatile TickType_t xNextTaskUnblockTime = ( TickType_t ) 0U; /* Initialised to portMAX_DELAY before the scheduler starts. */
375 PRIVILEGED_DATA static TaskHandle_t xIdleTaskHandle[ configNUM_CORES ] = { NULL }; /*< Holds the handle of the idle task. The idle task is created automatically when the scheduler is started. */
377 #define xYieldPending prvGetCurrentYieldPending()
379 /* Improve support for OpenOCD. The kernel tracks Ready tasks via priority lists.
380 * For tracking the state of remote threads, OpenOCD uses uxTopUsedPriority
381 * to determine the number of priority lists to read back from the remote target. */
382 const volatile UBaseType_t uxTopUsedPriority = configMAX_PRIORITIES - 1U;
384 /* Context switches are held pending while the scheduler is suspended. Also,
385 * interrupts must not manipulate the xStateListItem of a TCB, or any of the
386 * lists the xStateListItem can be referenced from, if the scheduler is suspended.
387 * If an interrupt needs to unblock a task while the scheduler is suspended then it
388 * moves the task's event list item into the xPendingReadyList, ready for the
389 * kernel to move the task from the pending ready list into the real ready list
390 * when the scheduler is unsuspended. The pending ready list itself can only be
391 * accessed from a critical section.
393 * Updates to uxSchedulerSuspended must be protected by both the task and ISR locks and
394 * must not be done by an ISR. Reads must be protected by either lock and may be done by
395 * either an ISR or a task. */
396 PRIVILEGED_DATA static volatile UBaseType_t uxSchedulerSuspended = ( UBaseType_t ) pdFALSE;
398 #if ( configGENERATE_RUN_TIME_STATS == 1 )
400 /* Do not move these variables to function scope as doing so prevents the
401 * code working with debuggers that need to remove the static qualifier. */
402 PRIVILEGED_DATA static uint32_t ulTaskSwitchedInTime = 0UL; /*< Holds the value of a timer/counter the last time a task was switched in. */
403 PRIVILEGED_DATA static volatile uint32_t ulTotalRunTime = 0UL; /*< Holds the total amount of execution time as defined by the run time counter clock. */
409 /*-----------------------------------------------------------*/
411 /* File private functions. --------------------------------*/
414 * Returns the yield pending count for the calling core.
416 static BaseType_t prvGetCurrentYieldPending( void );
419 * Checks to see if another task moved the current task out of the ready
420 * list while it was waiting to enter a critical section and yields if so.
422 static void prvCheckForRunStateChange( void );
425 * Yields the given core.
427 static void prvYieldCore( BaseType_t xCoreID );
430 * Yields a core, or cores if multiple priorities are not allowed to run
431 * simultaneously, to allow the task pxTCB to run.
433 static void prvYieldForTask( TCB_t * pxTCB,
434 const BaseType_t xPreemptEqualPriority );
437 * Selects the highest priority available task
439 static BaseType_t prvSelectHighestPriorityTask( const BaseType_t xCoreID );
442 * Utility task that simply returns pdTRUE if the task referenced by xTask is
443 * currently in the Suspended state, or pdFALSE if the task referenced by xTask
444 * is in any other state.
446 #if ( INCLUDE_vTaskSuspend == 1 )
448 static BaseType_t prvTaskIsTaskSuspended( const TaskHandle_t xTask ) PRIVILEGED_FUNCTION;
450 #endif /* INCLUDE_vTaskSuspend */
453 * Utility to ready all the lists used by the scheduler. This is called
454 * automatically upon the creation of the first task.
456 static void prvInitialiseTaskLists( void ) PRIVILEGED_FUNCTION;
459 * The idle task, which as all tasks is implemented as a never ending loop.
460 * The idle task is automatically created and added to the ready lists upon
461 * creation of the first user task.
463 * The portTASK_FUNCTION_PROTO() macro is used to allow port/compiler specific
464 * language extensions. The equivalent prototype for this function is:
466 * void prvIdleTask( void *pvParameters );
469 static portTASK_FUNCTION_PROTO( prvIdleTask, pvParameters ) PRIVILEGED_FUNCTION;
472 * Utility to free all memory allocated by the scheduler to hold a TCB,
473 * including the stack pointed to by the TCB.
475 * This does not free memory allocated by the task itself (i.e. memory
476 * allocated by calls to pvPortMalloc from within the tasks application code).
478 #if ( INCLUDE_vTaskDelete == 1 )
480 static void prvDeleteTCB( TCB_t * pxTCB ) PRIVILEGED_FUNCTION;
485 * Used only by the idle task. This checks to see if anything has been placed
486 * in the list of tasks waiting to be deleted. If so the task is cleaned up
487 * and its TCB deleted.
489 static void prvCheckTasksWaitingTermination( void ) PRIVILEGED_FUNCTION;
492 * The currently executing task is entering the Blocked state. Add the task to
493 * either the current or the overflow delayed task list.
495 static void prvAddCurrentTaskToDelayedList( TickType_t xTicksToWait,
496 const BaseType_t xCanBlockIndefinitely ) PRIVILEGED_FUNCTION;
499 * Fills an TaskStatus_t structure with information on each task that is
500 * referenced from the pxList list (which may be a ready list, a delayed list,
501 * a suspended list, etc.).
503 * THIS FUNCTION IS INTENDED FOR DEBUGGING ONLY, AND SHOULD NOT BE CALLED FROM
504 * NORMAL APPLICATION CODE.
506 #if ( configUSE_TRACE_FACILITY == 1 )
508 static UBaseType_t prvListTasksWithinSingleList( TaskStatus_t * pxTaskStatusArray,
510 eTaskState eState ) PRIVILEGED_FUNCTION;
515 * Searches pxList for a task with name pcNameToQuery - returning a handle to
516 * the task if it is found, or NULL if the task is not found.
518 #if ( INCLUDE_xTaskGetHandle == 1 )
520 static TCB_t * prvSearchForNameWithinSingleList( List_t * pxList,
521 const char pcNameToQuery[] ) PRIVILEGED_FUNCTION;
526 * When a task is created, the stack of the task is filled with a known value.
527 * This function determines the 'high water mark' of the task stack by
528 * determining how much of the stack remains at the original preset value.
530 #if ( ( configUSE_TRACE_FACILITY == 1 ) || ( INCLUDE_uxTaskGetStackHighWaterMark == 1 ) || ( INCLUDE_uxTaskGetStackHighWaterMark2 == 1 ) )
532 static configSTACK_DEPTH_TYPE prvTaskCheckFreeStackSpace( const uint8_t * pucStackByte ) PRIVILEGED_FUNCTION;
537 * Return the amount of time, in ticks, that will pass before the kernel will
538 * next move a task from the Blocked state to the Running state.
540 * This conditional compilation should use inequality to 0, not equality to 1.
541 * This is to ensure portSUPPRESS_TICKS_AND_SLEEP() can be called when user
542 * defined low power mode implementations require configUSE_TICKLESS_IDLE to be
543 * set to a value other than 1.
545 #if ( configUSE_TICKLESS_IDLE != 0 )
547 static TickType_t prvGetExpectedIdleTime( void ) PRIVILEGED_FUNCTION;
552 * Set xNextTaskUnblockTime to the time at which the next Blocked state task
553 * will exit the Blocked state.
555 static void prvResetNextTaskUnblockTime( void ) PRIVILEGED_FUNCTION;
557 #if ( ( configUSE_TRACE_FACILITY == 1 ) && ( configUSE_STATS_FORMATTING_FUNCTIONS > 0 ) )
560 * Helper function used to pad task names with spaces when printing out
561 * human readable tables of task information.
563 static char * prvWriteNameToBuffer( char * pcBuffer,
564 const char * pcTaskName ) PRIVILEGED_FUNCTION;
569 * Called after a Task_t structure has been allocated either statically or
570 * dynamically to fill in the structure's members.
572 static void prvInitialiseNewTask( TaskFunction_t pxTaskCode,
573 const char * const pcName, /*lint !e971 Unqualified char types are allowed for strings and single characters only. */
574 const uint32_t ulStackDepth,
575 void * const pvParameters,
576 UBaseType_t uxPriority,
577 TaskHandle_t * const pxCreatedTask,
579 const MemoryRegion_t * const xRegions ) PRIVILEGED_FUNCTION;
582 * Called after a new task has been created and initialised to place the task
583 * under the control of the scheduler.
585 static void prvAddNewTaskToReadyList( TCB_t * pxNewTCB ) PRIVILEGED_FUNCTION;
588 * freertos_tasks_c_additions_init() should only be called if the user definable
589 * macro FREERTOS_TASKS_C_ADDITIONS_INIT() is defined, as that is the only macro
590 * called by the function.
592 #ifdef FREERTOS_TASKS_C_ADDITIONS_INIT
594 static void freertos_tasks_c_additions_init( void ) PRIVILEGED_FUNCTION;
598 /*-----------------------------------------------------------*/
600 static BaseType_t prvGetCurrentYieldPending( void )
605 ulState = portDISABLE_INTERRUPTS();
606 xReturn = xYieldPendings[ portGET_CORE_ID() ];
607 portRESTORE_INTERRUPTS( ulState );
612 /*-----------------------------------------------------------*/
614 static void prvCheckForRunStateChange( void )
616 UBaseType_t uxPrevCriticalNesting;
617 UBaseType_t uxPrevSchedulerSuspended;
620 /* This should be skipped when entering a critical section within
621 * an ISR. If the task on the current core is no longer running, then
622 * vTaskSwitchContext() probably should be run before returning, but
623 * we don't have a way to force that to happen from here. */
624 if( portCHECK_IF_IN_ISR() == pdFALSE )
626 /* This function is always called with interrupts disabled
627 * so this is safe. */
628 pxThisTCB = pxCurrentTCBs[ portGET_CORE_ID() ];
630 while( pxThisTCB->xTaskRunState == taskTASK_YIELDING )
632 /* We are only here if we just entered a critical section
633 * or if we just suspended the scheduler, and another task
634 * has requested that we yield.
636 * This is slightly complicated since we need to save and restore
637 * the suspension and critical nesting counts, as well as release
638 * and reacquire the correct locks. And then do it all over again
639 * if our state changed again during the reacquisition. */
641 uxPrevCriticalNesting = pxThisTCB->uxCriticalNesting;
642 uxPrevSchedulerSuspended = uxSchedulerSuspended;
644 /* this must only be called the first time we enter into a critical
645 * section, otherwise it could context switch in the middle of a
646 * critical section. */
647 configASSERT( uxPrevCriticalNesting + uxPrevSchedulerSuspended == 1U );
649 uxSchedulerSuspended = 0U;
651 if( uxPrevCriticalNesting > 0U )
653 pxThisTCB->uxCriticalNesting = 0U;
654 portRELEASE_ISR_LOCK();
655 portRELEASE_TASK_LOCK();
659 /* uxPrevSchedulerSuspended must be 1 */
660 portRELEASE_TASK_LOCK();
663 portMEMORY_BARRIER();
664 configASSERT( pxThisTCB->xTaskRunState == taskTASK_YIELDING );
666 portENABLE_INTERRUPTS();
668 /* Enabling interrupts should cause this core to immediately
669 * service the pending interrupt and yield. If the run state is still
670 * yielding here then that is a problem. */
671 configASSERT( pxThisTCB->xTaskRunState != taskTASK_YIELDING );
673 portDISABLE_INTERRUPTS();
676 pxCurrentTCB->uxCriticalNesting = uxPrevCriticalNesting;
677 uxSchedulerSuspended = uxPrevSchedulerSuspended;
679 if( uxPrevCriticalNesting == 0U )
681 /* uxPrevSchedulerSuspended must be 1 */
682 configASSERT( uxPrevSchedulerSuspended != ( UBaseType_t ) pdFALSE );
683 portRELEASE_ISR_LOCK();
689 /*-----------------------------------------------------------*/
691 static void prvYieldCore( BaseType_t xCoreID )
693 /* This must be called from a critical section and
694 * xCoreID must be valid. */
696 if( portCHECK_IF_IN_ISR() && ( xCoreID == portGET_CORE_ID() ) )
698 xYieldPendings[ xCoreID ] = pdTRUE;
700 else if( pxCurrentTCBs[ xCoreID ]->xTaskRunState != taskTASK_YIELDING )
702 if( xCoreID == portGET_CORE_ID() )
704 xYieldPendings[ xCoreID ] = pdTRUE;
708 portYIELD_CORE( xCoreID );
709 pxCurrentTCBs[ xCoreID ]->xTaskRunState = taskTASK_YIELDING;
714 /*-----------------------------------------------------------*/
716 static void prvYieldForTask( TCB_t * pxTCB,
717 const BaseType_t xPreemptEqualPriority )
719 BaseType_t xLowestPriority;
720 BaseType_t xTaskPriority;
721 BaseType_t xLowestPriorityCore = -1;
722 BaseType_t xYieldCount = 0;
724 TaskRunning_t xTaskRunState;
726 /* THIS FUNCTION MUST BE CALLED FROM A CRITICAL SECTION */
728 configASSERT( pxCurrentTCB->uxCriticalNesting > 0U );
730 #if ( ( configRUN_MULTIPLE_PRIORITIES == 0 ) && ( configNUM_CORES > 1 ) )
732 /* No task should yield for this one if it is a lower priority
733 * than priority level of currently ready tasks. */
734 if( pxTCB->uxPriority < uxTopReadyPriority )
741 xLowestPriority = ( BaseType_t ) pxTCB->uxPriority;
743 if( xPreemptEqualPriority == pdFALSE )
745 /* xLowestPriority will be decremented to -1 if the priority of pxTCB
746 * is 0. This is ok as we will give system idle tasks a priority of -1 below. */
750 for( x = ( BaseType_t ) 0; x < ( BaseType_t ) configNUM_CORES; x++ )
752 /* System idle tasks are being assigned a priority of tskIDLE_PRIORITY - 1 here */
753 xTaskPriority = ( BaseType_t ) pxCurrentTCBs[ x ]->uxPriority - pxCurrentTCBs[ x ]->xIsIdle;
754 xTaskRunState = pxCurrentTCBs[ x ]->xTaskRunState;
756 if( ( taskTASK_IS_RUNNING( xTaskRunState ) != pdFALSE ) && ( xYieldPendings[ x ] == pdFALSE ) )
758 if( xTaskPriority <= xLowestPriority )
760 #if ( configUSE_CORE_EXCLUSION == 1 )
761 if( ( pxTCB->uxCoreExclude & ( 1 << x ) ) == 0 )
764 #if ( configUSE_TASK_PREEMPTION_DISABLE == 1 )
765 if( pxCurrentTCBs[ x ]->xPreemptionDisable == pdFALSE )
768 xLowestPriority = xTaskPriority;
769 xLowestPriorityCore = x;
775 mtCOVERAGE_TEST_MARKER();
778 #if ( ( configRUN_MULTIPLE_PRIORITIES == 0 ) && ( configNUM_CORES > 1 ) ) && 1
780 /* Yield all currently running non-idle tasks with a priority lower than
781 * the task that needs to run. */
782 if( ( ( BaseType_t ) tskIDLE_PRIORITY - 1 < xTaskPriority ) && ( xTaskPriority < ( BaseType_t ) pxTCB->uxPriority ) )
789 mtCOVERAGE_TEST_MARKER();
792 #endif /* if ( ( configRUN_MULTIPLE_PRIORITIES == 0 ) && ( configNUM_CORES > 1 ) ) && 1 */
796 mtCOVERAGE_TEST_MARKER();
800 if( ( xYieldCount == 0 ) && taskVALID_CORE_ID( xLowestPriorityCore ) )
802 prvYieldCore( xLowestPriorityCore );
806 #if ( ( configRUN_MULTIPLE_PRIORITIES == 0 ) && ( configNUM_CORES > 1 ) )
807 /* Verify that the calling core always yields to higher priority tasks */
808 if( !pxCurrentTCBs[ portGET_CORE_ID() ]->xIsIdle && ( pxTCB->uxPriority > pxCurrentTCBs[ portGET_CORE_ID() ]->uxPriority ) )
810 configASSERT( xYieldPendings[ portGET_CORE_ID() ] == pdTRUE || taskTASK_IS_RUNNING( pxCurrentTCBs[ portGET_CORE_ID() ]->xTaskRunState ) == pdFALSE );
814 /*-----------------------------------------------------------*/
816 #if ( configUSE_PORT_OPTIMISED_TASK_SELECTION == 0 )
818 static BaseType_t prvSelectHighestPriorityTask( const BaseType_t xCoreID )
820 UBaseType_t uxCurrentPriority = uxTopReadyPriority;
821 BaseType_t xTaskScheduled = pdFALSE;
822 BaseType_t xDecrementTopPriority = pdTRUE;
824 #if ( configUSE_CORE_EXCLUSION == 1 )
825 TCB_t * pxPreviousTCB = NULL;
827 #if ( ( configRUN_MULTIPLE_PRIORITIES == 0 ) && ( configNUM_CORES > 1 ) )
828 BaseType_t xPriorityDropped = pdFALSE;
831 while( xTaskScheduled == pdFALSE )
833 #if ( ( configRUN_MULTIPLE_PRIORITIES == 0 ) && ( configNUM_CORES > 1 ) )
835 if( uxCurrentPriority < uxTopReadyPriority )
837 /* We can't schedule any tasks, other than idle, that have a
838 * priority lower than the priority of a task currently running
839 * on another core. */
840 uxCurrentPriority = tskIDLE_PRIORITY;
845 if( listLIST_IS_EMPTY( &( pxReadyTasksLists[ uxCurrentPriority ] ) ) == pdFALSE )
847 List_t * const pxReadyList = &( pxReadyTasksLists[ uxCurrentPriority ] );
848 ListItem_t * pxLastTaskItem = pxReadyList->pxIndex->pxPrevious;
849 ListItem_t * pxTaskItem = pxLastTaskItem;
851 if( ( void * ) pxLastTaskItem == ( void * ) &( pxReadyList->xListEnd ) )
853 pxLastTaskItem = pxLastTaskItem->pxPrevious;
856 /* The ready task list for uxCurrentPriority is not empty, so uxTopReadyPriority
857 * must not be decremented any further */
858 xDecrementTopPriority = pdFALSE;
864 pxTaskItem = pxTaskItem->pxNext;
866 if( ( void * ) pxTaskItem == ( void * ) &( pxReadyList->xListEnd ) )
868 pxTaskItem = pxTaskItem->pxNext;
871 pxTCB = pxTaskItem->pvOwner;
873 /*debug_printf("Attempting to schedule %s on core %d\n", pxTCB->pcTaskName, portGET_CORE_ID() ); */
875 #if ( ( configRUN_MULTIPLE_PRIORITIES == 0 ) && ( configNUM_CORES > 1 ) )
877 /* When falling back to the idle priority because only one priority
878 * level is allowed to run at a time, we should ONLY schedule the true
879 * idle tasks, not user tasks at the idle priority. */
880 if( uxCurrentPriority < uxTopReadyPriority )
882 if( pxTCB->xIsIdle == pdFALSE )
888 #endif /* if ( ( configRUN_MULTIPLE_PRIORITIES == 0 ) && ( configNUM_CORES > 1 ) ) */
890 if( pxTCB->xTaskRunState == taskTASK_NOT_RUNNING )
892 #if ( configUSE_CORE_EXCLUSION == 1 )
893 if( ( pxTCB->uxCoreExclude & ( 1 << xCoreID ) ) == 0 )
896 /* If the task is not being executed by any core swap it in */
897 pxCurrentTCBs[ xCoreID ]->xTaskRunState = taskTASK_NOT_RUNNING;
898 #if ( configUSE_CORE_EXCLUSION == 1 )
899 pxPreviousTCB = pxCurrentTCBs[ xCoreID ];
901 pxTCB->xTaskRunState = ( TaskRunning_t ) xCoreID;
902 pxCurrentTCBs[ xCoreID ] = pxTCB;
903 xTaskScheduled = pdTRUE;
906 else if( pxTCB == pxCurrentTCBs[ xCoreID ] )
908 configASSERT( ( pxTCB->xTaskRunState == xCoreID ) || ( pxTCB->xTaskRunState == taskTASK_YIELDING ) );
909 #if ( configUSE_CORE_EXCLUSION == 1 )
910 if( ( pxTCB->uxCoreExclude & ( 1 << xCoreID ) ) == 0 )
913 /* The task is already running on this core, mark it as scheduled */
914 pxTCB->xTaskRunState = ( TaskRunning_t ) xCoreID;
915 xTaskScheduled = pdTRUE;
919 if( xTaskScheduled != pdFALSE )
921 /* Once a task has been selected to run on this core,
922 * move it to the end of the ready task list. */
923 uxListRemove( pxTaskItem );
924 vListInsertEnd( pxReadyList, pxTaskItem );
927 } while( pxTaskItem != pxLastTaskItem );
931 if( xDecrementTopPriority != pdFALSE )
933 uxTopReadyPriority--;
934 #if ( ( configRUN_MULTIPLE_PRIORITIES == 0 ) && ( configNUM_CORES > 1 ) )
936 xPriorityDropped = pdTRUE;
942 /* This function can get called by vTaskSuspend() before the scheduler is started.
943 * In that case, since the idle tasks have not yet been created it is possible that we
944 * won't find a new task to schedule. Return pdFALSE in this case. */
945 if( ( xSchedulerRunning == pdFALSE ) && ( uxCurrentPriority == tskIDLE_PRIORITY ) && ( xTaskScheduled == pdFALSE ) )
950 configASSERT( ( uxCurrentPriority > tskIDLE_PRIORITY ) || ( xTaskScheduled == pdTRUE ) );
954 configASSERT( taskTASK_IS_RUNNING( pxCurrentTCBs[ xCoreID ]->xTaskRunState ) );
956 #if ( ( configRUN_MULTIPLE_PRIORITIES == 0 ) && ( configNUM_CORES > 1 ) )
957 if( xPriorityDropped != pdFALSE )
959 /* There may be several ready tasks that were being prevented from running because there was
960 * a higher priority task running. Now that the last of the higher priority tasks is no longer
961 * running, make sure all the other idle tasks yield. */
964 for( x = ( BaseType_t ) 0; x < ( BaseType_t ) configNUM_CORES; x++ )
966 if( pxCurrentTCBs[ x ]->xIsIdle != pdFALSE )
972 #endif /* if ( ( configRUN_MULTIPLE_PRIORITIES == 0 ) && ( configNUM_CORES > 1 ) ) */
974 #if ( configUSE_CORE_EXCLUSION == 1 )
975 if( ( pxPreviousTCB != NULL ) && ( listIS_CONTAINED_WITHIN( &( pxReadyTasksLists[ pxPreviousTCB->uxPriority ] ), &( pxPreviousTCB->xStateListItem ) ) != pdFALSE ) )
977 /* A ready task was just bumped off this core. Look at the cores it is not excluded
978 * from to see if it is able to run on any of them */
979 UBaseType_t uxCoreMap = ~( pxPreviousTCB->uxCoreExclude );
980 BaseType_t xLowestPriority = pxPreviousTCB->uxPriority - pxPreviousTCB->xIsIdle;
981 BaseType_t xLowestPriorityCore = -1;
983 if( ( uxCoreMap & ( 1 << xCoreID ) ) != 0 )
985 /* The ready task that was removed from this core is not excluded from it.
986 * Only look at the intersection of the cores the removed task is allowed to run
987 * on with the cores that the new task is excluded from. It is possible that the
988 * new task was only placed onto this core because it is excluded from another.
989 * Check to see if the previous task could run on one of those cores. */
990 uxCoreMap &= pxCurrentTCBs[ xCoreID ]->uxCoreExclude;
994 /* The ready task that was removed from this core is excluded from it.
995 * See if we can schedule it on any of the cores where it is not excluded from. */
998 uxCoreMap &= ( ( 1 << configNUM_CORES ) - 1 );
1000 while( uxCoreMap != 0 )
1002 int uxCore = 31UL - ( uint32_t ) __builtin_clz( uxCoreMap );
1004 xassert( taskVALID_CORE_ID( uxCore ) );
1006 uxCoreMap &= ~( 1 << uxCore );
1008 BaseType_t xTaskPriority = ( BaseType_t ) pxCurrentTCBs[ uxCore ]->uxPriority - pxCurrentTCBs[ uxCore ]->xIsIdle;
1010 if( ( xTaskPriority < xLowestPriority ) && ( taskTASK_IS_RUNNING( pxCurrentTCBs[ uxCore ]->xTaskRunState ) != pdFALSE ) && ( xYieldPendings[ uxCore ] == pdFALSE ) )
1012 #if ( configUSE_TASK_PREEMPTION_DISABLE == 1 )
1013 if( pxCurrentTCBs[ uxCore ]->xPreemptionDisable == pdFALSE )
1016 xLowestPriority = xTaskPriority;
1017 xLowestPriorityCore = uxCore;
1022 if( taskVALID_CORE_ID( xLowestPriorityCore ) )
1024 prvYieldCore( xLowestPriorityCore );
1027 #endif /* if ( configUSE_CORE_EXCLUSION == 1 ) */
1032 #else /* configUSE_PORT_OPTIMISED_TASK_SELECTION */
1034 static void prvSelectHighestPriorityTask( BaseType_t xCoreID )
1036 UBaseType_t uxTopPriority;
1038 /* Find the highest priority list that contains ready tasks. */
1039 portGET_HIGHEST_PRIORITY( uxTopPriority, uxTopReadyPriority );
1040 configASSERT( listCURRENT_LIST_LENGTH( &( pxReadyTasksLists[ uxTopPriority ] ) ) > 0 );
1041 listGET_OWNER_OF_NEXT_ENTRY( pxCurrentTCB, &( pxReadyTasksLists[ uxTopPriority ] ) );
1044 #endif /* configUSE_PORT_OPTIMISED_TASK_SELECTION */
1045 /*-----------------------------------------------------------*/
1049 #if ( configSUPPORT_STATIC_ALLOCATION == 1 )
1051 TaskHandle_t xTaskCreateStatic( TaskFunction_t pxTaskCode,
1052 const char * const pcName, /*lint !e971 Unqualified char types are allowed for strings and single characters only. */
1053 const uint32_t ulStackDepth,
1054 void * const pvParameters,
1055 UBaseType_t uxPriority,
1056 StackType_t * const puxStackBuffer,
1057 StaticTask_t * const pxTaskBuffer )
1060 TaskHandle_t xReturn;
1062 configASSERT( puxStackBuffer != NULL );
1063 configASSERT( pxTaskBuffer != NULL );
1065 #if ( configASSERT_DEFINED == 1 )
1067 /* Sanity check that the size of the structure used to declare a
1068 * variable of type StaticTask_t equals the size of the real task
1070 volatile size_t xSize = sizeof( StaticTask_t );
1071 configASSERT( xSize == sizeof( TCB_t ) );
1072 ( void ) xSize; /* Prevent lint warning when configASSERT() is not used. */
1074 #endif /* configASSERT_DEFINED */
1076 if( ( pxTaskBuffer != NULL ) && ( puxStackBuffer != NULL ) )
1078 /* The memory used for the task's TCB and stack are passed into this
1079 * function - use them. */
1080 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. */
1081 pxNewTCB->pxStack = ( StackType_t * ) puxStackBuffer;
1083 #if ( tskSTATIC_AND_DYNAMIC_ALLOCATION_POSSIBLE != 0 ) /*lint !e731 !e9029 Macro has been consolidated for readability reasons. */
1085 /* Tasks can be created statically or dynamically, so note this
1086 * task was created statically in case the task is later deleted. */
1087 pxNewTCB->ucStaticallyAllocated = tskSTATICALLY_ALLOCATED_STACK_AND_TCB;
1089 #endif /* tskSTATIC_AND_DYNAMIC_ALLOCATION_POSSIBLE */
1091 prvInitialiseNewTask( pxTaskCode, pcName, ulStackDepth, pvParameters, uxPriority, &xReturn, pxNewTCB, NULL );
1092 prvAddNewTaskToReadyList( pxNewTCB );
1102 #endif /* SUPPORT_STATIC_ALLOCATION */
1103 /*-----------------------------------------------------------*/
1105 #if ( ( portUSING_MPU_WRAPPERS == 1 ) && ( configSUPPORT_STATIC_ALLOCATION == 1 ) )
1107 BaseType_t xTaskCreateRestrictedStatic( const TaskParameters_t * const pxTaskDefinition,
1108 TaskHandle_t * pxCreatedTask )
1111 BaseType_t xReturn = errCOULD_NOT_ALLOCATE_REQUIRED_MEMORY;
1113 configASSERT( pxTaskDefinition->puxStackBuffer != NULL );
1114 configASSERT( pxTaskDefinition->pxTaskBuffer != NULL );
1116 if( ( pxTaskDefinition->puxStackBuffer != NULL ) && ( pxTaskDefinition->pxTaskBuffer != NULL ) )
1118 /* Allocate space for the TCB. Where the memory comes from depends
1119 * on the implementation of the port malloc function and whether or
1120 * not static allocation is being used. */
1121 pxNewTCB = ( TCB_t * ) pxTaskDefinition->pxTaskBuffer;
1123 /* Store the stack location in the TCB. */
1124 pxNewTCB->pxStack = pxTaskDefinition->puxStackBuffer;
1126 #if ( tskSTATIC_AND_DYNAMIC_ALLOCATION_POSSIBLE != 0 )
1128 /* Tasks can be created statically or dynamically, so note this
1129 * task was created statically in case the task is later deleted. */
1130 pxNewTCB->ucStaticallyAllocated = tskSTATICALLY_ALLOCATED_STACK_AND_TCB;
1132 #endif /* tskSTATIC_AND_DYNAMIC_ALLOCATION_POSSIBLE */
1134 prvInitialiseNewTask( pxTaskDefinition->pvTaskCode,
1135 pxTaskDefinition->pcName,
1136 ( uint32_t ) pxTaskDefinition->usStackDepth,
1137 pxTaskDefinition->pvParameters,
1138 pxTaskDefinition->uxPriority,
1139 pxCreatedTask, pxNewTCB,
1140 pxTaskDefinition->xRegions );
1142 prvAddNewTaskToReadyList( pxNewTCB );
1149 #endif /* ( portUSING_MPU_WRAPPERS == 1 ) && ( configSUPPORT_STATIC_ALLOCATION == 1 ) */
1150 /*-----------------------------------------------------------*/
1152 #if ( ( portUSING_MPU_WRAPPERS == 1 ) && ( configSUPPORT_DYNAMIC_ALLOCATION == 1 ) )
1154 BaseType_t xTaskCreateRestricted( const TaskParameters_t * const pxTaskDefinition,
1155 TaskHandle_t * pxCreatedTask )
1158 BaseType_t xReturn = errCOULD_NOT_ALLOCATE_REQUIRED_MEMORY;
1160 configASSERT( pxTaskDefinition->puxStackBuffer );
1162 if( pxTaskDefinition->puxStackBuffer != NULL )
1164 /* Allocate space for the TCB. Where the memory comes from depends
1165 * on the implementation of the port malloc function and whether or
1166 * not static allocation is being used. */
1167 pxNewTCB = ( TCB_t * ) pvPortMalloc( sizeof( TCB_t ) );
1169 if( pxNewTCB != NULL )
1171 /* Store the stack location in the TCB. */
1172 pxNewTCB->pxStack = pxTaskDefinition->puxStackBuffer;
1174 #if ( tskSTATIC_AND_DYNAMIC_ALLOCATION_POSSIBLE != 0 )
1176 /* Tasks can be created statically or dynamically, so note
1177 * this task had a statically allocated stack in case it is
1178 * later deleted. The TCB was allocated dynamically. */
1179 pxNewTCB->ucStaticallyAllocated = tskSTATICALLY_ALLOCATED_STACK_ONLY;
1181 #endif /* tskSTATIC_AND_DYNAMIC_ALLOCATION_POSSIBLE */
1183 prvInitialiseNewTask( pxTaskDefinition->pvTaskCode,
1184 pxTaskDefinition->pcName,
1185 ( uint32_t ) pxTaskDefinition->usStackDepth,
1186 pxTaskDefinition->pvParameters,
1187 pxTaskDefinition->uxPriority,
1188 pxCreatedTask, pxNewTCB,
1189 pxTaskDefinition->xRegions );
1191 prvAddNewTaskToReadyList( pxNewTCB );
1199 #endif /* portUSING_MPU_WRAPPERS */
1200 /*-----------------------------------------------------------*/
1202 #if ( configSUPPORT_DYNAMIC_ALLOCATION == 1 )
1204 BaseType_t xTaskCreate( TaskFunction_t pxTaskCode,
1205 const char * const pcName, /*lint !e971 Unqualified char types are allowed for strings and single characters only. */
1206 const configSTACK_DEPTH_TYPE usStackDepth,
1207 void * const pvParameters,
1208 UBaseType_t uxPriority,
1209 TaskHandle_t * const pxCreatedTask )
1214 /* If the stack grows down then allocate the stack then the TCB so the stack
1215 * does not grow into the TCB. Likewise if the stack grows up then allocate
1216 * the TCB then the stack. */
1217 #if ( portSTACK_GROWTH > 0 )
1219 /* Allocate space for the TCB. Where the memory comes from depends on
1220 * the implementation of the port malloc function and whether or not static
1221 * allocation is being used. */
1222 pxNewTCB = ( TCB_t * ) pvPortMalloc( sizeof( TCB_t ) );
1224 if( pxNewTCB != NULL )
1226 /* Allocate space for the stack used by the task being created.
1227 * The base of the stack memory stored in the TCB so the task can
1228 * be deleted later if required. */
1229 pxNewTCB->pxStack = ( StackType_t * ) pvPortMallocStack( ( ( ( size_t ) usStackDepth ) * sizeof( StackType_t ) ) ); /*lint !e961 MISRA exception as the casts are only redundant for some ports. */
1231 if( pxNewTCB->pxStack == NULL )
1233 /* Could not allocate the stack. Delete the allocated TCB. */
1234 vPortFree( pxNewTCB );
1239 #else /* portSTACK_GROWTH */
1241 StackType_t * pxStack;
1243 /* Allocate space for the stack used by the task being created. */
1244 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. */
1246 if( pxStack != NULL )
1248 /* Allocate space for the TCB. */
1249 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. */
1251 if( pxNewTCB != NULL )
1253 /* Store the stack location in the TCB. */
1254 pxNewTCB->pxStack = pxStack;
1258 /* The stack cannot be used as the TCB was not created. Free
1260 vPortFreeStack( pxStack );
1268 #endif /* portSTACK_GROWTH */
1270 if( pxNewTCB != NULL )
1272 #if ( tskSTATIC_AND_DYNAMIC_ALLOCATION_POSSIBLE != 0 ) /*lint !e9029 !e731 Macro has been consolidated for readability reasons. */
1274 /* Tasks can be created statically or dynamically, so note this
1275 * task was created dynamically in case it is later deleted. */
1276 pxNewTCB->ucStaticallyAllocated = tskDYNAMICALLY_ALLOCATED_STACK_AND_TCB;
1278 #endif /* tskSTATIC_AND_DYNAMIC_ALLOCATION_POSSIBLE */
1280 prvInitialiseNewTask( pxTaskCode, pcName, ( uint32_t ) usStackDepth, pvParameters, uxPriority, pxCreatedTask, pxNewTCB, NULL );
1281 prvAddNewTaskToReadyList( pxNewTCB );
1286 xReturn = errCOULD_NOT_ALLOCATE_REQUIRED_MEMORY;
1292 #endif /* configSUPPORT_DYNAMIC_ALLOCATION */
1293 /*-----------------------------------------------------------*/
1295 static void prvInitialiseNewTask( TaskFunction_t pxTaskCode,
1296 const char * const pcName, /*lint !e971 Unqualified char types are allowed for strings and single characters only. */
1297 const uint32_t ulStackDepth,
1298 void * const pvParameters,
1299 UBaseType_t uxPriority,
1300 TaskHandle_t * const pxCreatedTask,
1302 const MemoryRegion_t * const xRegions )
1304 StackType_t * pxTopOfStack;
1307 #if ( portUSING_MPU_WRAPPERS == 1 )
1308 /* Should the task be created in privileged mode? */
1309 BaseType_t xRunPrivileged;
1311 if( ( uxPriority & portPRIVILEGE_BIT ) != 0U )
1313 xRunPrivileged = pdTRUE;
1317 xRunPrivileged = pdFALSE;
1319 uxPriority &= ~portPRIVILEGE_BIT;
1320 #endif /* portUSING_MPU_WRAPPERS == 1 */
1322 /* Avoid dependency on memset() if it is not required. */
1323 #if ( tskSET_NEW_STACKS_TO_KNOWN_VALUE == 1 )
1325 /* Fill the stack with a known value to assist debugging. */
1326 ( void ) memset( pxNewTCB->pxStack, ( int ) tskSTACK_FILL_BYTE, ( size_t ) ulStackDepth * sizeof( StackType_t ) );
1328 #endif /* tskSET_NEW_STACKS_TO_KNOWN_VALUE */
1330 /* Calculate the top of stack address. This depends on whether the stack
1331 * grows from high memory to low (as per the 80x86) or vice versa.
1332 * portSTACK_GROWTH is used to make the result positive or negative as required
1334 #if ( portSTACK_GROWTH < 0 )
1336 pxTopOfStack = &( pxNewTCB->pxStack[ ulStackDepth - ( uint32_t ) 1 ] );
1337 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(). */
1339 /* Check the alignment of the calculated top of stack is correct. */
1340 configASSERT( ( ( ( portPOINTER_SIZE_TYPE ) pxTopOfStack & ( portPOINTER_SIZE_TYPE ) portBYTE_ALIGNMENT_MASK ) == 0UL ) );
1342 #if ( configRECORD_STACK_HIGH_ADDRESS == 1 )
1344 /* Also record the stack's high address, which may assist
1346 pxNewTCB->pxEndOfStack = pxTopOfStack;
1348 #endif /* configRECORD_STACK_HIGH_ADDRESS */
1350 #else /* portSTACK_GROWTH */
1352 pxTopOfStack = pxNewTCB->pxStack;
1354 /* Check the alignment of the stack buffer is correct. */
1355 configASSERT( ( ( ( portPOINTER_SIZE_TYPE ) pxNewTCB->pxStack & ( portPOINTER_SIZE_TYPE ) portBYTE_ALIGNMENT_MASK ) == 0UL ) );
1357 /* The other extreme of the stack space is required if stack checking is
1359 pxNewTCB->pxEndOfStack = pxNewTCB->pxStack + ( ulStackDepth - ( uint32_t ) 1 );
1361 #endif /* portSTACK_GROWTH */
1363 /* Store the task name in the TCB. */
1364 if( pcName != NULL )
1366 for( x = ( UBaseType_t ) 0; x < ( UBaseType_t ) configMAX_TASK_NAME_LEN; x++ )
1368 pxNewTCB->pcTaskName[ x ] = pcName[ x ];
1370 /* Don't copy all configMAX_TASK_NAME_LEN if the string is shorter than
1371 * configMAX_TASK_NAME_LEN characters just in case the memory after the
1372 * string is not accessible (extremely unlikely). */
1373 if( pcName[ x ] == ( char ) 0x00 )
1379 mtCOVERAGE_TEST_MARKER();
1383 /* Ensure the name string is terminated in the case that the string length
1384 * was greater or equal to configMAX_TASK_NAME_LEN. */
1385 pxNewTCB->pcTaskName[ configMAX_TASK_NAME_LEN - 1 ] = '\0';
1389 /* The task has not been given a name, so just ensure there is a NULL
1390 * terminator when it is read out. */
1391 pxNewTCB->pcTaskName[ 0 ] = 0x00;
1394 /* This is used as an array index so must ensure it's not too large. First
1395 * remove the privilege bit if one is present. */
1396 if( uxPriority >= ( UBaseType_t ) configMAX_PRIORITIES )
1398 uxPriority = ( UBaseType_t ) configMAX_PRIORITIES - ( UBaseType_t ) 1U;
1402 mtCOVERAGE_TEST_MARKER();
1405 pxNewTCB->uxPriority = uxPriority;
1406 #if ( configUSE_MUTEXES == 1 )
1408 pxNewTCB->uxBasePriority = uxPriority;
1409 pxNewTCB->uxMutexesHeld = 0;
1411 #endif /* configUSE_MUTEXES */
1413 vListInitialiseItem( &( pxNewTCB->xStateListItem ) );
1414 vListInitialiseItem( &( pxNewTCB->xEventListItem ) );
1416 /* Set the pxNewTCB as a link back from the ListItem_t. This is so we can get
1417 * back to the containing TCB from a generic item in a list. */
1418 listSET_LIST_ITEM_OWNER( &( pxNewTCB->xStateListItem ), pxNewTCB );
1420 /* Event lists are always in priority order. */
1421 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. */
1422 listSET_LIST_ITEM_OWNER( &( pxNewTCB->xEventListItem ), pxNewTCB );
1424 #if ( portCRITICAL_NESTING_IN_TCB == 1 )
1426 pxNewTCB->uxCriticalNesting = ( UBaseType_t ) 0U;
1428 #endif /* portCRITICAL_NESTING_IN_TCB */
1430 #if ( configUSE_APPLICATION_TASK_TAG == 1 )
1432 pxNewTCB->pxTaskTag = NULL;
1434 #endif /* configUSE_APPLICATION_TASK_TAG */
1436 #if ( configGENERATE_RUN_TIME_STATS == 1 )
1438 pxNewTCB->ulRunTimeCounter = 0UL;
1440 #endif /* configGENERATE_RUN_TIME_STATS */
1442 #if ( portUSING_MPU_WRAPPERS == 1 )
1444 vPortStoreTaskMPUSettings( &( pxNewTCB->xMPUSettings ), xRegions, pxNewTCB->pxStack, ulStackDepth );
1448 /* Avoid compiler warning about unreferenced parameter. */
1453 #if ( configNUM_THREAD_LOCAL_STORAGE_POINTERS != 0 )
1455 memset( ( void * ) &( pxNewTCB->pvThreadLocalStoragePointers[ 0 ] ), 0x00, sizeof( pxNewTCB->pvThreadLocalStoragePointers ) );
1459 #if ( configUSE_TASK_NOTIFICATIONS == 1 )
1461 memset( ( void * ) &( pxNewTCB->ulNotifiedValue[ 0 ] ), 0x00, sizeof( pxNewTCB->ulNotifiedValue ) );
1462 memset( ( void * ) &( pxNewTCB->ucNotifyState[ 0 ] ), 0x00, sizeof( pxNewTCB->ucNotifyState ) );
1466 #if ( configUSE_NEWLIB_REENTRANT == 1 )
1468 /* Initialise this task's Newlib reent structure.
1469 * See the third party link http://www.nadler.com/embedded/newlibAndFreeRTOS.html
1470 * for additional information. */
1471 _REENT_INIT_PTR( ( &( pxNewTCB->xNewLib_reent ) ) );
1475 #if ( INCLUDE_xTaskAbortDelay == 1 )
1477 pxNewTCB->ucDelayAborted = pdFALSE;
1481 #if ( configUSE_CORE_EXCLUSION == 1 )
1483 pxNewTCB->uxCoreExclude = 0;
1486 #if ( configUSE_TASK_PREEMPTION_DISABLE == 1 )
1488 pxNewTCB->xPreemptionDisable = 0;
1492 /* Initialize the TCB stack to look as if the task was already running,
1493 * but had been interrupted by the scheduler. The return address is set
1494 * to the start of the task function. Once the stack has been initialised
1495 * the top of stack variable is updated. */
1496 #if ( portUSING_MPU_WRAPPERS == 1 )
1498 /* If the port has capability to detect stack overflow,
1499 * pass the stack end address to the stack initialization
1500 * function as well. */
1501 #if ( portHAS_STACK_OVERFLOW_CHECKING == 1 )
1503 #if ( portSTACK_GROWTH < 0 )
1505 pxNewTCB->pxTopOfStack = pxPortInitialiseStack( pxTopOfStack, pxNewTCB->pxStack, pxTaskCode, pvParameters, xRunPrivileged );
1507 #else /* portSTACK_GROWTH */
1509 pxNewTCB->pxTopOfStack = pxPortInitialiseStack( pxTopOfStack, pxNewTCB->pxEndOfStack, pxTaskCode, pvParameters, xRunPrivileged );
1511 #endif /* portSTACK_GROWTH */
1513 #else /* portHAS_STACK_OVERFLOW_CHECKING */
1515 pxNewTCB->pxTopOfStack = pxPortInitialiseStack( pxTopOfStack, pxTaskCode, pvParameters, xRunPrivileged );
1517 #endif /* portHAS_STACK_OVERFLOW_CHECKING */
1519 #else /* portUSING_MPU_WRAPPERS */
1521 /* If the port has capability to detect stack overflow,
1522 * pass the stack end address to the stack initialization
1523 * function as well. */
1524 #if ( portHAS_STACK_OVERFLOW_CHECKING == 1 )
1526 #if ( portSTACK_GROWTH < 0 )
1528 pxNewTCB->pxTopOfStack = pxPortInitialiseStack( pxTopOfStack, pxNewTCB->pxStack, pxTaskCode, pvParameters );
1530 #else /* portSTACK_GROWTH */
1532 pxNewTCB->pxTopOfStack = pxPortInitialiseStack( pxTopOfStack, pxNewTCB->pxEndOfStack, pxTaskCode, pvParameters );
1534 #endif /* portSTACK_GROWTH */
1536 #else /* portHAS_STACK_OVERFLOW_CHECKING */
1538 pxNewTCB->pxTopOfStack = pxPortInitialiseStack( pxTopOfStack, pxTaskCode, pvParameters );
1540 #endif /* portHAS_STACK_OVERFLOW_CHECKING */
1542 #endif /* portUSING_MPU_WRAPPERS */
1544 /* Initialize to not running */
1545 pxNewTCB->xTaskRunState = taskTASK_NOT_RUNNING;
1547 /* Is this an idle task? */
1548 pxNewTCB->xIsIdle = ( pxTaskCode == prvIdleTask );
1550 if( pxCreatedTask != NULL )
1552 /* Pass the handle out in an anonymous way. The handle can be used to
1553 * change the created task's priority, delete the created task, etc.*/
1554 *pxCreatedTask = ( TaskHandle_t ) pxNewTCB;
1558 mtCOVERAGE_TEST_MARKER();
1561 /*-----------------------------------------------------------*/
1563 static void prvAddNewTaskToReadyList( TCB_t * pxNewTCB )
1565 /* Ensure interrupts don't access the task lists while the lists are being
1567 taskENTER_CRITICAL();
1569 uxCurrentNumberOfTasks++;
1571 if( xSchedulerRunning == pdFALSE )
1573 if( uxCurrentNumberOfTasks == ( UBaseType_t ) 1 )
1575 /* This is the first task to be created so do the preliminary
1576 * initialisation required. We will not recover if this call
1577 * fails, but we will report the failure. */
1578 prvInitialiseTaskLists();
1582 mtCOVERAGE_TEST_MARKER();
1585 if( pxNewTCB->xIsIdle != pdFALSE )
1589 /* Check if a core is free. */
1590 for( xCoreID = ( UBaseType_t ) 0; xCoreID < ( UBaseType_t ) configNUM_CORES; xCoreID++ )
1592 if( pxCurrentTCBs[ xCoreID ] == NULL )
1594 pxNewTCB->xTaskRunState = xCoreID;
1595 #if ( configUSE_CORE_EXCLUSION == 1 )
1597 pxNewTCB->uxCoreExclude = ~( 1 << xCoreID );
1600 pxCurrentTCBs[ xCoreID ] = pxNewTCB;
1608 mtCOVERAGE_TEST_MARKER();
1613 #if ( configUSE_TRACE_FACILITY == 1 )
1615 /* Add a counter into the TCB for tracing only. */
1616 pxNewTCB->uxTCBNumber = uxTaskNumber;
1618 #endif /* configUSE_TRACE_FACILITY */
1619 traceTASK_CREATE( pxNewTCB );
1621 prvAddTaskToReadyList( pxNewTCB );
1623 portSETUP_TCB( pxNewTCB );
1625 if( xSchedulerRunning != pdFALSE )
1627 /* If the created task is of a higher priority than another
1628 * currently running task and preemption is on then it should
1630 #if ( configUSE_PREEMPTION == 1 )
1631 prvYieldForTask( pxNewTCB, pdFALSE );
1636 mtCOVERAGE_TEST_MARKER();
1639 taskEXIT_CRITICAL();
1641 /*-----------------------------------------------------------*/
1643 #if ( INCLUDE_vTaskDelete == 1 )
1645 void vTaskDelete( TaskHandle_t xTaskToDelete )
1648 TaskRunning_t xTaskRunningOnCore;
1650 taskENTER_CRITICAL();
1652 /* If null is passed in here then it is the calling task that is
1654 pxTCB = prvGetTCBFromHandle( xTaskToDelete );
1656 xTaskRunningOnCore = pxTCB->xTaskRunState;
1658 /* Remove task from the ready/delayed list. */
1659 if( uxListRemove( &( pxTCB->xStateListItem ) ) == ( UBaseType_t ) 0 )
1661 taskRESET_READY_PRIORITY( pxTCB->uxPriority );
1665 mtCOVERAGE_TEST_MARKER();
1668 /* Is the task waiting on an event also? */
1669 if( listLIST_ITEM_CONTAINER( &( pxTCB->xEventListItem ) ) != NULL )
1671 ( void ) uxListRemove( &( pxTCB->xEventListItem ) );
1675 mtCOVERAGE_TEST_MARKER();
1678 /* Increment the uxTaskNumber also so kernel aware debuggers can
1679 * detect that the task lists need re-generating. This is done before
1680 * portPRE_TASK_DELETE_HOOK() as in the Windows port that macro will
1684 /* If the task is running (or yielding), we must add it to the
1685 * termination list so that an idle task can delete it when it is
1686 * no longer running. */
1687 if( xTaskRunningOnCore != taskTASK_NOT_RUNNING )
1690 /* A running task is being deleted. This cannot complete within the
1691 * task itself, as a context switch to another task is required.
1692 * Place the task in the termination list. The idle task will
1693 * check the termination list and free up any memory allocated by
1694 * the scheduler for the TCB and stack of the deleted task. */
1695 vListInsertEnd( &xTasksWaitingTermination, &( pxTCB->xStateListItem ) );
1697 /* Increment the ucTasksDeleted variable so the idle task knows
1698 * there is a task that has been deleted and that it should therefore
1699 * check the xTasksWaitingTermination list. */
1700 ++uxDeletedTasksWaitingCleanUp;
1702 /* Call the delete hook before portPRE_TASK_DELETE_HOOK() as
1703 * portPRE_TASK_DELETE_HOOK() does not return in the Win32 port. */
1704 traceTASK_DELETE( pxTCB );
1706 /* The pre-delete hook is primarily for the Windows simulator,
1707 * in which Windows specific clean up operations are performed,
1708 * after which it is not possible to yield away from this task -
1709 * hence xYieldPending is used to latch that a context switch is
1711 portPRE_TASK_DELETE_HOOK( pxTCB, &xYieldPendings[ pxTCB->xTaskRunState ] );
1715 --uxCurrentNumberOfTasks;
1716 traceTASK_DELETE( pxTCB );
1717 prvDeleteTCB( pxTCB );
1719 /* Reset the next expected unblock time in case it referred to
1720 * the task that has just been deleted. */
1721 prvResetNextTaskUnblockTime();
1724 /* Force a reschedule if the task that has just been deleted was running. */
1725 if( ( xSchedulerRunning != pdFALSE ) && ( taskTASK_IS_RUNNING( xTaskRunningOnCore ) ) )
1729 xCoreID = portGET_CORE_ID();
1732 if( xTaskRunningOnCore == xCoreID )
1734 configASSERT( uxSchedulerSuspended == 0 );
1735 vTaskYieldWithinAPI();
1739 prvYieldCore( xTaskRunningOnCore );
1743 taskEXIT_CRITICAL();
1746 #endif /* INCLUDE_vTaskDelete */
1747 /*-----------------------------------------------------------*/
1749 #if ( INCLUDE_xTaskDelayUntil == 1 )
1751 BaseType_t xTaskDelayUntil( TickType_t * const pxPreviousWakeTime,
1752 const TickType_t xTimeIncrement )
1754 TickType_t xTimeToWake;
1755 BaseType_t xAlreadyYielded, xShouldDelay = pdFALSE;
1757 configASSERT( pxPreviousWakeTime );
1758 configASSERT( ( xTimeIncrement > 0U ) );
1762 configASSERT( uxSchedulerSuspended == 1 );
1764 /* Minor optimisation. The tick count cannot change in this
1766 const TickType_t xConstTickCount = xTickCount;
1768 /* Generate the tick time at which the task wants to wake. */
1769 xTimeToWake = *pxPreviousWakeTime + xTimeIncrement;
1771 if( xConstTickCount < *pxPreviousWakeTime )
1773 /* The tick count has overflowed since this function was
1774 * lasted called. In this case the only time we should ever
1775 * actually delay is if the wake time has also overflowed,
1776 * and the wake time is greater than the tick time. When this
1777 * is the case it is as if neither time had overflowed. */
1778 if( ( xTimeToWake < *pxPreviousWakeTime ) && ( xTimeToWake > xConstTickCount ) )
1780 xShouldDelay = pdTRUE;
1784 mtCOVERAGE_TEST_MARKER();
1789 /* The tick time has not overflowed. In this case we will
1790 * delay if either the wake time has overflowed, and/or the
1791 * tick time is less than the wake time. */
1792 if( ( xTimeToWake < *pxPreviousWakeTime ) || ( xTimeToWake > xConstTickCount ) )
1794 xShouldDelay = pdTRUE;
1798 mtCOVERAGE_TEST_MARKER();
1802 /* Update the wake time ready for the next call. */
1803 *pxPreviousWakeTime = xTimeToWake;
1805 if( xShouldDelay != pdFALSE )
1807 traceTASK_DELAY_UNTIL( xTimeToWake );
1809 /* prvAddCurrentTaskToDelayedList() needs the block time, not
1810 * the time to wake, so subtract the current tick count. */
1811 prvAddCurrentTaskToDelayedList( xTimeToWake - xConstTickCount, pdFALSE );
1815 mtCOVERAGE_TEST_MARKER();
1818 xAlreadyYielded = xTaskResumeAll();
1820 /* Force a reschedule if xTaskResumeAll has not already done so, we may
1821 * have put ourselves to sleep. */
1822 if( xAlreadyYielded == pdFALSE )
1824 vTaskYieldWithinAPI();
1828 mtCOVERAGE_TEST_MARKER();
1831 return xShouldDelay;
1834 #endif /* INCLUDE_xTaskDelayUntil */
1835 /*-----------------------------------------------------------*/
1837 #if ( INCLUDE_vTaskDelay == 1 )
1839 void vTaskDelay( const TickType_t xTicksToDelay )
1841 BaseType_t xAlreadyYielded = pdFALSE;
1843 /* A delay time of zero just forces a reschedule. */
1844 if( xTicksToDelay > ( TickType_t ) 0U )
1848 configASSERT( uxSchedulerSuspended == 1 );
1851 /* A task that is removed from the event list while the
1852 * scheduler is suspended will not get placed in the ready
1853 * list or removed from the blocked list until the scheduler
1856 * This task cannot be in an event list as it is the currently
1857 * executing task. */
1858 prvAddCurrentTaskToDelayedList( xTicksToDelay, pdFALSE );
1860 xAlreadyYielded = xTaskResumeAll();
1864 mtCOVERAGE_TEST_MARKER();
1867 /* Force a reschedule if xTaskResumeAll has not already done so, we may
1868 * have put ourselves to sleep. */
1869 if( xAlreadyYielded == pdFALSE )
1871 vTaskYieldWithinAPI();
1875 mtCOVERAGE_TEST_MARKER();
1879 #endif /* INCLUDE_vTaskDelay */
1880 /*-----------------------------------------------------------*/
1882 #if ( ( INCLUDE_eTaskGetState == 1 ) || ( configUSE_TRACE_FACILITY == 1 ) || ( INCLUDE_xTaskAbortDelay == 1 ) )
1884 eTaskState eTaskGetState( TaskHandle_t xTask )
1887 List_t const * pxStateList, * pxDelayedList, * pxOverflowedDelayedList;
1888 const TCB_t * const pxTCB = xTask;
1890 configASSERT( pxTCB );
1892 taskENTER_CRITICAL();
1894 pxStateList = listLIST_ITEM_CONTAINER( &( pxTCB->xStateListItem ) );
1895 pxDelayedList = pxDelayedTaskList;
1896 pxOverflowedDelayedList = pxOverflowDelayedTaskList;
1898 taskEXIT_CRITICAL();
1900 if( ( pxStateList == pxDelayedList ) || ( pxStateList == pxOverflowedDelayedList ) )
1902 /* The task being queried is referenced from one of the Blocked
1907 #if ( INCLUDE_vTaskSuspend == 1 )
1908 else if( pxStateList == &xSuspendedTaskList )
1910 /* The task being queried is referenced from the suspended
1911 * list. Is it genuinely suspended or is it blocked
1913 if( listLIST_ITEM_CONTAINER( &( pxTCB->xEventListItem ) ) == NULL )
1915 #if ( configUSE_TASK_NOTIFICATIONS == 1 )
1919 /* The task does not appear on the event list item of
1920 * and of the RTOS objects, but could still be in the
1921 * blocked state if it is waiting on its notification
1922 * rather than waiting on an object. If not, is
1924 eReturn = eSuspended;
1926 for( x = 0; x < configTASK_NOTIFICATION_ARRAY_ENTRIES; x++ )
1928 if( pxTCB->ucNotifyState[ x ] == taskWAITING_NOTIFICATION )
1935 #else /* if ( configUSE_TASK_NOTIFICATIONS == 1 ) */
1937 eReturn = eSuspended;
1939 #endif /* if ( configUSE_TASK_NOTIFICATIONS == 1 ) */
1946 #endif /* if ( INCLUDE_vTaskSuspend == 1 ) */
1948 #if ( INCLUDE_vTaskDelete == 1 )
1949 else if( ( pxStateList == &xTasksWaitingTermination ) || ( pxStateList == NULL ) )
1951 /* The task being queried is referenced from the deleted
1952 * tasks list, or it is not referenced from any lists at
1958 else /*lint !e525 Negative indentation is intended to make use of pre-processor clearer. */
1960 /* If the task is not in any other state, it must be in the
1961 * Ready (including pending ready) state. */
1962 if( taskTASK_IS_RUNNING( pxTCB->xTaskRunState ) )
1964 /* Is it actively running on a core? */
1974 } /*lint !e818 xTask cannot be a pointer to const because it is a typedef. */
1976 #endif /* INCLUDE_eTaskGetState */
1977 /*-----------------------------------------------------------*/
1979 #if ( INCLUDE_uxTaskPriorityGet == 1 )
1981 UBaseType_t uxTaskPriorityGet( const TaskHandle_t xTask )
1983 TCB_t const * pxTCB;
1984 UBaseType_t uxReturn;
1986 taskENTER_CRITICAL();
1988 /* If null is passed in here then it is the priority of the task
1989 * that called uxTaskPriorityGet() that is being queried. */
1990 pxTCB = prvGetTCBFromHandle( xTask );
1991 uxReturn = pxTCB->uxPriority;
1993 taskEXIT_CRITICAL();
1998 #endif /* INCLUDE_uxTaskPriorityGet */
1999 /*-----------------------------------------------------------*/
2001 #if ( INCLUDE_uxTaskPriorityGet == 1 )
2003 UBaseType_t uxTaskPriorityGetFromISR( const TaskHandle_t xTask )
2005 TCB_t const * pxTCB;
2006 UBaseType_t uxReturn, uxSavedInterruptState;
2008 /* RTOS ports that support interrupt nesting have the concept of a
2009 * maximum system call (or maximum API call) interrupt priority.
2010 * Interrupts that are above the maximum system call priority are keep
2011 * permanently enabled, even when the RTOS kernel is in a critical section,
2012 * but cannot make any calls to FreeRTOS API functions. If configASSERT()
2013 * is defined in FreeRTOSConfig.h then
2014 * portASSERT_IF_INTERRUPT_PRIORITY_INVALID() will result in an assertion
2015 * failure if a FreeRTOS API function is called from an interrupt that has
2016 * been assigned a priority above the configured maximum system call
2017 * priority. Only FreeRTOS functions that end in FromISR can be called
2018 * from interrupts that have been assigned a priority at or (logically)
2019 * below the maximum system call interrupt priority. FreeRTOS maintains a
2020 * separate interrupt safe API to ensure interrupt entry is as fast and as
2021 * simple as possible. More information (albeit Cortex-M specific) is
2022 * provided on the following link:
2023 * https://www.FreeRTOS.org/RTOS-Cortex-M3-M4.html */
2024 portASSERT_IF_INTERRUPT_PRIORITY_INVALID();
2026 uxSavedInterruptState = portSET_INTERRUPT_MASK_FROM_ISR();
2028 /* If null is passed in here then it is the priority of the calling
2029 * task that is being queried. */
2030 pxTCB = prvGetTCBFromHandle( xTask );
2031 uxReturn = pxTCB->uxPriority;
2033 portCLEAR_INTERRUPT_MASK_FROM_ISR( uxSavedInterruptState );
2038 #endif /* INCLUDE_uxTaskPriorityGet */
2039 /*-----------------------------------------------------------*/
2041 #if ( INCLUDE_vTaskPrioritySet == 1 )
2043 void vTaskPrioritySet( TaskHandle_t xTask,
2044 UBaseType_t uxNewPriority )
2047 UBaseType_t uxCurrentBasePriority, uxPriorityUsedOnEntry;
2048 BaseType_t xYieldRequired = pdFALSE;
2049 BaseType_t xYieldForTask = pdFALSE;
2052 configASSERT( ( uxNewPriority < configMAX_PRIORITIES ) );
2054 /* Ensure the new priority is valid. */
2055 if( uxNewPriority >= ( UBaseType_t ) configMAX_PRIORITIES )
2057 uxNewPriority = ( UBaseType_t ) configMAX_PRIORITIES - ( UBaseType_t ) 1U;
2061 mtCOVERAGE_TEST_MARKER();
2064 taskENTER_CRITICAL();
2066 /* If null is passed in here then it is the priority of the calling
2067 * task that is being changed. */
2068 pxTCB = prvGetTCBFromHandle( xTask );
2070 traceTASK_PRIORITY_SET( pxTCB, uxNewPriority );
2072 #if ( configUSE_MUTEXES == 1 )
2074 uxCurrentBasePriority = pxTCB->uxBasePriority;
2078 uxCurrentBasePriority = pxTCB->uxPriority;
2082 if( uxCurrentBasePriority != uxNewPriority )
2084 /* The priority change may have readied a task of higher
2085 * priority than a running task. */
2086 if( uxNewPriority > uxCurrentBasePriority )
2088 /* The priority of a task is being raised so
2089 * perform a yield for this task later. */
2090 xYieldForTask = pdTRUE;
2092 else if( taskTASK_IS_RUNNING( pxTCB->xTaskRunState ) )
2094 /* Setting the priority of a running task down means
2095 * there may now be another task of higher priority that
2096 * is ready to execute. */
2097 #if ( configUSE_TASK_PREEMPTION_DISABLE == 1 )
2098 if( pxTCB->xPreemptionDisable == pdFALSE )
2101 xCoreID = ( BaseType_t ) pxTCB->xTaskRunState;
2102 xYieldRequired = pdTRUE;
2107 /* Setting the priority of any other task down does not
2108 * require a yield as the running task must be above the
2109 * new priority of the task being modified. */
2112 /* Remember the ready list the task might be referenced from
2113 * before its uxPriority member is changed so the
2114 * taskRESET_READY_PRIORITY() macro can function correctly. */
2115 uxPriorityUsedOnEntry = pxTCB->uxPriority;
2117 #if ( configUSE_MUTEXES == 1 )
2119 /* Only change the priority being used if the task is not
2120 * currently using an inherited priority. */
2121 if( pxTCB->uxBasePriority == pxTCB->uxPriority )
2123 pxTCB->uxPriority = uxNewPriority;
2127 mtCOVERAGE_TEST_MARKER();
2130 /* The base priority gets set whatever. */
2131 pxTCB->uxBasePriority = uxNewPriority;
2133 #else /* if ( configUSE_MUTEXES == 1 ) */
2135 pxTCB->uxPriority = uxNewPriority;
2137 #endif /* if ( configUSE_MUTEXES == 1 ) */
2139 /* Only reset the event list item value if the value is not
2140 * being used for anything else. */
2141 if( ( listGET_LIST_ITEM_VALUE( &( pxTCB->xEventListItem ) ) & taskEVENT_LIST_ITEM_VALUE_IN_USE ) == 0UL )
2143 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. */
2147 mtCOVERAGE_TEST_MARKER();
2150 /* If the task is in the blocked or suspended list we need do
2151 * nothing more than change its priority variable. However, if
2152 * the task is in a ready list it needs to be removed and placed
2153 * in the list appropriate to its new priority. */
2154 if( listIS_CONTAINED_WITHIN( &( pxReadyTasksLists[ uxPriorityUsedOnEntry ] ), &( pxTCB->xStateListItem ) ) != pdFALSE )
2156 /* The task is currently in its ready list - remove before
2157 * adding it to its new ready list. As we are in a critical
2158 * section we can do this even if the scheduler is suspended. */
2159 if( uxListRemove( &( pxTCB->xStateListItem ) ) == ( UBaseType_t ) 0 )
2161 /* It is known that the task is in its ready list so
2162 * there is no need to check again and the port level
2163 * reset macro can be called directly. */
2164 portRESET_READY_PRIORITY( uxPriorityUsedOnEntry, uxTopReadyPriority );
2168 mtCOVERAGE_TEST_MARKER();
2171 prvAddTaskToReadyList( pxTCB );
2175 /* It's possible that xYieldForTask was already set to pdTRUE because
2176 * its priority is being raised. However, since it is not in a ready list
2177 * we don't actually need to yield for it. */
2178 xYieldForTask = pdFALSE;
2181 #if ( configUSE_PREEMPTION == 1 )
2182 if( xYieldRequired != pdFALSE )
2184 prvYieldCore( xCoreID );
2186 else if( xYieldForTask != pdFALSE )
2188 prvYieldForTask( pxTCB, pdTRUE );
2192 mtCOVERAGE_TEST_MARKER();
2194 #endif /* if ( configUSE_PREEMPTION == 1 ) */
2196 /* Remove compiler warning about unused variables when the port
2197 * optimised task selection is not being used. */
2198 ( void ) uxPriorityUsedOnEntry;
2201 taskEXIT_CRITICAL();
2204 #endif /* INCLUDE_vTaskPrioritySet */
2205 /*-----------------------------------------------------------*/
2207 #if ( configUSE_CORE_EXCLUSION == 1 )
2209 void vTaskCoreExclusionSet( const TaskHandle_t xTask,
2210 UBaseType_t uxCoreExclude )
2215 taskENTER_CRITICAL();
2217 pxTCB = prvGetTCBFromHandle( xTask );
2219 pxTCB->uxCoreExclude = uxCoreExclude;
2221 if( xSchedulerRunning != pdFALSE )
2223 if( taskTASK_IS_RUNNING( pxTCB->xTaskRunState ) )
2225 xCoreID = ( BaseType_t ) pxTCB->xTaskRunState;
2227 if( ( uxCoreExclude & ( 1 << xCoreID ) ) != 0 )
2229 prvYieldCore( xCoreID );
2234 taskEXIT_CRITICAL();
2237 #endif /* configUSE_CORE_EXCLUSION */
2238 /*-----------------------------------------------------------*/
2240 #if ( configUSE_CORE_EXCLUSION == 1 )
2242 UBaseType_t vTaskCoreExclusionGet( const TaskHandle_t xTask )
2245 UBaseType_t uxCoreExclude;
2247 taskENTER_CRITICAL();
2249 pxTCB = prvGetTCBFromHandle( xTask );
2250 uxCoreExclude = pxTCB->uxCoreExclude;
2252 taskEXIT_CRITICAL();
2254 return uxCoreExclude;
2257 #endif /* configUSE_CORE_EXCLUSION */
2258 /*-----------------------------------------------------------*/
2260 #if ( configUSE_TASK_PREEMPTION_DISABLE == 1 )
2262 void vTaskPreemptionDisable( const TaskHandle_t xTask )
2266 taskENTER_CRITICAL();
2268 pxTCB = prvGetTCBFromHandle( xTask );
2270 pxTCB->xPreemptionDisable = pdTRUE;
2272 taskEXIT_CRITICAL();
2275 #endif /* configUSE_TASK_PREEMPTION_DISABLE */
2276 /*-----------------------------------------------------------*/
2278 #if ( configUSE_TASK_PREEMPTION_DISABLE == 1 )
2280 void vTaskPreemptionEnable( const TaskHandle_t xTask )
2285 taskENTER_CRITICAL();
2287 pxTCB = prvGetTCBFromHandle( xTask );
2289 pxTCB->xPreemptionDisable = pdFALSE;
2291 if( xSchedulerRunning != pdFALSE )
2293 if( taskTASK_IS_RUNNING( pxTCB->xTaskRunState ) )
2295 xCoreID = ( BaseType_t ) pxTCB->xTaskRunState;
2296 prvYieldCore( xCoreID );
2300 taskEXIT_CRITICAL();
2303 #endif /* configUSE_TASK_PREEMPTION_DISABLE */
2304 /*-----------------------------------------------------------*/
2306 #if ( INCLUDE_vTaskSuspend == 1 )
2308 void vTaskSuspend( TaskHandle_t xTaskToSuspend )
2311 TaskRunning_t xTaskRunningOnCore;
2313 taskENTER_CRITICAL();
2315 /* If null is passed in here then it is the running task that is
2316 * being suspended. */
2317 pxTCB = prvGetTCBFromHandle( xTaskToSuspend );
2319 traceTASK_SUSPEND( pxTCB );
2321 xTaskRunningOnCore = pxTCB->xTaskRunState;
2323 /* Remove task from the ready/delayed list and place in the
2324 * suspended list. */
2325 if( uxListRemove( &( pxTCB->xStateListItem ) ) == ( UBaseType_t ) 0 )
2327 taskRESET_READY_PRIORITY( pxTCB->uxPriority );
2331 mtCOVERAGE_TEST_MARKER();
2334 /* Is the task waiting on an event also? */
2335 if( listLIST_ITEM_CONTAINER( &( pxTCB->xEventListItem ) ) != NULL )
2337 ( void ) uxListRemove( &( pxTCB->xEventListItem ) );
2341 mtCOVERAGE_TEST_MARKER();
2344 vListInsertEnd( &xSuspendedTaskList, &( pxTCB->xStateListItem ) );
2346 #if ( configUSE_TASK_NOTIFICATIONS == 1 )
2350 for( x = 0; x < configTASK_NOTIFICATION_ARRAY_ENTRIES; x++ )
2352 if( pxTCB->ucNotifyState[ x ] == taskWAITING_NOTIFICATION )
2354 /* The task was blocked to wait for a notification, but is
2355 * now suspended, so no notification was received. */
2356 pxTCB->ucNotifyState[ x ] = taskNOT_WAITING_NOTIFICATION;
2360 #endif /* if ( configUSE_TASK_NOTIFICATIONS == 1 ) */
2362 if( xSchedulerRunning != pdFALSE )
2364 /* Reset the next expected unblock time in case it referred to the
2365 * task that is now in the Suspended state. */
2366 prvResetNextTaskUnblockTime();
2370 mtCOVERAGE_TEST_MARKER();
2373 if( taskTASK_IS_RUNNING( xTaskRunningOnCore ) )
2375 if( xSchedulerRunning != pdFALSE )
2377 if( xTaskRunningOnCore == portGET_CORE_ID() )
2379 /* The current task has just been suspended. */
2380 configASSERT( uxSchedulerSuspended == 0 );
2381 vTaskYieldWithinAPI();
2385 prvYieldCore( xTaskRunningOnCore );
2388 taskEXIT_CRITICAL();
2392 taskEXIT_CRITICAL();
2394 configASSERT( pxTCB == pxCurrentTCBs[ xTaskRunningOnCore ] );
2396 /* The scheduler is not running, but the task that was pointed
2397 * to by pxCurrentTCB has just been suspended and pxCurrentTCB
2398 * must be adjusted to point to a different task. */
2399 if( listCURRENT_LIST_LENGTH( &xSuspendedTaskList ) == uxCurrentNumberOfTasks ) /*lint !e931 Right has no side effect, just volatile. */
2401 /* No other tasks are ready, so set the core's TCB back to
2402 * NULL so when the next task is created the core's TCB will
2403 * be able to be set to point to it no matter what its relative
2405 pxTCB->xTaskRunState = taskTASK_NOT_RUNNING;
2406 pxCurrentTCBs[ xTaskRunningOnCore ] = NULL;
2410 /* Attempt to switch in a new task. This could fail since the idle tasks
2411 * haven't been created yet. If it does then set the core's TCB back to
2413 if( prvSelectHighestPriorityTask( xTaskRunningOnCore ) == pdFALSE )
2415 pxTCB->xTaskRunState = taskTASK_NOT_RUNNING;
2416 pxCurrentTCBs[ xTaskRunningOnCore ] = NULL;
2423 taskEXIT_CRITICAL();
2425 } /* taskEXIT_CRITICAL() - already exited in one of three cases above */
2428 #endif /* INCLUDE_vTaskSuspend */
2429 /*-----------------------------------------------------------*/
2431 #if ( INCLUDE_vTaskSuspend == 1 )
2433 static BaseType_t prvTaskIsTaskSuspended( const TaskHandle_t xTask )
2435 BaseType_t xReturn = pdFALSE;
2436 const TCB_t * const pxTCB = xTask;
2438 /* Accesses xPendingReadyList so must be called from a critical
2441 /* It does not make sense to check if the calling task is suspended. */
2442 configASSERT( xTask );
2444 /* Is the task being resumed actually in the suspended list? */
2445 if( listIS_CONTAINED_WITHIN( &xSuspendedTaskList, &( pxTCB->xStateListItem ) ) != pdFALSE )
2447 /* Has the task already been resumed from within an ISR? */
2448 if( listIS_CONTAINED_WITHIN( &xPendingReadyList, &( pxTCB->xEventListItem ) ) == pdFALSE )
2450 /* Is it in the suspended list because it is in the Suspended
2451 * state, or because is is blocked with no timeout? */
2452 if( listIS_CONTAINED_WITHIN( NULL, &( pxTCB->xEventListItem ) ) != pdFALSE ) /*lint !e961. The cast is only redundant when NULL is used. */
2458 mtCOVERAGE_TEST_MARKER();
2463 mtCOVERAGE_TEST_MARKER();
2468 mtCOVERAGE_TEST_MARKER();
2472 } /*lint !e818 xTask cannot be a pointer to const because it is a typedef. */
2474 #endif /* INCLUDE_vTaskSuspend */
2475 /*-----------------------------------------------------------*/
2477 #if ( INCLUDE_vTaskSuspend == 1 )
2479 void vTaskResume( TaskHandle_t xTaskToResume )
2481 TCB_t * const pxTCB = xTaskToResume;
2483 /* It does not make sense to resume the calling task. */
2484 configASSERT( xTaskToResume );
2486 /* The parameter cannot be NULL as it is impossible to resume the
2487 * currently executing task. It is also impossible to resume a task
2488 * that is actively running on another core but it is too dangerous
2489 * to check their run state here. Safer to get into a critical section
2490 * and check if it is actually suspended or not below. */
2493 taskENTER_CRITICAL();
2495 if( prvTaskIsTaskSuspended( pxTCB ) != pdFALSE )
2497 traceTASK_RESUME( pxTCB );
2499 /* The ready list can be accessed even if the scheduler is
2500 * suspended because this is inside a critical section. */
2501 ( void ) uxListRemove( &( pxTCB->xStateListItem ) );
2502 prvAddTaskToReadyList( pxTCB );
2504 /* A higher priority task may have just been resumed. */
2505 #if ( configUSE_PREEMPTION == 1 )
2507 prvYieldForTask( pxTCB, pdTRUE );
2513 mtCOVERAGE_TEST_MARKER();
2516 taskEXIT_CRITICAL();
2520 mtCOVERAGE_TEST_MARKER();
2524 #endif /* INCLUDE_vTaskSuspend */
2526 /*-----------------------------------------------------------*/
2528 #if ( ( INCLUDE_xTaskResumeFromISR == 1 ) && ( INCLUDE_vTaskSuspend == 1 ) )
2530 BaseType_t xTaskResumeFromISR( TaskHandle_t xTaskToResume )
2532 BaseType_t xYieldRequired = pdFALSE;
2533 TCB_t * const pxTCB = xTaskToResume;
2534 UBaseType_t uxSavedInterruptStatus;
2536 configASSERT( xTaskToResume );
2538 /* RTOS ports that support interrupt nesting have the concept of a
2539 * maximum system call (or maximum API call) interrupt priority.
2540 * Interrupts that are above the maximum system call priority are keep
2541 * permanently enabled, even when the RTOS kernel is in a critical section,
2542 * but cannot make any calls to FreeRTOS API functions. If configASSERT()
2543 * is defined in FreeRTOSConfig.h then
2544 * portASSERT_IF_INTERRUPT_PRIORITY_INVALID() will result in an assertion
2545 * failure if a FreeRTOS API function is called from an interrupt that has
2546 * been assigned a priority above the configured maximum system call
2547 * priority. Only FreeRTOS functions that end in FromISR can be called
2548 * from interrupts that have been assigned a priority at or (logically)
2549 * below the maximum system call interrupt priority. FreeRTOS maintains a
2550 * separate interrupt safe API to ensure interrupt entry is as fast and as
2551 * simple as possible. More information (albeit Cortex-M specific) is
2552 * provided on the following link:
2553 * https://www.FreeRTOS.org/RTOS-Cortex-M3-M4.html */
2554 portASSERT_IF_INTERRUPT_PRIORITY_INVALID();
2556 uxSavedInterruptStatus = portSET_INTERRUPT_MASK_FROM_ISR();
2558 if( prvTaskIsTaskSuspended( pxTCB ) != pdFALSE )
2560 traceTASK_RESUME_FROM_ISR( pxTCB );
2562 /* Check the ready lists can be accessed. */
2563 if( uxSchedulerSuspended == ( UBaseType_t ) pdFALSE )
2565 /* Ready lists can be accessed so move the task from the
2566 * suspended list to the ready list directly. */
2568 ( void ) uxListRemove( &( pxTCB->xStateListItem ) );
2569 prvAddTaskToReadyList( pxTCB );
2573 /* The delayed or ready lists cannot be accessed so the task
2574 * is held in the pending ready list until the scheduler is
2576 vListInsertEnd( &( xPendingReadyList ), &( pxTCB->xEventListItem ) );
2579 #if ( configUSE_PREEMPTION == 1 )
2580 prvYieldForTask( pxTCB, pdTRUE );
2582 if( xYieldPendings[ portGET_CORE_ID() ] != pdFALSE )
2584 xYieldRequired = pdTRUE;
2590 mtCOVERAGE_TEST_MARKER();
2593 portCLEAR_INTERRUPT_MASK_FROM_ISR( uxSavedInterruptStatus );
2595 return xYieldRequired;
2598 #endif /* ( ( INCLUDE_xTaskResumeFromISR == 1 ) && ( INCLUDE_vTaskSuspend == 1 ) ) */
2599 /*-----------------------------------------------------------*/
2601 void vTaskStartScheduler( void )
2605 char cIdleName[ configMAX_TASK_NAME_LEN ];
2607 #if ( configUSE_TIMERS == 1 )
2609 xReturn = xTimerCreateTimerTask();
2611 #endif /* configUSE_TIMERS */
2613 /* Add each idle task at the lowest priority. */
2614 for( xCoreID = ( BaseType_t ) 0; xCoreID < ( BaseType_t ) configNUM_CORES; xCoreID++ )
2618 if( xReturn == pdFAIL )
2624 mtCOVERAGE_TEST_MARKER();
2627 for( x = ( BaseType_t ) 0; x < ( BaseType_t ) configMAX_TASK_NAME_LEN; x++ )
2629 cIdleName[ x ] = configIDLE_TASK_NAME[ x ];
2631 /* Don't copy all configMAX_TASK_NAME_LEN if the string is shorter than
2632 * configMAX_TASK_NAME_LEN characters just in case the memory after the
2633 * string is not accessible (extremely unlikely). */
2634 if( cIdleName[ x ] == ( char ) 0x00 )
2640 mtCOVERAGE_TEST_MARKER();
2644 /* Append the idle task number to the end of the name if there is space */
2645 if( x < configMAX_TASK_NAME_LEN )
2647 cIdleName[ x++ ] = xCoreID + '0';
2649 /* And append a null character if there is space */
2650 if( x < configMAX_TASK_NAME_LEN )
2652 cIdleName[ x ] = '\0';
2656 mtCOVERAGE_TEST_MARKER();
2661 mtCOVERAGE_TEST_MARKER();
2664 #if ( configSUPPORT_STATIC_ALLOCATION == 1 )
2666 #error User must specify an array of buffers for idle task TCBs and stacks
2667 StaticTask_t * pxIdleTaskTCBBuffer = NULL;
2668 StackType_t * pxIdleTaskStackBuffer = NULL;
2669 uint32_t ulIdleTaskStackSize;
2671 /* The Idle task is created using user provided RAM - obtain the
2672 * address of the RAM then create the idle task. */
2673 vApplicationGetIdleTaskMemory( &pxIdleTaskTCBBuffer, &pxIdleTaskStackBuffer, &ulIdleTaskStackSize );
2674 xIdleTaskHandle[ xCoreID ] = xTaskCreateStatic( prvIdleTask,
2676 ulIdleTaskStackSize,
2677 ( void * ) NULL, /*lint !e961. The cast is not redundant for all compilers. */
2678 portPRIVILEGE_BIT, /* In effect ( tskIDLE_PRIORITY | portPRIVILEGE_BIT ), but tskIDLE_PRIORITY is zero. */
2679 pxIdleTaskStackBuffer,
2680 pxIdleTaskTCBBuffer ); /*lint !e961 MISRA exception, justified as it is not a redundant explicit cast to all supported compilers. */
2682 if( xIdleTaskHandle[ xCoreID ] != NULL )
2691 #else /* if ( configSUPPORT_STATIC_ALLOCATION == 1 ) */
2693 /* The Idle task is being created using dynamically allocated RAM. */
2694 xReturn = xTaskCreate( prvIdleTask,
2696 configMINIMAL_STACK_SIZE,
2698 portPRIVILEGE_BIT, /* In effect ( tskIDLE_PRIORITY | portPRIVILEGE_BIT ), but tskIDLE_PRIORITY is zero. */
2699 &xIdleTaskHandle[ xCoreID ] ); /*lint !e961 MISRA exception, justified as it is not a redundant explicit cast to all supported compilers. */
2701 #endif /* configSUPPORT_STATIC_ALLOCATION */
2704 if( xReturn == pdPASS )
2706 /* freertos_tasks_c_additions_init() should only be called if the user
2707 * definable macro FREERTOS_TASKS_C_ADDITIONS_INIT() is defined, as that is
2708 * the only macro called by the function. */
2709 #ifdef FREERTOS_TASKS_C_ADDITIONS_INIT
2711 freertos_tasks_c_additions_init();
2715 /* Interrupts are turned off here, to ensure a tick does not occur
2716 * before or during the call to xPortStartScheduler(). The stacks of
2717 * the created tasks contain a status word with interrupts switched on
2718 * so interrupts will automatically get re-enabled when the first task
2720 portDISABLE_INTERRUPTS();
2722 #if ( configUSE_NEWLIB_REENTRANT == 1 )
2724 /* Switch Newlib's _impure_ptr variable to point to the _reent
2725 * structure specific to the task that will run first.
2726 * See the third party link http://www.nadler.com/embedded/newlibAndFreeRTOS.html
2727 * for additional information. */
2728 _impure_ptr = &( pxCurrentTCB->xNewLib_reent );
2730 #endif /* configUSE_NEWLIB_REENTRANT */
2732 xNextTaskUnblockTime = portMAX_DELAY;
2733 xSchedulerRunning = pdTRUE;
2734 xTickCount = ( TickType_t ) configINITIAL_TICK_COUNT;
2736 /* If configGENERATE_RUN_TIME_STATS is defined then the following
2737 * macro must be defined to configure the timer/counter used to generate
2738 * the run time counter time base. NOTE: If configGENERATE_RUN_TIME_STATS
2739 * is set to 0 and the following line fails to build then ensure you do not
2740 * have portCONFIGURE_TIMER_FOR_RUN_TIME_STATS() defined in your
2741 * FreeRTOSConfig.h file. */
2742 portCONFIGURE_TIMER_FOR_RUN_TIME_STATS();
2744 traceTASK_SWITCHED_IN();
2746 /* Setting up the timer tick is hardware specific and thus in the
2747 * portable interface. */
2748 if( xPortStartScheduler() != pdFALSE )
2750 /* Should not reach here as if the scheduler is running the
2751 * function will not return. */
2755 /* Should only reach here if a task calls xTaskEndScheduler(). */
2760 /* This line will only be reached if the kernel could not be started,
2761 * because there was not enough FreeRTOS heap to create the idle task
2762 * or the timer task. */
2763 configASSERT( xReturn != errCOULD_NOT_ALLOCATE_REQUIRED_MEMORY );
2766 /* Prevent compiler warnings if INCLUDE_xTaskGetIdleTaskHandle is set to 0,
2767 * meaning xIdleTaskHandle is not used anywhere else. */
2768 ( void ) xIdleTaskHandle;
2770 /* OpenOCD makes use of uxTopUsedPriority for thread debugging. Prevent uxTopUsedPriority
2771 * from getting optimized out as it is no longer used by the kernel. */
2772 ( void ) uxTopUsedPriority;
2774 /*-----------------------------------------------------------*/
2776 void vTaskEndScheduler( void )
2778 /* Stop the scheduler interrupts and call the portable scheduler end
2779 * routine so the original ISRs can be restored if necessary. The port
2780 * layer must ensure interrupts enable bit is left in the correct state. */
2781 portDISABLE_INTERRUPTS();
2782 xSchedulerRunning = pdFALSE;
2783 vPortEndScheduler();
2785 /*----------------------------------------------------------*/
2787 void vTaskSuspendAll( void )
2789 UBaseType_t ulState;
2791 /* This must only be called from within a task */
2792 portASSERT_IF_IN_ISR();
2794 if( xSchedulerRunning != pdFALSE )
2796 /* writes to uxSchedulerSuspended must be protected by both the task AND ISR locks.
2797 * We must disable interrupts before we grab the locks in the event that this task is
2798 * interrupted and switches context before incrementing uxSchedulerSuspended.
2799 * It is safe to re-enable interrupts after releasing the ISR lock and incrementing
2800 * uxSchedulerSuspended since that will prevent context switches. */
2801 ulState = portDISABLE_INTERRUPTS();
2803 /* portSOFRWARE_BARRIER() is only implemented for emulated/simulated ports that
2804 * do not otherwise exhibit real time behaviour. */
2805 portSOFTWARE_BARRIER();
2807 portGET_TASK_LOCK();
2810 /* The scheduler is suspended if uxSchedulerSuspended is non-zero. An increment
2811 * is used to allow calls to vTaskSuspendAll() to nest. */
2812 ++uxSchedulerSuspended;
2813 portRELEASE_ISR_LOCK();
2815 if( ( uxSchedulerSuspended == 1U ) && ( pxCurrentTCB->uxCriticalNesting == 0U ) )
2817 prvCheckForRunStateChange();
2820 portRESTORE_INTERRUPTS( ulState );
2824 mtCOVERAGE_TEST_MARKER();
2827 /*----------------------------------------------------------*/
2829 #if ( configUSE_TICKLESS_IDLE != 0 )
2831 static TickType_t prvGetExpectedIdleTime( void )
2834 UBaseType_t uxHigherPriorityReadyTasks = pdFALSE;
2836 /* uxHigherPriorityReadyTasks takes care of the case where
2837 * configUSE_PREEMPTION is 0, so there may be tasks above the idle priority
2838 * task that are in the Ready state, even though the idle task is
2840 #if ( configUSE_PORT_OPTIMISED_TASK_SELECTION == 0 )
2842 if( uxTopReadyPriority > tskIDLE_PRIORITY )
2844 uxHigherPriorityReadyTasks = pdTRUE;
2849 const UBaseType_t uxLeastSignificantBit = ( UBaseType_t ) 0x01;
2851 /* When port optimised task selection is used the uxTopReadyPriority
2852 * variable is used as a bit map. If bits other than the least
2853 * significant bit are set then there are tasks that have a priority
2854 * above the idle priority that are in the Ready state. This takes
2855 * care of the case where the co-operative scheduler is in use. */
2856 if( uxTopReadyPriority > uxLeastSignificantBit )
2858 uxHigherPriorityReadyTasks = pdTRUE;
2861 #endif /* if ( configUSE_PORT_OPTIMISED_TASK_SELECTION == 0 ) */
2863 if( pxCurrentTCB->uxPriority > tskIDLE_PRIORITY )
2867 else if( listCURRENT_LIST_LENGTH( &( pxReadyTasksLists[ tskIDLE_PRIORITY ] ) ) > 1 )
2869 /* There are other idle priority tasks in the ready state. If
2870 * time slicing is used then the very next tick interrupt must be
2874 else if( uxHigherPriorityReadyTasks != pdFALSE )
2876 /* There are tasks in the Ready state that have a priority above the
2877 * idle priority. This path can only be reached if
2878 * configUSE_PREEMPTION is 0. */
2883 xReturn = xNextTaskUnblockTime - xTickCount;
2889 #endif /* configUSE_TICKLESS_IDLE */
2890 /*----------------------------------------------------------*/
2892 BaseType_t xTaskResumeAll( void )
2894 TCB_t * pxTCB = NULL;
2895 BaseType_t xAlreadyYielded = pdFALSE;
2897 if( xSchedulerRunning != pdFALSE )
2899 /* It is possible that an ISR caused a task to be removed from an event
2900 * list while the scheduler was suspended. If this was the case then the
2901 * removed task will have been added to the xPendingReadyList. Once the
2902 * scheduler has been resumed it is safe to move all the pending ready
2903 * tasks from this list into their appropriate ready list. */
2904 taskENTER_CRITICAL();
2908 xCoreID = portGET_CORE_ID();
2910 /* If uxSchedulerSuspended is zero then this function does not match a
2911 * previous call to vTaskSuspendAll(). */
2912 configASSERT( uxSchedulerSuspended );
2914 --uxSchedulerSuspended;
2915 portRELEASE_TASK_LOCK();
2917 if( uxSchedulerSuspended == ( UBaseType_t ) pdFALSE )
2919 if( uxCurrentNumberOfTasks > ( UBaseType_t ) 0U )
2921 /* Move any readied tasks from the pending list into the
2922 * appropriate ready list. */
2923 while( listLIST_IS_EMPTY( &xPendingReadyList ) == pdFALSE )
2925 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. */
2926 ( void ) uxListRemove( &( pxTCB->xEventListItem ) );
2927 ( void ) uxListRemove( &( pxTCB->xStateListItem ) );
2928 prvAddTaskToReadyList( pxTCB );
2930 /* All appropriate tasks yield at the moment a task is added to xPendingReadyList.
2931 * If the current core yielded then vTaskSwitchContext() has already been called
2932 * which sets xYieldPendings for the current core to pdTRUE. */
2937 /* A task was unblocked while the scheduler was suspended,
2938 * which may have prevented the next unblock time from being
2939 * re-calculated, in which case re-calculate it now. Mainly
2940 * important for low power tickless implementations, where
2941 * this can prevent an unnecessary exit from low power
2943 prvResetNextTaskUnblockTime();
2946 /* If any ticks occurred while the scheduler was suspended then
2947 * they should be processed now. This ensures the tick count does
2948 * not slip, and that any delayed tasks are resumed at the correct
2951 * It should be safe to call xTaskIncrementTick here from any core
2952 * since we are in a critical section and xTaskIncrementTick itself
2953 * protects itself within a critical section. Suspending the scheduler
2954 * from any core causes xTaskIncrementTick to increment uxPendedCounts.*/
2956 TickType_t xPendedCounts = xPendedTicks; /* Non-volatile copy. */
2958 if( xPendedCounts > ( TickType_t ) 0U )
2962 if( xTaskIncrementTick() != pdFALSE )
2964 /* other cores are interrupted from
2965 * within xTaskIncrementTick(). */
2966 xYieldPendings[ xCoreID ] = pdTRUE;
2970 mtCOVERAGE_TEST_MARKER();
2974 } while( xPendedCounts > ( TickType_t ) 0U );
2980 mtCOVERAGE_TEST_MARKER();
2984 if( xYieldPendings[ xCoreID ] != pdFALSE )
2986 /* If xYieldPendings is true then taskEXIT_CRITICAL()
2987 * will yield, so make sure we return true to let the
2988 * caller know a yield has already happened. */
2989 xAlreadyYielded = pdTRUE;
2995 mtCOVERAGE_TEST_MARKER();
2998 taskEXIT_CRITICAL();
3002 mtCOVERAGE_TEST_MARKER();
3005 return xAlreadyYielded;
3007 /*-----------------------------------------------------------*/
3009 TickType_t xTaskGetTickCount( void )
3013 /* Critical section required if running on a 16 bit processor. */
3014 portTICK_TYPE_ENTER_CRITICAL();
3016 xTicks = xTickCount;
3018 portTICK_TYPE_EXIT_CRITICAL();
3022 /*-----------------------------------------------------------*/
3024 TickType_t xTaskGetTickCountFromISR( void )
3027 UBaseType_t uxSavedInterruptStatus;
3029 /* RTOS ports that support interrupt nesting have the concept of a maximum
3030 * system call (or maximum API call) interrupt priority. Interrupts that are
3031 * above the maximum system call priority are kept permanently enabled, even
3032 * when the RTOS kernel is in a critical section, but cannot make any calls to
3033 * FreeRTOS API functions. If configASSERT() is defined in FreeRTOSConfig.h
3034 * then portASSERT_IF_INTERRUPT_PRIORITY_INVALID() will result in an assertion
3035 * failure if a FreeRTOS API function is called from an interrupt that has been
3036 * assigned a priority above the configured maximum system call priority.
3037 * Only FreeRTOS functions that end in FromISR can be called from interrupts
3038 * that have been assigned a priority at or (logically) below the maximum
3039 * system call interrupt priority. FreeRTOS maintains a separate interrupt
3040 * safe API to ensure interrupt entry is as fast and as simple as possible.
3041 * More information (albeit Cortex-M specific) is provided on the following
3042 * link: https://www.FreeRTOS.org/RTOS-Cortex-M3-M4.html */
3043 portASSERT_IF_INTERRUPT_PRIORITY_INVALID();
3045 uxSavedInterruptStatus = portTICK_TYPE_SET_INTERRUPT_MASK_FROM_ISR();
3047 xReturn = xTickCount;
3049 portTICK_TYPE_CLEAR_INTERRUPT_MASK_FROM_ISR( uxSavedInterruptStatus );
3053 /*-----------------------------------------------------------*/
3055 UBaseType_t uxTaskGetNumberOfTasks( void )
3057 /* A critical section is not required because the variables are of type
3059 return uxCurrentNumberOfTasks;
3061 /*-----------------------------------------------------------*/
3063 char * pcTaskGetName( TaskHandle_t xTaskToQuery ) /*lint !e971 Unqualified char types are allowed for strings and single characters only. */
3067 /* If null is passed in here then the name of the calling task is being
3069 pxTCB = prvGetTCBFromHandle( xTaskToQuery );
3070 configASSERT( pxTCB );
3071 return &( pxTCB->pcTaskName[ 0 ] );
3073 /*-----------------------------------------------------------*/
3075 #if ( INCLUDE_xTaskGetHandle == 1 )
3077 static TCB_t * prvSearchForNameWithinSingleList( List_t * pxList,
3078 const char pcNameToQuery[] )
3080 TCB_t * pxNextTCB, * pxFirstTCB, * pxReturn = NULL;
3083 BaseType_t xBreakLoop;
3085 /* This function is called with the scheduler suspended. */
3087 if( listCURRENT_LIST_LENGTH( pxList ) > ( UBaseType_t ) 0 )
3089 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. */
3093 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. */
3095 /* Check each character in the name looking for a match or
3097 xBreakLoop = pdFALSE;
3099 for( x = ( UBaseType_t ) 0; x < ( UBaseType_t ) configMAX_TASK_NAME_LEN; x++ )
3101 cNextChar = pxNextTCB->pcTaskName[ x ];
3103 if( cNextChar != pcNameToQuery[ x ] )
3105 /* Characters didn't match. */
3106 xBreakLoop = pdTRUE;
3108 else if( cNextChar == ( char ) 0x00 )
3110 /* Both strings terminated, a match must have been
3112 pxReturn = pxNextTCB;
3113 xBreakLoop = pdTRUE;
3117 mtCOVERAGE_TEST_MARKER();
3120 if( xBreakLoop != pdFALSE )
3126 if( pxReturn != NULL )
3128 /* The handle has been found. */
3131 } while( pxNextTCB != pxFirstTCB );
3135 mtCOVERAGE_TEST_MARKER();
3141 #endif /* INCLUDE_xTaskGetHandle */
3142 /*-----------------------------------------------------------*/
3144 #if ( INCLUDE_xTaskGetHandle == 1 )
3146 TaskHandle_t xTaskGetHandle( const char * pcNameToQuery ) /*lint !e971 Unqualified char types are allowed for strings and single characters only. */
3148 UBaseType_t uxQueue = configMAX_PRIORITIES;
3151 /* Task names will be truncated to configMAX_TASK_NAME_LEN - 1 bytes. */
3152 configASSERT( strlen( pcNameToQuery ) < configMAX_TASK_NAME_LEN );
3156 /* Search the ready lists. */
3160 pxTCB = prvSearchForNameWithinSingleList( ( List_t * ) &( pxReadyTasksLists[ uxQueue ] ), pcNameToQuery );
3164 /* Found the handle. */
3167 } while( uxQueue > ( UBaseType_t ) tskIDLE_PRIORITY ); /*lint !e961 MISRA exception as the casts are only redundant for some ports. */
3169 /* Search the delayed lists. */
3172 pxTCB = prvSearchForNameWithinSingleList( ( List_t * ) pxDelayedTaskList, pcNameToQuery );
3177 pxTCB = prvSearchForNameWithinSingleList( ( List_t * ) pxOverflowDelayedTaskList, pcNameToQuery );
3180 #if ( INCLUDE_vTaskSuspend == 1 )
3184 /* Search the suspended list. */
3185 pxTCB = prvSearchForNameWithinSingleList( &xSuspendedTaskList, pcNameToQuery );
3190 #if ( INCLUDE_vTaskDelete == 1 )
3194 /* Search the deleted list. */
3195 pxTCB = prvSearchForNameWithinSingleList( &xTasksWaitingTermination, pcNameToQuery );
3200 ( void ) xTaskResumeAll();
3205 #endif /* INCLUDE_xTaskGetHandle */
3206 /*-----------------------------------------------------------*/
3208 #if ( configUSE_TRACE_FACILITY == 1 )
3210 UBaseType_t uxTaskGetSystemState( TaskStatus_t * const pxTaskStatusArray,
3211 const UBaseType_t uxArraySize,
3212 uint32_t * const pulTotalRunTime )
3214 UBaseType_t uxTask = 0, uxQueue = configMAX_PRIORITIES;
3218 /* Is there a space in the array for each task in the system? */
3219 if( uxArraySize >= uxCurrentNumberOfTasks )
3221 /* Fill in an TaskStatus_t structure with information on each
3222 * task in the Ready state. */
3226 uxTask += prvListTasksWithinSingleList( &( pxTaskStatusArray[ uxTask ] ), &( pxReadyTasksLists[ uxQueue ] ), eReady );
3227 } while( uxQueue > ( UBaseType_t ) tskIDLE_PRIORITY ); /*lint !e961 MISRA exception as the casts are only redundant for some ports. */
3229 /* Fill in an TaskStatus_t structure with information on each
3230 * task in the Blocked state. */
3231 uxTask += prvListTasksWithinSingleList( &( pxTaskStatusArray[ uxTask ] ), ( List_t * ) pxDelayedTaskList, eBlocked );
3232 uxTask += prvListTasksWithinSingleList( &( pxTaskStatusArray[ uxTask ] ), ( List_t * ) pxOverflowDelayedTaskList, eBlocked );
3234 #if ( INCLUDE_vTaskDelete == 1 )
3236 /* Fill in an TaskStatus_t structure with information on
3237 * each task that has been deleted but not yet cleaned up. */
3238 uxTask += prvListTasksWithinSingleList( &( pxTaskStatusArray[ uxTask ] ), &xTasksWaitingTermination, eDeleted );
3242 #if ( INCLUDE_vTaskSuspend == 1 )
3244 /* Fill in an TaskStatus_t structure with information on
3245 * each task in the Suspended state. */
3246 uxTask += prvListTasksWithinSingleList( &( pxTaskStatusArray[ uxTask ] ), &xSuspendedTaskList, eSuspended );
3250 #if ( configGENERATE_RUN_TIME_STATS == 1 )
3252 if( pulTotalRunTime != NULL )
3254 #ifdef portALT_GET_RUN_TIME_COUNTER_VALUE
3255 portALT_GET_RUN_TIME_COUNTER_VALUE( ( *pulTotalRunTime ) );
3257 *pulTotalRunTime = portGET_RUN_TIME_COUNTER_VALUE();
3261 #else /* if ( configGENERATE_RUN_TIME_STATS == 1 ) */
3263 if( pulTotalRunTime != NULL )
3265 *pulTotalRunTime = 0;
3268 #endif /* if ( configGENERATE_RUN_TIME_STATS == 1 ) */
3272 mtCOVERAGE_TEST_MARKER();
3275 ( void ) xTaskResumeAll();
3280 #endif /* configUSE_TRACE_FACILITY */
3281 /*----------------------------------------------------------*/
3283 #if ( INCLUDE_xTaskGetIdleTaskHandle == 1 )
3285 TaskHandle_t * xTaskGetIdleTaskHandle( void )
3287 /* If xTaskGetIdleTaskHandle() is called before the scheduler has been
3288 * started, then xIdleTaskHandle will be NULL. */
3289 configASSERT( ( xIdleTaskHandle != NULL ) );
3290 return &( xIdleTaskHandle[ 0 ] );
3293 #endif /* INCLUDE_xTaskGetIdleTaskHandle */
3294 /*----------------------------------------------------------*/
3296 /* This conditional compilation should use inequality to 0, not equality to 1.
3297 * This is to ensure vTaskStepTick() is available when user defined low power mode
3298 * implementations require configUSE_TICKLESS_IDLE to be set to a value other than
3300 #if ( configUSE_TICKLESS_IDLE != 0 )
3302 void vTaskStepTick( const TickType_t xTicksToJump )
3304 /* Correct the tick count value after a period during which the tick
3305 * was suppressed. Note this does *not* call the tick hook function for
3306 * each stepped tick. */
3307 configASSERT( ( xTickCount + xTicksToJump ) <= xNextTaskUnblockTime );
3308 xTickCount += xTicksToJump;
3309 traceINCREASE_TICK_COUNT( xTicksToJump );
3312 #endif /* configUSE_TICKLESS_IDLE */
3313 /*----------------------------------------------------------*/
3315 BaseType_t xTaskCatchUpTicks( TickType_t xTicksToCatchUp )
3317 BaseType_t xYieldOccurred;
3319 /* Must not be called with the scheduler suspended as the implementation
3320 * relies on xPendedTicks being wound down to 0 in xTaskResumeAll(). */
3321 configASSERT( uxSchedulerSuspended == 0 );
3323 /* Use xPendedTicks to mimic xTicksToCatchUp number of ticks occurring when
3324 * the scheduler is suspended so the ticks are executed in xTaskResumeAll(). */
3326 xPendedTicks += xTicksToCatchUp;
3327 xYieldOccurred = xTaskResumeAll();
3329 return xYieldOccurred;
3331 /*----------------------------------------------------------*/
3333 #if ( INCLUDE_xTaskAbortDelay == 1 )
3335 BaseType_t xTaskAbortDelay( TaskHandle_t xTask )
3337 TCB_t * pxTCB = xTask;
3340 configASSERT( pxTCB );
3344 /* A task can only be prematurely removed from the Blocked state if
3345 * it is actually in the Blocked state. */
3346 if( eTaskGetState( xTask ) == eBlocked )
3350 /* Remove the reference to the task from the blocked list. An
3351 * interrupt won't touch the xStateListItem because the
3352 * scheduler is suspended. */
3353 ( void ) uxListRemove( &( pxTCB->xStateListItem ) );
3355 /* Is the task waiting on an event also? If so remove it from
3356 * the event list too. Interrupts can touch the event list item,
3357 * even though the scheduler is suspended, so a critical section
3359 taskENTER_CRITICAL();
3361 if( listLIST_ITEM_CONTAINER( &( pxTCB->xEventListItem ) ) != NULL )
3363 ( void ) uxListRemove( &( pxTCB->xEventListItem ) );
3365 /* This lets the task know it was forcibly removed from the
3366 * blocked state so it should not re-evaluate its block time and
3367 * then block again. */
3368 pxTCB->ucDelayAborted = pdTRUE;
3372 mtCOVERAGE_TEST_MARKER();
3375 taskEXIT_CRITICAL();
3377 /* Place the unblocked task into the appropriate ready list. */
3378 prvAddTaskToReadyList( pxTCB );
3380 /* A task being unblocked cannot cause an immediate context
3381 * switch if preemption is turned off. */
3382 #if ( configUSE_PREEMPTION == 1 )
3384 taskENTER_CRITICAL();
3386 prvYieldForTask( pxTCB, pdFALSE );
3388 taskEXIT_CRITICAL();
3390 #endif /* configUSE_PREEMPTION */
3397 ( void ) xTaskResumeAll();
3402 #endif /* INCLUDE_xTaskAbortDelay */
3403 /*----------------------------------------------------------*/
3405 BaseType_t xTaskIncrementTick( void )
3408 TickType_t xItemValue;
3409 BaseType_t xSwitchRequired = pdFALSE;
3411 #if ( configUSE_PREEMPTION == 1 )
3413 BaseType_t xCoreYieldList[ configNUM_CORES ] = { pdFALSE };
3414 #endif /* configUSE_PREEMPTION */
3416 taskENTER_CRITICAL();
3418 /* Called by the portable layer each time a tick interrupt occurs.
3419 * Increments the tick then checks to see if the new tick value will cause any
3420 * tasks to be unblocked. */
3421 traceTASK_INCREMENT_TICK( xTickCount );
3423 /* Tick increment should occur on every kernel timer event. Core 0 has the
3424 * responsibility to increment the tick, or increment the pended ticks if the
3425 * scheduler is suspended. If pended ticks is greater than zero, the core that
3426 * calls xTaskResumeAll has the responsibility to increment the tick. */
3427 if( uxSchedulerSuspended == ( UBaseType_t ) pdFALSE )
3429 /* Minor optimisation. The tick count cannot change in this
3431 const TickType_t xConstTickCount = xTickCount + ( TickType_t ) 1;
3433 /* Increment the RTOS tick, switching the delayed and overflowed
3434 * delayed lists if it wraps to 0. */
3435 xTickCount = xConstTickCount;
3437 if( xConstTickCount == ( TickType_t ) 0U ) /*lint !e774 'if' does not always evaluate to false as it is looking for an overflow. */
3439 taskSWITCH_DELAYED_LISTS();
3443 mtCOVERAGE_TEST_MARKER();
3446 /* See if this tick has made a timeout expire. Tasks are stored in
3447 * the queue in the order of their wake time - meaning once one task
3448 * has been found whose block time has not expired there is no need to
3449 * look any further down the list. */
3450 if( xConstTickCount >= xNextTaskUnblockTime )
3454 if( listLIST_IS_EMPTY( pxDelayedTaskList ) != pdFALSE )
3456 /* The delayed list is empty. Set xNextTaskUnblockTime
3457 * to the maximum possible value so it is extremely
3459 * if( xTickCount >= xNextTaskUnblockTime ) test will pass
3460 * next time through. */
3461 xNextTaskUnblockTime = portMAX_DELAY; /*lint !e961 MISRA exception as the casts are only redundant for some ports. */
3466 /* The delayed list is not empty, get the value of the
3467 * item at the head of the delayed list. This is the time
3468 * at which the task at the head of the delayed list must
3469 * be removed from the Blocked state. */
3470 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. */
3471 xItemValue = listGET_LIST_ITEM_VALUE( &( pxTCB->xStateListItem ) );
3473 if( xConstTickCount < xItemValue )
3475 /* It is not time to unblock this item yet, but the
3476 * item value is the time at which the task at the head
3477 * of the blocked list must be removed from the Blocked
3478 * state - so record the item value in
3479 * xNextTaskUnblockTime. */
3480 xNextTaskUnblockTime = xItemValue;
3481 break; /*lint !e9011 Code structure here is deemed easier to understand with multiple breaks. */
3485 mtCOVERAGE_TEST_MARKER();
3488 /* It is time to remove the item from the Blocked state. */
3489 ( void ) uxListRemove( &( pxTCB->xStateListItem ) );
3491 /* Is the task waiting on an event also? If so remove
3492 * it from the event list. */
3493 if( listLIST_ITEM_CONTAINER( &( pxTCB->xEventListItem ) ) != NULL )
3495 ( void ) uxListRemove( &( pxTCB->xEventListItem ) );
3499 mtCOVERAGE_TEST_MARKER();
3502 /* Place the unblocked task into the appropriate ready
3504 prvAddTaskToReadyList( pxTCB );
3506 /* A task being unblocked cannot cause an immediate
3507 * context switch if preemption is turned off. */
3508 #if ( configUSE_PREEMPTION == 1 )
3510 prvYieldForTask( pxTCB, pdTRUE );
3512 #endif /* configUSE_PREEMPTION */
3517 /* Tasks of equal priority to the currently running task will share
3518 * processing time (time slice) if preemption is on, and the application
3519 * writer has not explicitly turned time slicing off. */
3520 #if ( ( configUSE_PREEMPTION == 1 ) && ( configUSE_TIME_SLICING == 1 ) )
3522 /* TODO: If there are fewer "non-IDLE" READY tasks than cores, do not
3523 * force a context switch that would just shuffle tasks around cores */
3524 /* TODO: There are certainly better ways of doing this that would reduce
3525 * the number of interrupts and also potentially help prevent tasks from
3526 * moving between cores as often. This, however, works for now. */
3527 for( x = ( UBaseType_t ) 0; x < ( UBaseType_t ) configNUM_CORES; x++ )
3529 if( listCURRENT_LIST_LENGTH( &( pxReadyTasksLists[ pxCurrentTCBs[ x ]->uxPriority ] ) ) > ( UBaseType_t ) 1 )
3531 xCoreYieldList[ x ] = pdTRUE;
3535 mtCOVERAGE_TEST_MARKER();
3539 #endif /* ( ( configUSE_PREEMPTION == 1 ) && ( configUSE_TIME_SLICING == 1 ) ) */
3541 #if ( configUSE_TICK_HOOK == 1 )
3543 /* Guard against the tick hook being called when the pended tick
3544 * count is being unwound (when the scheduler is being unlocked). */
3545 if( xPendedTicks == ( TickType_t ) 0 )
3547 vApplicationTickHook();
3551 mtCOVERAGE_TEST_MARKER();
3554 #endif /* configUSE_TICK_HOOK */
3556 #if ( configUSE_PREEMPTION == 1 )
3558 for( x = ( UBaseType_t ) 0; x < ( UBaseType_t ) configNUM_CORES; x++ )
3560 if( xYieldPendings[ x ] != pdFALSE )
3562 xCoreYieldList[ x ] = pdTRUE;
3566 mtCOVERAGE_TEST_MARKER();
3570 #endif /* configUSE_PREEMPTION */
3572 #if ( configUSE_PREEMPTION == 1 )
3576 xCoreID = portGET_CORE_ID();
3578 for( x = ( UBaseType_t ) 0; x < ( UBaseType_t ) configNUM_CORES; x++ )
3580 #if ( configUSE_TASK_PREEMPTION_DISABLE == 1 )
3581 if( pxCurrentTCBs[ x ]->xPreemptionDisable == pdFALSE )
3584 if( xCoreYieldList[ x ] != pdFALSE )
3588 xSwitchRequired = pdTRUE;
3597 mtCOVERAGE_TEST_MARKER();
3602 #endif /* configUSE_PREEMPTION */
3608 /* The tick hook gets called at regular intervals, even if the
3609 * scheduler is locked. */
3610 #if ( configUSE_TICK_HOOK == 1 )
3612 vApplicationTickHook();
3617 taskEXIT_CRITICAL();
3619 return xSwitchRequired;
3621 /*-----------------------------------------------------------*/
3623 #if ( configUSE_APPLICATION_TASK_TAG == 1 )
3625 void vTaskSetApplicationTaskTag( TaskHandle_t xTask,
3626 TaskHookFunction_t pxHookFunction )
3630 /* If xTask is NULL then it is the task hook of the calling task that is
3634 xTCB = ( TCB_t * ) pxCurrentTCB;
3641 /* Save the hook function in the TCB. A critical section is required as
3642 * the value can be accessed from an interrupt. */
3643 taskENTER_CRITICAL();
3645 xTCB->pxTaskTag = pxHookFunction;
3647 taskEXIT_CRITICAL();
3650 #endif /* configUSE_APPLICATION_TASK_TAG */
3651 /*-----------------------------------------------------------*/
3653 #if ( configUSE_APPLICATION_TASK_TAG == 1 )
3655 TaskHookFunction_t xTaskGetApplicationTaskTag( TaskHandle_t xTask )
3658 TaskHookFunction_t xReturn;
3660 /* If xTask is NULL then set the calling task's hook. */
3661 pxTCB = prvGetTCBFromHandle( xTask );
3663 /* Save the hook function in the TCB. A critical section is required as
3664 * the value can be accessed from an interrupt. */
3665 taskENTER_CRITICAL();
3667 xReturn = pxTCB->pxTaskTag;
3669 taskEXIT_CRITICAL();
3674 #endif /* configUSE_APPLICATION_TASK_TAG */
3675 /*-----------------------------------------------------------*/
3677 #if ( configUSE_APPLICATION_TASK_TAG == 1 )
3679 TaskHookFunction_t xTaskGetApplicationTaskTagFromISR( TaskHandle_t xTask )
3682 TaskHookFunction_t xReturn;
3683 UBaseType_t uxSavedInterruptStatus;
3685 /* If xTask is NULL then set the calling task's hook. */
3686 pxTCB = prvGetTCBFromHandle( xTask );
3688 /* Save the hook function in the TCB. A critical section is required as
3689 * the value can be accessed from an interrupt. */
3690 uxSavedInterruptStatus = portSET_INTERRUPT_MASK_FROM_ISR();
3692 xReturn = pxTCB->pxTaskTag;
3694 portCLEAR_INTERRUPT_MASK_FROM_ISR( uxSavedInterruptStatus );
3699 #endif /* configUSE_APPLICATION_TASK_TAG */
3700 /*-----------------------------------------------------------*/
3702 #if ( configUSE_APPLICATION_TASK_TAG == 1 )
3704 BaseType_t xTaskCallApplicationTaskHook( TaskHandle_t xTask,
3705 void * pvParameter )
3710 /* If xTask is NULL then we are calling our own task hook. */
3713 xTCB = pxCurrentTCB;
3720 if( xTCB->pxTaskTag != NULL )
3722 xReturn = xTCB->pxTaskTag( pvParameter );
3732 #endif /* configUSE_APPLICATION_TASK_TAG */
3733 /*-----------------------------------------------------------*/
3735 void vTaskSwitchContext( BaseType_t xCoreID )
3737 /* Acquire both locks:
3738 * - The ISR lock protects the ready list from simultaneous access by
3739 * both other ISRs and tasks.
3740 * - We also take the task lock to pause here in case another core has
3741 * suspended the scheduler. We don't want to simply set xYieldPending
3742 * and move on if another core suspended the scheduler. We should only
3743 * do that if the current core has suspended the scheduler. */
3745 portGET_TASK_LOCK(); /* Must always acquire the task lock first */
3748 /* vTaskSwitchContext() must never be called from within a critical section.
3749 * This is not necessarily true for vanilla FreeRTOS, but it is for this SMP port. */
3750 configASSERT( pxCurrentTCB->uxCriticalNesting == 0 );
3752 if( uxSchedulerSuspended != ( UBaseType_t ) pdFALSE )
3754 /* The scheduler is currently suspended - do not allow a context
3756 xYieldPendings[ xCoreID ] = pdTRUE;
3760 xYieldPendings[ xCoreID ] = pdFALSE;
3761 traceTASK_SWITCHED_OUT();
3763 #if ( configGENERATE_RUN_TIME_STATS == 1 )
3765 #ifdef portALT_GET_RUN_TIME_COUNTER_VALUE
3766 portALT_GET_RUN_TIME_COUNTER_VALUE( ulTotalRunTime );
3768 ulTotalRunTime = portGET_RUN_TIME_COUNTER_VALUE();
3771 /* Add the amount of time the task has been running to the
3772 * accumulated time so far. The time the task started running was
3773 * stored in ulTaskSwitchedInTime. Note that there is no overflow
3774 * protection here so count values are only valid until the timer
3775 * overflows. The guard against negative values is to protect
3776 * against suspect run time stat counter implementations - which
3777 * are provided by the application, not the kernel. */
3778 if( ulTotalRunTime > ulTaskSwitchedInTime )
3780 pxCurrentTCB->ulRunTimeCounter += ( ulTotalRunTime - ulTaskSwitchedInTime );
3784 mtCOVERAGE_TEST_MARKER();
3787 ulTaskSwitchedInTime = ulTotalRunTime;
3789 #endif /* configGENERATE_RUN_TIME_STATS */
3791 /* Check for stack overflow, if configured. */
3792 taskCHECK_FOR_STACK_OVERFLOW();
3794 /* Before the currently running task is switched out, save its errno. */
3795 #if ( configUSE_POSIX_ERRNO == 1 )
3797 pxCurrentTCB->iTaskErrno = FreeRTOS_errno;
3801 /* Select a new task to run using either the generic C or port
3802 * optimised asm code. */
3803 ( void ) prvSelectHighestPriorityTask( xCoreID );
3804 traceTASK_SWITCHED_IN();
3806 /* After the new task is switched in, update the global errno. */
3807 #if ( configUSE_POSIX_ERRNO == 1 )
3809 FreeRTOS_errno = pxCurrentTCB->iTaskErrno;
3813 #if ( configUSE_NEWLIB_REENTRANT == 1 )
3815 /* Switch Newlib's _impure_ptr variable to point to the _reent
3816 * structure specific to this task.
3817 * See the third party link http://www.nadler.com/embedded/newlibAndFreeRTOS.html
3818 * for additional information. */
3819 _impure_ptr = &( pxCurrentTCB->xNewLib_reent );
3821 #endif /* configUSE_NEWLIB_REENTRANT */
3824 portRELEASE_ISR_LOCK();
3825 portRELEASE_TASK_LOCK();
3827 /*-----------------------------------------------------------*/
3829 void vTaskPlaceOnEventList( List_t * const pxEventList,
3830 const TickType_t xTicksToWait )
3832 configASSERT( pxEventList );
3834 /* THIS FUNCTION MUST BE CALLED WITH EITHER INTERRUPTS DISABLED OR THE
3835 * SCHEDULER SUSPENDED AND THE QUEUE BEING ACCESSED LOCKED. */
3837 /* Place the event list item of the TCB in the appropriate event list.
3838 * This is placed in the list in priority order so the highest priority task
3839 * is the first to be woken by the event. The queue that contains the event
3840 * list is locked, preventing simultaneous access from interrupts. */
3841 vListInsert( pxEventList, &( pxCurrentTCB->xEventListItem ) );
3843 prvAddCurrentTaskToDelayedList( xTicksToWait, pdTRUE );
3845 /*-----------------------------------------------------------*/
3847 void vTaskPlaceOnUnorderedEventList( List_t * pxEventList,
3848 const TickType_t xItemValue,
3849 const TickType_t xTicksToWait )
3851 configASSERT( pxEventList );
3853 /* THIS FUNCTION MUST BE CALLED WITH THE SCHEDULER SUSPENDED. It is used by
3854 * the event groups implementation. */
3855 configASSERT( uxSchedulerSuspended != 0 );
3857 /* Store the item value in the event list item. It is safe to access the
3858 * event list item here as interrupts won't access the event list item of a
3859 * task that is not in the Blocked state. */
3860 listSET_LIST_ITEM_VALUE( &( pxCurrentTCB->xEventListItem ), xItemValue | taskEVENT_LIST_ITEM_VALUE_IN_USE );
3862 /* Place the event list item of the TCB at the end of the appropriate event
3863 * list. It is safe to access the event list here because it is part of an
3864 * event group implementation - and interrupts don't access event groups
3865 * directly (instead they access them indirectly by pending function calls to
3866 * the task level). */
3867 vListInsertEnd( pxEventList, &( pxCurrentTCB->xEventListItem ) );
3869 prvAddCurrentTaskToDelayedList( xTicksToWait, pdTRUE );
3871 /*-----------------------------------------------------------*/
3873 #if ( configUSE_TIMERS == 1 )
3875 void vTaskPlaceOnEventListRestricted( List_t * const pxEventList,
3876 TickType_t xTicksToWait,
3877 const BaseType_t xWaitIndefinitely )
3879 configASSERT( pxEventList );
3881 /* This function should not be called by application code hence the
3882 * 'Restricted' in its name. It is not part of the public API. It is
3883 * designed for use by kernel code, and has special calling requirements -
3884 * it should be called with the scheduler suspended. */
3887 /* Place the event list item of the TCB in the appropriate event list.
3888 * In this case it is assume that this is the only task that is going to
3889 * be waiting on this event list, so the faster vListInsertEnd() function
3890 * can be used in place of vListInsert. */
3891 vListInsertEnd( pxEventList, &( pxCurrentTCB->xEventListItem ) );
3893 /* If the task should block indefinitely then set the block time to a
3894 * value that will be recognised as an indefinite delay inside the
3895 * prvAddCurrentTaskToDelayedList() function. */
3896 if( xWaitIndefinitely != pdFALSE )
3898 xTicksToWait = portMAX_DELAY;
3901 traceTASK_DELAY_UNTIL( ( xTickCount + xTicksToWait ) );
3902 prvAddCurrentTaskToDelayedList( xTicksToWait, xWaitIndefinitely );
3905 #endif /* configUSE_TIMERS */
3906 /*-----------------------------------------------------------*/
3908 BaseType_t xTaskRemoveFromEventList( const List_t * const pxEventList )
3910 TCB_t * pxUnblockedTCB;
3913 /* THIS FUNCTION MUST BE CALLED FROM A CRITICAL SECTION. It can also be
3914 * called from a critical section within an ISR. */
3916 /* The event list is sorted in priority order, so the first in the list can
3917 * be removed as it is known to be the highest priority. Remove the TCB from
3918 * the delayed list, and add it to the ready list.
3920 * If an event is for a queue that is locked then this function will never
3921 * get called - the lock count on the queue will get modified instead. This
3922 * means exclusive access to the event list is guaranteed here.
3924 * This function assumes that a check has already been made to ensure that
3925 * pxEventList is not empty. */
3926 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. */
3927 configASSERT( pxUnblockedTCB );
3928 ( void ) uxListRemove( &( pxUnblockedTCB->xEventListItem ) );
3930 if( uxSchedulerSuspended == ( UBaseType_t ) pdFALSE )
3932 ( void ) uxListRemove( &( pxUnblockedTCB->xStateListItem ) );
3933 prvAddTaskToReadyList( pxUnblockedTCB );
3935 #if ( configUSE_TICKLESS_IDLE != 0 )
3937 /* If a task is blocked on a kernel object then xNextTaskUnblockTime
3938 * might be set to the blocked task's time out time. If the task is
3939 * unblocked for a reason other than a timeout xNextTaskUnblockTime is
3940 * normally left unchanged, because it is automatically reset to a new
3941 * value when the tick count equals xNextTaskUnblockTime. However if
3942 * tickless idling is used it might be more important to enter sleep mode
3943 * at the earliest possible time - so reset xNextTaskUnblockTime here to
3944 * ensure it is updated at the earliest possible time. */
3945 prvResetNextTaskUnblockTime();
3951 /* The delayed and ready lists cannot be accessed, so hold this task
3952 * pending until the scheduler is resumed. */
3953 vListInsertEnd( &( xPendingReadyList ), &( pxUnblockedTCB->xEventListItem ) );
3957 #if ( configUSE_PREEMPTION == 1 )
3958 prvYieldForTask( pxUnblockedTCB, pdFALSE );
3960 if( xYieldPendings[ portGET_CORE_ID() ] != pdFALSE )
3968 /*-----------------------------------------------------------*/
3970 void vTaskRemoveFromUnorderedEventList( ListItem_t * pxEventListItem,
3971 const TickType_t xItemValue )
3973 TCB_t * pxUnblockedTCB;
3975 /* THIS FUNCTION MUST BE CALLED WITH THE SCHEDULER SUSPENDED. It is used by
3976 * the event flags implementation. */
3977 configASSERT( uxSchedulerSuspended != pdFALSE );
3979 /* Store the new item value in the event list. */
3980 listSET_LIST_ITEM_VALUE( pxEventListItem, xItemValue | taskEVENT_LIST_ITEM_VALUE_IN_USE );
3982 /* Remove the event list form the event flag. Interrupts do not access
3984 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. */
3985 configASSERT( pxUnblockedTCB );
3986 ( void ) uxListRemove( pxEventListItem );
3988 #if ( configUSE_TICKLESS_IDLE != 0 )
3990 /* If a task is blocked on a kernel object then xNextTaskUnblockTime
3991 * might be set to the blocked task's time out time. If the task is
3992 * unblocked for a reason other than a timeout xNextTaskUnblockTime is
3993 * normally left unchanged, because it is automatically reset to a new
3994 * value when the tick count equals xNextTaskUnblockTime. However if
3995 * tickless idling is used it might be more important to enter sleep mode
3996 * at the earliest possible time - so reset xNextTaskUnblockTime here to
3997 * ensure it is updated at the earliest possible time. */
3998 prvResetNextTaskUnblockTime();
4002 /* Remove the task from the delayed list and add it to the ready list. The
4003 * scheduler is suspended so interrupts will not be accessing the ready
4005 ( void ) uxListRemove( &( pxUnblockedTCB->xStateListItem ) );
4006 prvAddTaskToReadyList( pxUnblockedTCB );
4008 #if ( configUSE_PREEMPTION == 1 )
4009 taskENTER_CRITICAL();
4011 prvYieldForTask( pxUnblockedTCB, pdFALSE );
4013 taskEXIT_CRITICAL();
4016 /*-----------------------------------------------------------*/
4018 void vTaskSetTimeOutState( TimeOut_t * const pxTimeOut )
4020 configASSERT( pxTimeOut );
4021 taskENTER_CRITICAL();
4023 pxTimeOut->xOverflowCount = xNumOfOverflows;
4024 pxTimeOut->xTimeOnEntering = xTickCount;
4026 taskEXIT_CRITICAL();
4028 /*-----------------------------------------------------------*/
4030 void vTaskInternalSetTimeOutState( TimeOut_t * const pxTimeOut )
4032 /* For internal use only as it does not use a critical section. */
4033 pxTimeOut->xOverflowCount = xNumOfOverflows;
4034 pxTimeOut->xTimeOnEntering = xTickCount;
4036 /*-----------------------------------------------------------*/
4038 BaseType_t xTaskCheckForTimeOut( TimeOut_t * const pxTimeOut,
4039 TickType_t * const pxTicksToWait )
4043 configASSERT( pxTimeOut );
4044 configASSERT( pxTicksToWait );
4046 taskENTER_CRITICAL();
4048 /* Minor optimisation. The tick count cannot change in this block. */
4049 const TickType_t xConstTickCount = xTickCount;
4050 const TickType_t xElapsedTime = xConstTickCount - pxTimeOut->xTimeOnEntering;
4052 #if ( INCLUDE_xTaskAbortDelay == 1 )
4053 if( pxCurrentTCB->ucDelayAborted != ( uint8_t ) pdFALSE )
4055 /* The delay was aborted, which is not the same as a time out,
4056 * but has the same result. */
4057 pxCurrentTCB->ucDelayAborted = pdFALSE;
4063 #if ( INCLUDE_vTaskSuspend == 1 )
4064 if( *pxTicksToWait == portMAX_DELAY )
4066 /* If INCLUDE_vTaskSuspend is set to 1 and the block time
4067 * specified is the maximum block time then the task should block
4068 * indefinitely, and therefore never time out. */
4074 if( ( xNumOfOverflows != pxTimeOut->xOverflowCount ) && ( xConstTickCount >= pxTimeOut->xTimeOnEntering ) ) /*lint !e525 Indentation preferred as is to make code within pre-processor directives clearer. */
4076 /* The tick count is greater than the time at which
4077 * vTaskSetTimeout() was called, but has also overflowed since
4078 * vTaskSetTimeOut() was called. It must have wrapped all the way
4079 * around and gone past again. This passed since vTaskSetTimeout()
4082 *pxTicksToWait = ( TickType_t ) 0;
4084 else if( xElapsedTime < *pxTicksToWait ) /*lint !e961 Explicit casting is only redundant with some compilers, whereas others require it to prevent integer conversion errors. */
4086 /* Not a genuine timeout. Adjust parameters for time remaining. */
4087 *pxTicksToWait -= xElapsedTime;
4088 vTaskInternalSetTimeOutState( pxTimeOut );
4093 *pxTicksToWait = ( TickType_t ) 0;
4097 taskEXIT_CRITICAL();
4101 /*-----------------------------------------------------------*/
4103 void vTaskMissedYield( void )
4105 /* Must be called from within a critical section */
4106 xYieldPendings[ portGET_CORE_ID() ] = pdTRUE;
4108 /*-----------------------------------------------------------*/
4110 #if ( configUSE_TRACE_FACILITY == 1 )
4112 UBaseType_t uxTaskGetTaskNumber( TaskHandle_t xTask )
4114 UBaseType_t uxReturn;
4115 TCB_t const * pxTCB;
4120 uxReturn = pxTCB->uxTaskNumber;
4130 #endif /* configUSE_TRACE_FACILITY */
4131 /*-----------------------------------------------------------*/
4133 #if ( configUSE_TRACE_FACILITY == 1 )
4135 void vTaskSetTaskNumber( TaskHandle_t xTask,
4136 const UBaseType_t uxHandle )
4143 pxTCB->uxTaskNumber = uxHandle;
4147 #endif /* configUSE_TRACE_FACILITY */
4150 * -----------------------------------------------------------
4152 * ----------------------------------------------------------
4154 * The portTASK_FUNCTION() macro is used to allow port/compiler specific
4155 * language extensions. The equivalent prototype for this function is:
4157 * void prvIdleTask( void *pvParameters );
4160 static portTASK_FUNCTION( prvIdleTask, pvParameters )
4162 /* Stop warnings. */
4163 ( void ) pvParameters;
4165 /** THIS IS THE RTOS IDLE TASK - WHICH IS CREATED AUTOMATICALLY WHEN THE
4166 * SCHEDULER IS STARTED. **/
4168 /* In case a task that has a secure context deletes itself, in which case
4169 * the idle task is responsible for deleting the task's secure context, if
4171 portALLOCATE_SECURE_CONTEXT( configMINIMAL_SECURE_STACK_SIZE );
4173 /* All cores start up in the idle task. This initial yield gets the application
4179 /* See if any tasks have deleted themselves - if so then the idle task
4180 * is responsible for freeing the deleted task's TCB and stack. */
4181 prvCheckTasksWaitingTermination();
4183 #if ( configUSE_PREEMPTION == 0 )
4185 /* If we are not using preemption we keep forcing a task switch to
4186 * see if any other task has become available. If we are using
4187 * preemption we don't need to do this as any task becoming available
4188 * will automatically get the processor anyway. */
4191 #endif /* configUSE_PREEMPTION */
4193 #if ( ( configUSE_PREEMPTION == 1 ) && ( configIDLE_SHOULD_YIELD == 1 ) )
4195 /* When using preemption tasks of equal priority will be
4196 * timesliced. If a task that is sharing the idle priority is ready
4197 * to run then the idle task should yield before the end of the
4200 * A critical region is not required here as we are just reading from
4201 * the list, and an occasional incorrect value will not matter. If
4202 * the ready list at the idle priority contains one more task than the
4203 * number of idle tasks, which is equal to the configured numbers of cores
4204 * then a task other than the idle task is ready to execute. */
4205 if( listCURRENT_LIST_LENGTH( &( pxReadyTasksLists[ tskIDLE_PRIORITY ] ) ) > ( UBaseType_t ) configNUM_CORES )
4211 mtCOVERAGE_TEST_MARKER();
4214 #endif /* ( ( configUSE_PREEMPTION == 1 ) && ( configIDLE_SHOULD_YIELD == 1 ) ) */
4216 #if ( configUSE_IDLE_HOOK == 1 )
4218 extern void vApplicationIdleHook( void );
4220 /* Call the user defined function from within the idle task. This
4221 * allows the application designer to add background functionality
4222 * without the overhead of a separate task.
4223 * NOTE: vApplicationIdleHook() MUST NOT, UNDER ANY CIRCUMSTANCES,
4224 * CALL A FUNCTION THAT MIGHT BLOCK. */
4225 vApplicationIdleHook();
4227 #endif /* configUSE_IDLE_HOOK */
4229 /* This conditional compilation should use inequality to 0, not equality
4230 * to 1. This is to ensure portSUPPRESS_TICKS_AND_SLEEP() is called when
4231 * user defined low power mode implementations require
4232 * configUSE_TICKLESS_IDLE to be set to a value other than 1. */
4233 #if ( configUSE_TICKLESS_IDLE != 0 )
4235 TickType_t xExpectedIdleTime;
4237 /* It is not desirable to suspend then resume the scheduler on
4238 * each iteration of the idle task. Therefore, a preliminary
4239 * test of the expected idle time is performed without the
4240 * scheduler suspended. The result here is not necessarily
4242 xExpectedIdleTime = prvGetExpectedIdleTime();
4244 if( xExpectedIdleTime >= configEXPECTED_IDLE_TIME_BEFORE_SLEEP )
4248 /* Now the scheduler is suspended, the expected idle
4249 * time can be sampled again, and this time its value can
4251 configASSERT( xNextTaskUnblockTime >= xTickCount );
4252 xExpectedIdleTime = prvGetExpectedIdleTime();
4254 /* Define the following macro to set xExpectedIdleTime to 0
4255 * if the application does not want
4256 * portSUPPRESS_TICKS_AND_SLEEP() to be called. */
4257 configPRE_SUPPRESS_TICKS_AND_SLEEP_PROCESSING( xExpectedIdleTime );
4259 if( xExpectedIdleTime >= configEXPECTED_IDLE_TIME_BEFORE_SLEEP )
4261 traceLOW_POWER_IDLE_BEGIN();
4262 portSUPPRESS_TICKS_AND_SLEEP( xExpectedIdleTime );
4263 traceLOW_POWER_IDLE_END();
4267 mtCOVERAGE_TEST_MARKER();
4270 ( void ) xTaskResumeAll();
4274 mtCOVERAGE_TEST_MARKER();
4277 #endif /* configUSE_TICKLESS_IDLE */
4280 /*-----------------------------------------------------------*/
4282 #if ( configUSE_TICKLESS_IDLE != 0 )
4284 eSleepModeStatus eTaskConfirmSleepModeStatus( void )
4286 /* The idle task exists in addition to the application tasks. */
4287 const UBaseType_t uxNonApplicationTasks = 1;
4288 eSleepModeStatus eReturn = eStandardSleep;
4290 /* This function must be called from a critical section. */
4292 if( listCURRENT_LIST_LENGTH( &xPendingReadyList ) != 0 )
4294 /* A task was made ready while the scheduler was suspended. */
4295 eReturn = eAbortSleep;
4297 else if( xYieldPending != pdFALSE )
4299 /* A yield was pended while the scheduler was suspended. */
4300 eReturn = eAbortSleep;
4302 else if( xPendedTicks != 0 )
4304 /* A tick interrupt has already occurred but was held pending
4305 * because the scheduler is suspended. */
4306 eReturn = eAbortSleep;
4310 /* If all the tasks are in the suspended list (which might mean they
4311 * have an infinite block time rather than actually being suspended)
4312 * then it is safe to turn all clocks off and just wait for external
4314 if( listCURRENT_LIST_LENGTH( &xSuspendedTaskList ) == ( uxCurrentNumberOfTasks - uxNonApplicationTasks ) )
4316 eReturn = eNoTasksWaitingTimeout;
4320 mtCOVERAGE_TEST_MARKER();
4327 #endif /* configUSE_TICKLESS_IDLE */
4328 /*-----------------------------------------------------------*/
4330 #if ( configNUM_THREAD_LOCAL_STORAGE_POINTERS != 0 )
4332 void vTaskSetThreadLocalStoragePointer( TaskHandle_t xTaskToSet,
4338 if( xIndex < configNUM_THREAD_LOCAL_STORAGE_POINTERS )
4340 pxTCB = prvGetTCBFromHandle( xTaskToSet );
4341 configASSERT( pxTCB != NULL );
4342 pxTCB->pvThreadLocalStoragePointers[ xIndex ] = pvValue;
4346 #endif /* configNUM_THREAD_LOCAL_STORAGE_POINTERS */
4347 /*-----------------------------------------------------------*/
4349 #if ( configNUM_THREAD_LOCAL_STORAGE_POINTERS != 0 )
4351 void * pvTaskGetThreadLocalStoragePointer( TaskHandle_t xTaskToQuery,
4354 void * pvReturn = NULL;
4357 if( xIndex < configNUM_THREAD_LOCAL_STORAGE_POINTERS )
4359 pxTCB = prvGetTCBFromHandle( xTaskToQuery );
4360 pvReturn = pxTCB->pvThreadLocalStoragePointers[ xIndex ];
4370 #endif /* configNUM_THREAD_LOCAL_STORAGE_POINTERS */
4371 /*-----------------------------------------------------------*/
4373 #if ( portUSING_MPU_WRAPPERS == 1 )
4375 void vTaskAllocateMPURegions( TaskHandle_t xTaskToModify,
4376 const MemoryRegion_t * const xRegions )
4380 /* If null is passed in here then we are modifying the MPU settings of
4381 * the calling task. */
4382 pxTCB = prvGetTCBFromHandle( xTaskToModify );
4384 vPortStoreTaskMPUSettings( &( pxTCB->xMPUSettings ), xRegions, NULL, 0 );
4387 #endif /* portUSING_MPU_WRAPPERS */
4388 /*-----------------------------------------------------------*/
4390 static void prvInitialiseTaskLists( void )
4392 UBaseType_t uxPriority;
4394 for( uxPriority = ( UBaseType_t ) 0U; uxPriority < ( UBaseType_t ) configMAX_PRIORITIES; uxPriority++ )
4396 vListInitialise( &( pxReadyTasksLists[ uxPriority ] ) );
4399 vListInitialise( &xDelayedTaskList1 );
4400 vListInitialise( &xDelayedTaskList2 );
4401 vListInitialise( &xPendingReadyList );
4403 #if ( INCLUDE_vTaskDelete == 1 )
4405 vListInitialise( &xTasksWaitingTermination );
4407 #endif /* INCLUDE_vTaskDelete */
4409 #if ( INCLUDE_vTaskSuspend == 1 )
4411 vListInitialise( &xSuspendedTaskList );
4413 #endif /* INCLUDE_vTaskSuspend */
4415 /* Start with pxDelayedTaskList using list1 and the pxOverflowDelayedTaskList
4417 pxDelayedTaskList = &xDelayedTaskList1;
4418 pxOverflowDelayedTaskList = &xDelayedTaskList2;
4420 /*-----------------------------------------------------------*/
4422 static void prvCheckTasksWaitingTermination( void )
4424 /** THIS FUNCTION IS CALLED FROM THE RTOS IDLE TASK **/
4426 #if ( INCLUDE_vTaskDelete == 1 )
4430 /* uxDeletedTasksWaitingCleanUp is used to prevent taskENTER_CRITICAL()
4431 * being called too often in the idle task. */
4432 while( uxDeletedTasksWaitingCleanUp > ( UBaseType_t ) 0U )
4434 taskENTER_CRITICAL();
4436 /* Since we are SMP, multiple idles can be running simultaneously
4437 * and we need to check that other idles did not cleanup while we were
4438 * waiting to enter the critical section */
4439 if( uxDeletedTasksWaitingCleanUp > ( UBaseType_t ) 0U )
4441 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. */
4443 if( pxTCB->xTaskRunState == taskTASK_NOT_RUNNING )
4445 ( void ) uxListRemove( &( pxTCB->xStateListItem ) );
4446 --uxCurrentNumberOfTasks;
4447 --uxDeletedTasksWaitingCleanUp;
4448 prvDeleteTCB( pxTCB );
4452 /* The TCB to be deleted still has not yet been switched out
4453 * by the scheduler, so we will just exit this loop early and
4454 * try again next time. */
4455 taskEXIT_CRITICAL();
4460 taskEXIT_CRITICAL();
4463 #endif /* INCLUDE_vTaskDelete */
4465 /*-----------------------------------------------------------*/
4467 #if ( configUSE_TRACE_FACILITY == 1 )
4469 void vTaskGetInfo( TaskHandle_t xTask,
4470 TaskStatus_t * pxTaskStatus,
4471 BaseType_t xGetFreeStackSpace,
4476 /* xTask is NULL then get the state of the calling task. */
4477 pxTCB = prvGetTCBFromHandle( xTask );
4479 pxTaskStatus->xHandle = ( TaskHandle_t ) pxTCB;
4480 pxTaskStatus->pcTaskName = ( const char * ) &( pxTCB->pcTaskName[ 0 ] );
4481 pxTaskStatus->uxCurrentPriority = pxTCB->uxPriority;
4482 pxTaskStatus->pxStackBase = pxTCB->pxStack;
4483 pxTaskStatus->xTaskNumber = pxTCB->uxTCBNumber;
4485 #if ( configUSE_MUTEXES == 1 )
4487 pxTaskStatus->uxBasePriority = pxTCB->uxBasePriority;
4491 pxTaskStatus->uxBasePriority = 0;
4495 #if ( configGENERATE_RUN_TIME_STATS == 1 )
4497 pxTaskStatus->ulRunTimeCounter = pxTCB->ulRunTimeCounter;
4501 pxTaskStatus->ulRunTimeCounter = 0;
4505 /* Obtaining the task state is a little fiddly, so is only done if the
4506 * value of eState passed into this function is eInvalid - otherwise the
4507 * state is just set to whatever is passed in. */
4508 if( eState != eInvalid )
4510 if( taskTASK_IS_RUNNING( pxTCB->xTaskRunState ) )
4512 pxTaskStatus->eCurrentState = eRunning;
4516 pxTaskStatus->eCurrentState = eState;
4518 #if ( INCLUDE_vTaskSuspend == 1 )
4520 /* If the task is in the suspended list then there is a
4521 * chance it is actually just blocked indefinitely - so really
4522 * it should be reported as being in the Blocked state. */
4523 if( eState == eSuspended )
4527 if( listLIST_ITEM_CONTAINER( &( pxTCB->xEventListItem ) ) != NULL )
4529 pxTaskStatus->eCurrentState = eBlocked;
4532 ( void ) xTaskResumeAll();
4535 #endif /* INCLUDE_vTaskSuspend */
4540 pxTaskStatus->eCurrentState = eTaskGetState( pxTCB );
4543 /* Obtaining the stack space takes some time, so the xGetFreeStackSpace
4544 * parameter is provided to allow it to be skipped. */
4545 if( xGetFreeStackSpace != pdFALSE )
4547 #if ( portSTACK_GROWTH > 0 )
4549 pxTaskStatus->usStackHighWaterMark = prvTaskCheckFreeStackSpace( ( uint8_t * ) pxTCB->pxEndOfStack );
4553 pxTaskStatus->usStackHighWaterMark = prvTaskCheckFreeStackSpace( ( uint8_t * ) pxTCB->pxStack );
4559 pxTaskStatus->usStackHighWaterMark = 0;
4563 #endif /* configUSE_TRACE_FACILITY */
4564 /*-----------------------------------------------------------*/
4566 #if ( configUSE_TRACE_FACILITY == 1 )
4568 static UBaseType_t prvListTasksWithinSingleList( TaskStatus_t * pxTaskStatusArray,
4572 configLIST_VOLATILE TCB_t * pxNextTCB, * pxFirstTCB;
4573 UBaseType_t uxTask = 0;
4575 if( listCURRENT_LIST_LENGTH( pxList ) > ( UBaseType_t ) 0 )
4577 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. */
4579 /* Populate an TaskStatus_t structure within the
4580 * pxTaskStatusArray array for each task that is referenced from
4581 * pxList. See the definition of TaskStatus_t in task.h for the
4582 * meaning of each TaskStatus_t structure member. */
4585 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. */
4586 vTaskGetInfo( ( TaskHandle_t ) pxNextTCB, &( pxTaskStatusArray[ uxTask ] ), pdTRUE, eState );
4588 } while( pxNextTCB != pxFirstTCB );
4592 mtCOVERAGE_TEST_MARKER();
4598 #endif /* configUSE_TRACE_FACILITY */
4599 /*-----------------------------------------------------------*/
4601 #if ( ( configUSE_TRACE_FACILITY == 1 ) || ( INCLUDE_uxTaskGetStackHighWaterMark == 1 ) || ( INCLUDE_uxTaskGetStackHighWaterMark2 == 1 ) )
4603 static configSTACK_DEPTH_TYPE prvTaskCheckFreeStackSpace( const uint8_t * pucStackByte )
4605 uint32_t ulCount = 0U;
4607 while( *pucStackByte == ( uint8_t ) tskSTACK_FILL_BYTE )
4609 pucStackByte -= portSTACK_GROWTH;
4613 ulCount /= ( uint32_t ) sizeof( StackType_t ); /*lint !e961 Casting is not redundant on smaller architectures. */
4615 return ( configSTACK_DEPTH_TYPE ) ulCount;
4618 #endif /* ( ( configUSE_TRACE_FACILITY == 1 ) || ( INCLUDE_uxTaskGetStackHighWaterMark == 1 ) || ( INCLUDE_uxTaskGetStackHighWaterMark2 == 1 ) ) */
4619 /*-----------------------------------------------------------*/
4621 #if ( INCLUDE_uxTaskGetStackHighWaterMark2 == 1 )
4623 /* uxTaskGetStackHighWaterMark() and uxTaskGetStackHighWaterMark2() are the
4624 * same except for their return type. Using configSTACK_DEPTH_TYPE allows the
4625 * user to determine the return type. It gets around the problem of the value
4626 * overflowing on 8-bit types without breaking backward compatibility for
4627 * applications that expect an 8-bit return type. */
4628 configSTACK_DEPTH_TYPE uxTaskGetStackHighWaterMark2( TaskHandle_t xTask )
4631 uint8_t * pucEndOfStack;
4632 configSTACK_DEPTH_TYPE uxReturn;
4634 /* uxTaskGetStackHighWaterMark() and uxTaskGetStackHighWaterMark2() are
4635 * the same except for their return type. Using configSTACK_DEPTH_TYPE
4636 * allows the user to determine the return type. It gets around the
4637 * problem of the value overflowing on 8-bit types without breaking
4638 * backward compatibility for applications that expect an 8-bit return
4641 pxTCB = prvGetTCBFromHandle( xTask );
4643 #if portSTACK_GROWTH < 0
4645 pucEndOfStack = ( uint8_t * ) pxTCB->pxStack;
4649 pucEndOfStack = ( uint8_t * ) pxTCB->pxEndOfStack;
4653 uxReturn = prvTaskCheckFreeStackSpace( pucEndOfStack );
4658 #endif /* INCLUDE_uxTaskGetStackHighWaterMark2 */
4659 /*-----------------------------------------------------------*/
4661 #if ( INCLUDE_uxTaskGetStackHighWaterMark == 1 )
4663 UBaseType_t uxTaskGetStackHighWaterMark( TaskHandle_t xTask )
4666 uint8_t * pucEndOfStack;
4667 UBaseType_t uxReturn;
4669 pxTCB = prvGetTCBFromHandle( xTask );
4671 #if portSTACK_GROWTH < 0
4673 pucEndOfStack = ( uint8_t * ) pxTCB->pxStack;
4677 pucEndOfStack = ( uint8_t * ) pxTCB->pxEndOfStack;
4681 uxReturn = ( UBaseType_t ) prvTaskCheckFreeStackSpace( pucEndOfStack );
4686 #endif /* INCLUDE_uxTaskGetStackHighWaterMark */
4687 /*-----------------------------------------------------------*/
4689 #if ( INCLUDE_vTaskDelete == 1 )
4691 static void prvDeleteTCB( TCB_t * pxTCB )
4693 /* This call is required specifically for the TriCore port. It must be
4694 * above the vPortFree() calls. The call is also used by ports/demos that
4695 * want to allocate and clean RAM statically. */
4696 portCLEAN_UP_TCB( pxTCB );
4698 /* Free up the memory allocated by the scheduler for the task. It is up
4699 * to the task to free any memory allocated at the application level.
4700 * See the third party link http://www.nadler.com/embedded/newlibAndFreeRTOS.html
4701 * for additional information. */
4702 #if ( configUSE_NEWLIB_REENTRANT == 1 )
4704 _reclaim_reent( &( pxTCB->xNewLib_reent ) );
4706 #endif /* configUSE_NEWLIB_REENTRANT */
4708 #if ( ( configSUPPORT_DYNAMIC_ALLOCATION == 1 ) && ( configSUPPORT_STATIC_ALLOCATION == 0 ) && ( portUSING_MPU_WRAPPERS == 0 ) )
4710 /* The task can only have been allocated dynamically - free both
4711 * the stack and TCB. */
4712 vPortFreeStack( pxTCB->pxStack );
4715 #elif ( tskSTATIC_AND_DYNAMIC_ALLOCATION_POSSIBLE != 0 ) /*lint !e731 !e9029 Macro has been consolidated for readability reasons. */
4717 /* The task could have been allocated statically or dynamically, so
4718 * check what was statically allocated before trying to free the
4720 if( pxTCB->ucStaticallyAllocated == tskDYNAMICALLY_ALLOCATED_STACK_AND_TCB )
4722 /* Both the stack and TCB were allocated dynamically, so both
4724 vPortFreeStack( pxTCB->pxStack );
4727 else if( pxTCB->ucStaticallyAllocated == tskSTATICALLY_ALLOCATED_STACK_ONLY )
4729 /* Only the stack was statically allocated, so the TCB is the
4730 * only memory that must be freed. */
4735 /* Neither the stack nor the TCB were allocated dynamically, so
4736 * nothing needs to be freed. */
4737 configASSERT( pxTCB->ucStaticallyAllocated == tskSTATICALLY_ALLOCATED_STACK_AND_TCB );
4738 mtCOVERAGE_TEST_MARKER();
4741 #endif /* configSUPPORT_DYNAMIC_ALLOCATION */
4744 #endif /* INCLUDE_vTaskDelete */
4745 /*-----------------------------------------------------------*/
4747 static void prvResetNextTaskUnblockTime( void )
4749 if( listLIST_IS_EMPTY( pxDelayedTaskList ) != pdFALSE )
4751 /* The new current delayed list is empty. Set xNextTaskUnblockTime to
4752 * the maximum possible value so it is extremely unlikely that the
4753 * if( xTickCount >= xNextTaskUnblockTime ) test will pass until
4754 * there is an item in the delayed list. */
4755 xNextTaskUnblockTime = portMAX_DELAY;
4759 /* The new current delayed list is not empty, get the value of
4760 * the item at the head of the delayed list. This is the time at
4761 * which the task at the head of the delayed list should be removed
4762 * from the Blocked state. */
4763 xNextTaskUnblockTime = listGET_ITEM_VALUE_OF_HEAD_ENTRY( pxDelayedTaskList );
4766 /*-----------------------------------------------------------*/
4768 #if ( ( INCLUDE_xTaskGetCurrentTaskHandle == 1 ) || ( configUSE_MUTEXES == 1 ) )
4770 TaskHandle_t xTaskGetCurrentTaskHandle( void )
4772 TaskHandle_t xReturn;
4775 ulState = portDISABLE_INTERRUPTS();
4776 xReturn = pxCurrentTCBs[ portGET_CORE_ID() ];
4777 portRESTORE_INTERRUPTS( ulState );
4782 TaskHandle_t xTaskGetCurrentTaskHandleCPU( UBaseType_t xCoreID )
4784 TaskHandle_t xReturn = NULL;
4786 if( taskVALID_CORE_ID( xCoreID ) != pdFALSE )
4788 xReturn = pxCurrentTCBs[ xCoreID ];
4794 #endif /* ( ( INCLUDE_xTaskGetCurrentTaskHandle == 1 ) || ( configUSE_MUTEXES == 1 ) ) */
4795 /*-----------------------------------------------------------*/
4797 #if ( ( INCLUDE_xTaskGetSchedulerState == 1 ) || ( configUSE_TIMERS == 1 ) )
4799 BaseType_t xTaskGetSchedulerState( void )
4803 if( xSchedulerRunning == pdFALSE )
4805 xReturn = taskSCHEDULER_NOT_STARTED;
4809 taskENTER_CRITICAL();
4811 if( uxSchedulerSuspended == ( UBaseType_t ) pdFALSE )
4813 xReturn = taskSCHEDULER_RUNNING;
4817 xReturn = taskSCHEDULER_SUSPENDED;
4820 taskEXIT_CRITICAL();
4826 #endif /* ( ( INCLUDE_xTaskGetSchedulerState == 1 ) || ( configUSE_TIMERS == 1 ) ) */
4827 /*-----------------------------------------------------------*/
4829 #if ( configUSE_MUTEXES == 1 )
4831 BaseType_t xTaskPriorityInherit( TaskHandle_t const pxMutexHolder )
4833 TCB_t * const pxMutexHolderTCB = pxMutexHolder;
4834 BaseType_t xReturn = pdFALSE;
4836 /* If the mutex was given back by an interrupt while the queue was
4837 * locked then the mutex holder might now be NULL. _RB_ Is this still
4838 * needed as interrupts can no longer use mutexes? */
4839 if( pxMutexHolder != NULL )
4841 /* If the holder of the mutex has a priority below the priority of
4842 * the task attempting to obtain the mutex then it will temporarily
4843 * inherit the priority of the task attempting to obtain the mutex. */
4844 if( pxMutexHolderTCB->uxPriority < pxCurrentTCB->uxPriority )
4846 /* Adjust the mutex holder state to account for its new
4847 * priority. Only reset the event list item value if the value is
4848 * not being used for anything else. */
4849 if( ( listGET_LIST_ITEM_VALUE( &( pxMutexHolderTCB->xEventListItem ) ) & taskEVENT_LIST_ITEM_VALUE_IN_USE ) == 0UL )
4851 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. */
4855 mtCOVERAGE_TEST_MARKER();
4858 /* If the task being modified is in the ready state it will need
4859 * to be moved into a new list. */
4860 if( listIS_CONTAINED_WITHIN( &( pxReadyTasksLists[ pxMutexHolderTCB->uxPriority ] ), &( pxMutexHolderTCB->xStateListItem ) ) != pdFALSE )
4862 if( uxListRemove( &( pxMutexHolderTCB->xStateListItem ) ) == ( UBaseType_t ) 0 )
4864 /* It is known that the task is in its ready list so
4865 * there is no need to check again and the port level
4866 * reset macro can be called directly. */
4867 portRESET_READY_PRIORITY( pxMutexHolderTCB->uxPriority, uxTopReadyPriority );
4871 mtCOVERAGE_TEST_MARKER();
4874 /* Inherit the priority before being moved into the new list. */
4875 pxMutexHolderTCB->uxPriority = pxCurrentTCB->uxPriority;
4876 prvAddTaskToReadyList( pxMutexHolderTCB );
4880 /* Just inherit the priority. */
4881 pxMutexHolderTCB->uxPriority = pxCurrentTCB->uxPriority;
4884 traceTASK_PRIORITY_INHERIT( pxMutexHolderTCB, pxCurrentTCB->uxPriority );
4886 /* Inheritance occurred. */
4891 if( pxMutexHolderTCB->uxBasePriority < pxCurrentTCB->uxPriority )
4893 /* The base priority of the mutex holder is lower than the
4894 * priority of the task attempting to take the mutex, but the
4895 * current priority of the mutex holder is not lower than the
4896 * priority of the task attempting to take the mutex.
4897 * Therefore the mutex holder must have already inherited a
4898 * priority, but inheritance would have occurred if that had
4899 * not been the case. */
4904 mtCOVERAGE_TEST_MARKER();
4910 mtCOVERAGE_TEST_MARKER();
4916 #endif /* configUSE_MUTEXES */
4917 /*-----------------------------------------------------------*/
4919 #if ( configUSE_MUTEXES == 1 )
4921 BaseType_t xTaskPriorityDisinherit( TaskHandle_t const pxMutexHolder )
4923 TCB_t * const pxTCB = pxMutexHolder;
4924 BaseType_t xReturn = pdFALSE;
4926 if( pxMutexHolder != NULL )
4928 /* A task can only have an inherited priority if it holds the mutex.
4929 * If the mutex is held by a task then it cannot be given from an
4930 * interrupt, and if a mutex is given by the holding task then it must
4931 * be the running state task. */
4932 configASSERT( pxTCB == pxCurrentTCB );
4933 configASSERT( pxTCB->uxMutexesHeld );
4934 ( pxTCB->uxMutexesHeld )--;
4936 /* Has the holder of the mutex inherited the priority of another
4938 if( pxTCB->uxPriority != pxTCB->uxBasePriority )
4940 /* Only disinherit if no other mutexes are held. */
4941 if( pxTCB->uxMutexesHeld == ( UBaseType_t ) 0 )
4943 /* A task can only have an inherited priority if it holds
4944 * the mutex. If the mutex is held by a task then it cannot be
4945 * given from an interrupt, and if a mutex is given by the
4946 * holding task then it must be the running state task. Remove
4947 * the holding task from the ready list. */
4948 if( uxListRemove( &( pxTCB->xStateListItem ) ) == ( UBaseType_t ) 0 )
4950 portRESET_READY_PRIORITY( pxTCB->uxPriority, uxTopReadyPriority );
4954 mtCOVERAGE_TEST_MARKER();
4957 /* Disinherit the priority before adding the task into the
4958 * new ready list. */
4959 traceTASK_PRIORITY_DISINHERIT( pxTCB, pxTCB->uxBasePriority );
4960 pxTCB->uxPriority = pxTCB->uxBasePriority;
4962 /* Reset the event list item value. It cannot be in use for
4963 * any other purpose if this task is running, and it must be
4964 * running to give back the mutex. */
4965 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. */
4966 prvAddTaskToReadyList( pxTCB );
4968 /* Return true to indicate that a context switch is required.
4969 * This is only actually required in the corner case whereby
4970 * multiple mutexes were held and the mutexes were given back
4971 * in an order different to that in which they were taken.
4972 * If a context switch did not occur when the first mutex was
4973 * returned, even if a task was waiting on it, then a context
4974 * switch should occur when the last mutex is returned whether
4975 * a task is waiting on it or not. */
4980 mtCOVERAGE_TEST_MARKER();
4985 mtCOVERAGE_TEST_MARKER();
4990 mtCOVERAGE_TEST_MARKER();
4996 #endif /* configUSE_MUTEXES */
4997 /*-----------------------------------------------------------*/
4999 #if ( configUSE_MUTEXES == 1 )
5001 void vTaskPriorityDisinheritAfterTimeout( TaskHandle_t const pxMutexHolder,
5002 UBaseType_t uxHighestPriorityWaitingTask )
5004 TCB_t * const pxTCB = pxMutexHolder;
5005 UBaseType_t uxPriorityUsedOnEntry, uxPriorityToUse;
5006 const UBaseType_t uxOnlyOneMutexHeld = ( UBaseType_t ) 1;
5008 if( pxMutexHolder != NULL )
5010 /* If pxMutexHolder is not NULL then the holder must hold at least
5012 configASSERT( pxTCB->uxMutexesHeld );
5014 /* Determine the priority to which the priority of the task that
5015 * holds the mutex should be set. This will be the greater of the
5016 * holding task's base priority and the priority of the highest
5017 * priority task that is waiting to obtain the mutex. */
5018 if( pxTCB->uxBasePriority < uxHighestPriorityWaitingTask )
5020 uxPriorityToUse = uxHighestPriorityWaitingTask;
5024 uxPriorityToUse = pxTCB->uxBasePriority;
5027 /* Does the priority need to change? */
5028 if( pxTCB->uxPriority != uxPriorityToUse )
5030 /* Only disinherit if no other mutexes are held. This is a
5031 * simplification in the priority inheritance implementation. If
5032 * the task that holds the mutex is also holding other mutexes then
5033 * the other mutexes may have caused the priority inheritance. */
5034 if( pxTCB->uxMutexesHeld == uxOnlyOneMutexHeld )
5036 /* If a task has timed out because it already holds the
5037 * mutex it was trying to obtain then it cannot of inherited
5038 * its own priority. */
5039 configASSERT( pxTCB != pxCurrentTCB );
5041 /* Disinherit the priority, remembering the previous
5042 * priority to facilitate determining the subject task's
5044 traceTASK_PRIORITY_DISINHERIT( pxTCB, uxPriorityToUse );
5045 uxPriorityUsedOnEntry = pxTCB->uxPriority;
5046 pxTCB->uxPriority = uxPriorityToUse;
5048 /* Only reset the event list item value if the value is not
5049 * being used for anything else. */
5050 if( ( listGET_LIST_ITEM_VALUE( &( pxTCB->xEventListItem ) ) & taskEVENT_LIST_ITEM_VALUE_IN_USE ) == 0UL )
5052 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. */
5056 mtCOVERAGE_TEST_MARKER();
5059 /* If the running task is not the task that holds the mutex
5060 * then the task that holds the mutex could be in either the
5061 * Ready, Blocked or Suspended states. Only remove the task
5062 * from its current state list if it is in the Ready state as
5063 * the task's priority is going to change and there is one
5064 * Ready list per priority. */
5065 if( listIS_CONTAINED_WITHIN( &( pxReadyTasksLists[ uxPriorityUsedOnEntry ] ), &( pxTCB->xStateListItem ) ) != pdFALSE )
5067 if( uxListRemove( &( pxTCB->xStateListItem ) ) == ( UBaseType_t ) 0 )
5069 /* It is known that the task is in its ready list so
5070 * there is no need to check again and the port level
5071 * reset macro can be called directly. */
5072 portRESET_READY_PRIORITY( pxTCB->uxPriority, uxTopReadyPriority );
5076 mtCOVERAGE_TEST_MARKER();
5079 prvAddTaskToReadyList( pxTCB );
5083 mtCOVERAGE_TEST_MARKER();
5088 mtCOVERAGE_TEST_MARKER();
5093 mtCOVERAGE_TEST_MARKER();
5098 mtCOVERAGE_TEST_MARKER();
5102 #endif /* configUSE_MUTEXES */
5103 /*-----------------------------------------------------------*/
5106 * If not in a critical section then yield immediately.
5107 * Otherwise set xYieldPending to true to wait to
5108 * yield until exiting the critical section.
5110 void vTaskYieldWithinAPI( void )
5112 if( pxCurrentTCB->uxCriticalNesting == 0U )
5118 xYieldPendings[ portGET_CORE_ID() ] = pdTRUE;
5121 /*-----------------------------------------------------------*/
5123 #if ( portCRITICAL_NESTING_IN_TCB == 1 )
5125 void vTaskEnterCritical( void )
5127 portDISABLE_INTERRUPTS();
5129 if( xSchedulerRunning != pdFALSE )
5131 if( pxCurrentTCB->uxCriticalNesting == 0U )
5133 if( portCHECK_IF_IN_ISR() == pdFALSE )
5135 portGET_TASK_LOCK();
5141 ( pxCurrentTCB->uxCriticalNesting )++;
5143 /* This should now be interrupt safe. The only time there would be
5144 * a problem is if this is called before a context switch and
5145 * vTaskExitCritical() is called after pxCurrentTCB changes. Therefore
5146 * this should not be used within vTaskSwitchContext(). */
5148 if( ( uxSchedulerSuspended == 0U ) && ( pxCurrentTCB->uxCriticalNesting == 1U ) )
5150 prvCheckForRunStateChange();
5155 mtCOVERAGE_TEST_MARKER();
5159 #endif /* portCRITICAL_NESTING_IN_TCB */
5160 /*-----------------------------------------------------------*/
5162 #if ( portCRITICAL_NESTING_IN_TCB == 1 )
5164 void vTaskExitCritical( void )
5166 if( xSchedulerRunning != pdFALSE )
5168 /* If pxCurrentTCB->uxCriticalNesting is zero then this function
5169 * does not match a previous call to vTaskEnterCritical(). */
5170 configASSERT( pxCurrentTCB->uxCriticalNesting > 0U );
5172 if( pxCurrentTCB->uxCriticalNesting > 0U )
5174 ( pxCurrentTCB->uxCriticalNesting )--;
5176 if( pxCurrentTCB->uxCriticalNesting == 0U )
5178 portRELEASE_ISR_LOCK();
5180 if( portCHECK_IF_IN_ISR() == pdFALSE )
5182 portRELEASE_TASK_LOCK();
5183 portENABLE_INTERRUPTS();
5185 /* When a task yields in a critical section it just sets
5186 * xYieldPending to true. So now that we have exited the
5187 * critical section check if xYieldPending is true, and
5189 if( xYieldPending != pdFALSE )
5196 /* In an ISR we don't hold the task lock and don't
5197 * need to yield. Yield will happen if necessary when
5198 * the application ISR calls portEND_SWITCHING_ISR() */
5199 mtCOVERAGE_TEST_MARKER();
5204 mtCOVERAGE_TEST_MARKER();
5209 mtCOVERAGE_TEST_MARKER();
5214 mtCOVERAGE_TEST_MARKER();
5218 #endif /* portCRITICAL_NESTING_IN_TCB */
5219 /*-----------------------------------------------------------*/
5221 #if ( ( configUSE_TRACE_FACILITY == 1 ) && ( configUSE_STATS_FORMATTING_FUNCTIONS > 0 ) )
5223 static char * prvWriteNameToBuffer( char * pcBuffer,
5224 const char * pcTaskName )
5228 /* Start by copying the entire string. */
5229 strcpy( pcBuffer, pcTaskName );
5231 /* Pad the end of the string with spaces to ensure columns line up when
5233 for( x = strlen( pcBuffer ); x < ( size_t ) ( configMAX_TASK_NAME_LEN - 1 ); x++ )
5235 pcBuffer[ x ] = ' ';
5239 pcBuffer[ x ] = ( char ) 0x00;
5241 /* Return the new end of string. */
5242 return &( pcBuffer[ x ] );
5245 #endif /* ( configUSE_TRACE_FACILITY == 1 ) && ( configUSE_STATS_FORMATTING_FUNCTIONS > 0 ) */
5246 /*-----------------------------------------------------------*/
5248 #if ( ( configUSE_TRACE_FACILITY == 1 ) && ( configUSE_STATS_FORMATTING_FUNCTIONS > 0 ) && ( configSUPPORT_DYNAMIC_ALLOCATION == 1 ) )
5250 void vTaskList( char * pcWriteBuffer )
5252 TaskStatus_t * pxTaskStatusArray;
5253 UBaseType_t uxArraySize, x;
5259 * This function is provided for convenience only, and is used by many
5260 * of the demo applications. Do not consider it to be part of the
5263 * vTaskList() calls uxTaskGetSystemState(), then formats part of the
5264 * uxTaskGetSystemState() output into a human readable table that
5265 * displays task: names, states, priority, stack usage and task number.
5266 * Stack usage specified as the number of unused StackType_t words stack can hold
5267 * on top of stack - not the number of bytes.
5269 * vTaskList() has a dependency on the sprintf() C library function that
5270 * might bloat the code size, use a lot of stack, and provide different
5271 * results on different platforms. An alternative, tiny, third party,
5272 * and limited functionality implementation of sprintf() is provided in
5273 * many of the FreeRTOS/Demo sub-directories in a file called
5274 * printf-stdarg.c (note printf-stdarg.c does not provide a full
5275 * snprintf() implementation!).
5277 * It is recommended that production systems call uxTaskGetSystemState()
5278 * directly to get access to raw stats data, rather than indirectly
5279 * through a call to vTaskList().
5283 /* Make sure the write buffer does not contain a string. */
5284 *pcWriteBuffer = ( char ) 0x00;
5286 /* Take a snapshot of the number of tasks in case it changes while this
5287 * function is executing. */
5288 uxArraySize = uxCurrentNumberOfTasks;
5290 /* Allocate an array index for each task. NOTE! if
5291 * configSUPPORT_DYNAMIC_ALLOCATION is set to 0 then pvPortMalloc() will
5292 * equate to NULL. */
5293 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. */
5295 if( pxTaskStatusArray != NULL )
5297 /* Generate the (binary) data. */
5298 uxArraySize = uxTaskGetSystemState( pxTaskStatusArray, uxArraySize, NULL );
5300 /* Create a human readable table from the binary data. */
5301 for( x = 0; x < uxArraySize; x++ )
5303 switch( pxTaskStatusArray[ x ].eCurrentState )
5306 cStatus = tskRUNNING_CHAR;
5310 cStatus = tskREADY_CHAR;
5314 cStatus = tskBLOCKED_CHAR;
5318 cStatus = tskSUSPENDED_CHAR;
5322 cStatus = tskDELETED_CHAR;
5325 case eInvalid: /* Fall through. */
5326 default: /* Should not get here, but it is included
5327 * to prevent static checking errors. */
5328 cStatus = ( char ) 0x00;
5332 /* Write the task name to the string, padding with spaces so it
5333 * can be printed in tabular form more easily. */
5334 pcWriteBuffer = prvWriteNameToBuffer( pcWriteBuffer, pxTaskStatusArray[ x ].pcTaskName );
5336 /* Write the rest of the string. */
5337 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. */
5338 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. */
5341 /* Free the array again. NOTE! If configSUPPORT_DYNAMIC_ALLOCATION
5342 * is 0 then vPortFree() will be #defined to nothing. */
5343 vPortFree( pxTaskStatusArray );
5347 mtCOVERAGE_TEST_MARKER();
5351 #endif /* ( ( configUSE_TRACE_FACILITY == 1 ) && ( configUSE_STATS_FORMATTING_FUNCTIONS > 0 ) && ( configSUPPORT_DYNAMIC_ALLOCATION == 1 ) ) */
5352 /*----------------------------------------------------------*/
5354 #if ( ( configGENERATE_RUN_TIME_STATS == 1 ) && ( configUSE_STATS_FORMATTING_FUNCTIONS > 0 ) && ( configSUPPORT_DYNAMIC_ALLOCATION == 1 ) )
5356 void vTaskGetRunTimeStats( char * pcWriteBuffer )
5358 TaskStatus_t * pxTaskStatusArray;
5359 UBaseType_t uxArraySize, x;
5360 uint32_t ulTotalTime, ulStatsAsPercentage;
5362 #if ( configUSE_TRACE_FACILITY != 1 )
5364 #error configUSE_TRACE_FACILITY must also be set to 1 in FreeRTOSConfig.h to use vTaskGetRunTimeStats().
5371 * This function is provided for convenience only, and is used by many
5372 * of the demo applications. Do not consider it to be part of the
5375 * vTaskGetRunTimeStats() calls uxTaskGetSystemState(), then formats part
5376 * of the uxTaskGetSystemState() output into a human readable table that
5377 * displays the amount of time each task has spent in the Running state
5378 * in both absolute and percentage terms.
5380 * vTaskGetRunTimeStats() has a dependency on the sprintf() C library
5381 * function that might bloat the code size, use a lot of stack, and
5382 * provide different results on different platforms. An alternative,
5383 * tiny, third party, and limited functionality implementation of
5384 * sprintf() is provided in many of the FreeRTOS/Demo sub-directories in
5385 * a file called printf-stdarg.c (note printf-stdarg.c does not provide
5386 * a full snprintf() implementation!).
5388 * It is recommended that production systems call uxTaskGetSystemState()
5389 * directly to get access to raw stats data, rather than indirectly
5390 * through a call to vTaskGetRunTimeStats().
5393 /* Make sure the write buffer does not contain a string. */
5394 *pcWriteBuffer = ( char ) 0x00;
5396 /* Take a snapshot of the number of tasks in case it changes while this
5397 * function is executing. */
5398 uxArraySize = uxCurrentNumberOfTasks;
5400 /* Allocate an array index for each task. NOTE! If
5401 * configSUPPORT_DYNAMIC_ALLOCATION is set to 0 then pvPortMalloc() will
5402 * equate to NULL. */
5403 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. */
5405 if( pxTaskStatusArray != NULL )
5407 /* Generate the (binary) data. */
5408 uxArraySize = uxTaskGetSystemState( pxTaskStatusArray, uxArraySize, &ulTotalTime );
5410 /* For percentage calculations. */
5411 ulTotalTime /= 100UL;
5413 /* Avoid divide by zero errors. */
5414 if( ulTotalTime > 0UL )
5416 /* Create a human readable table from the binary data. */
5417 for( x = 0; x < uxArraySize; x++ )
5419 /* What percentage of the total run time has the task used?
5420 * This will always be rounded down to the nearest integer.
5421 * ulTotalRunTimeDiv100 has already been divided by 100. */
5422 ulStatsAsPercentage = pxTaskStatusArray[ x ].ulRunTimeCounter / ulTotalTime;
5424 /* Write the task name to the string, padding with
5425 * spaces so it can be printed in tabular form more
5427 pcWriteBuffer = prvWriteNameToBuffer( pcWriteBuffer, pxTaskStatusArray[ x ].pcTaskName );
5429 if( ulStatsAsPercentage > 0UL )
5431 #ifdef portLU_PRINTF_SPECIFIER_REQUIRED
5433 sprintf( pcWriteBuffer, "\t%lu\t\t%lu%%\r\n", pxTaskStatusArray[ x ].ulRunTimeCounter, ulStatsAsPercentage );
5437 /* sizeof( int ) == sizeof( long ) so a smaller
5438 * printf() library can be used. */
5439 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. */
5445 /* If the percentage is zero here then the task has
5446 * consumed less than 1% of the total run time. */
5447 #ifdef portLU_PRINTF_SPECIFIER_REQUIRED
5449 sprintf( pcWriteBuffer, "\t%lu\t\t<1%%\r\n", pxTaskStatusArray[ x ].ulRunTimeCounter );
5453 /* sizeof( int ) == sizeof( long ) so a smaller
5454 * printf() library can be used. */
5455 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. */
5460 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. */
5465 mtCOVERAGE_TEST_MARKER();
5468 /* Free the array again. NOTE! If configSUPPORT_DYNAMIC_ALLOCATION
5469 * is 0 then vPortFree() will be #defined to nothing. */
5470 vPortFree( pxTaskStatusArray );
5474 mtCOVERAGE_TEST_MARKER();
5478 #endif /* ( ( configGENERATE_RUN_TIME_STATS == 1 ) && ( configUSE_STATS_FORMATTING_FUNCTIONS > 0 ) && ( configSUPPORT_STATIC_ALLOCATION == 1 ) ) */
5479 /*-----------------------------------------------------------*/
5481 TickType_t uxTaskResetEventItemValue( void )
5483 TickType_t uxReturn;
5485 uxReturn = listGET_LIST_ITEM_VALUE( &( pxCurrentTCB->xEventListItem ) );
5487 /* Reset the event list item to its normal value - so it can be used with
5488 * queues and semaphores. */
5489 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. */
5493 /*-----------------------------------------------------------*/
5495 #if ( configUSE_MUTEXES == 1 )
5497 TaskHandle_t pvTaskIncrementMutexHeldCount( void )
5499 /* If xSemaphoreCreateMutex() is called before any tasks have been created
5500 * then pxCurrentTCB will be NULL. */
5501 if( pxCurrentTCB != NULL )
5503 ( pxCurrentTCB->uxMutexesHeld )++;
5506 return pxCurrentTCB;
5509 #endif /* configUSE_MUTEXES */
5510 /*-----------------------------------------------------------*/
5512 #if ( configUSE_TASK_NOTIFICATIONS == 1 )
5514 uint32_t ulTaskGenericNotifyTake( UBaseType_t uxIndexToWait,
5515 BaseType_t xClearCountOnExit,
5516 TickType_t xTicksToWait )
5520 configASSERT( uxIndexToWait < configTASK_NOTIFICATION_ARRAY_ENTRIES );
5522 taskENTER_CRITICAL();
5524 /* Only block if the notification count is not already non-zero. */
5525 if( pxCurrentTCB->ulNotifiedValue[ uxIndexToWait ] == 0UL )
5527 /* Mark this task as waiting for a notification. */
5528 pxCurrentTCB->ucNotifyState[ uxIndexToWait ] = taskWAITING_NOTIFICATION;
5530 if( xTicksToWait > ( TickType_t ) 0 )
5532 prvAddCurrentTaskToDelayedList( xTicksToWait, pdTRUE );
5533 traceTASK_NOTIFY_TAKE_BLOCK( uxIndexToWait );
5535 /* All ports are written to allow a yield in a critical
5536 * section (some will yield immediately, others wait until the
5537 * critical section exits) - but it is not something that
5538 * application code should ever do. */
5539 vTaskYieldWithinAPI();
5543 mtCOVERAGE_TEST_MARKER();
5548 mtCOVERAGE_TEST_MARKER();
5551 taskEXIT_CRITICAL();
5553 taskENTER_CRITICAL();
5555 traceTASK_NOTIFY_TAKE( uxIndexToWait );
5556 ulReturn = pxCurrentTCB->ulNotifiedValue[ uxIndexToWait ];
5558 if( ulReturn != 0UL )
5560 if( xClearCountOnExit != pdFALSE )
5562 pxCurrentTCB->ulNotifiedValue[ uxIndexToWait ] = 0UL;
5566 pxCurrentTCB->ulNotifiedValue[ uxIndexToWait ] = ulReturn - ( uint32_t ) 1;
5571 mtCOVERAGE_TEST_MARKER();
5574 pxCurrentTCB->ucNotifyState[ uxIndexToWait ] = taskNOT_WAITING_NOTIFICATION;
5576 taskEXIT_CRITICAL();
5581 #endif /* configUSE_TASK_NOTIFICATIONS */
5582 /*-----------------------------------------------------------*/
5584 #if ( configUSE_TASK_NOTIFICATIONS == 1 )
5586 BaseType_t xTaskGenericNotifyWait( UBaseType_t uxIndexToWait,
5587 uint32_t ulBitsToClearOnEntry,
5588 uint32_t ulBitsToClearOnExit,
5589 uint32_t * pulNotificationValue,
5590 TickType_t xTicksToWait )
5594 configASSERT( uxIndexToWait < configTASK_NOTIFICATION_ARRAY_ENTRIES );
5596 taskENTER_CRITICAL();
5598 /* Only block if a notification is not already pending. */
5599 if( pxCurrentTCB->ucNotifyState[ uxIndexToWait ] != taskNOTIFICATION_RECEIVED )
5601 /* Clear bits in the task's notification value as bits may get
5602 * set by the notifying task or interrupt. This can be used to
5603 * clear the value to zero. */
5604 pxCurrentTCB->ulNotifiedValue[ uxIndexToWait ] &= ~ulBitsToClearOnEntry;
5606 /* Mark this task as waiting for a notification. */
5607 pxCurrentTCB->ucNotifyState[ uxIndexToWait ] = taskWAITING_NOTIFICATION;
5609 if( xTicksToWait > ( TickType_t ) 0 )
5611 prvAddCurrentTaskToDelayedList( xTicksToWait, pdTRUE );
5612 traceTASK_NOTIFY_WAIT_BLOCK( uxIndexToWait );
5614 /* All ports are written to allow a yield in a critical
5615 * section (some will yield immediately, others wait until the
5616 * critical section exits) - but it is not something that
5617 * application code should ever do. */
5618 vTaskYieldWithinAPI();
5622 mtCOVERAGE_TEST_MARKER();
5627 mtCOVERAGE_TEST_MARKER();
5630 taskEXIT_CRITICAL();
5632 taskENTER_CRITICAL();
5634 traceTASK_NOTIFY_WAIT( uxIndexToWait );
5636 if( pulNotificationValue != NULL )
5638 /* Output the current notification value, which may or may not
5640 *pulNotificationValue = pxCurrentTCB->ulNotifiedValue[ uxIndexToWait ];
5643 /* If ucNotifyValue is set then either the task never entered the
5644 * blocked state (because a notification was already pending) or the
5645 * task unblocked because of a notification. Otherwise the task
5646 * unblocked because of a timeout. */
5647 if( pxCurrentTCB->ucNotifyState[ uxIndexToWait ] != taskNOTIFICATION_RECEIVED )
5649 /* A notification was not received. */
5654 /* A notification was already pending or a notification was
5655 * received while the task was waiting. */
5656 pxCurrentTCB->ulNotifiedValue[ uxIndexToWait ] &= ~ulBitsToClearOnExit;
5660 pxCurrentTCB->ucNotifyState[ uxIndexToWait ] = taskNOT_WAITING_NOTIFICATION;
5662 taskEXIT_CRITICAL();
5667 #endif /* configUSE_TASK_NOTIFICATIONS */
5668 /*-----------------------------------------------------------*/
5670 #if ( configUSE_TASK_NOTIFICATIONS == 1 )
5672 BaseType_t xTaskGenericNotify( TaskHandle_t xTaskToNotify,
5673 UBaseType_t uxIndexToNotify,
5675 eNotifyAction eAction,
5676 uint32_t * pulPreviousNotificationValue )
5679 BaseType_t xReturn = pdPASS;
5680 uint8_t ucOriginalNotifyState;
5682 configASSERT( uxIndexToNotify < configTASK_NOTIFICATION_ARRAY_ENTRIES );
5683 configASSERT( xTaskToNotify );
5684 pxTCB = xTaskToNotify;
5686 taskENTER_CRITICAL();
5688 if( pulPreviousNotificationValue != NULL )
5690 *pulPreviousNotificationValue = pxTCB->ulNotifiedValue[ uxIndexToNotify ];
5693 ucOriginalNotifyState = pxTCB->ucNotifyState[ uxIndexToNotify ];
5695 pxTCB->ucNotifyState[ uxIndexToNotify ] = taskNOTIFICATION_RECEIVED;
5700 pxTCB->ulNotifiedValue[ uxIndexToNotify ] |= ulValue;
5704 ( pxTCB->ulNotifiedValue[ uxIndexToNotify ] )++;
5707 case eSetValueWithOverwrite:
5708 pxTCB->ulNotifiedValue[ uxIndexToNotify ] = ulValue;
5711 case eSetValueWithoutOverwrite:
5713 if( ucOriginalNotifyState != taskNOTIFICATION_RECEIVED )
5715 pxTCB->ulNotifiedValue[ uxIndexToNotify ] = ulValue;
5719 /* The value could not be written to the task. */
5727 /* The task is being notified without its notify value being
5733 /* Should not get here if all enums are handled.
5734 * Artificially force an assert by testing a value the
5735 * compiler can't assume is const. */
5736 configASSERT( xTickCount == ( TickType_t ) 0 );
5741 traceTASK_NOTIFY( uxIndexToNotify );
5743 /* If the task is in the blocked state specifically to wait for a
5744 * notification then unblock it now. */
5745 if( ucOriginalNotifyState == taskWAITING_NOTIFICATION )
5747 ( void ) uxListRemove( &( pxTCB->xStateListItem ) );
5748 prvAddTaskToReadyList( pxTCB );
5750 /* The task should not have been on an event list. */
5751 configASSERT( listLIST_ITEM_CONTAINER( &( pxTCB->xEventListItem ) ) == NULL );
5753 #if ( configUSE_TICKLESS_IDLE != 0 )
5755 /* If a task is blocked waiting for a notification then
5756 * xNextTaskUnblockTime might be set to the blocked task's time
5757 * out time. If the task is unblocked for a reason other than
5758 * a timeout xNextTaskUnblockTime is normally left unchanged,
5759 * because it will automatically get reset to a new value when
5760 * the tick count equals xNextTaskUnblockTime. However if
5761 * tickless idling is used it might be more important to enter
5762 * sleep mode at the earliest possible time - so reset
5763 * xNextTaskUnblockTime here to ensure it is updated at the
5764 * earliest possible time. */
5765 prvResetNextTaskUnblockTime();
5769 #if ( configUSE_PREEMPTION == 1 )
5771 prvYieldForTask( pxTCB, pdFALSE );
5777 mtCOVERAGE_TEST_MARKER();
5780 taskEXIT_CRITICAL();
5785 #endif /* configUSE_TASK_NOTIFICATIONS */
5786 /*-----------------------------------------------------------*/
5788 #if ( configUSE_TASK_NOTIFICATIONS == 1 )
5790 BaseType_t xTaskGenericNotifyFromISR( TaskHandle_t xTaskToNotify,
5791 UBaseType_t uxIndexToNotify,
5793 eNotifyAction eAction,
5794 uint32_t * pulPreviousNotificationValue,
5795 BaseType_t * pxHigherPriorityTaskWoken )
5798 uint8_t ucOriginalNotifyState;
5799 BaseType_t xReturn = pdPASS;
5800 UBaseType_t uxSavedInterruptStatus;
5802 configASSERT( xTaskToNotify );
5803 configASSERT( uxIndexToNotify < configTASK_NOTIFICATION_ARRAY_ENTRIES );
5805 /* RTOS ports that support interrupt nesting have the concept of a
5806 * maximum system call (or maximum API call) interrupt priority.
5807 * Interrupts that are above the maximum system call priority are keep
5808 * permanently enabled, even when the RTOS kernel is in a critical section,
5809 * but cannot make any calls to FreeRTOS API functions. If configASSERT()
5810 * is defined in FreeRTOSConfig.h then
5811 * portASSERT_IF_INTERRUPT_PRIORITY_INVALID() will result in an assertion
5812 * failure if a FreeRTOS API function is called from an interrupt that has
5813 * been assigned a priority above the configured maximum system call
5814 * priority. Only FreeRTOS functions that end in FromISR can be called
5815 * from interrupts that have been assigned a priority at or (logically)
5816 * below the maximum system call interrupt priority. FreeRTOS maintains a
5817 * separate interrupt safe API to ensure interrupt entry is as fast and as
5818 * simple as possible. More information (albeit Cortex-M specific) is
5819 * provided on the following link:
5820 * https://www.FreeRTOS.org/RTOS-Cortex-M3-M4.html */
5821 portASSERT_IF_INTERRUPT_PRIORITY_INVALID();
5823 pxTCB = xTaskToNotify;
5825 uxSavedInterruptStatus = portSET_INTERRUPT_MASK_FROM_ISR();
5827 if( pulPreviousNotificationValue != NULL )
5829 *pulPreviousNotificationValue = pxTCB->ulNotifiedValue[ uxIndexToNotify ];
5832 ucOriginalNotifyState = pxTCB->ucNotifyState[ uxIndexToNotify ];
5833 pxTCB->ucNotifyState[ uxIndexToNotify ] = taskNOTIFICATION_RECEIVED;
5838 pxTCB->ulNotifiedValue[ uxIndexToNotify ] |= ulValue;
5842 ( pxTCB->ulNotifiedValue[ uxIndexToNotify ] )++;
5845 case eSetValueWithOverwrite:
5846 pxTCB->ulNotifiedValue[ uxIndexToNotify ] = ulValue;
5849 case eSetValueWithoutOverwrite:
5851 if( ucOriginalNotifyState != taskNOTIFICATION_RECEIVED )
5853 pxTCB->ulNotifiedValue[ uxIndexToNotify ] = ulValue;
5857 /* The value could not be written to the task. */
5865 /* The task is being notified without its notify value being
5871 /* Should not get here if all enums are handled.
5872 * Artificially force an assert by testing a value the
5873 * compiler can't assume is const. */
5874 configASSERT( xTickCount == ( TickType_t ) 0 );
5878 traceTASK_NOTIFY_FROM_ISR( uxIndexToNotify );
5880 /* If the task is in the blocked state specifically to wait for a
5881 * notification then unblock it now. */
5882 if( ucOriginalNotifyState == taskWAITING_NOTIFICATION )
5884 /* The task should not have been on an event list. */
5885 configASSERT( listLIST_ITEM_CONTAINER( &( pxTCB->xEventListItem ) ) == NULL );
5887 if( uxSchedulerSuspended == ( UBaseType_t ) pdFALSE )
5889 ( void ) uxListRemove( &( pxTCB->xStateListItem ) );
5890 prvAddTaskToReadyList( pxTCB );
5894 /* The delayed and ready lists cannot be accessed, so hold
5895 * this task pending until the scheduler is resumed. */
5896 vListInsertEnd( &( xPendingReadyList ), &( pxTCB->xEventListItem ) );
5899 #if ( configUSE_PREEMPTION == 1 )
5900 prvYieldForTask( pxTCB, pdFALSE );
5902 if( xYieldPendings[ portGET_CORE_ID() ] == pdTRUE )
5904 if( pxHigherPriorityTaskWoken != NULL )
5906 *pxHigherPriorityTaskWoken = pdTRUE;
5912 portCLEAR_INTERRUPT_MASK_FROM_ISR( uxSavedInterruptStatus );
5917 #endif /* configUSE_TASK_NOTIFICATIONS */
5918 /*-----------------------------------------------------------*/
5920 #if ( configUSE_TASK_NOTIFICATIONS == 1 )
5922 void vTaskGenericNotifyGiveFromISR( TaskHandle_t xTaskToNotify,
5923 UBaseType_t uxIndexToNotify,
5924 BaseType_t * pxHigherPriorityTaskWoken )
5927 uint8_t ucOriginalNotifyState;
5928 UBaseType_t uxSavedInterruptStatus;
5930 configASSERT( xTaskToNotify );
5931 configASSERT( uxIndexToNotify < configTASK_NOTIFICATION_ARRAY_ENTRIES );
5933 /* RTOS ports that support interrupt nesting have the concept of a
5934 * maximum system call (or maximum API call) interrupt priority.
5935 * Interrupts that are above the maximum system call priority are keep
5936 * permanently enabled, even when the RTOS kernel is in a critical section,
5937 * but cannot make any calls to FreeRTOS API functions. If configASSERT()
5938 * is defined in FreeRTOSConfig.h then
5939 * portASSERT_IF_INTERRUPT_PRIORITY_INVALID() will result in an assertion
5940 * failure if a FreeRTOS API function is called from an interrupt that has
5941 * been assigned a priority above the configured maximum system call
5942 * priority. Only FreeRTOS functions that end in FromISR can be called
5943 * from interrupts that have been assigned a priority at or (logically)
5944 * below the maximum system call interrupt priority. FreeRTOS maintains a
5945 * separate interrupt safe API to ensure interrupt entry is as fast and as
5946 * simple as possible. More information (albeit Cortex-M specific) is
5947 * provided on the following link:
5948 * https://www.FreeRTOS.org/RTOS-Cortex-M3-M4.html */
5949 portASSERT_IF_INTERRUPT_PRIORITY_INVALID();
5951 pxTCB = xTaskToNotify;
5953 uxSavedInterruptStatus = portSET_INTERRUPT_MASK_FROM_ISR();
5955 ucOriginalNotifyState = pxTCB->ucNotifyState[ uxIndexToNotify ];
5956 pxTCB->ucNotifyState[ uxIndexToNotify ] = taskNOTIFICATION_RECEIVED;
5958 /* 'Giving' is equivalent to incrementing a count in a counting
5960 ( pxTCB->ulNotifiedValue[ uxIndexToNotify ] )++;
5962 traceTASK_NOTIFY_GIVE_FROM_ISR( uxIndexToNotify );
5964 /* If the task is in the blocked state specifically to wait for a
5965 * notification then unblock it now. */
5966 if( ucOriginalNotifyState == taskWAITING_NOTIFICATION )
5968 /* The task should not have been on an event list. */
5969 configASSERT( listLIST_ITEM_CONTAINER( &( pxTCB->xEventListItem ) ) == NULL );
5971 if( uxSchedulerSuspended == ( UBaseType_t ) pdFALSE )
5973 ( void ) uxListRemove( &( pxTCB->xStateListItem ) );
5974 prvAddTaskToReadyList( pxTCB );
5978 /* The delayed and ready lists cannot be accessed, so hold
5979 * this task pending until the scheduler is resumed. */
5980 vListInsertEnd( &( xPendingReadyList ), &( pxTCB->xEventListItem ) );
5983 #if ( configUSE_PREEMPTION == 1 )
5984 prvYieldForTask( pxTCB, pdFALSE );
5986 if( xYieldPendings[ portGET_CORE_ID() ] == pdTRUE )
5988 if( pxHigherPriorityTaskWoken != NULL )
5990 *pxHigherPriorityTaskWoken = pdTRUE;
5996 portCLEAR_INTERRUPT_MASK_FROM_ISR( uxSavedInterruptStatus );
5999 #endif /* configUSE_TASK_NOTIFICATIONS */
6000 /*-----------------------------------------------------------*/
6002 #if ( configUSE_TASK_NOTIFICATIONS == 1 )
6004 BaseType_t xTaskGenericNotifyStateClear( TaskHandle_t xTask,
6005 UBaseType_t uxIndexToClear )
6010 configASSERT( uxIndexToClear < configTASK_NOTIFICATION_ARRAY_ENTRIES );
6012 /* If null is passed in here then it is the calling task that is having
6013 * its notification state cleared. */
6014 pxTCB = prvGetTCBFromHandle( xTask );
6016 taskENTER_CRITICAL();
6018 if( pxTCB->ucNotifyState[ uxIndexToClear ] == taskNOTIFICATION_RECEIVED )
6020 pxTCB->ucNotifyState[ uxIndexToClear ] = taskNOT_WAITING_NOTIFICATION;
6028 taskEXIT_CRITICAL();
6033 #endif /* configUSE_TASK_NOTIFICATIONS */
6034 /*-----------------------------------------------------------*/
6036 #if ( configUSE_TASK_NOTIFICATIONS == 1 )
6038 uint32_t ulTaskGenericNotifyValueClear( TaskHandle_t xTask,
6039 UBaseType_t uxIndexToClear,
6040 uint32_t ulBitsToClear )
6045 /* If null is passed in here then it is the calling task that is having
6046 * its notification state cleared. */
6047 pxTCB = prvGetTCBFromHandle( xTask );
6049 taskENTER_CRITICAL();
6051 /* Return the notification as it was before the bits were cleared,
6052 * then clear the bit mask. */
6053 ulReturn = pxTCB->ulNotifiedValue[ uxIndexToClear ];
6054 pxTCB->ulNotifiedValue[ uxIndexToClear ] &= ~ulBitsToClear;
6056 taskEXIT_CRITICAL();
6061 #endif /* configUSE_TASK_NOTIFICATIONS */
6062 /*-----------------------------------------------------------*/
6064 #if ( ( configGENERATE_RUN_TIME_STATS == 1 ) && ( INCLUDE_xTaskGetIdleTaskHandle == 1 ) )
6066 uint32_t ulTaskGetIdleRunTimeCounter( void )
6068 uint32_t ulReturn = 0;
6070 for( BaseType_t i = 0; i < configNUM_CORES; i++ )
6072 ulReturn += xIdleTaskHandle[ i ]->ulRunTimeCounter;
6078 #endif /* if ( ( configGENERATE_RUN_TIME_STATS == 1 ) && ( INCLUDE_xTaskGetIdleTaskHandle == 1 ) ) */
6079 /*-----------------------------------------------------------*/
6081 static void prvAddCurrentTaskToDelayedList( TickType_t xTicksToWait,
6082 const BaseType_t xCanBlockIndefinitely )
6084 TickType_t xTimeToWake;
6085 const TickType_t xConstTickCount = xTickCount;
6087 #if ( INCLUDE_xTaskAbortDelay == 1 )
6089 /* About to enter a delayed list, so ensure the ucDelayAborted flag is
6090 * reset to pdFALSE so it can be detected as having been set to pdTRUE
6091 * when the task leaves the Blocked state. */
6092 pxCurrentTCB->ucDelayAborted = pdFALSE;
6096 /* Remove the task from the ready list before adding it to the blocked list
6097 * as the same list item is used for both lists. */
6098 if( uxListRemove( &( pxCurrentTCB->xStateListItem ) ) == ( UBaseType_t ) 0 )
6100 /* The current task must be in a ready list, so there is no need to
6101 * check, and the port reset macro can be called directly. */
6102 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. */
6106 mtCOVERAGE_TEST_MARKER();
6109 #if ( INCLUDE_vTaskSuspend == 1 )
6111 if( ( xTicksToWait == portMAX_DELAY ) && ( xCanBlockIndefinitely != pdFALSE ) )
6113 /* Add the task to the suspended task list instead of a delayed task
6114 * list to ensure it is not woken by a timing event. It will block
6116 vListInsertEnd( &xSuspendedTaskList, &( pxCurrentTCB->xStateListItem ) );
6120 /* Calculate the time at which the task should be woken if the event
6121 * does not occur. This may overflow but this doesn't matter, the
6122 * kernel will manage it correctly. */
6123 xTimeToWake = xConstTickCount + xTicksToWait;
6125 /* The list item will be inserted in wake time order. */
6126 listSET_LIST_ITEM_VALUE( &( pxCurrentTCB->xStateListItem ), xTimeToWake );
6128 if( xTimeToWake < xConstTickCount )
6130 /* Wake time has overflowed. Place this item in the overflow
6132 vListInsert( pxOverflowDelayedTaskList, &( pxCurrentTCB->xStateListItem ) );
6136 /* The wake time has not overflowed, so the current block list
6138 vListInsert( pxDelayedTaskList, &( pxCurrentTCB->xStateListItem ) );
6140 /* If the task entering the blocked state was placed at the
6141 * head of the list of blocked tasks then xNextTaskUnblockTime
6142 * needs to be updated too. */
6143 if( xTimeToWake < xNextTaskUnblockTime )
6145 xNextTaskUnblockTime = xTimeToWake;
6149 mtCOVERAGE_TEST_MARKER();
6154 #else /* INCLUDE_vTaskSuspend */
6156 /* Calculate the time at which the task should be woken if the event
6157 * does not occur. This may overflow but this doesn't matter, the kernel
6158 * will manage it correctly. */
6159 xTimeToWake = xConstTickCount + xTicksToWait;
6161 /* The list item will be inserted in wake time order. */
6162 listSET_LIST_ITEM_VALUE( &( pxCurrentTCB->xStateListItem ), xTimeToWake );
6164 if( xTimeToWake < xConstTickCount )
6166 /* Wake time has overflowed. Place this item in the overflow list. */
6167 vListInsert( pxOverflowDelayedTaskList, &( pxCurrentTCB->xStateListItem ) );
6171 /* The wake time has not overflowed, so the current block list is used. */
6172 vListInsert( pxDelayedTaskList, &( pxCurrentTCB->xStateListItem ) );
6174 /* If the task entering the blocked state was placed at the head of the
6175 * list of blocked tasks then xNextTaskUnblockTime needs to be updated
6177 if( xTimeToWake < xNextTaskUnblockTime )
6179 xNextTaskUnblockTime = xTimeToWake;
6183 mtCOVERAGE_TEST_MARKER();
6187 /* Avoid compiler warning when INCLUDE_vTaskSuspend is not 1. */
6188 ( void ) xCanBlockIndefinitely;
6190 #endif /* INCLUDE_vTaskSuspend */
6193 /* Code below here allows additional code to be inserted into this source file,
6194 * especially where access to file scope functions and data is needed (for example
6195 * when performing module tests). */
6197 #ifdef FREERTOS_MODULE_TEST
6198 #include "tasks_test_access_functions.h"
6202 #if ( configINCLUDE_FREERTOS_TASK_C_ADDITIONS_H == 1 )
6204 #include "freertos_tasks_c_additions.h"
6206 #ifdef FREERTOS_TASKS_C_ADDITIONS_INIT
6207 static void freertos_tasks_c_additions_init( void )
6209 FREERTOS_TASKS_C_ADDITIONS_INIT();
6213 #endif /* if ( configINCLUDE_FREERTOS_TASK_C_ADDITIONS_H == 1 ) */