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 * Creates the idle tasks during scheduler start
416 static BaseType_t prvCreateIdleTasks( void );
419 * Returns the yield pending count for the calling core.
421 static BaseType_t prvGetCurrentYieldPending( void );
424 * Checks to see if another task moved the current task out of the ready
425 * list while it was waiting to enter a critical section and yields if so.
427 static void prvCheckForRunStateChange( void );
430 * Yields the given core.
432 static void prvYieldCore( BaseType_t xCoreID );
435 * Yields a core, or cores if multiple priorities are not allowed to run
436 * simultaneously, to allow the task pxTCB to run.
438 static void prvYieldForTask( TCB_t * pxTCB,
439 const BaseType_t xPreemptEqualPriority );
442 * Selects the highest priority available task
444 static BaseType_t prvSelectHighestPriorityTask( const BaseType_t xCoreID );
447 * Utility task that simply returns pdTRUE if the task referenced by xTask is
448 * currently in the Suspended state, or pdFALSE if the task referenced by xTask
449 * is in any other state.
451 #if ( INCLUDE_vTaskSuspend == 1 )
453 static BaseType_t prvTaskIsTaskSuspended( const TaskHandle_t xTask ) PRIVILEGED_FUNCTION;
455 #endif /* INCLUDE_vTaskSuspend */
458 * Utility to ready all the lists used by the scheduler. This is called
459 * automatically upon the creation of the first task.
461 static void prvInitialiseTaskLists( void ) PRIVILEGED_FUNCTION;
464 * The idle task, which as all tasks is implemented as a never ending loop.
465 * The idle task is automatically created and added to the ready lists upon
466 * creation of the first user task.
469 static portTASK_FUNCTION_PROTO( prvIdleTask, pvParameters ) PRIVILEGED_FUNCTION;
470 #if ( configNUM_CORES > 1 )
471 static portTASK_FUNCTION_PROTO( prvMinimalIdleTask, pvParameters ) PRIVILEGED_FUNCTION;
475 * Utility to free all memory allocated by the scheduler to hold a TCB,
476 * including the stack pointed to by the TCB.
478 * This does not free memory allocated by the task itself (i.e. memory
479 * allocated by calls to pvPortMalloc from within the tasks application code).
481 #if ( INCLUDE_vTaskDelete == 1 )
483 static void prvDeleteTCB( TCB_t * pxTCB ) PRIVILEGED_FUNCTION;
488 * Used only by the idle task. This checks to see if anything has been placed
489 * in the list of tasks waiting to be deleted. If so the task is cleaned up
490 * and its TCB deleted.
492 static void prvCheckTasksWaitingTermination( void ) PRIVILEGED_FUNCTION;
495 * The currently executing task is entering the Blocked state. Add the task to
496 * either the current or the overflow delayed task list.
498 static void prvAddCurrentTaskToDelayedList( TickType_t xTicksToWait,
499 const BaseType_t xCanBlockIndefinitely ) PRIVILEGED_FUNCTION;
502 * Fills an TaskStatus_t structure with information on each task that is
503 * referenced from the pxList list (which may be a ready list, a delayed list,
504 * a suspended list, etc.).
506 * THIS FUNCTION IS INTENDED FOR DEBUGGING ONLY, AND SHOULD NOT BE CALLED FROM
507 * NORMAL APPLICATION CODE.
509 #if ( configUSE_TRACE_FACILITY == 1 )
511 static UBaseType_t prvListTasksWithinSingleList( TaskStatus_t * pxTaskStatusArray,
513 eTaskState eState ) PRIVILEGED_FUNCTION;
518 * Searches pxList for a task with name pcNameToQuery - returning a handle to
519 * the task if it is found, or NULL if the task is not found.
521 #if ( INCLUDE_xTaskGetHandle == 1 )
523 static TCB_t * prvSearchForNameWithinSingleList( List_t * pxList,
524 const char pcNameToQuery[] ) PRIVILEGED_FUNCTION;
529 * When a task is created, the stack of the task is filled with a known value.
530 * This function determines the 'high water mark' of the task stack by
531 * determining how much of the stack remains at the original preset value.
533 #if ( ( configUSE_TRACE_FACILITY == 1 ) || ( INCLUDE_uxTaskGetStackHighWaterMark == 1 ) || ( INCLUDE_uxTaskGetStackHighWaterMark2 == 1 ) )
535 static configSTACK_DEPTH_TYPE prvTaskCheckFreeStackSpace( const uint8_t * pucStackByte ) PRIVILEGED_FUNCTION;
540 * Return the amount of time, in ticks, that will pass before the kernel will
541 * next move a task from the Blocked state to the Running state.
543 * This conditional compilation should use inequality to 0, not equality to 1.
544 * This is to ensure portSUPPRESS_TICKS_AND_SLEEP() can be called when user
545 * defined low power mode implementations require configUSE_TICKLESS_IDLE to be
546 * set to a value other than 1.
548 #if ( configUSE_TICKLESS_IDLE != 0 )
550 static TickType_t prvGetExpectedIdleTime( void ) PRIVILEGED_FUNCTION;
555 * Set xNextTaskUnblockTime to the time at which the next Blocked state task
556 * will exit the Blocked state.
558 static void prvResetNextTaskUnblockTime( void ) PRIVILEGED_FUNCTION;
560 #if ( ( configUSE_TRACE_FACILITY == 1 ) && ( configUSE_STATS_FORMATTING_FUNCTIONS > 0 ) )
563 * Helper function used to pad task names with spaces when printing out
564 * human readable tables of task information.
566 static char * prvWriteNameToBuffer( char * pcBuffer,
567 const char * pcTaskName ) PRIVILEGED_FUNCTION;
572 * Called after a Task_t structure has been allocated either statically or
573 * dynamically to fill in the structure's members.
575 static void prvInitialiseNewTask( TaskFunction_t pxTaskCode,
576 const char * const pcName, /*lint !e971 Unqualified char types are allowed for strings and single characters only. */
577 const uint32_t ulStackDepth,
578 void * const pvParameters,
579 UBaseType_t uxPriority,
580 TaskHandle_t * const pxCreatedTask,
582 const MemoryRegion_t * const xRegions ) PRIVILEGED_FUNCTION;
585 * Called after a new task has been created and initialised to place the task
586 * under the control of the scheduler.
588 static void prvAddNewTaskToReadyList( TCB_t * pxNewTCB ) PRIVILEGED_FUNCTION;
591 * freertos_tasks_c_additions_init() should only be called if the user definable
592 * macro FREERTOS_TASKS_C_ADDITIONS_INIT() is defined, as that is the only macro
593 * called by the function.
595 #ifdef FREERTOS_TASKS_C_ADDITIONS_INIT
597 static void freertos_tasks_c_additions_init( void ) PRIVILEGED_FUNCTION;
601 /*-----------------------------------------------------------*/
603 static BaseType_t prvGetCurrentYieldPending( void )
608 ulState = portDISABLE_INTERRUPTS();
609 xReturn = xYieldPendings[ portGET_CORE_ID() ];
610 portRESTORE_INTERRUPTS( ulState );
615 /*-----------------------------------------------------------*/
617 static void prvCheckForRunStateChange( void )
619 UBaseType_t uxPrevCriticalNesting;
620 UBaseType_t uxPrevSchedulerSuspended;
623 /* This should be skipped when entering a critical section within
624 * an ISR. If the task on the current core is no longer running, then
625 * vTaskSwitchContext() probably should be run before returning, but
626 * we don't have a way to force that to happen from here. */
627 if( portCHECK_IF_IN_ISR() == pdFALSE )
629 /* This function is always called with interrupts disabled
630 * so this is safe. */
631 pxThisTCB = pxCurrentTCBs[ portGET_CORE_ID() ];
633 while( pxThisTCB->xTaskRunState == taskTASK_YIELDING )
635 /* We are only here if we just entered a critical section
636 * or if we just suspended the scheduler, and another task
637 * has requested that we yield.
639 * This is slightly complicated since we need to save and restore
640 * the suspension and critical nesting counts, as well as release
641 * and reacquire the correct locks. And then do it all over again
642 * if our state changed again during the reacquisition. */
644 uxPrevCriticalNesting = pxThisTCB->uxCriticalNesting;
645 uxPrevSchedulerSuspended = uxSchedulerSuspended;
647 /* this must only be called the first time we enter into a critical
648 * section, otherwise it could context switch in the middle of a
649 * critical section. */
650 configASSERT( uxPrevCriticalNesting + uxPrevSchedulerSuspended == 1U );
652 uxSchedulerSuspended = 0U;
654 if( uxPrevCriticalNesting > 0U )
656 pxThisTCB->uxCriticalNesting = 0U;
657 portRELEASE_ISR_LOCK();
658 portRELEASE_TASK_LOCK();
662 /* uxPrevSchedulerSuspended must be 1 */
663 portRELEASE_TASK_LOCK();
666 portMEMORY_BARRIER();
667 configASSERT( pxThisTCB->xTaskRunState == taskTASK_YIELDING );
669 portENABLE_INTERRUPTS();
671 /* Enabling interrupts should cause this core to immediately
672 * service the pending interrupt and yield. If the run state is still
673 * yielding here then that is a problem. */
674 configASSERT( pxThisTCB->xTaskRunState != taskTASK_YIELDING );
676 portDISABLE_INTERRUPTS();
679 pxCurrentTCB->uxCriticalNesting = uxPrevCriticalNesting;
680 uxSchedulerSuspended = uxPrevSchedulerSuspended;
682 if( uxPrevCriticalNesting == 0U )
684 /* uxPrevSchedulerSuspended must be 1 */
685 configASSERT( uxPrevSchedulerSuspended != ( UBaseType_t ) pdFALSE );
686 portRELEASE_ISR_LOCK();
692 /*-----------------------------------------------------------*/
694 static void prvYieldCore( BaseType_t xCoreID )
696 /* This must be called from a critical section and
697 * xCoreID must be valid. */
699 if( portCHECK_IF_IN_ISR() && ( xCoreID == portGET_CORE_ID() ) )
701 xYieldPendings[ xCoreID ] = pdTRUE;
703 else if( pxCurrentTCBs[ xCoreID ]->xTaskRunState != taskTASK_YIELDING )
705 if( xCoreID == portGET_CORE_ID() )
707 xYieldPendings[ xCoreID ] = pdTRUE;
711 portYIELD_CORE( xCoreID );
712 pxCurrentTCBs[ xCoreID ]->xTaskRunState = taskTASK_YIELDING;
717 /*-----------------------------------------------------------*/
719 static void prvYieldForTask( TCB_t * pxTCB,
720 const BaseType_t xPreemptEqualPriority )
722 BaseType_t xLowestPriority;
723 BaseType_t xTaskPriority;
724 BaseType_t xLowestPriorityCore = -1;
725 BaseType_t xYieldCount = 0;
727 TaskRunning_t xTaskRunState;
729 /* THIS FUNCTION MUST BE CALLED FROM A CRITICAL SECTION */
731 configASSERT( pxCurrentTCB->uxCriticalNesting > 0U );
733 #if ( ( configRUN_MULTIPLE_PRIORITIES == 0 ) && ( configNUM_CORES > 1 ) )
735 /* No task should yield for this one if it is a lower priority
736 * than priority level of currently ready tasks. */
737 if( pxTCB->uxPriority < uxTopReadyPriority )
744 xLowestPriority = ( BaseType_t ) pxTCB->uxPriority;
746 if( xPreemptEqualPriority == pdFALSE )
748 /* xLowestPriority will be decremented to -1 if the priority of pxTCB
749 * is 0. This is ok as we will give system idle tasks a priority of -1 below. */
753 for( x = ( BaseType_t ) 0; x < ( BaseType_t ) configNUM_CORES; x++ )
755 /* System idle tasks are being assigned a priority of tskIDLE_PRIORITY - 1 here */
756 xTaskPriority = ( BaseType_t ) pxCurrentTCBs[ x ]->uxPriority - pxCurrentTCBs[ x ]->xIsIdle;
757 xTaskRunState = pxCurrentTCBs[ x ]->xTaskRunState;
759 if( ( taskTASK_IS_RUNNING( xTaskRunState ) != pdFALSE ) && ( xYieldPendings[ x ] == pdFALSE ) )
761 if( xTaskPriority <= xLowestPriority )
763 #if ( configUSE_CORE_EXCLUSION == 1 )
764 if( ( pxTCB->uxCoreExclude & ( 1 << x ) ) == 0 )
767 #if ( configUSE_TASK_PREEMPTION_DISABLE == 1 )
768 if( pxCurrentTCBs[ x ]->xPreemptionDisable == pdFALSE )
771 xLowestPriority = xTaskPriority;
772 xLowestPriorityCore = x;
778 mtCOVERAGE_TEST_MARKER();
781 #if ( ( configRUN_MULTIPLE_PRIORITIES == 0 ) && ( configNUM_CORES > 1 ) ) && 1
783 /* Yield all currently running non-idle tasks with a priority lower than
784 * the task that needs to run. */
785 if( ( ( BaseType_t ) tskIDLE_PRIORITY - 1 < xTaskPriority ) && ( xTaskPriority < ( BaseType_t ) pxTCB->uxPriority ) )
792 mtCOVERAGE_TEST_MARKER();
795 #endif /* if ( ( configRUN_MULTIPLE_PRIORITIES == 0 ) && ( configNUM_CORES > 1 ) ) && 1 */
799 mtCOVERAGE_TEST_MARKER();
803 if( ( xYieldCount == 0 ) && taskVALID_CORE_ID( xLowestPriorityCore ) )
805 prvYieldCore( xLowestPriorityCore );
809 #if ( ( configRUN_MULTIPLE_PRIORITIES == 0 ) && ( configNUM_CORES > 1 ) )
810 /* Verify that the calling core always yields to higher priority tasks */
811 if( !pxCurrentTCBs[ portGET_CORE_ID() ]->xIsIdle && ( pxTCB->uxPriority > pxCurrentTCBs[ portGET_CORE_ID() ]->uxPriority ) )
813 configASSERT( xYieldPendings[ portGET_CORE_ID() ] == pdTRUE || taskTASK_IS_RUNNING( pxCurrentTCBs[ portGET_CORE_ID() ]->xTaskRunState ) == pdFALSE );
817 /*-----------------------------------------------------------*/
819 #if ( configUSE_PORT_OPTIMISED_TASK_SELECTION == 0 )
821 static BaseType_t prvSelectHighestPriorityTask( const BaseType_t xCoreID )
823 UBaseType_t uxCurrentPriority = uxTopReadyPriority;
824 BaseType_t xTaskScheduled = pdFALSE;
825 BaseType_t xDecrementTopPriority = pdTRUE;
827 #if ( configUSE_CORE_EXCLUSION == 1 )
828 TCB_t * pxPreviousTCB = NULL;
830 #if ( ( configRUN_MULTIPLE_PRIORITIES == 0 ) && ( configNUM_CORES > 1 ) )
831 BaseType_t xPriorityDropped = pdFALSE;
834 while( xTaskScheduled == pdFALSE )
836 #if ( ( configRUN_MULTIPLE_PRIORITIES == 0 ) && ( configNUM_CORES > 1 ) )
838 if( uxCurrentPriority < uxTopReadyPriority )
840 /* We can't schedule any tasks, other than idle, that have a
841 * priority lower than the priority of a task currently running
842 * on another core. */
843 uxCurrentPriority = tskIDLE_PRIORITY;
848 if( listLIST_IS_EMPTY( &( pxReadyTasksLists[ uxCurrentPriority ] ) ) == pdFALSE )
850 List_t * const pxReadyList = &( pxReadyTasksLists[ uxCurrentPriority ] );
851 ListItem_t * pxLastTaskItem = pxReadyList->pxIndex->pxPrevious;
852 ListItem_t * pxTaskItem = pxLastTaskItem;
854 if( ( void * ) pxLastTaskItem == ( void * ) &( pxReadyList->xListEnd ) )
856 pxLastTaskItem = pxLastTaskItem->pxPrevious;
859 /* The ready task list for uxCurrentPriority is not empty, so uxTopReadyPriority
860 * must not be decremented any further */
861 xDecrementTopPriority = pdFALSE;
867 pxTaskItem = pxTaskItem->pxNext;
869 if( ( void * ) pxTaskItem == ( void * ) &( pxReadyList->xListEnd ) )
871 pxTaskItem = pxTaskItem->pxNext;
874 pxTCB = pxTaskItem->pvOwner;
876 /*debug_printf("Attempting to schedule %s on core %d\n", pxTCB->pcTaskName, portGET_CORE_ID() ); */
878 #if ( ( configRUN_MULTIPLE_PRIORITIES == 0 ) && ( configNUM_CORES > 1 ) )
880 /* When falling back to the idle priority because only one priority
881 * level is allowed to run at a time, we should ONLY schedule the true
882 * idle tasks, not user tasks at the idle priority. */
883 if( uxCurrentPriority < uxTopReadyPriority )
885 if( pxTCB->xIsIdle == pdFALSE )
891 #endif /* if ( ( configRUN_MULTIPLE_PRIORITIES == 0 ) && ( configNUM_CORES > 1 ) ) */
893 if( pxTCB->xTaskRunState == taskTASK_NOT_RUNNING )
895 #if ( configUSE_CORE_EXCLUSION == 1 )
896 if( ( pxTCB->uxCoreExclude & ( 1 << xCoreID ) ) == 0 )
899 /* If the task is not being executed by any core swap it in */
900 pxCurrentTCBs[ xCoreID ]->xTaskRunState = taskTASK_NOT_RUNNING;
901 #if ( configUSE_CORE_EXCLUSION == 1 )
902 pxPreviousTCB = pxCurrentTCBs[ xCoreID ];
904 pxTCB->xTaskRunState = ( TaskRunning_t ) xCoreID;
905 pxCurrentTCBs[ xCoreID ] = pxTCB;
906 xTaskScheduled = pdTRUE;
909 else if( pxTCB == pxCurrentTCBs[ xCoreID ] )
911 configASSERT( ( pxTCB->xTaskRunState == xCoreID ) || ( pxTCB->xTaskRunState == taskTASK_YIELDING ) );
912 #if ( configUSE_CORE_EXCLUSION == 1 )
913 if( ( pxTCB->uxCoreExclude & ( 1 << xCoreID ) ) == 0 )
916 /* The task is already running on this core, mark it as scheduled */
917 pxTCB->xTaskRunState = ( TaskRunning_t ) xCoreID;
918 xTaskScheduled = pdTRUE;
922 if( xTaskScheduled != pdFALSE )
924 /* Once a task has been selected to run on this core,
925 * move it to the end of the ready task list. */
926 uxListRemove( pxTaskItem );
927 vListInsertEnd( pxReadyList, pxTaskItem );
930 } while( pxTaskItem != pxLastTaskItem );
934 if( xDecrementTopPriority != pdFALSE )
936 uxTopReadyPriority--;
937 #if ( ( configRUN_MULTIPLE_PRIORITIES == 0 ) && ( configNUM_CORES > 1 ) )
939 xPriorityDropped = pdTRUE;
945 /* This function can get called by vTaskSuspend() before the scheduler is started.
946 * In that case, since the idle tasks have not yet been created it is possible that we
947 * won't find a new task to schedule. Return pdFALSE in this case. */
948 if( ( xSchedulerRunning == pdFALSE ) && ( uxCurrentPriority == tskIDLE_PRIORITY ) && ( xTaskScheduled == pdFALSE ) )
953 configASSERT( ( uxCurrentPriority > tskIDLE_PRIORITY ) || ( xTaskScheduled == pdTRUE ) );
957 configASSERT( taskTASK_IS_RUNNING( pxCurrentTCBs[ xCoreID ]->xTaskRunState ) );
959 #if ( ( configRUN_MULTIPLE_PRIORITIES == 0 ) && ( configNUM_CORES > 1 ) )
960 if( xPriorityDropped != pdFALSE )
962 /* There may be several ready tasks that were being prevented from running because there was
963 * a higher priority task running. Now that the last of the higher priority tasks is no longer
964 * running, make sure all the other idle tasks yield. */
967 for( x = ( BaseType_t ) 0; x < ( BaseType_t ) configNUM_CORES; x++ )
969 if( pxCurrentTCBs[ x ]->xIsIdle != pdFALSE )
975 #endif /* if ( ( configRUN_MULTIPLE_PRIORITIES == 0 ) && ( configNUM_CORES > 1 ) ) */
977 #if ( configUSE_CORE_EXCLUSION == 1 )
978 if( ( pxPreviousTCB != NULL ) && ( listIS_CONTAINED_WITHIN( &( pxReadyTasksLists[ pxPreviousTCB->uxPriority ] ), &( pxPreviousTCB->xStateListItem ) ) != pdFALSE ) )
980 /* A ready task was just bumped off this core. Look at the cores it is not excluded
981 * from to see if it is able to run on any of them */
982 UBaseType_t uxCoreMap = ~( pxPreviousTCB->uxCoreExclude );
983 BaseType_t xLowestPriority = pxPreviousTCB->uxPriority - pxPreviousTCB->xIsIdle;
984 BaseType_t xLowestPriorityCore = -1;
986 if( ( uxCoreMap & ( 1 << xCoreID ) ) != 0 )
988 /* The ready task that was removed from this core is not excluded from it.
989 * Only look at the intersection of the cores the removed task is allowed to run
990 * on with the cores that the new task is excluded from. It is possible that the
991 * new task was only placed onto this core because it is excluded from another.
992 * Check to see if the previous task could run on one of those cores. */
993 uxCoreMap &= pxCurrentTCBs[ xCoreID ]->uxCoreExclude;
997 /* The ready task that was removed from this core is excluded from it.
998 * See if we can schedule it on any of the cores where it is not excluded from. */
1001 uxCoreMap &= ( ( 1 << configNUM_CORES ) - 1 );
1003 while( uxCoreMap != 0 )
1005 int uxCore = 31UL - ( uint32_t ) __builtin_clz( uxCoreMap );
1007 xassert( taskVALID_CORE_ID( uxCore ) );
1009 uxCoreMap &= ~( 1 << uxCore );
1011 BaseType_t xTaskPriority = ( BaseType_t ) pxCurrentTCBs[ uxCore ]->uxPriority - pxCurrentTCBs[ uxCore ]->xIsIdle;
1013 if( ( xTaskPriority < xLowestPriority ) && ( taskTASK_IS_RUNNING( pxCurrentTCBs[ uxCore ]->xTaskRunState ) != pdFALSE ) && ( xYieldPendings[ uxCore ] == pdFALSE ) )
1015 #if ( configUSE_TASK_PREEMPTION_DISABLE == 1 )
1016 if( pxCurrentTCBs[ uxCore ]->xPreemptionDisable == pdFALSE )
1019 xLowestPriority = xTaskPriority;
1020 xLowestPriorityCore = uxCore;
1025 if( taskVALID_CORE_ID( xLowestPriorityCore ) )
1027 prvYieldCore( xLowestPriorityCore );
1030 #endif /* if ( configUSE_CORE_EXCLUSION == 1 ) */
1035 #else /* configUSE_PORT_OPTIMISED_TASK_SELECTION */
1037 static void prvSelectHighestPriorityTask( BaseType_t xCoreID )
1039 UBaseType_t uxTopPriority;
1041 /* Find the highest priority list that contains ready tasks. */
1042 portGET_HIGHEST_PRIORITY( uxTopPriority, uxTopReadyPriority );
1043 configASSERT( listCURRENT_LIST_LENGTH( &( pxReadyTasksLists[ uxTopPriority ] ) ) > 0 );
1044 listGET_OWNER_OF_NEXT_ENTRY( pxCurrentTCB, &( pxReadyTasksLists[ uxTopPriority ] ) );
1047 #endif /* configUSE_PORT_OPTIMISED_TASK_SELECTION */
1048 /*-----------------------------------------------------------*/
1052 #if ( configSUPPORT_STATIC_ALLOCATION == 1 )
1054 TaskHandle_t xTaskCreateStatic( TaskFunction_t pxTaskCode,
1055 const char * const pcName, /*lint !e971 Unqualified char types are allowed for strings and single characters only. */
1056 const uint32_t ulStackDepth,
1057 void * const pvParameters,
1058 UBaseType_t uxPriority,
1059 StackType_t * const puxStackBuffer,
1060 StaticTask_t * const pxTaskBuffer )
1063 TaskHandle_t xReturn;
1065 configASSERT( puxStackBuffer != NULL );
1066 configASSERT( pxTaskBuffer != NULL );
1068 #if ( configASSERT_DEFINED == 1 )
1070 /* Sanity check that the size of the structure used to declare a
1071 * variable of type StaticTask_t equals the size of the real task
1073 volatile size_t xSize = sizeof( StaticTask_t );
1074 configASSERT( xSize == sizeof( TCB_t ) );
1075 ( void ) xSize; /* Prevent lint warning when configASSERT() is not used. */
1077 #endif /* configASSERT_DEFINED */
1079 if( ( pxTaskBuffer != NULL ) && ( puxStackBuffer != NULL ) )
1081 /* The memory used for the task's TCB and stack are passed into this
1082 * function - use them. */
1083 pxNewTCB = ( TCB_t * ) pxTaskBuffer; /*lint !e740 !e9087 Unusual cast is ok as the structures are designed to have the same alignment, and the size is checked by an assert. */
1084 pxNewTCB->pxStack = ( StackType_t * ) puxStackBuffer;
1086 #if ( tskSTATIC_AND_DYNAMIC_ALLOCATION_POSSIBLE != 0 ) /*lint !e731 !e9029 Macro has been consolidated for readability reasons. */
1088 /* Tasks can be created statically or dynamically, so note this
1089 * task was created statically in case the task is later deleted. */
1090 pxNewTCB->ucStaticallyAllocated = tskSTATICALLY_ALLOCATED_STACK_AND_TCB;
1092 #endif /* tskSTATIC_AND_DYNAMIC_ALLOCATION_POSSIBLE */
1094 prvInitialiseNewTask( pxTaskCode, pcName, ulStackDepth, pvParameters, uxPriority, &xReturn, pxNewTCB, NULL );
1095 prvAddNewTaskToReadyList( pxNewTCB );
1105 #endif /* SUPPORT_STATIC_ALLOCATION */
1106 /*-----------------------------------------------------------*/
1108 #if ( ( portUSING_MPU_WRAPPERS == 1 ) && ( configSUPPORT_STATIC_ALLOCATION == 1 ) )
1110 BaseType_t xTaskCreateRestrictedStatic( const TaskParameters_t * const pxTaskDefinition,
1111 TaskHandle_t * pxCreatedTask )
1114 BaseType_t xReturn = errCOULD_NOT_ALLOCATE_REQUIRED_MEMORY;
1116 configASSERT( pxTaskDefinition->puxStackBuffer != NULL );
1117 configASSERT( pxTaskDefinition->pxTaskBuffer != NULL );
1119 if( ( pxTaskDefinition->puxStackBuffer != NULL ) && ( pxTaskDefinition->pxTaskBuffer != NULL ) )
1121 /* Allocate space for the TCB. Where the memory comes from depends
1122 * on the implementation of the port malloc function and whether or
1123 * not static allocation is being used. */
1124 pxNewTCB = ( TCB_t * ) pxTaskDefinition->pxTaskBuffer;
1126 /* Store the stack location in the TCB. */
1127 pxNewTCB->pxStack = pxTaskDefinition->puxStackBuffer;
1129 #if ( tskSTATIC_AND_DYNAMIC_ALLOCATION_POSSIBLE != 0 )
1131 /* Tasks can be created statically or dynamically, so note this
1132 * task was created statically in case the task is later deleted. */
1133 pxNewTCB->ucStaticallyAllocated = tskSTATICALLY_ALLOCATED_STACK_AND_TCB;
1135 #endif /* tskSTATIC_AND_DYNAMIC_ALLOCATION_POSSIBLE */
1137 prvInitialiseNewTask( pxTaskDefinition->pvTaskCode,
1138 pxTaskDefinition->pcName,
1139 ( uint32_t ) pxTaskDefinition->usStackDepth,
1140 pxTaskDefinition->pvParameters,
1141 pxTaskDefinition->uxPriority,
1142 pxCreatedTask, pxNewTCB,
1143 pxTaskDefinition->xRegions );
1145 prvAddNewTaskToReadyList( pxNewTCB );
1152 #endif /* ( portUSING_MPU_WRAPPERS == 1 ) && ( configSUPPORT_STATIC_ALLOCATION == 1 ) */
1153 /*-----------------------------------------------------------*/
1155 #if ( ( portUSING_MPU_WRAPPERS == 1 ) && ( configSUPPORT_DYNAMIC_ALLOCATION == 1 ) )
1157 BaseType_t xTaskCreateRestricted( const TaskParameters_t * const pxTaskDefinition,
1158 TaskHandle_t * pxCreatedTask )
1161 BaseType_t xReturn = errCOULD_NOT_ALLOCATE_REQUIRED_MEMORY;
1163 configASSERT( pxTaskDefinition->puxStackBuffer );
1165 if( pxTaskDefinition->puxStackBuffer != NULL )
1167 /* Allocate space for the TCB. Where the memory comes from depends
1168 * on the implementation of the port malloc function and whether or
1169 * not static allocation is being used. */
1170 pxNewTCB = ( TCB_t * ) pvPortMalloc( sizeof( TCB_t ) );
1172 if( pxNewTCB != NULL )
1174 /* Store the stack location in the TCB. */
1175 pxNewTCB->pxStack = pxTaskDefinition->puxStackBuffer;
1177 #if ( tskSTATIC_AND_DYNAMIC_ALLOCATION_POSSIBLE != 0 )
1179 /* Tasks can be created statically or dynamically, so note
1180 * this task had a statically allocated stack in case it is
1181 * later deleted. The TCB was allocated dynamically. */
1182 pxNewTCB->ucStaticallyAllocated = tskSTATICALLY_ALLOCATED_STACK_ONLY;
1184 #endif /* tskSTATIC_AND_DYNAMIC_ALLOCATION_POSSIBLE */
1186 prvInitialiseNewTask( pxTaskDefinition->pvTaskCode,
1187 pxTaskDefinition->pcName,
1188 ( uint32_t ) pxTaskDefinition->usStackDepth,
1189 pxTaskDefinition->pvParameters,
1190 pxTaskDefinition->uxPriority,
1191 pxCreatedTask, pxNewTCB,
1192 pxTaskDefinition->xRegions );
1194 prvAddNewTaskToReadyList( pxNewTCB );
1202 #endif /* portUSING_MPU_WRAPPERS */
1203 /*-----------------------------------------------------------*/
1205 #if ( configSUPPORT_DYNAMIC_ALLOCATION == 1 )
1207 BaseType_t xTaskCreate( TaskFunction_t pxTaskCode,
1208 const char * const pcName, /*lint !e971 Unqualified char types are allowed for strings and single characters only. */
1209 const configSTACK_DEPTH_TYPE usStackDepth,
1210 void * const pvParameters,
1211 UBaseType_t uxPriority,
1212 TaskHandle_t * const pxCreatedTask )
1217 /* If the stack grows down then allocate the stack then the TCB so the stack
1218 * does not grow into the TCB. Likewise if the stack grows up then allocate
1219 * the TCB then the stack. */
1220 #if ( portSTACK_GROWTH > 0 )
1222 /* Allocate space for the TCB. Where the memory comes from depends on
1223 * the implementation of the port malloc function and whether or not static
1224 * allocation is being used. */
1225 pxNewTCB = ( TCB_t * ) pvPortMalloc( sizeof( TCB_t ) );
1227 if( pxNewTCB != NULL )
1229 /* Allocate space for the stack used by the task being created.
1230 * The base of the stack memory stored in the TCB so the task can
1231 * be deleted later if required. */
1232 pxNewTCB->pxStack = ( StackType_t * ) pvPortMallocStack( ( ( ( size_t ) usStackDepth ) * sizeof( StackType_t ) ) ); /*lint !e961 MISRA exception as the casts are only redundant for some ports. */
1234 if( pxNewTCB->pxStack == NULL )
1236 /* Could not allocate the stack. Delete the allocated TCB. */
1237 vPortFree( pxNewTCB );
1242 #else /* portSTACK_GROWTH */
1244 StackType_t * pxStack;
1246 /* Allocate space for the stack used by the task being created. */
1247 pxStack = pvPortMallocStack( ( ( ( size_t ) usStackDepth ) * sizeof( StackType_t ) ) ); /*lint !e9079 All values returned by pvPortMalloc() have at least the alignment required by the MCU's stack and this allocation is the stack. */
1249 if( pxStack != NULL )
1251 /* Allocate space for the TCB. */
1252 pxNewTCB = ( TCB_t * ) pvPortMalloc( sizeof( TCB_t ) ); /*lint !e9087 !e9079 All values returned by pvPortMalloc() have at least the alignment required by the MCU's stack, and the first member of TCB_t is always a pointer to the task's stack. */
1254 if( pxNewTCB != NULL )
1256 /* Store the stack location in the TCB. */
1257 pxNewTCB->pxStack = pxStack;
1261 /* The stack cannot be used as the TCB was not created. Free
1263 vPortFreeStack( pxStack );
1271 #endif /* portSTACK_GROWTH */
1273 if( pxNewTCB != NULL )
1275 #if ( tskSTATIC_AND_DYNAMIC_ALLOCATION_POSSIBLE != 0 ) /*lint !e9029 !e731 Macro has been consolidated for readability reasons. */
1277 /* Tasks can be created statically or dynamically, so note this
1278 * task was created dynamically in case it is later deleted. */
1279 pxNewTCB->ucStaticallyAllocated = tskDYNAMICALLY_ALLOCATED_STACK_AND_TCB;
1281 #endif /* tskSTATIC_AND_DYNAMIC_ALLOCATION_POSSIBLE */
1283 prvInitialiseNewTask( pxTaskCode, pcName, ( uint32_t ) usStackDepth, pvParameters, uxPriority, pxCreatedTask, pxNewTCB, NULL );
1284 prvAddNewTaskToReadyList( pxNewTCB );
1289 xReturn = errCOULD_NOT_ALLOCATE_REQUIRED_MEMORY;
1295 #endif /* configSUPPORT_DYNAMIC_ALLOCATION */
1296 /*-----------------------------------------------------------*/
1298 static void prvInitialiseNewTask( TaskFunction_t pxTaskCode,
1299 const char * const pcName, /*lint !e971 Unqualified char types are allowed for strings and single characters only. */
1300 const uint32_t ulStackDepth,
1301 void * const pvParameters,
1302 UBaseType_t uxPriority,
1303 TaskHandle_t * const pxCreatedTask,
1305 const MemoryRegion_t * const xRegions )
1307 StackType_t * pxTopOfStack;
1310 #if ( portUSING_MPU_WRAPPERS == 1 )
1311 /* Should the task be created in privileged mode? */
1312 BaseType_t xRunPrivileged;
1314 if( ( uxPriority & portPRIVILEGE_BIT ) != 0U )
1316 xRunPrivileged = pdTRUE;
1320 xRunPrivileged = pdFALSE;
1322 uxPriority &= ~portPRIVILEGE_BIT;
1323 #endif /* portUSING_MPU_WRAPPERS == 1 */
1325 /* Avoid dependency on memset() if it is not required. */
1326 #if ( tskSET_NEW_STACKS_TO_KNOWN_VALUE == 1 )
1328 /* Fill the stack with a known value to assist debugging. */
1329 ( void ) memset( pxNewTCB->pxStack, ( int ) tskSTACK_FILL_BYTE, ( size_t ) ulStackDepth * sizeof( StackType_t ) );
1331 #endif /* tskSET_NEW_STACKS_TO_KNOWN_VALUE */
1333 /* Calculate the top of stack address. This depends on whether the stack
1334 * grows from high memory to low (as per the 80x86) or vice versa.
1335 * portSTACK_GROWTH is used to make the result positive or negative as required
1337 #if ( portSTACK_GROWTH < 0 )
1339 pxTopOfStack = &( pxNewTCB->pxStack[ ulStackDepth - ( uint32_t ) 1 ] );
1340 pxTopOfStack = ( StackType_t * ) ( ( ( portPOINTER_SIZE_TYPE ) pxTopOfStack ) & ( ~( ( portPOINTER_SIZE_TYPE ) portBYTE_ALIGNMENT_MASK ) ) ); /*lint !e923 !e9033 !e9078 MISRA exception. Avoiding casts between pointers and integers is not practical. Size differences accounted for using portPOINTER_SIZE_TYPE type. Checked by assert(). */
1342 /* Check the alignment of the calculated top of stack is correct. */
1343 configASSERT( ( ( ( portPOINTER_SIZE_TYPE ) pxTopOfStack & ( portPOINTER_SIZE_TYPE ) portBYTE_ALIGNMENT_MASK ) == 0UL ) );
1345 #if ( configRECORD_STACK_HIGH_ADDRESS == 1 )
1347 /* Also record the stack's high address, which may assist
1349 pxNewTCB->pxEndOfStack = pxTopOfStack;
1351 #endif /* configRECORD_STACK_HIGH_ADDRESS */
1353 #else /* portSTACK_GROWTH */
1355 pxTopOfStack = pxNewTCB->pxStack;
1357 /* Check the alignment of the stack buffer is correct. */
1358 configASSERT( ( ( ( portPOINTER_SIZE_TYPE ) pxNewTCB->pxStack & ( portPOINTER_SIZE_TYPE ) portBYTE_ALIGNMENT_MASK ) == 0UL ) );
1360 /* The other extreme of the stack space is required if stack checking is
1362 pxNewTCB->pxEndOfStack = pxNewTCB->pxStack + ( ulStackDepth - ( uint32_t ) 1 );
1364 #endif /* portSTACK_GROWTH */
1366 /* Store the task name in the TCB. */
1367 if( pcName != NULL )
1369 for( x = ( UBaseType_t ) 0; x < ( UBaseType_t ) configMAX_TASK_NAME_LEN; x++ )
1371 pxNewTCB->pcTaskName[ x ] = pcName[ x ];
1373 /* Don't copy all configMAX_TASK_NAME_LEN if the string is shorter than
1374 * configMAX_TASK_NAME_LEN characters just in case the memory after the
1375 * string is not accessible (extremely unlikely). */
1376 if( pcName[ x ] == ( char ) 0x00 )
1382 mtCOVERAGE_TEST_MARKER();
1386 /* Ensure the name string is terminated in the case that the string length
1387 * was greater or equal to configMAX_TASK_NAME_LEN. */
1388 pxNewTCB->pcTaskName[ configMAX_TASK_NAME_LEN - 1 ] = '\0';
1392 /* The task has not been given a name, so just ensure there is a NULL
1393 * terminator when it is read out. */
1394 pxNewTCB->pcTaskName[ 0 ] = 0x00;
1397 /* This is used as an array index so must ensure it's not too large. First
1398 * remove the privilege bit if one is present. */
1399 if( uxPriority >= ( UBaseType_t ) configMAX_PRIORITIES )
1401 uxPriority = ( UBaseType_t ) configMAX_PRIORITIES - ( UBaseType_t ) 1U;
1405 mtCOVERAGE_TEST_MARKER();
1408 pxNewTCB->uxPriority = uxPriority;
1409 #if ( configUSE_MUTEXES == 1 )
1411 pxNewTCB->uxBasePriority = uxPriority;
1412 pxNewTCB->uxMutexesHeld = 0;
1414 #endif /* configUSE_MUTEXES */
1416 vListInitialiseItem( &( pxNewTCB->xStateListItem ) );
1417 vListInitialiseItem( &( pxNewTCB->xEventListItem ) );
1419 /* Set the pxNewTCB as a link back from the ListItem_t. This is so we can get
1420 * back to the containing TCB from a generic item in a list. */
1421 listSET_LIST_ITEM_OWNER( &( pxNewTCB->xStateListItem ), pxNewTCB );
1423 /* Event lists are always in priority order. */
1424 listSET_LIST_ITEM_VALUE( &( pxNewTCB->xEventListItem ), ( TickType_t ) configMAX_PRIORITIES - ( TickType_t ) uxPriority ); /*lint !e961 MISRA exception as the casts are only redundant for some ports. */
1425 listSET_LIST_ITEM_OWNER( &( pxNewTCB->xEventListItem ), pxNewTCB );
1427 #if ( portCRITICAL_NESTING_IN_TCB == 1 )
1429 pxNewTCB->uxCriticalNesting = ( UBaseType_t ) 0U;
1431 #endif /* portCRITICAL_NESTING_IN_TCB */
1433 #if ( configUSE_APPLICATION_TASK_TAG == 1 )
1435 pxNewTCB->pxTaskTag = NULL;
1437 #endif /* configUSE_APPLICATION_TASK_TAG */
1439 #if ( configGENERATE_RUN_TIME_STATS == 1 )
1441 pxNewTCB->ulRunTimeCounter = 0UL;
1443 #endif /* configGENERATE_RUN_TIME_STATS */
1445 #if ( portUSING_MPU_WRAPPERS == 1 )
1447 vPortStoreTaskMPUSettings( &( pxNewTCB->xMPUSettings ), xRegions, pxNewTCB->pxStack, ulStackDepth );
1451 /* Avoid compiler warning about unreferenced parameter. */
1456 #if ( configNUM_THREAD_LOCAL_STORAGE_POINTERS != 0 )
1458 memset( ( void * ) &( pxNewTCB->pvThreadLocalStoragePointers[ 0 ] ), 0x00, sizeof( pxNewTCB->pvThreadLocalStoragePointers ) );
1462 #if ( configUSE_TASK_NOTIFICATIONS == 1 )
1464 memset( ( void * ) &( pxNewTCB->ulNotifiedValue[ 0 ] ), 0x00, sizeof( pxNewTCB->ulNotifiedValue ) );
1465 memset( ( void * ) &( pxNewTCB->ucNotifyState[ 0 ] ), 0x00, sizeof( pxNewTCB->ucNotifyState ) );
1469 #if ( configUSE_NEWLIB_REENTRANT == 1 )
1471 /* Initialise this task's Newlib reent structure.
1472 * See the third party link http://www.nadler.com/embedded/newlibAndFreeRTOS.html
1473 * for additional information. */
1474 _REENT_INIT_PTR( ( &( pxNewTCB->xNewLib_reent ) ) );
1478 #if ( INCLUDE_xTaskAbortDelay == 1 )
1480 pxNewTCB->ucDelayAborted = pdFALSE;
1484 #if ( configUSE_CORE_EXCLUSION == 1 )
1486 pxNewTCB->uxCoreExclude = 0;
1489 #if ( configUSE_TASK_PREEMPTION_DISABLE == 1 )
1491 pxNewTCB->xPreemptionDisable = 0;
1495 /* Initialize the TCB stack to look as if the task was already running,
1496 * but had been interrupted by the scheduler. The return address is set
1497 * to the start of the task function. Once the stack has been initialised
1498 * the top of stack variable is updated. */
1499 #if ( portUSING_MPU_WRAPPERS == 1 )
1501 /* If the port has capability to detect stack overflow,
1502 * pass the stack end address to the stack initialization
1503 * function as well. */
1504 #if ( portHAS_STACK_OVERFLOW_CHECKING == 1 )
1506 #if ( portSTACK_GROWTH < 0 )
1508 pxNewTCB->pxTopOfStack = pxPortInitialiseStack( pxTopOfStack, pxNewTCB->pxStack, pxTaskCode, pvParameters, xRunPrivileged );
1510 #else /* portSTACK_GROWTH */
1512 pxNewTCB->pxTopOfStack = pxPortInitialiseStack( pxTopOfStack, pxNewTCB->pxEndOfStack, pxTaskCode, pvParameters, xRunPrivileged );
1514 #endif /* portSTACK_GROWTH */
1516 #else /* portHAS_STACK_OVERFLOW_CHECKING */
1518 pxNewTCB->pxTopOfStack = pxPortInitialiseStack( pxTopOfStack, pxTaskCode, pvParameters, xRunPrivileged );
1520 #endif /* portHAS_STACK_OVERFLOW_CHECKING */
1522 #else /* portUSING_MPU_WRAPPERS */
1524 /* If the port has capability to detect stack overflow,
1525 * pass the stack end address to the stack initialization
1526 * function as well. */
1527 #if ( portHAS_STACK_OVERFLOW_CHECKING == 1 )
1529 #if ( portSTACK_GROWTH < 0 )
1531 pxNewTCB->pxTopOfStack = pxPortInitialiseStack( pxTopOfStack, pxNewTCB->pxStack, pxTaskCode, pvParameters );
1533 #else /* portSTACK_GROWTH */
1535 pxNewTCB->pxTopOfStack = pxPortInitialiseStack( pxTopOfStack, pxNewTCB->pxEndOfStack, pxTaskCode, pvParameters );
1537 #endif /* portSTACK_GROWTH */
1539 #else /* portHAS_STACK_OVERFLOW_CHECKING */
1541 pxNewTCB->pxTopOfStack = pxPortInitialiseStack( pxTopOfStack, pxTaskCode, pvParameters );
1543 #endif /* portHAS_STACK_OVERFLOW_CHECKING */
1545 #endif /* portUSING_MPU_WRAPPERS */
1547 /* Initialize to not running */
1548 pxNewTCB->xTaskRunState = taskTASK_NOT_RUNNING;
1550 /* Is this an idle task? */
1551 if(pxTaskCode == prvIdleTask)
1553 pxNewTCB->xIsIdle = pdTRUE;
1555 #if(configNUM_CORES > 1)
1556 else if(pxTaskCode == prvMinimalIdleTask)
1558 pxNewTCB->xIsIdle = pdTRUE;
1563 pxNewTCB->xIsIdle = pdFALSE;
1566 if( pxCreatedTask != NULL )
1568 /* Pass the handle out in an anonymous way. The handle can be used to
1569 * change the created task's priority, delete the created task, etc.*/
1570 *pxCreatedTask = ( TaskHandle_t ) pxNewTCB;
1574 mtCOVERAGE_TEST_MARKER();
1577 /*-----------------------------------------------------------*/
1579 static void prvAddNewTaskToReadyList( TCB_t * pxNewTCB )
1581 /* Ensure interrupts don't access the task lists while the lists are being
1583 taskENTER_CRITICAL();
1585 uxCurrentNumberOfTasks++;
1587 if( xSchedulerRunning == pdFALSE )
1589 if( uxCurrentNumberOfTasks == ( UBaseType_t ) 1 )
1591 /* This is the first task to be created so do the preliminary
1592 * initialisation required. We will not recover if this call
1593 * fails, but we will report the failure. */
1594 prvInitialiseTaskLists();
1598 mtCOVERAGE_TEST_MARKER();
1601 if( pxNewTCB->xIsIdle != pdFALSE )
1605 /* Check if a core is free. */
1606 for( xCoreID = ( UBaseType_t ) 0; xCoreID < ( UBaseType_t ) configNUM_CORES; xCoreID++ )
1608 if( pxCurrentTCBs[ xCoreID ] == NULL )
1610 pxNewTCB->xTaskRunState = xCoreID;
1611 #if ( configUSE_CORE_EXCLUSION == 1 )
1613 pxNewTCB->uxCoreExclude = ~( 1 << xCoreID );
1616 pxCurrentTCBs[ xCoreID ] = pxNewTCB;
1624 mtCOVERAGE_TEST_MARKER();
1629 #if ( configUSE_TRACE_FACILITY == 1 )
1631 /* Add a counter into the TCB for tracing only. */
1632 pxNewTCB->uxTCBNumber = uxTaskNumber;
1634 #endif /* configUSE_TRACE_FACILITY */
1635 traceTASK_CREATE( pxNewTCB );
1637 prvAddTaskToReadyList( pxNewTCB );
1639 portSETUP_TCB( pxNewTCB );
1641 if( xSchedulerRunning != pdFALSE )
1643 /* If the created task is of a higher priority than another
1644 * currently running task and preemption is on then it should
1646 #if ( configUSE_PREEMPTION == 1 )
1647 prvYieldForTask( pxNewTCB, pdFALSE );
1652 mtCOVERAGE_TEST_MARKER();
1655 taskEXIT_CRITICAL();
1657 /*-----------------------------------------------------------*/
1659 #if ( INCLUDE_vTaskDelete == 1 )
1661 void vTaskDelete( TaskHandle_t xTaskToDelete )
1664 TaskRunning_t xTaskRunningOnCore;
1666 taskENTER_CRITICAL();
1668 /* If null is passed in here then it is the calling task that is
1670 pxTCB = prvGetTCBFromHandle( xTaskToDelete );
1672 xTaskRunningOnCore = pxTCB->xTaskRunState;
1674 /* Remove task from the ready/delayed list. */
1675 if( uxListRemove( &( pxTCB->xStateListItem ) ) == ( UBaseType_t ) 0 )
1677 taskRESET_READY_PRIORITY( pxTCB->uxPriority );
1681 mtCOVERAGE_TEST_MARKER();
1684 /* Is the task waiting on an event also? */
1685 if( listLIST_ITEM_CONTAINER( &( pxTCB->xEventListItem ) ) != NULL )
1687 ( void ) uxListRemove( &( pxTCB->xEventListItem ) );
1691 mtCOVERAGE_TEST_MARKER();
1694 /* Increment the uxTaskNumber also so kernel aware debuggers can
1695 * detect that the task lists need re-generating. This is done before
1696 * portPRE_TASK_DELETE_HOOK() as in the Windows port that macro will
1700 /* If the task is running (or yielding), we must add it to the
1701 * termination list so that an idle task can delete it when it is
1702 * no longer running. */
1703 if( xTaskRunningOnCore != taskTASK_NOT_RUNNING )
1706 /* A running task is being deleted. This cannot complete within the
1707 * task itself, as a context switch to another task is required.
1708 * Place the task in the termination list. The idle task will
1709 * check the termination list and free up any memory allocated by
1710 * the scheduler for the TCB and stack of the deleted task. */
1711 vListInsertEnd( &xTasksWaitingTermination, &( pxTCB->xStateListItem ) );
1713 /* Increment the ucTasksDeleted variable so the idle task knows
1714 * there is a task that has been deleted and that it should therefore
1715 * check the xTasksWaitingTermination list. */
1716 ++uxDeletedTasksWaitingCleanUp;
1718 /* Call the delete hook before portPRE_TASK_DELETE_HOOK() as
1719 * portPRE_TASK_DELETE_HOOK() does not return in the Win32 port. */
1720 traceTASK_DELETE( pxTCB );
1722 /* The pre-delete hook is primarily for the Windows simulator,
1723 * in which Windows specific clean up operations are performed,
1724 * after which it is not possible to yield away from this task -
1725 * hence xYieldPending is used to latch that a context switch is
1727 portPRE_TASK_DELETE_HOOK( pxTCB, &xYieldPendings[ pxTCB->xTaskRunState ] );
1731 --uxCurrentNumberOfTasks;
1732 traceTASK_DELETE( pxTCB );
1733 prvDeleteTCB( pxTCB );
1735 /* Reset the next expected unblock time in case it referred to
1736 * the task that has just been deleted. */
1737 prvResetNextTaskUnblockTime();
1740 /* Force a reschedule if the task that has just been deleted was running. */
1741 if( ( xSchedulerRunning != pdFALSE ) && ( taskTASK_IS_RUNNING( xTaskRunningOnCore ) ) )
1745 xCoreID = portGET_CORE_ID();
1748 if( xTaskRunningOnCore == xCoreID )
1750 configASSERT( uxSchedulerSuspended == 0 );
1751 vTaskYieldWithinAPI();
1755 prvYieldCore( xTaskRunningOnCore );
1759 taskEXIT_CRITICAL();
1762 #endif /* INCLUDE_vTaskDelete */
1763 /*-----------------------------------------------------------*/
1765 #if ( INCLUDE_xTaskDelayUntil == 1 )
1767 BaseType_t xTaskDelayUntil( TickType_t * const pxPreviousWakeTime,
1768 const TickType_t xTimeIncrement )
1770 TickType_t xTimeToWake;
1771 BaseType_t xAlreadyYielded, xShouldDelay = pdFALSE;
1773 configASSERT( pxPreviousWakeTime );
1774 configASSERT( ( xTimeIncrement > 0U ) );
1778 configASSERT( uxSchedulerSuspended == 1 );
1780 /* Minor optimisation. The tick count cannot change in this
1782 const TickType_t xConstTickCount = xTickCount;
1784 /* Generate the tick time at which the task wants to wake. */
1785 xTimeToWake = *pxPreviousWakeTime + xTimeIncrement;
1787 if( xConstTickCount < *pxPreviousWakeTime )
1789 /* The tick count has overflowed since this function was
1790 * lasted called. In this case the only time we should ever
1791 * actually delay is if the wake time has also overflowed,
1792 * and the wake time is greater than the tick time. When this
1793 * is the case it is as if neither time had overflowed. */
1794 if( ( xTimeToWake < *pxPreviousWakeTime ) && ( xTimeToWake > xConstTickCount ) )
1796 xShouldDelay = pdTRUE;
1800 mtCOVERAGE_TEST_MARKER();
1805 /* The tick time has not overflowed. In this case we will
1806 * delay if either the wake time has overflowed, and/or the
1807 * tick time is less than the wake time. */
1808 if( ( xTimeToWake < *pxPreviousWakeTime ) || ( xTimeToWake > xConstTickCount ) )
1810 xShouldDelay = pdTRUE;
1814 mtCOVERAGE_TEST_MARKER();
1818 /* Update the wake time ready for the next call. */
1819 *pxPreviousWakeTime = xTimeToWake;
1821 if( xShouldDelay != pdFALSE )
1823 traceTASK_DELAY_UNTIL( xTimeToWake );
1825 /* prvAddCurrentTaskToDelayedList() needs the block time, not
1826 * the time to wake, so subtract the current tick count. */
1827 prvAddCurrentTaskToDelayedList( xTimeToWake - xConstTickCount, pdFALSE );
1831 mtCOVERAGE_TEST_MARKER();
1834 xAlreadyYielded = xTaskResumeAll();
1836 /* Force a reschedule if xTaskResumeAll has not already done so, we may
1837 * have put ourselves to sleep. */
1838 if( xAlreadyYielded == pdFALSE )
1840 vTaskYieldWithinAPI();
1844 mtCOVERAGE_TEST_MARKER();
1847 return xShouldDelay;
1850 #endif /* INCLUDE_xTaskDelayUntil */
1851 /*-----------------------------------------------------------*/
1853 #if ( INCLUDE_vTaskDelay == 1 )
1855 void vTaskDelay( const TickType_t xTicksToDelay )
1857 BaseType_t xAlreadyYielded = pdFALSE;
1859 /* A delay time of zero just forces a reschedule. */
1860 if( xTicksToDelay > ( TickType_t ) 0U )
1864 configASSERT( uxSchedulerSuspended == 1 );
1867 /* A task that is removed from the event list while the
1868 * scheduler is suspended will not get placed in the ready
1869 * list or removed from the blocked list until the scheduler
1872 * This task cannot be in an event list as it is the currently
1873 * executing task. */
1874 prvAddCurrentTaskToDelayedList( xTicksToDelay, pdFALSE );
1876 xAlreadyYielded = xTaskResumeAll();
1880 mtCOVERAGE_TEST_MARKER();
1883 /* Force a reschedule if xTaskResumeAll has not already done so, we may
1884 * have put ourselves to sleep. */
1885 if( xAlreadyYielded == pdFALSE )
1887 vTaskYieldWithinAPI();
1891 mtCOVERAGE_TEST_MARKER();
1895 #endif /* INCLUDE_vTaskDelay */
1896 /*-----------------------------------------------------------*/
1898 #if ( ( INCLUDE_eTaskGetState == 1 ) || ( configUSE_TRACE_FACILITY == 1 ) || ( INCLUDE_xTaskAbortDelay == 1 ) )
1900 eTaskState eTaskGetState( TaskHandle_t xTask )
1903 List_t const * pxStateList, * pxDelayedList, * pxOverflowedDelayedList;
1904 const TCB_t * const pxTCB = xTask;
1906 configASSERT( pxTCB );
1908 taskENTER_CRITICAL();
1910 pxStateList = listLIST_ITEM_CONTAINER( &( pxTCB->xStateListItem ) );
1911 pxDelayedList = pxDelayedTaskList;
1912 pxOverflowedDelayedList = pxOverflowDelayedTaskList;
1914 taskEXIT_CRITICAL();
1916 if( ( pxStateList == pxDelayedList ) || ( pxStateList == pxOverflowedDelayedList ) )
1918 /* The task being queried is referenced from one of the Blocked
1923 #if ( INCLUDE_vTaskSuspend == 1 )
1924 else if( pxStateList == &xSuspendedTaskList )
1926 /* The task being queried is referenced from the suspended
1927 * list. Is it genuinely suspended or is it blocked
1929 if( listLIST_ITEM_CONTAINER( &( pxTCB->xEventListItem ) ) == NULL )
1931 #if ( configUSE_TASK_NOTIFICATIONS == 1 )
1935 /* The task does not appear on the event list item of
1936 * and of the RTOS objects, but could still be in the
1937 * blocked state if it is waiting on its notification
1938 * rather than waiting on an object. If not, is
1940 eReturn = eSuspended;
1942 for( x = 0; x < configTASK_NOTIFICATION_ARRAY_ENTRIES; x++ )
1944 if( pxTCB->ucNotifyState[ x ] == taskWAITING_NOTIFICATION )
1951 #else /* if ( configUSE_TASK_NOTIFICATIONS == 1 ) */
1953 eReturn = eSuspended;
1955 #endif /* if ( configUSE_TASK_NOTIFICATIONS == 1 ) */
1962 #endif /* if ( INCLUDE_vTaskSuspend == 1 ) */
1964 #if ( INCLUDE_vTaskDelete == 1 )
1965 else if( ( pxStateList == &xTasksWaitingTermination ) || ( pxStateList == NULL ) )
1967 /* The task being queried is referenced from the deleted
1968 * tasks list, or it is not referenced from any lists at
1974 else /*lint !e525 Negative indentation is intended to make use of pre-processor clearer. */
1976 /* If the task is not in any other state, it must be in the
1977 * Ready (including pending ready) state. */
1978 if( taskTASK_IS_RUNNING( pxTCB->xTaskRunState ) )
1980 /* Is it actively running on a core? */
1990 } /*lint !e818 xTask cannot be a pointer to const because it is a typedef. */
1992 #endif /* INCLUDE_eTaskGetState */
1993 /*-----------------------------------------------------------*/
1995 #if ( INCLUDE_uxTaskPriorityGet == 1 )
1997 UBaseType_t uxTaskPriorityGet( const TaskHandle_t xTask )
1999 TCB_t const * pxTCB;
2000 UBaseType_t uxReturn;
2002 taskENTER_CRITICAL();
2004 /* If null is passed in here then it is the priority of the task
2005 * that called uxTaskPriorityGet() that is being queried. */
2006 pxTCB = prvGetTCBFromHandle( xTask );
2007 uxReturn = pxTCB->uxPriority;
2009 taskEXIT_CRITICAL();
2014 #endif /* INCLUDE_uxTaskPriorityGet */
2015 /*-----------------------------------------------------------*/
2017 #if ( INCLUDE_uxTaskPriorityGet == 1 )
2019 UBaseType_t uxTaskPriorityGetFromISR( const TaskHandle_t xTask )
2021 TCB_t const * pxTCB;
2022 UBaseType_t uxReturn, uxSavedInterruptState;
2024 /* RTOS ports that support interrupt nesting have the concept of a
2025 * maximum system call (or maximum API call) interrupt priority.
2026 * Interrupts that are above the maximum system call priority are keep
2027 * permanently enabled, even when the RTOS kernel is in a critical section,
2028 * but cannot make any calls to FreeRTOS API functions. If configASSERT()
2029 * is defined in FreeRTOSConfig.h then
2030 * portASSERT_IF_INTERRUPT_PRIORITY_INVALID() will result in an assertion
2031 * failure if a FreeRTOS API function is called from an interrupt that has
2032 * been assigned a priority above the configured maximum system call
2033 * priority. Only FreeRTOS functions that end in FromISR can be called
2034 * from interrupts that have been assigned a priority at or (logically)
2035 * below the maximum system call interrupt priority. FreeRTOS maintains a
2036 * separate interrupt safe API to ensure interrupt entry is as fast and as
2037 * simple as possible. More information (albeit Cortex-M specific) is
2038 * provided on the following link:
2039 * https://www.FreeRTOS.org/RTOS-Cortex-M3-M4.html */
2040 portASSERT_IF_INTERRUPT_PRIORITY_INVALID();
2042 uxSavedInterruptState = portSET_INTERRUPT_MASK_FROM_ISR();
2044 /* If null is passed in here then it is the priority of the calling
2045 * task that is being queried. */
2046 pxTCB = prvGetTCBFromHandle( xTask );
2047 uxReturn = pxTCB->uxPriority;
2049 portCLEAR_INTERRUPT_MASK_FROM_ISR( uxSavedInterruptState );
2054 #endif /* INCLUDE_uxTaskPriorityGet */
2055 /*-----------------------------------------------------------*/
2057 #if ( INCLUDE_vTaskPrioritySet == 1 )
2059 void vTaskPrioritySet( TaskHandle_t xTask,
2060 UBaseType_t uxNewPriority )
2063 UBaseType_t uxCurrentBasePriority, uxPriorityUsedOnEntry;
2064 BaseType_t xYieldRequired = pdFALSE;
2065 BaseType_t xYieldForTask = pdFALSE;
2068 configASSERT( ( uxNewPriority < configMAX_PRIORITIES ) );
2070 /* Ensure the new priority is valid. */
2071 if( uxNewPriority >= ( UBaseType_t ) configMAX_PRIORITIES )
2073 uxNewPriority = ( UBaseType_t ) configMAX_PRIORITIES - ( UBaseType_t ) 1U;
2077 mtCOVERAGE_TEST_MARKER();
2080 taskENTER_CRITICAL();
2082 /* If null is passed in here then it is the priority of the calling
2083 * task that is being changed. */
2084 pxTCB = prvGetTCBFromHandle( xTask );
2086 traceTASK_PRIORITY_SET( pxTCB, uxNewPriority );
2088 #if ( configUSE_MUTEXES == 1 )
2090 uxCurrentBasePriority = pxTCB->uxBasePriority;
2094 uxCurrentBasePriority = pxTCB->uxPriority;
2098 if( uxCurrentBasePriority != uxNewPriority )
2100 /* The priority change may have readied a task of higher
2101 * priority than a running task. */
2102 if( uxNewPriority > uxCurrentBasePriority )
2104 /* The priority of a task is being raised so
2105 * perform a yield for this task later. */
2106 xYieldForTask = pdTRUE;
2108 else if( taskTASK_IS_RUNNING( pxTCB->xTaskRunState ) )
2110 /* Setting the priority of a running task down means
2111 * there may now be another task of higher priority that
2112 * is ready to execute. */
2113 #if ( configUSE_TASK_PREEMPTION_DISABLE == 1 )
2114 if( pxTCB->xPreemptionDisable == pdFALSE )
2117 xCoreID = ( BaseType_t ) pxTCB->xTaskRunState;
2118 xYieldRequired = pdTRUE;
2123 /* Setting the priority of any other task down does not
2124 * require a yield as the running task must be above the
2125 * new priority of the task being modified. */
2128 /* Remember the ready list the task might be referenced from
2129 * before its uxPriority member is changed so the
2130 * taskRESET_READY_PRIORITY() macro can function correctly. */
2131 uxPriorityUsedOnEntry = pxTCB->uxPriority;
2133 #if ( configUSE_MUTEXES == 1 )
2135 /* Only change the priority being used if the task is not
2136 * currently using an inherited priority. */
2137 if( pxTCB->uxBasePriority == pxTCB->uxPriority )
2139 pxTCB->uxPriority = uxNewPriority;
2143 mtCOVERAGE_TEST_MARKER();
2146 /* The base priority gets set whatever. */
2147 pxTCB->uxBasePriority = uxNewPriority;
2149 #else /* if ( configUSE_MUTEXES == 1 ) */
2151 pxTCB->uxPriority = uxNewPriority;
2153 #endif /* if ( configUSE_MUTEXES == 1 ) */
2155 /* Only reset the event list item value if the value is not
2156 * being used for anything else. */
2157 if( ( listGET_LIST_ITEM_VALUE( &( pxTCB->xEventListItem ) ) & taskEVENT_LIST_ITEM_VALUE_IN_USE ) == 0UL )
2159 listSET_LIST_ITEM_VALUE( &( pxTCB->xEventListItem ), ( ( TickType_t ) configMAX_PRIORITIES - ( TickType_t ) uxNewPriority ) ); /*lint !e961 MISRA exception as the casts are only redundant for some ports. */
2163 mtCOVERAGE_TEST_MARKER();
2166 /* If the task is in the blocked or suspended list we need do
2167 * nothing more than change its priority variable. However, if
2168 * the task is in a ready list it needs to be removed and placed
2169 * in the list appropriate to its new priority. */
2170 if( listIS_CONTAINED_WITHIN( &( pxReadyTasksLists[ uxPriorityUsedOnEntry ] ), &( pxTCB->xStateListItem ) ) != pdFALSE )
2172 /* The task is currently in its ready list - remove before
2173 * adding it to its new ready list. As we are in a critical
2174 * section we can do this even if the scheduler is suspended. */
2175 if( uxListRemove( &( pxTCB->xStateListItem ) ) == ( UBaseType_t ) 0 )
2177 /* It is known that the task is in its ready list so
2178 * there is no need to check again and the port level
2179 * reset macro can be called directly. */
2180 portRESET_READY_PRIORITY( uxPriorityUsedOnEntry, uxTopReadyPriority );
2184 mtCOVERAGE_TEST_MARKER();
2187 prvAddTaskToReadyList( pxTCB );
2191 /* It's possible that xYieldForTask was already set to pdTRUE because
2192 * its priority is being raised. However, since it is not in a ready list
2193 * we don't actually need to yield for it. */
2194 xYieldForTask = pdFALSE;
2197 #if ( configUSE_PREEMPTION == 1 )
2198 if( xYieldRequired != pdFALSE )
2200 prvYieldCore( xCoreID );
2202 else if( xYieldForTask != pdFALSE )
2204 prvYieldForTask( pxTCB, pdTRUE );
2208 mtCOVERAGE_TEST_MARKER();
2210 #endif /* if ( configUSE_PREEMPTION == 1 ) */
2212 /* Remove compiler warning about unused variables when the port
2213 * optimised task selection is not being used. */
2214 ( void ) uxPriorityUsedOnEntry;
2217 taskEXIT_CRITICAL();
2220 #endif /* INCLUDE_vTaskPrioritySet */
2221 /*-----------------------------------------------------------*/
2223 #if ( configUSE_CORE_EXCLUSION == 1 )
2225 void vTaskCoreExclusionSet( const TaskHandle_t xTask,
2226 UBaseType_t uxCoreExclude )
2231 taskENTER_CRITICAL();
2233 pxTCB = prvGetTCBFromHandle( xTask );
2235 pxTCB->uxCoreExclude = uxCoreExclude;
2237 if( xSchedulerRunning != pdFALSE )
2239 if( taskTASK_IS_RUNNING( pxTCB->xTaskRunState ) )
2241 xCoreID = ( BaseType_t ) pxTCB->xTaskRunState;
2243 if( ( uxCoreExclude & ( 1 << xCoreID ) ) != 0 )
2245 prvYieldCore( xCoreID );
2250 taskEXIT_CRITICAL();
2253 #endif /* configUSE_CORE_EXCLUSION */
2254 /*-----------------------------------------------------------*/
2256 #if ( configUSE_CORE_EXCLUSION == 1 )
2258 UBaseType_t vTaskCoreExclusionGet( const TaskHandle_t xTask )
2261 UBaseType_t uxCoreExclude;
2263 taskENTER_CRITICAL();
2265 pxTCB = prvGetTCBFromHandle( xTask );
2266 uxCoreExclude = pxTCB->uxCoreExclude;
2268 taskEXIT_CRITICAL();
2270 return uxCoreExclude;
2273 #endif /* configUSE_CORE_EXCLUSION */
2274 /*-----------------------------------------------------------*/
2276 #if ( configUSE_TASK_PREEMPTION_DISABLE == 1 )
2278 void vTaskPreemptionDisable( const TaskHandle_t xTask )
2282 taskENTER_CRITICAL();
2284 pxTCB = prvGetTCBFromHandle( xTask );
2286 pxTCB->xPreemptionDisable = pdTRUE;
2288 taskEXIT_CRITICAL();
2291 #endif /* configUSE_TASK_PREEMPTION_DISABLE */
2292 /*-----------------------------------------------------------*/
2294 #if ( configUSE_TASK_PREEMPTION_DISABLE == 1 )
2296 void vTaskPreemptionEnable( const TaskHandle_t xTask )
2301 taskENTER_CRITICAL();
2303 pxTCB = prvGetTCBFromHandle( xTask );
2305 pxTCB->xPreemptionDisable = pdFALSE;
2307 if( xSchedulerRunning != pdFALSE )
2309 if( taskTASK_IS_RUNNING( pxTCB->xTaskRunState ) )
2311 xCoreID = ( BaseType_t ) pxTCB->xTaskRunState;
2312 prvYieldCore( xCoreID );
2316 taskEXIT_CRITICAL();
2319 #endif /* configUSE_TASK_PREEMPTION_DISABLE */
2320 /*-----------------------------------------------------------*/
2322 #if ( INCLUDE_vTaskSuspend == 1 )
2324 void vTaskSuspend( TaskHandle_t xTaskToSuspend )
2327 TaskRunning_t xTaskRunningOnCore;
2329 taskENTER_CRITICAL();
2331 /* If null is passed in here then it is the running task that is
2332 * being suspended. */
2333 pxTCB = prvGetTCBFromHandle( xTaskToSuspend );
2335 traceTASK_SUSPEND( pxTCB );
2337 xTaskRunningOnCore = pxTCB->xTaskRunState;
2339 /* Remove task from the ready/delayed list and place in the
2340 * suspended list. */
2341 if( uxListRemove( &( pxTCB->xStateListItem ) ) == ( UBaseType_t ) 0 )
2343 taskRESET_READY_PRIORITY( pxTCB->uxPriority );
2347 mtCOVERAGE_TEST_MARKER();
2350 /* Is the task waiting on an event also? */
2351 if( listLIST_ITEM_CONTAINER( &( pxTCB->xEventListItem ) ) != NULL )
2353 ( void ) uxListRemove( &( pxTCB->xEventListItem ) );
2357 mtCOVERAGE_TEST_MARKER();
2360 vListInsertEnd( &xSuspendedTaskList, &( pxTCB->xStateListItem ) );
2362 #if ( configUSE_TASK_NOTIFICATIONS == 1 )
2366 for( x = 0; x < configTASK_NOTIFICATION_ARRAY_ENTRIES; x++ )
2368 if( pxTCB->ucNotifyState[ x ] == taskWAITING_NOTIFICATION )
2370 /* The task was blocked to wait for a notification, but is
2371 * now suspended, so no notification was received. */
2372 pxTCB->ucNotifyState[ x ] = taskNOT_WAITING_NOTIFICATION;
2376 #endif /* if ( configUSE_TASK_NOTIFICATIONS == 1 ) */
2378 if( xSchedulerRunning != pdFALSE )
2380 /* Reset the next expected unblock time in case it referred to the
2381 * task that is now in the Suspended state. */
2382 prvResetNextTaskUnblockTime();
2386 mtCOVERAGE_TEST_MARKER();
2389 if( taskTASK_IS_RUNNING( xTaskRunningOnCore ) )
2391 if( xSchedulerRunning != pdFALSE )
2393 if( xTaskRunningOnCore == portGET_CORE_ID() )
2395 /* The current task has just been suspended. */
2396 configASSERT( uxSchedulerSuspended == 0 );
2397 vTaskYieldWithinAPI();
2401 prvYieldCore( xTaskRunningOnCore );
2404 taskEXIT_CRITICAL();
2408 taskEXIT_CRITICAL();
2410 configASSERT( pxTCB == pxCurrentTCBs[ xTaskRunningOnCore ] );
2412 /* The scheduler is not running, but the task that was pointed
2413 * to by pxCurrentTCB has just been suspended and pxCurrentTCB
2414 * must be adjusted to point to a different task. */
2415 if( listCURRENT_LIST_LENGTH( &xSuspendedTaskList ) == uxCurrentNumberOfTasks ) /*lint !e931 Right has no side effect, just volatile. */
2417 /* No other tasks are ready, so set the core's TCB back to
2418 * NULL so when the next task is created the core's TCB will
2419 * be able to be set to point to it no matter what its relative
2421 pxTCB->xTaskRunState = taskTASK_NOT_RUNNING;
2422 pxCurrentTCBs[ xTaskRunningOnCore ] = NULL;
2426 /* Attempt to switch in a new task. This could fail since the idle tasks
2427 * haven't been created yet. If it does then set the core's TCB back to
2429 if( prvSelectHighestPriorityTask( xTaskRunningOnCore ) == pdFALSE )
2431 pxTCB->xTaskRunState = taskTASK_NOT_RUNNING;
2432 pxCurrentTCBs[ xTaskRunningOnCore ] = NULL;
2439 taskEXIT_CRITICAL();
2441 } /* taskEXIT_CRITICAL() - already exited in one of three cases above */
2444 #endif /* INCLUDE_vTaskSuspend */
2445 /*-----------------------------------------------------------*/
2447 #if ( INCLUDE_vTaskSuspend == 1 )
2449 static BaseType_t prvTaskIsTaskSuspended( const TaskHandle_t xTask )
2451 BaseType_t xReturn = pdFALSE;
2452 const TCB_t * const pxTCB = xTask;
2454 /* Accesses xPendingReadyList so must be called from a critical
2457 /* It does not make sense to check if the calling task is suspended. */
2458 configASSERT( xTask );
2460 /* Is the task being resumed actually in the suspended list? */
2461 if( listIS_CONTAINED_WITHIN( &xSuspendedTaskList, &( pxTCB->xStateListItem ) ) != pdFALSE )
2463 /* Has the task already been resumed from within an ISR? */
2464 if( listIS_CONTAINED_WITHIN( &xPendingReadyList, &( pxTCB->xEventListItem ) ) == pdFALSE )
2466 /* Is it in the suspended list because it is in the Suspended
2467 * state, or because is is blocked with no timeout? */
2468 if( listIS_CONTAINED_WITHIN( NULL, &( pxTCB->xEventListItem ) ) != pdFALSE ) /*lint !e961. The cast is only redundant when NULL is used. */
2474 mtCOVERAGE_TEST_MARKER();
2479 mtCOVERAGE_TEST_MARKER();
2484 mtCOVERAGE_TEST_MARKER();
2488 } /*lint !e818 xTask cannot be a pointer to const because it is a typedef. */
2490 #endif /* INCLUDE_vTaskSuspend */
2491 /*-----------------------------------------------------------*/
2493 #if ( INCLUDE_vTaskSuspend == 1 )
2495 void vTaskResume( TaskHandle_t xTaskToResume )
2497 TCB_t * const pxTCB = xTaskToResume;
2499 /* It does not make sense to resume the calling task. */
2500 configASSERT( xTaskToResume );
2502 /* The parameter cannot be NULL as it is impossible to resume the
2503 * currently executing task. It is also impossible to resume a task
2504 * that is actively running on another core but it is too dangerous
2505 * to check their run state here. Safer to get into a critical section
2506 * and check if it is actually suspended or not below. */
2509 taskENTER_CRITICAL();
2511 if( prvTaskIsTaskSuspended( pxTCB ) != pdFALSE )
2513 traceTASK_RESUME( pxTCB );
2515 /* The ready list can be accessed even if the scheduler is
2516 * suspended because this is inside a critical section. */
2517 ( void ) uxListRemove( &( pxTCB->xStateListItem ) );
2518 prvAddTaskToReadyList( pxTCB );
2520 /* A higher priority task may have just been resumed. */
2521 #if ( configUSE_PREEMPTION == 1 )
2523 prvYieldForTask( pxTCB, pdTRUE );
2529 mtCOVERAGE_TEST_MARKER();
2532 taskEXIT_CRITICAL();
2536 mtCOVERAGE_TEST_MARKER();
2540 #endif /* INCLUDE_vTaskSuspend */
2542 /*-----------------------------------------------------------*/
2544 #if ( ( INCLUDE_xTaskResumeFromISR == 1 ) && ( INCLUDE_vTaskSuspend == 1 ) )
2546 BaseType_t xTaskResumeFromISR( TaskHandle_t xTaskToResume )
2548 BaseType_t xYieldRequired = pdFALSE;
2549 TCB_t * const pxTCB = xTaskToResume;
2550 UBaseType_t uxSavedInterruptStatus;
2552 configASSERT( xTaskToResume );
2554 /* RTOS ports that support interrupt nesting have the concept of a
2555 * maximum system call (or maximum API call) interrupt priority.
2556 * Interrupts that are above the maximum system call priority are keep
2557 * permanently enabled, even when the RTOS kernel is in a critical section,
2558 * but cannot make any calls to FreeRTOS API functions. If configASSERT()
2559 * is defined in FreeRTOSConfig.h then
2560 * portASSERT_IF_INTERRUPT_PRIORITY_INVALID() will result in an assertion
2561 * failure if a FreeRTOS API function is called from an interrupt that has
2562 * been assigned a priority above the configured maximum system call
2563 * priority. Only FreeRTOS functions that end in FromISR can be called
2564 * from interrupts that have been assigned a priority at or (logically)
2565 * below the maximum system call interrupt priority. FreeRTOS maintains a
2566 * separate interrupt safe API to ensure interrupt entry is as fast and as
2567 * simple as possible. More information (albeit Cortex-M specific) is
2568 * provided on the following link:
2569 * https://www.FreeRTOS.org/RTOS-Cortex-M3-M4.html */
2570 portASSERT_IF_INTERRUPT_PRIORITY_INVALID();
2572 uxSavedInterruptStatus = portSET_INTERRUPT_MASK_FROM_ISR();
2574 if( prvTaskIsTaskSuspended( pxTCB ) != pdFALSE )
2576 traceTASK_RESUME_FROM_ISR( pxTCB );
2578 /* Check the ready lists can be accessed. */
2579 if( uxSchedulerSuspended == ( UBaseType_t ) pdFALSE )
2581 /* Ready lists can be accessed so move the task from the
2582 * suspended list to the ready list directly. */
2584 ( void ) uxListRemove( &( pxTCB->xStateListItem ) );
2585 prvAddTaskToReadyList( pxTCB );
2589 /* The delayed or ready lists cannot be accessed so the task
2590 * is held in the pending ready list until the scheduler is
2592 vListInsertEnd( &( xPendingReadyList ), &( pxTCB->xEventListItem ) );
2595 #if ( configUSE_PREEMPTION == 1 )
2596 prvYieldForTask( pxTCB, pdTRUE );
2598 if( xYieldPendings[ portGET_CORE_ID() ] != pdFALSE )
2600 xYieldRequired = pdTRUE;
2606 mtCOVERAGE_TEST_MARKER();
2609 portCLEAR_INTERRUPT_MASK_FROM_ISR( uxSavedInterruptStatus );
2611 return xYieldRequired;
2614 #endif /* ( ( INCLUDE_xTaskResumeFromISR == 1 ) && ( INCLUDE_vTaskSuspend == 1 ) ) */
2615 /*-----------------------------------------------------------*/
2617 static BaseType_t prvCreateIdleTasks( void )
2619 BaseType_t xReturn = pdPASS;
2621 char cIdleName[ configMAX_TASK_NAME_LEN ];
2623 /* Add each idle task at the lowest priority. */
2624 for( xCoreID = ( BaseType_t ) 0; xCoreID < ( BaseType_t ) configNUM_CORES; xCoreID++ )
2628 if( xReturn == pdFAIL )
2634 mtCOVERAGE_TEST_MARKER();
2637 for( x = ( BaseType_t ) 0; x < ( BaseType_t ) configMAX_TASK_NAME_LEN; x++ )
2639 cIdleName[ x ] = configIDLE_TASK_NAME[ x ];
2641 /* Don't copy all configMAX_TASK_NAME_LEN if the string is shorter than
2642 * configMAX_TASK_NAME_LEN characters just in case the memory after the
2643 * string is not accessible (extremely unlikely). */
2644 if( cIdleName[ x ] == ( char ) 0x00 )
2650 mtCOVERAGE_TEST_MARKER();
2654 /* Append the idle task number to the end of the name if there is space */
2655 if( x < configMAX_TASK_NAME_LEN )
2657 cIdleName[ x++ ] = xCoreID + '0';
2659 /* And append a null character if there is space */
2660 if( x < configMAX_TASK_NAME_LEN )
2662 cIdleName[ x ] = '\0';
2666 mtCOVERAGE_TEST_MARKER();
2671 mtCOVERAGE_TEST_MARKER();
2674 #if ( configSUPPORT_STATIC_ALLOCATION == 1 )
2678 StaticTask_t * pxIdleTaskTCBBuffer = NULL;
2679 StackType_t * pxIdleTaskStackBuffer = NULL;
2680 uint32_t ulIdleTaskStackSize;
2682 /* The Idle task is created using user provided RAM - obtain the
2683 * address of the RAM then create the idle task. */
2684 vApplicationGetIdleTaskMemory( &pxIdleTaskTCBBuffer, &pxIdleTaskStackBuffer, &ulIdleTaskStackSize );
2685 xIdleTaskHandle[ xCoreID ] = xTaskCreateStatic( prvIdleTask,
2687 ulIdleTaskStackSize,
2688 ( void * ) NULL, /*lint !e961. The cast is not redundant for all compilers. */
2689 portPRIVILEGE_BIT, /* In effect ( tskIDLE_PRIORITY | portPRIVILEGE_BIT ), but tskIDLE_PRIORITY is zero. */
2690 pxIdleTaskStackBuffer,
2691 pxIdleTaskTCBBuffer ); /*lint !e961 MISRA exception, justified as it is not a redundant explicit cast to all supported compilers. */
2693 #if( configNUM_CORES > 1)
2696 static StaticTask_t xIdleTCBBuffers[configNUM_CORES-1];
2697 static StackType_t xIdleTaskStackBuffers[configMINIMAL_STACK_SIZE][configNUM_CORES-1];
2699 xIdleTaskHandle[ xCoreID ] = xTaskCreateStatic( prvMinimalIdleTask,
2701 configMINIMAL_STACK_SIZE,
2702 ( void * ) NULL, /*lint !e961. The cast is not redundant for all compilers. */
2703 portPRIVILEGE_BIT, /* In effect ( tskIDLE_PRIORITY | portPRIVILEGE_BIT ), but tskIDLE_PRIORITY is zero. */
2704 xIdleTaskStackBuffers[xCoreID-1],
2705 &xIdleTCBBuffers[xCoreID-1] ); /*lint !e961 MISRA exception, justified as it is not a redundant explicit cast to all supported compilers. */
2708 if( xIdleTaskHandle[ xCoreID ] != NULL )
2717 #else /* if ( configSUPPORT_STATIC_ALLOCATION == 1 ) */
2721 /* The Idle task is being created using dynamically allocated RAM. */
2722 xReturn = xTaskCreate( prvIdleTask,
2724 configMINIMAL_STACK_SIZE,
2726 portPRIVILEGE_BIT, /* In effect ( tskIDLE_PRIORITY | portPRIVILEGE_BIT ), but tskIDLE_PRIORITY is zero. */
2727 &xIdleTaskHandle[ xCoreID ] ); /*lint !e961 MISRA exception, justified as it is not a redundant explicit cast to all supported compilers. */
2729 #if( configNUM_CORES > 1 )
2732 xReturn = xTaskCreate( prvMinimalIdleTask,
2734 configMINIMAL_STACK_SIZE,
2736 portPRIVILEGE_BIT, /* In effect ( tskIDLE_PRIORITY | portPRIVILEGE_BIT ), but tskIDLE_PRIORITY is zero. */
2737 &xIdleTaskHandle[ xCoreID ] ); /*lint !e961 MISRA exception, justified as it is not a redundant explicit cast to all supported compilers. */
2741 #endif /* configSUPPORT_STATIC_ALLOCATION */
2746 void vTaskStartScheduler( void )
2750 #if ( configUSE_TIMERS == 1 )
2752 xReturn = xTimerCreateTimerTask();
2754 #endif /* configUSE_TIMERS */
2756 xReturn = prvCreateIdleTasks();
2758 if( xReturn == pdPASS )
2760 /* freertos_tasks_c_additions_init() should only be called if the user
2761 * definable macro FREERTOS_TASKS_C_ADDITIONS_INIT() is defined, as that is
2762 * the only macro called by the function. */
2763 #ifdef FREERTOS_TASKS_C_ADDITIONS_INIT
2765 freertos_tasks_c_additions_init();
2769 /* Interrupts are turned off here, to ensure a tick does not occur
2770 * before or during the call to xPortStartScheduler(). The stacks of
2771 * the created tasks contain a status word with interrupts switched on
2772 * so interrupts will automatically get re-enabled when the first task
2774 portDISABLE_INTERRUPTS();
2776 #if ( configUSE_NEWLIB_REENTRANT == 1 )
2778 /* Switch Newlib's _impure_ptr variable to point to the _reent
2779 * structure specific to the task that will run first.
2780 * See the third party link http://www.nadler.com/embedded/newlibAndFreeRTOS.html
2781 * for additional information. */
2782 _impure_ptr = &( pxCurrentTCB->xNewLib_reent );
2784 #endif /* configUSE_NEWLIB_REENTRANT */
2786 xNextTaskUnblockTime = portMAX_DELAY;
2787 xSchedulerRunning = pdTRUE;
2788 xTickCount = ( TickType_t ) configINITIAL_TICK_COUNT;
2790 /* If configGENERATE_RUN_TIME_STATS is defined then the following
2791 * macro must be defined to configure the timer/counter used to generate
2792 * the run time counter time base. NOTE: If configGENERATE_RUN_TIME_STATS
2793 * is set to 0 and the following line fails to build then ensure you do not
2794 * have portCONFIGURE_TIMER_FOR_RUN_TIME_STATS() defined in your
2795 * FreeRTOSConfig.h file. */
2796 portCONFIGURE_TIMER_FOR_RUN_TIME_STATS();
2798 traceTASK_SWITCHED_IN();
2800 /* Setting up the timer tick is hardware specific and thus in the
2801 * portable interface. */
2802 if( xPortStartScheduler() != pdFALSE )
2804 /* Should not reach here as if the scheduler is running the
2805 * function will not return. */
2809 /* Should only reach here if a task calls xTaskEndScheduler(). */
2814 /* This line will only be reached if the kernel could not be started,
2815 * because there was not enough FreeRTOS heap to create the idle task
2816 * or the timer task. */
2817 configASSERT( xReturn != errCOULD_NOT_ALLOCATE_REQUIRED_MEMORY );
2820 /* Prevent compiler warnings if INCLUDE_xTaskGetIdleTaskHandle is set to 0,
2821 * meaning xIdleTaskHandle is not used anywhere else. */
2822 ( void ) xIdleTaskHandle;
2824 /* OpenOCD makes use of uxTopUsedPriority for thread debugging. Prevent uxTopUsedPriority
2825 * from getting optimized out as it is no longer used by the kernel. */
2826 ( void ) uxTopUsedPriority;
2828 /*-----------------------------------------------------------*/
2830 void vTaskEndScheduler( void )
2832 /* Stop the scheduler interrupts and call the portable scheduler end
2833 * routine so the original ISRs can be restored if necessary. The port
2834 * layer must ensure interrupts enable bit is left in the correct state. */
2835 portDISABLE_INTERRUPTS();
2836 xSchedulerRunning = pdFALSE;
2837 vPortEndScheduler();
2839 /*----------------------------------------------------------*/
2841 void vTaskSuspendAll( void )
2843 UBaseType_t ulState;
2845 /* This must only be called from within a task */
2846 portASSERT_IF_IN_ISR();
2848 if( xSchedulerRunning != pdFALSE )
2850 /* writes to uxSchedulerSuspended must be protected by both the task AND ISR locks.
2851 * We must disable interrupts before we grab the locks in the event that this task is
2852 * interrupted and switches context before incrementing uxSchedulerSuspended.
2853 * It is safe to re-enable interrupts after releasing the ISR lock and incrementing
2854 * uxSchedulerSuspended since that will prevent context switches. */
2855 ulState = portDISABLE_INTERRUPTS();
2857 /* portSOFRWARE_BARRIER() is only implemented for emulated/simulated ports that
2858 * do not otherwise exhibit real time behaviour. */
2859 portSOFTWARE_BARRIER();
2861 portGET_TASK_LOCK();
2864 /* The scheduler is suspended if uxSchedulerSuspended is non-zero. An increment
2865 * is used to allow calls to vTaskSuspendAll() to nest. */
2866 ++uxSchedulerSuspended;
2867 portRELEASE_ISR_LOCK();
2869 if( ( uxSchedulerSuspended == 1U ) && ( pxCurrentTCB->uxCriticalNesting == 0U ) )
2871 prvCheckForRunStateChange();
2874 portRESTORE_INTERRUPTS( ulState );
2878 mtCOVERAGE_TEST_MARKER();
2881 /*----------------------------------------------------------*/
2883 #if ( configUSE_TICKLESS_IDLE != 0 )
2885 static TickType_t prvGetExpectedIdleTime( void )
2888 UBaseType_t uxHigherPriorityReadyTasks = pdFALSE;
2890 /* uxHigherPriorityReadyTasks takes care of the case where
2891 * configUSE_PREEMPTION is 0, so there may be tasks above the idle priority
2892 * task that are in the Ready state, even though the idle task is
2894 #if ( configUSE_PORT_OPTIMISED_TASK_SELECTION == 0 )
2896 if( uxTopReadyPriority > tskIDLE_PRIORITY )
2898 uxHigherPriorityReadyTasks = pdTRUE;
2903 const UBaseType_t uxLeastSignificantBit = ( UBaseType_t ) 0x01;
2905 /* When port optimised task selection is used the uxTopReadyPriority
2906 * variable is used as a bit map. If bits other than the least
2907 * significant bit are set then there are tasks that have a priority
2908 * above the idle priority that are in the Ready state. This takes
2909 * care of the case where the co-operative scheduler is in use. */
2910 if( uxTopReadyPriority > uxLeastSignificantBit )
2912 uxHigherPriorityReadyTasks = pdTRUE;
2915 #endif /* if ( configUSE_PORT_OPTIMISED_TASK_SELECTION == 0 ) */
2917 if( pxCurrentTCB->uxPriority > tskIDLE_PRIORITY )
2921 else if( listCURRENT_LIST_LENGTH( &( pxReadyTasksLists[ tskIDLE_PRIORITY ] ) ) > 1 )
2923 /* There are other idle priority tasks in the ready state. If
2924 * time slicing is used then the very next tick interrupt must be
2928 else if( uxHigherPriorityReadyTasks != pdFALSE )
2930 /* There are tasks in the Ready state that have a priority above the
2931 * idle priority. This path can only be reached if
2932 * configUSE_PREEMPTION is 0. */
2937 xReturn = xNextTaskUnblockTime - xTickCount;
2943 #endif /* configUSE_TICKLESS_IDLE */
2944 /*----------------------------------------------------------*/
2946 BaseType_t xTaskResumeAll( void )
2948 TCB_t * pxTCB = NULL;
2949 BaseType_t xAlreadyYielded = pdFALSE;
2951 if( xSchedulerRunning != pdFALSE )
2953 /* It is possible that an ISR caused a task to be removed from an event
2954 * list while the scheduler was suspended. If this was the case then the
2955 * removed task will have been added to the xPendingReadyList. Once the
2956 * scheduler has been resumed it is safe to move all the pending ready
2957 * tasks from this list into their appropriate ready list. */
2958 taskENTER_CRITICAL();
2962 xCoreID = portGET_CORE_ID();
2964 /* If uxSchedulerSuspended is zero then this function does not match a
2965 * previous call to vTaskSuspendAll(). */
2966 configASSERT( uxSchedulerSuspended );
2968 --uxSchedulerSuspended;
2969 portRELEASE_TASK_LOCK();
2971 if( uxSchedulerSuspended == ( UBaseType_t ) pdFALSE )
2973 if( uxCurrentNumberOfTasks > ( UBaseType_t ) 0U )
2975 /* Move any readied tasks from the pending list into the
2976 * appropriate ready list. */
2977 while( listLIST_IS_EMPTY( &xPendingReadyList ) == pdFALSE )
2979 pxTCB = listGET_OWNER_OF_HEAD_ENTRY( ( &xPendingReadyList ) ); /*lint !e9079 void * is used as this macro is used with timers and co-routines too. Alignment is known to be fine as the type of the pointer stored and retrieved is the same. */
2980 ( void ) uxListRemove( &( pxTCB->xEventListItem ) );
2981 ( void ) uxListRemove( &( pxTCB->xStateListItem ) );
2982 prvAddTaskToReadyList( pxTCB );
2984 /* All appropriate tasks yield at the moment a task is added to xPendingReadyList.
2985 * If the current core yielded then vTaskSwitchContext() has already been called
2986 * which sets xYieldPendings for the current core to pdTRUE. */
2991 /* A task was unblocked while the scheduler was suspended,
2992 * which may have prevented the next unblock time from being
2993 * re-calculated, in which case re-calculate it now. Mainly
2994 * important for low power tickless implementations, where
2995 * this can prevent an unnecessary exit from low power
2997 prvResetNextTaskUnblockTime();
3000 /* If any ticks occurred while the scheduler was suspended then
3001 * they should be processed now. This ensures the tick count does
3002 * not slip, and that any delayed tasks are resumed at the correct
3005 * It should be safe to call xTaskIncrementTick here from any core
3006 * since we are in a critical section and xTaskIncrementTick itself
3007 * protects itself within a critical section. Suspending the scheduler
3008 * from any core causes xTaskIncrementTick to increment uxPendedCounts.*/
3010 TickType_t xPendedCounts = xPendedTicks; /* Non-volatile copy. */
3012 if( xPendedCounts > ( TickType_t ) 0U )
3016 if( xTaskIncrementTick() != pdFALSE )
3018 /* other cores are interrupted from
3019 * within xTaskIncrementTick(). */
3020 xYieldPendings[ xCoreID ] = pdTRUE;
3024 mtCOVERAGE_TEST_MARKER();
3028 } while( xPendedCounts > ( TickType_t ) 0U );
3034 mtCOVERAGE_TEST_MARKER();
3038 if( xYieldPendings[ xCoreID ] != pdFALSE )
3040 /* If xYieldPendings is true then taskEXIT_CRITICAL()
3041 * will yield, so make sure we return true to let the
3042 * caller know a yield has already happened. */
3043 xAlreadyYielded = pdTRUE;
3049 mtCOVERAGE_TEST_MARKER();
3052 taskEXIT_CRITICAL();
3056 mtCOVERAGE_TEST_MARKER();
3059 return xAlreadyYielded;
3061 /*-----------------------------------------------------------*/
3063 TickType_t xTaskGetTickCount( void )
3067 /* Critical section required if running on a 16 bit processor. */
3068 portTICK_TYPE_ENTER_CRITICAL();
3070 xTicks = xTickCount;
3072 portTICK_TYPE_EXIT_CRITICAL();
3076 /*-----------------------------------------------------------*/
3078 TickType_t xTaskGetTickCountFromISR( void )
3081 UBaseType_t uxSavedInterruptStatus;
3083 /* RTOS ports that support interrupt nesting have the concept of a maximum
3084 * system call (or maximum API call) interrupt priority. Interrupts that are
3085 * above the maximum system call priority are kept permanently enabled, even
3086 * when the RTOS kernel is in a critical section, but cannot make any calls to
3087 * FreeRTOS API functions. If configASSERT() is defined in FreeRTOSConfig.h
3088 * then portASSERT_IF_INTERRUPT_PRIORITY_INVALID() will result in an assertion
3089 * failure if a FreeRTOS API function is called from an interrupt that has been
3090 * assigned a priority above the configured maximum system call priority.
3091 * Only FreeRTOS functions that end in FromISR can be called from interrupts
3092 * that have been assigned a priority at or (logically) below the maximum
3093 * system call interrupt priority. FreeRTOS maintains a separate interrupt
3094 * safe API to ensure interrupt entry is as fast and as simple as possible.
3095 * More information (albeit Cortex-M specific) is provided on the following
3096 * link: https://www.FreeRTOS.org/RTOS-Cortex-M3-M4.html */
3097 portASSERT_IF_INTERRUPT_PRIORITY_INVALID();
3099 uxSavedInterruptStatus = portTICK_TYPE_SET_INTERRUPT_MASK_FROM_ISR();
3101 xReturn = xTickCount;
3103 portTICK_TYPE_CLEAR_INTERRUPT_MASK_FROM_ISR( uxSavedInterruptStatus );
3107 /*-----------------------------------------------------------*/
3109 UBaseType_t uxTaskGetNumberOfTasks( void )
3111 /* A critical section is not required because the variables are of type
3113 return uxCurrentNumberOfTasks;
3115 /*-----------------------------------------------------------*/
3117 char * pcTaskGetName( TaskHandle_t xTaskToQuery ) /*lint !e971 Unqualified char types are allowed for strings and single characters only. */
3121 /* If null is passed in here then the name of the calling task is being
3123 pxTCB = prvGetTCBFromHandle( xTaskToQuery );
3124 configASSERT( pxTCB );
3125 return &( pxTCB->pcTaskName[ 0 ] );
3127 /*-----------------------------------------------------------*/
3129 #if ( INCLUDE_xTaskGetHandle == 1 )
3131 static TCB_t * prvSearchForNameWithinSingleList( List_t * pxList,
3132 const char pcNameToQuery[] )
3134 TCB_t * pxNextTCB, * pxFirstTCB, * pxReturn = NULL;
3137 BaseType_t xBreakLoop;
3139 /* This function is called with the scheduler suspended. */
3141 if( listCURRENT_LIST_LENGTH( pxList ) > ( UBaseType_t ) 0 )
3143 listGET_OWNER_OF_NEXT_ENTRY( pxFirstTCB, pxList ); /*lint !e9079 void * is used as this macro is used with timers and co-routines too. Alignment is known to be fine as the type of the pointer stored and retrieved is the same. */
3147 listGET_OWNER_OF_NEXT_ENTRY( pxNextTCB, pxList ); /*lint !e9079 void * is used as this macro is used with timers and co-routines too. Alignment is known to be fine as the type of the pointer stored and retrieved is the same. */
3149 /* Check each character in the name looking for a match or
3151 xBreakLoop = pdFALSE;
3153 for( x = ( UBaseType_t ) 0; x < ( UBaseType_t ) configMAX_TASK_NAME_LEN; x++ )
3155 cNextChar = pxNextTCB->pcTaskName[ x ];
3157 if( cNextChar != pcNameToQuery[ x ] )
3159 /* Characters didn't match. */
3160 xBreakLoop = pdTRUE;
3162 else if( cNextChar == ( char ) 0x00 )
3164 /* Both strings terminated, a match must have been
3166 pxReturn = pxNextTCB;
3167 xBreakLoop = pdTRUE;
3171 mtCOVERAGE_TEST_MARKER();
3174 if( xBreakLoop != pdFALSE )
3180 if( pxReturn != NULL )
3182 /* The handle has been found. */
3185 } while( pxNextTCB != pxFirstTCB );
3189 mtCOVERAGE_TEST_MARKER();
3195 #endif /* INCLUDE_xTaskGetHandle */
3196 /*-----------------------------------------------------------*/
3198 #if ( INCLUDE_xTaskGetHandle == 1 )
3200 TaskHandle_t xTaskGetHandle( const char * pcNameToQuery ) /*lint !e971 Unqualified char types are allowed for strings and single characters only. */
3202 UBaseType_t uxQueue = configMAX_PRIORITIES;
3205 /* Task names will be truncated to configMAX_TASK_NAME_LEN - 1 bytes. */
3206 configASSERT( strlen( pcNameToQuery ) < configMAX_TASK_NAME_LEN );
3210 /* Search the ready lists. */
3214 pxTCB = prvSearchForNameWithinSingleList( ( List_t * ) &( pxReadyTasksLists[ uxQueue ] ), pcNameToQuery );
3218 /* Found the handle. */
3221 } while( uxQueue > ( UBaseType_t ) tskIDLE_PRIORITY ); /*lint !e961 MISRA exception as the casts are only redundant for some ports. */
3223 /* Search the delayed lists. */
3226 pxTCB = prvSearchForNameWithinSingleList( ( List_t * ) pxDelayedTaskList, pcNameToQuery );
3231 pxTCB = prvSearchForNameWithinSingleList( ( List_t * ) pxOverflowDelayedTaskList, pcNameToQuery );
3234 #if ( INCLUDE_vTaskSuspend == 1 )
3238 /* Search the suspended list. */
3239 pxTCB = prvSearchForNameWithinSingleList( &xSuspendedTaskList, pcNameToQuery );
3244 #if ( INCLUDE_vTaskDelete == 1 )
3248 /* Search the deleted list. */
3249 pxTCB = prvSearchForNameWithinSingleList( &xTasksWaitingTermination, pcNameToQuery );
3254 ( void ) xTaskResumeAll();
3259 #endif /* INCLUDE_xTaskGetHandle */
3260 /*-----------------------------------------------------------*/
3262 #if ( configUSE_TRACE_FACILITY == 1 )
3264 UBaseType_t uxTaskGetSystemState( TaskStatus_t * const pxTaskStatusArray,
3265 const UBaseType_t uxArraySize,
3266 uint32_t * const pulTotalRunTime )
3268 UBaseType_t uxTask = 0, uxQueue = configMAX_PRIORITIES;
3272 /* Is there a space in the array for each task in the system? */
3273 if( uxArraySize >= uxCurrentNumberOfTasks )
3275 /* Fill in an TaskStatus_t structure with information on each
3276 * task in the Ready state. */
3280 uxTask += prvListTasksWithinSingleList( &( pxTaskStatusArray[ uxTask ] ), &( pxReadyTasksLists[ uxQueue ] ), eReady );
3281 } while( uxQueue > ( UBaseType_t ) tskIDLE_PRIORITY ); /*lint !e961 MISRA exception as the casts are only redundant for some ports. */
3283 /* Fill in an TaskStatus_t structure with information on each
3284 * task in the Blocked state. */
3285 uxTask += prvListTasksWithinSingleList( &( pxTaskStatusArray[ uxTask ] ), ( List_t * ) pxDelayedTaskList, eBlocked );
3286 uxTask += prvListTasksWithinSingleList( &( pxTaskStatusArray[ uxTask ] ), ( List_t * ) pxOverflowDelayedTaskList, eBlocked );
3288 #if ( INCLUDE_vTaskDelete == 1 )
3290 /* Fill in an TaskStatus_t structure with information on
3291 * each task that has been deleted but not yet cleaned up. */
3292 uxTask += prvListTasksWithinSingleList( &( pxTaskStatusArray[ uxTask ] ), &xTasksWaitingTermination, eDeleted );
3296 #if ( INCLUDE_vTaskSuspend == 1 )
3298 /* Fill in an TaskStatus_t structure with information on
3299 * each task in the Suspended state. */
3300 uxTask += prvListTasksWithinSingleList( &( pxTaskStatusArray[ uxTask ] ), &xSuspendedTaskList, eSuspended );
3304 #if ( configGENERATE_RUN_TIME_STATS == 1 )
3306 if( pulTotalRunTime != NULL )
3308 #ifdef portALT_GET_RUN_TIME_COUNTER_VALUE
3309 portALT_GET_RUN_TIME_COUNTER_VALUE( ( *pulTotalRunTime ) );
3311 *pulTotalRunTime = portGET_RUN_TIME_COUNTER_VALUE();
3315 #else /* if ( configGENERATE_RUN_TIME_STATS == 1 ) */
3317 if( pulTotalRunTime != NULL )
3319 *pulTotalRunTime = 0;
3322 #endif /* if ( configGENERATE_RUN_TIME_STATS == 1 ) */
3326 mtCOVERAGE_TEST_MARKER();
3329 ( void ) xTaskResumeAll();
3334 #endif /* configUSE_TRACE_FACILITY */
3335 /*----------------------------------------------------------*/
3337 #if ( INCLUDE_xTaskGetIdleTaskHandle == 1 )
3339 TaskHandle_t * xTaskGetIdleTaskHandle( void )
3341 /* If xTaskGetIdleTaskHandle() is called before the scheduler has been
3342 * started, then xIdleTaskHandle will be NULL. */
3343 configASSERT( ( xIdleTaskHandle != NULL ) );
3344 return &( xIdleTaskHandle[ 0 ] );
3347 #endif /* INCLUDE_xTaskGetIdleTaskHandle */
3348 /*----------------------------------------------------------*/
3350 /* This conditional compilation should use inequality to 0, not equality to 1.
3351 * This is to ensure vTaskStepTick() is available when user defined low power mode
3352 * implementations require configUSE_TICKLESS_IDLE to be set to a value other than
3354 #if ( configUSE_TICKLESS_IDLE != 0 )
3356 void vTaskStepTick( const TickType_t xTicksToJump )
3358 /* Correct the tick count value after a period during which the tick
3359 * was suppressed. Note this does *not* call the tick hook function for
3360 * each stepped tick. */
3361 configASSERT( ( xTickCount + xTicksToJump ) <= xNextTaskUnblockTime );
3362 xTickCount += xTicksToJump;
3363 traceINCREASE_TICK_COUNT( xTicksToJump );
3366 #endif /* configUSE_TICKLESS_IDLE */
3367 /*----------------------------------------------------------*/
3369 BaseType_t xTaskCatchUpTicks( TickType_t xTicksToCatchUp )
3371 BaseType_t xYieldOccurred;
3373 /* Must not be called with the scheduler suspended as the implementation
3374 * relies on xPendedTicks being wound down to 0 in xTaskResumeAll(). */
3375 configASSERT( uxSchedulerSuspended == 0 );
3377 /* Use xPendedTicks to mimic xTicksToCatchUp number of ticks occurring when
3378 * the scheduler is suspended so the ticks are executed in xTaskResumeAll(). */
3380 xPendedTicks += xTicksToCatchUp;
3381 xYieldOccurred = xTaskResumeAll();
3383 return xYieldOccurred;
3385 /*----------------------------------------------------------*/
3387 #if ( INCLUDE_xTaskAbortDelay == 1 )
3389 BaseType_t xTaskAbortDelay( TaskHandle_t xTask )
3391 TCB_t * pxTCB = xTask;
3394 configASSERT( pxTCB );
3398 /* A task can only be prematurely removed from the Blocked state if
3399 * it is actually in the Blocked state. */
3400 if( eTaskGetState( xTask ) == eBlocked )
3404 /* Remove the reference to the task from the blocked list. An
3405 * interrupt won't touch the xStateListItem because the
3406 * scheduler is suspended. */
3407 ( void ) uxListRemove( &( pxTCB->xStateListItem ) );
3409 /* Is the task waiting on an event also? If so remove it from
3410 * the event list too. Interrupts can touch the event list item,
3411 * even though the scheduler is suspended, so a critical section
3413 taskENTER_CRITICAL();
3415 if( listLIST_ITEM_CONTAINER( &( pxTCB->xEventListItem ) ) != NULL )
3417 ( void ) uxListRemove( &( pxTCB->xEventListItem ) );
3419 /* This lets the task know it was forcibly removed from the
3420 * blocked state so it should not re-evaluate its block time and
3421 * then block again. */
3422 pxTCB->ucDelayAborted = pdTRUE;
3426 mtCOVERAGE_TEST_MARKER();
3429 taskEXIT_CRITICAL();
3431 /* Place the unblocked task into the appropriate ready list. */
3432 prvAddTaskToReadyList( pxTCB );
3434 /* A task being unblocked cannot cause an immediate context
3435 * switch if preemption is turned off. */
3436 #if ( configUSE_PREEMPTION == 1 )
3438 taskENTER_CRITICAL();
3440 prvYieldForTask( pxTCB, pdFALSE );
3442 taskEXIT_CRITICAL();
3444 #endif /* configUSE_PREEMPTION */
3451 ( void ) xTaskResumeAll();
3456 #endif /* INCLUDE_xTaskAbortDelay */
3457 /*----------------------------------------------------------*/
3459 BaseType_t xTaskIncrementTick( void )
3462 TickType_t xItemValue;
3463 BaseType_t xSwitchRequired = pdFALSE;
3465 #if ( configUSE_PREEMPTION == 1 )
3467 BaseType_t xCoreYieldList[ configNUM_CORES ] = { pdFALSE };
3468 #endif /* configUSE_PREEMPTION */
3470 taskENTER_CRITICAL();
3472 /* Called by the portable layer each time a tick interrupt occurs.
3473 * Increments the tick then checks to see if the new tick value will cause any
3474 * tasks to be unblocked. */
3475 traceTASK_INCREMENT_TICK( xTickCount );
3477 /* Tick increment should occur on every kernel timer event. Core 0 has the
3478 * responsibility to increment the tick, or increment the pended ticks if the
3479 * scheduler is suspended. If pended ticks is greater than zero, the core that
3480 * calls xTaskResumeAll has the responsibility to increment the tick. */
3481 if( uxSchedulerSuspended == ( UBaseType_t ) pdFALSE )
3483 /* Minor optimisation. The tick count cannot change in this
3485 const TickType_t xConstTickCount = xTickCount + ( TickType_t ) 1;
3487 /* Increment the RTOS tick, switching the delayed and overflowed
3488 * delayed lists if it wraps to 0. */
3489 xTickCount = xConstTickCount;
3491 if( xConstTickCount == ( TickType_t ) 0U ) /*lint !e774 'if' does not always evaluate to false as it is looking for an overflow. */
3493 taskSWITCH_DELAYED_LISTS();
3497 mtCOVERAGE_TEST_MARKER();
3500 /* See if this tick has made a timeout expire. Tasks are stored in
3501 * the queue in the order of their wake time - meaning once one task
3502 * has been found whose block time has not expired there is no need to
3503 * look any further down the list. */
3504 if( xConstTickCount >= xNextTaskUnblockTime )
3508 if( listLIST_IS_EMPTY( pxDelayedTaskList ) != pdFALSE )
3510 /* The delayed list is empty. Set xNextTaskUnblockTime
3511 * to the maximum possible value so it is extremely
3513 * if( xTickCount >= xNextTaskUnblockTime ) test will pass
3514 * next time through. */
3515 xNextTaskUnblockTime = portMAX_DELAY; /*lint !e961 MISRA exception as the casts are only redundant for some ports. */
3520 /* The delayed list is not empty, get the value of the
3521 * item at the head of the delayed list. This is the time
3522 * at which the task at the head of the delayed list must
3523 * be removed from the Blocked state. */
3524 pxTCB = listGET_OWNER_OF_HEAD_ENTRY( pxDelayedTaskList ); /*lint !e9079 void * is used as this macro is used with timers and co-routines too. Alignment is known to be fine as the type of the pointer stored and retrieved is the same. */
3525 xItemValue = listGET_LIST_ITEM_VALUE( &( pxTCB->xStateListItem ) );
3527 if( xConstTickCount < xItemValue )
3529 /* It is not time to unblock this item yet, but the
3530 * item value is the time at which the task at the head
3531 * of the blocked list must be removed from the Blocked
3532 * state - so record the item value in
3533 * xNextTaskUnblockTime. */
3534 xNextTaskUnblockTime = xItemValue;
3535 break; /*lint !e9011 Code structure here is deemed easier to understand with multiple breaks. */
3539 mtCOVERAGE_TEST_MARKER();
3542 /* It is time to remove the item from the Blocked state. */
3543 ( void ) uxListRemove( &( pxTCB->xStateListItem ) );
3545 /* Is the task waiting on an event also? If so remove
3546 * it from the event list. */
3547 if( listLIST_ITEM_CONTAINER( &( pxTCB->xEventListItem ) ) != NULL )
3549 ( void ) uxListRemove( &( pxTCB->xEventListItem ) );
3553 mtCOVERAGE_TEST_MARKER();
3556 /* Place the unblocked task into the appropriate ready
3558 prvAddTaskToReadyList( pxTCB );
3560 /* A task being unblocked cannot cause an immediate
3561 * context switch if preemption is turned off. */
3562 #if ( configUSE_PREEMPTION == 1 )
3564 prvYieldForTask( pxTCB, pdTRUE );
3566 #endif /* configUSE_PREEMPTION */
3571 /* Tasks of equal priority to the currently running task will share
3572 * processing time (time slice) if preemption is on, and the application
3573 * writer has not explicitly turned time slicing off. */
3574 #if ( ( configUSE_PREEMPTION == 1 ) && ( configUSE_TIME_SLICING == 1 ) )
3576 /* TODO: If there are fewer "non-IDLE" READY tasks than cores, do not
3577 * force a context switch that would just shuffle tasks around cores */
3578 /* TODO: There are certainly better ways of doing this that would reduce
3579 * the number of interrupts and also potentially help prevent tasks from
3580 * moving between cores as often. This, however, works for now. */
3581 for( x = ( UBaseType_t ) 0; x < ( UBaseType_t ) configNUM_CORES; x++ )
3583 if( listCURRENT_LIST_LENGTH( &( pxReadyTasksLists[ pxCurrentTCBs[ x ]->uxPriority ] ) ) > ( UBaseType_t ) 1 )
3585 xCoreYieldList[ x ] = pdTRUE;
3589 mtCOVERAGE_TEST_MARKER();
3593 #endif /* ( ( configUSE_PREEMPTION == 1 ) && ( configUSE_TIME_SLICING == 1 ) ) */
3595 #if ( configUSE_TICK_HOOK == 1 )
3597 /* Guard against the tick hook being called when the pended tick
3598 * count is being unwound (when the scheduler is being unlocked). */
3599 if( xPendedTicks == ( TickType_t ) 0 )
3601 vApplicationTickHook();
3605 mtCOVERAGE_TEST_MARKER();
3608 #endif /* configUSE_TICK_HOOK */
3610 #if ( configUSE_PREEMPTION == 1 )
3612 for( x = ( UBaseType_t ) 0; x < ( UBaseType_t ) configNUM_CORES; x++ )
3614 if( xYieldPendings[ x ] != pdFALSE )
3616 xCoreYieldList[ x ] = pdTRUE;
3620 mtCOVERAGE_TEST_MARKER();
3624 #endif /* configUSE_PREEMPTION */
3626 #if ( configUSE_PREEMPTION == 1 )
3630 xCoreID = portGET_CORE_ID();
3632 for( x = ( UBaseType_t ) 0; x < ( UBaseType_t ) configNUM_CORES; x++ )
3634 #if ( configUSE_TASK_PREEMPTION_DISABLE == 1 )
3635 if( pxCurrentTCBs[ x ]->xPreemptionDisable == pdFALSE )
3638 if( xCoreYieldList[ x ] != pdFALSE )
3642 xSwitchRequired = pdTRUE;
3651 mtCOVERAGE_TEST_MARKER();
3656 #endif /* configUSE_PREEMPTION */
3662 /* The tick hook gets called at regular intervals, even if the
3663 * scheduler is locked. */
3664 #if ( configUSE_TICK_HOOK == 1 )
3666 vApplicationTickHook();
3671 taskEXIT_CRITICAL();
3673 return xSwitchRequired;
3675 /*-----------------------------------------------------------*/
3677 #if ( configUSE_APPLICATION_TASK_TAG == 1 )
3679 void vTaskSetApplicationTaskTag( TaskHandle_t xTask,
3680 TaskHookFunction_t pxHookFunction )
3684 /* If xTask is NULL then it is the task hook of the calling task that is
3688 xTCB = ( TCB_t * ) pxCurrentTCB;
3695 /* Save the hook function in the TCB. A critical section is required as
3696 * the value can be accessed from an interrupt. */
3697 taskENTER_CRITICAL();
3699 xTCB->pxTaskTag = pxHookFunction;
3701 taskEXIT_CRITICAL();
3704 #endif /* configUSE_APPLICATION_TASK_TAG */
3705 /*-----------------------------------------------------------*/
3707 #if ( configUSE_APPLICATION_TASK_TAG == 1 )
3709 TaskHookFunction_t xTaskGetApplicationTaskTag( TaskHandle_t xTask )
3712 TaskHookFunction_t xReturn;
3714 /* If xTask is NULL then set the calling task's hook. */
3715 pxTCB = prvGetTCBFromHandle( xTask );
3717 /* Save the hook function in the TCB. A critical section is required as
3718 * the value can be accessed from an interrupt. */
3719 taskENTER_CRITICAL();
3721 xReturn = pxTCB->pxTaskTag;
3723 taskEXIT_CRITICAL();
3728 #endif /* configUSE_APPLICATION_TASK_TAG */
3729 /*-----------------------------------------------------------*/
3731 #if ( configUSE_APPLICATION_TASK_TAG == 1 )
3733 TaskHookFunction_t xTaskGetApplicationTaskTagFromISR( TaskHandle_t xTask )
3736 TaskHookFunction_t xReturn;
3737 UBaseType_t uxSavedInterruptStatus;
3739 /* If xTask is NULL then set the calling task's hook. */
3740 pxTCB = prvGetTCBFromHandle( xTask );
3742 /* Save the hook function in the TCB. A critical section is required as
3743 * the value can be accessed from an interrupt. */
3744 uxSavedInterruptStatus = portSET_INTERRUPT_MASK_FROM_ISR();
3746 xReturn = pxTCB->pxTaskTag;
3748 portCLEAR_INTERRUPT_MASK_FROM_ISR( uxSavedInterruptStatus );
3753 #endif /* configUSE_APPLICATION_TASK_TAG */
3754 /*-----------------------------------------------------------*/
3756 #if ( configUSE_APPLICATION_TASK_TAG == 1 )
3758 BaseType_t xTaskCallApplicationTaskHook( TaskHandle_t xTask,
3759 void * pvParameter )
3764 /* If xTask is NULL then we are calling our own task hook. */
3767 xTCB = pxCurrentTCB;
3774 if( xTCB->pxTaskTag != NULL )
3776 xReturn = xTCB->pxTaskTag( pvParameter );
3786 #endif /* configUSE_APPLICATION_TASK_TAG */
3787 /*-----------------------------------------------------------*/
3789 void vTaskSwitchContext( BaseType_t xCoreID )
3791 /* Acquire both locks:
3792 * - The ISR lock protects the ready list from simultaneous access by
3793 * both other ISRs and tasks.
3794 * - We also take the task lock to pause here in case another core has
3795 * suspended the scheduler. We don't want to simply set xYieldPending
3796 * and move on if another core suspended the scheduler. We should only
3797 * do that if the current core has suspended the scheduler. */
3799 portGET_TASK_LOCK(); /* Must always acquire the task lock first */
3802 /* vTaskSwitchContext() must never be called from within a critical section.
3803 * This is not necessarily true for vanilla FreeRTOS, but it is for this SMP port. */
3804 configASSERT( pxCurrentTCB->uxCriticalNesting == 0 );
3806 if( uxSchedulerSuspended != ( UBaseType_t ) pdFALSE )
3808 /* The scheduler is currently suspended - do not allow a context
3810 xYieldPendings[ xCoreID ] = pdTRUE;
3814 xYieldPendings[ xCoreID ] = pdFALSE;
3815 traceTASK_SWITCHED_OUT();
3817 #if ( configGENERATE_RUN_TIME_STATS == 1 )
3819 #ifdef portALT_GET_RUN_TIME_COUNTER_VALUE
3820 portALT_GET_RUN_TIME_COUNTER_VALUE( ulTotalRunTime );
3822 ulTotalRunTime = portGET_RUN_TIME_COUNTER_VALUE();
3825 /* Add the amount of time the task has been running to the
3826 * accumulated time so far. The time the task started running was
3827 * stored in ulTaskSwitchedInTime. Note that there is no overflow
3828 * protection here so count values are only valid until the timer
3829 * overflows. The guard against negative values is to protect
3830 * against suspect run time stat counter implementations - which
3831 * are provided by the application, not the kernel. */
3832 if( ulTotalRunTime > ulTaskSwitchedInTime )
3834 pxCurrentTCB->ulRunTimeCounter += ( ulTotalRunTime - ulTaskSwitchedInTime );
3838 mtCOVERAGE_TEST_MARKER();
3841 ulTaskSwitchedInTime = ulTotalRunTime;
3843 #endif /* configGENERATE_RUN_TIME_STATS */
3845 /* Check for stack overflow, if configured. */
3846 taskCHECK_FOR_STACK_OVERFLOW();
3848 /* Before the currently running task is switched out, save its errno. */
3849 #if ( configUSE_POSIX_ERRNO == 1 )
3851 pxCurrentTCB->iTaskErrno = FreeRTOS_errno;
3855 /* Select a new task to run using either the generic C or port
3856 * optimised asm code. */
3857 ( void ) prvSelectHighestPriorityTask( xCoreID );
3858 traceTASK_SWITCHED_IN();
3860 /* After the new task is switched in, update the global errno. */
3861 #if ( configUSE_POSIX_ERRNO == 1 )
3863 FreeRTOS_errno = pxCurrentTCB->iTaskErrno;
3867 #if ( configUSE_NEWLIB_REENTRANT == 1 )
3869 /* Switch Newlib's _impure_ptr variable to point to the _reent
3870 * structure specific to this task.
3871 * See the third party link http://www.nadler.com/embedded/newlibAndFreeRTOS.html
3872 * for additional information. */
3873 _impure_ptr = &( pxCurrentTCB->xNewLib_reent );
3875 #endif /* configUSE_NEWLIB_REENTRANT */
3878 portRELEASE_ISR_LOCK();
3879 portRELEASE_TASK_LOCK();
3881 /*-----------------------------------------------------------*/
3883 void vTaskPlaceOnEventList( List_t * const pxEventList,
3884 const TickType_t xTicksToWait )
3886 configASSERT( pxEventList );
3888 /* THIS FUNCTION MUST BE CALLED WITH EITHER INTERRUPTS DISABLED OR THE
3889 * SCHEDULER SUSPENDED AND THE QUEUE BEING ACCESSED LOCKED. */
3891 /* Place the event list item of the TCB in the appropriate event list.
3892 * This is placed in the list in priority order so the highest priority task
3893 * is the first to be woken by the event. The queue that contains the event
3894 * list is locked, preventing simultaneous access from interrupts. */
3895 vListInsert( pxEventList, &( pxCurrentTCB->xEventListItem ) );
3897 prvAddCurrentTaskToDelayedList( xTicksToWait, pdTRUE );
3899 /*-----------------------------------------------------------*/
3901 void vTaskPlaceOnUnorderedEventList( List_t * pxEventList,
3902 const TickType_t xItemValue,
3903 const TickType_t xTicksToWait )
3905 configASSERT( pxEventList );
3907 /* THIS FUNCTION MUST BE CALLED WITH THE SCHEDULER SUSPENDED. It is used by
3908 * the event groups implementation. */
3909 configASSERT( uxSchedulerSuspended != 0 );
3911 /* Store the item value in the event list item. It is safe to access the
3912 * event list item here as interrupts won't access the event list item of a
3913 * task that is not in the Blocked state. */
3914 listSET_LIST_ITEM_VALUE( &( pxCurrentTCB->xEventListItem ), xItemValue | taskEVENT_LIST_ITEM_VALUE_IN_USE );
3916 /* Place the event list item of the TCB at the end of the appropriate event
3917 * list. It is safe to access the event list here because it is part of an
3918 * event group implementation - and interrupts don't access event groups
3919 * directly (instead they access them indirectly by pending function calls to
3920 * the task level). */
3921 vListInsertEnd( pxEventList, &( pxCurrentTCB->xEventListItem ) );
3923 prvAddCurrentTaskToDelayedList( xTicksToWait, pdTRUE );
3925 /*-----------------------------------------------------------*/
3927 #if ( configUSE_TIMERS == 1 )
3929 void vTaskPlaceOnEventListRestricted( List_t * const pxEventList,
3930 TickType_t xTicksToWait,
3931 const BaseType_t xWaitIndefinitely )
3933 configASSERT( pxEventList );
3935 /* This function should not be called by application code hence the
3936 * 'Restricted' in its name. It is not part of the public API. It is
3937 * designed for use by kernel code, and has special calling requirements -
3938 * it should be called with the scheduler suspended. */
3941 /* Place the event list item of the TCB in the appropriate event list.
3942 * In this case it is assume that this is the only task that is going to
3943 * be waiting on this event list, so the faster vListInsertEnd() function
3944 * can be used in place of vListInsert. */
3945 vListInsertEnd( pxEventList, &( pxCurrentTCB->xEventListItem ) );
3947 /* If the task should block indefinitely then set the block time to a
3948 * value that will be recognised as an indefinite delay inside the
3949 * prvAddCurrentTaskToDelayedList() function. */
3950 if( xWaitIndefinitely != pdFALSE )
3952 xTicksToWait = portMAX_DELAY;
3955 traceTASK_DELAY_UNTIL( ( xTickCount + xTicksToWait ) );
3956 prvAddCurrentTaskToDelayedList( xTicksToWait, xWaitIndefinitely );
3959 #endif /* configUSE_TIMERS */
3960 /*-----------------------------------------------------------*/
3962 BaseType_t xTaskRemoveFromEventList( const List_t * const pxEventList )
3964 TCB_t * pxUnblockedTCB;
3967 /* THIS FUNCTION MUST BE CALLED FROM A CRITICAL SECTION. It can also be
3968 * called from a critical section within an ISR. */
3970 /* The event list is sorted in priority order, so the first in the list can
3971 * be removed as it is known to be the highest priority. Remove the TCB from
3972 * the delayed list, and add it to the ready list.
3974 * If an event is for a queue that is locked then this function will never
3975 * get called - the lock count on the queue will get modified instead. This
3976 * means exclusive access to the event list is guaranteed here.
3978 * This function assumes that a check has already been made to ensure that
3979 * pxEventList is not empty. */
3980 pxUnblockedTCB = listGET_OWNER_OF_HEAD_ENTRY( pxEventList ); /*lint !e9079 void * is used as this macro is used with timers and co-routines too. Alignment is known to be fine as the type of the pointer stored and retrieved is the same. */
3981 configASSERT( pxUnblockedTCB );
3982 ( void ) uxListRemove( &( pxUnblockedTCB->xEventListItem ) );
3984 if( uxSchedulerSuspended == ( UBaseType_t ) pdFALSE )
3986 ( void ) uxListRemove( &( pxUnblockedTCB->xStateListItem ) );
3987 prvAddTaskToReadyList( pxUnblockedTCB );
3989 #if ( configUSE_TICKLESS_IDLE != 0 )
3991 /* If a task is blocked on a kernel object then xNextTaskUnblockTime
3992 * might be set to the blocked task's time out time. If the task is
3993 * unblocked for a reason other than a timeout xNextTaskUnblockTime is
3994 * normally left unchanged, because it is automatically reset to a new
3995 * value when the tick count equals xNextTaskUnblockTime. However if
3996 * tickless idling is used it might be more important to enter sleep mode
3997 * at the earliest possible time - so reset xNextTaskUnblockTime here to
3998 * ensure it is updated at the earliest possible time. */
3999 prvResetNextTaskUnblockTime();
4005 /* The delayed and ready lists cannot be accessed, so hold this task
4006 * pending until the scheduler is resumed. */
4007 vListInsertEnd( &( xPendingReadyList ), &( pxUnblockedTCB->xEventListItem ) );
4011 #if ( configUSE_PREEMPTION == 1 )
4012 prvYieldForTask( pxUnblockedTCB, pdFALSE );
4014 if( xYieldPendings[ portGET_CORE_ID() ] != pdFALSE )
4022 /*-----------------------------------------------------------*/
4024 void vTaskRemoveFromUnorderedEventList( ListItem_t * pxEventListItem,
4025 const TickType_t xItemValue )
4027 TCB_t * pxUnblockedTCB;
4029 /* THIS FUNCTION MUST BE CALLED WITH THE SCHEDULER SUSPENDED. It is used by
4030 * the event flags implementation. */
4031 configASSERT( uxSchedulerSuspended != pdFALSE );
4033 /* Store the new item value in the event list. */
4034 listSET_LIST_ITEM_VALUE( pxEventListItem, xItemValue | taskEVENT_LIST_ITEM_VALUE_IN_USE );
4036 /* Remove the event list form the event flag. Interrupts do not access
4038 pxUnblockedTCB = listGET_LIST_ITEM_OWNER( pxEventListItem ); /*lint !e9079 void * is used as this macro is used with timers and co-routines too. Alignment is known to be fine as the type of the pointer stored and retrieved is the same. */
4039 configASSERT( pxUnblockedTCB );
4040 ( void ) uxListRemove( pxEventListItem );
4042 #if ( configUSE_TICKLESS_IDLE != 0 )
4044 /* If a task is blocked on a kernel object then xNextTaskUnblockTime
4045 * might be set to the blocked task's time out time. If the task is
4046 * unblocked for a reason other than a timeout xNextTaskUnblockTime is
4047 * normally left unchanged, because it is automatically reset to a new
4048 * value when the tick count equals xNextTaskUnblockTime. However if
4049 * tickless idling is used it might be more important to enter sleep mode
4050 * at the earliest possible time - so reset xNextTaskUnblockTime here to
4051 * ensure it is updated at the earliest possible time. */
4052 prvResetNextTaskUnblockTime();
4056 /* Remove the task from the delayed list and add it to the ready list. The
4057 * scheduler is suspended so interrupts will not be accessing the ready
4059 ( void ) uxListRemove( &( pxUnblockedTCB->xStateListItem ) );
4060 prvAddTaskToReadyList( pxUnblockedTCB );
4062 #if ( configUSE_PREEMPTION == 1 )
4063 taskENTER_CRITICAL();
4065 prvYieldForTask( pxUnblockedTCB, pdFALSE );
4067 taskEXIT_CRITICAL();
4070 /*-----------------------------------------------------------*/
4072 void vTaskSetTimeOutState( TimeOut_t * const pxTimeOut )
4074 configASSERT( pxTimeOut );
4075 taskENTER_CRITICAL();
4077 pxTimeOut->xOverflowCount = xNumOfOverflows;
4078 pxTimeOut->xTimeOnEntering = xTickCount;
4080 taskEXIT_CRITICAL();
4082 /*-----------------------------------------------------------*/
4084 void vTaskInternalSetTimeOutState( TimeOut_t * const pxTimeOut )
4086 /* For internal use only as it does not use a critical section. */
4087 pxTimeOut->xOverflowCount = xNumOfOverflows;
4088 pxTimeOut->xTimeOnEntering = xTickCount;
4090 /*-----------------------------------------------------------*/
4092 BaseType_t xTaskCheckForTimeOut( TimeOut_t * const pxTimeOut,
4093 TickType_t * const pxTicksToWait )
4097 configASSERT( pxTimeOut );
4098 configASSERT( pxTicksToWait );
4100 taskENTER_CRITICAL();
4102 /* Minor optimisation. The tick count cannot change in this block. */
4103 const TickType_t xConstTickCount = xTickCount;
4104 const TickType_t xElapsedTime = xConstTickCount - pxTimeOut->xTimeOnEntering;
4106 #if ( INCLUDE_xTaskAbortDelay == 1 )
4107 if( pxCurrentTCB->ucDelayAborted != ( uint8_t ) pdFALSE )
4109 /* The delay was aborted, which is not the same as a time out,
4110 * but has the same result. */
4111 pxCurrentTCB->ucDelayAborted = pdFALSE;
4117 #if ( INCLUDE_vTaskSuspend == 1 )
4118 if( *pxTicksToWait == portMAX_DELAY )
4120 /* If INCLUDE_vTaskSuspend is set to 1 and the block time
4121 * specified is the maximum block time then the task should block
4122 * indefinitely, and therefore never time out. */
4128 if( ( xNumOfOverflows != pxTimeOut->xOverflowCount ) && ( xConstTickCount >= pxTimeOut->xTimeOnEntering ) ) /*lint !e525 Indentation preferred as is to make code within pre-processor directives clearer. */
4130 /* The tick count is greater than the time at which
4131 * vTaskSetTimeout() was called, but has also overflowed since
4132 * vTaskSetTimeOut() was called. It must have wrapped all the way
4133 * around and gone past again. This passed since vTaskSetTimeout()
4136 *pxTicksToWait = ( TickType_t ) 0;
4138 else if( xElapsedTime < *pxTicksToWait ) /*lint !e961 Explicit casting is only redundant with some compilers, whereas others require it to prevent integer conversion errors. */
4140 /* Not a genuine timeout. Adjust parameters for time remaining. */
4141 *pxTicksToWait -= xElapsedTime;
4142 vTaskInternalSetTimeOutState( pxTimeOut );
4147 *pxTicksToWait = ( TickType_t ) 0;
4151 taskEXIT_CRITICAL();
4155 /*-----------------------------------------------------------*/
4157 void vTaskMissedYield( void )
4159 /* Must be called from within a critical section */
4160 xYieldPendings[ portGET_CORE_ID() ] = pdTRUE;
4162 /*-----------------------------------------------------------*/
4164 #if ( configUSE_TRACE_FACILITY == 1 )
4166 UBaseType_t uxTaskGetTaskNumber( TaskHandle_t xTask )
4168 UBaseType_t uxReturn;
4169 TCB_t const * pxTCB;
4174 uxReturn = pxTCB->uxTaskNumber;
4184 #endif /* configUSE_TRACE_FACILITY */
4185 /*-----------------------------------------------------------*/
4187 #if ( configUSE_TRACE_FACILITY == 1 )
4189 void vTaskSetTaskNumber( TaskHandle_t xTask,
4190 const UBaseType_t uxHandle )
4197 pxTCB->uxTaskNumber = uxHandle;
4201 #endif /* configUSE_TRACE_FACILITY */
4204 * -----------------------------------------------------------
4205 * The MinimalIdle task.
4206 * ----------------------------------------------------------
4208 * The minimal idle task is used for all the additional Cores in a SMP system.
4209 * There must be only 1 idle task and the rest are minimal idle tasks.
4211 * @todo additional conditional compiles to remove this function.
4213 #if (configNUM_CORES > 1)
4214 static portTASK_FUNCTION( prvMinimalIdleTask, pvParameters )
4218 #if ( configUSE_PREEMPTION == 0 )
4220 /* If we are not using preemption we keep forcing a task switch to
4221 * see if any other task has become available. If we are using
4222 * preemption we don't need to do this as any task becoming available
4223 * will automatically get the processor anyway. */
4226 #endif /* configUSE_PREEMPTION */
4228 #if ( ( configUSE_PREEMPTION == 1 ) && ( configIDLE_SHOULD_YIELD == 1 ) )
4230 /* When using preemption tasks of equal priority will be
4231 * timesliced. If a task that is sharing the idle priority is ready
4232 * to run then the idle task should yield before the end of the
4235 * A critical region is not required here as we are just reading from
4236 * the list, and an occasional incorrect value will not matter. If
4237 * the ready list at the idle priority contains one more task than the
4238 * number of idle tasks, which is equal to the configured numbers of cores
4239 * then a task other than the idle task is ready to execute. */
4240 if( listCURRENT_LIST_LENGTH( &( pxReadyTasksLists[ tskIDLE_PRIORITY ] ) ) > ( UBaseType_t ) configNUM_CORES )
4246 mtCOVERAGE_TEST_MARKER();
4249 #endif /* ( ( configUSE_PREEMPTION == 1 ) && ( configIDLE_SHOULD_YIELD == 1 ) ) */
4254 * -----------------------------------------------------------
4256 * ----------------------------------------------------------
4260 static portTASK_FUNCTION( prvIdleTask, pvParameters )
4262 /* Stop warnings. */
4263 ( void ) pvParameters;
4265 /** THIS IS THE RTOS IDLE TASK - WHICH IS CREATED AUTOMATICALLY WHEN THE
4266 * SCHEDULER IS STARTED. **/
4268 /* In case a task that has a secure context deletes itself, in which case
4269 * the idle task is responsible for deleting the task's secure context, if
4271 portALLOCATE_SECURE_CONTEXT( configMINIMAL_SECURE_STACK_SIZE );
4273 /* All cores start up in the idle task. This initial yield gets the application
4279 /* See if any tasks have deleted themselves - if so then the idle task
4280 * is responsible for freeing the deleted task's TCB and stack. */
4281 prvCheckTasksWaitingTermination();
4283 #if ( configUSE_PREEMPTION == 0 )
4285 /* If we are not using preemption we keep forcing a task switch to
4286 * see if any other task has become available. If we are using
4287 * preemption we don't need to do this as any task becoming available
4288 * will automatically get the processor anyway. */
4291 #endif /* configUSE_PREEMPTION */
4293 #if ( ( configUSE_PREEMPTION == 1 ) && ( configIDLE_SHOULD_YIELD == 1 ) )
4295 /* When using preemption tasks of equal priority will be
4296 * timesliced. If a task that is sharing the idle priority is ready
4297 * to run then the idle task should yield before the end of the
4300 * A critical region is not required here as we are just reading from
4301 * the list, and an occasional incorrect value will not matter. If
4302 * the ready list at the idle priority contains one more task than the
4303 * number of idle tasks, which is equal to the configured numbers of cores
4304 * then a task other than the idle task is ready to execute. */
4305 if( listCURRENT_LIST_LENGTH( &( pxReadyTasksLists[ tskIDLE_PRIORITY ] ) ) > ( UBaseType_t ) configNUM_CORES )
4311 mtCOVERAGE_TEST_MARKER();
4314 #endif /* ( ( configUSE_PREEMPTION == 1 ) && ( configIDLE_SHOULD_YIELD == 1 ) ) */
4316 #if ( configUSE_IDLE_HOOK == 1 )
4318 extern void vApplicationIdleHook( void );
4320 /* Call the user defined function from within the idle task. This
4321 * allows the application designer to add background functionality
4322 * without the overhead of a separate task.
4323 * NOTE: vApplicationIdleHook() MUST NOT, UNDER ANY CIRCUMSTANCES,
4324 * CALL A FUNCTION THAT MIGHT BLOCK. */
4325 vApplicationIdleHook();
4327 #endif /* configUSE_IDLE_HOOK */
4329 /* This conditional compilation should use inequality to 0, not equality
4330 * to 1. This is to ensure portSUPPRESS_TICKS_AND_SLEEP() is called when
4331 * user defined low power mode implementations require
4332 * configUSE_TICKLESS_IDLE to be set to a value other than 1. */
4333 #if ( configUSE_TICKLESS_IDLE != 0 )
4335 TickType_t xExpectedIdleTime;
4337 /* It is not desirable to suspend then resume the scheduler on
4338 * each iteration of the idle task. Therefore, a preliminary
4339 * test of the expected idle time is performed without the
4340 * scheduler suspended. The result here is not necessarily
4342 xExpectedIdleTime = prvGetExpectedIdleTime();
4344 if( xExpectedIdleTime >= configEXPECTED_IDLE_TIME_BEFORE_SLEEP )
4348 /* Now the scheduler is suspended, the expected idle
4349 * time can be sampled again, and this time its value can
4351 configASSERT( xNextTaskUnblockTime >= xTickCount );
4352 xExpectedIdleTime = prvGetExpectedIdleTime();
4354 /* Define the following macro to set xExpectedIdleTime to 0
4355 * if the application does not want
4356 * portSUPPRESS_TICKS_AND_SLEEP() to be called. */
4357 configPRE_SUPPRESS_TICKS_AND_SLEEP_PROCESSING( xExpectedIdleTime );
4359 if( xExpectedIdleTime >= configEXPECTED_IDLE_TIME_BEFORE_SLEEP )
4361 traceLOW_POWER_IDLE_BEGIN();
4362 portSUPPRESS_TICKS_AND_SLEEP( xExpectedIdleTime );
4363 traceLOW_POWER_IDLE_END();
4367 mtCOVERAGE_TEST_MARKER();
4370 ( void ) xTaskResumeAll();
4374 mtCOVERAGE_TEST_MARKER();
4377 #endif /* configUSE_TICKLESS_IDLE */
4380 /*-----------------------------------------------------------*/
4382 #if ( configUSE_TICKLESS_IDLE != 0 )
4384 eSleepModeStatus eTaskConfirmSleepModeStatus( void )
4386 /* The idle task exists in addition to the application tasks. */
4387 const UBaseType_t uxNonApplicationTasks = 1;
4388 eSleepModeStatus eReturn = eStandardSleep;
4390 /* This function must be called from a critical section. */
4392 if( listCURRENT_LIST_LENGTH( &xPendingReadyList ) != 0 )
4394 /* A task was made ready while the scheduler was suspended. */
4395 eReturn = eAbortSleep;
4397 else if( xYieldPending != pdFALSE )
4399 /* A yield was pended while the scheduler was suspended. */
4400 eReturn = eAbortSleep;
4402 else if( xPendedTicks != 0 )
4404 /* A tick interrupt has already occurred but was held pending
4405 * because the scheduler is suspended. */
4406 eReturn = eAbortSleep;
4410 /* If all the tasks are in the suspended list (which might mean they
4411 * have an infinite block time rather than actually being suspended)
4412 * then it is safe to turn all clocks off and just wait for external
4414 if( listCURRENT_LIST_LENGTH( &xSuspendedTaskList ) == ( uxCurrentNumberOfTasks - uxNonApplicationTasks ) )
4416 eReturn = eNoTasksWaitingTimeout;
4420 mtCOVERAGE_TEST_MARKER();
4427 #endif /* configUSE_TICKLESS_IDLE */
4428 /*-----------------------------------------------------------*/
4430 #if ( configNUM_THREAD_LOCAL_STORAGE_POINTERS != 0 )
4432 void vTaskSetThreadLocalStoragePointer( TaskHandle_t xTaskToSet,
4438 if( xIndex < configNUM_THREAD_LOCAL_STORAGE_POINTERS )
4440 pxTCB = prvGetTCBFromHandle( xTaskToSet );
4441 configASSERT( pxTCB != NULL );
4442 pxTCB->pvThreadLocalStoragePointers[ xIndex ] = pvValue;
4446 #endif /* configNUM_THREAD_LOCAL_STORAGE_POINTERS */
4447 /*-----------------------------------------------------------*/
4449 #if ( configNUM_THREAD_LOCAL_STORAGE_POINTERS != 0 )
4451 void * pvTaskGetThreadLocalStoragePointer( TaskHandle_t xTaskToQuery,
4454 void * pvReturn = NULL;
4457 if( xIndex < configNUM_THREAD_LOCAL_STORAGE_POINTERS )
4459 pxTCB = prvGetTCBFromHandle( xTaskToQuery );
4460 pvReturn = pxTCB->pvThreadLocalStoragePointers[ xIndex ];
4470 #endif /* configNUM_THREAD_LOCAL_STORAGE_POINTERS */
4471 /*-----------------------------------------------------------*/
4473 #if ( portUSING_MPU_WRAPPERS == 1 )
4475 void vTaskAllocateMPURegions( TaskHandle_t xTaskToModify,
4476 const MemoryRegion_t * const xRegions )
4480 /* If null is passed in here then we are modifying the MPU settings of
4481 * the calling task. */
4482 pxTCB = prvGetTCBFromHandle( xTaskToModify );
4484 vPortStoreTaskMPUSettings( &( pxTCB->xMPUSettings ), xRegions, NULL, 0 );
4487 #endif /* portUSING_MPU_WRAPPERS */
4488 /*-----------------------------------------------------------*/
4490 static void prvInitialiseTaskLists( void )
4492 UBaseType_t uxPriority;
4494 for( uxPriority = ( UBaseType_t ) 0U; uxPriority < ( UBaseType_t ) configMAX_PRIORITIES; uxPriority++ )
4496 vListInitialise( &( pxReadyTasksLists[ uxPriority ] ) );
4499 vListInitialise( &xDelayedTaskList1 );
4500 vListInitialise( &xDelayedTaskList2 );
4501 vListInitialise( &xPendingReadyList );
4503 #if ( INCLUDE_vTaskDelete == 1 )
4505 vListInitialise( &xTasksWaitingTermination );
4507 #endif /* INCLUDE_vTaskDelete */
4509 #if ( INCLUDE_vTaskSuspend == 1 )
4511 vListInitialise( &xSuspendedTaskList );
4513 #endif /* INCLUDE_vTaskSuspend */
4515 /* Start with pxDelayedTaskList using list1 and the pxOverflowDelayedTaskList
4517 pxDelayedTaskList = &xDelayedTaskList1;
4518 pxOverflowDelayedTaskList = &xDelayedTaskList2;
4520 /*-----------------------------------------------------------*/
4522 static void prvCheckTasksWaitingTermination( void )
4524 /** THIS FUNCTION IS CALLED FROM THE RTOS IDLE TASK **/
4526 #if ( INCLUDE_vTaskDelete == 1 )
4530 /* uxDeletedTasksWaitingCleanUp is used to prevent taskENTER_CRITICAL()
4531 * being called too often in the idle task. */
4532 while( uxDeletedTasksWaitingCleanUp > ( UBaseType_t ) 0U )
4534 taskENTER_CRITICAL();
4536 /* Since we are SMP, multiple idles can be running simultaneously
4537 * and we need to check that other idles did not cleanup while we were
4538 * waiting to enter the critical section */
4539 if( uxDeletedTasksWaitingCleanUp > ( UBaseType_t ) 0U )
4541 pxTCB = listGET_OWNER_OF_HEAD_ENTRY( ( &xTasksWaitingTermination ) ); /*lint !e9079 void * is used as this macro is used with timers and co-routines too. Alignment is known to be fine as the type of the pointer stored and retrieved is the same. */
4543 if( pxTCB->xTaskRunState == taskTASK_NOT_RUNNING )
4545 ( void ) uxListRemove( &( pxTCB->xStateListItem ) );
4546 --uxCurrentNumberOfTasks;
4547 --uxDeletedTasksWaitingCleanUp;
4548 prvDeleteTCB( pxTCB );
4552 /* The TCB to be deleted still has not yet been switched out
4553 * by the scheduler, so we will just exit this loop early and
4554 * try again next time. */
4555 taskEXIT_CRITICAL();
4560 taskEXIT_CRITICAL();
4563 #endif /* INCLUDE_vTaskDelete */
4565 /*-----------------------------------------------------------*/
4567 #if ( configUSE_TRACE_FACILITY == 1 )
4569 void vTaskGetInfo( TaskHandle_t xTask,
4570 TaskStatus_t * pxTaskStatus,
4571 BaseType_t xGetFreeStackSpace,
4576 /* xTask is NULL then get the state of the calling task. */
4577 pxTCB = prvGetTCBFromHandle( xTask );
4579 pxTaskStatus->xHandle = ( TaskHandle_t ) pxTCB;
4580 pxTaskStatus->pcTaskName = ( const char * ) &( pxTCB->pcTaskName[ 0 ] );
4581 pxTaskStatus->uxCurrentPriority = pxTCB->uxPriority;
4582 pxTaskStatus->pxStackBase = pxTCB->pxStack;
4583 pxTaskStatus->xTaskNumber = pxTCB->uxTCBNumber;
4585 #if ( configUSE_MUTEXES == 1 )
4587 pxTaskStatus->uxBasePriority = pxTCB->uxBasePriority;
4591 pxTaskStatus->uxBasePriority = 0;
4595 #if ( configGENERATE_RUN_TIME_STATS == 1 )
4597 pxTaskStatus->ulRunTimeCounter = pxTCB->ulRunTimeCounter;
4601 pxTaskStatus->ulRunTimeCounter = 0;
4605 /* Obtaining the task state is a little fiddly, so is only done if the
4606 * value of eState passed into this function is eInvalid - otherwise the
4607 * state is just set to whatever is passed in. */
4608 if( eState != eInvalid )
4610 if( taskTASK_IS_RUNNING( pxTCB->xTaskRunState ) )
4612 pxTaskStatus->eCurrentState = eRunning;
4616 pxTaskStatus->eCurrentState = eState;
4618 #if ( INCLUDE_vTaskSuspend == 1 )
4620 /* If the task is in the suspended list then there is a
4621 * chance it is actually just blocked indefinitely - so really
4622 * it should be reported as being in the Blocked state. */
4623 if( eState == eSuspended )
4627 if( listLIST_ITEM_CONTAINER( &( pxTCB->xEventListItem ) ) != NULL )
4629 pxTaskStatus->eCurrentState = eBlocked;
4632 ( void ) xTaskResumeAll();
4635 #endif /* INCLUDE_vTaskSuspend */
4640 pxTaskStatus->eCurrentState = eTaskGetState( pxTCB );
4643 /* Obtaining the stack space takes some time, so the xGetFreeStackSpace
4644 * parameter is provided to allow it to be skipped. */
4645 if( xGetFreeStackSpace != pdFALSE )
4647 #if ( portSTACK_GROWTH > 0 )
4649 pxTaskStatus->usStackHighWaterMark = prvTaskCheckFreeStackSpace( ( uint8_t * ) pxTCB->pxEndOfStack );
4653 pxTaskStatus->usStackHighWaterMark = prvTaskCheckFreeStackSpace( ( uint8_t * ) pxTCB->pxStack );
4659 pxTaskStatus->usStackHighWaterMark = 0;
4663 #endif /* configUSE_TRACE_FACILITY */
4664 /*-----------------------------------------------------------*/
4666 #if ( configUSE_TRACE_FACILITY == 1 )
4668 static UBaseType_t prvListTasksWithinSingleList( TaskStatus_t * pxTaskStatusArray,
4672 configLIST_VOLATILE TCB_t * pxNextTCB, * pxFirstTCB;
4673 UBaseType_t uxTask = 0;
4675 if( listCURRENT_LIST_LENGTH( pxList ) > ( UBaseType_t ) 0 )
4677 listGET_OWNER_OF_NEXT_ENTRY( pxFirstTCB, pxList ); /*lint !e9079 void * is used as this macro is used with timers and co-routines too. Alignment is known to be fine as the type of the pointer stored and retrieved is the same. */
4679 /* Populate an TaskStatus_t structure within the
4680 * pxTaskStatusArray array for each task that is referenced from
4681 * pxList. See the definition of TaskStatus_t in task.h for the
4682 * meaning of each TaskStatus_t structure member. */
4685 listGET_OWNER_OF_NEXT_ENTRY( pxNextTCB, pxList ); /*lint !e9079 void * is used as this macro is used with timers and co-routines too. Alignment is known to be fine as the type of the pointer stored and retrieved is the same. */
4686 vTaskGetInfo( ( TaskHandle_t ) pxNextTCB, &( pxTaskStatusArray[ uxTask ] ), pdTRUE, eState );
4688 } while( pxNextTCB != pxFirstTCB );
4692 mtCOVERAGE_TEST_MARKER();
4698 #endif /* configUSE_TRACE_FACILITY */
4699 /*-----------------------------------------------------------*/
4701 #if ( ( configUSE_TRACE_FACILITY == 1 ) || ( INCLUDE_uxTaskGetStackHighWaterMark == 1 ) || ( INCLUDE_uxTaskGetStackHighWaterMark2 == 1 ) )
4703 static configSTACK_DEPTH_TYPE prvTaskCheckFreeStackSpace( const uint8_t * pucStackByte )
4705 uint32_t ulCount = 0U;
4707 while( *pucStackByte == ( uint8_t ) tskSTACK_FILL_BYTE )
4709 pucStackByte -= portSTACK_GROWTH;
4713 ulCount /= ( uint32_t ) sizeof( StackType_t ); /*lint !e961 Casting is not redundant on smaller architectures. */
4715 return ( configSTACK_DEPTH_TYPE ) ulCount;
4718 #endif /* ( ( configUSE_TRACE_FACILITY == 1 ) || ( INCLUDE_uxTaskGetStackHighWaterMark == 1 ) || ( INCLUDE_uxTaskGetStackHighWaterMark2 == 1 ) ) */
4719 /*-----------------------------------------------------------*/
4721 #if ( INCLUDE_uxTaskGetStackHighWaterMark2 == 1 )
4723 /* uxTaskGetStackHighWaterMark() and uxTaskGetStackHighWaterMark2() are the
4724 * same except for their return type. Using configSTACK_DEPTH_TYPE allows the
4725 * user to determine the return type. It gets around the problem of the value
4726 * overflowing on 8-bit types without breaking backward compatibility for
4727 * applications that expect an 8-bit return type. */
4728 configSTACK_DEPTH_TYPE uxTaskGetStackHighWaterMark2( TaskHandle_t xTask )
4731 uint8_t * pucEndOfStack;
4732 configSTACK_DEPTH_TYPE uxReturn;
4734 /* uxTaskGetStackHighWaterMark() and uxTaskGetStackHighWaterMark2() are
4735 * the same except for their return type. Using configSTACK_DEPTH_TYPE
4736 * allows the user to determine the return type. It gets around the
4737 * problem of the value overflowing on 8-bit types without breaking
4738 * backward compatibility for applications that expect an 8-bit return
4741 pxTCB = prvGetTCBFromHandle( xTask );
4743 #if portSTACK_GROWTH < 0
4745 pucEndOfStack = ( uint8_t * ) pxTCB->pxStack;
4749 pucEndOfStack = ( uint8_t * ) pxTCB->pxEndOfStack;
4753 uxReturn = prvTaskCheckFreeStackSpace( pucEndOfStack );
4758 #endif /* INCLUDE_uxTaskGetStackHighWaterMark2 */
4759 /*-----------------------------------------------------------*/
4761 #if ( INCLUDE_uxTaskGetStackHighWaterMark == 1 )
4763 UBaseType_t uxTaskGetStackHighWaterMark( TaskHandle_t xTask )
4766 uint8_t * pucEndOfStack;
4767 UBaseType_t uxReturn;
4769 pxTCB = prvGetTCBFromHandle( xTask );
4771 #if portSTACK_GROWTH < 0
4773 pucEndOfStack = ( uint8_t * ) pxTCB->pxStack;
4777 pucEndOfStack = ( uint8_t * ) pxTCB->pxEndOfStack;
4781 uxReturn = ( UBaseType_t ) prvTaskCheckFreeStackSpace( pucEndOfStack );
4786 #endif /* INCLUDE_uxTaskGetStackHighWaterMark */
4787 /*-----------------------------------------------------------*/
4789 #if ( INCLUDE_vTaskDelete == 1 )
4791 static void prvDeleteTCB( TCB_t * pxTCB )
4793 /* This call is required specifically for the TriCore port. It must be
4794 * above the vPortFree() calls. The call is also used by ports/demos that
4795 * want to allocate and clean RAM statically. */
4796 portCLEAN_UP_TCB( pxTCB );
4798 /* Free up the memory allocated by the scheduler for the task. It is up
4799 * to the task to free any memory allocated at the application level.
4800 * See the third party link http://www.nadler.com/embedded/newlibAndFreeRTOS.html
4801 * for additional information. */
4802 #if ( configUSE_NEWLIB_REENTRANT == 1 )
4804 _reclaim_reent( &( pxTCB->xNewLib_reent ) );
4806 #endif /* configUSE_NEWLIB_REENTRANT */
4808 #if ( ( configSUPPORT_DYNAMIC_ALLOCATION == 1 ) && ( configSUPPORT_STATIC_ALLOCATION == 0 ) && ( portUSING_MPU_WRAPPERS == 0 ) )
4810 /* The task can only have been allocated dynamically - free both
4811 * the stack and TCB. */
4812 vPortFreeStack( pxTCB->pxStack );
4815 #elif ( tskSTATIC_AND_DYNAMIC_ALLOCATION_POSSIBLE != 0 ) /*lint !e731 !e9029 Macro has been consolidated for readability reasons. */
4817 /* The task could have been allocated statically or dynamically, so
4818 * check what was statically allocated before trying to free the
4820 if( pxTCB->ucStaticallyAllocated == tskDYNAMICALLY_ALLOCATED_STACK_AND_TCB )
4822 /* Both the stack and TCB were allocated dynamically, so both
4824 vPortFreeStack( pxTCB->pxStack );
4827 else if( pxTCB->ucStaticallyAllocated == tskSTATICALLY_ALLOCATED_STACK_ONLY )
4829 /* Only the stack was statically allocated, so the TCB is the
4830 * only memory that must be freed. */
4835 /* Neither the stack nor the TCB were allocated dynamically, so
4836 * nothing needs to be freed. */
4837 configASSERT( pxTCB->ucStaticallyAllocated == tskSTATICALLY_ALLOCATED_STACK_AND_TCB );
4838 mtCOVERAGE_TEST_MARKER();
4841 #endif /* configSUPPORT_DYNAMIC_ALLOCATION */
4844 #endif /* INCLUDE_vTaskDelete */
4845 /*-----------------------------------------------------------*/
4847 static void prvResetNextTaskUnblockTime( void )
4849 if( listLIST_IS_EMPTY( pxDelayedTaskList ) != pdFALSE )
4851 /* The new current delayed list is empty. Set xNextTaskUnblockTime to
4852 * the maximum possible value so it is extremely unlikely that the
4853 * if( xTickCount >= xNextTaskUnblockTime ) test will pass until
4854 * there is an item in the delayed list. */
4855 xNextTaskUnblockTime = portMAX_DELAY;
4859 /* The new current delayed list is not empty, get the value of
4860 * the item at the head of the delayed list. This is the time at
4861 * which the task at the head of the delayed list should be removed
4862 * from the Blocked state. */
4863 xNextTaskUnblockTime = listGET_ITEM_VALUE_OF_HEAD_ENTRY( pxDelayedTaskList );
4866 /*-----------------------------------------------------------*/
4868 #if ( ( INCLUDE_xTaskGetCurrentTaskHandle == 1 ) || ( configUSE_MUTEXES == 1 ) )
4870 TaskHandle_t xTaskGetCurrentTaskHandle( void )
4872 TaskHandle_t xReturn;
4875 ulState = portDISABLE_INTERRUPTS();
4876 xReturn = pxCurrentTCBs[ portGET_CORE_ID() ];
4877 portRESTORE_INTERRUPTS( ulState );
4882 TaskHandle_t xTaskGetCurrentTaskHandleCPU( UBaseType_t xCoreID )
4884 TaskHandle_t xReturn = NULL;
4886 if( taskVALID_CORE_ID( xCoreID ) != pdFALSE )
4888 xReturn = pxCurrentTCBs[ xCoreID ];
4894 #endif /* ( ( INCLUDE_xTaskGetCurrentTaskHandle == 1 ) || ( configUSE_MUTEXES == 1 ) ) */
4895 /*-----------------------------------------------------------*/
4897 #if ( ( INCLUDE_xTaskGetSchedulerState == 1 ) || ( configUSE_TIMERS == 1 ) )
4899 BaseType_t xTaskGetSchedulerState( void )
4903 if( xSchedulerRunning == pdFALSE )
4905 xReturn = taskSCHEDULER_NOT_STARTED;
4909 taskENTER_CRITICAL();
4911 if( uxSchedulerSuspended == ( UBaseType_t ) pdFALSE )
4913 xReturn = taskSCHEDULER_RUNNING;
4917 xReturn = taskSCHEDULER_SUSPENDED;
4920 taskEXIT_CRITICAL();
4926 #endif /* ( ( INCLUDE_xTaskGetSchedulerState == 1 ) || ( configUSE_TIMERS == 1 ) ) */
4927 /*-----------------------------------------------------------*/
4929 #if ( configUSE_MUTEXES == 1 )
4931 BaseType_t xTaskPriorityInherit( TaskHandle_t const pxMutexHolder )
4933 TCB_t * const pxMutexHolderTCB = pxMutexHolder;
4934 BaseType_t xReturn = pdFALSE;
4936 /* If the mutex was given back by an interrupt while the queue was
4937 * locked then the mutex holder might now be NULL. _RB_ Is this still
4938 * needed as interrupts can no longer use mutexes? */
4939 if( pxMutexHolder != NULL )
4941 /* If the holder of the mutex has a priority below the priority of
4942 * the task attempting to obtain the mutex then it will temporarily
4943 * inherit the priority of the task attempting to obtain the mutex. */
4944 if( pxMutexHolderTCB->uxPriority < pxCurrentTCB->uxPriority )
4946 /* Adjust the mutex holder state to account for its new
4947 * priority. Only reset the event list item value if the value is
4948 * not being used for anything else. */
4949 if( ( listGET_LIST_ITEM_VALUE( &( pxMutexHolderTCB->xEventListItem ) ) & taskEVENT_LIST_ITEM_VALUE_IN_USE ) == 0UL )
4951 listSET_LIST_ITEM_VALUE( &( pxMutexHolderTCB->xEventListItem ), ( TickType_t ) configMAX_PRIORITIES - ( TickType_t ) pxCurrentTCB->uxPriority ); /*lint !e961 MISRA exception as the casts are only redundant for some ports. */
4955 mtCOVERAGE_TEST_MARKER();
4958 /* If the task being modified is in the ready state it will need
4959 * to be moved into a new list. */
4960 if( listIS_CONTAINED_WITHIN( &( pxReadyTasksLists[ pxMutexHolderTCB->uxPriority ] ), &( pxMutexHolderTCB->xStateListItem ) ) != pdFALSE )
4962 if( uxListRemove( &( pxMutexHolderTCB->xStateListItem ) ) == ( UBaseType_t ) 0 )
4964 /* It is known that the task is in its ready list so
4965 * there is no need to check again and the port level
4966 * reset macro can be called directly. */
4967 portRESET_READY_PRIORITY( pxMutexHolderTCB->uxPriority, uxTopReadyPriority );
4971 mtCOVERAGE_TEST_MARKER();
4974 /* Inherit the priority before being moved into the new list. */
4975 pxMutexHolderTCB->uxPriority = pxCurrentTCB->uxPriority;
4976 prvAddTaskToReadyList( pxMutexHolderTCB );
4980 /* Just inherit the priority. */
4981 pxMutexHolderTCB->uxPriority = pxCurrentTCB->uxPriority;
4984 traceTASK_PRIORITY_INHERIT( pxMutexHolderTCB, pxCurrentTCB->uxPriority );
4986 /* Inheritance occurred. */
4991 if( pxMutexHolderTCB->uxBasePriority < pxCurrentTCB->uxPriority )
4993 /* The base priority of the mutex holder is lower than the
4994 * priority of the task attempting to take the mutex, but the
4995 * current priority of the mutex holder is not lower than the
4996 * priority of the task attempting to take the mutex.
4997 * Therefore the mutex holder must have already inherited a
4998 * priority, but inheritance would have occurred if that had
4999 * not been the case. */
5004 mtCOVERAGE_TEST_MARKER();
5010 mtCOVERAGE_TEST_MARKER();
5016 #endif /* configUSE_MUTEXES */
5017 /*-----------------------------------------------------------*/
5019 #if ( configUSE_MUTEXES == 1 )
5021 BaseType_t xTaskPriorityDisinherit( TaskHandle_t const pxMutexHolder )
5023 TCB_t * const pxTCB = pxMutexHolder;
5024 BaseType_t xReturn = pdFALSE;
5026 if( pxMutexHolder != NULL )
5028 /* A task can only have an inherited priority if it holds the mutex.
5029 * If the mutex is held by a task then it cannot be given from an
5030 * interrupt, and if a mutex is given by the holding task then it must
5031 * be the running state task. */
5032 configASSERT( pxTCB == pxCurrentTCB );
5033 configASSERT( pxTCB->uxMutexesHeld );
5034 ( pxTCB->uxMutexesHeld )--;
5036 /* Has the holder of the mutex inherited the priority of another
5038 if( pxTCB->uxPriority != pxTCB->uxBasePriority )
5040 /* Only disinherit if no other mutexes are held. */
5041 if( pxTCB->uxMutexesHeld == ( UBaseType_t ) 0 )
5043 /* A task can only have an inherited priority if it holds
5044 * the mutex. If the mutex is held by a task then it cannot be
5045 * given from an interrupt, and if a mutex is given by the
5046 * holding task then it must be the running state task. Remove
5047 * the holding task from the ready list. */
5048 if( uxListRemove( &( pxTCB->xStateListItem ) ) == ( UBaseType_t ) 0 )
5050 portRESET_READY_PRIORITY( pxTCB->uxPriority, uxTopReadyPriority );
5054 mtCOVERAGE_TEST_MARKER();
5057 /* Disinherit the priority before adding the task into the
5058 * new ready list. */
5059 traceTASK_PRIORITY_DISINHERIT( pxTCB, pxTCB->uxBasePriority );
5060 pxTCB->uxPriority = pxTCB->uxBasePriority;
5062 /* Reset the event list item value. It cannot be in use for
5063 * any other purpose if this task is running, and it must be
5064 * running to give back the mutex. */
5065 listSET_LIST_ITEM_VALUE( &( pxTCB->xEventListItem ), ( TickType_t ) configMAX_PRIORITIES - ( TickType_t ) pxTCB->uxPriority ); /*lint !e961 MISRA exception as the casts are only redundant for some ports. */
5066 prvAddTaskToReadyList( pxTCB );
5068 /* Return true to indicate that a context switch is required.
5069 * This is only actually required in the corner case whereby
5070 * multiple mutexes were held and the mutexes were given back
5071 * in an order different to that in which they were taken.
5072 * If a context switch did not occur when the first mutex was
5073 * returned, even if a task was waiting on it, then a context
5074 * switch should occur when the last mutex is returned whether
5075 * a task is waiting on it or not. */
5080 mtCOVERAGE_TEST_MARKER();
5085 mtCOVERAGE_TEST_MARKER();
5090 mtCOVERAGE_TEST_MARKER();
5096 #endif /* configUSE_MUTEXES */
5097 /*-----------------------------------------------------------*/
5099 #if ( configUSE_MUTEXES == 1 )
5101 void vTaskPriorityDisinheritAfterTimeout( TaskHandle_t const pxMutexHolder,
5102 UBaseType_t uxHighestPriorityWaitingTask )
5104 TCB_t * const pxTCB = pxMutexHolder;
5105 UBaseType_t uxPriorityUsedOnEntry, uxPriorityToUse;
5106 const UBaseType_t uxOnlyOneMutexHeld = ( UBaseType_t ) 1;
5108 if( pxMutexHolder != NULL )
5110 /* If pxMutexHolder is not NULL then the holder must hold at least
5112 configASSERT( pxTCB->uxMutexesHeld );
5114 /* Determine the priority to which the priority of the task that
5115 * holds the mutex should be set. This will be the greater of the
5116 * holding task's base priority and the priority of the highest
5117 * priority task that is waiting to obtain the mutex. */
5118 if( pxTCB->uxBasePriority < uxHighestPriorityWaitingTask )
5120 uxPriorityToUse = uxHighestPriorityWaitingTask;
5124 uxPriorityToUse = pxTCB->uxBasePriority;
5127 /* Does the priority need to change? */
5128 if( pxTCB->uxPriority != uxPriorityToUse )
5130 /* Only disinherit if no other mutexes are held. This is a
5131 * simplification in the priority inheritance implementation. If
5132 * the task that holds the mutex is also holding other mutexes then
5133 * the other mutexes may have caused the priority inheritance. */
5134 if( pxTCB->uxMutexesHeld == uxOnlyOneMutexHeld )
5136 /* If a task has timed out because it already holds the
5137 * mutex it was trying to obtain then it cannot of inherited
5138 * its own priority. */
5139 configASSERT( pxTCB != pxCurrentTCB );
5141 /* Disinherit the priority, remembering the previous
5142 * priority to facilitate determining the subject task's
5144 traceTASK_PRIORITY_DISINHERIT( pxTCB, uxPriorityToUse );
5145 uxPriorityUsedOnEntry = pxTCB->uxPriority;
5146 pxTCB->uxPriority = uxPriorityToUse;
5148 /* Only reset the event list item value if the value is not
5149 * being used for anything else. */
5150 if( ( listGET_LIST_ITEM_VALUE( &( pxTCB->xEventListItem ) ) & taskEVENT_LIST_ITEM_VALUE_IN_USE ) == 0UL )
5152 listSET_LIST_ITEM_VALUE( &( pxTCB->xEventListItem ), ( TickType_t ) configMAX_PRIORITIES - ( TickType_t ) uxPriorityToUse ); /*lint !e961 MISRA exception as the casts are only redundant for some ports. */
5156 mtCOVERAGE_TEST_MARKER();
5159 /* If the running task is not the task that holds the mutex
5160 * then the task that holds the mutex could be in either the
5161 * Ready, Blocked or Suspended states. Only remove the task
5162 * from its current state list if it is in the Ready state as
5163 * the task's priority is going to change and there is one
5164 * Ready list per priority. */
5165 if( listIS_CONTAINED_WITHIN( &( pxReadyTasksLists[ uxPriorityUsedOnEntry ] ), &( pxTCB->xStateListItem ) ) != pdFALSE )
5167 if( uxListRemove( &( pxTCB->xStateListItem ) ) == ( UBaseType_t ) 0 )
5169 /* It is known that the task is in its ready list so
5170 * there is no need to check again and the port level
5171 * reset macro can be called directly. */
5172 portRESET_READY_PRIORITY( pxTCB->uxPriority, uxTopReadyPriority );
5176 mtCOVERAGE_TEST_MARKER();
5179 prvAddTaskToReadyList( pxTCB );
5183 mtCOVERAGE_TEST_MARKER();
5188 mtCOVERAGE_TEST_MARKER();
5193 mtCOVERAGE_TEST_MARKER();
5198 mtCOVERAGE_TEST_MARKER();
5202 #endif /* configUSE_MUTEXES */
5203 /*-----------------------------------------------------------*/
5206 * If not in a critical section then yield immediately.
5207 * Otherwise set xYieldPending to true to wait to
5208 * yield until exiting the critical section.
5210 void vTaskYieldWithinAPI( void )
5212 if( pxCurrentTCB->uxCriticalNesting == 0U )
5218 xYieldPendings[ portGET_CORE_ID() ] = pdTRUE;
5221 /*-----------------------------------------------------------*/
5223 #if ( portCRITICAL_NESTING_IN_TCB == 1 )
5225 void vTaskEnterCritical( void )
5227 portDISABLE_INTERRUPTS();
5229 if( xSchedulerRunning != pdFALSE )
5231 if( pxCurrentTCB->uxCriticalNesting == 0U )
5233 if( portCHECK_IF_IN_ISR() == pdFALSE )
5235 portGET_TASK_LOCK();
5241 ( pxCurrentTCB->uxCriticalNesting )++;
5243 /* This should now be interrupt safe. The only time there would be
5244 * a problem is if this is called before a context switch and
5245 * vTaskExitCritical() is called after pxCurrentTCB changes. Therefore
5246 * this should not be used within vTaskSwitchContext(). */
5248 if( ( uxSchedulerSuspended == 0U ) && ( pxCurrentTCB->uxCriticalNesting == 1U ) )
5250 prvCheckForRunStateChange();
5255 mtCOVERAGE_TEST_MARKER();
5259 #endif /* portCRITICAL_NESTING_IN_TCB */
5260 /*-----------------------------------------------------------*/
5262 #if ( portCRITICAL_NESTING_IN_TCB == 1 )
5264 void vTaskExitCritical( void )
5266 if( xSchedulerRunning != pdFALSE )
5268 /* If pxCurrentTCB->uxCriticalNesting is zero then this function
5269 * does not match a previous call to vTaskEnterCritical(). */
5270 configASSERT( pxCurrentTCB->uxCriticalNesting > 0U );
5272 if( pxCurrentTCB->uxCriticalNesting > 0U )
5274 ( pxCurrentTCB->uxCriticalNesting )--;
5276 if( pxCurrentTCB->uxCriticalNesting == 0U )
5278 portRELEASE_ISR_LOCK();
5280 if( portCHECK_IF_IN_ISR() == pdFALSE )
5282 portRELEASE_TASK_LOCK();
5283 portENABLE_INTERRUPTS();
5285 /* When a task yields in a critical section it just sets
5286 * xYieldPending to true. So now that we have exited the
5287 * critical section check if xYieldPending is true, and
5289 if( xYieldPending != pdFALSE )
5296 /* In an ISR we don't hold the task lock and don't
5297 * need to yield. Yield will happen if necessary when
5298 * the application ISR calls portEND_SWITCHING_ISR() */
5299 mtCOVERAGE_TEST_MARKER();
5304 mtCOVERAGE_TEST_MARKER();
5309 mtCOVERAGE_TEST_MARKER();
5314 mtCOVERAGE_TEST_MARKER();
5318 #endif /* portCRITICAL_NESTING_IN_TCB */
5319 /*-----------------------------------------------------------*/
5321 #if ( ( configUSE_TRACE_FACILITY == 1 ) && ( configUSE_STATS_FORMATTING_FUNCTIONS > 0 ) )
5323 static char * prvWriteNameToBuffer( char * pcBuffer,
5324 const char * pcTaskName )
5328 /* Start by copying the entire string. */
5329 strcpy( pcBuffer, pcTaskName );
5331 /* Pad the end of the string with spaces to ensure columns line up when
5333 for( x = strlen( pcBuffer ); x < ( size_t ) ( configMAX_TASK_NAME_LEN - 1 ); x++ )
5335 pcBuffer[ x ] = ' ';
5339 pcBuffer[ x ] = ( char ) 0x00;
5341 /* Return the new end of string. */
5342 return &( pcBuffer[ x ] );
5345 #endif /* ( configUSE_TRACE_FACILITY == 1 ) && ( configUSE_STATS_FORMATTING_FUNCTIONS > 0 ) */
5346 /*-----------------------------------------------------------*/
5348 #if ( ( configUSE_TRACE_FACILITY == 1 ) && ( configUSE_STATS_FORMATTING_FUNCTIONS > 0 ) && ( configSUPPORT_DYNAMIC_ALLOCATION == 1 ) )
5350 void vTaskList( char * pcWriteBuffer )
5352 TaskStatus_t * pxTaskStatusArray;
5353 UBaseType_t uxArraySize, x;
5359 * This function is provided for convenience only, and is used by many
5360 * of the demo applications. Do not consider it to be part of the
5363 * vTaskList() calls uxTaskGetSystemState(), then formats part of the
5364 * uxTaskGetSystemState() output into a human readable table that
5365 * displays task: names, states, priority, stack usage and task number.
5366 * Stack usage specified as the number of unused StackType_t words stack can hold
5367 * on top of stack - not the number of bytes.
5369 * vTaskList() has a dependency on the sprintf() C library function that
5370 * might bloat the code size, use a lot of stack, and provide different
5371 * results on different platforms. An alternative, tiny, third party,
5372 * and limited functionality implementation of sprintf() is provided in
5373 * many of the FreeRTOS/Demo sub-directories in a file called
5374 * printf-stdarg.c (note printf-stdarg.c does not provide a full
5375 * snprintf() implementation!).
5377 * It is recommended that production systems call uxTaskGetSystemState()
5378 * directly to get access to raw stats data, rather than indirectly
5379 * through a call to vTaskList().
5383 /* Make sure the write buffer does not contain a string. */
5384 *pcWriteBuffer = ( char ) 0x00;
5386 /* Take a snapshot of the number of tasks in case it changes while this
5387 * function is executing. */
5388 uxArraySize = uxCurrentNumberOfTasks;
5390 /* Allocate an array index for each task. NOTE! if
5391 * configSUPPORT_DYNAMIC_ALLOCATION is set to 0 then pvPortMalloc() will
5392 * equate to NULL. */
5393 pxTaskStatusArray = pvPortMalloc( uxCurrentNumberOfTasks * sizeof( TaskStatus_t ) ); /*lint !e9079 All values returned by pvPortMalloc() have at least the alignment required by the MCU's stack and this allocation allocates a struct that has the alignment requirements of a pointer. */
5395 if( pxTaskStatusArray != NULL )
5397 /* Generate the (binary) data. */
5398 uxArraySize = uxTaskGetSystemState( pxTaskStatusArray, uxArraySize, NULL );
5400 /* Create a human readable table from the binary data. */
5401 for( x = 0; x < uxArraySize; x++ )
5403 switch( pxTaskStatusArray[ x ].eCurrentState )
5406 cStatus = tskRUNNING_CHAR;
5410 cStatus = tskREADY_CHAR;
5414 cStatus = tskBLOCKED_CHAR;
5418 cStatus = tskSUSPENDED_CHAR;
5422 cStatus = tskDELETED_CHAR;
5425 case eInvalid: /* Fall through. */
5426 default: /* Should not get here, but it is included
5427 * to prevent static checking errors. */
5428 cStatus = ( char ) 0x00;
5432 /* Write the task name to the string, padding with spaces so it
5433 * can be printed in tabular form more easily. */
5434 pcWriteBuffer = prvWriteNameToBuffer( pcWriteBuffer, pxTaskStatusArray[ x ].pcTaskName );
5436 /* Write the rest of the string. */
5437 sprintf( pcWriteBuffer, "\t%c\t%u\t%u\t%u\r\n", cStatus, ( unsigned int ) pxTaskStatusArray[ x ].uxCurrentPriority, ( unsigned int ) pxTaskStatusArray[ x ].usStackHighWaterMark, ( unsigned int ) pxTaskStatusArray[ x ].xTaskNumber ); /*lint !e586 sprintf() allowed as this is compiled with many compilers and this is a utility function only - not part of the core kernel implementation. */
5438 pcWriteBuffer += strlen( pcWriteBuffer ); /*lint !e9016 Pointer arithmetic ok on char pointers especially as in this case where it best denotes the intent of the code. */
5441 /* Free the array again. NOTE! If configSUPPORT_DYNAMIC_ALLOCATION
5442 * is 0 then vPortFree() will be #defined to nothing. */
5443 vPortFree( pxTaskStatusArray );
5447 mtCOVERAGE_TEST_MARKER();
5451 #endif /* ( ( configUSE_TRACE_FACILITY == 1 ) && ( configUSE_STATS_FORMATTING_FUNCTIONS > 0 ) && ( configSUPPORT_DYNAMIC_ALLOCATION == 1 ) ) */
5452 /*----------------------------------------------------------*/
5454 #if ( ( configGENERATE_RUN_TIME_STATS == 1 ) && ( configUSE_STATS_FORMATTING_FUNCTIONS > 0 ) && ( configSUPPORT_DYNAMIC_ALLOCATION == 1 ) )
5456 void vTaskGetRunTimeStats( char * pcWriteBuffer )
5458 TaskStatus_t * pxTaskStatusArray;
5459 UBaseType_t uxArraySize, x;
5460 uint32_t ulTotalTime, ulStatsAsPercentage;
5462 #if ( configUSE_TRACE_FACILITY != 1 )
5464 #error configUSE_TRACE_FACILITY must also be set to 1 in FreeRTOSConfig.h to use vTaskGetRunTimeStats().
5471 * This function is provided for convenience only, and is used by many
5472 * of the demo applications. Do not consider it to be part of the
5475 * vTaskGetRunTimeStats() calls uxTaskGetSystemState(), then formats part
5476 * of the uxTaskGetSystemState() output into a human readable table that
5477 * displays the amount of time each task has spent in the Running state
5478 * in both absolute and percentage terms.
5480 * vTaskGetRunTimeStats() has a dependency on the sprintf() C library
5481 * function that might bloat the code size, use a lot of stack, and
5482 * provide different results on different platforms. An alternative,
5483 * tiny, third party, and limited functionality implementation of
5484 * sprintf() is provided in many of the FreeRTOS/Demo sub-directories in
5485 * a file called printf-stdarg.c (note printf-stdarg.c does not provide
5486 * a full snprintf() implementation!).
5488 * It is recommended that production systems call uxTaskGetSystemState()
5489 * directly to get access to raw stats data, rather than indirectly
5490 * through a call to vTaskGetRunTimeStats().
5493 /* Make sure the write buffer does not contain a string. */
5494 *pcWriteBuffer = ( char ) 0x00;
5496 /* Take a snapshot of the number of tasks in case it changes while this
5497 * function is executing. */
5498 uxArraySize = uxCurrentNumberOfTasks;
5500 /* Allocate an array index for each task. NOTE! If
5501 * configSUPPORT_DYNAMIC_ALLOCATION is set to 0 then pvPortMalloc() will
5502 * equate to NULL. */
5503 pxTaskStatusArray = pvPortMalloc( uxCurrentNumberOfTasks * sizeof( TaskStatus_t ) ); /*lint !e9079 All values returned by pvPortMalloc() have at least the alignment required by the MCU's stack and this allocation allocates a struct that has the alignment requirements of a pointer. */
5505 if( pxTaskStatusArray != NULL )
5507 /* Generate the (binary) data. */
5508 uxArraySize = uxTaskGetSystemState( pxTaskStatusArray, uxArraySize, &ulTotalTime );
5510 /* For percentage calculations. */
5511 ulTotalTime /= 100UL;
5513 /* Avoid divide by zero errors. */
5514 if( ulTotalTime > 0UL )
5516 /* Create a human readable table from the binary data. */
5517 for( x = 0; x < uxArraySize; x++ )
5519 /* What percentage of the total run time has the task used?
5520 * This will always be rounded down to the nearest integer.
5521 * ulTotalRunTimeDiv100 has already been divided by 100. */
5522 ulStatsAsPercentage = pxTaskStatusArray[ x ].ulRunTimeCounter / ulTotalTime;
5524 /* Write the task name to the string, padding with
5525 * spaces so it can be printed in tabular form more
5527 pcWriteBuffer = prvWriteNameToBuffer( pcWriteBuffer, pxTaskStatusArray[ x ].pcTaskName );
5529 if( ulStatsAsPercentage > 0UL )
5531 #ifdef portLU_PRINTF_SPECIFIER_REQUIRED
5533 sprintf( pcWriteBuffer, "\t%lu\t\t%lu%%\r\n", pxTaskStatusArray[ x ].ulRunTimeCounter, ulStatsAsPercentage );
5537 /* sizeof( int ) == sizeof( long ) so a smaller
5538 * printf() library can be used. */
5539 sprintf( pcWriteBuffer, "\t%u\t\t%u%%\r\n", ( unsigned int ) pxTaskStatusArray[ x ].ulRunTimeCounter, ( unsigned int ) ulStatsAsPercentage ); /*lint !e586 sprintf() allowed as this is compiled with many compilers and this is a utility function only - not part of the core kernel implementation. */
5545 /* If the percentage is zero here then the task has
5546 * consumed less than 1% of the total run time. */
5547 #ifdef portLU_PRINTF_SPECIFIER_REQUIRED
5549 sprintf( pcWriteBuffer, "\t%lu\t\t<1%%\r\n", pxTaskStatusArray[ x ].ulRunTimeCounter );
5553 /* sizeof( int ) == sizeof( long ) so a smaller
5554 * printf() library can be used. */
5555 sprintf( pcWriteBuffer, "\t%u\t\t<1%%\r\n", ( unsigned int ) pxTaskStatusArray[ x ].ulRunTimeCounter ); /*lint !e586 sprintf() allowed as this is compiled with many compilers and this is a utility function only - not part of the core kernel implementation. */
5560 pcWriteBuffer += strlen( pcWriteBuffer ); /*lint !e9016 Pointer arithmetic ok on char pointers especially as in this case where it best denotes the intent of the code. */
5565 mtCOVERAGE_TEST_MARKER();
5568 /* Free the array again. NOTE! If configSUPPORT_DYNAMIC_ALLOCATION
5569 * is 0 then vPortFree() will be #defined to nothing. */
5570 vPortFree( pxTaskStatusArray );
5574 mtCOVERAGE_TEST_MARKER();
5578 #endif /* ( ( configGENERATE_RUN_TIME_STATS == 1 ) && ( configUSE_STATS_FORMATTING_FUNCTIONS > 0 ) && ( configSUPPORT_STATIC_ALLOCATION == 1 ) ) */
5579 /*-----------------------------------------------------------*/
5581 TickType_t uxTaskResetEventItemValue( void )
5583 TickType_t uxReturn;
5585 uxReturn = listGET_LIST_ITEM_VALUE( &( pxCurrentTCB->xEventListItem ) );
5587 /* Reset the event list item to its normal value - so it can be used with
5588 * queues and semaphores. */
5589 listSET_LIST_ITEM_VALUE( &( pxCurrentTCB->xEventListItem ), ( ( TickType_t ) configMAX_PRIORITIES - ( TickType_t ) pxCurrentTCB->uxPriority ) ); /*lint !e961 MISRA exception as the casts are only redundant for some ports. */
5593 /*-----------------------------------------------------------*/
5595 #if ( configUSE_MUTEXES == 1 )
5597 TaskHandle_t pvTaskIncrementMutexHeldCount( void )
5599 /* If xSemaphoreCreateMutex() is called before any tasks have been created
5600 * then pxCurrentTCB will be NULL. */
5601 if( pxCurrentTCB != NULL )
5603 ( pxCurrentTCB->uxMutexesHeld )++;
5606 return pxCurrentTCB;
5609 #endif /* configUSE_MUTEXES */
5610 /*-----------------------------------------------------------*/
5612 #if ( configUSE_TASK_NOTIFICATIONS == 1 )
5614 uint32_t ulTaskGenericNotifyTake( UBaseType_t uxIndexToWait,
5615 BaseType_t xClearCountOnExit,
5616 TickType_t xTicksToWait )
5620 configASSERT( uxIndexToWait < configTASK_NOTIFICATION_ARRAY_ENTRIES );
5622 taskENTER_CRITICAL();
5624 /* Only block if the notification count is not already non-zero. */
5625 if( pxCurrentTCB->ulNotifiedValue[ uxIndexToWait ] == 0UL )
5627 /* Mark this task as waiting for a notification. */
5628 pxCurrentTCB->ucNotifyState[ uxIndexToWait ] = taskWAITING_NOTIFICATION;
5630 if( xTicksToWait > ( TickType_t ) 0 )
5632 prvAddCurrentTaskToDelayedList( xTicksToWait, pdTRUE );
5633 traceTASK_NOTIFY_TAKE_BLOCK( uxIndexToWait );
5635 /* All ports are written to allow a yield in a critical
5636 * section (some will yield immediately, others wait until the
5637 * critical section exits) - but it is not something that
5638 * application code should ever do. */
5639 vTaskYieldWithinAPI();
5643 mtCOVERAGE_TEST_MARKER();
5648 mtCOVERAGE_TEST_MARKER();
5651 taskEXIT_CRITICAL();
5653 taskENTER_CRITICAL();
5655 traceTASK_NOTIFY_TAKE( uxIndexToWait );
5656 ulReturn = pxCurrentTCB->ulNotifiedValue[ uxIndexToWait ];
5658 if( ulReturn != 0UL )
5660 if( xClearCountOnExit != pdFALSE )
5662 pxCurrentTCB->ulNotifiedValue[ uxIndexToWait ] = 0UL;
5666 pxCurrentTCB->ulNotifiedValue[ uxIndexToWait ] = ulReturn - ( uint32_t ) 1;
5671 mtCOVERAGE_TEST_MARKER();
5674 pxCurrentTCB->ucNotifyState[ uxIndexToWait ] = taskNOT_WAITING_NOTIFICATION;
5676 taskEXIT_CRITICAL();
5681 #endif /* configUSE_TASK_NOTIFICATIONS */
5682 /*-----------------------------------------------------------*/
5684 #if ( configUSE_TASK_NOTIFICATIONS == 1 )
5686 BaseType_t xTaskGenericNotifyWait( UBaseType_t uxIndexToWait,
5687 uint32_t ulBitsToClearOnEntry,
5688 uint32_t ulBitsToClearOnExit,
5689 uint32_t * pulNotificationValue,
5690 TickType_t xTicksToWait )
5694 configASSERT( uxIndexToWait < configTASK_NOTIFICATION_ARRAY_ENTRIES );
5696 taskENTER_CRITICAL();
5698 /* Only block if a notification is not already pending. */
5699 if( pxCurrentTCB->ucNotifyState[ uxIndexToWait ] != taskNOTIFICATION_RECEIVED )
5701 /* Clear bits in the task's notification value as bits may get
5702 * set by the notifying task or interrupt. This can be used to
5703 * clear the value to zero. */
5704 pxCurrentTCB->ulNotifiedValue[ uxIndexToWait ] &= ~ulBitsToClearOnEntry;
5706 /* Mark this task as waiting for a notification. */
5707 pxCurrentTCB->ucNotifyState[ uxIndexToWait ] = taskWAITING_NOTIFICATION;
5709 if( xTicksToWait > ( TickType_t ) 0 )
5711 prvAddCurrentTaskToDelayedList( xTicksToWait, pdTRUE );
5712 traceTASK_NOTIFY_WAIT_BLOCK( uxIndexToWait );
5714 /* All ports are written to allow a yield in a critical
5715 * section (some will yield immediately, others wait until the
5716 * critical section exits) - but it is not something that
5717 * application code should ever do. */
5718 vTaskYieldWithinAPI();
5722 mtCOVERAGE_TEST_MARKER();
5727 mtCOVERAGE_TEST_MARKER();
5730 taskEXIT_CRITICAL();
5732 taskENTER_CRITICAL();
5734 traceTASK_NOTIFY_WAIT( uxIndexToWait );
5736 if( pulNotificationValue != NULL )
5738 /* Output the current notification value, which may or may not
5740 *pulNotificationValue = pxCurrentTCB->ulNotifiedValue[ uxIndexToWait ];
5743 /* If ucNotifyValue is set then either the task never entered the
5744 * blocked state (because a notification was already pending) or the
5745 * task unblocked because of a notification. Otherwise the task
5746 * unblocked because of a timeout. */
5747 if( pxCurrentTCB->ucNotifyState[ uxIndexToWait ] != taskNOTIFICATION_RECEIVED )
5749 /* A notification was not received. */
5754 /* A notification was already pending or a notification was
5755 * received while the task was waiting. */
5756 pxCurrentTCB->ulNotifiedValue[ uxIndexToWait ] &= ~ulBitsToClearOnExit;
5760 pxCurrentTCB->ucNotifyState[ uxIndexToWait ] = taskNOT_WAITING_NOTIFICATION;
5762 taskEXIT_CRITICAL();
5767 #endif /* configUSE_TASK_NOTIFICATIONS */
5768 /*-----------------------------------------------------------*/
5770 #if ( configUSE_TASK_NOTIFICATIONS == 1 )
5772 BaseType_t xTaskGenericNotify( TaskHandle_t xTaskToNotify,
5773 UBaseType_t uxIndexToNotify,
5775 eNotifyAction eAction,
5776 uint32_t * pulPreviousNotificationValue )
5779 BaseType_t xReturn = pdPASS;
5780 uint8_t ucOriginalNotifyState;
5782 configASSERT( uxIndexToNotify < configTASK_NOTIFICATION_ARRAY_ENTRIES );
5783 configASSERT( xTaskToNotify );
5784 pxTCB = xTaskToNotify;
5786 taskENTER_CRITICAL();
5788 if( pulPreviousNotificationValue != NULL )
5790 *pulPreviousNotificationValue = pxTCB->ulNotifiedValue[ uxIndexToNotify ];
5793 ucOriginalNotifyState = pxTCB->ucNotifyState[ uxIndexToNotify ];
5795 pxTCB->ucNotifyState[ uxIndexToNotify ] = taskNOTIFICATION_RECEIVED;
5800 pxTCB->ulNotifiedValue[ uxIndexToNotify ] |= ulValue;
5804 ( pxTCB->ulNotifiedValue[ uxIndexToNotify ] )++;
5807 case eSetValueWithOverwrite:
5808 pxTCB->ulNotifiedValue[ uxIndexToNotify ] = ulValue;
5811 case eSetValueWithoutOverwrite:
5813 if( ucOriginalNotifyState != taskNOTIFICATION_RECEIVED )
5815 pxTCB->ulNotifiedValue[ uxIndexToNotify ] = ulValue;
5819 /* The value could not be written to the task. */
5827 /* The task is being notified without its notify value being
5833 /* Should not get here if all enums are handled.
5834 * Artificially force an assert by testing a value the
5835 * compiler can't assume is const. */
5836 configASSERT( xTickCount == ( TickType_t ) 0 );
5841 traceTASK_NOTIFY( uxIndexToNotify );
5843 /* If the task is in the blocked state specifically to wait for a
5844 * notification then unblock it now. */
5845 if( ucOriginalNotifyState == taskWAITING_NOTIFICATION )
5847 ( void ) uxListRemove( &( pxTCB->xStateListItem ) );
5848 prvAddTaskToReadyList( pxTCB );
5850 /* The task should not have been on an event list. */
5851 configASSERT( listLIST_ITEM_CONTAINER( &( pxTCB->xEventListItem ) ) == NULL );
5853 #if ( configUSE_TICKLESS_IDLE != 0 )
5855 /* If a task is blocked waiting for a notification then
5856 * xNextTaskUnblockTime might be set to the blocked task's time
5857 * out time. If the task is unblocked for a reason other than
5858 * a timeout xNextTaskUnblockTime is normally left unchanged,
5859 * because it will automatically get reset to a new value when
5860 * the tick count equals xNextTaskUnblockTime. However if
5861 * tickless idling is used it might be more important to enter
5862 * sleep mode at the earliest possible time - so reset
5863 * xNextTaskUnblockTime here to ensure it is updated at the
5864 * earliest possible time. */
5865 prvResetNextTaskUnblockTime();
5869 #if ( configUSE_PREEMPTION == 1 )
5871 prvYieldForTask( pxTCB, pdFALSE );
5877 mtCOVERAGE_TEST_MARKER();
5880 taskEXIT_CRITICAL();
5885 #endif /* configUSE_TASK_NOTIFICATIONS */
5886 /*-----------------------------------------------------------*/
5888 #if ( configUSE_TASK_NOTIFICATIONS == 1 )
5890 BaseType_t xTaskGenericNotifyFromISR( TaskHandle_t xTaskToNotify,
5891 UBaseType_t uxIndexToNotify,
5893 eNotifyAction eAction,
5894 uint32_t * pulPreviousNotificationValue,
5895 BaseType_t * pxHigherPriorityTaskWoken )
5898 uint8_t ucOriginalNotifyState;
5899 BaseType_t xReturn = pdPASS;
5900 UBaseType_t uxSavedInterruptStatus;
5902 configASSERT( xTaskToNotify );
5903 configASSERT( uxIndexToNotify < configTASK_NOTIFICATION_ARRAY_ENTRIES );
5905 /* RTOS ports that support interrupt nesting have the concept of a
5906 * maximum system call (or maximum API call) interrupt priority.
5907 * Interrupts that are above the maximum system call priority are keep
5908 * permanently enabled, even when the RTOS kernel is in a critical section,
5909 * but cannot make any calls to FreeRTOS API functions. If configASSERT()
5910 * is defined in FreeRTOSConfig.h then
5911 * portASSERT_IF_INTERRUPT_PRIORITY_INVALID() will result in an assertion
5912 * failure if a FreeRTOS API function is called from an interrupt that has
5913 * been assigned a priority above the configured maximum system call
5914 * priority. Only FreeRTOS functions that end in FromISR can be called
5915 * from interrupts that have been assigned a priority at or (logically)
5916 * below the maximum system call interrupt priority. FreeRTOS maintains a
5917 * separate interrupt safe API to ensure interrupt entry is as fast and as
5918 * simple as possible. More information (albeit Cortex-M specific) is
5919 * provided on the following link:
5920 * https://www.FreeRTOS.org/RTOS-Cortex-M3-M4.html */
5921 portASSERT_IF_INTERRUPT_PRIORITY_INVALID();
5923 pxTCB = xTaskToNotify;
5925 uxSavedInterruptStatus = portSET_INTERRUPT_MASK_FROM_ISR();
5927 if( pulPreviousNotificationValue != NULL )
5929 *pulPreviousNotificationValue = pxTCB->ulNotifiedValue[ uxIndexToNotify ];
5932 ucOriginalNotifyState = pxTCB->ucNotifyState[ uxIndexToNotify ];
5933 pxTCB->ucNotifyState[ uxIndexToNotify ] = taskNOTIFICATION_RECEIVED;
5938 pxTCB->ulNotifiedValue[ uxIndexToNotify ] |= ulValue;
5942 ( pxTCB->ulNotifiedValue[ uxIndexToNotify ] )++;
5945 case eSetValueWithOverwrite:
5946 pxTCB->ulNotifiedValue[ uxIndexToNotify ] = ulValue;
5949 case eSetValueWithoutOverwrite:
5951 if( ucOriginalNotifyState != taskNOTIFICATION_RECEIVED )
5953 pxTCB->ulNotifiedValue[ uxIndexToNotify ] = ulValue;
5957 /* The value could not be written to the task. */
5965 /* The task is being notified without its notify value being
5971 /* Should not get here if all enums are handled.
5972 * Artificially force an assert by testing a value the
5973 * compiler can't assume is const. */
5974 configASSERT( xTickCount == ( TickType_t ) 0 );
5978 traceTASK_NOTIFY_FROM_ISR( uxIndexToNotify );
5980 /* If the task is in the blocked state specifically to wait for a
5981 * notification then unblock it now. */
5982 if( ucOriginalNotifyState == taskWAITING_NOTIFICATION )
5984 /* The task should not have been on an event list. */
5985 configASSERT( listLIST_ITEM_CONTAINER( &( pxTCB->xEventListItem ) ) == NULL );
5987 if( uxSchedulerSuspended == ( UBaseType_t ) pdFALSE )
5989 ( void ) uxListRemove( &( pxTCB->xStateListItem ) );
5990 prvAddTaskToReadyList( pxTCB );
5994 /* The delayed and ready lists cannot be accessed, so hold
5995 * this task pending until the scheduler is resumed. */
5996 vListInsertEnd( &( xPendingReadyList ), &( pxTCB->xEventListItem ) );
5999 #if ( configUSE_PREEMPTION == 1 )
6000 prvYieldForTask( pxTCB, pdFALSE );
6002 if( xYieldPendings[ portGET_CORE_ID() ] == pdTRUE )
6004 if( pxHigherPriorityTaskWoken != NULL )
6006 *pxHigherPriorityTaskWoken = pdTRUE;
6012 portCLEAR_INTERRUPT_MASK_FROM_ISR( uxSavedInterruptStatus );
6017 #endif /* configUSE_TASK_NOTIFICATIONS */
6018 /*-----------------------------------------------------------*/
6020 #if ( configUSE_TASK_NOTIFICATIONS == 1 )
6022 void vTaskGenericNotifyGiveFromISR( TaskHandle_t xTaskToNotify,
6023 UBaseType_t uxIndexToNotify,
6024 BaseType_t * pxHigherPriorityTaskWoken )
6027 uint8_t ucOriginalNotifyState;
6028 UBaseType_t uxSavedInterruptStatus;
6030 configASSERT( xTaskToNotify );
6031 configASSERT( uxIndexToNotify < configTASK_NOTIFICATION_ARRAY_ENTRIES );
6033 /* RTOS ports that support interrupt nesting have the concept of a
6034 * maximum system call (or maximum API call) interrupt priority.
6035 * Interrupts that are above the maximum system call priority are keep
6036 * permanently enabled, even when the RTOS kernel is in a critical section,
6037 * but cannot make any calls to FreeRTOS API functions. If configASSERT()
6038 * is defined in FreeRTOSConfig.h then
6039 * portASSERT_IF_INTERRUPT_PRIORITY_INVALID() will result in an assertion
6040 * failure if a FreeRTOS API function is called from an interrupt that has
6041 * been assigned a priority above the configured maximum system call
6042 * priority. Only FreeRTOS functions that end in FromISR can be called
6043 * from interrupts that have been assigned a priority at or (logically)
6044 * below the maximum system call interrupt priority. FreeRTOS maintains a
6045 * separate interrupt safe API to ensure interrupt entry is as fast and as
6046 * simple as possible. More information (albeit Cortex-M specific) is
6047 * provided on the following link:
6048 * https://www.FreeRTOS.org/RTOS-Cortex-M3-M4.html */
6049 portASSERT_IF_INTERRUPT_PRIORITY_INVALID();
6051 pxTCB = xTaskToNotify;
6053 uxSavedInterruptStatus = portSET_INTERRUPT_MASK_FROM_ISR();
6055 ucOriginalNotifyState = pxTCB->ucNotifyState[ uxIndexToNotify ];
6056 pxTCB->ucNotifyState[ uxIndexToNotify ] = taskNOTIFICATION_RECEIVED;
6058 /* 'Giving' is equivalent to incrementing a count in a counting
6060 ( pxTCB->ulNotifiedValue[ uxIndexToNotify ] )++;
6062 traceTASK_NOTIFY_GIVE_FROM_ISR( uxIndexToNotify );
6064 /* If the task is in the blocked state specifically to wait for a
6065 * notification then unblock it now. */
6066 if( ucOriginalNotifyState == taskWAITING_NOTIFICATION )
6068 /* The task should not have been on an event list. */
6069 configASSERT( listLIST_ITEM_CONTAINER( &( pxTCB->xEventListItem ) ) == NULL );
6071 if( uxSchedulerSuspended == ( UBaseType_t ) pdFALSE )
6073 ( void ) uxListRemove( &( pxTCB->xStateListItem ) );
6074 prvAddTaskToReadyList( pxTCB );
6078 /* The delayed and ready lists cannot be accessed, so hold
6079 * this task pending until the scheduler is resumed. */
6080 vListInsertEnd( &( xPendingReadyList ), &( pxTCB->xEventListItem ) );
6083 #if ( configUSE_PREEMPTION == 1 )
6084 prvYieldForTask( pxTCB, pdFALSE );
6086 if( xYieldPendings[ portGET_CORE_ID() ] == pdTRUE )
6088 if( pxHigherPriorityTaskWoken != NULL )
6090 *pxHigherPriorityTaskWoken = pdTRUE;
6096 portCLEAR_INTERRUPT_MASK_FROM_ISR( uxSavedInterruptStatus );
6099 #endif /* configUSE_TASK_NOTIFICATIONS */
6100 /*-----------------------------------------------------------*/
6102 #if ( configUSE_TASK_NOTIFICATIONS == 1 )
6104 BaseType_t xTaskGenericNotifyStateClear( TaskHandle_t xTask,
6105 UBaseType_t uxIndexToClear )
6110 configASSERT( uxIndexToClear < configTASK_NOTIFICATION_ARRAY_ENTRIES );
6112 /* If null is passed in here then it is the calling task that is having
6113 * its notification state cleared. */
6114 pxTCB = prvGetTCBFromHandle( xTask );
6116 taskENTER_CRITICAL();
6118 if( pxTCB->ucNotifyState[ uxIndexToClear ] == taskNOTIFICATION_RECEIVED )
6120 pxTCB->ucNotifyState[ uxIndexToClear ] = taskNOT_WAITING_NOTIFICATION;
6128 taskEXIT_CRITICAL();
6133 #endif /* configUSE_TASK_NOTIFICATIONS */
6134 /*-----------------------------------------------------------*/
6136 #if ( configUSE_TASK_NOTIFICATIONS == 1 )
6138 uint32_t ulTaskGenericNotifyValueClear( TaskHandle_t xTask,
6139 UBaseType_t uxIndexToClear,
6140 uint32_t ulBitsToClear )
6145 /* If null is passed in here then it is the calling task that is having
6146 * its notification state cleared. */
6147 pxTCB = prvGetTCBFromHandle( xTask );
6149 taskENTER_CRITICAL();
6151 /* Return the notification as it was before the bits were cleared,
6152 * then clear the bit mask. */
6153 ulReturn = pxTCB->ulNotifiedValue[ uxIndexToClear ];
6154 pxTCB->ulNotifiedValue[ uxIndexToClear ] &= ~ulBitsToClear;
6156 taskEXIT_CRITICAL();
6161 #endif /* configUSE_TASK_NOTIFICATIONS */
6162 /*-----------------------------------------------------------*/
6164 #if ( ( configGENERATE_RUN_TIME_STATS == 1 ) && ( INCLUDE_xTaskGetIdleTaskHandle == 1 ) )
6166 uint32_t ulTaskGetIdleRunTimeCounter( void )
6168 uint32_t ulReturn = 0;
6170 for( BaseType_t i = 0; i < configNUM_CORES; i++ )
6172 ulReturn += xIdleTaskHandle[ i ]->ulRunTimeCounter;
6178 #endif /* if ( ( configGENERATE_RUN_TIME_STATS == 1 ) && ( INCLUDE_xTaskGetIdleTaskHandle == 1 ) ) */
6179 /*-----------------------------------------------------------*/
6181 static void prvAddCurrentTaskToDelayedList( TickType_t xTicksToWait,
6182 const BaseType_t xCanBlockIndefinitely )
6184 TickType_t xTimeToWake;
6185 const TickType_t xConstTickCount = xTickCount;
6187 #if ( INCLUDE_xTaskAbortDelay == 1 )
6189 /* About to enter a delayed list, so ensure the ucDelayAborted flag is
6190 * reset to pdFALSE so it can be detected as having been set to pdTRUE
6191 * when the task leaves the Blocked state. */
6192 pxCurrentTCB->ucDelayAborted = pdFALSE;
6196 /* Remove the task from the ready list before adding it to the blocked list
6197 * as the same list item is used for both lists. */
6198 if( uxListRemove( &( pxCurrentTCB->xStateListItem ) ) == ( UBaseType_t ) 0 )
6200 /* The current task must be in a ready list, so there is no need to
6201 * check, and the port reset macro can be called directly. */
6202 portRESET_READY_PRIORITY( pxCurrentTCB->uxPriority, uxTopReadyPriority ); /*lint !e931 pxCurrentTCB cannot change as it is the calling task. pxCurrentTCB->uxPriority and uxTopReadyPriority cannot change as called with scheduler suspended or in a critical section. */
6206 mtCOVERAGE_TEST_MARKER();
6209 #if ( INCLUDE_vTaskSuspend == 1 )
6211 if( ( xTicksToWait == portMAX_DELAY ) && ( xCanBlockIndefinitely != pdFALSE ) )
6213 /* Add the task to the suspended task list instead of a delayed task
6214 * list to ensure it is not woken by a timing event. It will block
6216 vListInsertEnd( &xSuspendedTaskList, &( pxCurrentTCB->xStateListItem ) );
6220 /* Calculate the time at which the task should be woken if the event
6221 * does not occur. This may overflow but this doesn't matter, the
6222 * kernel will manage it correctly. */
6223 xTimeToWake = xConstTickCount + xTicksToWait;
6225 /* The list item will be inserted in wake time order. */
6226 listSET_LIST_ITEM_VALUE( &( pxCurrentTCB->xStateListItem ), xTimeToWake );
6228 if( xTimeToWake < xConstTickCount )
6230 /* Wake time has overflowed. Place this item in the overflow
6232 vListInsert( pxOverflowDelayedTaskList, &( pxCurrentTCB->xStateListItem ) );
6236 /* The wake time has not overflowed, so the current block list
6238 vListInsert( pxDelayedTaskList, &( pxCurrentTCB->xStateListItem ) );
6240 /* If the task entering the blocked state was placed at the
6241 * head of the list of blocked tasks then xNextTaskUnblockTime
6242 * needs to be updated too. */
6243 if( xTimeToWake < xNextTaskUnblockTime )
6245 xNextTaskUnblockTime = xTimeToWake;
6249 mtCOVERAGE_TEST_MARKER();
6254 #else /* INCLUDE_vTaskSuspend */
6256 /* Calculate the time at which the task should be woken if the event
6257 * does not occur. This may overflow but this doesn't matter, the kernel
6258 * will manage it correctly. */
6259 xTimeToWake = xConstTickCount + xTicksToWait;
6261 /* The list item will be inserted in wake time order. */
6262 listSET_LIST_ITEM_VALUE( &( pxCurrentTCB->xStateListItem ), xTimeToWake );
6264 if( xTimeToWake < xConstTickCount )
6266 /* Wake time has overflowed. Place this item in the overflow list. */
6267 vListInsert( pxOverflowDelayedTaskList, &( pxCurrentTCB->xStateListItem ) );
6271 /* The wake time has not overflowed, so the current block list is used. */
6272 vListInsert( pxDelayedTaskList, &( pxCurrentTCB->xStateListItem ) );
6274 /* If the task entering the blocked state was placed at the head of the
6275 * list of blocked tasks then xNextTaskUnblockTime needs to be updated
6277 if( xTimeToWake < xNextTaskUnblockTime )
6279 xNextTaskUnblockTime = xTimeToWake;
6283 mtCOVERAGE_TEST_MARKER();
6287 /* Avoid compiler warning when INCLUDE_vTaskSuspend is not 1. */
6288 ( void ) xCanBlockIndefinitely;
6290 #endif /* INCLUDE_vTaskSuspend */
6293 /* Code below here allows additional code to be inserted into this source file,
6294 * especially where access to file scope functions and data is needed (for example
6295 * when performing module tests). */
6297 #ifdef FREERTOS_MODULE_TEST
6298 #include "tasks_test_access_functions.h"
6302 #if ( configINCLUDE_FREERTOS_TASK_C_ADDITIONS_H == 1 )
6304 #include "freertos_tasks_c_additions.h"
6306 #ifdef FREERTOS_TASKS_C_ADDITIONS_INIT
6307 static void freertos_tasks_c_additions_init( void )
6309 FREERTOS_TASKS_C_ADDITIONS_INIT();
6313 #endif /* if ( configINCLUDE_FREERTOS_TASK_C_ADDITIONS_H == 1 ) */