2 * FreeRTOS Kernel <DEVELOPMENT BRANCH>
3 * Copyright (C) 2021 Amazon.com, Inc. or its affiliates. All Rights Reserved.
5 * SPDX-License-Identifier: MIT
7 * Permission is hereby granted, free of charge, to any person obtaining a copy of
8 * this software and associated documentation files (the "Software"), to deal in
9 * the Software without restriction, including without limitation the rights to
10 * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
11 * the Software, and to permit persons to whom the Software is furnished to do so,
12 * subject to the following conditions:
14 * The above copyright notice and this permission notice shall be included in all
15 * copies or substantial portions of the Software.
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
19 * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
20 * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
21 * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
22 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
24 * https://www.FreeRTOS.org
25 * https://github.com/FreeRTOS
29 /* Standard includes. */
33 /* Defining MPU_WRAPPERS_INCLUDED_FROM_API_FILE prevents task.h from redefining
34 * all the API functions to use the MPU wrappers. That should only be done when
35 * task.h is included from an application file. */
36 #define MPU_WRAPPERS_INCLUDED_FROM_API_FILE
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 ( configNUMBER_OF_CORES == 1 )
62 #if ( configUSE_PREEMPTION == 0 )
64 /* If the cooperative scheduler is being used then a yield should not be
65 * performed just because a higher priority task has been woken. */
66 #define taskYIELD_IF_USING_PREEMPTION()
68 #define taskYIELD_IF_USING_PREEMPTION() portYIELD_WITHIN_API()
70 #endif /* if ( configNUMBER_OF_CORES == 1 ) */
72 /* Values that can be assigned to the ucNotifyState member of the TCB. */
73 #define taskNOT_WAITING_NOTIFICATION ( ( uint8_t ) 0 ) /* Must be zero as it is the initialised value. */
74 #define taskWAITING_NOTIFICATION ( ( uint8_t ) 1 )
75 #define taskNOTIFICATION_RECEIVED ( ( uint8_t ) 2 )
78 * The value used to fill the stack of a task when the task is created. This
79 * is used purely for checking the high water mark for tasks.
81 #define tskSTACK_FILL_BYTE ( 0xa5U )
83 /* Bits used to record how a task's stack and TCB were allocated. */
84 #define tskDYNAMICALLY_ALLOCATED_STACK_AND_TCB ( ( uint8_t ) 0 )
85 #define tskSTATICALLY_ALLOCATED_STACK_ONLY ( ( uint8_t ) 1 )
86 #define tskSTATICALLY_ALLOCATED_STACK_AND_TCB ( ( uint8_t ) 2 )
88 /* If any of the following are set then task stacks are filled with a known
89 * value so the high water mark can be determined. If none of the following are
90 * set then don't fill the stack so there is no unnecessary dependency on memset. */
91 #if ( ( configCHECK_FOR_STACK_OVERFLOW > 1 ) || ( configUSE_TRACE_FACILITY == 1 ) || ( INCLUDE_uxTaskGetStackHighWaterMark == 1 ) || ( INCLUDE_uxTaskGetStackHighWaterMark2 == 1 ) )
92 #define tskSET_NEW_STACKS_TO_KNOWN_VALUE 1
94 #define tskSET_NEW_STACKS_TO_KNOWN_VALUE 0
98 * Macros used by vListTask to indicate which state a task is in.
100 #define tskRUNNING_CHAR ( 'X' )
101 #define tskBLOCKED_CHAR ( 'B' )
102 #define tskREADY_CHAR ( 'R' )
103 #define tskDELETED_CHAR ( 'D' )
104 #define tskSUSPENDED_CHAR ( 'S' )
107 * Some kernel aware debuggers require the data the debugger needs access to to
108 * be global, rather than file scope.
110 #ifdef portREMOVE_STATIC_QUALIFIER
114 /* The name allocated to the Idle task. This can be overridden by defining
115 * configIDLE_TASK_NAME in FreeRTOSConfig.h. */
116 #ifndef configIDLE_TASK_NAME
117 #define configIDLE_TASK_NAME "IDLE"
120 #if ( configUSE_PORT_OPTIMISED_TASK_SELECTION == 0 )
122 /* If configUSE_PORT_OPTIMISED_TASK_SELECTION is 0 then task selection is
123 * performed in a generic way that is not optimised to any particular
124 * microcontroller architecture. */
126 /* uxTopReadyPriority holds the priority of the highest priority ready
128 #define taskRECORD_READY_PRIORITY( uxPriority ) \
130 if( ( uxPriority ) > uxTopReadyPriority ) \
132 uxTopReadyPriority = ( uxPriority ); \
134 } while( 0 ) /* taskRECORD_READY_PRIORITY */
136 /*-----------------------------------------------------------*/
138 #if ( configNUMBER_OF_CORES == 1 )
139 #define taskSELECT_HIGHEST_PRIORITY_TASK() \
141 UBaseType_t uxTopPriority = uxTopReadyPriority; \
143 /* Find the highest priority queue that contains ready tasks. */ \
144 while( listLIST_IS_EMPTY( &( pxReadyTasksLists[ uxTopPriority ] ) ) ) \
146 configASSERT( uxTopPriority ); \
150 /* listGET_OWNER_OF_NEXT_ENTRY indexes through the list, so the tasks of \
151 * the same priority get an equal share of the processor time. */ \
152 listGET_OWNER_OF_NEXT_ENTRY( pxCurrentTCB, &( pxReadyTasksLists[ uxTopPriority ] ) ); \
153 uxTopReadyPriority = uxTopPriority; \
154 } while( 0 ) /* taskSELECT_HIGHEST_PRIORITY_TASK */
155 #endif /* if ( configNUMBER_OF_CORES == 1 ) */
157 /*-----------------------------------------------------------*/
159 /* Define away taskRESET_READY_PRIORITY() and portRESET_READY_PRIORITY() as
160 * they are only required when a port optimised method of task selection is
162 #define taskRESET_READY_PRIORITY( uxPriority )
163 #define portRESET_READY_PRIORITY( uxPriority, uxTopReadyPriority )
165 #else /* configUSE_PORT_OPTIMISED_TASK_SELECTION */
167 #if ( configNUMBER_OF_CORES > 1 )
168 #error configUSE_PORT_OPTIMISED_TASK_SELECTION not supported in FreeRTOS SMP.
171 /* If configUSE_PORT_OPTIMISED_TASK_SELECTION is 1 then task selection is
172 * performed in a way that is tailored to the particular microcontroller
173 * architecture being used. */
175 /* A port optimised version is provided. Call the port defined macros. */
176 #define taskRECORD_READY_PRIORITY( uxPriority ) portRECORD_READY_PRIORITY( ( uxPriority ), uxTopReadyPriority )
178 /*-----------------------------------------------------------*/
180 #define taskSELECT_HIGHEST_PRIORITY_TASK() \
182 UBaseType_t uxTopPriority; \
184 /* Find the highest priority list that contains ready tasks. */ \
185 portGET_HIGHEST_PRIORITY( uxTopPriority, uxTopReadyPriority ); \
186 configASSERT( listCURRENT_LIST_LENGTH( &( pxReadyTasksLists[ uxTopPriority ] ) ) > 0 ); \
187 listGET_OWNER_OF_NEXT_ENTRY( pxCurrentTCB, &( pxReadyTasksLists[ uxTopPriority ] ) ); \
190 /*-----------------------------------------------------------*/
192 /* A port optimised version is provided, call it only if the TCB being reset
193 * is being referenced from a ready list. If it is referenced from a delayed
194 * or suspended list then it won't be in a ready list. */
195 #define taskRESET_READY_PRIORITY( uxPriority ) \
197 if( listCURRENT_LIST_LENGTH( &( pxReadyTasksLists[ ( uxPriority ) ] ) ) == ( UBaseType_t ) 0 ) \
199 portRESET_READY_PRIORITY( ( uxPriority ), ( uxTopReadyPriority ) ); \
203 #endif /* configUSE_PORT_OPTIMISED_TASK_SELECTION */
205 /*-----------------------------------------------------------*/
207 /* pxDelayedTaskList and pxOverflowDelayedTaskList are switched when the tick
208 * count overflows. */
209 #define taskSWITCH_DELAYED_LISTS() \
213 /* The delayed tasks list should be empty when the lists are switched. */ \
214 configASSERT( ( listLIST_IS_EMPTY( pxDelayedTaskList ) ) ); \
216 pxTemp = pxDelayedTaskList; \
217 pxDelayedTaskList = pxOverflowDelayedTaskList; \
218 pxOverflowDelayedTaskList = pxTemp; \
220 prvResetNextTaskUnblockTime(); \
223 /*-----------------------------------------------------------*/
226 * Place the task represented by pxTCB into the appropriate ready list for
227 * the task. It is inserted at the end of the list.
229 #define prvAddTaskToReadyList( pxTCB ) \
231 traceMOVED_TASK_TO_READY_STATE( pxTCB ); \
232 taskRECORD_READY_PRIORITY( ( pxTCB )->uxPriority ); \
233 listINSERT_END( &( pxReadyTasksLists[ ( pxTCB )->uxPriority ] ), &( ( pxTCB )->xStateListItem ) ); \
234 tracePOST_MOVED_TASK_TO_READY_STATE( pxTCB ); \
236 /*-----------------------------------------------------------*/
239 * Several functions take a TaskHandle_t parameter that can optionally be NULL,
240 * where NULL is used to indicate that the handle of the currently executing
241 * task should be used in place of the parameter. This macro simply checks to
242 * see if the parameter is NULL and returns a pointer to the appropriate TCB.
244 #define prvGetTCBFromHandle( pxHandle ) ( ( ( pxHandle ) == NULL ) ? pxCurrentTCB : ( pxHandle ) )
246 /* The item value of the event list item is normally used to hold the priority
247 * of the task to which it belongs (coded to allow it to be held in reverse
248 * priority order). However, it is occasionally borrowed for other purposes. It
249 * is important its value is not updated due to a task priority change while it is
250 * being used for another purpose. The following bit definition is used to inform
251 * the scheduler that the value should not be changed - in which case it is the
252 * responsibility of whichever module is using the value to ensure it gets set back
253 * to its original value when it is released. */
254 #if ( configTICK_TYPE_WIDTH_IN_BITS == TICK_TYPE_WIDTH_16_BITS )
255 #define taskEVENT_LIST_ITEM_VALUE_IN_USE 0x8000U
256 #elif ( configTICK_TYPE_WIDTH_IN_BITS == TICK_TYPE_WIDTH_32_BITS )
257 #define taskEVENT_LIST_ITEM_VALUE_IN_USE 0x80000000UL
258 #elif ( configTICK_TYPE_WIDTH_IN_BITS == TICK_TYPE_WIDTH_64_BITS )
259 #define taskEVENT_LIST_ITEM_VALUE_IN_USE 0x8000000000000000ULL
263 typedef BaseType_t TaskRunning_t;
265 /* Indicates that the task is not actively running on any core. */
266 #define taskTASK_NOT_RUNNING ( TaskRunning_t ) ( -1 )
268 /* Indicates that the task is actively running but scheduled to yield. */
269 #define taskTASK_YIELDING ( TaskRunning_t ) ( -2 )
271 /* Returns pdTRUE if the task is actively running and not scheduled to yield. */
272 #if ( configNUMBER_OF_CORES == 1 )
273 #define taskTASK_IS_RUNNING( pxTCB ) ( ( ( pxTCB ) == pxCurrentTCB ) ? ( pdTRUE ) : ( pdFALSE ) )
275 #define taskTASK_IS_RUNNING( pxTCB ) ( ( ( ( pxTCB )->xTaskRunState >= ( BaseType_t ) 0 ) && ( ( pxTCB )->xTaskRunState < ( BaseType_t ) configNUMBER_OF_CORES ) ) ? ( pdTRUE ) : ( pdFALSE ) )
278 /* Indicates that the task is an Idle task. */
279 #define taskATTRIBUTE_IS_IDLE ( UBaseType_t ) ( 1UL << 0UL )
281 #if ( ( configNUMBER_OF_CORES > 1 ) && ( portCRITICAL_NESTING_IN_TCB == 1 ) )
282 #define portGET_CRITICAL_NESTING_COUNT() ( pxCurrentTCBs[ portGET_CORE_ID() ]->uxCriticalNesting )
283 #define portSET_CRITICAL_NESTING_COUNT( x ) ( pxCurrentTCBs[ portGET_CORE_ID() ]->uxCriticalNesting = ( x ) )
284 #define portINCREMENT_CRITICAL_NESTING_COUNT() ( pxCurrentTCBs[ portGET_CORE_ID() ]->uxCriticalNesting++ )
285 #define portDECREMENT_CRITICAL_NESTING_COUNT() ( pxCurrentTCBs[ portGET_CORE_ID() ]->uxCriticalNesting-- )
286 #endif /* #if ( ( configNUMBER_OF_CORES > 1 ) && ( portCRITICAL_NESTING_IN_TCB == 1 ) ) */
288 /* Code below here allows infinite loop controlling, especially for the infinite loop
289 * in idle task function (for example when performing unit tests). */
290 #ifndef INFINITE_LOOP
291 #define INFINITE_LOOP() 1
295 * Task control block. A task control block (TCB) is allocated for each task,
296 * and stores task state information, including a pointer to the task's context
297 * (the task's run time environment, including register values)
299 typedef struct tskTaskControlBlock /* The old naming convention is used to prevent breaking kernel aware debuggers. */
301 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. */
303 #if ( portUSING_MPU_WRAPPERS == 1 )
304 xMPU_SETTINGS xMPUSettings; /**< The MPU settings are defined as part of the port layer. THIS MUST BE THE SECOND MEMBER OF THE TCB STRUCT. */
307 #if ( configUSE_CORE_AFFINITY == 1 ) && ( configNUMBER_OF_CORES > 1 )
308 UBaseType_t uxCoreAffinityMask; /*< Used to link the task to certain cores. UBaseType_t must have greater than or equal to the number of bits as confNUM_CORES. */
311 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 ). */
312 ListItem_t xEventListItem; /**< Used to reference a task from an event list. */
313 UBaseType_t uxPriority; /**< The priority of the task. 0 is the lowest priority. */
314 StackType_t * pxStack; /**< Points to the start of the stack. */
315 #if ( configNUMBER_OF_CORES > 1 )
316 volatile TaskRunning_t xTaskRunState; /**< Used to identify the core the task is running on, if the task is running. Otherwise, identifies the task's state - not running or yielding. */
317 UBaseType_t uxTaskAttributes; /**< Task's attributes - currently used to identify the idle tasks. */
319 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. */
321 #if ( configUSE_TASK_PREEMPTION_DISABLE == 1 )
322 BaseType_t xPreemptionDisable; /**< Used to prevent the task from being preempted. */
325 #if ( ( portSTACK_GROWTH > 0 ) || ( configRECORD_STACK_HIGH_ADDRESS == 1 ) )
326 StackType_t * pxEndOfStack; /**< Points to the highest valid address for the stack. */
329 #if ( portCRITICAL_NESTING_IN_TCB == 1 )
330 UBaseType_t uxCriticalNesting; /**< Holds the critical section nesting depth for ports that do not maintain their own count in the port layer. */
333 #if ( configUSE_TRACE_FACILITY == 1 )
334 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. */
335 UBaseType_t uxTaskNumber; /**< Stores a number specifically for use by third party trace code. */
338 #if ( configUSE_MUTEXES == 1 )
339 UBaseType_t uxBasePriority; /**< The priority last assigned to the task - used by the priority inheritance mechanism. */
340 UBaseType_t uxMutexesHeld;
343 #if ( configUSE_APPLICATION_TASK_TAG == 1 )
344 TaskHookFunction_t pxTaskTag;
347 #if ( configNUM_THREAD_LOCAL_STORAGE_POINTERS > 0 )
348 void * pvThreadLocalStoragePointers[ configNUM_THREAD_LOCAL_STORAGE_POINTERS ];
351 #if ( configGENERATE_RUN_TIME_STATS == 1 )
352 configRUN_TIME_COUNTER_TYPE ulRunTimeCounter; /**< Stores the amount of time the task has spent in the Running state. */
355 #if ( configUSE_C_RUNTIME_TLS_SUPPORT == 1 )
356 configTLS_BLOCK_TYPE xTLSBlock; /**< Memory block used as Thread Local Storage (TLS) Block for the task. */
359 #if ( configUSE_TASK_NOTIFICATIONS == 1 )
360 volatile uint32_t ulNotifiedValue[ configTASK_NOTIFICATION_ARRAY_ENTRIES ];
361 volatile uint8_t ucNotifyState[ configTASK_NOTIFICATION_ARRAY_ENTRIES ];
364 /* See the comments in FreeRTOS.h with the definition of
365 * tskSTATIC_AND_DYNAMIC_ALLOCATION_POSSIBLE. */
366 #if ( tskSTATIC_AND_DYNAMIC_ALLOCATION_POSSIBLE != 0 ) /*lint !e731 !e9029 Macro has been consolidated for readability reasons. */
367 uint8_t ucStaticallyAllocated; /**< Set to pdTRUE if the task is a statically allocated to ensure no attempt is made to free the memory. */
370 #if ( INCLUDE_xTaskAbortDelay == 1 )
371 uint8_t ucDelayAborted;
374 #if ( configUSE_POSIX_ERRNO == 1 )
379 /* The old tskTCB name is maintained above then typedefed to the new TCB_t name
380 * below to enable the use of older kernel aware debuggers. */
381 typedef tskTCB TCB_t;
383 /*lint -save -e956 A manual analysis and inspection has been used to determine
384 * which static variables must be declared volatile. */
385 #if ( configNUMBER_OF_CORES == 1 )
386 portDONT_DISCARD PRIVILEGED_DATA TCB_t * volatile pxCurrentTCB = NULL;
388 /* MISRA Ref 8.4.1 [Declaration shall be visible] */
389 /* More details at: https://github.com/FreeRTOS/FreeRTOS-Kernel/blob/main/MISRA.md#rule-84 */
390 /* coverity[misra_c_2012_rule_8_4_violation] */
391 portDONT_DISCARD PRIVILEGED_DATA TCB_t * volatile pxCurrentTCBs[ configNUMBER_OF_CORES ];
392 #define pxCurrentTCB xTaskGetCurrentTaskHandle()
395 /* Lists for ready and blocked tasks. --------------------
396 * xDelayedTaskList1 and xDelayedTaskList2 could be moved to function scope but
397 * doing so breaks some kernel aware debuggers and debuggers that rely on removing
398 * the static qualifier. */
399 PRIVILEGED_DATA static List_t pxReadyTasksLists[ configMAX_PRIORITIES ]; /**< Prioritised ready tasks. */
400 PRIVILEGED_DATA static List_t xDelayedTaskList1; /**< Delayed tasks. */
401 PRIVILEGED_DATA static List_t xDelayedTaskList2; /**< Delayed tasks (two lists are used - one for delays that have overflowed the current tick count. */
402 PRIVILEGED_DATA static List_t * volatile pxDelayedTaskList; /**< Points to the delayed task list currently being used. */
403 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. */
404 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. */
406 #if ( INCLUDE_vTaskDelete == 1 )
408 PRIVILEGED_DATA static List_t xTasksWaitingTermination; /**< Tasks that have been deleted - but their memory not yet freed. */
409 PRIVILEGED_DATA static volatile UBaseType_t uxDeletedTasksWaitingCleanUp = ( UBaseType_t ) 0U;
413 #if ( INCLUDE_vTaskSuspend == 1 )
415 PRIVILEGED_DATA static List_t xSuspendedTaskList; /**< Tasks that are currently suspended. */
419 /* Global POSIX errno. Its value is changed upon context switching to match
420 * the errno of the currently running task. */
421 #if ( configUSE_POSIX_ERRNO == 1 )
422 int FreeRTOS_errno = 0;
425 /* Other file private variables. --------------------------------*/
426 PRIVILEGED_DATA static volatile UBaseType_t uxCurrentNumberOfTasks = ( UBaseType_t ) 0U;
427 PRIVILEGED_DATA static volatile TickType_t xTickCount = ( TickType_t ) configINITIAL_TICK_COUNT;
428 PRIVILEGED_DATA static volatile UBaseType_t uxTopReadyPriority = tskIDLE_PRIORITY;
429 PRIVILEGED_DATA static volatile BaseType_t xSchedulerRunning = pdFALSE;
430 PRIVILEGED_DATA static volatile TickType_t xPendedTicks = ( TickType_t ) 0U;
431 PRIVILEGED_DATA static volatile BaseType_t xYieldPendings[ configNUMBER_OF_CORES ] = { pdFALSE };
432 PRIVILEGED_DATA static volatile BaseType_t xNumOfOverflows = ( BaseType_t ) 0;
433 PRIVILEGED_DATA static UBaseType_t uxTaskNumber = ( UBaseType_t ) 0U;
434 PRIVILEGED_DATA static volatile TickType_t xNextTaskUnblockTime = ( TickType_t ) 0U; /* Initialised to portMAX_DELAY before the scheduler starts. */
435 PRIVILEGED_DATA static TaskHandle_t xIdleTaskHandles[ configNUMBER_OF_CORES ]; /**< Holds the handles of the idle tasks. The idle tasks are created automatically when the scheduler is started. */
437 /* Improve support for OpenOCD. The kernel tracks Ready tasks via priority lists.
438 * For tracking the state of remote threads, OpenOCD uses uxTopUsedPriority
439 * to determine the number of priority lists to read back from the remote target. */
440 const volatile UBaseType_t uxTopUsedPriority = configMAX_PRIORITIES - 1U;
442 /* Context switches are held pending while the scheduler is suspended. Also,
443 * interrupts must not manipulate the xStateListItem of a TCB, or any of the
444 * lists the xStateListItem can be referenced from, if the scheduler is suspended.
445 * If an interrupt needs to unblock a task while the scheduler is suspended then it
446 * moves the task's event list item into the xPendingReadyList, ready for the
447 * kernel to move the task from the pending ready list into the real ready list
448 * when the scheduler is unsuspended. The pending ready list itself can only be
449 * accessed from a critical section.
451 * Updates to uxSchedulerSuspended must be protected by both the task lock and the ISR lock
452 * and must not be done from an ISR. Reads must be protected by either lock and may be done
453 * from either an ISR or a task. */
454 PRIVILEGED_DATA static volatile UBaseType_t uxSchedulerSuspended = ( UBaseType_t ) 0U;
456 #if ( configGENERATE_RUN_TIME_STATS == 1 )
458 /* Do not move these variables to function scope as doing so prevents the
459 * code working with debuggers that need to remove the static qualifier. */
460 PRIVILEGED_DATA static configRUN_TIME_COUNTER_TYPE ulTaskSwitchedInTime[ configNUMBER_OF_CORES ] = { 0U }; /**< Holds the value of a timer/counter the last time a task was switched in. */
461 PRIVILEGED_DATA static volatile configRUN_TIME_COUNTER_TYPE ulTotalRunTime[ configNUMBER_OF_CORES ] = { 0U }; /**< Holds the total amount of execution time as defined by the run time counter clock. */
465 #if ( configSUPPORT_STATIC_ALLOCATION == 1 ) && ( configNUMBER_OF_CORES > 1 )
467 /* Do not move these variables to function scope as doing so prevents the
468 * code working with debuggers that need to remove the static qualifier. */
469 static StaticTask_t xIdleTCBBuffers[ configNUMBER_OF_CORES - 1 ];
470 static StackType_t xIdleTaskStackBuffers[ configNUMBER_OF_CORES - 1 ][ configMINIMAL_STACK_SIZE ];
472 #endif /* #if ( configSUPPORT_STATIC_ALLOCATION == 1 ) && ( configNUMBER_OF_CORES > 1 ) */
476 /*-----------------------------------------------------------*/
478 /* File private functions. --------------------------------*/
481 * Creates the idle tasks during scheduler start.
483 static BaseType_t prvCreateIdleTasks( void );
485 #if ( configNUMBER_OF_CORES > 1 )
488 * Checks to see if another task moved the current task out of the ready
489 * list while it was waiting to enter a critical section and yields, if so.
491 static void prvCheckForRunStateChange( void );
492 #endif /* #if ( configNUMBER_OF_CORES > 1 ) */
494 #if ( configNUMBER_OF_CORES > 1 )
497 * Yields the given core.
499 static void prvYieldCore( BaseType_t xCoreID );
500 #endif /* #if ( configNUMBER_OF_CORES > 1 ) */
502 #if ( configNUMBER_OF_CORES > 1 )
505 * Yields a core, or cores if multiple priorities are not allowed to run
506 * simultaneously, to allow the task pxTCB to run.
508 static void prvYieldForTask( const TCB_t * pxTCB );
509 #endif /* #if ( configNUMBER_OF_CORES > 1 ) */
511 #if ( configNUMBER_OF_CORES > 1 )
514 * Selects the highest priority available task for the given core.
516 static void prvSelectHighestPriorityTask( BaseType_t xCoreID );
517 #endif /* #if ( configNUMBER_OF_CORES > 1 ) */
520 * Utility task that simply returns pdTRUE if the task referenced by xTask is
521 * currently in the Suspended state, or pdFALSE if the task referenced by xTask
522 * is in any other state.
524 #if ( INCLUDE_vTaskSuspend == 1 )
526 static BaseType_t prvTaskIsTaskSuspended( const TaskHandle_t xTask ) PRIVILEGED_FUNCTION;
528 #endif /* INCLUDE_vTaskSuspend */
531 * Utility to ready all the lists used by the scheduler. This is called
532 * automatically upon the creation of the first task.
534 static void prvInitialiseTaskLists( void ) PRIVILEGED_FUNCTION;
537 * The idle task, which as all tasks is implemented as a never ending loop.
538 * The idle task is automatically created and added to the ready lists upon
539 * creation of the first user task.
541 * In the FreeRTOS SMP, configNUMBER_OF_CORES - 1 minimal idle tasks are also
542 * created to ensure that each core has an idle task to run when no other
543 * task is available to run.
545 * The portTASK_FUNCTION_PROTO() macro is used to allow port/compiler specific
546 * language extensions. The equivalent prototype for these functions are:
548 * void prvIdleTask( void *pvParameters );
549 * void prvMinimalIdleTask( void *pvParameters );
552 static portTASK_FUNCTION_PROTO( prvIdleTask, pvParameters ) PRIVILEGED_FUNCTION;
553 #if ( configNUMBER_OF_CORES > 1 )
554 static portTASK_FUNCTION_PROTO( prvMinimalIdleTask, pvParameters ) PRIVILEGED_FUNCTION;
558 * Utility to free all memory allocated by the scheduler to hold a TCB,
559 * including the stack pointed to by the TCB.
561 * This does not free memory allocated by the task itself (i.e. memory
562 * allocated by calls to pvPortMalloc from within the tasks application code).
564 #if ( INCLUDE_vTaskDelete == 1 )
566 static void prvDeleteTCB( TCB_t * pxTCB ) PRIVILEGED_FUNCTION;
571 * Used only by the idle task. This checks to see if anything has been placed
572 * in the list of tasks waiting to be deleted. If so the task is cleaned up
573 * and its TCB deleted.
575 static void prvCheckTasksWaitingTermination( void ) PRIVILEGED_FUNCTION;
578 * The currently executing task is entering the Blocked state. Add the task to
579 * either the current or the overflow delayed task list.
581 static void prvAddCurrentTaskToDelayedList( TickType_t xTicksToWait,
582 const BaseType_t xCanBlockIndefinitely ) PRIVILEGED_FUNCTION;
585 * Fills an TaskStatus_t structure with information on each task that is
586 * referenced from the pxList list (which may be a ready list, a delayed list,
587 * a suspended list, etc.).
589 * THIS FUNCTION IS INTENDED FOR DEBUGGING ONLY, AND SHOULD NOT BE CALLED FROM
590 * NORMAL APPLICATION CODE.
592 #if ( configUSE_TRACE_FACILITY == 1 )
594 static UBaseType_t prvListTasksWithinSingleList( TaskStatus_t * pxTaskStatusArray,
596 eTaskState eState ) PRIVILEGED_FUNCTION;
601 * Searches pxList for a task with name pcNameToQuery - returning a handle to
602 * the task if it is found, or NULL if the task is not found.
604 #if ( INCLUDE_xTaskGetHandle == 1 )
606 static TCB_t * prvSearchForNameWithinSingleList( List_t * pxList,
607 const char pcNameToQuery[] ) PRIVILEGED_FUNCTION;
612 * When a task is created, the stack of the task is filled with a known value.
613 * This function determines the 'high water mark' of the task stack by
614 * determining how much of the stack remains at the original preset value.
616 #if ( ( configUSE_TRACE_FACILITY == 1 ) || ( INCLUDE_uxTaskGetStackHighWaterMark == 1 ) || ( INCLUDE_uxTaskGetStackHighWaterMark2 == 1 ) )
618 static configSTACK_DEPTH_TYPE prvTaskCheckFreeStackSpace( const uint8_t * pucStackByte ) PRIVILEGED_FUNCTION;
623 * Return the amount of time, in ticks, that will pass before the kernel will
624 * next move a task from the Blocked state to the Running state.
626 * This conditional compilation should use inequality to 0, not equality to 1.
627 * This is to ensure portSUPPRESS_TICKS_AND_SLEEP() can be called when user
628 * defined low power mode implementations require configUSE_TICKLESS_IDLE to be
629 * set to a value other than 1.
631 #if ( configUSE_TICKLESS_IDLE != 0 )
633 static TickType_t prvGetExpectedIdleTime( void ) PRIVILEGED_FUNCTION;
638 * Set xNextTaskUnblockTime to the time at which the next Blocked state task
639 * will exit the Blocked state.
641 static void prvResetNextTaskUnblockTime( void ) PRIVILEGED_FUNCTION;
643 #if ( configUSE_STATS_FORMATTING_FUNCTIONS > 0 )
646 * Helper function used to pad task names with spaces when printing out
647 * human readable tables of task information.
649 static char * prvWriteNameToBuffer( char * pcBuffer,
650 const char * pcTaskName ) PRIVILEGED_FUNCTION;
655 * Called after a Task_t structure has been allocated either statically or
656 * dynamically to fill in the structure's members.
658 static void prvInitialiseNewTask( TaskFunction_t pxTaskCode,
659 const char * const pcName, /*lint !e971 Unqualified char types are allowed for strings and single characters only. */
660 const uint32_t ulStackDepth,
661 void * const pvParameters,
662 UBaseType_t uxPriority,
663 TaskHandle_t * const pxCreatedTask,
665 const MemoryRegion_t * const xRegions ) PRIVILEGED_FUNCTION;
668 * Called after a new task has been created and initialised to place the task
669 * under the control of the scheduler.
671 static void prvAddNewTaskToReadyList( TCB_t * pxNewTCB ) PRIVILEGED_FUNCTION;
674 * freertos_tasks_c_additions_init() should only be called if the user definable
675 * macro FREERTOS_TASKS_C_ADDITIONS_INIT() is defined, as that is the only macro
676 * called by the function.
678 #ifdef FREERTOS_TASKS_C_ADDITIONS_INIT
680 static void freertos_tasks_c_additions_init( void ) PRIVILEGED_FUNCTION;
684 #if ( configUSE_MINIMAL_IDLE_HOOK == 1 )
685 extern void vApplicationMinimalIdleHook( void );
686 #endif /* #if ( configUSE_MINIMAL_IDLE_HOOK == 1 ) */
688 /*-----------------------------------------------------------*/
690 #if ( configNUMBER_OF_CORES > 1 )
691 static void prvCheckForRunStateChange( void )
693 UBaseType_t uxPrevCriticalNesting;
694 const TCB_t * pxThisTCB;
696 /* This should be skipped if called from an ISR. If the task on the current
697 * core is no longer running, then vTaskSwitchContext() probably should
698 * be run before returning, but we don't have a way to force that to happen
700 if( portCHECK_IF_IN_ISR() == pdFALSE )
702 /* This function is always called with interrupts disabled
703 * so this is safe. */
704 pxThisTCB = pxCurrentTCBs[ portGET_CORE_ID() ];
706 while( pxThisTCB->xTaskRunState == taskTASK_YIELDING )
708 /* We are only here if we just entered a critical section
709 * or if we just suspended the scheduler, and another task
710 * has requested that we yield.
712 * This is slightly complicated since we need to save and restore
713 * the suspension and critical nesting counts, as well as release
714 * and reacquire the correct locks. And then, do it all over again
715 * if our state changed again during the reacquisition. */
716 uxPrevCriticalNesting = portGET_CRITICAL_NESTING_COUNT();
718 if( uxPrevCriticalNesting > 0U )
720 portSET_CRITICAL_NESTING_COUNT( 0U );
721 portRELEASE_ISR_LOCK();
725 /* The scheduler is suspended. uxSchedulerSuspended is updated
726 * only when the task is not requested to yield. */
727 mtCOVERAGE_TEST_MARKER();
730 portRELEASE_TASK_LOCK();
732 portMEMORY_BARRIER();
733 configASSERT( pxThisTCB->xTaskRunState == taskTASK_YIELDING );
735 portENABLE_INTERRUPTS();
737 /* Enabling interrupts should cause this core to immediately
738 * service the pending interrupt and yield. If the run state is still
739 * yielding here then that is a problem. */
740 configASSERT( pxThisTCB->xTaskRunState != taskTASK_YIELDING );
742 portDISABLE_INTERRUPTS();
746 portSET_CRITICAL_NESTING_COUNT( uxPrevCriticalNesting );
748 if( uxPrevCriticalNesting == 0U )
750 portRELEASE_ISR_LOCK();
755 #endif /* #if ( configNUMBER_OF_CORES > 1 ) */
757 /*-----------------------------------------------------------*/
759 #if ( configNUMBER_OF_CORES > 1 )
760 static void prvYieldCore( BaseType_t xCoreID )
762 /* This must be called from a critical section and xCoreID must be valid. */
763 if( ( portCHECK_IF_IN_ISR() == pdTRUE ) && ( xCoreID == ( BaseType_t ) portGET_CORE_ID() ) )
765 xYieldPendings[ xCoreID ] = pdTRUE;
769 if( pxCurrentTCBs[ xCoreID ]->xTaskRunState != taskTASK_YIELDING )
771 if( xCoreID == ( BaseType_t ) portGET_CORE_ID() )
773 xYieldPendings[ xCoreID ] = pdTRUE;
777 portYIELD_CORE( xCoreID );
778 pxCurrentTCBs[ xCoreID ]->xTaskRunState = taskTASK_YIELDING;
783 #endif /* #if ( configNUMBER_OF_CORES > 1 ) */
784 /*-----------------------------------------------------------*/
786 #if ( configNUMBER_OF_CORES > 1 )
787 static void prvYieldForTask( const TCB_t * pxTCB )
789 BaseType_t xLowestPriorityToPreempt;
790 BaseType_t xCurrentCoreTaskPriority;
791 BaseType_t xLowestPriorityCore = ( BaseType_t ) -1;
794 #if ( configRUN_MULTIPLE_PRIORITIES == 0 )
795 BaseType_t xYieldCount = 0;
796 #endif /* #if ( configRUN_MULTIPLE_PRIORITIES == 0 ) */
798 /* This must be called from a critical section. */
799 configASSERT( portGET_CRITICAL_NESTING_COUNT() > 0U );
801 #if ( configRUN_MULTIPLE_PRIORITIES == 0 )
803 /* No task should yield for this one if it is a lower priority
804 * than priority level of currently ready tasks. */
805 if( pxTCB->uxPriority >= uxTopReadyPriority )
807 /* Yield is not required for a task which is already running. */
808 if( taskTASK_IS_RUNNING( pxTCB ) == pdFALSE )
811 xLowestPriorityToPreempt = ( BaseType_t ) pxTCB->uxPriority;
813 /* xLowestPriorityToPreempt will be decremented to -1 if the priority of pxTCB
814 * is 0. This is ok as we will give system idle tasks a priority of -1 below. */
815 --xLowestPriorityToPreempt;
817 for( xCoreID = ( BaseType_t ) 0; xCoreID < ( BaseType_t ) configNUMBER_OF_CORES; xCoreID++ )
819 xCurrentCoreTaskPriority = ( BaseType_t ) pxCurrentTCBs[ xCoreID ]->uxPriority;
821 /* System idle tasks are being assigned a priority of tskIDLE_PRIORITY - 1 here. */
822 if( ( pxCurrentTCBs[ xCoreID ]->uxTaskAttributes & taskATTRIBUTE_IS_IDLE ) != 0U )
824 xCurrentCoreTaskPriority = xCurrentCoreTaskPriority - 1;
827 if( ( taskTASK_IS_RUNNING( pxCurrentTCBs[ xCoreID ] ) != pdFALSE ) && ( xYieldPendings[ xCoreID ] == pdFALSE ) )
829 #if ( configRUN_MULTIPLE_PRIORITIES == 0 )
830 if( taskTASK_IS_RUNNING( pxTCB ) == pdFALSE )
833 if( xCurrentCoreTaskPriority <= xLowestPriorityToPreempt )
835 #if ( configUSE_CORE_AFFINITY == 1 )
836 if( ( pxTCB->uxCoreAffinityMask & ( ( UBaseType_t ) 1U << ( UBaseType_t ) xCoreID ) ) != 0U )
839 #if ( configUSE_TASK_PREEMPTION_DISABLE == 1 )
840 if( pxCurrentTCBs[ xCoreID ]->xPreemptionDisable == pdFALSE )
843 xLowestPriorityToPreempt = xCurrentCoreTaskPriority;
844 xLowestPriorityCore = xCoreID;
850 mtCOVERAGE_TEST_MARKER();
854 #if ( configRUN_MULTIPLE_PRIORITIES == 0 )
856 /* Yield all currently running non-idle tasks with a priority lower than
857 * the task that needs to run. */
858 if( ( xCurrentCoreTaskPriority > ( ( BaseType_t ) tskIDLE_PRIORITY - 1 ) ) &&
859 ( xCurrentCoreTaskPriority < ( BaseType_t ) pxTCB->uxPriority ) )
861 prvYieldCore( xCoreID );
866 mtCOVERAGE_TEST_MARKER();
869 #endif /* #if ( configRUN_MULTIPLE_PRIORITIES == 0 ) */
873 mtCOVERAGE_TEST_MARKER();
877 #if ( configRUN_MULTIPLE_PRIORITIES == 0 )
878 if( ( xYieldCount == 0 ) && ( xLowestPriorityCore >= 0 ) )
879 #else /* #if ( configRUN_MULTIPLE_PRIORITIES == 0 ) */
880 if( xLowestPriorityCore >= 0 )
881 #endif /* #if ( configRUN_MULTIPLE_PRIORITIES == 0 ) */
883 prvYieldCore( xLowestPriorityCore );
886 #if ( configRUN_MULTIPLE_PRIORITIES == 0 )
887 /* Verify that the calling core always yields to higher priority tasks. */
888 if( ( ( pxCurrentTCBs[ portGET_CORE_ID() ]->uxTaskAttributes & taskATTRIBUTE_IS_IDLE ) == 0 ) &&
889 ( pxTCB->uxPriority > pxCurrentTCBs[ portGET_CORE_ID() ]->uxPriority ) )
891 configASSERT( ( xYieldPendings[ portGET_CORE_ID() ] == pdTRUE ) ||
892 ( taskTASK_IS_RUNNING( pxCurrentTCBs[ portGET_CORE_ID() ] ) == pdFALSE ) );
897 #endif /* #if ( configNUMBER_OF_CORES > 1 ) */
898 /*-----------------------------------------------------------*/
900 #if ( configNUMBER_OF_CORES > 1 )
901 static void prvSelectHighestPriorityTask( BaseType_t xCoreID )
903 UBaseType_t uxCurrentPriority = uxTopReadyPriority;
904 BaseType_t xTaskScheduled = pdFALSE;
905 BaseType_t xDecrementTopPriority = pdTRUE;
907 #if ( configUSE_CORE_AFFINITY == 1 )
908 const TCB_t * pxPreviousTCB = NULL;
910 #if ( configRUN_MULTIPLE_PRIORITIES == 0 )
911 BaseType_t xPriorityDropped = pdFALSE;
914 /* This function should be called when scheduler is running. */
915 configASSERT( xSchedulerRunning == pdTRUE );
917 /* A new task is created and a running task with the same priority yields
918 * itself to run the new task. When a running task yields itself, it is still
919 * in the ready list. This running task will be selected before the new task
920 * since the new task is always added to the end of the ready list.
921 * The other problem is that the running task still in the same position of
922 * the ready list when it yields itself. It is possible that it will be selected
923 * earlier then other tasks which waits longer than this task.
925 * To fix these problems, the running task should be put to the end of the
926 * ready list before searching for the ready task in the ready list. */
927 if( listIS_CONTAINED_WITHIN( &( pxReadyTasksLists[ pxCurrentTCBs[ xCoreID ]->uxPriority ] ),
928 &pxCurrentTCBs[ xCoreID ]->xStateListItem ) == pdTRUE )
930 ( void ) uxListRemove( &pxCurrentTCBs[ xCoreID ]->xStateListItem );
931 vListInsertEnd( &( pxReadyTasksLists[ pxCurrentTCBs[ xCoreID ]->uxPriority ] ),
932 &pxCurrentTCBs[ xCoreID ]->xStateListItem );
935 while( xTaskScheduled == pdFALSE )
937 #if ( configRUN_MULTIPLE_PRIORITIES == 0 )
939 if( uxCurrentPriority < uxTopReadyPriority )
941 /* We can't schedule any tasks, other than idle, that have a
942 * priority lower than the priority of a task currently running
943 * on another core. */
944 uxCurrentPriority = tskIDLE_PRIORITY;
949 if( listLIST_IS_EMPTY( &( pxReadyTasksLists[ uxCurrentPriority ] ) ) == pdFALSE )
951 const List_t * const pxReadyList = &( pxReadyTasksLists[ uxCurrentPriority ] );
952 const ListItem_t * pxEndMarker = listGET_END_MARKER( pxReadyList );
953 ListItem_t * pxIterator;
955 /* The ready task list for uxCurrentPriority is not empty, so uxTopReadyPriority
956 * must not be decremented any further. */
957 xDecrementTopPriority = pdFALSE;
959 for( pxIterator = listGET_HEAD_ENTRY( pxReadyList ); pxIterator != pxEndMarker; pxIterator = listGET_NEXT( pxIterator ) )
961 TCB_t * pxTCB = ( TCB_t * ) listGET_LIST_ITEM_OWNER( pxIterator );
963 #if ( configRUN_MULTIPLE_PRIORITIES == 0 )
965 /* When falling back to the idle priority because only one priority
966 * level is allowed to run at a time, we should ONLY schedule the true
967 * idle tasks, not user tasks at the idle priority. */
968 if( uxCurrentPriority < uxTopReadyPriority )
970 if( ( pxTCB->uxTaskAttributes & taskATTRIBUTE_IS_IDLE ) == 0 )
976 #endif /* #if ( configRUN_MULTIPLE_PRIORITIES == 0 ) */
978 if( pxTCB->xTaskRunState == taskTASK_NOT_RUNNING )
980 #if ( configUSE_CORE_AFFINITY == 1 )
981 if( ( pxTCB->uxCoreAffinityMask & ( ( UBaseType_t ) 1U << ( UBaseType_t ) xCoreID ) ) != 0U )
984 /* If the task is not being executed by any core swap it in. */
985 pxCurrentTCBs[ xCoreID ]->xTaskRunState = taskTASK_NOT_RUNNING;
986 #if ( configUSE_CORE_AFFINITY == 1 )
987 pxPreviousTCB = pxCurrentTCBs[ xCoreID ];
989 pxTCB->xTaskRunState = ( TaskRunning_t ) xCoreID;
990 pxCurrentTCBs[ xCoreID ] = pxTCB;
991 xTaskScheduled = pdTRUE;
994 else if( pxTCB == pxCurrentTCBs[ xCoreID ] )
996 configASSERT( ( pxTCB->xTaskRunState == xCoreID ) || ( pxTCB->xTaskRunState == taskTASK_YIELDING ) );
998 #if ( configUSE_CORE_AFFINITY == 1 )
999 if( ( pxTCB->uxCoreAffinityMask & ( ( UBaseType_t ) 1U << ( UBaseType_t ) xCoreID ) ) != 0U )
1002 /* The task is already running on this core, mark it as scheduled. */
1003 pxTCB->xTaskRunState = ( TaskRunning_t ) xCoreID;
1004 xTaskScheduled = pdTRUE;
1009 /* This task is running on the core other than xCoreID. */
1010 mtCOVERAGE_TEST_MARKER();
1013 if( xTaskScheduled != pdFALSE )
1015 /* A task has been selected to run on this core. */
1022 if( xDecrementTopPriority != pdFALSE )
1024 uxTopReadyPriority--;
1025 #if ( configRUN_MULTIPLE_PRIORITIES == 0 )
1027 xPriorityDropped = pdTRUE;
1033 /* There are configNUMBER_OF_CORES Idle tasks created when scheduler started.
1034 * The scheduler should be able to select a task to run when uxCurrentPriority
1035 * is tskIDLE_PRIORITY. uxCurrentPriority is never decreased to value blow
1036 * tskIDLE_PRIORITY. */
1037 if( uxCurrentPriority > tskIDLE_PRIORITY )
1039 uxCurrentPriority--;
1043 /* This function is called when idle task is not created. Break the
1044 * loop to prevent uxCurrentPriority overrun. */
1049 #if ( configRUN_MULTIPLE_PRIORITIES == 0 )
1051 if( xTaskScheduled == pdTRUE )
1053 if( xPriorityDropped != pdFALSE )
1055 /* There may be several ready tasks that were being prevented from running because there was
1056 * a higher priority task running. Now that the last of the higher priority tasks is no longer
1057 * running, make sure all the other idle tasks yield. */
1060 for( x = ( BaseType_t ) 0; x < ( BaseType_t ) configNUMBER_OF_CORES; x++ )
1062 if( ( pxCurrentTCBs[ x ]->uxTaskAttributes & taskATTRIBUTE_IS_IDLE ) != 0 )
1070 #endif /* #if ( configRUN_MULTIPLE_PRIORITIES == 0 ) */
1072 #if ( configUSE_CORE_AFFINITY == 1 )
1074 if( xTaskScheduled == pdTRUE )
1076 if( ( pxPreviousTCB != NULL ) && ( listIS_CONTAINED_WITHIN( &( pxReadyTasksLists[ pxPreviousTCB->uxPriority ] ), &( pxPreviousTCB->xStateListItem ) ) != pdFALSE ) )
1078 /* A ready task was just evicted from this core. See if it can be
1079 * scheduled on any other core. */
1080 UBaseType_t uxCoreMap = pxPreviousTCB->uxCoreAffinityMask;
1081 BaseType_t xLowestPriority = ( BaseType_t ) pxPreviousTCB->uxPriority;
1082 BaseType_t xLowestPriorityCore = -1;
1085 if( ( pxPreviousTCB->uxTaskAttributes & taskATTRIBUTE_IS_IDLE ) != 0U )
1087 xLowestPriority = xLowestPriority - 1;
1090 if( ( uxCoreMap & ( ( UBaseType_t ) 1U << ( UBaseType_t ) xCoreID ) ) != 0U )
1092 /* The ready task that was removed from this core is not excluded from it.
1093 * Only look at the intersection of the cores the removed task is allowed to run
1094 * on with the cores that the new task is excluded from. It is possible that the
1095 * new task was only placed onto this core because it is excluded from another.
1096 * Check to see if the previous task could run on one of those cores. */
1097 uxCoreMap &= ~( pxCurrentTCBs[ xCoreID ]->uxCoreAffinityMask );
1101 /* The ready task that was removed from this core is excluded from it. */
1104 uxCoreMap &= ( ( 1U << configNUMBER_OF_CORES ) - 1U );
1106 for( x = ( ( BaseType_t ) configNUMBER_OF_CORES - 1 ); x >= ( BaseType_t ) 0; x-- )
1108 UBaseType_t uxCore = ( UBaseType_t ) x;
1109 BaseType_t xTaskPriority;
1111 if( ( uxCoreMap & ( ( UBaseType_t ) 1U << uxCore ) ) != 0U )
1113 xTaskPriority = ( BaseType_t ) pxCurrentTCBs[ uxCore ]->uxPriority;
1115 if( ( pxCurrentTCBs[ uxCore ]->uxTaskAttributes & taskATTRIBUTE_IS_IDLE ) != 0U )
1117 xTaskPriority = xTaskPriority - ( BaseType_t ) 1;
1120 uxCoreMap &= ~( ( UBaseType_t ) 1U << uxCore );
1122 if( ( xTaskPriority < xLowestPriority ) &&
1123 ( taskTASK_IS_RUNNING( pxCurrentTCBs[ uxCore ] ) != pdFALSE ) &&
1124 ( xYieldPendings[ uxCore ] == pdFALSE ) )
1126 #if ( configUSE_TASK_PREEMPTION_DISABLE == 1 )
1127 if( pxCurrentTCBs[ uxCore ]->xPreemptionDisable == pdFALSE )
1130 xLowestPriority = xTaskPriority;
1131 xLowestPriorityCore = ( BaseType_t ) uxCore;
1137 if( xLowestPriorityCore >= 0 )
1139 prvYieldCore( xLowestPriorityCore );
1144 #endif /* #if ( configUSE_CORE_AFFINITY == 1 ) */
1147 #endif /* ( configNUMBER_OF_CORES > 1 ) */
1149 /*-----------------------------------------------------------*/
1151 #if ( configSUPPORT_STATIC_ALLOCATION == 1 )
1153 TaskHandle_t xTaskCreateStatic( TaskFunction_t pxTaskCode,
1154 const char * const pcName, /*lint !e971 Unqualified char types are allowed for strings and single characters only. */
1155 const uint32_t ulStackDepth,
1156 void * const pvParameters,
1157 UBaseType_t uxPriority,
1158 StackType_t * const puxStackBuffer,
1159 StaticTask_t * const pxTaskBuffer )
1160 #if ( ( configNUMBER_OF_CORES > 1 ) && ( configUSE_CORE_AFFINITY == 1 ) )
1162 return xTaskCreateStaticAffinitySet( pxTaskCode, pcName, ulStackDepth, pvParameters, uxPriority, puxStackBuffer, pxTaskBuffer, tskNO_AFFINITY );
1165 TaskHandle_t xTaskCreateStaticAffinitySet( TaskFunction_t pxTaskCode,
1166 const char * const pcName, /*lint !e971 Unqualified char types are allowed for strings and single characters only. */
1167 const uint32_t ulStackDepth,
1168 void * const pvParameters,
1169 UBaseType_t uxPriority,
1170 StackType_t * const puxStackBuffer,
1171 StaticTask_t * const pxTaskBuffer,
1172 UBaseType_t uxCoreAffinityMask )
1173 #endif /* #if ( ( configNUMBER_OF_CORES > 1 ) && ( configUSE_CORE_AFFINITY == 1 ) ) */
1176 TaskHandle_t xReturn;
1178 configASSERT( puxStackBuffer != NULL );
1179 configASSERT( pxTaskBuffer != NULL );
1181 #if ( configASSERT_DEFINED == 1 )
1183 /* Sanity check that the size of the structure used to declare a
1184 * variable of type StaticTask_t equals the size of the real task
1186 volatile size_t xSize = sizeof( StaticTask_t );
1187 configASSERT( xSize == sizeof( TCB_t ) );
1188 ( void ) xSize; /* Prevent lint warning when configASSERT() is not used. */
1190 #endif /* configASSERT_DEFINED */
1192 if( ( pxTaskBuffer != NULL ) && ( puxStackBuffer != NULL ) )
1194 /* The memory used for the task's TCB and stack are passed into this
1195 * function - use them. */
1196 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. */
1197 ( void ) memset( ( void * ) pxNewTCB, 0x00, sizeof( TCB_t ) );
1198 pxNewTCB->pxStack = ( StackType_t * ) puxStackBuffer;
1200 #if ( tskSTATIC_AND_DYNAMIC_ALLOCATION_POSSIBLE != 0 ) /*lint !e731 !e9029 Macro has been consolidated for readability reasons. */
1202 /* Tasks can be created statically or dynamically, so note this
1203 * task was created statically in case the task is later deleted. */
1204 pxNewTCB->ucStaticallyAllocated = tskSTATICALLY_ALLOCATED_STACK_AND_TCB;
1206 #endif /* tskSTATIC_AND_DYNAMIC_ALLOCATION_POSSIBLE */
1208 prvInitialiseNewTask( pxTaskCode, pcName, ulStackDepth, pvParameters, uxPriority, &xReturn, pxNewTCB, NULL );
1210 #if ( ( configNUMBER_OF_CORES > 1 ) && ( configUSE_CORE_AFFINITY == 1 ) )
1212 /* Set the task's affinity before scheduling it. */
1213 pxNewTCB->uxCoreAffinityMask = uxCoreAffinityMask;
1217 prvAddNewTaskToReadyList( pxNewTCB );
1227 #endif /* SUPPORT_STATIC_ALLOCATION */
1228 /*-----------------------------------------------------------*/
1230 #if ( ( portUSING_MPU_WRAPPERS == 1 ) && ( configSUPPORT_STATIC_ALLOCATION == 1 ) )
1232 BaseType_t xTaskCreateRestrictedStatic( const TaskParameters_t * const pxTaskDefinition,
1233 TaskHandle_t * pxCreatedTask )
1234 #if ( ( configNUMBER_OF_CORES > 1 ) && ( configUSE_CORE_AFFINITY == 1 ) )
1236 return xTaskCreateRestrictedStaticAffinitySet( pxTaskDefinition, tskNO_AFFINITY, pxCreatedTask );
1239 BaseType_t xTaskCreateRestrictedStaticAffinitySet( const TaskParameters_t * const pxTaskDefinition,
1240 UBaseType_t uxCoreAffinityMask,
1241 TaskHandle_t * pxCreatedTask )
1242 #endif /* #if ( ( configNUMBER_OF_CORES > 1 ) && ( configUSE_CORE_AFFINITY == 1 ) ) */
1245 BaseType_t xReturn = errCOULD_NOT_ALLOCATE_REQUIRED_MEMORY;
1247 configASSERT( pxTaskDefinition->puxStackBuffer != NULL );
1248 configASSERT( pxTaskDefinition->pxTaskBuffer != NULL );
1250 if( ( pxTaskDefinition->puxStackBuffer != NULL ) && ( pxTaskDefinition->pxTaskBuffer != NULL ) )
1252 /* Allocate space for the TCB. Where the memory comes from depends
1253 * on the implementation of the port malloc function and whether or
1254 * not static allocation is being used. */
1255 pxNewTCB = ( TCB_t * ) pxTaskDefinition->pxTaskBuffer;
1256 ( void ) memset( ( void * ) pxNewTCB, 0x00, sizeof( TCB_t ) );
1258 /* Store the stack location in the TCB. */
1259 pxNewTCB->pxStack = pxTaskDefinition->puxStackBuffer;
1261 #if ( tskSTATIC_AND_DYNAMIC_ALLOCATION_POSSIBLE != 0 )
1263 /* Tasks can be created statically or dynamically, so note this
1264 * task was created statically in case the task is later deleted. */
1265 pxNewTCB->ucStaticallyAllocated = tskSTATICALLY_ALLOCATED_STACK_AND_TCB;
1267 #endif /* tskSTATIC_AND_DYNAMIC_ALLOCATION_POSSIBLE */
1269 prvInitialiseNewTask( pxTaskDefinition->pvTaskCode,
1270 pxTaskDefinition->pcName,
1271 ( uint32_t ) pxTaskDefinition->usStackDepth,
1272 pxTaskDefinition->pvParameters,
1273 pxTaskDefinition->uxPriority,
1274 pxCreatedTask, pxNewTCB,
1275 pxTaskDefinition->xRegions );
1277 #if ( ( configNUMBER_OF_CORES > 1 ) && ( configUSE_CORE_AFFINITY == 1 ) )
1279 /* Set the task's affinity before scheduling it. */
1280 pxNewTCB->uxCoreAffinityMask = uxCoreAffinityMask;
1284 prvAddNewTaskToReadyList( pxNewTCB );
1291 #endif /* ( portUSING_MPU_WRAPPERS == 1 ) && ( configSUPPORT_STATIC_ALLOCATION == 1 ) */
1292 /*-----------------------------------------------------------*/
1294 #if ( ( portUSING_MPU_WRAPPERS == 1 ) && ( configSUPPORT_DYNAMIC_ALLOCATION == 1 ) )
1296 BaseType_t xTaskCreateRestricted( const TaskParameters_t * const pxTaskDefinition,
1297 TaskHandle_t * pxCreatedTask )
1298 #if ( ( configNUMBER_OF_CORES > 1 ) && ( configUSE_CORE_AFFINITY == 1 ) )
1300 return xTaskCreateRestrictedAffinitySet( pxTaskDefinition, tskNO_AFFINITY, pxCreatedTask );
1303 BaseType_t xTaskCreateRestrictedAffinitySet( const TaskParameters_t * const pxTaskDefinition,
1304 UBaseType_t uxCoreAffinityMask,
1305 TaskHandle_t * pxCreatedTask )
1306 #endif /* #if ( ( configNUMBER_OF_CORES > 1 ) && ( configUSE_CORE_AFFINITY == 1 ) ) */
1309 BaseType_t xReturn = errCOULD_NOT_ALLOCATE_REQUIRED_MEMORY;
1311 configASSERT( pxTaskDefinition->puxStackBuffer );
1313 if( pxTaskDefinition->puxStackBuffer != NULL )
1315 pxNewTCB = ( TCB_t * ) pvPortMalloc( sizeof( TCB_t ) );
1317 if( pxNewTCB != NULL )
1319 ( void ) memset( ( void * ) pxNewTCB, 0x00, sizeof( TCB_t ) );
1321 /* Store the stack location in the TCB. */
1322 pxNewTCB->pxStack = pxTaskDefinition->puxStackBuffer;
1324 #if ( tskSTATIC_AND_DYNAMIC_ALLOCATION_POSSIBLE != 0 )
1326 /* Tasks can be created statically or dynamically, so note
1327 * this task had a statically allocated stack in case it is
1328 * later deleted. The TCB was allocated dynamically. */
1329 pxNewTCB->ucStaticallyAllocated = tskSTATICALLY_ALLOCATED_STACK_ONLY;
1331 #endif /* tskSTATIC_AND_DYNAMIC_ALLOCATION_POSSIBLE */
1333 prvInitialiseNewTask( pxTaskDefinition->pvTaskCode,
1334 pxTaskDefinition->pcName,
1335 ( uint32_t ) pxTaskDefinition->usStackDepth,
1336 pxTaskDefinition->pvParameters,
1337 pxTaskDefinition->uxPriority,
1338 pxCreatedTask, pxNewTCB,
1339 pxTaskDefinition->xRegions );
1341 #if ( ( configNUMBER_OF_CORES > 1 ) && ( configUSE_CORE_AFFINITY == 1 ) )
1343 /* Set the task's affinity before scheduling it. */
1344 pxNewTCB->uxCoreAffinityMask = uxCoreAffinityMask;
1348 prvAddNewTaskToReadyList( pxNewTCB );
1356 #endif /* portUSING_MPU_WRAPPERS */
1357 /*-----------------------------------------------------------*/
1359 #if ( configSUPPORT_DYNAMIC_ALLOCATION == 1 )
1361 BaseType_t xTaskCreate( TaskFunction_t pxTaskCode,
1362 const char * const pcName, /*lint !e971 Unqualified char types are allowed for strings and single characters only. */
1363 const configSTACK_DEPTH_TYPE usStackDepth,
1364 void * const pvParameters,
1365 UBaseType_t uxPriority,
1366 TaskHandle_t * const pxCreatedTask )
1367 #if ( ( configNUMBER_OF_CORES > 1 ) && ( configUSE_CORE_AFFINITY == 1 ) )
1369 return xTaskCreateAffinitySet( pxTaskCode, pcName, usStackDepth, pvParameters, uxPriority, tskNO_AFFINITY, pxCreatedTask );
1372 BaseType_t xTaskCreateAffinitySet( TaskFunction_t pxTaskCode,
1373 const char * const pcName, /*lint !e971 Unqualified char types are allowed for strings and single characters only. */
1374 const configSTACK_DEPTH_TYPE usStackDepth,
1375 void * const pvParameters,
1376 UBaseType_t uxPriority,
1377 UBaseType_t uxCoreAffinityMask,
1378 TaskHandle_t * const pxCreatedTask )
1379 #endif /* #if ( ( configNUMBER_OF_CORES > 1 ) && ( configUSE_CORE_AFFINITY == 1 ) ) */
1384 /* If the stack grows down then allocate the stack then the TCB so the stack
1385 * does not grow into the TCB. Likewise if the stack grows up then allocate
1386 * the TCB then the stack. */
1387 #if ( portSTACK_GROWTH > 0 )
1389 /* Allocate space for the TCB. Where the memory comes from depends on
1390 * the implementation of the port malloc function and whether or not static
1391 * allocation is being used. */
1392 pxNewTCB = ( TCB_t * ) pvPortMalloc( sizeof( TCB_t ) );
1394 if( pxNewTCB != NULL )
1396 ( void ) memset( ( void * ) pxNewTCB, 0x00, sizeof( TCB_t ) );
1398 /* Allocate space for the stack used by the task being created.
1399 * The base of the stack memory stored in the TCB so the task can
1400 * be deleted later if required. */
1401 pxNewTCB->pxStack = ( StackType_t * ) pvPortMallocStack( ( ( ( size_t ) usStackDepth ) * sizeof( StackType_t ) ) ); /*lint !e961 MISRA exception as the casts are only redundant for some ports. */
1403 if( pxNewTCB->pxStack == NULL )
1405 /* Could not allocate the stack. Delete the allocated TCB. */
1406 vPortFree( pxNewTCB );
1411 #else /* portSTACK_GROWTH */
1413 StackType_t * pxStack;
1415 /* Allocate space for the stack used by the task being created. */
1416 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. */
1418 if( pxStack != NULL )
1420 /* Allocate space for the TCB. */
1421 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. */
1423 if( pxNewTCB != NULL )
1425 ( void ) memset( ( void * ) pxNewTCB, 0x00, sizeof( TCB_t ) );
1427 /* Store the stack location in the TCB. */
1428 pxNewTCB->pxStack = pxStack;
1432 /* The stack cannot be used as the TCB was not created. Free
1434 vPortFreeStack( pxStack );
1442 #endif /* portSTACK_GROWTH */
1444 if( pxNewTCB != NULL )
1446 #if ( tskSTATIC_AND_DYNAMIC_ALLOCATION_POSSIBLE != 0 ) /*lint !e9029 !e731 Macro has been consolidated for readability reasons. */
1448 /* Tasks can be created statically or dynamically, so note this
1449 * task was created dynamically in case it is later deleted. */
1450 pxNewTCB->ucStaticallyAllocated = tskDYNAMICALLY_ALLOCATED_STACK_AND_TCB;
1452 #endif /* tskSTATIC_AND_DYNAMIC_ALLOCATION_POSSIBLE */
1454 prvInitialiseNewTask( pxTaskCode, pcName, ( uint32_t ) usStackDepth, pvParameters, uxPriority, pxCreatedTask, pxNewTCB, NULL );
1456 #if ( ( configNUMBER_OF_CORES > 1 ) && ( configUSE_CORE_AFFINITY == 1 ) )
1458 /* Set the task's affinity before scheduling it. */
1459 pxNewTCB->uxCoreAffinityMask = uxCoreAffinityMask;
1463 prvAddNewTaskToReadyList( pxNewTCB );
1468 xReturn = errCOULD_NOT_ALLOCATE_REQUIRED_MEMORY;
1474 #endif /* configSUPPORT_DYNAMIC_ALLOCATION */
1475 /*-----------------------------------------------------------*/
1477 static void prvInitialiseNewTask( TaskFunction_t pxTaskCode,
1478 const char * const pcName, /*lint !e971 Unqualified char types are allowed for strings and single characters only. */
1479 const uint32_t ulStackDepth,
1480 void * const pvParameters,
1481 UBaseType_t uxPriority,
1482 TaskHandle_t * const pxCreatedTask,
1484 const MemoryRegion_t * const xRegions )
1486 StackType_t * pxTopOfStack;
1489 #if ( portUSING_MPU_WRAPPERS == 1 )
1490 /* Should the task be created in privileged mode? */
1491 BaseType_t xRunPrivileged;
1493 if( ( uxPriority & portPRIVILEGE_BIT ) != 0U )
1495 xRunPrivileged = pdTRUE;
1499 xRunPrivileged = pdFALSE;
1501 uxPriority &= ~portPRIVILEGE_BIT;
1502 #endif /* portUSING_MPU_WRAPPERS == 1 */
1504 /* Avoid dependency on memset() if it is not required. */
1505 #if ( tskSET_NEW_STACKS_TO_KNOWN_VALUE == 1 )
1507 /* Fill the stack with a known value to assist debugging. */
1508 ( void ) memset( pxNewTCB->pxStack, ( int ) tskSTACK_FILL_BYTE, ( size_t ) ulStackDepth * sizeof( StackType_t ) );
1510 #endif /* tskSET_NEW_STACKS_TO_KNOWN_VALUE */
1512 /* Calculate the top of stack address. This depends on whether the stack
1513 * grows from high memory to low (as per the 80x86) or vice versa.
1514 * portSTACK_GROWTH is used to make the result positive or negative as required
1516 #if ( portSTACK_GROWTH < 0 )
1518 pxTopOfStack = &( pxNewTCB->pxStack[ ulStackDepth - ( uint32_t ) 1 ] );
1519 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(). */
1521 /* Check the alignment of the calculated top of stack is correct. */
1522 configASSERT( ( ( ( portPOINTER_SIZE_TYPE ) pxTopOfStack & ( portPOINTER_SIZE_TYPE ) portBYTE_ALIGNMENT_MASK ) == 0UL ) );
1524 #if ( configRECORD_STACK_HIGH_ADDRESS == 1 )
1526 /* Also record the stack's high address, which may assist
1528 pxNewTCB->pxEndOfStack = pxTopOfStack;
1530 #endif /* configRECORD_STACK_HIGH_ADDRESS */
1532 #else /* portSTACK_GROWTH */
1534 pxTopOfStack = pxNewTCB->pxStack;
1535 pxTopOfStack = ( StackType_t * ) ( ( ( ( portPOINTER_SIZE_TYPE ) pxTopOfStack ) + portBYTE_ALIGNMENT_MASK ) & ( ~( ( 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(). */
1537 /* Check the alignment of the calculated top of stack is correct. */
1538 configASSERT( ( ( ( portPOINTER_SIZE_TYPE ) pxTopOfStack & ( portPOINTER_SIZE_TYPE ) portBYTE_ALIGNMENT_MASK ) == 0UL ) );
1540 /* The other extreme of the stack space is required if stack checking is
1542 pxNewTCB->pxEndOfStack = pxNewTCB->pxStack + ( ulStackDepth - ( uint32_t ) 1 );
1544 #endif /* portSTACK_GROWTH */
1546 /* Store the task name in the TCB. */
1547 if( pcName != NULL )
1549 for( x = ( UBaseType_t ) 0; x < ( UBaseType_t ) configMAX_TASK_NAME_LEN; x++ )
1551 pxNewTCB->pcTaskName[ x ] = pcName[ x ];
1553 /* Don't copy all configMAX_TASK_NAME_LEN if the string is shorter than
1554 * configMAX_TASK_NAME_LEN characters just in case the memory after the
1555 * string is not accessible (extremely unlikely). */
1556 if( pcName[ x ] == ( char ) 0x00 )
1562 mtCOVERAGE_TEST_MARKER();
1566 /* Ensure the name string is terminated in the case that the string length
1567 * was greater or equal to configMAX_TASK_NAME_LEN. */
1568 pxNewTCB->pcTaskName[ configMAX_TASK_NAME_LEN - 1 ] = '\0';
1572 mtCOVERAGE_TEST_MARKER();
1575 /* This is used as an array index so must ensure it's not too large. */
1576 configASSERT( uxPriority < configMAX_PRIORITIES );
1578 if( uxPriority >= ( UBaseType_t ) configMAX_PRIORITIES )
1580 uxPriority = ( UBaseType_t ) configMAX_PRIORITIES - ( UBaseType_t ) 1U;
1584 mtCOVERAGE_TEST_MARKER();
1587 pxNewTCB->uxPriority = uxPriority;
1588 #if ( configUSE_MUTEXES == 1 )
1590 pxNewTCB->uxBasePriority = uxPriority;
1592 #endif /* configUSE_MUTEXES */
1594 vListInitialiseItem( &( pxNewTCB->xStateListItem ) );
1595 vListInitialiseItem( &( pxNewTCB->xEventListItem ) );
1597 /* Set the pxNewTCB as a link back from the ListItem_t. This is so we can get
1598 * back to the containing TCB from a generic item in a list. */
1599 listSET_LIST_ITEM_OWNER( &( pxNewTCB->xStateListItem ), pxNewTCB );
1601 /* Event lists are always in priority order. */
1602 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. */
1603 listSET_LIST_ITEM_OWNER( &( pxNewTCB->xEventListItem ), pxNewTCB );
1605 #if ( portUSING_MPU_WRAPPERS == 1 )
1607 vPortStoreTaskMPUSettings( &( pxNewTCB->xMPUSettings ), xRegions, pxNewTCB->pxStack, ulStackDepth );
1611 /* Avoid compiler warning about unreferenced parameter. */
1616 #if ( configUSE_C_RUNTIME_TLS_SUPPORT == 1 )
1618 /* Allocate and initialize memory for the task's TLS Block. */
1619 configINIT_TLS_BLOCK( pxNewTCB->xTLSBlock, pxTopOfStack );
1623 #if ( ( configNUMBER_OF_CORES > 1 ) && ( configUSE_CORE_AFFINITY == 1 ) )
1625 pxNewTCB->uxCoreAffinityMask = tskNO_AFFINITY;
1629 #if ( configUSE_TASK_PREEMPTION_DISABLE == 1 )
1631 pxNewTCB->xPreemptionDisable = 0;
1635 /* Initialize the TCB stack to look as if the task was already running,
1636 * but had been interrupted by the scheduler. The return address is set
1637 * to the start of the task function. Once the stack has been initialised
1638 * the top of stack variable is updated. */
1639 #if ( portUSING_MPU_WRAPPERS == 1 )
1641 /* If the port has capability to detect stack overflow,
1642 * pass the stack end address to the stack initialization
1643 * function as well. */
1644 #if ( portHAS_STACK_OVERFLOW_CHECKING == 1 )
1646 #if ( portSTACK_GROWTH < 0 )
1648 pxNewTCB->pxTopOfStack = pxPortInitialiseStack( pxTopOfStack, pxNewTCB->pxStack, pxTaskCode, pvParameters, xRunPrivileged, &( pxNewTCB->xMPUSettings ) );
1650 #else /* portSTACK_GROWTH */
1652 pxNewTCB->pxTopOfStack = pxPortInitialiseStack( pxTopOfStack, pxNewTCB->pxEndOfStack, pxTaskCode, pvParameters, xRunPrivileged, &( pxNewTCB->xMPUSettings ) );
1654 #endif /* portSTACK_GROWTH */
1656 #else /* portHAS_STACK_OVERFLOW_CHECKING */
1658 pxNewTCB->pxTopOfStack = pxPortInitialiseStack( pxTopOfStack, pxTaskCode, pvParameters, xRunPrivileged, &( pxNewTCB->xMPUSettings ) );
1660 #endif /* portHAS_STACK_OVERFLOW_CHECKING */
1662 #else /* portUSING_MPU_WRAPPERS */
1664 /* If the port has capability to detect stack overflow,
1665 * pass the stack end address to the stack initialization
1666 * function as well. */
1667 #if ( portHAS_STACK_OVERFLOW_CHECKING == 1 )
1669 #if ( portSTACK_GROWTH < 0 )
1671 pxNewTCB->pxTopOfStack = pxPortInitialiseStack( pxTopOfStack, pxNewTCB->pxStack, pxTaskCode, pvParameters );
1673 #else /* portSTACK_GROWTH */
1675 pxNewTCB->pxTopOfStack = pxPortInitialiseStack( pxTopOfStack, pxNewTCB->pxEndOfStack, pxTaskCode, pvParameters );
1677 #endif /* portSTACK_GROWTH */
1679 #else /* portHAS_STACK_OVERFLOW_CHECKING */
1681 pxNewTCB->pxTopOfStack = pxPortInitialiseStack( pxTopOfStack, pxTaskCode, pvParameters );
1683 #endif /* portHAS_STACK_OVERFLOW_CHECKING */
1685 #endif /* portUSING_MPU_WRAPPERS */
1687 /* Initialize task state and task attributes. */
1688 #if ( configNUMBER_OF_CORES > 1 )
1690 pxNewTCB->xTaskRunState = taskTASK_NOT_RUNNING;
1692 /* Is this an idle task? */
1693 if( ( ( TaskFunction_t ) pxTaskCode == ( TaskFunction_t ) prvIdleTask ) || ( ( TaskFunction_t ) pxTaskCode == ( TaskFunction_t ) prvMinimalIdleTask ) )
1695 pxNewTCB->uxTaskAttributes |= taskATTRIBUTE_IS_IDLE;
1698 #endif /* #if ( configNUMBER_OF_CORES > 1 ) */
1700 if( pxCreatedTask != NULL )
1702 /* Pass the handle out in an anonymous way. The handle can be used to
1703 * change the created task's priority, delete the created task, etc.*/
1704 *pxCreatedTask = ( TaskHandle_t ) pxNewTCB;
1708 mtCOVERAGE_TEST_MARKER();
1711 /*-----------------------------------------------------------*/
1713 #if ( configNUMBER_OF_CORES == 1 )
1715 static void prvAddNewTaskToReadyList( TCB_t * pxNewTCB )
1717 /* Ensure interrupts don't access the task lists while the lists are being
1719 taskENTER_CRITICAL();
1721 uxCurrentNumberOfTasks++;
1723 if( pxCurrentTCB == NULL )
1725 /* There are no other tasks, or all the other tasks are in
1726 * the suspended state - make this the current task. */
1727 pxCurrentTCB = pxNewTCB;
1729 if( uxCurrentNumberOfTasks == ( UBaseType_t ) 1 )
1731 /* This is the first task to be created so do the preliminary
1732 * initialisation required. We will not recover if this call
1733 * fails, but we will report the failure. */
1734 prvInitialiseTaskLists();
1738 mtCOVERAGE_TEST_MARKER();
1743 /* If the scheduler is not already running, make this task the
1744 * current task if it is the highest priority task to be created
1746 if( xSchedulerRunning == pdFALSE )
1748 if( pxCurrentTCB->uxPriority <= pxNewTCB->uxPriority )
1750 pxCurrentTCB = pxNewTCB;
1754 mtCOVERAGE_TEST_MARKER();
1759 mtCOVERAGE_TEST_MARKER();
1765 #if ( configUSE_TRACE_FACILITY == 1 )
1767 /* Add a counter into the TCB for tracing only. */
1768 pxNewTCB->uxTCBNumber = uxTaskNumber;
1770 #endif /* configUSE_TRACE_FACILITY */
1771 traceTASK_CREATE( pxNewTCB );
1773 prvAddTaskToReadyList( pxNewTCB );
1775 portSETUP_TCB( pxNewTCB );
1777 taskEXIT_CRITICAL();
1779 if( xSchedulerRunning != pdFALSE )
1781 /* If the created task is of a higher priority than the current task
1782 * then it should run now. */
1783 if( pxCurrentTCB->uxPriority < pxNewTCB->uxPriority )
1785 taskYIELD_IF_USING_PREEMPTION();
1789 mtCOVERAGE_TEST_MARKER();
1794 mtCOVERAGE_TEST_MARKER();
1798 #else /* #if ( configNUMBER_OF_CORES == 1 ) */
1800 static void prvAddNewTaskToReadyList( TCB_t * pxNewTCB )
1802 /* Ensure interrupts don't access the task lists while the lists are being
1804 taskENTER_CRITICAL();
1806 uxCurrentNumberOfTasks++;
1808 if( xSchedulerRunning == pdFALSE )
1810 if( uxCurrentNumberOfTasks == ( UBaseType_t ) 1 )
1812 /* This is the first task to be created so do the preliminary
1813 * initialisation required. We will not recover if this call
1814 * fails, but we will report the failure. */
1815 prvInitialiseTaskLists();
1819 mtCOVERAGE_TEST_MARKER();
1822 if( ( pxNewTCB->uxTaskAttributes & taskATTRIBUTE_IS_IDLE ) != 0U )
1826 /* Check if a core is free. */
1827 for( xCoreID = ( BaseType_t ) 0; xCoreID < ( BaseType_t ) configNUMBER_OF_CORES; xCoreID++ )
1829 if( pxCurrentTCBs[ xCoreID ] == NULL )
1831 pxNewTCB->xTaskRunState = xCoreID;
1832 pxCurrentTCBs[ xCoreID ] = pxNewTCB;
1837 mtCOVERAGE_TEST_MARKER();
1843 mtCOVERAGE_TEST_MARKER();
1849 #if ( configUSE_TRACE_FACILITY == 1 )
1851 /* Add a counter into the TCB for tracing only. */
1852 pxNewTCB->uxTCBNumber = uxTaskNumber;
1854 #endif /* configUSE_TRACE_FACILITY */
1855 traceTASK_CREATE( pxNewTCB );
1857 prvAddTaskToReadyList( pxNewTCB );
1859 portSETUP_TCB( pxNewTCB );
1861 if( xSchedulerRunning != pdFALSE )
1863 /* If the created task is of a higher priority than another
1864 * currently running task and preemption is on then it should
1866 #if ( configUSE_PREEMPTION == 1 )
1867 prvYieldForTask( pxNewTCB );
1872 mtCOVERAGE_TEST_MARKER();
1875 taskEXIT_CRITICAL();
1878 #endif /* #if ( configNUMBER_OF_CORES == 1 ) */
1879 /*-----------------------------------------------------------*/
1881 #if ( INCLUDE_vTaskDelete == 1 )
1883 void vTaskDelete( TaskHandle_t xTaskToDelete )
1887 taskENTER_CRITICAL();
1889 /* If null is passed in here then it is the calling task that is
1891 pxTCB = prvGetTCBFromHandle( xTaskToDelete );
1893 /* Remove task from the ready/delayed list. */
1894 if( uxListRemove( &( pxTCB->xStateListItem ) ) == ( UBaseType_t ) 0 )
1896 taskRESET_READY_PRIORITY( pxTCB->uxPriority );
1900 mtCOVERAGE_TEST_MARKER();
1903 /* Is the task waiting on an event also? */
1904 if( listLIST_ITEM_CONTAINER( &( pxTCB->xEventListItem ) ) != NULL )
1906 ( void ) uxListRemove( &( pxTCB->xEventListItem ) );
1910 mtCOVERAGE_TEST_MARKER();
1913 /* Increment the uxTaskNumber also so kernel aware debuggers can
1914 * detect that the task lists need re-generating. This is done before
1915 * portPRE_TASK_DELETE_HOOK() as in the Windows port that macro will
1919 /* If the task is running (or yielding), we must add it to the
1920 * termination list so that an idle task can delete it when it is
1921 * no longer running. */
1922 #if ( configNUMBER_OF_CORES == 1 )
1923 if( pxTCB == pxCurrentTCB )
1925 if( pxTCB->xTaskRunState != taskTASK_NOT_RUNNING )
1928 /* A running task is being deleted. This cannot complete within the
1929 * task itself, as a context switch to another task is required.
1930 * Place the task in the termination list. The idle task will
1931 * check the termination list and free up any memory allocated by
1932 * the scheduler for the TCB and stack of the deleted task. */
1933 vListInsertEnd( &xTasksWaitingTermination, &( pxTCB->xStateListItem ) );
1935 /* Increment the ucTasksDeleted variable so the idle task knows
1936 * there is a task that has been deleted and that it should therefore
1937 * check the xTasksWaitingTermination list. */
1938 ++uxDeletedTasksWaitingCleanUp;
1940 /* Call the delete hook before portPRE_TASK_DELETE_HOOK() as
1941 * portPRE_TASK_DELETE_HOOK() does not return in the Win32 port. */
1942 traceTASK_DELETE( pxTCB );
1944 /* The pre-delete hook is primarily for the Windows simulator,
1945 * in which Windows specific clean up operations are performed,
1946 * after which it is not possible to yield away from this task -
1947 * hence xYieldPending is used to latch that a context switch is
1949 #if ( configNUMBER_OF_CORES == 1 )
1950 portPRE_TASK_DELETE_HOOK( pxTCB, &xYieldPendings[ 0 ] );
1952 portPRE_TASK_DELETE_HOOK( pxTCB, &xYieldPendings[ pxTCB->xTaskRunState ] );
1957 --uxCurrentNumberOfTasks;
1958 traceTASK_DELETE( pxTCB );
1960 /* Reset the next expected unblock time in case it referred to
1961 * the task that has just been deleted. */
1962 prvResetNextTaskUnblockTime();
1966 #if ( configNUMBER_OF_CORES == 1 )
1968 taskEXIT_CRITICAL();
1970 /* If the task is not deleting itself, call prvDeleteTCB from outside of
1971 * critical section. If a task deletes itself, prvDeleteTCB is called
1972 * from prvCheckTasksWaitingTermination which is called from Idle task. */
1973 if( pxTCB != pxCurrentTCB )
1975 prvDeleteTCB( pxTCB );
1978 /* Force a reschedule if it is the currently running task that has just
1980 if( xSchedulerRunning != pdFALSE )
1982 if( pxTCB == pxCurrentTCB )
1984 configASSERT( uxSchedulerSuspended == 0 );
1985 portYIELD_WITHIN_API();
1989 mtCOVERAGE_TEST_MARKER();
1993 #else /* #if ( configNUMBER_OF_CORES == 1 ) */
1995 /* If a running task is not deleting itself, call prvDeleteTCB. If a running
1996 * task deletes itself, prvDeleteTCB is called from prvCheckTasksWaitingTermination
1997 * which is called from Idle task. */
1998 if( pxTCB->xTaskRunState == taskTASK_NOT_RUNNING )
2000 prvDeleteTCB( pxTCB );
2003 /* Force a reschedule if the task that has just been deleted was running. */
2004 if( ( xSchedulerRunning != pdFALSE ) && ( taskTASK_IS_RUNNING( pxTCB ) == pdTRUE ) )
2006 if( pxTCB->xTaskRunState == ( TaskRunning_t ) portGET_CORE_ID() )
2008 configASSERT( uxSchedulerSuspended == 0 );
2009 vTaskYieldWithinAPI();
2013 prvYieldCore( pxTCB->xTaskRunState );
2017 taskEXIT_CRITICAL();
2019 #endif /* #if ( configNUMBER_OF_CORES == 1 ) */
2022 #endif /* INCLUDE_vTaskDelete */
2023 /*-----------------------------------------------------------*/
2025 #if ( INCLUDE_xTaskDelayUntil == 1 )
2027 BaseType_t xTaskDelayUntil( TickType_t * const pxPreviousWakeTime,
2028 const TickType_t xTimeIncrement )
2030 TickType_t xTimeToWake;
2031 BaseType_t xAlreadyYielded, xShouldDelay = pdFALSE;
2033 configASSERT( pxPreviousWakeTime );
2034 configASSERT( ( xTimeIncrement > 0U ) );
2038 /* Minor optimisation. The tick count cannot change in this
2040 const TickType_t xConstTickCount = xTickCount;
2042 configASSERT( uxSchedulerSuspended == 1U );
2044 /* Generate the tick time at which the task wants to wake. */
2045 xTimeToWake = *pxPreviousWakeTime + xTimeIncrement;
2047 if( xConstTickCount < *pxPreviousWakeTime )
2049 /* The tick count has overflowed since this function was
2050 * lasted called. In this case the only time we should ever
2051 * actually delay is if the wake time has also overflowed,
2052 * and the wake time is greater than the tick time. When this
2053 * is the case it is as if neither time had overflowed. */
2054 if( ( xTimeToWake < *pxPreviousWakeTime ) && ( xTimeToWake > xConstTickCount ) )
2056 xShouldDelay = pdTRUE;
2060 mtCOVERAGE_TEST_MARKER();
2065 /* The tick time has not overflowed. In this case we will
2066 * delay if either the wake time has overflowed, and/or the
2067 * tick time is less than the wake time. */
2068 if( ( xTimeToWake < *pxPreviousWakeTime ) || ( xTimeToWake > xConstTickCount ) )
2070 xShouldDelay = pdTRUE;
2074 mtCOVERAGE_TEST_MARKER();
2078 /* Update the wake time ready for the next call. */
2079 *pxPreviousWakeTime = xTimeToWake;
2081 if( xShouldDelay != pdFALSE )
2083 traceTASK_DELAY_UNTIL( xTimeToWake );
2085 /* prvAddCurrentTaskToDelayedList() needs the block time, not
2086 * the time to wake, so subtract the current tick count. */
2087 prvAddCurrentTaskToDelayedList( xTimeToWake - xConstTickCount, pdFALSE );
2091 mtCOVERAGE_TEST_MARKER();
2094 xAlreadyYielded = xTaskResumeAll();
2096 /* Force a reschedule if xTaskResumeAll has not already done so, we may
2097 * have put ourselves to sleep. */
2098 if( xAlreadyYielded == pdFALSE )
2100 #if ( configNUMBER_OF_CORES == 1 )
2101 portYIELD_WITHIN_API();
2103 vTaskYieldWithinAPI();
2108 mtCOVERAGE_TEST_MARKER();
2111 return xShouldDelay;
2114 #endif /* INCLUDE_xTaskDelayUntil */
2115 /*-----------------------------------------------------------*/
2117 #if ( INCLUDE_vTaskDelay == 1 )
2119 void vTaskDelay( const TickType_t xTicksToDelay )
2121 BaseType_t xAlreadyYielded = pdFALSE;
2123 /* A delay time of zero just forces a reschedule. */
2124 if( xTicksToDelay > ( TickType_t ) 0U )
2128 configASSERT( uxSchedulerSuspended == 1U );
2132 /* A task that is removed from the event list while the
2133 * scheduler is suspended will not get placed in the ready
2134 * list or removed from the blocked list until the scheduler
2137 * This task cannot be in an event list as it is the currently
2138 * executing task. */
2139 prvAddCurrentTaskToDelayedList( xTicksToDelay, pdFALSE );
2141 xAlreadyYielded = xTaskResumeAll();
2145 mtCOVERAGE_TEST_MARKER();
2148 /* Force a reschedule if xTaskResumeAll has not already done so, we may
2149 * have put ourselves to sleep. */
2150 if( xAlreadyYielded == pdFALSE )
2152 #if ( configNUMBER_OF_CORES == 1 )
2153 portYIELD_WITHIN_API();
2155 vTaskYieldWithinAPI();
2160 mtCOVERAGE_TEST_MARKER();
2164 #endif /* INCLUDE_vTaskDelay */
2165 /*-----------------------------------------------------------*/
2167 #if ( ( INCLUDE_eTaskGetState == 1 ) || ( configUSE_TRACE_FACILITY == 1 ) || ( INCLUDE_xTaskAbortDelay == 1 ) )
2169 eTaskState eTaskGetState( TaskHandle_t xTask )
2172 List_t const * pxStateList;
2173 List_t const * pxEventList;
2174 List_t const * pxDelayedList;
2175 List_t const * pxOverflowedDelayedList;
2176 const TCB_t * const pxTCB = xTask;
2178 configASSERT( pxTCB );
2180 #if ( configNUMBER_OF_CORES == 1 )
2181 if( pxTCB == pxCurrentTCB )
2183 /* The task calling this function is querying its own state. */
2189 taskENTER_CRITICAL();
2191 pxStateList = listLIST_ITEM_CONTAINER( &( pxTCB->xStateListItem ) );
2192 pxEventList = listLIST_ITEM_CONTAINER( &( pxTCB->xEventListItem ) );
2193 pxDelayedList = pxDelayedTaskList;
2194 pxOverflowedDelayedList = pxOverflowDelayedTaskList;
2196 taskEXIT_CRITICAL();
2198 if( pxEventList == &xPendingReadyList )
2200 /* The task has been placed on the pending ready list, so its
2201 * state is eReady regardless of what list the task's state list
2202 * item is currently placed on. */
2205 else if( ( pxStateList == pxDelayedList ) || ( pxStateList == pxOverflowedDelayedList ) )
2207 /* The task being queried is referenced from one of the Blocked
2212 #if ( INCLUDE_vTaskSuspend == 1 )
2213 else if( pxStateList == &xSuspendedTaskList )
2215 /* The task being queried is referenced from the suspended
2216 * list. Is it genuinely suspended or is it blocked
2218 if( listLIST_ITEM_CONTAINER( &( pxTCB->xEventListItem ) ) == NULL )
2220 #if ( configUSE_TASK_NOTIFICATIONS == 1 )
2224 /* The task does not appear on the event list item of
2225 * and of the RTOS objects, but could still be in the
2226 * blocked state if it is waiting on its notification
2227 * rather than waiting on an object. If not, is
2229 eReturn = eSuspended;
2231 for( x = ( BaseType_t ) 0; x < ( BaseType_t ) configTASK_NOTIFICATION_ARRAY_ENTRIES; x++ )
2233 if( pxTCB->ucNotifyState[ x ] == taskWAITING_NOTIFICATION )
2240 #else /* if ( configUSE_TASK_NOTIFICATIONS == 1 ) */
2242 eReturn = eSuspended;
2244 #endif /* if ( configUSE_TASK_NOTIFICATIONS == 1 ) */
2251 #endif /* if ( INCLUDE_vTaskSuspend == 1 ) */
2253 #if ( INCLUDE_vTaskDelete == 1 )
2254 else if( ( pxStateList == &xTasksWaitingTermination ) || ( pxStateList == NULL ) )
2256 /* The task being queried is referenced from the deleted
2257 * tasks list, or it is not referenced from any lists at
2263 else /*lint !e525 Negative indentation is intended to make use of pre-processor clearer. */
2265 #if ( configNUMBER_OF_CORES == 1 )
2267 /* If the task is not in any other state, it must be in the
2268 * Ready (including pending ready) state. */
2271 #else /* #if ( configNUMBER_OF_CORES == 1 ) */
2273 if( taskTASK_IS_RUNNING( pxTCB ) == pdTRUE )
2275 /* Is it actively running on a core? */
2280 /* If the task is not in any other state, it must be in the
2281 * Ready (including pending ready) state. */
2285 #endif /* #if ( configNUMBER_OF_CORES == 1 ) */
2290 } /*lint !e818 xTask cannot be a pointer to const because it is a typedef. */
2292 #endif /* INCLUDE_eTaskGetState */
2293 /*-----------------------------------------------------------*/
2295 #if ( INCLUDE_uxTaskPriorityGet == 1 )
2297 UBaseType_t uxTaskPriorityGet( const TaskHandle_t xTask )
2299 TCB_t const * pxTCB;
2300 UBaseType_t uxReturn;
2302 taskENTER_CRITICAL();
2304 /* If null is passed in here then it is the priority of the task
2305 * that called uxTaskPriorityGet() that is being queried. */
2306 pxTCB = prvGetTCBFromHandle( xTask );
2307 uxReturn = pxTCB->uxPriority;
2309 taskEXIT_CRITICAL();
2314 #endif /* INCLUDE_uxTaskPriorityGet */
2315 /*-----------------------------------------------------------*/
2317 #if ( INCLUDE_uxTaskPriorityGet == 1 )
2319 UBaseType_t uxTaskPriorityGetFromISR( const TaskHandle_t xTask )
2321 TCB_t const * pxTCB;
2322 UBaseType_t uxReturn;
2323 UBaseType_t uxSavedInterruptStatus;
2325 /* RTOS ports that support interrupt nesting have the concept of a
2326 * maximum system call (or maximum API call) interrupt priority.
2327 * Interrupts that are above the maximum system call priority are keep
2328 * permanently enabled, even when the RTOS kernel is in a critical section,
2329 * but cannot make any calls to FreeRTOS API functions. If configASSERT()
2330 * is defined in FreeRTOSConfig.h then
2331 * portASSERT_IF_INTERRUPT_PRIORITY_INVALID() will result in an assertion
2332 * failure if a FreeRTOS API function is called from an interrupt that has
2333 * been assigned a priority above the configured maximum system call
2334 * priority. Only FreeRTOS functions that end in FromISR can be called
2335 * from interrupts that have been assigned a priority at or (logically)
2336 * below the maximum system call interrupt priority. FreeRTOS maintains a
2337 * separate interrupt safe API to ensure interrupt entry is as fast and as
2338 * simple as possible. More information (albeit Cortex-M specific) is
2339 * provided on the following link:
2340 * https://www.FreeRTOS.org/RTOS-Cortex-M3-M4.html */
2341 portASSERT_IF_INTERRUPT_PRIORITY_INVALID();
2343 uxSavedInterruptStatus = taskENTER_CRITICAL_FROM_ISR();
2345 /* If null is passed in here then it is the priority of the calling
2346 * task that is being queried. */
2347 pxTCB = prvGetTCBFromHandle( xTask );
2348 uxReturn = pxTCB->uxPriority;
2350 taskEXIT_CRITICAL_FROM_ISR( uxSavedInterruptStatus );
2355 #endif /* INCLUDE_uxTaskPriorityGet */
2356 /*-----------------------------------------------------------*/
2358 #if ( INCLUDE_vTaskPrioritySet == 1 )
2360 void vTaskPrioritySet( TaskHandle_t xTask,
2361 UBaseType_t uxNewPriority )
2364 UBaseType_t uxCurrentBasePriority, uxPriorityUsedOnEntry;
2365 BaseType_t xYieldRequired = pdFALSE;
2367 #if ( configNUMBER_OF_CORES > 1 )
2368 BaseType_t xYieldForTask = pdFALSE;
2371 configASSERT( uxNewPriority < configMAX_PRIORITIES );
2373 /* Ensure the new priority is valid. */
2374 if( uxNewPriority >= ( UBaseType_t ) configMAX_PRIORITIES )
2376 uxNewPriority = ( UBaseType_t ) configMAX_PRIORITIES - ( UBaseType_t ) 1U;
2380 mtCOVERAGE_TEST_MARKER();
2383 taskENTER_CRITICAL();
2385 /* If null is passed in here then it is the priority of the calling
2386 * task that is being changed. */
2387 pxTCB = prvGetTCBFromHandle( xTask );
2389 traceTASK_PRIORITY_SET( pxTCB, uxNewPriority );
2391 #if ( configUSE_MUTEXES == 1 )
2393 uxCurrentBasePriority = pxTCB->uxBasePriority;
2397 uxCurrentBasePriority = pxTCB->uxPriority;
2401 if( uxCurrentBasePriority != uxNewPriority )
2403 /* The priority change may have readied a task of higher
2404 * priority than a running task. */
2405 if( uxNewPriority > uxCurrentBasePriority )
2407 #if ( configNUMBER_OF_CORES == 1 )
2409 if( pxTCB != pxCurrentTCB )
2411 /* The priority of a task other than the currently
2412 * running task is being raised. Is the priority being
2413 * raised above that of the running task? */
2414 if( uxNewPriority > pxCurrentTCB->uxPriority )
2416 xYieldRequired = pdTRUE;
2420 mtCOVERAGE_TEST_MARKER();
2425 /* The priority of the running task is being raised,
2426 * but the running task must already be the highest
2427 * priority task able to run so no yield is required. */
2430 #else /* #if ( configNUMBER_OF_CORES == 1 ) */
2432 /* The priority of a task is being raised so
2433 * perform a yield for this task later. */
2434 xYieldForTask = pdTRUE;
2436 #endif /* #if ( configNUMBER_OF_CORES == 1 ) */
2438 else if( taskTASK_IS_RUNNING( pxTCB ) == pdTRUE )
2440 /* Setting the priority of a running task down means
2441 * there may now be another task of higher priority that
2442 * is ready to execute. */
2443 #if ( configUSE_TASK_PREEMPTION_DISABLE == 1 )
2444 if( pxTCB->xPreemptionDisable == pdFALSE )
2447 xYieldRequired = pdTRUE;
2452 /* Setting the priority of any other task down does not
2453 * require a yield as the running task must be above the
2454 * new priority of the task being modified. */
2457 /* Remember the ready list the task might be referenced from
2458 * before its uxPriority member is changed so the
2459 * taskRESET_READY_PRIORITY() macro can function correctly. */
2460 uxPriorityUsedOnEntry = pxTCB->uxPriority;
2462 #if ( configUSE_MUTEXES == 1 )
2464 /* Only change the priority being used if the task is not
2465 * currently using an inherited priority or the new priority
2466 * is bigger than the inherited priority. */
2467 if( ( pxTCB->uxBasePriority == pxTCB->uxPriority ) || ( uxNewPriority > pxTCB->uxPriority ) )
2469 pxTCB->uxPriority = uxNewPriority;
2473 mtCOVERAGE_TEST_MARKER();
2476 /* The base priority gets set whatever. */
2477 pxTCB->uxBasePriority = uxNewPriority;
2479 #else /* if ( configUSE_MUTEXES == 1 ) */
2481 pxTCB->uxPriority = uxNewPriority;
2483 #endif /* if ( configUSE_MUTEXES == 1 ) */
2485 /* Only reset the event list item value if the value is not
2486 * being used for anything else. */
2487 if( ( listGET_LIST_ITEM_VALUE( &( pxTCB->xEventListItem ) ) & taskEVENT_LIST_ITEM_VALUE_IN_USE ) == 0UL )
2489 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. */
2493 mtCOVERAGE_TEST_MARKER();
2496 /* If the task is in the blocked or suspended list we need do
2497 * nothing more than change its priority variable. However, if
2498 * the task is in a ready list it needs to be removed and placed
2499 * in the list appropriate to its new priority. */
2500 if( listIS_CONTAINED_WITHIN( &( pxReadyTasksLists[ uxPriorityUsedOnEntry ] ), &( pxTCB->xStateListItem ) ) != pdFALSE )
2502 /* The task is currently in its ready list - remove before
2503 * adding it to its new ready list. As we are in a critical
2504 * section we can do this even if the scheduler is suspended. */
2505 if( uxListRemove( &( pxTCB->xStateListItem ) ) == ( UBaseType_t ) 0 )
2507 /* It is known that the task is in its ready list so
2508 * there is no need to check again and the port level
2509 * reset macro can be called directly. */
2510 portRESET_READY_PRIORITY( uxPriorityUsedOnEntry, uxTopReadyPriority );
2514 mtCOVERAGE_TEST_MARKER();
2517 prvAddTaskToReadyList( pxTCB );
2521 #if ( configNUMBER_OF_CORES == 1 )
2523 mtCOVERAGE_TEST_MARKER();
2527 /* It's possible that xYieldForTask was already set to pdTRUE because
2528 * its priority is being raised. However, since it is not in a ready list
2529 * we don't actually need to yield for it. */
2530 xYieldForTask = pdFALSE;
2535 #if ( configNUMBER_OF_CORES == 1 )
2537 if( xYieldRequired != pdFALSE )
2539 taskYIELD_IF_USING_PREEMPTION();
2543 mtCOVERAGE_TEST_MARKER();
2546 #else /* #if ( configNUMBER_OF_CORES == 1 ) */
2548 #if ( configUSE_PREEMPTION == 1 )
2550 if( xYieldRequired != pdFALSE )
2552 prvYieldCore( pxTCB->xTaskRunState );
2554 else if( xYieldForTask != pdFALSE )
2556 prvYieldForTask( pxTCB );
2560 mtCOVERAGE_TEST_MARKER();
2563 #endif /* #if ( configUSE_PREEMPTION == 1 ) */
2565 #endif /* #if ( configNUMBER_OF_CORES == 1 ) */
2567 /* Remove compiler warning about unused variables when the port
2568 * optimised task selection is not being used. */
2569 ( void ) uxPriorityUsedOnEntry;
2572 taskEXIT_CRITICAL();
2575 #endif /* INCLUDE_vTaskPrioritySet */
2576 /*-----------------------------------------------------------*/
2578 #if ( ( configNUMBER_OF_CORES > 1 ) && ( configUSE_CORE_AFFINITY == 1 ) )
2579 void vTaskCoreAffinitySet( const TaskHandle_t xTask,
2580 UBaseType_t uxCoreAffinityMask )
2584 UBaseType_t uxPrevCoreAffinityMask;
2586 #if ( configUSE_PREEMPTION == 1 )
2587 UBaseType_t uxPrevNotAllowedCores;
2590 taskENTER_CRITICAL();
2592 pxTCB = prvGetTCBFromHandle( xTask );
2594 uxPrevCoreAffinityMask = pxTCB->uxCoreAffinityMask;
2595 pxTCB->uxCoreAffinityMask = uxCoreAffinityMask;
2597 if( xSchedulerRunning != pdFALSE )
2599 if( taskTASK_IS_RUNNING( pxTCB ) == pdTRUE )
2601 xCoreID = ( BaseType_t ) pxTCB->xTaskRunState;
2603 /* If the task can no longer run on the core it was running,
2604 * request the core to yield. */
2605 if( ( uxCoreAffinityMask & ( ( UBaseType_t ) 1U << ( UBaseType_t ) xCoreID ) ) == 0U )
2607 prvYieldCore( xCoreID );
2612 #if ( configUSE_PREEMPTION == 1 )
2614 /* Calculate the cores on which this task was not allowed to
2615 * run previously. */
2616 uxPrevNotAllowedCores = ( ~uxPrevCoreAffinityMask ) & ( ( 1U << configNUMBER_OF_CORES ) - 1U );
2618 /* Does the new core mask enables this task to run on any of the
2619 * previously not allowed cores? If yes, check if this task can be
2620 * scheduled on any of those cores. */
2621 if( ( uxPrevNotAllowedCores & uxCoreAffinityMask ) != 0U )
2623 prvYieldForTask( pxTCB );
2626 #else /* #if( configUSE_PREEMPTION == 1 ) */
2628 mtCOVERAGE_TEST_MARKER();
2630 #endif /* #if( configUSE_PREEMPTION == 1 ) */
2634 taskEXIT_CRITICAL();
2636 #endif /* #if ( ( configNUMBER_OF_CORES > 1 ) && ( configUSE_CORE_AFFINITY == 1 ) ) */
2637 /*-----------------------------------------------------------*/
2639 #if ( ( configNUMBER_OF_CORES > 1 ) && ( configUSE_CORE_AFFINITY == 1 ) )
2640 UBaseType_t vTaskCoreAffinityGet( ConstTaskHandle_t xTask )
2642 const TCB_t * pxTCB;
2643 UBaseType_t uxCoreAffinityMask;
2645 taskENTER_CRITICAL();
2647 pxTCB = prvGetTCBFromHandle( xTask );
2648 uxCoreAffinityMask = pxTCB->uxCoreAffinityMask;
2650 taskEXIT_CRITICAL();
2652 return uxCoreAffinityMask;
2654 #endif /* #if ( ( configNUMBER_OF_CORES > 1 ) && ( configUSE_CORE_AFFINITY == 1 ) ) */
2656 /*-----------------------------------------------------------*/
2658 #if ( configUSE_TASK_PREEMPTION_DISABLE == 1 )
2660 void vTaskPreemptionDisable( const TaskHandle_t xTask )
2664 taskENTER_CRITICAL();
2666 pxTCB = prvGetTCBFromHandle( xTask );
2668 pxTCB->xPreemptionDisable = pdTRUE;
2670 taskEXIT_CRITICAL();
2673 #endif /* #if ( configUSE_TASK_PREEMPTION_DISABLE == 1 ) */
2674 /*-----------------------------------------------------------*/
2676 #if ( configUSE_TASK_PREEMPTION_DISABLE == 1 )
2678 void vTaskPreemptionEnable( const TaskHandle_t xTask )
2683 taskENTER_CRITICAL();
2685 pxTCB = prvGetTCBFromHandle( xTask );
2687 pxTCB->xPreemptionDisable = pdFALSE;
2689 if( xSchedulerRunning != pdFALSE )
2691 if( taskTASK_IS_RUNNING( pxTCB ) == pdTRUE )
2693 xCoreID = ( BaseType_t ) pxTCB->xTaskRunState;
2694 prvYieldCore( xCoreID );
2698 taskEXIT_CRITICAL();
2701 #endif /* #if ( configUSE_TASK_PREEMPTION_DISABLE == 1 ) */
2702 /*-----------------------------------------------------------*/
2704 #if ( INCLUDE_vTaskSuspend == 1 )
2706 void vTaskSuspend( TaskHandle_t xTaskToSuspend )
2710 #if ( configNUMBER_OF_CORES > 1 )
2711 TaskRunning_t xTaskRunningOnCore;
2714 taskENTER_CRITICAL();
2716 /* If null is passed in here then it is the running task that is
2717 * being suspended. */
2718 pxTCB = prvGetTCBFromHandle( xTaskToSuspend );
2720 traceTASK_SUSPEND( pxTCB );
2722 #if ( configNUMBER_OF_CORES > 1 )
2723 xTaskRunningOnCore = pxTCB->xTaskRunState;
2726 /* Remove task from the ready/delayed list and place in the
2727 * suspended list. */
2728 if( uxListRemove( &( pxTCB->xStateListItem ) ) == ( UBaseType_t ) 0 )
2730 taskRESET_READY_PRIORITY( pxTCB->uxPriority );
2734 mtCOVERAGE_TEST_MARKER();
2737 /* Is the task waiting on an event also? */
2738 if( listLIST_ITEM_CONTAINER( &( pxTCB->xEventListItem ) ) != NULL )
2740 ( void ) uxListRemove( &( pxTCB->xEventListItem ) );
2744 mtCOVERAGE_TEST_MARKER();
2747 vListInsertEnd( &xSuspendedTaskList, &( pxTCB->xStateListItem ) );
2749 #if ( configUSE_TASK_NOTIFICATIONS == 1 )
2753 for( x = ( BaseType_t ) 0; x < ( BaseType_t ) configTASK_NOTIFICATION_ARRAY_ENTRIES; x++ )
2755 if( pxTCB->ucNotifyState[ x ] == taskWAITING_NOTIFICATION )
2757 /* The task was blocked to wait for a notification, but is
2758 * now suspended, so no notification was received. */
2759 pxTCB->ucNotifyState[ x ] = taskNOT_WAITING_NOTIFICATION;
2763 #endif /* if ( configUSE_TASK_NOTIFICATIONS == 1 ) */
2766 #if ( configNUMBER_OF_CORES == 1 )
2768 taskEXIT_CRITICAL();
2770 if( xSchedulerRunning != pdFALSE )
2772 /* Reset the next expected unblock time in case it referred to the
2773 * task that is now in the Suspended state. */
2774 taskENTER_CRITICAL();
2776 prvResetNextTaskUnblockTime();
2778 taskEXIT_CRITICAL();
2782 mtCOVERAGE_TEST_MARKER();
2785 if( pxTCB == pxCurrentTCB )
2787 if( xSchedulerRunning != pdFALSE )
2789 /* The current task has just been suspended. */
2790 configASSERT( uxSchedulerSuspended == 0 );
2791 portYIELD_WITHIN_API();
2795 /* The scheduler is not running, but the task that was pointed
2796 * to by pxCurrentTCB has just been suspended and pxCurrentTCB
2797 * must be adjusted to point to a different task. */
2798 if( listCURRENT_LIST_LENGTH( &xSuspendedTaskList ) == uxCurrentNumberOfTasks ) /*lint !e931 Right has no side effect, just volatile. */
2800 /* No other tasks are ready, so set pxCurrentTCB back to
2801 * NULL so when the next task is created pxCurrentTCB will
2802 * be set to point to it no matter what its relative priority
2804 pxCurrentTCB = NULL;
2808 vTaskSwitchContext();
2814 mtCOVERAGE_TEST_MARKER();
2817 #else /* #if ( configNUMBER_OF_CORES == 1 ) */
2819 if( xSchedulerRunning != pdFALSE )
2821 /* Reset the next expected unblock time in case it referred to the
2822 * task that is now in the Suspended state. */
2823 prvResetNextTaskUnblockTime();
2827 mtCOVERAGE_TEST_MARKER();
2830 if( taskTASK_IS_RUNNING( pxTCB ) == pdTRUE )
2832 if( xSchedulerRunning != pdFALSE )
2834 if( xTaskRunningOnCore == ( TaskRunning_t ) portGET_CORE_ID() )
2836 /* The current task has just been suspended. */
2837 configASSERT( uxSchedulerSuspended == 0 );
2838 vTaskYieldWithinAPI();
2842 prvYieldCore( xTaskRunningOnCore );
2847 /* This code path is not possible because only Idle tasks are
2848 * assigned a core before the scheduler is started ( i.e.
2849 * taskTASK_IS_RUNNING is only true for idle tasks before
2850 * the scheduler is started ) and idle tasks cannot be
2852 mtCOVERAGE_TEST_MARKER();
2857 mtCOVERAGE_TEST_MARKER();
2860 taskEXIT_CRITICAL();
2862 #endif /* #if ( configNUMBER_OF_CORES == 1 ) */
2865 #endif /* INCLUDE_vTaskSuspend */
2866 /*-----------------------------------------------------------*/
2868 #if ( INCLUDE_vTaskSuspend == 1 )
2870 static BaseType_t prvTaskIsTaskSuspended( const TaskHandle_t xTask )
2872 BaseType_t xReturn = pdFALSE;
2873 const TCB_t * const pxTCB = xTask;
2875 /* Accesses xPendingReadyList so must be called from a critical
2878 /* It does not make sense to check if the calling task is suspended. */
2879 configASSERT( xTask );
2881 /* Is the task being resumed actually in the suspended list? */
2882 if( listIS_CONTAINED_WITHIN( &xSuspendedTaskList, &( pxTCB->xStateListItem ) ) != pdFALSE )
2884 /* Has the task already been resumed from within an ISR? */
2885 if( listIS_CONTAINED_WITHIN( &xPendingReadyList, &( pxTCB->xEventListItem ) ) == pdFALSE )
2887 /* Is it in the suspended list because it is in the Suspended
2888 * state, or because is is blocked with no timeout? */
2889 if( listIS_CONTAINED_WITHIN( NULL, &( pxTCB->xEventListItem ) ) != pdFALSE ) /*lint !e961. The cast is only redundant when NULL is used. */
2895 mtCOVERAGE_TEST_MARKER();
2900 mtCOVERAGE_TEST_MARKER();
2905 mtCOVERAGE_TEST_MARKER();
2909 } /*lint !e818 xTask cannot be a pointer to const because it is a typedef. */
2911 #endif /* INCLUDE_vTaskSuspend */
2912 /*-----------------------------------------------------------*/
2914 #if ( INCLUDE_vTaskSuspend == 1 )
2916 void vTaskResume( TaskHandle_t xTaskToResume )
2918 TCB_t * const pxTCB = xTaskToResume;
2920 /* It does not make sense to resume the calling task. */
2921 configASSERT( xTaskToResume );
2923 #if ( configNUMBER_OF_CORES == 1 )
2925 /* The parameter cannot be NULL as it is impossible to resume the
2926 * currently executing task. */
2927 if( ( pxTCB != pxCurrentTCB ) && ( pxTCB != NULL ) )
2930 /* The parameter cannot be NULL as it is impossible to resume the
2931 * currently executing task. It is also impossible to resume a task
2932 * that is actively running on another core but it is not safe
2933 * to check their run state here. Therefore, we get into a critical
2934 * section and check if the task is actually suspended or not. */
2938 taskENTER_CRITICAL();
2940 if( prvTaskIsTaskSuspended( pxTCB ) != pdFALSE )
2942 traceTASK_RESUME( pxTCB );
2944 /* The ready list can be accessed even if the scheduler is
2945 * suspended because this is inside a critical section. */
2946 ( void ) uxListRemove( &( pxTCB->xStateListItem ) );
2947 prvAddTaskToReadyList( pxTCB );
2949 #if ( configNUMBER_OF_CORES == 1 )
2951 /* A higher priority task may have just been resumed. */
2952 if( pxTCB->uxPriority > pxCurrentTCB->uxPriority )
2954 /* This yield may not cause the task just resumed to run,
2955 * but will leave the lists in the correct state for the
2957 taskYIELD_IF_USING_PREEMPTION();
2961 mtCOVERAGE_TEST_MARKER();
2964 #else /* #if ( configNUMBER_OF_CORES == 1 ) */
2966 #if ( configUSE_PREEMPTION == 1 )
2968 prvYieldForTask( pxTCB );
2970 #endif /* #if ( configUSE_PREEMPTION == 1 ) */
2972 #endif /* #if ( configNUMBER_OF_CORES == 1 ) */
2976 mtCOVERAGE_TEST_MARKER();
2979 taskEXIT_CRITICAL();
2983 mtCOVERAGE_TEST_MARKER();
2987 #endif /* INCLUDE_vTaskSuspend */
2989 /*-----------------------------------------------------------*/
2991 #if ( ( INCLUDE_xTaskResumeFromISR == 1 ) && ( INCLUDE_vTaskSuspend == 1 ) )
2993 BaseType_t xTaskResumeFromISR( TaskHandle_t xTaskToResume )
2995 BaseType_t xYieldRequired = pdFALSE;
2996 TCB_t * const pxTCB = xTaskToResume;
2997 UBaseType_t uxSavedInterruptStatus;
2999 configASSERT( xTaskToResume );
3001 /* RTOS ports that support interrupt nesting have the concept of a
3002 * maximum system call (or maximum API call) interrupt priority.
3003 * Interrupts that are above the maximum system call priority are keep
3004 * permanently enabled, even when the RTOS kernel is in a critical section,
3005 * but cannot make any calls to FreeRTOS API functions. If configASSERT()
3006 * is defined in FreeRTOSConfig.h then
3007 * portASSERT_IF_INTERRUPT_PRIORITY_INVALID() will result in an assertion
3008 * failure if a FreeRTOS API function is called from an interrupt that has
3009 * been assigned a priority above the configured maximum system call
3010 * priority. Only FreeRTOS functions that end in FromISR can be called
3011 * from interrupts that have been assigned a priority at or (logically)
3012 * below the maximum system call interrupt priority. FreeRTOS maintains a
3013 * separate interrupt safe API to ensure interrupt entry is as fast and as
3014 * simple as possible. More information (albeit Cortex-M specific) is
3015 * provided on the following link:
3016 * https://www.FreeRTOS.org/RTOS-Cortex-M3-M4.html */
3017 portASSERT_IF_INTERRUPT_PRIORITY_INVALID();
3019 uxSavedInterruptStatus = taskENTER_CRITICAL_FROM_ISR();
3021 if( prvTaskIsTaskSuspended( pxTCB ) != pdFALSE )
3023 traceTASK_RESUME_FROM_ISR( pxTCB );
3025 /* Check the ready lists can be accessed. */
3026 if( uxSchedulerSuspended == ( UBaseType_t ) 0U )
3028 #if ( configNUMBER_OF_CORES == 1 )
3030 /* Ready lists can be accessed so move the task from the
3031 * suspended list to the ready list directly. */
3032 if( pxTCB->uxPriority > pxCurrentTCB->uxPriority )
3034 xYieldRequired = pdTRUE;
3036 /* Mark that a yield is pending in case the user is not
3037 * using the return value to initiate a context switch
3038 * from the ISR using portYIELD_FROM_ISR. */
3039 xYieldPendings[ 0 ] = pdTRUE;
3043 mtCOVERAGE_TEST_MARKER();
3046 #endif /* #if ( configNUMBER_OF_CORES == 1 ) */
3048 ( void ) uxListRemove( &( pxTCB->xStateListItem ) );
3049 prvAddTaskToReadyList( pxTCB );
3053 /* The delayed or ready lists cannot be accessed so the task
3054 * is held in the pending ready list until the scheduler is
3056 vListInsertEnd( &( xPendingReadyList ), &( pxTCB->xEventListItem ) );
3059 #if ( ( configNUMBER_OF_CORES > 1 ) && ( configUSE_PREEMPTION == 1 ) )
3061 prvYieldForTask( pxTCB );
3063 if( xYieldPendings[ portGET_CORE_ID() ] != pdFALSE )
3065 xYieldRequired = pdTRUE;
3068 #endif /* #if ( ( configNUMBER_OF_CORES > 1 ) && ( configUSE_PREEMPTION == 1 ) ) */
3072 mtCOVERAGE_TEST_MARKER();
3075 taskEXIT_CRITICAL_FROM_ISR( uxSavedInterruptStatus );
3077 return xYieldRequired;
3080 #endif /* ( ( INCLUDE_xTaskResumeFromISR == 1 ) && ( INCLUDE_vTaskSuspend == 1 ) ) */
3081 /*-----------------------------------------------------------*/
3083 static BaseType_t prvCreateIdleTasks( void )
3085 BaseType_t xReturn = pdPASS;
3087 #if ( configNUMBER_OF_CORES == 1 )
3089 /* Add the idle task at the lowest priority. */
3090 #if ( configSUPPORT_STATIC_ALLOCATION == 1 )
3092 StaticTask_t * pxIdleTaskTCBBuffer = NULL;
3093 StackType_t * pxIdleTaskStackBuffer = NULL;
3094 uint32_t ulIdleTaskStackSize;
3096 /* The Idle task is created using user provided RAM - obtain the
3097 * address of the RAM then create the idle task. */
3098 vApplicationGetIdleTaskMemory( &pxIdleTaskTCBBuffer, &pxIdleTaskStackBuffer, &ulIdleTaskStackSize );
3099 xIdleTaskHandles[ 0 ] = xTaskCreateStatic( prvIdleTask,
3100 configIDLE_TASK_NAME,
3101 ulIdleTaskStackSize,
3102 ( void * ) NULL, /*lint !e961. The cast is not redundant for all compilers. */
3103 portPRIVILEGE_BIT, /* In effect ( tskIDLE_PRIORITY | portPRIVILEGE_BIT ), but tskIDLE_PRIORITY is zero. */
3104 pxIdleTaskStackBuffer,
3105 pxIdleTaskTCBBuffer ); /*lint !e961 MISRA exception, justified as it is not a redundant explicit cast to all supported compilers. */
3107 if( xIdleTaskHandles[ 0 ] != NULL )
3116 #else /* if ( configSUPPORT_STATIC_ALLOCATION == 1 ) */
3118 /* The Idle task is being created using dynamically allocated RAM. */
3119 xReturn = xTaskCreate( prvIdleTask,
3120 configIDLE_TASK_NAME,
3121 configMINIMAL_STACK_SIZE,
3123 portPRIVILEGE_BIT, /* In effect ( tskIDLE_PRIORITY | portPRIVILEGE_BIT ), but tskIDLE_PRIORITY is zero. */
3124 &xIdleTaskHandles[ 0 ] ); /*lint !e961 MISRA exception, justified as it is not a redundant explicit cast to all supported compilers. */
3126 #endif /* configSUPPORT_STATIC_ALLOCATION */
3128 #else /* #if ( configNUMBER_OF_CORES == 1 ) */
3131 char cIdleName[ configMAX_TASK_NAME_LEN ];
3133 /* Add each idle task at the lowest priority. */
3134 for( xCoreID = ( BaseType_t ) 0; xCoreID < ( BaseType_t ) configNUMBER_OF_CORES; xCoreID++ )
3138 if( xReturn == pdFAIL )
3144 mtCOVERAGE_TEST_MARKER();
3147 for( x = ( BaseType_t ) 0; x < ( BaseType_t ) configMAX_TASK_NAME_LEN; x++ )
3149 cIdleName[ x ] = configIDLE_TASK_NAME[ x ];
3151 /* Don't copy all configMAX_TASK_NAME_LEN if the string is shorter than
3152 * configMAX_TASK_NAME_LEN characters just in case the memory after the
3153 * string is not accessible (extremely unlikely). */
3154 if( cIdleName[ x ] == ( char ) 0x00 )
3160 mtCOVERAGE_TEST_MARKER();
3164 /* Append the idle task number to the end of the name if there is space. */
3165 if( x < ( BaseType_t ) configMAX_TASK_NAME_LEN )
3167 cIdleName[ x ] = ( char ) ( xCoreID + '0' );
3170 /* And append a null character if there is space. */
3171 if( x < ( BaseType_t ) configMAX_TASK_NAME_LEN )
3173 cIdleName[ x ] = '\0';
3177 mtCOVERAGE_TEST_MARKER();
3182 mtCOVERAGE_TEST_MARKER();
3185 #if ( configSUPPORT_STATIC_ALLOCATION == 1 )
3189 StaticTask_t * pxIdleTaskTCBBuffer = NULL;
3190 StackType_t * pxIdleTaskStackBuffer = NULL;
3191 uint32_t ulIdleTaskStackSize;
3193 /* The Idle task is created using user provided RAM - obtain the
3194 * address of the RAM then create the idle task. */
3195 vApplicationGetIdleTaskMemory( &pxIdleTaskTCBBuffer, &pxIdleTaskStackBuffer, &ulIdleTaskStackSize );
3196 xIdleTaskHandles[ xCoreID ] = xTaskCreateStatic( prvIdleTask,
3198 ulIdleTaskStackSize,
3199 ( void * ) NULL, /*lint !e961. The cast is not redundant for all compilers. */
3200 portPRIVILEGE_BIT, /* In effect ( tskIDLE_PRIORITY | portPRIVILEGE_BIT ), but tskIDLE_PRIORITY is zero. */
3201 pxIdleTaskStackBuffer,
3202 pxIdleTaskTCBBuffer ); /*lint !e961 MISRA exception, justified as it is not a redundant explicit cast to all supported compilers. */
3206 xIdleTaskHandles[ xCoreID ] = xTaskCreateStatic( prvMinimalIdleTask,
3208 configMINIMAL_STACK_SIZE,
3209 ( void * ) NULL, /*lint !e961. The cast is not redundant for all compilers. */
3210 portPRIVILEGE_BIT, /* In effect ( tskIDLE_PRIORITY | portPRIVILEGE_BIT ), but tskIDLE_PRIORITY is zero. */
3211 xIdleTaskStackBuffers[ xCoreID - 1 ],
3212 &xIdleTCBBuffers[ xCoreID - 1 ] ); /*lint !e961 MISRA exception, justified as it is not a redundant explicit cast to all supported compilers. */
3215 if( xIdleTaskHandles[ xCoreID ] != NULL )
3224 #else /* if ( configSUPPORT_STATIC_ALLOCATION == 1 ) */
3228 /* The Idle task is being created using dynamically allocated RAM. */
3229 xReturn = xTaskCreate( prvIdleTask,
3231 configMINIMAL_STACK_SIZE,
3233 portPRIVILEGE_BIT, /* In effect ( tskIDLE_PRIORITY | portPRIVILEGE_BIT ), but tskIDLE_PRIORITY is zero. */
3234 &xIdleTaskHandles[ xCoreID ] ); /*lint !e961 MISRA exception, justified as it is not a redundant explicit cast to all supported compilers. */
3238 xReturn = xTaskCreate( prvMinimalIdleTask,
3240 configMINIMAL_STACK_SIZE,
3242 portPRIVILEGE_BIT, /* In effect ( tskIDLE_PRIORITY | portPRIVILEGE_BIT ), but tskIDLE_PRIORITY is zero. */
3243 &xIdleTaskHandles[ xCoreID ] ); /*lint !e961 MISRA exception, justified as it is not a redundant explicit cast to all supported compilers. */
3246 #endif /* configSUPPORT_STATIC_ALLOCATION */
3249 #endif /* #if ( configNUMBER_OF_CORES == 1 ) */
3254 /*-----------------------------------------------------------*/
3256 void vTaskStartScheduler( void )
3260 xReturn = prvCreateIdleTasks();
3262 #if ( configUSE_TIMERS == 1 )
3264 if( xReturn == pdPASS )
3266 xReturn = xTimerCreateTimerTask();
3270 mtCOVERAGE_TEST_MARKER();
3273 #endif /* configUSE_TIMERS */
3275 if( xReturn == pdPASS )
3277 /* freertos_tasks_c_additions_init() should only be called if the user
3278 * definable macro FREERTOS_TASKS_C_ADDITIONS_INIT() is defined, as that is
3279 * the only macro called by the function. */
3280 #ifdef FREERTOS_TASKS_C_ADDITIONS_INIT
3282 freertos_tasks_c_additions_init();
3286 /* Interrupts are turned off here, to ensure a tick does not occur
3287 * before or during the call to xPortStartScheduler(). The stacks of
3288 * the created tasks contain a status word with interrupts switched on
3289 * so interrupts will automatically get re-enabled when the first task
3291 portDISABLE_INTERRUPTS();
3293 #if ( configUSE_C_RUNTIME_TLS_SUPPORT == 1 )
3295 /* Switch C-Runtime's TLS Block to point to the TLS
3296 * block specific to the task that will run first. */
3297 configSET_TLS_BLOCK( pxCurrentTCB->xTLSBlock );
3301 xNextTaskUnblockTime = portMAX_DELAY;
3302 xSchedulerRunning = pdTRUE;
3303 xTickCount = ( TickType_t ) configINITIAL_TICK_COUNT;
3305 /* If configGENERATE_RUN_TIME_STATS is defined then the following
3306 * macro must be defined to configure the timer/counter used to generate
3307 * the run time counter time base. NOTE: If configGENERATE_RUN_TIME_STATS
3308 * is set to 0 and the following line fails to build then ensure you do not
3309 * have portCONFIGURE_TIMER_FOR_RUN_TIME_STATS() defined in your
3310 * FreeRTOSConfig.h file. */
3311 portCONFIGURE_TIMER_FOR_RUN_TIME_STATS();
3313 traceTASK_SWITCHED_IN();
3315 /* Setting up the timer tick is hardware specific and thus in the
3316 * portable interface. */
3317 xPortStartScheduler();
3319 /* In most cases, xPortStartScheduler() will not return. If it
3320 * returns pdTRUE then there was not enough heap memory available
3321 * to create either the Idle or the Timer task. If it returned
3322 * pdFALSE, then the application called xTaskEndScheduler().
3323 * Most ports don't implement xTaskEndScheduler() as there is
3324 * nothing to return to. */
3328 /* This line will only be reached if the kernel could not be started,
3329 * because there was not enough FreeRTOS heap to create the idle task
3330 * or the timer task. */
3331 configASSERT( xReturn != errCOULD_NOT_ALLOCATE_REQUIRED_MEMORY );
3334 /* Prevent compiler warnings if INCLUDE_xTaskGetIdleTaskHandle is set to 0,
3335 * meaning xIdleTaskHandles are not used anywhere else. */
3336 ( void ) xIdleTaskHandles;
3338 /* OpenOCD makes use of uxTopUsedPriority for thread debugging. Prevent uxTopUsedPriority
3339 * from getting optimized out as it is no longer used by the kernel. */
3340 ( void ) uxTopUsedPriority;
3342 /*-----------------------------------------------------------*/
3344 void vTaskEndScheduler( void )
3346 /* Stop the scheduler interrupts and call the portable scheduler end
3347 * routine so the original ISRs can be restored if necessary. The port
3348 * layer must ensure interrupts enable bit is left in the correct state. */
3349 portDISABLE_INTERRUPTS();
3350 xSchedulerRunning = pdFALSE;
3351 vPortEndScheduler();
3353 /*----------------------------------------------------------*/
3355 void vTaskSuspendAll( void )
3357 #if ( configNUMBER_OF_CORES == 1 )
3359 /* A critical section is not required as the variable is of type
3360 * BaseType_t. Please read Richard Barry's reply in the following link to a
3361 * post in the FreeRTOS support forum before reporting this as a bug! -
3362 * https://goo.gl/wu4acr */
3364 /* portSOFTWARE_BARRIER() is only implemented for emulated/simulated ports that
3365 * do not otherwise exhibit real time behaviour. */
3366 portSOFTWARE_BARRIER();
3368 /* The scheduler is suspended if uxSchedulerSuspended is non-zero. An increment
3369 * is used to allow calls to vTaskSuspendAll() to nest. */
3370 ++uxSchedulerSuspended;
3372 /* Enforces ordering for ports and optimised compilers that may otherwise place
3373 * the above increment elsewhere. */
3374 portMEMORY_BARRIER();
3376 #else /* #if ( configNUMBER_OF_CORES == 1 ) */
3378 UBaseType_t ulState;
3380 /* This must only be called from within a task. */
3381 portASSERT_IF_IN_ISR();
3383 if( xSchedulerRunning != pdFALSE )
3385 /* Writes to uxSchedulerSuspended must be protected by both the task AND ISR locks.
3386 * We must disable interrupts before we grab the locks in the event that this task is
3387 * interrupted and switches context before incrementing uxSchedulerSuspended.
3388 * It is safe to re-enable interrupts after releasing the ISR lock and incrementing
3389 * uxSchedulerSuspended since that will prevent context switches. */
3390 ulState = portSET_INTERRUPT_MASK();
3392 /* portSOFRWARE_BARRIER() is only implemented for emulated/simulated ports that
3393 * do not otherwise exhibit real time behaviour. */
3394 portSOFTWARE_BARRIER();
3396 portGET_TASK_LOCK();
3398 /* uxSchedulerSuspended is increased after prvCheckForRunStateChange. The
3399 * purpose is to prevent altering the variable when fromISR APIs are readying
3401 if( uxSchedulerSuspended == 0U )
3403 if( portGET_CRITICAL_NESTING_COUNT() == 0U )
3405 prvCheckForRunStateChange();
3409 mtCOVERAGE_TEST_MARKER();
3414 mtCOVERAGE_TEST_MARKER();
3419 /* The scheduler is suspended if uxSchedulerSuspended is non-zero. An increment
3420 * is used to allow calls to vTaskSuspendAll() to nest. */
3421 ++uxSchedulerSuspended;
3422 portRELEASE_ISR_LOCK();
3424 portCLEAR_INTERRUPT_MASK( ulState );
3428 mtCOVERAGE_TEST_MARKER();
3431 #endif /* #if ( configNUMBER_OF_CORES == 1 ) */
3434 /*----------------------------------------------------------*/
3436 #if ( configUSE_TICKLESS_IDLE != 0 )
3438 static TickType_t prvGetExpectedIdleTime( void )
3441 UBaseType_t uxHigherPriorityReadyTasks = pdFALSE;
3443 /* uxHigherPriorityReadyTasks takes care of the case where
3444 * configUSE_PREEMPTION is 0, so there may be tasks above the idle priority
3445 * task that are in the Ready state, even though the idle task is
3447 #if ( configUSE_PORT_OPTIMISED_TASK_SELECTION == 0 )
3449 if( uxTopReadyPriority > tskIDLE_PRIORITY )
3451 uxHigherPriorityReadyTasks = pdTRUE;
3456 const UBaseType_t uxLeastSignificantBit = ( UBaseType_t ) 0x01;
3458 /* When port optimised task selection is used the uxTopReadyPriority
3459 * variable is used as a bit map. If bits other than the least
3460 * significant bit are set then there are tasks that have a priority
3461 * above the idle priority that are in the Ready state. This takes
3462 * care of the case where the co-operative scheduler is in use. */
3463 if( uxTopReadyPriority > uxLeastSignificantBit )
3465 uxHigherPriorityReadyTasks = pdTRUE;
3468 #endif /* if ( configUSE_PORT_OPTIMISED_TASK_SELECTION == 0 ) */
3470 if( pxCurrentTCB->uxPriority > tskIDLE_PRIORITY )
3474 else if( listCURRENT_LIST_LENGTH( &( pxReadyTasksLists[ tskIDLE_PRIORITY ] ) ) > 1 )
3476 /* There are other idle priority tasks in the ready state. If
3477 * time slicing is used then the very next tick interrupt must be
3481 else if( uxHigherPriorityReadyTasks != pdFALSE )
3483 /* There are tasks in the Ready state that have a priority above the
3484 * idle priority. This path can only be reached if
3485 * configUSE_PREEMPTION is 0. */
3490 xReturn = xNextTaskUnblockTime - xTickCount;
3496 #endif /* configUSE_TICKLESS_IDLE */
3497 /*----------------------------------------------------------*/
3499 BaseType_t xTaskResumeAll( void )
3501 TCB_t * pxTCB = NULL;
3502 BaseType_t xAlreadyYielded = pdFALSE;
3504 #if ( configNUMBER_OF_CORES > 1 )
3505 if( xSchedulerRunning != pdFALSE )
3508 /* It is possible that an ISR caused a task to be removed from an event
3509 * list while the scheduler was suspended. If this was the case then the
3510 * removed task will have been added to the xPendingReadyList. Once the
3511 * scheduler has been resumed it is safe to move all the pending ready
3512 * tasks from this list into their appropriate ready list. */
3513 taskENTER_CRITICAL();
3516 xCoreID = ( BaseType_t ) portGET_CORE_ID();
3518 /* If uxSchedulerSuspended is zero then this function does not match a
3519 * previous call to vTaskSuspendAll(). */
3520 configASSERT( uxSchedulerSuspended != 0U );
3522 --uxSchedulerSuspended;
3523 portRELEASE_TASK_LOCK();
3525 if( uxSchedulerSuspended == ( UBaseType_t ) 0U )
3527 if( uxCurrentNumberOfTasks > ( UBaseType_t ) 0U )
3529 /* Move any readied tasks from the pending list into the
3530 * appropriate ready list. */
3531 while( listLIST_IS_EMPTY( &xPendingReadyList ) == pdFALSE )
3533 pxTCB = listGET_OWNER_OF_HEAD_ENTRY( ( &xPendingReadyList ) ); /*lint !e9079 void * is used as this macro is used with timers too. Alignment is known to be fine as the type of the pointer stored and retrieved is the same. */
3534 listREMOVE_ITEM( &( pxTCB->xEventListItem ) );
3535 portMEMORY_BARRIER();
3536 listREMOVE_ITEM( &( pxTCB->xStateListItem ) );
3537 prvAddTaskToReadyList( pxTCB );
3539 #if ( configNUMBER_OF_CORES == 1 )
3541 /* If the moved task has a priority higher than the current
3542 * task then a yield must be performed. */
3543 if( pxTCB->uxPriority > pxCurrentTCB->uxPriority )
3545 xYieldPendings[ xCoreID ] = pdTRUE;
3549 mtCOVERAGE_TEST_MARKER();
3552 #else /* #if ( configNUMBER_OF_CORES == 1 ) */
3554 /* All appropriate tasks yield at the moment a task is added to xPendingReadyList.
3555 * If the current core yielded then vTaskSwitchContext() has already been called
3556 * which sets xYieldPendings for the current core to pdTRUE. */
3558 #endif /* #if ( configNUMBER_OF_CORES == 1 ) */
3563 /* A task was unblocked while the scheduler was suspended,
3564 * which may have prevented the next unblock time from being
3565 * re-calculated, in which case re-calculate it now. Mainly
3566 * important for low power tickless implementations, where
3567 * this can prevent an unnecessary exit from low power
3569 prvResetNextTaskUnblockTime();
3572 /* If any ticks occurred while the scheduler was suspended then
3573 * they should be processed now. This ensures the tick count does
3574 * not slip, and that any delayed tasks are resumed at the correct
3577 * It should be safe to call xTaskIncrementTick here from any core
3578 * since we are in a critical section and xTaskIncrementTick itself
3579 * protects itself within a critical section. Suspending the scheduler
3580 * from any core causes xTaskIncrementTick to increment uxPendedCounts. */
3582 TickType_t xPendedCounts = xPendedTicks; /* Non-volatile copy. */
3584 if( xPendedCounts > ( TickType_t ) 0U )
3588 if( xTaskIncrementTick() != pdFALSE )
3590 /* Other cores are interrupted from
3591 * within xTaskIncrementTick(). */
3592 xYieldPendings[ xCoreID ] = pdTRUE;
3596 mtCOVERAGE_TEST_MARKER();
3600 } while( xPendedCounts > ( TickType_t ) 0U );
3606 mtCOVERAGE_TEST_MARKER();
3610 if( xYieldPendings[ xCoreID ] != pdFALSE )
3612 #if ( configUSE_PREEMPTION != 0 )
3614 xAlreadyYielded = pdTRUE;
3616 #endif /* #if ( configUSE_PREEMPTION != 0 ) */
3618 #if ( configNUMBER_OF_CORES == 1 )
3620 taskYIELD_IF_USING_PREEMPTION();
3622 #endif /* #if ( configNUMBER_OF_CORES == 1 ) */
3626 mtCOVERAGE_TEST_MARKER();
3632 mtCOVERAGE_TEST_MARKER();
3635 taskEXIT_CRITICAL();
3638 return xAlreadyYielded;
3640 /*-----------------------------------------------------------*/
3642 TickType_t xTaskGetTickCount( void )
3646 /* Critical section required if running on a 16 bit processor. */
3647 portTICK_TYPE_ENTER_CRITICAL();
3649 xTicks = xTickCount;
3651 portTICK_TYPE_EXIT_CRITICAL();
3655 /*-----------------------------------------------------------*/
3657 TickType_t xTaskGetTickCountFromISR( void )
3660 UBaseType_t uxSavedInterruptStatus;
3662 /* RTOS ports that support interrupt nesting have the concept of a maximum
3663 * system call (or maximum API call) interrupt priority. Interrupts that are
3664 * above the maximum system call priority are kept permanently enabled, even
3665 * when the RTOS kernel is in a critical section, but cannot make any calls to
3666 * FreeRTOS API functions. If configASSERT() is defined in FreeRTOSConfig.h
3667 * then portASSERT_IF_INTERRUPT_PRIORITY_INVALID() will result in an assertion
3668 * failure if a FreeRTOS API function is called from an interrupt that has been
3669 * assigned a priority above the configured maximum system call priority.
3670 * Only FreeRTOS functions that end in FromISR can be called from interrupts
3671 * that have been assigned a priority at or (logically) below the maximum
3672 * system call interrupt priority. FreeRTOS maintains a separate interrupt
3673 * safe API to ensure interrupt entry is as fast and as simple as possible.
3674 * More information (albeit Cortex-M specific) is provided on the following
3675 * link: https://www.FreeRTOS.org/RTOS-Cortex-M3-M4.html */
3676 portASSERT_IF_INTERRUPT_PRIORITY_INVALID();
3678 uxSavedInterruptStatus = portTICK_TYPE_SET_INTERRUPT_MASK_FROM_ISR();
3680 xReturn = xTickCount;
3682 portTICK_TYPE_CLEAR_INTERRUPT_MASK_FROM_ISR( uxSavedInterruptStatus );
3686 /*-----------------------------------------------------------*/
3688 UBaseType_t uxTaskGetNumberOfTasks( void )
3690 /* A critical section is not required because the variables are of type
3692 return uxCurrentNumberOfTasks;
3694 /*-----------------------------------------------------------*/
3696 char * pcTaskGetName( TaskHandle_t xTaskToQuery ) /*lint !e971 Unqualified char types are allowed for strings and single characters only. */
3700 /* If null is passed in here then the name of the calling task is being
3702 pxTCB = prvGetTCBFromHandle( xTaskToQuery );
3703 configASSERT( pxTCB );
3704 return &( pxTCB->pcTaskName[ 0 ] );
3706 /*-----------------------------------------------------------*/
3708 #if ( INCLUDE_xTaskGetHandle == 1 )
3710 #if ( configNUMBER_OF_CORES == 1 )
3711 static TCB_t * prvSearchForNameWithinSingleList( List_t * pxList,
3712 const char pcNameToQuery[] )
3716 TCB_t * pxReturn = NULL;
3719 BaseType_t xBreakLoop;
3721 /* This function is called with the scheduler suspended. */
3723 if( listCURRENT_LIST_LENGTH( pxList ) > ( UBaseType_t ) 0 )
3725 listGET_OWNER_OF_NEXT_ENTRY( pxFirstTCB, pxList ); /*lint !e9079 void * is used as this macro is used with timers too. Alignment is known to be fine as the type of the pointer stored and retrieved is the same. */
3729 listGET_OWNER_OF_NEXT_ENTRY( pxNextTCB, pxList ); /*lint !e9079 void * is used as this macro is used with timers too. Alignment is known to be fine as the type of the pointer stored and retrieved is the same. */
3731 /* Check each character in the name looking for a match or
3733 xBreakLoop = pdFALSE;
3735 for( x = ( UBaseType_t ) 0; x < ( UBaseType_t ) configMAX_TASK_NAME_LEN; x++ )
3737 cNextChar = pxNextTCB->pcTaskName[ x ];
3739 if( cNextChar != pcNameToQuery[ x ] )
3741 /* Characters didn't match. */
3742 xBreakLoop = pdTRUE;
3744 else if( cNextChar == ( char ) 0x00 )
3746 /* Both strings terminated, a match must have been
3748 pxReturn = pxNextTCB;
3749 xBreakLoop = pdTRUE;
3753 mtCOVERAGE_TEST_MARKER();
3756 if( xBreakLoop != pdFALSE )
3762 if( pxReturn != NULL )
3764 /* The handle has been found. */
3767 } while( pxNextTCB != pxFirstTCB );
3771 mtCOVERAGE_TEST_MARKER();
3776 #else /* if ( configNUMBER_OF_CORES == 1 ) */
3777 static TCB_t * prvSearchForNameWithinSingleList( List_t * pxList,
3778 const char pcNameToQuery[] )
3780 TCB_t * pxReturn = NULL;
3783 BaseType_t xBreakLoop;
3784 const ListItem_t * pxEndMarker = listGET_END_MARKER( pxList );
3785 ListItem_t * pxIterator;
3787 /* This function is called with the scheduler suspended. */
3789 if( listCURRENT_LIST_LENGTH( pxList ) > ( UBaseType_t ) 0 )
3791 for( pxIterator = listGET_HEAD_ENTRY( pxList ); pxIterator != pxEndMarker; pxIterator = listGET_NEXT( pxIterator ) )
3793 TCB_t * pxTCB = listGET_LIST_ITEM_OWNER( pxIterator );
3795 /* Check each character in the name looking for a match or
3797 xBreakLoop = pdFALSE;
3799 for( x = ( UBaseType_t ) 0; x < ( UBaseType_t ) configMAX_TASK_NAME_LEN; x++ )
3801 cNextChar = pxTCB->pcTaskName[ x ];
3803 if( cNextChar != pcNameToQuery[ x ] )
3805 /* Characters didn't match. */
3806 xBreakLoop = pdTRUE;
3808 else if( cNextChar == ( char ) 0x00 )
3810 /* Both strings terminated, a match must have been
3813 xBreakLoop = pdTRUE;
3817 mtCOVERAGE_TEST_MARKER();
3820 if( xBreakLoop != pdFALSE )
3826 if( pxReturn != NULL )
3828 /* The handle has been found. */
3835 mtCOVERAGE_TEST_MARKER();
3840 #endif /* #if ( configNUMBER_OF_CORES == 1 ) */
3842 #endif /* INCLUDE_xTaskGetHandle */
3843 /*-----------------------------------------------------------*/
3845 #if ( INCLUDE_xTaskGetHandle == 1 )
3847 TaskHandle_t xTaskGetHandle( const char * pcNameToQuery ) /*lint !e971 Unqualified char types are allowed for strings and single characters only. */
3849 UBaseType_t uxQueue = configMAX_PRIORITIES;
3852 /* Task names will be truncated to configMAX_TASK_NAME_LEN - 1 bytes. */
3853 configASSERT( strlen( pcNameToQuery ) < configMAX_TASK_NAME_LEN );
3857 /* Search the ready lists. */
3861 pxTCB = prvSearchForNameWithinSingleList( ( List_t * ) &( pxReadyTasksLists[ uxQueue ] ), pcNameToQuery );
3865 /* Found the handle. */
3868 } while( uxQueue > ( UBaseType_t ) tskIDLE_PRIORITY ); /*lint !e961 MISRA exception as the casts are only redundant for some ports. */
3870 /* Search the delayed lists. */
3873 pxTCB = prvSearchForNameWithinSingleList( ( List_t * ) pxDelayedTaskList, pcNameToQuery );
3878 pxTCB = prvSearchForNameWithinSingleList( ( List_t * ) pxOverflowDelayedTaskList, pcNameToQuery );
3881 #if ( INCLUDE_vTaskSuspend == 1 )
3885 /* Search the suspended list. */
3886 pxTCB = prvSearchForNameWithinSingleList( &xSuspendedTaskList, pcNameToQuery );
3891 #if ( INCLUDE_vTaskDelete == 1 )
3895 /* Search the deleted list. */
3896 pxTCB = prvSearchForNameWithinSingleList( &xTasksWaitingTermination, pcNameToQuery );
3901 ( void ) xTaskResumeAll();
3906 #endif /* INCLUDE_xTaskGetHandle */
3907 /*-----------------------------------------------------------*/
3909 #if ( configSUPPORT_STATIC_ALLOCATION == 1 )
3911 BaseType_t xTaskGetStaticBuffers( TaskHandle_t xTask,
3912 StackType_t ** ppuxStackBuffer,
3913 StaticTask_t ** ppxTaskBuffer )
3918 configASSERT( ppuxStackBuffer != NULL );
3919 configASSERT( ppxTaskBuffer != NULL );
3921 pxTCB = prvGetTCBFromHandle( xTask );
3923 #if ( tskSTATIC_AND_DYNAMIC_ALLOCATION_POSSIBLE == 1 )
3925 if( pxTCB->ucStaticallyAllocated == tskSTATICALLY_ALLOCATED_STACK_AND_TCB )
3927 *ppuxStackBuffer = pxTCB->pxStack;
3928 *ppxTaskBuffer = ( StaticTask_t * ) pxTCB;
3931 else if( pxTCB->ucStaticallyAllocated == tskSTATICALLY_ALLOCATED_STACK_ONLY )
3933 *ppuxStackBuffer = pxTCB->pxStack;
3934 *ppxTaskBuffer = NULL;
3942 #else /* tskSTATIC_AND_DYNAMIC_ALLOCATION_POSSIBLE == 1 */
3944 *ppuxStackBuffer = pxTCB->pxStack;
3945 *ppxTaskBuffer = ( StaticTask_t * ) pxTCB;
3948 #endif /* tskSTATIC_AND_DYNAMIC_ALLOCATION_POSSIBLE == 1 */
3953 #endif /* configSUPPORT_STATIC_ALLOCATION */
3954 /*-----------------------------------------------------------*/
3956 #if ( configUSE_TRACE_FACILITY == 1 )
3958 UBaseType_t uxTaskGetSystemState( TaskStatus_t * const pxTaskStatusArray,
3959 const UBaseType_t uxArraySize,
3960 configRUN_TIME_COUNTER_TYPE * const pulTotalRunTime )
3962 UBaseType_t uxTask = 0, uxQueue = configMAX_PRIORITIES;
3966 /* Is there a space in the array for each task in the system? */
3967 if( uxArraySize >= uxCurrentNumberOfTasks )
3969 /* Fill in an TaskStatus_t structure with information on each
3970 * task in the Ready state. */
3974 uxTask = ( UBaseType_t ) ( uxTask + prvListTasksWithinSingleList( &( pxTaskStatusArray[ uxTask ] ), &( pxReadyTasksLists[ uxQueue ] ), eReady ) );
3975 } while( uxQueue > ( UBaseType_t ) tskIDLE_PRIORITY ); /*lint !e961 MISRA exception as the casts are only redundant for some ports. */
3977 /* Fill in an TaskStatus_t structure with information on each
3978 * task in the Blocked state. */
3979 uxTask = ( UBaseType_t ) ( uxTask + prvListTasksWithinSingleList( &( pxTaskStatusArray[ uxTask ] ), ( List_t * ) pxDelayedTaskList, eBlocked ) );
3980 uxTask = ( UBaseType_t ) ( uxTask + prvListTasksWithinSingleList( &( pxTaskStatusArray[ uxTask ] ), ( List_t * ) pxOverflowDelayedTaskList, eBlocked ) );
3982 #if ( INCLUDE_vTaskDelete == 1 )
3984 /* Fill in an TaskStatus_t structure with information on
3985 * each task that has been deleted but not yet cleaned up. */
3986 uxTask = ( UBaseType_t ) ( uxTask + prvListTasksWithinSingleList( &( pxTaskStatusArray[ uxTask ] ), &xTasksWaitingTermination, eDeleted ) );
3990 #if ( INCLUDE_vTaskSuspend == 1 )
3992 /* Fill in an TaskStatus_t structure with information on
3993 * each task in the Suspended state. */
3994 uxTask = ( UBaseType_t ) ( uxTask + prvListTasksWithinSingleList( &( pxTaskStatusArray[ uxTask ] ), &xSuspendedTaskList, eSuspended ) );
3998 #if ( configGENERATE_RUN_TIME_STATS == 1 )
4000 if( pulTotalRunTime != NULL )
4002 #ifdef portALT_GET_RUN_TIME_COUNTER_VALUE
4003 portALT_GET_RUN_TIME_COUNTER_VALUE( ( *pulTotalRunTime ) );
4005 *pulTotalRunTime = ( configRUN_TIME_COUNTER_TYPE ) portGET_RUN_TIME_COUNTER_VALUE();
4009 #else /* if ( configGENERATE_RUN_TIME_STATS == 1 ) */
4011 if( pulTotalRunTime != NULL )
4013 *pulTotalRunTime = 0;
4016 #endif /* if ( configGENERATE_RUN_TIME_STATS == 1 ) */
4020 mtCOVERAGE_TEST_MARKER();
4023 ( void ) xTaskResumeAll();
4028 #endif /* configUSE_TRACE_FACILITY */
4029 /*----------------------------------------------------------*/
4031 #if ( INCLUDE_xTaskGetIdleTaskHandle == 1 )
4033 /* SMP_TODO : This function returns only idle task handle for core 0.
4034 * Consider to add another function to return the idle task handles. */
4035 TaskHandle_t xTaskGetIdleTaskHandle( void )
4037 /* If xTaskGetIdleTaskHandle() is called before the scheduler has been
4038 * started, then xIdleTaskHandles will be NULL. */
4039 configASSERT( ( xIdleTaskHandles[ 0 ] != NULL ) );
4040 return xIdleTaskHandles[ 0 ];
4043 #endif /* INCLUDE_xTaskGetIdleTaskHandle */
4044 /*----------------------------------------------------------*/
4046 /* This conditional compilation should use inequality to 0, not equality to 1.
4047 * This is to ensure vTaskStepTick() is available when user defined low power mode
4048 * implementations require configUSE_TICKLESS_IDLE to be set to a value other than
4050 #if ( configUSE_TICKLESS_IDLE != 0 )
4052 void vTaskStepTick( TickType_t xTicksToJump )
4054 /* Correct the tick count value after a period during which the tick
4055 * was suppressed. Note this does *not* call the tick hook function for
4056 * each stepped tick. */
4057 configASSERT( ( xTickCount + xTicksToJump ) <= xNextTaskUnblockTime );
4059 if( ( xTickCount + xTicksToJump ) == xNextTaskUnblockTime )
4061 /* Arrange for xTickCount to reach xNextTaskUnblockTime in
4062 * xTaskIncrementTick() when the scheduler resumes. This ensures
4063 * that any delayed tasks are resumed at the correct time. */
4064 configASSERT( uxSchedulerSuspended != ( UBaseType_t ) 0U );
4065 configASSERT( xTicksToJump != ( TickType_t ) 0 );
4067 /* Prevent the tick interrupt modifying xPendedTicks simultaneously. */
4068 taskENTER_CRITICAL();
4072 taskEXIT_CRITICAL();
4077 mtCOVERAGE_TEST_MARKER();
4080 xTickCount += xTicksToJump;
4081 traceINCREASE_TICK_COUNT( xTicksToJump );
4084 #endif /* configUSE_TICKLESS_IDLE */
4085 /*----------------------------------------------------------*/
4087 BaseType_t xTaskCatchUpTicks( TickType_t xTicksToCatchUp )
4089 BaseType_t xYieldOccurred;
4091 /* Must not be called with the scheduler suspended as the implementation
4092 * relies on xPendedTicks being wound down to 0 in xTaskResumeAll(). */
4093 configASSERT( uxSchedulerSuspended == ( UBaseType_t ) 0U );
4095 /* Use xPendedTicks to mimic xTicksToCatchUp number of ticks occurring when
4096 * the scheduler is suspended so the ticks are executed in xTaskResumeAll(). */
4099 /* Prevent the tick interrupt modifying xPendedTicks simultaneously. */
4100 taskENTER_CRITICAL();
4102 xPendedTicks += xTicksToCatchUp;
4104 taskEXIT_CRITICAL();
4105 xYieldOccurred = xTaskResumeAll();
4107 return xYieldOccurred;
4109 /*----------------------------------------------------------*/
4111 #if ( INCLUDE_xTaskAbortDelay == 1 )
4113 BaseType_t xTaskAbortDelay( TaskHandle_t xTask )
4115 TCB_t * pxTCB = xTask;
4118 configASSERT( pxTCB );
4122 /* A task can only be prematurely removed from the Blocked state if
4123 * it is actually in the Blocked state. */
4124 if( eTaskGetState( xTask ) == eBlocked )
4128 /* Remove the reference to the task from the blocked list. An
4129 * interrupt won't touch the xStateListItem because the
4130 * scheduler is suspended. */
4131 ( void ) uxListRemove( &( pxTCB->xStateListItem ) );
4133 /* Is the task waiting on an event also? If so remove it from
4134 * the event list too. Interrupts can touch the event list item,
4135 * even though the scheduler is suspended, so a critical section
4137 taskENTER_CRITICAL();
4139 if( listLIST_ITEM_CONTAINER( &( pxTCB->xEventListItem ) ) != NULL )
4141 ( void ) uxListRemove( &( pxTCB->xEventListItem ) );
4143 /* This lets the task know it was forcibly removed from the
4144 * blocked state so it should not re-evaluate its block time and
4145 * then block again. */
4146 pxTCB->ucDelayAborted = pdTRUE;
4150 mtCOVERAGE_TEST_MARKER();
4153 taskEXIT_CRITICAL();
4155 /* Place the unblocked task into the appropriate ready list. */
4156 prvAddTaskToReadyList( pxTCB );
4158 /* A task being unblocked cannot cause an immediate context
4159 * switch if preemption is turned off. */
4160 #if ( configUSE_PREEMPTION == 1 )
4162 #if ( configNUMBER_OF_CORES == 1 )
4164 /* Preemption is on, but a context switch should only be
4165 * performed if the unblocked task has a priority that is
4166 * higher than the currently executing task. */
4167 if( pxTCB->uxPriority > pxCurrentTCB->uxPriority )
4169 /* Pend the yield to be performed when the scheduler
4170 * is unsuspended. */
4171 xYieldPendings[ 0 ] = pdTRUE;
4175 mtCOVERAGE_TEST_MARKER();
4178 #else /* #if ( configNUMBER_OF_CORES == 1 ) */
4180 taskENTER_CRITICAL();
4182 prvYieldForTask( pxTCB );
4184 taskEXIT_CRITICAL();
4186 #endif /* #if ( configNUMBER_OF_CORES == 1 ) */
4188 #endif /* #if ( configUSE_PREEMPTION == 1 ) */
4195 ( void ) xTaskResumeAll();
4200 #endif /* INCLUDE_xTaskAbortDelay */
4201 /*----------------------------------------------------------*/
4203 BaseType_t xTaskIncrementTick( void )
4206 TickType_t xItemValue;
4207 BaseType_t xSwitchRequired = pdFALSE;
4209 #if ( configUSE_PREEMPTION == 1 ) && ( configNUMBER_OF_CORES > 1 )
4210 BaseType_t xYieldRequiredForCore[ configNUMBER_OF_CORES ] = { pdFALSE };
4211 #endif /* #if ( configUSE_PREEMPTION == 1 ) && ( configNUMBER_OF_CORES > 1 ) */
4213 /* Called by the portable layer each time a tick interrupt occurs.
4214 * Increments the tick then checks to see if the new tick value will cause any
4215 * tasks to be unblocked. */
4216 traceTASK_INCREMENT_TICK( xTickCount );
4218 /* Tick increment should occur on every kernel timer event. Core 0 has the
4219 * responsibility to increment the tick, or increment the pended ticks if the
4220 * scheduler is suspended. If pended ticks is greater than zero, the core that
4221 * calls xTaskResumeAll has the responsibility to increment the tick. */
4222 if( uxSchedulerSuspended == ( UBaseType_t ) 0U )
4224 /* Minor optimisation. The tick count cannot change in this
4226 const TickType_t xConstTickCount = xTickCount + ( TickType_t ) 1;
4228 /* Increment the RTOS tick, switching the delayed and overflowed
4229 * delayed lists if it wraps to 0. */
4230 xTickCount = xConstTickCount;
4232 if( xConstTickCount == ( TickType_t ) 0U ) /*lint !e774 'if' does not always evaluate to false as it is looking for an overflow. */
4234 taskSWITCH_DELAYED_LISTS();
4238 mtCOVERAGE_TEST_MARKER();
4241 /* See if this tick has made a timeout expire. Tasks are stored in
4242 * the queue in the order of their wake time - meaning once one task
4243 * has been found whose block time has not expired there is no need to
4244 * look any further down the list. */
4245 if( xConstTickCount >= xNextTaskUnblockTime )
4249 if( listLIST_IS_EMPTY( pxDelayedTaskList ) != pdFALSE )
4251 /* The delayed list is empty. Set xNextTaskUnblockTime
4252 * to the maximum possible value so it is extremely
4254 * if( xTickCount >= xNextTaskUnblockTime ) test will pass
4255 * next time through. */
4256 xNextTaskUnblockTime = portMAX_DELAY; /*lint !e961 MISRA exception as the casts are only redundant for some ports. */
4261 /* The delayed list is not empty, get the value of the
4262 * item at the head of the delayed list. This is the time
4263 * at which the task at the head of the delayed list must
4264 * be removed from the Blocked state. */
4265 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. */
4266 xItemValue = listGET_LIST_ITEM_VALUE( &( pxTCB->xStateListItem ) );
4268 if( xConstTickCount < xItemValue )
4270 /* It is not time to unblock this item yet, but the
4271 * item value is the time at which the task at the head
4272 * of the blocked list must be removed from the Blocked
4273 * state - so record the item value in
4274 * xNextTaskUnblockTime. */
4275 xNextTaskUnblockTime = xItemValue;
4276 break; /*lint !e9011 Code structure here is deemed easier to understand with multiple breaks. */
4280 mtCOVERAGE_TEST_MARKER();
4283 /* It is time to remove the item from the Blocked state. */
4284 listREMOVE_ITEM( &( pxTCB->xStateListItem ) );
4286 /* Is the task waiting on an event also? If so remove
4287 * it from the event list. */
4288 if( listLIST_ITEM_CONTAINER( &( pxTCB->xEventListItem ) ) != NULL )
4290 listREMOVE_ITEM( &( pxTCB->xEventListItem ) );
4294 mtCOVERAGE_TEST_MARKER();
4297 /* Place the unblocked task into the appropriate ready
4299 prvAddTaskToReadyList( pxTCB );
4301 /* A task being unblocked cannot cause an immediate
4302 * context switch if preemption is turned off. */
4303 #if ( configUSE_PREEMPTION == 1 )
4305 #if ( configNUMBER_OF_CORES == 1 )
4307 /* Preemption is on, but a context switch should
4308 * only be performed if the unblocked task's
4309 * priority is higher than the currently executing
4311 * The case of equal priority tasks sharing
4312 * processing time (which happens when both
4313 * preemption and time slicing are on) is
4315 if( pxTCB->uxPriority > pxCurrentTCB->uxPriority )
4317 xSwitchRequired = pdTRUE;
4321 mtCOVERAGE_TEST_MARKER();
4324 #else /* #if( configNUMBER_OF_CORES == 1 ) */
4326 prvYieldForTask( pxTCB );
4328 #endif /* #if( configNUMBER_OF_CORES == 1 ) */
4330 #endif /* #if ( configUSE_PREEMPTION == 1 ) */
4335 /* Tasks of equal priority to the currently running task will share
4336 * processing time (time slice) if preemption is on, and the application
4337 * writer has not explicitly turned time slicing off. */
4338 #if ( ( configUSE_PREEMPTION == 1 ) && ( configUSE_TIME_SLICING == 1 ) )
4340 #if ( configNUMBER_OF_CORES == 1 )
4342 if( listCURRENT_LIST_LENGTH( &( pxReadyTasksLists[ pxCurrentTCB->uxPriority ] ) ) > ( UBaseType_t ) 1 )
4344 xSwitchRequired = pdTRUE;
4348 mtCOVERAGE_TEST_MARKER();
4351 #else /* #if ( configNUMBER_OF_CORES == 1 ) */
4355 for( xCoreID = 0; xCoreID < ( ( BaseType_t ) configNUMBER_OF_CORES ); xCoreID++ )
4357 if( listCURRENT_LIST_LENGTH( &( pxReadyTasksLists[ pxCurrentTCBs[ xCoreID ]->uxPriority ] ) ) > 1 )
4359 xYieldRequiredForCore[ xCoreID ] = pdTRUE;
4363 mtCOVERAGE_TEST_MARKER();
4367 #endif /* #if ( configNUMBER_OF_CORES == 1 ) */
4369 #endif /* #if ( ( configUSE_PREEMPTION == 1 ) && ( configUSE_TIME_SLICING == 1 ) ) */
4371 #if ( configUSE_TICK_HOOK == 1 )
4373 /* Guard against the tick hook being called when the pended tick
4374 * count is being unwound (when the scheduler is being unlocked). */
4375 if( xPendedTicks == ( TickType_t ) 0 )
4377 vApplicationTickHook();
4381 mtCOVERAGE_TEST_MARKER();
4384 #endif /* configUSE_TICK_HOOK */
4386 #if ( configUSE_PREEMPTION == 1 )
4388 #if ( configNUMBER_OF_CORES == 1 )
4390 /* For single core the core ID is always 0. */
4391 if( xYieldPendings[ 0 ] != pdFALSE )
4393 xSwitchRequired = pdTRUE;
4397 mtCOVERAGE_TEST_MARKER();
4400 #else /* #if ( configNUMBER_OF_CORES == 1 ) */
4402 BaseType_t xCoreID, xCurrentCoreID;
4403 xCurrentCoreID = ( BaseType_t ) portGET_CORE_ID();
4405 for( xCoreID = 0; xCoreID < ( BaseType_t ) configNUMBER_OF_CORES; xCoreID++ )
4407 #if ( configUSE_TASK_PREEMPTION_DISABLE == 1 )
4408 if( pxCurrentTCBs[ xCoreID ]->xPreemptionDisable == pdFALSE )
4411 if( ( xYieldRequiredForCore[ xCoreID ] != pdFALSE ) || ( xYieldPendings[ xCoreID ] != pdFALSE ) )
4413 if( xCoreID == xCurrentCoreID )
4415 xSwitchRequired = pdTRUE;
4419 prvYieldCore( xCoreID );
4424 mtCOVERAGE_TEST_MARKER();
4429 #endif /* #if ( configNUMBER_OF_CORES == 1 ) */
4431 #endif /* #if ( configUSE_PREEMPTION == 1 ) */
4437 /* The tick hook gets called at regular intervals, even if the
4438 * scheduler is locked. */
4439 #if ( configUSE_TICK_HOOK == 1 )
4441 vApplicationTickHook();
4446 return xSwitchRequired;
4448 /*-----------------------------------------------------------*/
4450 #if ( configUSE_APPLICATION_TASK_TAG == 1 )
4452 void vTaskSetApplicationTaskTag( TaskHandle_t xTask,
4453 TaskHookFunction_t pxHookFunction )
4457 /* If xTask is NULL then it is the task hook of the calling task that is
4461 xTCB = ( TCB_t * ) pxCurrentTCB;
4468 /* Save the hook function in the TCB. A critical section is required as
4469 * the value can be accessed from an interrupt. */
4470 taskENTER_CRITICAL();
4472 xTCB->pxTaskTag = pxHookFunction;
4474 taskEXIT_CRITICAL();
4477 #endif /* configUSE_APPLICATION_TASK_TAG */
4478 /*-----------------------------------------------------------*/
4480 #if ( configUSE_APPLICATION_TASK_TAG == 1 )
4482 TaskHookFunction_t xTaskGetApplicationTaskTag( TaskHandle_t xTask )
4485 TaskHookFunction_t xReturn;
4487 /* If xTask is NULL then set the calling task's hook. */
4488 pxTCB = prvGetTCBFromHandle( xTask );
4490 /* Save the hook function in the TCB. A critical section is required as
4491 * the value can be accessed from an interrupt. */
4492 taskENTER_CRITICAL();
4494 xReturn = pxTCB->pxTaskTag;
4496 taskEXIT_CRITICAL();
4501 #endif /* configUSE_APPLICATION_TASK_TAG */
4502 /*-----------------------------------------------------------*/
4504 #if ( configUSE_APPLICATION_TASK_TAG == 1 )
4506 TaskHookFunction_t xTaskGetApplicationTaskTagFromISR( TaskHandle_t xTask )
4509 TaskHookFunction_t xReturn;
4510 UBaseType_t uxSavedInterruptStatus;
4512 /* If xTask is NULL then set the calling task's hook. */
4513 pxTCB = prvGetTCBFromHandle( xTask );
4515 /* Save the hook function in the TCB. A critical section is required as
4516 * the value can be accessed from an interrupt. */
4517 uxSavedInterruptStatus = taskENTER_CRITICAL_FROM_ISR();
4519 xReturn = pxTCB->pxTaskTag;
4521 taskEXIT_CRITICAL_FROM_ISR( uxSavedInterruptStatus );
4526 #endif /* configUSE_APPLICATION_TASK_TAG */
4527 /*-----------------------------------------------------------*/
4529 #if ( configUSE_APPLICATION_TASK_TAG == 1 )
4531 BaseType_t xTaskCallApplicationTaskHook( TaskHandle_t xTask,
4532 void * pvParameter )
4537 /* If xTask is NULL then we are calling our own task hook. */
4540 xTCB = pxCurrentTCB;
4547 if( xTCB->pxTaskTag != NULL )
4549 xReturn = xTCB->pxTaskTag( pvParameter );
4559 #endif /* configUSE_APPLICATION_TASK_TAG */
4560 /*-----------------------------------------------------------*/
4562 #if ( configNUMBER_OF_CORES == 1 )
4563 void vTaskSwitchContext( void )
4565 if( uxSchedulerSuspended != ( UBaseType_t ) 0U )
4567 /* The scheduler is currently suspended - do not allow a context
4569 xYieldPendings[ 0 ] = pdTRUE;
4573 xYieldPendings[ 0 ] = pdFALSE;
4574 traceTASK_SWITCHED_OUT();
4576 #if ( configGENERATE_RUN_TIME_STATS == 1 )
4578 #ifdef portALT_GET_RUN_TIME_COUNTER_VALUE
4579 portALT_GET_RUN_TIME_COUNTER_VALUE( ulTotalRunTime[ 0 ] );
4581 ulTotalRunTime[ 0 ] = portGET_RUN_TIME_COUNTER_VALUE();
4584 /* Add the amount of time the task has been running to the
4585 * accumulated time so far. The time the task started running was
4586 * stored in ulTaskSwitchedInTime. Note that there is no overflow
4587 * protection here so count values are only valid until the timer
4588 * overflows. The guard against negative values is to protect
4589 * against suspect run time stat counter implementations - which
4590 * are provided by the application, not the kernel. */
4591 if( ulTotalRunTime[ 0 ] > ulTaskSwitchedInTime[ 0 ] )
4593 pxCurrentTCB->ulRunTimeCounter += ( ulTotalRunTime[ 0 ] - ulTaskSwitchedInTime[ 0 ] );
4597 mtCOVERAGE_TEST_MARKER();
4600 ulTaskSwitchedInTime[ 0 ] = ulTotalRunTime[ 0 ];
4602 #endif /* configGENERATE_RUN_TIME_STATS */
4604 /* Check for stack overflow, if configured. */
4605 taskCHECK_FOR_STACK_OVERFLOW();
4607 /* Before the currently running task is switched out, save its errno. */
4608 #if ( configUSE_POSIX_ERRNO == 1 )
4610 pxCurrentTCB->iTaskErrno = FreeRTOS_errno;
4614 /* Select a new task to run using either the generic C or port
4615 * optimised asm code. */
4616 taskSELECT_HIGHEST_PRIORITY_TASK(); /*lint !e9079 void * is used as this macro is used with timers too. Alignment is known to be fine as the type of the pointer stored and retrieved is the same. */
4617 traceTASK_SWITCHED_IN();
4619 /* After the new task is switched in, update the global errno. */
4620 #if ( configUSE_POSIX_ERRNO == 1 )
4622 FreeRTOS_errno = pxCurrentTCB->iTaskErrno;
4626 #if ( configUSE_C_RUNTIME_TLS_SUPPORT == 1 )
4628 /* Switch C-Runtime's TLS Block to point to the TLS
4629 * Block specific to this task. */
4630 configSET_TLS_BLOCK( pxCurrentTCB->xTLSBlock );
4635 #else /* if ( configNUMBER_OF_CORES == 1 ) */
4636 void vTaskSwitchContext( BaseType_t xCoreID )
4638 /* Acquire both locks:
4639 * - The ISR lock protects the ready list from simultaneous access by
4640 * both other ISRs and tasks.
4641 * - We also take the task lock to pause here in case another core has
4642 * suspended the scheduler. We don't want to simply set xYieldPending
4643 * and move on if another core suspended the scheduler. We should only
4644 * do that if the current core has suspended the scheduler. */
4646 portGET_TASK_LOCK(); /* Must always acquire the task lock first. */
4649 /* vTaskSwitchContext() must never be called from within a critical section.
4650 * This is not necessarily true for single core FreeRTOS, but it is for this
4652 configASSERT( portGET_CRITICAL_NESTING_COUNT() == 0 );
4654 if( uxSchedulerSuspended != ( UBaseType_t ) 0U )
4656 /* The scheduler is currently suspended - do not allow a context
4658 xYieldPendings[ xCoreID ] = pdTRUE;
4662 xYieldPendings[ xCoreID ] = pdFALSE;
4663 traceTASK_SWITCHED_OUT();
4665 #if ( configGENERATE_RUN_TIME_STATS == 1 )
4667 #ifdef portALT_GET_RUN_TIME_COUNTER_VALUE
4668 portALT_GET_RUN_TIME_COUNTER_VALUE( ulTotalRunTime[ xCoreID ] );
4670 ulTotalRunTime[ xCoreID ] = portGET_RUN_TIME_COUNTER_VALUE();
4673 /* Add the amount of time the task has been running to the
4674 * accumulated time so far. The time the task started running was
4675 * stored in ulTaskSwitchedInTime. Note that there is no overflow
4676 * protection here so count values are only valid until the timer
4677 * overflows. The guard against negative values is to protect
4678 * against suspect run time stat counter implementations - which
4679 * are provided by the application, not the kernel. */
4680 if( ulTotalRunTime[ xCoreID ] > ulTaskSwitchedInTime[ xCoreID ] )
4682 pxCurrentTCB->ulRunTimeCounter += ( ulTotalRunTime[ xCoreID ] - ulTaskSwitchedInTime[ xCoreID ] );
4686 mtCOVERAGE_TEST_MARKER();
4689 ulTaskSwitchedInTime[ xCoreID ] = ulTotalRunTime[ xCoreID ];
4691 #endif /* configGENERATE_RUN_TIME_STATS */
4693 /* Check for stack overflow, if configured. */
4694 taskCHECK_FOR_STACK_OVERFLOW();
4696 /* Before the currently running task is switched out, save its errno. */
4697 #if ( configUSE_POSIX_ERRNO == 1 )
4699 pxCurrentTCB->iTaskErrno = FreeRTOS_errno;
4703 /* Select a new task to run. */
4704 prvSelectHighestPriorityTask( xCoreID );
4705 traceTASK_SWITCHED_IN();
4707 /* After the new task is switched in, update the global errno. */
4708 #if ( configUSE_POSIX_ERRNO == 1 )
4710 FreeRTOS_errno = pxCurrentTCB->iTaskErrno;
4714 #if ( configUSE_C_RUNTIME_TLS_SUPPORT == 1 )
4716 /* Switch C-Runtime's TLS Block to point to the TLS
4717 * Block specific to this task. */
4718 configSET_TLS_BLOCK( pxCurrentTCB->xTLSBlock );
4723 portRELEASE_ISR_LOCK();
4724 portRELEASE_TASK_LOCK();
4726 #endif /* if ( configNUMBER_OF_CORES > 1 ) */
4727 /*-----------------------------------------------------------*/
4729 void vTaskPlaceOnEventList( List_t * const pxEventList,
4730 const TickType_t xTicksToWait )
4732 configASSERT( pxEventList );
4734 /* THIS FUNCTION MUST BE CALLED WITH EITHER INTERRUPTS DISABLED OR THE
4735 * SCHEDULER SUSPENDED AND THE QUEUE BEING ACCESSED LOCKED. */
4737 /* Place the event list item of the TCB in the appropriate event list.
4738 * This is placed in the list in priority order so the highest priority task
4739 * is the first to be woken by the event.
4741 * Note: Lists are sorted in ascending order by ListItem_t.xItemValue.
4742 * Normally, the xItemValue of a TCB's ListItem_t members is:
4743 * xItemValue = ( configMAX_PRIORITIES - uxPriority )
4744 * Therefore, the event list is sorted in descending priority order.
4746 * The queue that contains the event list is locked, preventing
4747 * simultaneous access from interrupts. */
4748 vListInsert( pxEventList, &( pxCurrentTCB->xEventListItem ) );
4750 prvAddCurrentTaskToDelayedList( xTicksToWait, pdTRUE );
4752 /*-----------------------------------------------------------*/
4754 void vTaskPlaceOnUnorderedEventList( List_t * pxEventList,
4755 const TickType_t xItemValue,
4756 const TickType_t xTicksToWait )
4758 configASSERT( pxEventList );
4760 /* THIS FUNCTION MUST BE CALLED WITH THE SCHEDULER SUSPENDED. It is used by
4761 * the event groups implementation. */
4762 configASSERT( uxSchedulerSuspended != ( UBaseType_t ) 0U );
4764 /* Store the item value in the event list item. It is safe to access the
4765 * event list item here as interrupts won't access the event list item of a
4766 * task that is not in the Blocked state. */
4767 listSET_LIST_ITEM_VALUE( &( pxCurrentTCB->xEventListItem ), xItemValue | taskEVENT_LIST_ITEM_VALUE_IN_USE );
4769 /* Place the event list item of the TCB at the end of the appropriate event
4770 * list. It is safe to access the event list here because it is part of an
4771 * event group implementation - and interrupts don't access event groups
4772 * directly (instead they access them indirectly by pending function calls to
4773 * the task level). */
4774 listINSERT_END( pxEventList, &( pxCurrentTCB->xEventListItem ) );
4776 prvAddCurrentTaskToDelayedList( xTicksToWait, pdTRUE );
4778 /*-----------------------------------------------------------*/
4780 #if ( configUSE_TIMERS == 1 )
4782 void vTaskPlaceOnEventListRestricted( List_t * const pxEventList,
4783 TickType_t xTicksToWait,
4784 const BaseType_t xWaitIndefinitely )
4786 configASSERT( pxEventList );
4788 /* This function should not be called by application code hence the
4789 * 'Restricted' in its name. It is not part of the public API. It is
4790 * designed for use by kernel code, and has special calling requirements -
4791 * it should be called with the scheduler suspended. */
4794 /* Place the event list item of the TCB in the appropriate event list.
4795 * In this case it is assume that this is the only task that is going to
4796 * be waiting on this event list, so the faster vListInsertEnd() function
4797 * can be used in place of vListInsert. */
4798 listINSERT_END( pxEventList, &( pxCurrentTCB->xEventListItem ) );
4800 /* If the task should block indefinitely then set the block time to a
4801 * value that will be recognised as an indefinite delay inside the
4802 * prvAddCurrentTaskToDelayedList() function. */
4803 if( xWaitIndefinitely != pdFALSE )
4805 xTicksToWait = portMAX_DELAY;
4808 traceTASK_DELAY_UNTIL( ( xTickCount + xTicksToWait ) );
4809 prvAddCurrentTaskToDelayedList( xTicksToWait, xWaitIndefinitely );
4812 #endif /* configUSE_TIMERS */
4813 /*-----------------------------------------------------------*/
4815 BaseType_t xTaskRemoveFromEventList( const List_t * const pxEventList )
4817 TCB_t * pxUnblockedTCB;
4820 /* THIS FUNCTION MUST BE CALLED FROM A CRITICAL SECTION. It can also be
4821 * called from a critical section within an ISR. */
4823 /* The event list is sorted in priority order, so the first in the list can
4824 * be removed as it is known to be the highest priority. Remove the TCB from
4825 * the delayed list, and add it to the ready list.
4827 * If an event is for a queue that is locked then this function will never
4828 * get called - the lock count on the queue will get modified instead. This
4829 * means exclusive access to the event list is guaranteed here.
4831 * This function assumes that a check has already been made to ensure that
4832 * pxEventList is not empty. */
4833 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. */
4834 configASSERT( pxUnblockedTCB );
4835 listREMOVE_ITEM( &( pxUnblockedTCB->xEventListItem ) );
4837 if( uxSchedulerSuspended == ( UBaseType_t ) 0U )
4839 listREMOVE_ITEM( &( pxUnblockedTCB->xStateListItem ) );
4840 prvAddTaskToReadyList( pxUnblockedTCB );
4842 #if ( configUSE_TICKLESS_IDLE != 0 )
4844 /* If a task is blocked on a kernel object then xNextTaskUnblockTime
4845 * might be set to the blocked task's time out time. If the task is
4846 * unblocked for a reason other than a timeout xNextTaskUnblockTime is
4847 * normally left unchanged, because it is automatically reset to a new
4848 * value when the tick count equals xNextTaskUnblockTime. However if
4849 * tickless idling is used it might be more important to enter sleep mode
4850 * at the earliest possible time - so reset xNextTaskUnblockTime here to
4851 * ensure it is updated at the earliest possible time. */
4852 prvResetNextTaskUnblockTime();
4858 /* The delayed and ready lists cannot be accessed, so hold this task
4859 * pending until the scheduler is resumed. */
4860 listINSERT_END( &( xPendingReadyList ), &( pxUnblockedTCB->xEventListItem ) );
4863 #if ( configNUMBER_OF_CORES == 1 )
4865 if( pxUnblockedTCB->uxPriority > pxCurrentTCB->uxPriority )
4867 /* Return true if the task removed from the event list has a higher
4868 * priority than the calling task. This allows the calling task to know if
4869 * it should force a context switch now. */
4872 /* Mark that a yield is pending in case the user is not using the
4873 * "xHigherPriorityTaskWoken" parameter to an ISR safe FreeRTOS function. */
4874 xYieldPendings[ 0 ] = pdTRUE;
4881 #else /* #if ( configNUMBER_OF_CORES == 1 ) */
4885 #if ( configUSE_PREEMPTION == 1 )
4887 prvYieldForTask( pxUnblockedTCB );
4889 if( xYieldPendings[ portGET_CORE_ID() ] != pdFALSE )
4894 #endif /* #if ( configUSE_PREEMPTION == 1 ) */
4896 #endif /* #if ( configNUMBER_OF_CORES == 1 ) */
4900 /*-----------------------------------------------------------*/
4902 void vTaskRemoveFromUnorderedEventList( ListItem_t * pxEventListItem,
4903 const TickType_t xItemValue )
4905 TCB_t * pxUnblockedTCB;
4907 /* THIS FUNCTION MUST BE CALLED WITH THE SCHEDULER SUSPENDED. It is used by
4908 * the event flags implementation. */
4909 configASSERT( uxSchedulerSuspended != ( UBaseType_t ) 0U );
4911 /* Store the new item value in the event list. */
4912 listSET_LIST_ITEM_VALUE( pxEventListItem, xItemValue | taskEVENT_LIST_ITEM_VALUE_IN_USE );
4914 /* Remove the event list form the event flag. Interrupts do not access
4916 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. */
4917 configASSERT( pxUnblockedTCB );
4918 listREMOVE_ITEM( pxEventListItem );
4920 #if ( configUSE_TICKLESS_IDLE != 0 )
4922 /* If a task is blocked on a kernel object then xNextTaskUnblockTime
4923 * might be set to the blocked task's time out time. If the task is
4924 * unblocked for a reason other than a timeout xNextTaskUnblockTime is
4925 * normally left unchanged, because it is automatically reset to a new
4926 * value when the tick count equals xNextTaskUnblockTime. However if
4927 * tickless idling is used it might be more important to enter sleep mode
4928 * at the earliest possible time - so reset xNextTaskUnblockTime here to
4929 * ensure it is updated at the earliest possible time. */
4930 prvResetNextTaskUnblockTime();
4934 /* Remove the task from the delayed list and add it to the ready list. The
4935 * scheduler is suspended so interrupts will not be accessing the ready
4937 listREMOVE_ITEM( &( pxUnblockedTCB->xStateListItem ) );
4938 prvAddTaskToReadyList( pxUnblockedTCB );
4940 #if ( configNUMBER_OF_CORES == 1 )
4942 if( pxUnblockedTCB->uxPriority > pxCurrentTCB->uxPriority )
4944 /* The unblocked task has a priority above that of the calling task, so
4945 * a context switch is required. This function is called with the
4946 * scheduler suspended so xYieldPending is set so the context switch
4947 * occurs immediately that the scheduler is resumed (unsuspended). */
4948 xYieldPendings[ 0 ] = pdTRUE;
4951 #else /* #if ( configNUMBER_OF_CORES == 1 ) */
4953 #if ( configUSE_PREEMPTION == 1 )
4955 taskENTER_CRITICAL();
4957 prvYieldForTask( pxUnblockedTCB );
4959 taskEXIT_CRITICAL();
4963 #endif /* #if ( configNUMBER_OF_CORES == 1 ) */
4965 /*-----------------------------------------------------------*/
4967 void vTaskSetTimeOutState( TimeOut_t * const pxTimeOut )
4969 configASSERT( pxTimeOut );
4970 taskENTER_CRITICAL();
4972 pxTimeOut->xOverflowCount = xNumOfOverflows;
4973 pxTimeOut->xTimeOnEntering = xTickCount;
4975 taskEXIT_CRITICAL();
4977 /*-----------------------------------------------------------*/
4979 void vTaskInternalSetTimeOutState( TimeOut_t * const pxTimeOut )
4981 /* For internal use only as it does not use a critical section. */
4982 pxTimeOut->xOverflowCount = xNumOfOverflows;
4983 pxTimeOut->xTimeOnEntering = xTickCount;
4985 /*-----------------------------------------------------------*/
4987 BaseType_t xTaskCheckForTimeOut( TimeOut_t * const pxTimeOut,
4988 TickType_t * const pxTicksToWait )
4992 configASSERT( pxTimeOut );
4993 configASSERT( pxTicksToWait );
4995 taskENTER_CRITICAL();
4997 /* Minor optimisation. The tick count cannot change in this block. */
4998 const TickType_t xConstTickCount = xTickCount;
4999 const TickType_t xElapsedTime = xConstTickCount - pxTimeOut->xTimeOnEntering;
5001 #if ( INCLUDE_xTaskAbortDelay == 1 )
5002 if( pxCurrentTCB->ucDelayAborted != ( uint8_t ) pdFALSE )
5004 /* The delay was aborted, which is not the same as a time out,
5005 * but has the same result. */
5006 pxCurrentTCB->ucDelayAborted = pdFALSE;
5012 #if ( INCLUDE_vTaskSuspend == 1 )
5013 if( *pxTicksToWait == portMAX_DELAY )
5015 /* If INCLUDE_vTaskSuspend is set to 1 and the block time
5016 * specified is the maximum block time then the task should block
5017 * indefinitely, and therefore never time out. */
5023 if( ( xNumOfOverflows != pxTimeOut->xOverflowCount ) && ( xConstTickCount >= pxTimeOut->xTimeOnEntering ) ) /*lint !e525 Indentation preferred as is to make code within pre-processor directives clearer. */
5025 /* The tick count is greater than the time at which
5026 * vTaskSetTimeout() was called, but has also overflowed since
5027 * vTaskSetTimeOut() was called. It must have wrapped all the way
5028 * around and gone past again. This passed since vTaskSetTimeout()
5031 *pxTicksToWait = ( TickType_t ) 0;
5033 else if( xElapsedTime < *pxTicksToWait ) /*lint !e961 Explicit casting is only redundant with some compilers, whereas others require it to prevent integer conversion errors. */
5035 /* Not a genuine timeout. Adjust parameters for time remaining. */
5036 *pxTicksToWait -= xElapsedTime;
5037 vTaskInternalSetTimeOutState( pxTimeOut );
5042 *pxTicksToWait = ( TickType_t ) 0;
5046 taskEXIT_CRITICAL();
5050 /*-----------------------------------------------------------*/
5052 void vTaskMissedYield( void )
5054 /* Must be called from within a critical section. */
5055 xYieldPendings[ portGET_CORE_ID() ] = pdTRUE;
5057 /*-----------------------------------------------------------*/
5059 #if ( configUSE_TRACE_FACILITY == 1 )
5061 UBaseType_t uxTaskGetTaskNumber( TaskHandle_t xTask )
5063 UBaseType_t uxReturn;
5064 TCB_t const * pxTCB;
5069 uxReturn = pxTCB->uxTaskNumber;
5079 #endif /* configUSE_TRACE_FACILITY */
5080 /*-----------------------------------------------------------*/
5082 #if ( configUSE_TRACE_FACILITY == 1 )
5084 void vTaskSetTaskNumber( TaskHandle_t xTask,
5085 const UBaseType_t uxHandle )
5092 pxTCB->uxTaskNumber = uxHandle;
5096 #endif /* configUSE_TRACE_FACILITY */
5097 /*-----------------------------------------------------------*/
5100 * -----------------------------------------------------------
5101 * The MinimalIdle task.
5102 * ----------------------------------------------------------
5104 * The minimal idle task is used for all the additional cores in a SMP
5105 * system. There must be only 1 idle task and the rest are minimal idle
5108 * The portTASK_FUNCTION() macro is used to allow port/compiler specific
5109 * language extensions. The equivalent prototype for this function is:
5111 * void prvMinimalIdleTask( void *pvParameters );
5114 #if ( configNUMBER_OF_CORES > 1 )
5115 static portTASK_FUNCTION( prvMinimalIdleTask, pvParameters )
5117 ( void ) pvParameters;
5121 while( INFINITE_LOOP() )
5123 #if ( configUSE_PREEMPTION == 0 )
5125 /* If we are not using preemption we keep forcing a task switch to
5126 * see if any other task has become available. If we are using
5127 * preemption we don't need to do this as any task becoming available
5128 * will automatically get the processor anyway. */
5131 #endif /* configUSE_PREEMPTION */
5133 #if ( ( configUSE_PREEMPTION == 1 ) && ( configIDLE_SHOULD_YIELD == 1 ) )
5135 /* When using preemption tasks of equal priority will be
5136 * timesliced. If a task that is sharing the idle priority is ready
5137 * to run then the idle task should yield before the end of the
5140 * A critical region is not required here as we are just reading from
5141 * the list, and an occasional incorrect value will not matter. If
5142 * the ready list at the idle priority contains one more task than the
5143 * number of idle tasks, which is equal to the configured numbers of cores
5144 * then a task other than the idle task is ready to execute. */
5145 if( listCURRENT_LIST_LENGTH( &( pxReadyTasksLists[ tskIDLE_PRIORITY ] ) ) > ( UBaseType_t ) configNUMBER_OF_CORES )
5151 mtCOVERAGE_TEST_MARKER();
5154 #endif /* ( ( configUSE_PREEMPTION == 1 ) && ( configIDLE_SHOULD_YIELD == 1 ) ) */
5156 #if ( configUSE_MINIMAL_IDLE_HOOK == 1 )
5158 /* Call the user defined function from within the idle task. This
5159 * allows the application designer to add background functionality
5160 * without the overhead of a separate task.
5162 * This hook is intended to manage core activity such as disabling cores that go idle.
5164 * NOTE: vApplicationMinimalIdleHook() MUST NOT, UNDER ANY CIRCUMSTANCES,
5165 * CALL A FUNCTION THAT MIGHT BLOCK. */
5166 vApplicationMinimalIdleHook();
5168 #endif /* configUSE_MINIMAL_IDLE_HOOK */
5171 #endif /* #if ( configNUMBER_OF_CORES > 1 ) */
5174 * -----------------------------------------------------------
5176 * ----------------------------------------------------------
5178 * The portTASK_FUNCTION() macro is used to allow port/compiler specific
5179 * language extensions. The equivalent prototype for this function is:
5181 * void prvIdleTask( void *pvParameters );
5185 static portTASK_FUNCTION( prvIdleTask, pvParameters )
5187 /* Stop warnings. */
5188 ( void ) pvParameters;
5190 /** THIS IS THE RTOS IDLE TASK - WHICH IS CREATED AUTOMATICALLY WHEN THE
5191 * SCHEDULER IS STARTED. **/
5193 /* In case a task that has a secure context deletes itself, in which case
5194 * the idle task is responsible for deleting the task's secure context, if
5196 portALLOCATE_SECURE_CONTEXT( configMINIMAL_SECURE_STACK_SIZE );
5198 #if ( configNUMBER_OF_CORES > 1 )
5200 /* SMP all cores start up in the idle task. This initial yield gets the application
5204 #endif /* #if ( configNUMBER_OF_CORES > 1 ) */
5206 while( INFINITE_LOOP() )
5208 /* See if any tasks have deleted themselves - if so then the idle task
5209 * is responsible for freeing the deleted task's TCB and stack. */
5210 prvCheckTasksWaitingTermination();
5212 #if ( configUSE_PREEMPTION == 0 )
5214 /* If we are not using preemption we keep forcing a task switch to
5215 * see if any other task has become available. If we are using
5216 * preemption we don't need to do this as any task becoming available
5217 * will automatically get the processor anyway. */
5220 #endif /* configUSE_PREEMPTION */
5222 #if ( ( configUSE_PREEMPTION == 1 ) && ( configIDLE_SHOULD_YIELD == 1 ) )
5224 /* When using preemption tasks of equal priority will be
5225 * timesliced. If a task that is sharing the idle priority is ready
5226 * to run then the idle task should yield before the end of the
5229 * A critical region is not required here as we are just reading from
5230 * the list, and an occasional incorrect value will not matter. If
5231 * the ready list at the idle priority contains one more task than the
5232 * number of idle tasks, which is equal to the configured numbers of cores
5233 * then a task other than the idle task is ready to execute. */
5234 if( listCURRENT_LIST_LENGTH( &( pxReadyTasksLists[ tskIDLE_PRIORITY ] ) ) > ( UBaseType_t ) configNUMBER_OF_CORES )
5240 mtCOVERAGE_TEST_MARKER();
5243 #endif /* ( ( configUSE_PREEMPTION == 1 ) && ( configIDLE_SHOULD_YIELD == 1 ) ) */
5245 #if ( configUSE_IDLE_HOOK == 1 )
5247 /* Call the user defined function from within the idle task. */
5248 vApplicationIdleHook();
5250 #endif /* configUSE_IDLE_HOOK */
5252 /* This conditional compilation should use inequality to 0, not equality
5253 * to 1. This is to ensure portSUPPRESS_TICKS_AND_SLEEP() is called when
5254 * user defined low power mode implementations require
5255 * configUSE_TICKLESS_IDLE to be set to a value other than 1. */
5256 #if ( configUSE_TICKLESS_IDLE != 0 )
5258 TickType_t xExpectedIdleTime;
5260 /* It is not desirable to suspend then resume the scheduler on
5261 * each iteration of the idle task. Therefore, a preliminary
5262 * test of the expected idle time is performed without the
5263 * scheduler suspended. The result here is not necessarily
5265 xExpectedIdleTime = prvGetExpectedIdleTime();
5267 if( xExpectedIdleTime >= configEXPECTED_IDLE_TIME_BEFORE_SLEEP )
5271 /* Now the scheduler is suspended, the expected idle
5272 * time can be sampled again, and this time its value can
5274 configASSERT( xNextTaskUnblockTime >= xTickCount );
5275 xExpectedIdleTime = prvGetExpectedIdleTime();
5277 /* Define the following macro to set xExpectedIdleTime to 0
5278 * if the application does not want
5279 * portSUPPRESS_TICKS_AND_SLEEP() to be called. */
5280 configPRE_SUPPRESS_TICKS_AND_SLEEP_PROCESSING( xExpectedIdleTime );
5282 if( xExpectedIdleTime >= configEXPECTED_IDLE_TIME_BEFORE_SLEEP )
5284 traceLOW_POWER_IDLE_BEGIN();
5285 portSUPPRESS_TICKS_AND_SLEEP( xExpectedIdleTime );
5286 traceLOW_POWER_IDLE_END();
5290 mtCOVERAGE_TEST_MARKER();
5293 ( void ) xTaskResumeAll();
5297 mtCOVERAGE_TEST_MARKER();
5300 #endif /* configUSE_TICKLESS_IDLE */
5302 #if ( ( configNUMBER_OF_CORES > 1 ) && ( configUSE_MINIMAL_IDLE_HOOK == 1 ) )
5304 /* Call the user defined function from within the idle task. This
5305 * allows the application designer to add background functionality
5306 * without the overhead of a separate task.
5308 * This hook is intended to manage core activity such as disabling cores that go idle.
5310 * NOTE: vApplicationMinimalIdleHook() MUST NOT, UNDER ANY CIRCUMSTANCES,
5311 * CALL A FUNCTION THAT MIGHT BLOCK. */
5312 vApplicationMinimalIdleHook();
5314 #endif /* #if ( ( configNUMBER_OF_CORES > 1 ) && ( configUSE_MINIMAL_IDLE_HOOK == 1 ) ) */
5317 /*-----------------------------------------------------------*/
5319 #if ( configUSE_TICKLESS_IDLE != 0 )
5321 eSleepModeStatus eTaskConfirmSleepModeStatus( void )
5323 #if ( INCLUDE_vTaskSuspend == 1 )
5324 /* The idle task exists in addition to the application tasks. */
5325 const UBaseType_t uxNonApplicationTasks = 1;
5326 #endif /* INCLUDE_vTaskSuspend */
5328 eSleepModeStatus eReturn = eStandardSleep;
5330 /* This function must be called from a critical section. */
5332 if( listCURRENT_LIST_LENGTH( &xPendingReadyList ) != 0 )
5334 /* A task was made ready while the scheduler was suspended. */
5335 eReturn = eAbortSleep;
5337 else if( xYieldPendings[ portGET_CORE_ID() ] != pdFALSE )
5339 /* A yield was pended while the scheduler was suspended. */
5340 eReturn = eAbortSleep;
5342 else if( xPendedTicks != 0 )
5344 /* A tick interrupt has already occurred but was held pending
5345 * because the scheduler is suspended. */
5346 eReturn = eAbortSleep;
5349 #if ( INCLUDE_vTaskSuspend == 1 )
5350 else if( listCURRENT_LIST_LENGTH( &xSuspendedTaskList ) == ( uxCurrentNumberOfTasks - uxNonApplicationTasks ) )
5352 /* If all the tasks are in the suspended list (which might mean they
5353 * have an infinite block time rather than actually being suspended)
5354 * then it is safe to turn all clocks off and just wait for external
5356 eReturn = eNoTasksWaitingTimeout;
5358 #endif /* INCLUDE_vTaskSuspend */
5361 mtCOVERAGE_TEST_MARKER();
5367 #endif /* configUSE_TICKLESS_IDLE */
5368 /*-----------------------------------------------------------*/
5370 #if ( configNUM_THREAD_LOCAL_STORAGE_POINTERS != 0 )
5372 void vTaskSetThreadLocalStoragePointer( TaskHandle_t xTaskToSet,
5378 if( ( xIndex >= 0 ) &&
5379 ( xIndex < ( BaseType_t ) configNUM_THREAD_LOCAL_STORAGE_POINTERS ) )
5381 pxTCB = prvGetTCBFromHandle( xTaskToSet );
5382 configASSERT( pxTCB != NULL );
5383 pxTCB->pvThreadLocalStoragePointers[ xIndex ] = pvValue;
5387 #endif /* configNUM_THREAD_LOCAL_STORAGE_POINTERS */
5388 /*-----------------------------------------------------------*/
5390 #if ( configNUM_THREAD_LOCAL_STORAGE_POINTERS != 0 )
5392 void * pvTaskGetThreadLocalStoragePointer( TaskHandle_t xTaskToQuery,
5395 void * pvReturn = NULL;
5398 if( ( xIndex >= 0 ) &&
5399 ( xIndex < ( BaseType_t ) configNUM_THREAD_LOCAL_STORAGE_POINTERS ) )
5401 pxTCB = prvGetTCBFromHandle( xTaskToQuery );
5402 pvReturn = pxTCB->pvThreadLocalStoragePointers[ xIndex ];
5412 #endif /* configNUM_THREAD_LOCAL_STORAGE_POINTERS */
5413 /*-----------------------------------------------------------*/
5415 #if ( portUSING_MPU_WRAPPERS == 1 )
5417 void vTaskAllocateMPURegions( TaskHandle_t xTaskToModify,
5418 const MemoryRegion_t * const pxRegions )
5422 /* If null is passed in here then we are modifying the MPU settings of
5423 * the calling task. */
5424 pxTCB = prvGetTCBFromHandle( xTaskToModify );
5426 vPortStoreTaskMPUSettings( &( pxTCB->xMPUSettings ), pxRegions, NULL, 0 );
5429 #endif /* portUSING_MPU_WRAPPERS */
5430 /*-----------------------------------------------------------*/
5432 static void prvInitialiseTaskLists( void )
5434 UBaseType_t uxPriority;
5436 for( uxPriority = ( UBaseType_t ) 0U; uxPriority < ( UBaseType_t ) configMAX_PRIORITIES; uxPriority++ )
5438 vListInitialise( &( pxReadyTasksLists[ uxPriority ] ) );
5441 vListInitialise( &xDelayedTaskList1 );
5442 vListInitialise( &xDelayedTaskList2 );
5443 vListInitialise( &xPendingReadyList );
5445 #if ( INCLUDE_vTaskDelete == 1 )
5447 vListInitialise( &xTasksWaitingTermination );
5449 #endif /* INCLUDE_vTaskDelete */
5451 #if ( INCLUDE_vTaskSuspend == 1 )
5453 vListInitialise( &xSuspendedTaskList );
5455 #endif /* INCLUDE_vTaskSuspend */
5457 /* Start with pxDelayedTaskList using list1 and the pxOverflowDelayedTaskList
5459 pxDelayedTaskList = &xDelayedTaskList1;
5460 pxOverflowDelayedTaskList = &xDelayedTaskList2;
5462 /*-----------------------------------------------------------*/
5464 static void prvCheckTasksWaitingTermination( void )
5466 /** THIS FUNCTION IS CALLED FROM THE RTOS IDLE TASK **/
5468 #if ( INCLUDE_vTaskDelete == 1 )
5472 /* uxDeletedTasksWaitingCleanUp is used to prevent taskENTER_CRITICAL()
5473 * being called too often in the idle task. */
5474 while( uxDeletedTasksWaitingCleanUp > ( UBaseType_t ) 0U )
5476 #if ( configNUMBER_OF_CORES == 1 )
5478 taskENTER_CRITICAL();
5481 pxTCB = listGET_OWNER_OF_HEAD_ENTRY( ( &xTasksWaitingTermination ) ); /*lint !e9079 void * is used as this macro is used with timers too. Alignment is known to be fine as the type of the pointer stored and retrieved is the same. */
5482 ( void ) uxListRemove( &( pxTCB->xStateListItem ) );
5483 --uxCurrentNumberOfTasks;
5484 --uxDeletedTasksWaitingCleanUp;
5487 taskEXIT_CRITICAL();
5489 prvDeleteTCB( pxTCB );
5491 #else /* #if( configNUMBER_OF_CORES == 1 ) */
5495 taskENTER_CRITICAL();
5497 /* For SMP, multiple idles can be running simultaneously
5498 * and we need to check that other idles did not cleanup while we were
5499 * waiting to enter the critical section. */
5500 if( uxDeletedTasksWaitingCleanUp > ( UBaseType_t ) 0U )
5502 pxTCB = listGET_OWNER_OF_HEAD_ENTRY( ( &xTasksWaitingTermination ) ); /*lint !e9079 void * is used as this macro is used with timers too. Alignment is known to be fine as the type of the pointer stored and retrieved is the same. */
5504 if( pxTCB->xTaskRunState == taskTASK_NOT_RUNNING )
5506 ( void ) uxListRemove( &( pxTCB->xStateListItem ) );
5507 --uxCurrentNumberOfTasks;
5508 --uxDeletedTasksWaitingCleanUp;
5512 /* The TCB to be deleted still has not yet been switched out
5513 * by the scheduler, so we will just exit this loop early and
5514 * try again next time. */
5515 taskEXIT_CRITICAL();
5520 taskEXIT_CRITICAL();
5524 prvDeleteTCB( pxTCB );
5527 #endif /* #if( configNUMBER_OF_CORES == 1 ) */
5530 #endif /* INCLUDE_vTaskDelete */
5532 /*-----------------------------------------------------------*/
5534 #if ( configUSE_TRACE_FACILITY == 1 )
5536 void vTaskGetInfo( TaskHandle_t xTask,
5537 TaskStatus_t * pxTaskStatus,
5538 BaseType_t xGetFreeStackSpace,
5543 /* xTask is NULL then get the state of the calling task. */
5544 pxTCB = prvGetTCBFromHandle( xTask );
5546 pxTaskStatus->xHandle = pxTCB;
5547 pxTaskStatus->pcTaskName = ( const char * ) &( pxTCB->pcTaskName[ 0 ] );
5548 pxTaskStatus->uxCurrentPriority = pxTCB->uxPriority;
5549 pxTaskStatus->pxStackBase = pxTCB->pxStack;
5550 #if ( ( portSTACK_GROWTH > 0 ) || ( configRECORD_STACK_HIGH_ADDRESS == 1 ) )
5551 pxTaskStatus->pxTopOfStack = ( StackType_t * ) pxTCB->pxTopOfStack;
5552 pxTaskStatus->pxEndOfStack = pxTCB->pxEndOfStack;
5554 pxTaskStatus->xTaskNumber = pxTCB->uxTCBNumber;
5556 #if ( ( configUSE_CORE_AFFINITY == 1 ) && ( configNUMBER_OF_CORES > 1 ) )
5558 pxTaskStatus->uxCoreAffinityMask = pxTCB->uxCoreAffinityMask;
5562 #if ( configUSE_MUTEXES == 1 )
5564 pxTaskStatus->uxBasePriority = pxTCB->uxBasePriority;
5568 pxTaskStatus->uxBasePriority = 0;
5572 #if ( configGENERATE_RUN_TIME_STATS == 1 )
5574 pxTaskStatus->ulRunTimeCounter = pxTCB->ulRunTimeCounter;
5578 pxTaskStatus->ulRunTimeCounter = ( configRUN_TIME_COUNTER_TYPE ) 0;
5582 /* Obtaining the task state is a little fiddly, so is only done if the
5583 * value of eState passed into this function is eInvalid - otherwise the
5584 * state is just set to whatever is passed in. */
5585 if( eState != eInvalid )
5587 if( taskTASK_IS_RUNNING( pxTCB ) == pdTRUE )
5589 pxTaskStatus->eCurrentState = eRunning;
5593 pxTaskStatus->eCurrentState = eState;
5595 #if ( INCLUDE_vTaskSuspend == 1 )
5597 /* If the task is in the suspended list then there is a
5598 * chance it is actually just blocked indefinitely - so really
5599 * it should be reported as being in the Blocked state. */
5600 if( eState == eSuspended )
5604 if( listLIST_ITEM_CONTAINER( &( pxTCB->xEventListItem ) ) != NULL )
5606 pxTaskStatus->eCurrentState = eBlocked;
5609 ( void ) xTaskResumeAll();
5612 #endif /* INCLUDE_vTaskSuspend */
5614 /* Tasks can be in pending ready list and other state list at the
5615 * same time. These tasks are in ready state no matter what state
5616 * list the task is in. */
5617 taskENTER_CRITICAL();
5619 if( listIS_CONTAINED_WITHIN( &xPendingReadyList, &( pxTCB->xEventListItem ) ) != pdFALSE )
5621 pxTaskStatus->eCurrentState = eReady;
5624 taskEXIT_CRITICAL();
5629 pxTaskStatus->eCurrentState = eTaskGetState( pxTCB );
5632 /* Obtaining the stack space takes some time, so the xGetFreeStackSpace
5633 * parameter is provided to allow it to be skipped. */
5634 if( xGetFreeStackSpace != pdFALSE )
5636 #if ( portSTACK_GROWTH > 0 )
5638 pxTaskStatus->usStackHighWaterMark = prvTaskCheckFreeStackSpace( ( uint8_t * ) pxTCB->pxEndOfStack );
5642 pxTaskStatus->usStackHighWaterMark = prvTaskCheckFreeStackSpace( ( uint8_t * ) pxTCB->pxStack );
5648 pxTaskStatus->usStackHighWaterMark = 0;
5652 #endif /* configUSE_TRACE_FACILITY */
5653 /*-----------------------------------------------------------*/
5655 #if ( configUSE_TRACE_FACILITY == 1 )
5657 static UBaseType_t prvListTasksWithinSingleList( TaskStatus_t * pxTaskStatusArray,
5661 configLIST_VOLATILE TCB_t * pxNextTCB;
5662 configLIST_VOLATILE TCB_t * pxFirstTCB;
5663 UBaseType_t uxTask = 0;
5665 if( listCURRENT_LIST_LENGTH( pxList ) > ( UBaseType_t ) 0 )
5667 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. */
5669 /* Populate an TaskStatus_t structure within the
5670 * pxTaskStatusArray array for each task that is referenced from
5671 * pxList. See the definition of TaskStatus_t in task.h for the
5672 * meaning of each TaskStatus_t structure member. */
5675 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. */
5676 vTaskGetInfo( ( TaskHandle_t ) pxNextTCB, &( pxTaskStatusArray[ uxTask ] ), pdTRUE, eState );
5678 } while( pxNextTCB != pxFirstTCB );
5682 mtCOVERAGE_TEST_MARKER();
5688 #endif /* configUSE_TRACE_FACILITY */
5689 /*-----------------------------------------------------------*/
5691 #if ( ( configUSE_TRACE_FACILITY == 1 ) || ( INCLUDE_uxTaskGetStackHighWaterMark == 1 ) || ( INCLUDE_uxTaskGetStackHighWaterMark2 == 1 ) )
5693 static configSTACK_DEPTH_TYPE prvTaskCheckFreeStackSpace( const uint8_t * pucStackByte )
5695 uint32_t ulCount = 0U;
5697 while( *pucStackByte == ( uint8_t ) tskSTACK_FILL_BYTE )
5699 pucStackByte -= portSTACK_GROWTH;
5703 ulCount /= ( uint32_t ) sizeof( StackType_t ); /*lint !e961 Casting is not redundant on smaller architectures. */
5705 return ( configSTACK_DEPTH_TYPE ) ulCount;
5708 #endif /* ( ( configUSE_TRACE_FACILITY == 1 ) || ( INCLUDE_uxTaskGetStackHighWaterMark == 1 ) || ( INCLUDE_uxTaskGetStackHighWaterMark2 == 1 ) ) */
5709 /*-----------------------------------------------------------*/
5711 #if ( INCLUDE_uxTaskGetStackHighWaterMark2 == 1 )
5713 /* uxTaskGetStackHighWaterMark() and uxTaskGetStackHighWaterMark2() are the
5714 * same except for their return type. Using configSTACK_DEPTH_TYPE allows the
5715 * user to determine the return type. It gets around the problem of the value
5716 * overflowing on 8-bit types without breaking backward compatibility for
5717 * applications that expect an 8-bit return type. */
5718 configSTACK_DEPTH_TYPE uxTaskGetStackHighWaterMark2( TaskHandle_t xTask )
5721 uint8_t * pucEndOfStack;
5722 configSTACK_DEPTH_TYPE uxReturn;
5724 /* uxTaskGetStackHighWaterMark() and uxTaskGetStackHighWaterMark2() are
5725 * the same except for their return type. Using configSTACK_DEPTH_TYPE
5726 * allows the user to determine the return type. It gets around the
5727 * problem of the value overflowing on 8-bit types without breaking
5728 * backward compatibility for applications that expect an 8-bit return
5731 pxTCB = prvGetTCBFromHandle( xTask );
5733 #if portSTACK_GROWTH < 0
5735 pucEndOfStack = ( uint8_t * ) pxTCB->pxStack;
5739 pucEndOfStack = ( uint8_t * ) pxTCB->pxEndOfStack;
5743 uxReturn = prvTaskCheckFreeStackSpace( pucEndOfStack );
5748 #endif /* INCLUDE_uxTaskGetStackHighWaterMark2 */
5749 /*-----------------------------------------------------------*/
5751 #if ( INCLUDE_uxTaskGetStackHighWaterMark == 1 )
5753 UBaseType_t uxTaskGetStackHighWaterMark( TaskHandle_t xTask )
5756 uint8_t * pucEndOfStack;
5757 UBaseType_t uxReturn;
5759 pxTCB = prvGetTCBFromHandle( xTask );
5761 #if portSTACK_GROWTH < 0
5763 pucEndOfStack = ( uint8_t * ) pxTCB->pxStack;
5767 pucEndOfStack = ( uint8_t * ) pxTCB->pxEndOfStack;
5771 uxReturn = ( UBaseType_t ) prvTaskCheckFreeStackSpace( pucEndOfStack );
5776 #endif /* INCLUDE_uxTaskGetStackHighWaterMark */
5777 /*-----------------------------------------------------------*/
5779 #if ( INCLUDE_vTaskDelete == 1 )
5781 static void prvDeleteTCB( TCB_t * pxTCB )
5783 /* This call is required specifically for the TriCore port. It must be
5784 * above the vPortFree() calls. The call is also used by ports/demos that
5785 * want to allocate and clean RAM statically. */
5786 portCLEAN_UP_TCB( pxTCB );
5788 #if ( configUSE_C_RUNTIME_TLS_SUPPORT == 1 )
5790 /* Free up the memory allocated for the task's TLS Block. */
5791 configDEINIT_TLS_BLOCK( pxTCB->xTLSBlock );
5795 #if ( ( configSUPPORT_DYNAMIC_ALLOCATION == 1 ) && ( configSUPPORT_STATIC_ALLOCATION == 0 ) && ( portUSING_MPU_WRAPPERS == 0 ) )
5797 /* The task can only have been allocated dynamically - free both
5798 * the stack and TCB. */
5799 vPortFreeStack( pxTCB->pxStack );
5802 #elif ( tskSTATIC_AND_DYNAMIC_ALLOCATION_POSSIBLE != 0 ) /*lint !e731 !e9029 Macro has been consolidated for readability reasons. */
5804 /* The task could have been allocated statically or dynamically, so
5805 * check what was statically allocated before trying to free the
5807 if( pxTCB->ucStaticallyAllocated == tskDYNAMICALLY_ALLOCATED_STACK_AND_TCB )
5809 /* Both the stack and TCB were allocated dynamically, so both
5811 vPortFreeStack( pxTCB->pxStack );
5814 else if( pxTCB->ucStaticallyAllocated == tskSTATICALLY_ALLOCATED_STACK_ONLY )
5816 /* Only the stack was statically allocated, so the TCB is the
5817 * only memory that must be freed. */
5822 /* Neither the stack nor the TCB were allocated dynamically, so
5823 * nothing needs to be freed. */
5824 configASSERT( pxTCB->ucStaticallyAllocated == tskSTATICALLY_ALLOCATED_STACK_AND_TCB );
5825 mtCOVERAGE_TEST_MARKER();
5828 #endif /* configSUPPORT_DYNAMIC_ALLOCATION */
5831 #endif /* INCLUDE_vTaskDelete */
5832 /*-----------------------------------------------------------*/
5834 static void prvResetNextTaskUnblockTime( void )
5836 if( listLIST_IS_EMPTY( pxDelayedTaskList ) != pdFALSE )
5838 /* The new current delayed list is empty. Set xNextTaskUnblockTime to
5839 * the maximum possible value so it is extremely unlikely that the
5840 * if( xTickCount >= xNextTaskUnblockTime ) test will pass until
5841 * there is an item in the delayed list. */
5842 xNextTaskUnblockTime = portMAX_DELAY;
5846 /* The new current delayed list is not empty, get the value of
5847 * the item at the head of the delayed list. This is the time at
5848 * which the task at the head of the delayed list should be removed
5849 * from the Blocked state. */
5850 xNextTaskUnblockTime = listGET_ITEM_VALUE_OF_HEAD_ENTRY( pxDelayedTaskList );
5853 /*-----------------------------------------------------------*/
5855 #if ( ( INCLUDE_xTaskGetCurrentTaskHandle == 1 ) || ( configUSE_MUTEXES == 1 ) ) || ( configNUMBER_OF_CORES > 1 )
5857 #if ( configNUMBER_OF_CORES == 1 )
5858 TaskHandle_t xTaskGetCurrentTaskHandle( void )
5860 TaskHandle_t xReturn;
5862 /* A critical section is not required as this is not called from
5863 * an interrupt and the current TCB will always be the same for any
5864 * individual execution thread. */
5865 xReturn = pxCurrentTCB;
5869 #else /* #if ( configNUMBER_OF_CORES == 1 ) */
5870 TaskHandle_t xTaskGetCurrentTaskHandle( void )
5872 TaskHandle_t xReturn;
5873 UBaseType_t uxSavedInterruptStatus;
5875 uxSavedInterruptStatus = portSET_INTERRUPT_MASK();
5877 xReturn = pxCurrentTCBs[ portGET_CORE_ID() ];
5879 portCLEAR_INTERRUPT_MASK( uxSavedInterruptStatus );
5884 TaskHandle_t xTaskGetCurrentTaskHandleCPU( BaseType_t xCoreID )
5886 TaskHandle_t xReturn = NULL;
5888 if( taskVALID_CORE_ID( xCoreID ) != pdFALSE )
5890 xReturn = pxCurrentTCBs[ xCoreID ];
5895 #endif /* #if ( configNUMBER_OF_CORES == 1 ) */
5897 #endif /* ( ( INCLUDE_xTaskGetCurrentTaskHandle == 1 ) || ( configUSE_MUTEXES == 1 ) ) */
5898 /*-----------------------------------------------------------*/
5900 #if ( ( INCLUDE_xTaskGetSchedulerState == 1 ) || ( configUSE_TIMERS == 1 ) )
5902 BaseType_t xTaskGetSchedulerState( void )
5906 if( xSchedulerRunning == pdFALSE )
5908 xReturn = taskSCHEDULER_NOT_STARTED;
5912 #if ( configNUMBER_OF_CORES > 1 )
5913 taskENTER_CRITICAL();
5916 if( uxSchedulerSuspended == ( UBaseType_t ) 0U )
5918 xReturn = taskSCHEDULER_RUNNING;
5922 xReturn = taskSCHEDULER_SUSPENDED;
5925 #if ( configNUMBER_OF_CORES > 1 )
5926 taskEXIT_CRITICAL();
5933 #endif /* ( ( INCLUDE_xTaskGetSchedulerState == 1 ) || ( configUSE_TIMERS == 1 ) ) */
5934 /*-----------------------------------------------------------*/
5936 #if ( configUSE_MUTEXES == 1 )
5938 BaseType_t xTaskPriorityInherit( TaskHandle_t const pxMutexHolder )
5940 TCB_t * const pxMutexHolderTCB = pxMutexHolder;
5941 BaseType_t xReturn = pdFALSE;
5943 /* If the mutex was given back by an interrupt while the queue was
5944 * locked then the mutex holder might now be NULL. _RB_ Is this still
5945 * needed as interrupts can no longer use mutexes? */
5946 if( pxMutexHolder != NULL )
5948 /* If the holder of the mutex has a priority below the priority of
5949 * the task attempting to obtain the mutex then it will temporarily
5950 * inherit the priority of the task attempting to obtain the mutex. */
5951 if( pxMutexHolderTCB->uxPriority < pxCurrentTCB->uxPriority )
5953 /* Adjust the mutex holder state to account for its new
5954 * priority. Only reset the event list item value if the value is
5955 * not being used for anything else. */
5956 if( ( listGET_LIST_ITEM_VALUE( &( pxMutexHolderTCB->xEventListItem ) ) & taskEVENT_LIST_ITEM_VALUE_IN_USE ) == 0UL )
5958 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. */
5962 mtCOVERAGE_TEST_MARKER();
5965 /* If the task being modified is in the ready state it will need
5966 * to be moved into a new list. */
5967 if( listIS_CONTAINED_WITHIN( &( pxReadyTasksLists[ pxMutexHolderTCB->uxPriority ] ), &( pxMutexHolderTCB->xStateListItem ) ) != pdFALSE )
5969 if( uxListRemove( &( pxMutexHolderTCB->xStateListItem ) ) == ( UBaseType_t ) 0 )
5971 /* It is known that the task is in its ready list so
5972 * there is no need to check again and the port level
5973 * reset macro can be called directly. */
5974 portRESET_READY_PRIORITY( pxMutexHolderTCB->uxPriority, uxTopReadyPriority );
5978 mtCOVERAGE_TEST_MARKER();
5981 /* Inherit the priority before being moved into the new list. */
5982 pxMutexHolderTCB->uxPriority = pxCurrentTCB->uxPriority;
5983 prvAddTaskToReadyList( pxMutexHolderTCB );
5984 #if ( configNUMBER_OF_CORES > 1 )
5986 /* The priority of the task is raised. Yield for this task
5987 * if it is not running. */
5988 if( taskTASK_IS_RUNNING( pxMutexHolderTCB ) != pdTRUE )
5990 prvYieldForTask( pxMutexHolderTCB );
5993 #endif /* if ( configNUMBER_OF_CORES > 1 ) */
5997 /* Just inherit the priority. */
5998 pxMutexHolderTCB->uxPriority = pxCurrentTCB->uxPriority;
6001 traceTASK_PRIORITY_INHERIT( pxMutexHolderTCB, pxCurrentTCB->uxPriority );
6003 /* Inheritance occurred. */
6008 if( pxMutexHolderTCB->uxBasePriority < pxCurrentTCB->uxPriority )
6010 /* The base priority of the mutex holder is lower than the
6011 * priority of the task attempting to take the mutex, but the
6012 * current priority of the mutex holder is not lower than the
6013 * priority of the task attempting to take the mutex.
6014 * Therefore the mutex holder must have already inherited a
6015 * priority, but inheritance would have occurred if that had
6016 * not been the case. */
6021 mtCOVERAGE_TEST_MARKER();
6027 mtCOVERAGE_TEST_MARKER();
6033 #endif /* configUSE_MUTEXES */
6034 /*-----------------------------------------------------------*/
6036 #if ( configUSE_MUTEXES == 1 )
6038 BaseType_t xTaskPriorityDisinherit( TaskHandle_t const pxMutexHolder )
6040 TCB_t * const pxTCB = pxMutexHolder;
6041 BaseType_t xReturn = pdFALSE;
6043 if( pxMutexHolder != NULL )
6045 /* A task can only have an inherited priority if it holds the mutex.
6046 * If the mutex is held by a task then it cannot be given from an
6047 * interrupt, and if a mutex is given by the holding task then it must
6048 * be the running state task. */
6049 configASSERT( pxTCB == pxCurrentTCB );
6050 configASSERT( pxTCB->uxMutexesHeld );
6051 ( pxTCB->uxMutexesHeld )--;
6053 /* Has the holder of the mutex inherited the priority of another
6055 if( pxTCB->uxPriority != pxTCB->uxBasePriority )
6057 /* Only disinherit if no other mutexes are held. */
6058 if( pxTCB->uxMutexesHeld == ( UBaseType_t ) 0 )
6060 /* A task can only have an inherited priority if it holds
6061 * the mutex. If the mutex is held by a task then it cannot be
6062 * given from an interrupt, and if a mutex is given by the
6063 * holding task then it must be the running state task. Remove
6064 * the holding task from the ready list. */
6065 if( uxListRemove( &( pxTCB->xStateListItem ) ) == ( UBaseType_t ) 0 )
6067 portRESET_READY_PRIORITY( pxTCB->uxPriority, uxTopReadyPriority );
6071 mtCOVERAGE_TEST_MARKER();
6074 /* Disinherit the priority before adding the task into the
6075 * new ready list. */
6076 traceTASK_PRIORITY_DISINHERIT( pxTCB, pxTCB->uxBasePriority );
6077 pxTCB->uxPriority = pxTCB->uxBasePriority;
6079 /* Reset the event list item value. It cannot be in use for
6080 * any other purpose if this task is running, and it must be
6081 * running to give back the mutex. */
6082 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. */
6083 prvAddTaskToReadyList( pxTCB );
6084 #if ( configNUMBER_OF_CORES > 1 )
6086 /* The priority of the task is dropped. Yield the core on
6087 * which the task is running. */
6088 if( taskTASK_IS_RUNNING( pxTCB ) == pdTRUE )
6090 prvYieldCore( pxTCB->xTaskRunState );
6093 #endif /* if ( configNUMBER_OF_CORES > 1 ) */
6095 /* Return true to indicate that a context switch is required.
6096 * This is only actually required in the corner case whereby
6097 * multiple mutexes were held and the mutexes were given back
6098 * in an order different to that in which they were taken.
6099 * If a context switch did not occur when the first mutex was
6100 * returned, even if a task was waiting on it, then a context
6101 * switch should occur when the last mutex is returned whether
6102 * a task is waiting on it or not. */
6107 mtCOVERAGE_TEST_MARKER();
6112 mtCOVERAGE_TEST_MARKER();
6117 mtCOVERAGE_TEST_MARKER();
6123 #endif /* configUSE_MUTEXES */
6124 /*-----------------------------------------------------------*/
6126 #if ( configUSE_MUTEXES == 1 )
6128 void vTaskPriorityDisinheritAfterTimeout( TaskHandle_t const pxMutexHolder,
6129 UBaseType_t uxHighestPriorityWaitingTask )
6131 TCB_t * const pxTCB = pxMutexHolder;
6132 UBaseType_t uxPriorityUsedOnEntry, uxPriorityToUse;
6133 const UBaseType_t uxOnlyOneMutexHeld = ( UBaseType_t ) 1;
6135 if( pxMutexHolder != NULL )
6137 /* If pxMutexHolder is not NULL then the holder must hold at least
6139 configASSERT( pxTCB->uxMutexesHeld );
6141 /* Determine the priority to which the priority of the task that
6142 * holds the mutex should be set. This will be the greater of the
6143 * holding task's base priority and the priority of the highest
6144 * priority task that is waiting to obtain the mutex. */
6145 if( pxTCB->uxBasePriority < uxHighestPriorityWaitingTask )
6147 uxPriorityToUse = uxHighestPriorityWaitingTask;
6151 uxPriorityToUse = pxTCB->uxBasePriority;
6154 /* Does the priority need to change? */
6155 if( pxTCB->uxPriority != uxPriorityToUse )
6157 /* Only disinherit if no other mutexes are held. This is a
6158 * simplification in the priority inheritance implementation. If
6159 * the task that holds the mutex is also holding other mutexes then
6160 * the other mutexes may have caused the priority inheritance. */
6161 if( pxTCB->uxMutexesHeld == uxOnlyOneMutexHeld )
6163 /* If a task has timed out because it already holds the
6164 * mutex it was trying to obtain then it cannot of inherited
6165 * its own priority. */
6166 configASSERT( pxTCB != pxCurrentTCB );
6168 /* Disinherit the priority, remembering the previous
6169 * priority to facilitate determining the subject task's
6171 traceTASK_PRIORITY_DISINHERIT( pxTCB, uxPriorityToUse );
6172 uxPriorityUsedOnEntry = pxTCB->uxPriority;
6173 pxTCB->uxPriority = uxPriorityToUse;
6175 /* Only reset the event list item value if the value is not
6176 * being used for anything else. */
6177 if( ( listGET_LIST_ITEM_VALUE( &( pxTCB->xEventListItem ) ) & taskEVENT_LIST_ITEM_VALUE_IN_USE ) == 0UL )
6179 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. */
6183 mtCOVERAGE_TEST_MARKER();
6186 /* If the running task is not the task that holds the mutex
6187 * then the task that holds the mutex could be in either the
6188 * Ready, Blocked or Suspended states. Only remove the task
6189 * from its current state list if it is in the Ready state as
6190 * the task's priority is going to change and there is one
6191 * Ready list per priority. */
6192 if( listIS_CONTAINED_WITHIN( &( pxReadyTasksLists[ uxPriorityUsedOnEntry ] ), &( pxTCB->xStateListItem ) ) != pdFALSE )
6194 if( uxListRemove( &( pxTCB->xStateListItem ) ) == ( UBaseType_t ) 0 )
6196 /* It is known that the task is in its ready list so
6197 * there is no need to check again and the port level
6198 * reset macro can be called directly. */
6199 portRESET_READY_PRIORITY( pxTCB->uxPriority, uxTopReadyPriority );
6203 mtCOVERAGE_TEST_MARKER();
6206 prvAddTaskToReadyList( pxTCB );
6207 #if ( configNUMBER_OF_CORES > 1 )
6209 /* The priority of the task is dropped. Yield the core on
6210 * which the task is running. */
6211 if( taskTASK_IS_RUNNING( pxTCB ) == pdTRUE )
6213 prvYieldCore( pxTCB->xTaskRunState );
6216 #endif /* if ( configNUMBER_OF_CORES > 1 ) */
6220 mtCOVERAGE_TEST_MARKER();
6225 mtCOVERAGE_TEST_MARKER();
6230 mtCOVERAGE_TEST_MARKER();
6235 mtCOVERAGE_TEST_MARKER();
6239 #endif /* configUSE_MUTEXES */
6240 /*-----------------------------------------------------------*/
6242 #if ( configNUMBER_OF_CORES > 1 )
6244 /* If not in a critical section then yield immediately.
6245 * Otherwise set xYieldPendings to true to wait to
6246 * yield until exiting the critical section.
6248 void vTaskYieldWithinAPI( void )
6250 if( portGET_CRITICAL_NESTING_COUNT() == 0U )
6256 xYieldPendings[ portGET_CORE_ID() ] = pdTRUE;
6259 #endif /* #if ( configNUMBER_OF_CORES > 1 ) */
6261 /*-----------------------------------------------------------*/
6263 #if ( ( portCRITICAL_NESTING_IN_TCB == 1 ) && ( configNUMBER_OF_CORES == 1 ) )
6265 void vTaskEnterCritical( void )
6267 portDISABLE_INTERRUPTS();
6269 if( xSchedulerRunning != pdFALSE )
6271 ( pxCurrentTCB->uxCriticalNesting )++;
6273 /* This is not the interrupt safe version of the enter critical
6274 * function so assert() if it is being called from an interrupt
6275 * context. Only API functions that end in "FromISR" can be used in an
6276 * interrupt. Only assert if the critical nesting count is 1 to
6277 * protect against recursive calls if the assert function also uses a
6278 * critical section. */
6279 if( pxCurrentTCB->uxCriticalNesting == 1 )
6281 portASSERT_IF_IN_ISR();
6286 mtCOVERAGE_TEST_MARKER();
6290 #endif /* #if ( ( portCRITICAL_NESTING_IN_TCB == 1 ) && ( configNUMBER_OF_CORES == 1 ) ) */
6291 /*-----------------------------------------------------------*/
6293 #if ( configNUMBER_OF_CORES > 1 )
6295 void vTaskEnterCritical( void )
6297 portDISABLE_INTERRUPTS();
6299 if( xSchedulerRunning != pdFALSE )
6301 if( portGET_CRITICAL_NESTING_COUNT() == 0U )
6303 portGET_TASK_LOCK();
6307 portINCREMENT_CRITICAL_NESTING_COUNT();
6309 /* This is not the interrupt safe version of the enter critical
6310 * function so assert() if it is being called from an interrupt
6311 * context. Only API functions that end in "FromISR" can be used in an
6312 * interrupt. Only assert if the critical nesting count is 1 to
6313 * protect against recursive calls if the assert function also uses a
6314 * critical section. */
6315 if( portGET_CRITICAL_NESTING_COUNT() == 1U )
6317 portASSERT_IF_IN_ISR();
6319 if( uxSchedulerSuspended == 0U )
6321 /* The only time there would be a problem is if this is called
6322 * before a context switch and vTaskExitCritical() is called
6323 * after pxCurrentTCB changes. Therefore this should not be
6324 * used within vTaskSwitchContext(). */
6325 prvCheckForRunStateChange();
6331 mtCOVERAGE_TEST_MARKER();
6335 #endif /* #if ( configNUMBER_OF_CORES > 1 ) */
6337 /*-----------------------------------------------------------*/
6339 #if ( configNUMBER_OF_CORES > 1 )
6341 UBaseType_t vTaskEnterCriticalFromISR( void )
6343 UBaseType_t uxSavedInterruptStatus = 0;
6345 if( xSchedulerRunning != pdFALSE )
6347 uxSavedInterruptStatus = portSET_INTERRUPT_MASK_FROM_ISR();
6349 if( portGET_CRITICAL_NESTING_COUNT() == 0U )
6354 portINCREMENT_CRITICAL_NESTING_COUNT();
6358 mtCOVERAGE_TEST_MARKER();
6361 return uxSavedInterruptStatus;
6364 #endif /* #if ( configNUMBER_OF_CORES > 1 ) */
6365 /*-----------------------------------------------------------*/
6367 #if ( ( portCRITICAL_NESTING_IN_TCB == 1 ) && ( configNUMBER_OF_CORES == 1 ) )
6369 void vTaskExitCritical( void )
6371 if( xSchedulerRunning != pdFALSE )
6373 /* If pxCurrentTCB->uxCriticalNesting is zero then this function
6374 * does not match a previous call to vTaskEnterCritical(). */
6375 configASSERT( pxCurrentTCB->uxCriticalNesting > 0U );
6377 /* This function should not be called in ISR. Use vTaskExitCriticalFromISR
6378 * to exit critical section from ISR. */
6379 portASSERT_IF_IN_ISR();
6381 if( pxCurrentTCB->uxCriticalNesting > 0U )
6383 ( pxCurrentTCB->uxCriticalNesting )--;
6385 if( pxCurrentTCB->uxCriticalNesting == 0U )
6387 portENABLE_INTERRUPTS();
6391 mtCOVERAGE_TEST_MARKER();
6396 mtCOVERAGE_TEST_MARKER();
6401 mtCOVERAGE_TEST_MARKER();
6405 #endif /* #if ( ( portCRITICAL_NESTING_IN_TCB == 1 ) && ( configNUMBER_OF_CORES == 1 ) ) */
6406 /*-----------------------------------------------------------*/
6408 #if ( configNUMBER_OF_CORES > 1 )
6410 void vTaskExitCritical( void )
6412 if( xSchedulerRunning != pdFALSE )
6414 /* If critical nesting count is zero then this function
6415 * does not match a previous call to vTaskEnterCritical(). */
6416 configASSERT( portGET_CRITICAL_NESTING_COUNT() > 0U );
6418 /* This function should not be called in ISR. Use vTaskExitCriticalFromISR
6419 * to exit critical section from ISR. */
6420 portASSERT_IF_IN_ISR();
6422 if( portGET_CRITICAL_NESTING_COUNT() > 0U )
6424 portDECREMENT_CRITICAL_NESTING_COUNT();
6426 if( portGET_CRITICAL_NESTING_COUNT() == 0U )
6428 BaseType_t xYieldCurrentTask;
6430 /* Get the xYieldPending stats inside the critical section. */
6431 xYieldCurrentTask = xYieldPendings[ portGET_CORE_ID() ];
6433 portRELEASE_ISR_LOCK();
6434 portRELEASE_TASK_LOCK();
6435 portENABLE_INTERRUPTS();
6437 /* When a task yields in a critical section it just sets
6438 * xYieldPending to true. So now that we have exited the
6439 * critical section check if xYieldPending is true, and
6441 if( xYieldCurrentTask != pdFALSE )
6448 mtCOVERAGE_TEST_MARKER();
6453 mtCOVERAGE_TEST_MARKER();
6458 mtCOVERAGE_TEST_MARKER();
6462 #endif /* #if ( configNUMBER_OF_CORES > 1 ) */
6463 /*-----------------------------------------------------------*/
6465 #if ( configNUMBER_OF_CORES > 1 )
6467 void vTaskExitCriticalFromISR( UBaseType_t uxSavedInterruptStatus )
6469 if( xSchedulerRunning != pdFALSE )
6471 /* If critical nesting count is zero then this function
6472 * does not match a previous call to vTaskEnterCritical(). */
6473 configASSERT( portGET_CRITICAL_NESTING_COUNT() > 0U );
6475 if( portGET_CRITICAL_NESTING_COUNT() > 0U )
6477 portDECREMENT_CRITICAL_NESTING_COUNT();
6479 if( portGET_CRITICAL_NESTING_COUNT() == 0U )
6481 portRELEASE_ISR_LOCK();
6482 portCLEAR_INTERRUPT_MASK_FROM_ISR( uxSavedInterruptStatus );
6486 mtCOVERAGE_TEST_MARKER();
6491 mtCOVERAGE_TEST_MARKER();
6496 mtCOVERAGE_TEST_MARKER();
6500 #endif /* #if ( configNUMBER_OF_CORES > 1 ) */
6501 /*-----------------------------------------------------------*/
6503 #if ( configUSE_STATS_FORMATTING_FUNCTIONS > 0 )
6505 static char * prvWriteNameToBuffer( char * pcBuffer,
6506 const char * pcTaskName )
6510 /* Start by copying the entire string. */
6511 ( void ) strcpy( pcBuffer, pcTaskName );
6513 /* Pad the end of the string with spaces to ensure columns line up when
6515 for( x = strlen( pcBuffer ); x < ( size_t ) ( ( size_t ) configMAX_TASK_NAME_LEN - 1U ); x++ )
6517 pcBuffer[ x ] = ' ';
6521 pcBuffer[ x ] = ( char ) 0x00;
6523 /* Return the new end of string. */
6524 return &( pcBuffer[ x ] );
6527 #endif /* ( configUSE_STATS_FORMATTING_FUNCTIONS > 0 ) */
6528 /*-----------------------------------------------------------*/
6530 #if ( ( configUSE_TRACE_FACILITY == 1 ) && ( configUSE_STATS_FORMATTING_FUNCTIONS > 0 ) )
6532 void vTaskList( char * pcWriteBuffer )
6534 TaskStatus_t * pxTaskStatusArray;
6535 UBaseType_t uxArraySize, x;
6541 * This function is provided for convenience only, and is used by many
6542 * of the demo applications. Do not consider it to be part of the
6545 * vTaskList() calls uxTaskGetSystemState(), then formats part of the
6546 * uxTaskGetSystemState() output into a human readable table that
6547 * displays task: names, states, priority, stack usage and task number.
6548 * Stack usage specified as the number of unused StackType_t words stack can hold
6549 * on top of stack - not the number of bytes.
6551 * vTaskList() has a dependency on the sprintf() C library function that
6552 * might bloat the code size, use a lot of stack, and provide different
6553 * results on different platforms. An alternative, tiny, third party,
6554 * and limited functionality implementation of sprintf() is provided in
6555 * many of the FreeRTOS/Demo sub-directories in a file called
6556 * printf-stdarg.c (note printf-stdarg.c does not provide a full
6557 * snprintf() implementation!).
6559 * It is recommended that production systems call uxTaskGetSystemState()
6560 * directly to get access to raw stats data, rather than indirectly
6561 * through a call to vTaskList().
6565 /* Make sure the write buffer does not contain a string. */
6566 *pcWriteBuffer = ( char ) 0x00;
6568 /* Take a snapshot of the number of tasks in case it changes while this
6569 * function is executing. */
6570 uxArraySize = uxCurrentNumberOfTasks;
6572 /* Allocate an array index for each task. NOTE! if
6573 * configSUPPORT_DYNAMIC_ALLOCATION is set to 0 then pvPortMalloc() will
6574 * equate to NULL. */
6575 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. */
6577 if( pxTaskStatusArray != NULL )
6579 /* Generate the (binary) data. */
6580 uxArraySize = uxTaskGetSystemState( pxTaskStatusArray, uxArraySize, NULL );
6582 /* Create a human readable table from the binary data. */
6583 for( x = 0; x < uxArraySize; x++ )
6585 switch( pxTaskStatusArray[ x ].eCurrentState )
6588 cStatus = tskRUNNING_CHAR;
6592 cStatus = tskREADY_CHAR;
6596 cStatus = tskBLOCKED_CHAR;
6600 cStatus = tskSUSPENDED_CHAR;
6604 cStatus = tskDELETED_CHAR;
6607 case eInvalid: /* Fall through. */
6608 default: /* Should not get here, but it is included
6609 * to prevent static checking errors. */
6610 cStatus = ( char ) 0x00;
6614 /* Write the task name to the string, padding with spaces so it
6615 * can be printed in tabular form more easily. */
6616 pcWriteBuffer = prvWriteNameToBuffer( pcWriteBuffer, pxTaskStatusArray[ x ].pcTaskName );
6618 /* Write the rest of the string. */
6619 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. */
6620 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. */
6623 /* Free the array again. NOTE! If configSUPPORT_DYNAMIC_ALLOCATION
6624 * is 0 then vPortFree() will be #defined to nothing. */
6625 vPortFree( pxTaskStatusArray );
6629 mtCOVERAGE_TEST_MARKER();
6633 #endif /* ( ( configUSE_TRACE_FACILITY == 1 ) && ( configUSE_STATS_FORMATTING_FUNCTIONS > 0 ) ) */
6634 /*----------------------------------------------------------*/
6636 #if ( ( configGENERATE_RUN_TIME_STATS == 1 ) && ( configUSE_STATS_FORMATTING_FUNCTIONS > 0 ) && ( configUSE_TRACE_FACILITY == 1 ) )
6638 void vTaskGetRunTimeStats( char * pcWriteBuffer )
6640 TaskStatus_t * pxTaskStatusArray;
6641 UBaseType_t uxArraySize, x;
6642 configRUN_TIME_COUNTER_TYPE ulTotalTime, ulStatsAsPercentage;
6647 * This function is provided for convenience only, and is used by many
6648 * of the demo applications. Do not consider it to be part of the
6651 * vTaskGetRunTimeStats() calls uxTaskGetSystemState(), then formats part
6652 * of the uxTaskGetSystemState() output into a human readable table that
6653 * displays the amount of time each task has spent in the Running state
6654 * in both absolute and percentage terms.
6656 * vTaskGetRunTimeStats() has a dependency on the sprintf() C library
6657 * function that might bloat the code size, use a lot of stack, and
6658 * provide different results on different platforms. An alternative,
6659 * tiny, third party, and limited functionality implementation of
6660 * sprintf() is provided in many of the FreeRTOS/Demo sub-directories in
6661 * a file called printf-stdarg.c (note printf-stdarg.c does not provide
6662 * a full snprintf() implementation!).
6664 * It is recommended that production systems call uxTaskGetSystemState()
6665 * directly to get access to raw stats data, rather than indirectly
6666 * through a call to vTaskGetRunTimeStats().
6669 /* Make sure the write buffer does not contain a string. */
6670 *pcWriteBuffer = ( char ) 0x00;
6672 /* Take a snapshot of the number of tasks in case it changes while this
6673 * function is executing. */
6674 uxArraySize = uxCurrentNumberOfTasks;
6676 /* Allocate an array index for each task. NOTE! If
6677 * configSUPPORT_DYNAMIC_ALLOCATION is set to 0 then pvPortMalloc() will
6678 * equate to NULL. */
6679 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. */
6681 if( pxTaskStatusArray != NULL )
6683 /* Generate the (binary) data. */
6684 uxArraySize = uxTaskGetSystemState( pxTaskStatusArray, uxArraySize, &ulTotalTime );
6686 /* For percentage calculations. */
6687 ulTotalTime /= 100UL;
6689 /* Avoid divide by zero errors. */
6690 if( ulTotalTime > 0UL )
6692 /* Create a human readable table from the binary data. */
6693 for( x = 0; x < uxArraySize; x++ )
6695 /* What percentage of the total run time has the task used?
6696 * This will always be rounded down to the nearest integer.
6697 * ulTotalRunTime has already been divided by 100. */
6698 ulStatsAsPercentage = pxTaskStatusArray[ x ].ulRunTimeCounter / ulTotalTime;
6700 /* Write the task name to the string, padding with
6701 * spaces so it can be printed in tabular form more
6703 pcWriteBuffer = prvWriteNameToBuffer( pcWriteBuffer, pxTaskStatusArray[ x ].pcTaskName );
6705 if( ulStatsAsPercentage > 0UL )
6707 #ifdef portLU_PRINTF_SPECIFIER_REQUIRED
6709 sprintf( pcWriteBuffer, "\t%lu\t\t%lu%%\r\n", pxTaskStatusArray[ x ].ulRunTimeCounter, ulStatsAsPercentage );
6713 /* sizeof( int ) == sizeof( long ) so a smaller
6714 * printf() library can be used. */
6715 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. */
6721 /* If the percentage is zero here then the task has
6722 * consumed less than 1% of the total run time. */
6723 #ifdef portLU_PRINTF_SPECIFIER_REQUIRED
6725 sprintf( pcWriteBuffer, "\t%lu\t\t<1%%\r\n", pxTaskStatusArray[ x ].ulRunTimeCounter );
6729 /* sizeof( int ) == sizeof( long ) so a smaller
6730 * printf() library can be used. */
6731 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. */
6736 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. */
6741 mtCOVERAGE_TEST_MARKER();
6744 /* Free the array again. NOTE! If configSUPPORT_DYNAMIC_ALLOCATION
6745 * is 0 then vPortFree() will be #defined to nothing. */
6746 vPortFree( pxTaskStatusArray );
6750 mtCOVERAGE_TEST_MARKER();
6754 #endif /* ( ( configGENERATE_RUN_TIME_STATS == 1 ) && ( configUSE_STATS_FORMATTING_FUNCTIONS > 0 ) ) */
6755 /*-----------------------------------------------------------*/
6757 TickType_t uxTaskResetEventItemValue( void )
6759 TickType_t uxReturn;
6761 uxReturn = listGET_LIST_ITEM_VALUE( &( pxCurrentTCB->xEventListItem ) );
6763 /* Reset the event list item to its normal value - so it can be used with
6764 * queues and semaphores. */
6765 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. */
6769 /*-----------------------------------------------------------*/
6771 #if ( configUSE_MUTEXES == 1 )
6773 TaskHandle_t pvTaskIncrementMutexHeldCount( void )
6777 pxTCB = pxCurrentTCB;
6779 /* If xSemaphoreCreateMutex() is called before any tasks have been created
6780 * then pxCurrentTCB will be NULL. */
6783 ( pxTCB->uxMutexesHeld )++;
6789 #endif /* configUSE_MUTEXES */
6790 /*-----------------------------------------------------------*/
6792 #if ( configUSE_TASK_NOTIFICATIONS == 1 )
6794 uint32_t ulTaskGenericNotifyTake( UBaseType_t uxIndexToWaitOn,
6795 BaseType_t xClearCountOnExit,
6796 TickType_t xTicksToWait )
6800 configASSERT( uxIndexToWaitOn < configTASK_NOTIFICATION_ARRAY_ENTRIES );
6802 taskENTER_CRITICAL();
6804 /* Only block if the notification count is not already non-zero. */
6805 if( pxCurrentTCB->ulNotifiedValue[ uxIndexToWaitOn ] == 0UL )
6807 /* Mark this task as waiting for a notification. */
6808 pxCurrentTCB->ucNotifyState[ uxIndexToWaitOn ] = taskWAITING_NOTIFICATION;
6810 if( xTicksToWait > ( TickType_t ) 0 )
6812 prvAddCurrentTaskToDelayedList( xTicksToWait, pdTRUE );
6813 traceTASK_NOTIFY_TAKE_BLOCK( uxIndexToWaitOn );
6815 /* All ports are written to allow a yield in a critical
6816 * section (some will yield immediately, others wait until the
6817 * critical section exits) - but it is not something that
6818 * application code should ever do. */
6819 #if ( configNUMBER_OF_CORES == 1 )
6821 portYIELD_WITHIN_API();
6825 vTaskYieldWithinAPI();
6831 mtCOVERAGE_TEST_MARKER();
6836 mtCOVERAGE_TEST_MARKER();
6839 taskEXIT_CRITICAL();
6841 taskENTER_CRITICAL();
6843 traceTASK_NOTIFY_TAKE( uxIndexToWaitOn );
6844 ulReturn = pxCurrentTCB->ulNotifiedValue[ uxIndexToWaitOn ];
6846 if( ulReturn != 0UL )
6848 if( xClearCountOnExit != pdFALSE )
6850 pxCurrentTCB->ulNotifiedValue[ uxIndexToWaitOn ] = 0UL;
6854 pxCurrentTCB->ulNotifiedValue[ uxIndexToWaitOn ] = ulReturn - ( uint32_t ) 1;
6859 mtCOVERAGE_TEST_MARKER();
6862 pxCurrentTCB->ucNotifyState[ uxIndexToWaitOn ] = taskNOT_WAITING_NOTIFICATION;
6864 taskEXIT_CRITICAL();
6869 #endif /* configUSE_TASK_NOTIFICATIONS */
6870 /*-----------------------------------------------------------*/
6872 #if ( configUSE_TASK_NOTIFICATIONS == 1 )
6874 BaseType_t xTaskGenericNotifyWait( UBaseType_t uxIndexToWaitOn,
6875 uint32_t ulBitsToClearOnEntry,
6876 uint32_t ulBitsToClearOnExit,
6877 uint32_t * pulNotificationValue,
6878 TickType_t xTicksToWait )
6882 configASSERT( uxIndexToWaitOn < configTASK_NOTIFICATION_ARRAY_ENTRIES );
6884 taskENTER_CRITICAL();
6886 /* Only block if a notification is not already pending. */
6887 if( pxCurrentTCB->ucNotifyState[ uxIndexToWaitOn ] != taskNOTIFICATION_RECEIVED )
6889 /* Clear bits in the task's notification value as bits may get
6890 * set by the notifying task or interrupt. This can be used to
6891 * clear the value to zero. */
6892 pxCurrentTCB->ulNotifiedValue[ uxIndexToWaitOn ] &= ~ulBitsToClearOnEntry;
6894 /* Mark this task as waiting for a notification. */
6895 pxCurrentTCB->ucNotifyState[ uxIndexToWaitOn ] = taskWAITING_NOTIFICATION;
6897 if( xTicksToWait > ( TickType_t ) 0 )
6899 prvAddCurrentTaskToDelayedList( xTicksToWait, pdTRUE );
6900 traceTASK_NOTIFY_WAIT_BLOCK( uxIndexToWaitOn );
6902 /* All ports are written to allow a yield in a critical
6903 * section (some will yield immediately, others wait until the
6904 * critical section exits) - but it is not something that
6905 * application code should ever do. */
6906 #if ( configNUMBER_OF_CORES == 1 )
6908 portYIELD_WITHIN_API();
6912 vTaskYieldWithinAPI();
6918 mtCOVERAGE_TEST_MARKER();
6923 mtCOVERAGE_TEST_MARKER();
6926 taskEXIT_CRITICAL();
6928 taskENTER_CRITICAL();
6930 traceTASK_NOTIFY_WAIT( uxIndexToWaitOn );
6932 if( pulNotificationValue != NULL )
6934 /* Output the current notification value, which may or may not
6936 *pulNotificationValue = pxCurrentTCB->ulNotifiedValue[ uxIndexToWaitOn ];
6939 /* If ucNotifyValue is set then either the task never entered the
6940 * blocked state (because a notification was already pending) or the
6941 * task unblocked because of a notification. Otherwise the task
6942 * unblocked because of a timeout. */
6943 if( pxCurrentTCB->ucNotifyState[ uxIndexToWaitOn ] != taskNOTIFICATION_RECEIVED )
6945 /* A notification was not received. */
6950 /* A notification was already pending or a notification was
6951 * received while the task was waiting. */
6952 pxCurrentTCB->ulNotifiedValue[ uxIndexToWaitOn ] &= ~ulBitsToClearOnExit;
6956 pxCurrentTCB->ucNotifyState[ uxIndexToWaitOn ] = taskNOT_WAITING_NOTIFICATION;
6958 taskEXIT_CRITICAL();
6963 #endif /* configUSE_TASK_NOTIFICATIONS */
6964 /*-----------------------------------------------------------*/
6966 #if ( configUSE_TASK_NOTIFICATIONS == 1 )
6968 BaseType_t xTaskGenericNotify( TaskHandle_t xTaskToNotify,
6969 UBaseType_t uxIndexToNotify,
6971 eNotifyAction eAction,
6972 uint32_t * pulPreviousNotificationValue )
6975 BaseType_t xReturn = pdPASS;
6976 uint8_t ucOriginalNotifyState;
6978 configASSERT( uxIndexToNotify < configTASK_NOTIFICATION_ARRAY_ENTRIES );
6979 configASSERT( xTaskToNotify );
6980 pxTCB = xTaskToNotify;
6982 taskENTER_CRITICAL();
6984 if( pulPreviousNotificationValue != NULL )
6986 *pulPreviousNotificationValue = pxTCB->ulNotifiedValue[ uxIndexToNotify ];
6989 ucOriginalNotifyState = pxTCB->ucNotifyState[ uxIndexToNotify ];
6991 pxTCB->ucNotifyState[ uxIndexToNotify ] = taskNOTIFICATION_RECEIVED;
6996 pxTCB->ulNotifiedValue[ uxIndexToNotify ] |= ulValue;
7000 ( pxTCB->ulNotifiedValue[ uxIndexToNotify ] )++;
7003 case eSetValueWithOverwrite:
7004 pxTCB->ulNotifiedValue[ uxIndexToNotify ] = ulValue;
7007 case eSetValueWithoutOverwrite:
7009 if( ucOriginalNotifyState != taskNOTIFICATION_RECEIVED )
7011 pxTCB->ulNotifiedValue[ uxIndexToNotify ] = ulValue;
7015 /* The value could not be written to the task. */
7023 /* The task is being notified without its notify value being
7029 /* Should not get here if all enums are handled.
7030 * Artificially force an assert by testing a value the
7031 * compiler can't assume is const. */
7032 configASSERT( xTickCount == ( TickType_t ) 0 );
7037 traceTASK_NOTIFY( uxIndexToNotify );
7039 /* If the task is in the blocked state specifically to wait for a
7040 * notification then unblock it now. */
7041 if( ucOriginalNotifyState == taskWAITING_NOTIFICATION )
7043 listREMOVE_ITEM( &( pxTCB->xStateListItem ) );
7044 prvAddTaskToReadyList( pxTCB );
7046 /* The task should not have been on an event list. */
7047 configASSERT( listLIST_ITEM_CONTAINER( &( pxTCB->xEventListItem ) ) == NULL );
7049 #if ( configUSE_TICKLESS_IDLE != 0 )
7051 /* If a task is blocked waiting for a notification then
7052 * xNextTaskUnblockTime might be set to the blocked task's time
7053 * out time. If the task is unblocked for a reason other than
7054 * a timeout xNextTaskUnblockTime is normally left unchanged,
7055 * because it will automatically get reset to a new value when
7056 * the tick count equals xNextTaskUnblockTime. However if
7057 * tickless idling is used it might be more important to enter
7058 * sleep mode at the earliest possible time - so reset
7059 * xNextTaskUnblockTime here to ensure it is updated at the
7060 * earliest possible time. */
7061 prvResetNextTaskUnblockTime();
7065 #if ( configNUMBER_OF_CORES == 1 )
7067 if( pxTCB->uxPriority > pxCurrentTCB->uxPriority )
7069 /* The notified task has a priority above the currently
7070 * executing task so a yield is required. */
7071 taskYIELD_IF_USING_PREEMPTION();
7075 mtCOVERAGE_TEST_MARKER();
7078 #else /* #if ( configNUMBER_OF_CORES == 1 ) */
7080 #if ( configUSE_PREEMPTION == 1 )
7082 prvYieldForTask( pxTCB );
7086 #endif /* #if ( configNUMBER_OF_CORES == 1 ) */
7090 mtCOVERAGE_TEST_MARKER();
7093 taskEXIT_CRITICAL();
7098 #endif /* configUSE_TASK_NOTIFICATIONS */
7099 /*-----------------------------------------------------------*/
7101 #if ( configUSE_TASK_NOTIFICATIONS == 1 )
7103 BaseType_t xTaskGenericNotifyFromISR( TaskHandle_t xTaskToNotify,
7104 UBaseType_t uxIndexToNotify,
7106 eNotifyAction eAction,
7107 uint32_t * pulPreviousNotificationValue,
7108 BaseType_t * pxHigherPriorityTaskWoken )
7111 uint8_t ucOriginalNotifyState;
7112 BaseType_t xReturn = pdPASS;
7113 UBaseType_t uxSavedInterruptStatus;
7115 configASSERT( xTaskToNotify );
7116 configASSERT( uxIndexToNotify < configTASK_NOTIFICATION_ARRAY_ENTRIES );
7118 /* RTOS ports that support interrupt nesting have the concept of a
7119 * maximum system call (or maximum API call) interrupt priority.
7120 * Interrupts that are above the maximum system call priority are keep
7121 * permanently enabled, even when the RTOS kernel is in a critical section,
7122 * but cannot make any calls to FreeRTOS API functions. If configASSERT()
7123 * is defined in FreeRTOSConfig.h then
7124 * portASSERT_IF_INTERRUPT_PRIORITY_INVALID() will result in an assertion
7125 * failure if a FreeRTOS API function is called from an interrupt that has
7126 * been assigned a priority above the configured maximum system call
7127 * priority. Only FreeRTOS functions that end in FromISR can be called
7128 * from interrupts that have been assigned a priority at or (logically)
7129 * below the maximum system call interrupt priority. FreeRTOS maintains a
7130 * separate interrupt safe API to ensure interrupt entry is as fast and as
7131 * simple as possible. More information (albeit Cortex-M specific) is
7132 * provided on the following link:
7133 * https://www.FreeRTOS.org/RTOS-Cortex-M3-M4.html */
7134 portASSERT_IF_INTERRUPT_PRIORITY_INVALID();
7136 pxTCB = xTaskToNotify;
7138 uxSavedInterruptStatus = taskENTER_CRITICAL_FROM_ISR();
7140 if( pulPreviousNotificationValue != NULL )
7142 *pulPreviousNotificationValue = pxTCB->ulNotifiedValue[ uxIndexToNotify ];
7145 ucOriginalNotifyState = pxTCB->ucNotifyState[ uxIndexToNotify ];
7146 pxTCB->ucNotifyState[ uxIndexToNotify ] = taskNOTIFICATION_RECEIVED;
7151 pxTCB->ulNotifiedValue[ uxIndexToNotify ] |= ulValue;
7155 ( pxTCB->ulNotifiedValue[ uxIndexToNotify ] )++;
7158 case eSetValueWithOverwrite:
7159 pxTCB->ulNotifiedValue[ uxIndexToNotify ] = ulValue;
7162 case eSetValueWithoutOverwrite:
7164 if( ucOriginalNotifyState != taskNOTIFICATION_RECEIVED )
7166 pxTCB->ulNotifiedValue[ uxIndexToNotify ] = ulValue;
7170 /* The value could not be written to the task. */
7178 /* The task is being notified without its notify value being
7184 /* Should not get here if all enums are handled.
7185 * Artificially force an assert by testing a value the
7186 * compiler can't assume is const. */
7187 configASSERT( xTickCount == ( TickType_t ) 0 );
7191 traceTASK_NOTIFY_FROM_ISR( uxIndexToNotify );
7193 /* If the task is in the blocked state specifically to wait for a
7194 * notification then unblock it now. */
7195 if( ucOriginalNotifyState == taskWAITING_NOTIFICATION )
7197 /* The task should not have been on an event list. */
7198 configASSERT( listLIST_ITEM_CONTAINER( &( pxTCB->xEventListItem ) ) == NULL );
7200 if( uxSchedulerSuspended == ( UBaseType_t ) 0U )
7202 listREMOVE_ITEM( &( pxTCB->xStateListItem ) );
7203 prvAddTaskToReadyList( pxTCB );
7207 /* The delayed and ready lists cannot be accessed, so hold
7208 * this task pending until the scheduler is resumed. */
7209 listINSERT_END( &( xPendingReadyList ), &( pxTCB->xEventListItem ) );
7212 #if ( configNUMBER_OF_CORES == 1 )
7214 if( pxTCB->uxPriority > pxCurrentTCB->uxPriority )
7216 /* The notified task has a priority above the currently
7217 * executing task so a yield is required. */
7218 if( pxHigherPriorityTaskWoken != NULL )
7220 *pxHigherPriorityTaskWoken = pdTRUE;
7223 /* Mark that a yield is pending in case the user is not
7224 * using the "xHigherPriorityTaskWoken" parameter to an ISR
7225 * safe FreeRTOS function. */
7226 xYieldPendings[ 0 ] = pdTRUE;
7230 mtCOVERAGE_TEST_MARKER();
7233 #else /* #if ( configNUMBER_OF_CORES == 1 ) */
7235 #if ( configUSE_PREEMPTION == 1 )
7237 prvYieldForTask( pxTCB );
7239 if( xYieldPendings[ portGET_CORE_ID() ] == pdTRUE )
7241 if( pxHigherPriorityTaskWoken != NULL )
7243 *pxHigherPriorityTaskWoken = pdTRUE;
7247 #endif /* if ( configUSE_PREEMPTION == 1 ) */
7249 #endif /* #if ( configNUMBER_OF_CORES == 1 ) */
7252 taskEXIT_CRITICAL_FROM_ISR( uxSavedInterruptStatus );
7257 #endif /* configUSE_TASK_NOTIFICATIONS */
7258 /*-----------------------------------------------------------*/
7260 #if ( configUSE_TASK_NOTIFICATIONS == 1 )
7262 void vTaskGenericNotifyGiveFromISR( TaskHandle_t xTaskToNotify,
7263 UBaseType_t uxIndexToNotify,
7264 BaseType_t * pxHigherPriorityTaskWoken )
7267 uint8_t ucOriginalNotifyState;
7268 UBaseType_t uxSavedInterruptStatus;
7270 configASSERT( xTaskToNotify );
7271 configASSERT( uxIndexToNotify < configTASK_NOTIFICATION_ARRAY_ENTRIES );
7273 /* RTOS ports that support interrupt nesting have the concept of a
7274 * maximum system call (or maximum API call) interrupt priority.
7275 * Interrupts that are above the maximum system call priority are keep
7276 * permanently enabled, even when the RTOS kernel is in a critical section,
7277 * but cannot make any calls to FreeRTOS API functions. If configASSERT()
7278 * is defined in FreeRTOSConfig.h then
7279 * portASSERT_IF_INTERRUPT_PRIORITY_INVALID() will result in an assertion
7280 * failure if a FreeRTOS API function is called from an interrupt that has
7281 * been assigned a priority above the configured maximum system call
7282 * priority. Only FreeRTOS functions that end in FromISR can be called
7283 * from interrupts that have been assigned a priority at or (logically)
7284 * below the maximum system call interrupt priority. FreeRTOS maintains a
7285 * separate interrupt safe API to ensure interrupt entry is as fast and as
7286 * simple as possible. More information (albeit Cortex-M specific) is
7287 * provided on the following link:
7288 * https://www.FreeRTOS.org/RTOS-Cortex-M3-M4.html */
7289 portASSERT_IF_INTERRUPT_PRIORITY_INVALID();
7291 pxTCB = xTaskToNotify;
7293 uxSavedInterruptStatus = taskENTER_CRITICAL_FROM_ISR();
7295 ucOriginalNotifyState = pxTCB->ucNotifyState[ uxIndexToNotify ];
7296 pxTCB->ucNotifyState[ uxIndexToNotify ] = taskNOTIFICATION_RECEIVED;
7298 /* 'Giving' is equivalent to incrementing a count in a counting
7300 ( pxTCB->ulNotifiedValue[ uxIndexToNotify ] )++;
7302 traceTASK_NOTIFY_GIVE_FROM_ISR( uxIndexToNotify );
7304 /* If the task is in the blocked state specifically to wait for a
7305 * notification then unblock it now. */
7306 if( ucOriginalNotifyState == taskWAITING_NOTIFICATION )
7308 /* The task should not have been on an event list. */
7309 configASSERT( listLIST_ITEM_CONTAINER( &( pxTCB->xEventListItem ) ) == NULL );
7311 if( uxSchedulerSuspended == ( UBaseType_t ) 0U )
7313 listREMOVE_ITEM( &( pxTCB->xStateListItem ) );
7314 prvAddTaskToReadyList( pxTCB );
7318 /* The delayed and ready lists cannot be accessed, so hold
7319 * this task pending until the scheduler is resumed. */
7320 listINSERT_END( &( xPendingReadyList ), &( pxTCB->xEventListItem ) );
7323 #if ( configNUMBER_OF_CORES == 1 )
7325 if( pxTCB->uxPriority > pxCurrentTCB->uxPriority )
7327 /* The notified task has a priority above the currently
7328 * executing task so a yield is required. */
7329 if( pxHigherPriorityTaskWoken != NULL )
7331 *pxHigherPriorityTaskWoken = pdTRUE;
7334 /* Mark that a yield is pending in case the user is not
7335 * using the "xHigherPriorityTaskWoken" parameter in an ISR
7336 * safe FreeRTOS function. */
7337 xYieldPendings[ 0 ] = pdTRUE;
7341 mtCOVERAGE_TEST_MARKER();
7344 #else /* #if ( configNUMBER_OF_CORES == 1 ) */
7346 #if ( configUSE_PREEMPTION == 1 )
7348 prvYieldForTask( pxTCB );
7350 if( xYieldPendings[ portGET_CORE_ID() ] == pdTRUE )
7352 if( pxHigherPriorityTaskWoken != NULL )
7354 *pxHigherPriorityTaskWoken = pdTRUE;
7358 #endif /* #if ( configUSE_PREEMPTION == 1 ) */
7360 #endif /* #if ( configNUMBER_OF_CORES == 1 ) */
7363 taskEXIT_CRITICAL_FROM_ISR( uxSavedInterruptStatus );
7366 #endif /* configUSE_TASK_NOTIFICATIONS */
7367 /*-----------------------------------------------------------*/
7369 #if ( configUSE_TASK_NOTIFICATIONS == 1 )
7371 BaseType_t xTaskGenericNotifyStateClear( TaskHandle_t xTask,
7372 UBaseType_t uxIndexToClear )
7377 configASSERT( uxIndexToClear < configTASK_NOTIFICATION_ARRAY_ENTRIES );
7379 /* If null is passed in here then it is the calling task that is having
7380 * its notification state cleared. */
7381 pxTCB = prvGetTCBFromHandle( xTask );
7383 taskENTER_CRITICAL();
7385 if( pxTCB->ucNotifyState[ uxIndexToClear ] == taskNOTIFICATION_RECEIVED )
7387 pxTCB->ucNotifyState[ uxIndexToClear ] = taskNOT_WAITING_NOTIFICATION;
7395 taskEXIT_CRITICAL();
7400 #endif /* configUSE_TASK_NOTIFICATIONS */
7401 /*-----------------------------------------------------------*/
7403 #if ( configUSE_TASK_NOTIFICATIONS == 1 )
7405 uint32_t ulTaskGenericNotifyValueClear( TaskHandle_t xTask,
7406 UBaseType_t uxIndexToClear,
7407 uint32_t ulBitsToClear )
7412 configASSERT( uxIndexToClear < configTASK_NOTIFICATION_ARRAY_ENTRIES );
7414 /* If null is passed in here then it is the calling task that is having
7415 * its notification state cleared. */
7416 pxTCB = prvGetTCBFromHandle( xTask );
7418 taskENTER_CRITICAL();
7420 /* Return the notification as it was before the bits were cleared,
7421 * then clear the bit mask. */
7422 ulReturn = pxTCB->ulNotifiedValue[ uxIndexToClear ];
7423 pxTCB->ulNotifiedValue[ uxIndexToClear ] &= ~ulBitsToClear;
7425 taskEXIT_CRITICAL();
7430 #endif /* configUSE_TASK_NOTIFICATIONS */
7431 /*-----------------------------------------------------------*/
7433 #if ( configGENERATE_RUN_TIME_STATS == 1 )
7435 configRUN_TIME_COUNTER_TYPE ulTaskGetRunTimeCounter( const TaskHandle_t xTask )
7439 pxTCB = prvGetTCBFromHandle( xTask );
7441 return pxTCB->ulRunTimeCounter;
7444 #endif /* if ( configGENERATE_RUN_TIME_STATS == 1 ) */
7445 /*-----------------------------------------------------------*/
7447 #if ( configGENERATE_RUN_TIME_STATS == 1 )
7449 configRUN_TIME_COUNTER_TYPE ulTaskGetRunTimePercent( const TaskHandle_t xTask )
7452 configRUN_TIME_COUNTER_TYPE ulTotalTime, ulReturn;
7454 ulTotalTime = ( configRUN_TIME_COUNTER_TYPE ) portGET_RUN_TIME_COUNTER_VALUE();
7456 /* For percentage calculations. */
7457 ulTotalTime /= ( configRUN_TIME_COUNTER_TYPE ) 100;
7459 /* Avoid divide by zero errors. */
7460 if( ulTotalTime > ( configRUN_TIME_COUNTER_TYPE ) 0 )
7462 pxTCB = prvGetTCBFromHandle( xTask );
7463 ulReturn = pxTCB->ulRunTimeCounter / ulTotalTime;
7473 #endif /* if ( configGENERATE_RUN_TIME_STATS == 1 ) */
7474 /*-----------------------------------------------------------*/
7476 #if ( ( configGENERATE_RUN_TIME_STATS == 1 ) && ( INCLUDE_xTaskGetIdleTaskHandle == 1 ) )
7478 configRUN_TIME_COUNTER_TYPE ulTaskGetIdleRunTimeCounter( void )
7480 configRUN_TIME_COUNTER_TYPE ulReturn = 0;
7483 for( i = 0; i < ( BaseType_t ) configNUMBER_OF_CORES; i++ )
7485 ulReturn += xIdleTaskHandles[ i ]->ulRunTimeCounter;
7491 #endif /* if ( ( configGENERATE_RUN_TIME_STATS == 1 ) && ( INCLUDE_xTaskGetIdleTaskHandle == 1 ) ) */
7492 /*-----------------------------------------------------------*/
7494 #if ( ( configGENERATE_RUN_TIME_STATS == 1 ) && ( INCLUDE_xTaskGetIdleTaskHandle == 1 ) )
7496 configRUN_TIME_COUNTER_TYPE ulTaskGetIdleRunTimePercent( void )
7498 configRUN_TIME_COUNTER_TYPE ulTotalTime, ulReturn;
7499 configRUN_TIME_COUNTER_TYPE ulRunTimeCounter = 0;
7502 ulTotalTime = portGET_RUN_TIME_COUNTER_VALUE() * configNUMBER_OF_CORES;
7504 /* For percentage calculations. */
7505 ulTotalTime /= ( configRUN_TIME_COUNTER_TYPE ) 100;
7507 /* Avoid divide by zero errors. */
7508 if( ulTotalTime > ( configRUN_TIME_COUNTER_TYPE ) 0 )
7510 for( i = 0; i < ( BaseType_t ) configNUMBER_OF_CORES; i++ )
7512 ulRunTimeCounter += xIdleTaskHandles[ i ]->ulRunTimeCounter;
7515 ulReturn = ulRunTimeCounter / ulTotalTime;
7525 #endif /* if ( ( configGENERATE_RUN_TIME_STATS == 1 ) && ( INCLUDE_xTaskGetIdleTaskHandle == 1 ) ) */
7526 /*-----------------------------------------------------------*/
7528 static void prvAddCurrentTaskToDelayedList( TickType_t xTicksToWait,
7529 const BaseType_t xCanBlockIndefinitely )
7531 TickType_t xTimeToWake;
7532 const TickType_t xConstTickCount = xTickCount;
7534 #if ( INCLUDE_xTaskAbortDelay == 1 )
7536 /* About to enter a delayed list, so ensure the ucDelayAborted flag is
7537 * reset to pdFALSE so it can be detected as having been set to pdTRUE
7538 * when the task leaves the Blocked state. */
7539 pxCurrentTCB->ucDelayAborted = pdFALSE;
7543 /* Remove the task from the ready list before adding it to the blocked list
7544 * as the same list item is used for both lists. */
7545 if( uxListRemove( &( pxCurrentTCB->xStateListItem ) ) == ( UBaseType_t ) 0 )
7547 /* The current task must be in a ready list, so there is no need to
7548 * check, and the port reset macro can be called directly. */
7549 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. */
7553 mtCOVERAGE_TEST_MARKER();
7556 #if ( INCLUDE_vTaskSuspend == 1 )
7558 if( ( xTicksToWait == portMAX_DELAY ) && ( xCanBlockIndefinitely != pdFALSE ) )
7560 /* Add the task to the suspended task list instead of a delayed task
7561 * list to ensure it is not woken by a timing event. It will block
7563 listINSERT_END( &xSuspendedTaskList, &( pxCurrentTCB->xStateListItem ) );
7567 /* Calculate the time at which the task should be woken if the event
7568 * does not occur. This may overflow but this doesn't matter, the
7569 * kernel will manage it correctly. */
7570 xTimeToWake = xConstTickCount + xTicksToWait;
7572 /* The list item will be inserted in wake time order. */
7573 listSET_LIST_ITEM_VALUE( &( pxCurrentTCB->xStateListItem ), xTimeToWake );
7575 if( xTimeToWake < xConstTickCount )
7577 /* Wake time has overflowed. Place this item in the overflow
7579 vListInsert( pxOverflowDelayedTaskList, &( pxCurrentTCB->xStateListItem ) );
7583 /* The wake time has not overflowed, so the current block list
7585 vListInsert( pxDelayedTaskList, &( pxCurrentTCB->xStateListItem ) );
7587 /* If the task entering the blocked state was placed at the
7588 * head of the list of blocked tasks then xNextTaskUnblockTime
7589 * needs to be updated too. */
7590 if( xTimeToWake < xNextTaskUnblockTime )
7592 xNextTaskUnblockTime = xTimeToWake;
7596 mtCOVERAGE_TEST_MARKER();
7601 #else /* INCLUDE_vTaskSuspend */
7603 /* Calculate the time at which the task should be woken if the event
7604 * does not occur. This may overflow but this doesn't matter, the kernel
7605 * will manage it correctly. */
7606 xTimeToWake = xConstTickCount + xTicksToWait;
7608 /* The list item will be inserted in wake time order. */
7609 listSET_LIST_ITEM_VALUE( &( pxCurrentTCB->xStateListItem ), xTimeToWake );
7611 if( xTimeToWake < xConstTickCount )
7613 /* Wake time has overflowed. Place this item in the overflow list. */
7614 vListInsert( pxOverflowDelayedTaskList, &( pxCurrentTCB->xStateListItem ) );
7618 /* The wake time has not overflowed, so the current block list is used. */
7619 vListInsert( pxDelayedTaskList, &( pxCurrentTCB->xStateListItem ) );
7621 /* If the task entering the blocked state was placed at the head of the
7622 * list of blocked tasks then xNextTaskUnblockTime needs to be updated
7624 if( xTimeToWake < xNextTaskUnblockTime )
7626 xNextTaskUnblockTime = xTimeToWake;
7630 mtCOVERAGE_TEST_MARKER();
7634 /* Avoid compiler warning when INCLUDE_vTaskSuspend is not 1. */
7635 ( void ) xCanBlockIndefinitely;
7637 #endif /* INCLUDE_vTaskSuspend */
7639 /*-----------------------------------------------------------*/
7641 #if ( portUSING_MPU_WRAPPERS == 1 )
7643 xMPU_SETTINGS * xTaskGetMPUSettings( TaskHandle_t xTask )
7647 pxTCB = prvGetTCBFromHandle( xTask );
7649 return &( pxTCB->xMPUSettings );
7652 #endif /* portUSING_MPU_WRAPPERS */
7653 /*-----------------------------------------------------------*/
7655 /* Code below here allows additional code to be inserted into this source file,
7656 * especially where access to file scope functions and data is needed (for example
7657 * when performing module tests). */
7659 #ifdef FREERTOS_MODULE_TEST
7660 #include "tasks_test_access_functions.h"
7664 #if ( configINCLUDE_FREERTOS_TASK_C_ADDITIONS_H == 1 )
7666 #include "freertos_tasks_c_additions.h"
7668 #ifdef FREERTOS_TASKS_C_ADDITIONS_INIT
7669 static void freertos_tasks_c_additions_init( void )
7671 FREERTOS_TASKS_C_ADDITIONS_INIT();
7675 #endif /* if ( configINCLUDE_FREERTOS_TASK_C_ADDITIONS_H == 1 ) */