From d4b8bfdb36676a302a6445a7822afda65c13c051 Mon Sep 17 00:00:00 2001 From: "Ching-Hsin,Lee" Date: Mon, 20 May 2024 12:36:28 +0800 Subject: [PATCH 01/25] First version of pxCurrentTCB --- tasks.c | 303 +++++++++++++++++++++++++++++++------------------------- 1 file changed, 167 insertions(+), 136 deletions(-) diff --git a/tasks.c b/tasks.c index 3423becd4f..633b3cf56c 100644 --- a/tasks.c +++ b/tasks.c @@ -84,16 +84,17 @@ portYIELD_WITHIN_API(); \ } while( 0 ) - #define taskYIELD_ANY_CORE_IF_USING_PREEMPTION( pxTCB ) \ - do { \ - if( pxCurrentTCB->uxPriority < ( pxTCB )->uxPriority ) \ - { \ - portYIELD_WITHIN_API(); \ - } \ - else \ - { \ - mtCOVERAGE_TEST_MARKER(); \ - } \ + #define taskYIELD_ANY_CORE_IF_USING_PREEMPTION( pxTCB ) \ + do { \ + TCB_t * const pxConstCurrentTCB = prvGetCurrentTask(); \ + if( pxConstCurrentTCB->uxPriority < ( pxTCB )->uxPriority ) \ + { \ + portYIELD_WITHIN_API(); \ + } \ + else \ + { \ + mtCOVERAGE_TEST_MARKER(); \ + } \ } while( 0 ) #else /* if ( configNUMBER_OF_CORES == 1 ) */ @@ -216,14 +217,15 @@ /*-----------------------------------------------------------*/ - #define taskSELECT_HIGHEST_PRIORITY_TASK() \ - do { \ - UBaseType_t uxTopPriority; \ - \ - /* Find the highest priority list that contains ready tasks. */ \ - portGET_HIGHEST_PRIORITY( uxTopPriority, uxTopReadyPriority ); \ - configASSERT( listCURRENT_LIST_LENGTH( &( pxReadyTasksLists[ uxTopPriority ] ) ) > 0 ); \ - listGET_OWNER_OF_NEXT_ENTRY( pxCurrentTCB, &( pxReadyTasksLists[ uxTopPriority ] ) ); \ + #define taskSELECT_HIGHEST_PRIORITY_TASK() \ + do { \ + UBaseType_t uxTopPriority; \ + TCB_t * const pxConstCurrentTCB = prvGetCurrentTask(); \ + \ + /* Find the highest priority list that contains ready tasks. */ \ + portGET_HIGHEST_PRIORITY( uxTopPriority, uxTopReadyPriority ); \ + configASSERT( listCURRENT_LIST_LENGTH( &( pxReadyTasksLists[ uxTopPriority ] ) ) > 0 ); \ + listGET_OWNER_OF_NEXT_ENTRY( pxConstCurrentTCB, &( pxReadyTasksLists[ uxTopPriority ] ) ); \ } while( 0 ) /*-----------------------------------------------------------*/ @@ -280,7 +282,7 @@ * task should be used in place of the parameter. This macro simply checks to * see if the parameter is NULL and returns a pointer to the appropriate TCB. */ -#define prvGetTCBFromHandle( pxHandle ) ( ( ( pxHandle ) == NULL ) ? pxCurrentTCB : ( pxHandle ) ) +#define prvGetTCBFromHandle( pxHandle ) ( ( ( pxHandle ) == NULL ) ? prvGetCurrentTask() : ( pxHandle ) ) /* The item value of the event list item is normally used to hold the priority * of the task to which it belongs (coded to allow it to be held in reverse @@ -439,6 +441,13 @@ typedef struct tskTaskControlBlock /* The old naming convention is used to * below to enable the use of older kernel aware debuggers. */ typedef tskTCB TCB_t; +#if configNUMBER_OF_CORES == 1 + #define prvGetCurrentTaskImmutable() pxCurrentTCB + #define prvGetCurrentTask() pxCurrentTCB +#else + #define prvGetCurrentTaskImmutable() pxCurrentTCBs[ portGET_CORE_ID() ] +#endif + #if ( configNUMBER_OF_CORES == 1 ) /* MISRA Ref 8.4.1 [Declaration shall be visible] */ /* More details at: https://github.com/FreeRTOS/FreeRTOS-Kernel/blob/main/MISRA.md#rule-84 */ @@ -449,7 +458,6 @@ typedef tskTCB TCB_t; /* More details at: https://github.com/FreeRTOS/FreeRTOS-Kernel/blob/main/MISRA.md#rule-84 */ /* coverity[misra_c_2012_rule_8_4_violation] */ portDONT_DISCARD PRIVILEGED_DATA TCB_t * volatile pxCurrentTCBs[ configNUMBER_OF_CORES ]; - #define pxCurrentTCB xTaskGetCurrentTaskHandle() #endif /* Lists for ready and blocked tasks. -------------------- @@ -801,6 +809,23 @@ static void prvAddNewTaskToReadyList( TCB_t * pxNewTCB ) PRIVILEGED_FUNCTION; #endif /* #if ( ( configUSE_TRACE_FACILITY == 1 ) && ( configUSE_STATS_FORMATTING_FUNCTIONS > 0 ) ) */ /*-----------------------------------------------------------*/ +#if ( configNUMBER_OF_CORES > 1 ) + + static TCB_t * prvGetCurrentTask( void ) + { + TCB_t * pxTCB; + UBaseType_t uxSavedInterruptStatus; + + uxSavedInterruptStatus = portSET_INTERRUPT_MASK(); + { + pxTCB = pxCurrentTCBs[ portGET_CORE_ID() ]; + } + portCLEAR_INTERRUPT_MASK( uxSavedInterruptStatus ); + + return pxTCB; + } +#endif /* if ( configNUMBER_OF_CORES > 1 ) */ + #if ( configNUMBER_OF_CORES > 1 ) static void prvCheckForRunStateChange( void ) { @@ -2278,7 +2303,7 @@ static void prvInitialiseNewTask( TaskFunction_t pxTaskCode, * ready list. */ #if ( configNUMBER_OF_CORES > 1 ) { - if( taskTASK_IS_RUNNING( pxTCB ) == pdTRUE ) + if( taskTASK_IS_RUNNING( pxTCB ) != pdFALSE ) { if( pxTCB->xTaskRunState == ( BaseType_t ) portGET_CORE_ID() ) { @@ -2319,7 +2344,7 @@ static void prvInitialiseNewTask( TaskFunction_t pxTaskCode, { if( xSchedulerRunning != pdFALSE ) { - if( pxTCB == pxCurrentTCB ) + if( taskTASK_IS_RUNNING( pxTCB ) != pdFALSE ) { configASSERT( uxSchedulerSuspended == 0 ); taskYIELD_WITHIN_API(); @@ -2496,7 +2521,7 @@ static void prvInitialiseNewTask( TaskFunction_t pxTaskCode, configASSERT( pxTCB ); #if ( configNUMBER_OF_CORES == 1 ) - if( pxTCB == pxCurrentTCB ) + if( taskTASK_IS_RUNNING( pxTCB ) == pdTRUE ) { /* The task calling this function is querying its own state. */ eReturn = eRunning; @@ -2768,6 +2793,7 @@ static void prvInitialiseNewTask( TaskFunction_t pxTaskCode, TCB_t * pxTCB; UBaseType_t uxCurrentBasePriority, uxPriorityUsedOnEntry; BaseType_t xYieldRequired = pdFALSE; + TCB_t * const pxConstCurrentTCB = prvGetCurrentTask(); #if ( configNUMBER_OF_CORES > 1 ) BaseType_t xYieldForTask = pdFALSE; @@ -2813,12 +2839,12 @@ static void prvInitialiseNewTask( TaskFunction_t pxTaskCode, { #if ( configNUMBER_OF_CORES == 1 ) { - if( pxTCB != pxCurrentTCB ) + if( taskTASK_IS_RUNNING( pxTCB ) != pdTRUE ) { /* The priority of a task other than the currently * running task is being raised. Is the priority being * raised above that of the running task? */ - if( uxNewPriority > pxCurrentTCB->uxPriority ) + if( uxNewPriority > pxConstCurrentTCB->uxPriority ) { xYieldRequired = pdTRUE; } @@ -3232,7 +3258,7 @@ static void prvInitialiseNewTask( TaskFunction_t pxTaskCode, mtCOVERAGE_TEST_MARKER(); } - if( pxTCB == pxCurrentTCB ) + if( taskTASK_IS_RUNNING( pxTCB ) != pdFALSE ) { if( xSchedulerRunning != pdFALSE ) { @@ -3363,7 +3389,7 @@ static void prvInitialiseNewTask( TaskFunction_t pxTaskCode, /* The parameter cannot be NULL as it is impossible to resume the * currently executing task. */ - if( ( pxTCB != pxCurrentTCB ) && ( pxTCB != NULL ) ) + if( ( taskTASK_IS_RUNNING( pxTCB ) != pdTRUE ) && ( pxTCB != NULL ) ) #else /* The parameter cannot be NULL as it is impossible to resume the @@ -3416,6 +3442,7 @@ static void prvInitialiseNewTask( TaskFunction_t pxTaskCode, BaseType_t xYieldRequired = pdFALSE; TCB_t * const pxTCB = xTaskToResume; UBaseType_t uxSavedInterruptStatus; + TCB_t * const pxConstCurrentTCB = prvGetCurrentTaskImmutable(); traceENTER_xTaskResumeFromISR( xTaskToResume ); @@ -3455,7 +3482,7 @@ static void prvInitialiseNewTask( TaskFunction_t pxTaskCode, { /* Ready lists can be accessed so move the task from the * suspended list to the ready list directly. */ - if( pxTCB->uxPriority > pxCurrentTCB->uxPriority ) + if( pxTCB->uxPriority > pxConstCurrentTCB->uxPriority ) { xYieldRequired = pdTRUE; @@ -3711,9 +3738,11 @@ void vTaskStartScheduler( void ) #if ( configUSE_C_RUNTIME_TLS_SUPPORT == 1 ) { + TCB_t * const pxConstCurrentTCB = prvGetCurrentTask(); + /* Switch C-Runtime's TLS Block to point to the TLS * block specific to the task that will run first. */ - configSET_TLS_BLOCK( pxCurrentTCB->xTLSBlock ); + configSET_TLS_BLOCK( pxConstCurrentTCB->xTLSBlock ); } #endif @@ -3895,6 +3924,7 @@ void vTaskSuspendAll( void ) { TickType_t xReturn; UBaseType_t uxHigherPriorityReadyTasks = pdFALSE; + TCB_t * const pxConstCurrentTCB = prvGetCurrentTask(); /* uxHigherPriorityReadyTasks takes care of the case where * configUSE_PREEMPTION is 0, so there may be tasks above the idle priority @@ -3923,7 +3953,7 @@ void vTaskSuspendAll( void ) } #endif /* if ( configUSE_PORT_OPTIMISED_TASK_SELECTION == 0 ) */ - if( pxCurrentTCB->uxPriority > tskIDLE_PRIORITY ) + if( pxConstCurrentTCB->uxPriority > tskIDLE_PRIORITY ) { xReturn = 0; } @@ -3972,6 +4002,8 @@ BaseType_t xTaskResumeAll( void ) taskENTER_CRITICAL(); { BaseType_t xCoreID; + TCB_t * const pxConstCurrentTCB = prvGetCurrentTaskImmutable(); + xCoreID = ( BaseType_t ) portGET_CORE_ID(); /* If uxSchedulerSuspended is zero then this function does not match a @@ -4002,7 +4034,7 @@ BaseType_t xTaskResumeAll( void ) { /* If the moved task has a priority higher than the current * task then a yield must be performed. */ - if( pxTCB->uxPriority > pxCurrentTCB->uxPriority ) + if( pxTCB->uxPriority > pxConstCurrentTCB->uxPriority ) { xYieldPendings[ xCoreID ] = pdTRUE; } @@ -4626,10 +4658,12 @@ BaseType_t xTaskCatchUpTicks( TickType_t xTicksToCatchUp ) { #if ( configNUMBER_OF_CORES == 1 ) { + TCB_t * const pxCurrentTCB = prvGetCurrentTask(); + /* Preemption is on, but a context switch should only be * performed if the unblocked task has a priority that is * higher than the currently executing task. */ - if( pxTCB->uxPriority > pxCurrentTCB->uxPriority ) + if( pxTCB->uxPriority > pxConstCurrentTCB->uxPriority ) { /* Pend the yield to be performed when the scheduler * is unsuspended. */ @@ -4672,6 +4706,7 @@ BaseType_t xTaskIncrementTick( void ) TCB_t * pxTCB; TickType_t xItemValue; BaseType_t xSwitchRequired = pdFALSE; + TCB_t * const pxConstCurrentTCB = prvGetCurrentTaskImmutable(); #if ( configUSE_PREEMPTION == 1 ) && ( configNUMBER_OF_CORES > 1 ) BaseType_t xYieldRequiredForCore[ configNUMBER_OF_CORES ] = { pdFALSE }; @@ -4784,7 +4819,7 @@ BaseType_t xTaskIncrementTick( void ) * processing time (which happens when both * preemption and time slicing are on) is * handled below.*/ - if( pxTCB->uxPriority > pxCurrentTCB->uxPriority ) + if( pxTCB->uxPriority > pxConstCurrentTCB->uxPriority ) { xSwitchRequired = pdTRUE; } @@ -4811,7 +4846,7 @@ BaseType_t xTaskIncrementTick( void ) { #if ( configNUMBER_OF_CORES == 1 ) { - if( listCURRENT_LIST_LENGTH( &( pxReadyTasksLists[ pxCurrentTCB->uxPriority ] ) ) > 1U ) + if( listCURRENT_LIST_LENGTH( &( pxReadyTasksLists[ pxConstCurrentTCB->uxPriority ] ) ) > 1U ) { xSwitchRequired = pdTRUE; } @@ -4926,26 +4961,19 @@ BaseType_t xTaskIncrementTick( void ) void vTaskSetApplicationTaskTag( TaskHandle_t xTask, TaskHookFunction_t pxHookFunction ) { - TCB_t * xTCB; + TCB_t * pxTCB; traceENTER_vTaskSetApplicationTaskTag( xTask, pxHookFunction ); /* If xTask is NULL then it is the task hook of the calling task that is * getting set. */ - if( xTask == NULL ) - { - xTCB = ( TCB_t * ) pxCurrentTCB; - } - else - { - xTCB = xTask; - } + pxTCB = prvGetTCBFromHandle( xTask ); /* Save the hook function in the TCB. A critical section is required as * the value can be accessed from an interrupt. */ taskENTER_CRITICAL(); { - xTCB->pxTaskTag = pxHookFunction; + pxTCB->pxTaskTag = pxHookFunction; } taskEXIT_CRITICAL(); @@ -5020,24 +5048,17 @@ BaseType_t xTaskIncrementTick( void ) BaseType_t xTaskCallApplicationTaskHook( TaskHandle_t xTask, void * pvParameter ) { - TCB_t * xTCB; + TCB_t * pxTCB; BaseType_t xReturn; traceENTER_xTaskCallApplicationTaskHook( xTask, pvParameter ); /* If xTask is NULL then we are calling our own task hook. */ - if( xTask == NULL ) - { - xTCB = pxCurrentTCB; - } - else - { - xTCB = xTask; - } + pxTCB = prvGetTCBFromHandle( xTask ); - if( xTCB->pxTaskTag != NULL ) + if( pxTCB->pxTaskTag != NULL ) { - xReturn = xTCB->pxTaskTag( pvParameter ); + xReturn = pxTCB->pxTaskTag( pvParameter ); } else { @@ -5214,7 +5235,7 @@ BaseType_t xTaskIncrementTick( void ) /* Macro to inject port specific behaviour immediately after * switching tasks, such as setting an end of stack watchpoint * or reconfiguring the MPU. */ - portTASK_SWITCH_HOOK( pxCurrentTCBs[ portGET_CORE_ID() ] ); + portTASK_SWITCH_HOOK( pxCurrentTCBs[ xCoreID ] ); /* After the new task is switched in, update the global errno. */ #if ( configUSE_POSIX_ERRNO == 1 ) @@ -5243,6 +5264,8 @@ BaseType_t xTaskIncrementTick( void ) void vTaskPlaceOnEventList( List_t * const pxEventList, const TickType_t xTicksToWait ) { + TCB_t * const pxConstCurrentTCB = prvGetCurrentTaskImmutable(); + traceENTER_vTaskPlaceOnEventList( pxEventList, xTicksToWait ); configASSERT( pxEventList ); @@ -5261,7 +5284,7 @@ void vTaskPlaceOnEventList( List_t * const pxEventList, * * The queue that contains the event list is locked, preventing * simultaneous access from interrupts. */ - vListInsert( pxEventList, &( pxCurrentTCB->xEventListItem ) ); + vListInsert( pxEventList, &( pxConstCurrentTCB->xEventListItem ) ); prvAddCurrentTaskToDelayedList( xTicksToWait, pdTRUE ); @@ -5273,6 +5296,8 @@ void vTaskPlaceOnUnorderedEventList( List_t * pxEventList, const TickType_t xItemValue, const TickType_t xTicksToWait ) { + TCB_t * const pxConstCurrentTCB = prvGetCurrentTaskImmutable(); + traceENTER_vTaskPlaceOnUnorderedEventList( pxEventList, xItemValue, xTicksToWait ); configASSERT( pxEventList ); @@ -5284,14 +5309,14 @@ void vTaskPlaceOnUnorderedEventList( List_t * pxEventList, /* Store the item value in the event list item. It is safe to access the * event list item here as interrupts won't access the event list item of a * task that is not in the Blocked state. */ - listSET_LIST_ITEM_VALUE( &( pxCurrentTCB->xEventListItem ), xItemValue | taskEVENT_LIST_ITEM_VALUE_IN_USE ); + listSET_LIST_ITEM_VALUE( &( pxConstCurrentTCB->xEventListItem ), xItemValue | taskEVENT_LIST_ITEM_VALUE_IN_USE ); /* Place the event list item of the TCB at the end of the appropriate event * list. It is safe to access the event list here because it is part of an * event group implementation - and interrupts don't access event groups * directly (instead they access them indirectly by pending function calls to * the task level). */ - listINSERT_END( pxEventList, &( pxCurrentTCB->xEventListItem ) ); + listINSERT_END( pxEventList, &( pxConstCurrentTCB->xEventListItem ) ); prvAddCurrentTaskToDelayedList( xTicksToWait, pdTRUE ); @@ -5305,6 +5330,8 @@ void vTaskPlaceOnUnorderedEventList( List_t * pxEventList, TickType_t xTicksToWait, const BaseType_t xWaitIndefinitely ) { + TCB_t * const pxConstCurrentTCB = prvGetCurrentTaskImmutable(); + traceENTER_vTaskPlaceOnEventListRestricted( pxEventList, xTicksToWait, xWaitIndefinitely ); configASSERT( pxEventList ); @@ -5319,7 +5346,7 @@ void vTaskPlaceOnUnorderedEventList( List_t * pxEventList, * In this case it is assume that this is the only task that is going to * be waiting on this event list, so the faster vListInsertEnd() function * can be used in place of vListInsert. */ - listINSERT_END( pxEventList, &( pxCurrentTCB->xEventListItem ) ); + listINSERT_END( pxEventList, &( pxConstCurrentTCB->xEventListItem ) ); /* If the task should block indefinitely then set the block time to a * value that will be recognised as an indefinite delay inside the @@ -5343,6 +5370,8 @@ BaseType_t xTaskRemoveFromEventList( const List_t * const pxEventList ) TCB_t * pxUnblockedTCB; BaseType_t xReturn; + TCB_t * const pxConstCurrentTCB = prvGetCurrentTaskImmutable(); + traceENTER_xTaskRemoveFromEventList( pxEventList ); /* THIS FUNCTION MUST BE CALLED FROM A CRITICAL SECTION. It can also be @@ -5393,7 +5422,7 @@ BaseType_t xTaskRemoveFromEventList( const List_t * const pxEventList ) #if ( configNUMBER_OF_CORES == 1 ) { - if( pxUnblockedTCB->uxPriority > pxCurrentTCB->uxPriority ) + if( pxUnblockedTCB->uxPriority > pxConstCurrentTCB->uxPriority ) { /* Return true if the task removed from the event list has a higher * priority than the calling task. This allows the calling task to know if @@ -5435,6 +5464,7 @@ void vTaskRemoveFromUnorderedEventList( ListItem_t * pxEventListItem, const TickType_t xItemValue ) { TCB_t * pxUnblockedTCB; + TCB_t * const pxConstCurrentTCB = prvGetCurrentTaskImmutable(); traceENTER_vTaskRemoveFromUnorderedEventList( pxEventListItem, xItemValue ); @@ -5476,7 +5506,7 @@ void vTaskRemoveFromUnorderedEventList( ListItem_t * pxEventListItem, #if ( configNUMBER_OF_CORES == 1 ) { - if( pxUnblockedTCB->uxPriority > pxCurrentTCB->uxPriority ) + if( pxUnblockedTCB->uxPriority > pxConstCurrentTCB->uxPriority ) { /* The unblocked task has a priority above that of the calling task, so * a context switch is required. This function is called with the @@ -5536,6 +5566,10 @@ BaseType_t xTaskCheckForTimeOut( TimeOut_t * const pxTimeOut, { BaseType_t xReturn; + #if ( INCLUDE_xTaskAbortDelay == 1 ) + TCB_t * const pxConstCurrentTCB = prvGetCurrentTaskImmutable(); + #endif + traceENTER_xTaskCheckForTimeOut( pxTimeOut, pxTicksToWait ); configASSERT( pxTimeOut ); @@ -5548,11 +5582,11 @@ BaseType_t xTaskCheckForTimeOut( TimeOut_t * const pxTimeOut, const TickType_t xElapsedTime = xConstTickCount - pxTimeOut->xTimeOnEntering; #if ( INCLUDE_xTaskAbortDelay == 1 ) - if( pxCurrentTCB->ucDelayAborted != ( uint8_t ) pdFALSE ) + if( pxConstCurrentTCB->ucDelayAborted != ( uint8_t ) pdFALSE ) { /* The delay was aborted, which is not the same as a time out, * but has the same result. */ - pxCurrentTCB->ucDelayAborted = ( uint8_t ) pdFALSE; + pxConstCurrentTCB->ucDelayAborted = ( uint8_t ) pdFALSE; xReturn = pdTRUE; } else @@ -6476,41 +6510,24 @@ static void prvResetNextTaskUnblockTime( void ) #if ( ( INCLUDE_xTaskGetCurrentTaskHandle == 1 ) || ( configUSE_MUTEXES == 1 ) ) || ( configNUMBER_OF_CORES > 1 ) - #if ( configNUMBER_OF_CORES == 1 ) - TaskHandle_t xTaskGetCurrentTaskHandle( void ) - { - TaskHandle_t xReturn; + TaskHandle_t xTaskGetCurrentTaskHandle( void ) + { + TaskHandle_t xReturn; - traceENTER_xTaskGetCurrentTaskHandle(); + traceENTER_xTaskGetCurrentTaskHandle(); - /* A critical section is not required as this is not called from - * an interrupt and the current TCB will always be the same for any - * individual execution thread. */ - xReturn = pxCurrentTCB; + /* A critical section is not required as this is not called from + * an interrupt and the current TCB will always be the same for any + * individual execution thread. */ - traceRETURN_xTaskGetCurrentTaskHandle( xReturn ); + /* In SMP environment, interrupt must be disabled to get the current + * task TCB. */ + xReturn = prvGetCurrentTask(); - return xReturn; - } - #else /* #if ( configNUMBER_OF_CORES == 1 ) */ - TaskHandle_t xTaskGetCurrentTaskHandle( void ) - { - TaskHandle_t xReturn; - UBaseType_t uxSavedInterruptStatus; - - traceENTER_xTaskGetCurrentTaskHandle(); - - uxSavedInterruptStatus = portSET_INTERRUPT_MASK(); - { - xReturn = pxCurrentTCBs[ portGET_CORE_ID() ]; - } - portCLEAR_INTERRUPT_MASK( uxSavedInterruptStatus ); - - traceRETURN_xTaskGetCurrentTaskHandle( xReturn ); + traceRETURN_xTaskGetCurrentTaskHandle( xReturn ); - return xReturn; - } - #endif /* #if ( configNUMBER_OF_CORES == 1 ) */ + return xReturn; + } TaskHandle_t xTaskGetCurrentTaskHandleForCore( BaseType_t xCoreID ) { @@ -6582,6 +6599,8 @@ static void prvResetNextTaskUnblockTime( void ) TCB_t * const pxMutexHolderTCB = pxMutexHolder; BaseType_t xReturn = pdFALSE; + TCB_t * const pxConstCurrentTCB = prvGetCurrentTaskImmutable(); + traceENTER_xTaskPriorityInherit( pxMutexHolder ); /* If the mutex is taken by an interrupt, the mutex holder is NULL. Priority @@ -6591,14 +6610,14 @@ static void prvResetNextTaskUnblockTime( void ) /* If the holder of the mutex has a priority below the priority of * the task attempting to obtain the mutex then it will temporarily * inherit the priority of the task attempting to obtain the mutex. */ - if( pxMutexHolderTCB->uxPriority < pxCurrentTCB->uxPriority ) + if( pxMutexHolderTCB->uxPriority < pxConstCurrentTCB->uxPriority ) { /* Adjust the mutex holder state to account for its new * priority. Only reset the event list item value if the value is * not being used for anything else. */ if( ( listGET_LIST_ITEM_VALUE( &( pxMutexHolderTCB->xEventListItem ) ) & taskEVENT_LIST_ITEM_VALUE_IN_USE ) == ( ( TickType_t ) 0U ) ) { - listSET_LIST_ITEM_VALUE( &( pxMutexHolderTCB->xEventListItem ), ( TickType_t ) configMAX_PRIORITIES - ( TickType_t ) pxCurrentTCB->uxPriority ); + listSET_LIST_ITEM_VALUE( &( pxMutexHolderTCB->xEventListItem ), ( TickType_t ) configMAX_PRIORITIES - ( TickType_t ) pxConstCurrentTCB->uxPriority ); } else { @@ -6622,7 +6641,7 @@ static void prvResetNextTaskUnblockTime( void ) } /* Inherit the priority before being moved into the new list. */ - pxMutexHolderTCB->uxPriority = pxCurrentTCB->uxPriority; + pxMutexHolderTCB->uxPriority = pxConstCurrentTCB->uxPriority; prvAddTaskToReadyList( pxMutexHolderTCB ); #if ( configNUMBER_OF_CORES > 1 ) { @@ -6638,17 +6657,17 @@ static void prvResetNextTaskUnblockTime( void ) else { /* Just inherit the priority. */ - pxMutexHolderTCB->uxPriority = pxCurrentTCB->uxPriority; + pxMutexHolderTCB->uxPriority = pxConstCurrentTCB->uxPriority; } - traceTASK_PRIORITY_INHERIT( pxMutexHolderTCB, pxCurrentTCB->uxPriority ); + traceTASK_PRIORITY_INHERIT( pxMutexHolderTCB, pxConstCurrentTCB->uxPriority ); /* Inheritance occurred. */ xReturn = pdTRUE; } else { - if( pxMutexHolderTCB->uxBasePriority < pxCurrentTCB->uxPriority ) + if( pxMutexHolderTCB->uxBasePriority < pxConstCurrentTCB->uxPriority ) { /* The base priority of the mutex holder is lower than the * priority of the task attempting to take the mutex, but the @@ -6693,7 +6712,7 @@ static void prvResetNextTaskUnblockTime( void ) * If the mutex is held by a task then it cannot be given from an * interrupt, and if a mutex is given by the holding task then it must * be the running state task. */ - configASSERT( pxTCB == pxCurrentTCB ); + configASSERT( taskTASK_IS_RUNNING( pxTCB ) == pdTRUE ); configASSERT( pxTCB->uxMutexesHeld ); ( pxTCB->uxMutexesHeld )--; @@ -6814,7 +6833,7 @@ static void prvResetNextTaskUnblockTime( void ) /* If a task has timed out because it already holds the * mutex it was trying to obtain then it cannot of inherited * its own priority. */ - configASSERT( pxTCB != pxCurrentTCB ); + configASSERT( taskTASK_IS_RUNNING( pxTCB ) == pdFALSE ); /* Disinherit the priority, remembering the previous * priority to facilitate determining the subject task's @@ -6927,7 +6946,9 @@ static void prvResetNextTaskUnblockTime( void ) if( xSchedulerRunning != pdFALSE ) { - ( pxCurrentTCB->uxCriticalNesting )++; + TCB_t * const pxConstCurrentTCB = prvGetCurrentTaskImmutable(); + + ( pxConstCurrentTCB->uxCriticalNesting )++; /* This is not the interrupt safe version of the enter critical * function so assert() if it is being called from an interrupt @@ -6935,7 +6956,7 @@ static void prvResetNextTaskUnblockTime( void ) * interrupt. Only assert if the critical nesting count is 1 to * protect against recursive calls if the assert function also uses a * critical section. */ - if( pxCurrentTCB->uxCriticalNesting == 1U ) + if( pxConstCurrentTCB->uxCriticalNesting == 1U ) { portASSERT_IF_IN_ISR(); } @@ -7037,23 +7058,25 @@ static void prvResetNextTaskUnblockTime( void ) void vTaskExitCritical( void ) { + TCB_t * const pxConstCurrentTCB = prvGetCurrentTaskImmutable(); + traceENTER_vTaskExitCritical(); if( xSchedulerRunning != pdFALSE ) { /* If pxCurrentTCB->uxCriticalNesting is zero then this function * does not match a previous call to vTaskEnterCritical(). */ - configASSERT( pxCurrentTCB->uxCriticalNesting > 0U ); + configASSERT( pxConstCurrentTCB->uxCriticalNesting > 0U ); /* This function should not be called in ISR. Use vTaskExitCriticalFromISR * to exit critical section from ISR. */ portASSERT_IF_IN_ISR(); - if( pxCurrentTCB->uxCriticalNesting > 0U ) + if( pxConstCurrentTCB->uxCriticalNesting > 0U ) { - ( pxCurrentTCB->uxCriticalNesting )--; + ( pxConstCurrentTCB->uxCriticalNesting )--; - if( pxCurrentTCB->uxCriticalNesting == 0U ) + if( pxConstCurrentTCB->uxCriticalNesting == 0U ) { portENABLE_INTERRUPTS(); } @@ -7570,13 +7593,15 @@ TickType_t uxTaskResetEventItemValue( void ) { TickType_t uxReturn; + TCB_t * const pxConstCurrentTCB = prvGetCurrentTaskImmutable(); + traceENTER_uxTaskResetEventItemValue(); - uxReturn = listGET_LIST_ITEM_VALUE( &( pxCurrentTCB->xEventListItem ) ); + uxReturn = listGET_LIST_ITEM_VALUE( &( pxConstCurrentTCB->xEventListItem ) ); /* Reset the event list item to its normal value - so it can be used with * queues and semaphores. */ - listSET_LIST_ITEM_VALUE( &( pxCurrentTCB->xEventListItem ), ( ( TickType_t ) configMAX_PRIORITIES - ( TickType_t ) pxCurrentTCB->uxPriority ) ); + listSET_LIST_ITEM_VALUE( &( pxConstCurrentTCB->xEventListItem ), ( ( TickType_t ) configMAX_PRIORITIES - ( TickType_t ) pxConstCurrentTCB->uxPriority ) ); traceRETURN_uxTaskResetEventItemValue( uxReturn ); @@ -7592,7 +7617,8 @@ TickType_t uxTaskResetEventItemValue( void ) traceENTER_pvTaskIncrementMutexHeldCount(); - pxTCB = pxCurrentTCB; + /* This API should be called in critical section only. */ + pxTCB = prvGetCurrentTaskImmutable(); /* If xSemaphoreCreateMutex() is called before any tasks have been created * then pxCurrentTCB will be NULL. */ @@ -7617,6 +7643,7 @@ TickType_t uxTaskResetEventItemValue( void ) { uint32_t ulReturn; BaseType_t xAlreadyYielded, xShouldBlock = pdFALSE; + TCB_t * const pxConstCurrentTCB = prvGetCurrentTask(); traceENTER_ulTaskGenericNotifyTake( uxIndexToWaitOn, xClearCountOnExit, xTicksToWait ); @@ -7633,10 +7660,10 @@ TickType_t uxTaskResetEventItemValue( void ) taskENTER_CRITICAL(); { /* Only block if the notification count is not already non-zero. */ - if( pxCurrentTCB->ulNotifiedValue[ uxIndexToWaitOn ] == 0U ) + if( pxConstCurrentTCB->ulNotifiedValue[ uxIndexToWaitOn ] == 0U ) { /* Mark this task as waiting for a notification. */ - pxCurrentTCB->ucNotifyState[ uxIndexToWaitOn ] = taskWAITING_NOTIFICATION; + pxConstCurrentTCB->ucNotifyState[ uxIndexToWaitOn ] = taskWAITING_NOTIFICATION; if( xTicksToWait > ( TickType_t ) 0 ) { @@ -7682,17 +7709,17 @@ TickType_t uxTaskResetEventItemValue( void ) taskENTER_CRITICAL(); { traceTASK_NOTIFY_TAKE( uxIndexToWaitOn ); - ulReturn = pxCurrentTCB->ulNotifiedValue[ uxIndexToWaitOn ]; + ulReturn = pxConstCurrentTCB->ulNotifiedValue[ uxIndexToWaitOn ]; if( ulReturn != 0U ) { if( xClearCountOnExit != pdFALSE ) { - pxCurrentTCB->ulNotifiedValue[ uxIndexToWaitOn ] = ( uint32_t ) 0U; + pxConstCurrentTCB->ulNotifiedValue[ uxIndexToWaitOn ] = ( uint32_t ) 0U; } else { - pxCurrentTCB->ulNotifiedValue[ uxIndexToWaitOn ] = ulReturn - ( uint32_t ) 1; + pxConstCurrentTCB->ulNotifiedValue[ uxIndexToWaitOn ] = ulReturn - ( uint32_t ) 1; } } else @@ -7700,7 +7727,7 @@ TickType_t uxTaskResetEventItemValue( void ) mtCOVERAGE_TEST_MARKER(); } - pxCurrentTCB->ucNotifyState[ uxIndexToWaitOn ] = taskNOT_WAITING_NOTIFICATION; + pxConstCurrentTCB->ucNotifyState[ uxIndexToWaitOn ] = taskNOT_WAITING_NOTIFICATION; } taskEXIT_CRITICAL(); @@ -7721,6 +7748,7 @@ TickType_t uxTaskResetEventItemValue( void ) TickType_t xTicksToWait ) { BaseType_t xReturn, xAlreadyYielded, xShouldBlock = pdFALSE; + TCB_t * const pxConstCurrentTCB = prvGetCurrentTask(); traceENTER_xTaskGenericNotifyWait( uxIndexToWaitOn, ulBitsToClearOnEntry, ulBitsToClearOnExit, pulNotificationValue, xTicksToWait ); @@ -7736,15 +7764,15 @@ TickType_t uxTaskResetEventItemValue( void ) taskENTER_CRITICAL(); { /* Only block if a notification is not already pending. */ - if( pxCurrentTCB->ucNotifyState[ uxIndexToWaitOn ] != taskNOTIFICATION_RECEIVED ) + if( pxConstCurrentTCB->ucNotifyState[ uxIndexToWaitOn ] != taskNOTIFICATION_RECEIVED ) { /* Clear bits in the task's notification value as bits may get * set by the notifying task or interrupt. This can be used * to clear the value to zero. */ - pxCurrentTCB->ulNotifiedValue[ uxIndexToWaitOn ] &= ~ulBitsToClearOnEntry; + pxConstCurrentTCB->ulNotifiedValue[ uxIndexToWaitOn ] &= ~ulBitsToClearOnEntry; /* Mark this task as waiting for a notification. */ - pxCurrentTCB->ucNotifyState[ uxIndexToWaitOn ] = taskWAITING_NOTIFICATION; + pxConstCurrentTCB->ucNotifyState[ uxIndexToWaitOn ] = taskWAITING_NOTIFICATION; if( xTicksToWait > ( TickType_t ) 0 ) { @@ -7795,14 +7823,14 @@ TickType_t uxTaskResetEventItemValue( void ) { /* Output the current notification value, which may or may not * have changed. */ - *pulNotificationValue = pxCurrentTCB->ulNotifiedValue[ uxIndexToWaitOn ]; + *pulNotificationValue = pxConstCurrentTCB->ulNotifiedValue[ uxIndexToWaitOn ]; } /* If ucNotifyValue is set then either the task never entered the * blocked state (because a notification was already pending) or the * task unblocked because of a notification. Otherwise the task * unblocked because of a timeout. */ - if( pxCurrentTCB->ucNotifyState[ uxIndexToWaitOn ] != taskNOTIFICATION_RECEIVED ) + if( pxConstCurrentTCB->ucNotifyState[ uxIndexToWaitOn ] != taskNOTIFICATION_RECEIVED ) { /* A notification was not received. */ xReturn = pdFALSE; @@ -7811,11 +7839,11 @@ TickType_t uxTaskResetEventItemValue( void ) { /* A notification was already pending or a notification was * received while the task was waiting. */ - pxCurrentTCB->ulNotifiedValue[ uxIndexToWaitOn ] &= ~ulBitsToClearOnExit; + pxConstCurrentTCB->ulNotifiedValue[ uxIndexToWaitOn ] &= ~ulBitsToClearOnExit; xReturn = pdTRUE; } - pxCurrentTCB->ucNotifyState[ uxIndexToWaitOn ] = taskNOT_WAITING_NOTIFICATION; + pxConstCurrentTCB->ucNotifyState[ uxIndexToWaitOn ] = taskNOT_WAITING_NOTIFICATION; } taskEXIT_CRITICAL(); @@ -7960,6 +7988,7 @@ TickType_t uxTaskResetEventItemValue( void ) uint8_t ucOriginalNotifyState; BaseType_t xReturn = pdPASS; UBaseType_t uxSavedInterruptStatus; + TCB_t * const pxConstCurrentTCB = prvGetCurrentTaskImmutable(); traceENTER_xTaskGenericNotifyFromISR( xTaskToNotify, uxIndexToNotify, ulValue, eAction, pulPreviousNotificationValue, pxHigherPriorityTaskWoken ); @@ -8065,7 +8094,7 @@ TickType_t uxTaskResetEventItemValue( void ) #if ( configNUMBER_OF_CORES == 1 ) { - if( pxTCB->uxPriority > pxCurrentTCB->uxPriority ) + if( pxTCB->uxPriority > pxConstCurrentTCB->uxPriority ) { /* The notified task has a priority above the currently * executing task so a yield is required. */ @@ -8122,6 +8151,7 @@ TickType_t uxTaskResetEventItemValue( void ) TCB_t * pxTCB; uint8_t ucOriginalNotifyState; UBaseType_t uxSavedInterruptStatus; + TCB_t * const pxConstCurrentTCB = prvGetCurrentTaskImmutable(); traceENTER_vTaskGenericNotifyGiveFromISR( xTaskToNotify, uxIndexToNotify, pxHigherPriorityTaskWoken ); @@ -8183,7 +8213,7 @@ TickType_t uxTaskResetEventItemValue( void ) #if ( configNUMBER_OF_CORES == 1 ) { - if( pxTCB->uxPriority > pxCurrentTCB->uxPriority ) + if( pxTCB->uxPriority > pxConstCurrentTCB->uxPriority ) { /* The notified task has a priority above the currently * executing task so a yield is required. */ @@ -8419,23 +8449,24 @@ static void prvAddCurrentTaskToDelayedList( TickType_t xTicksToWait, const TickType_t xConstTickCount = xTickCount; List_t * const pxDelayedList = pxDelayedTaskList; List_t * const pxOverflowDelayedList = pxOverflowDelayedTaskList; + TCB_t * const pxConstCurrentTCB = prvGetCurrentTaskImmutable(); #if ( INCLUDE_xTaskAbortDelay == 1 ) { /* About to enter a delayed list, so ensure the ucDelayAborted flag is * reset to pdFALSE so it can be detected as having been set to pdTRUE * when the task leaves the Blocked state. */ - pxCurrentTCB->ucDelayAborted = ( uint8_t ) pdFALSE; + pxConstCurrentTCB->ucDelayAborted = ( uint8_t ) pdFALSE; } #endif /* Remove the task from the ready list before adding it to the blocked list * as the same list item is used for both lists. */ - if( uxListRemove( &( pxCurrentTCB->xStateListItem ) ) == ( UBaseType_t ) 0 ) + if( uxListRemove( &( pxConstCurrentTCB->xStateListItem ) ) == ( UBaseType_t ) 0 ) { /* The current task must be in a ready list, so there is no need to * check, and the port reset macro can be called directly. */ - portRESET_READY_PRIORITY( pxCurrentTCB->uxPriority, uxTopReadyPriority ); + portRESET_READY_PRIORITY( pxConstCurrentTCB->uxPriority, uxTopReadyPriority ); } else { @@ -8449,7 +8480,7 @@ static void prvAddCurrentTaskToDelayedList( TickType_t xTicksToWait, /* Add the task to the suspended task list instead of a delayed task * list to ensure it is not woken by a timing event. It will block * indefinitely. */ - listINSERT_END( &xSuspendedTaskList, &( pxCurrentTCB->xStateListItem ) ); + listINSERT_END( &xSuspendedTaskList, &( pxConstCurrentTCB->xStateListItem ) ); } else { @@ -8459,21 +8490,21 @@ static void prvAddCurrentTaskToDelayedList( TickType_t xTicksToWait, xTimeToWake = xConstTickCount + xTicksToWait; /* The list item will be inserted in wake time order. */ - listSET_LIST_ITEM_VALUE( &( pxCurrentTCB->xStateListItem ), xTimeToWake ); + listSET_LIST_ITEM_VALUE( &( pxConstCurrentTCB->xStateListItem ), xTimeToWake ); if( xTimeToWake < xConstTickCount ) { /* Wake time has overflowed. Place this item in the overflow * list. */ traceMOVED_TASK_TO_OVERFLOW_DELAYED_LIST(); - vListInsert( pxOverflowDelayedList, &( pxCurrentTCB->xStateListItem ) ); + vListInsert( pxOverflowDelayedList, &( pxConstCurrentTCB->xStateListItem ) ); } else { /* The wake time has not overflowed, so the current block list * is used. */ traceMOVED_TASK_TO_DELAYED_LIST(); - vListInsert( pxDelayedList, &( pxCurrentTCB->xStateListItem ) ); + vListInsert( pxDelayedList, &( pxConstCurrentTCB->xStateListItem ) ); /* If the task entering the blocked state was placed at the * head of the list of blocked tasks then xNextTaskUnblockTime @@ -8497,19 +8528,19 @@ static void prvAddCurrentTaskToDelayedList( TickType_t xTicksToWait, xTimeToWake = xConstTickCount + xTicksToWait; /* The list item will be inserted in wake time order. */ - listSET_LIST_ITEM_VALUE( &( pxCurrentTCB->xStateListItem ), xTimeToWake ); + listSET_LIST_ITEM_VALUE( &( pxConstCurrentTCB->xStateListItem ), xTimeToWake ); if( xTimeToWake < xConstTickCount ) { traceMOVED_TASK_TO_OVERFLOW_DELAYED_LIST(); /* Wake time has overflowed. Place this item in the overflow list. */ - vListInsert( pxOverflowDelayedList, &( pxCurrentTCB->xStateListItem ) ); + vListInsert( pxOverflowDelayedList, &( pxConstCurrentTCB->xStateListItem ) ); } else { traceMOVED_TASK_TO_DELAYED_LIST(); /* The wake time has not overflowed, so the current block list is used. */ - vListInsert( pxDelayedList, &( pxCurrentTCB->xStateListItem ) ); + vListInsert( pxDelayedList, &( pxConstCurrentTCB->xStateListItem ) ); /* If the task entering the blocked state was placed at the head of the * list of blocked tasks then xNextTaskUnblockTime needs to be updated From edfa323bf36cc34116089e3bf252efc7ce5c46d1 Mon Sep 17 00:00:00 2001 From: "Ching-Hsin,Lee" Date: Mon, 20 May 2024 16:34:59 +0800 Subject: [PATCH 02/25] Update the usage of pxCurrentTCB * Update the usage of pxCurrentTCB to improve multiple accessing volatile variable in a function --- tasks.c | 81 ++++++++++++++++++++++++++++++++++----------------------- 1 file changed, 49 insertions(+), 32 deletions(-) diff --git a/tasks.c b/tasks.c index 633b3cf56c..eb616bf5da 100644 --- a/tasks.c +++ b/tasks.c @@ -86,7 +86,7 @@ #define taskYIELD_ANY_CORE_IF_USING_PREEMPTION( pxTCB ) \ do { \ - TCB_t * const pxConstCurrentTCB = prvGetCurrentTask(); \ + TCB_t * const pxConstCurrentTCB = prvGetCurrentTaskTCB(); \ if( pxConstCurrentTCB->uxPriority < ( pxTCB )->uxPriority ) \ { \ portYIELD_WITHIN_API(); \ @@ -220,7 +220,7 @@ #define taskSELECT_HIGHEST_PRIORITY_TASK() \ do { \ UBaseType_t uxTopPriority; \ - TCB_t * const pxConstCurrentTCB = prvGetCurrentTask(); \ + TCB_t * const pxConstCurrentTCB = prvGetCurrentTaskTCB(); \ \ /* Find the highest priority list that contains ready tasks. */ \ portGET_HIGHEST_PRIORITY( uxTopPriority, uxTopReadyPriority ); \ @@ -282,7 +282,7 @@ * task should be used in place of the parameter. This macro simply checks to * see if the parameter is NULL and returns a pointer to the appropriate TCB. */ -#define prvGetTCBFromHandle( pxHandle ) ( ( ( pxHandle ) == NULL ) ? prvGetCurrentTask() : ( pxHandle ) ) +#define prvGetTCBFromHandle( pxHandle ) ( ( ( pxHandle ) == NULL ) ? prvGetCurrentTaskTCB() : ( pxHandle ) ) /* The item value of the event list item is normally used to hold the priority * of the task to which it belongs (coded to allow it to be held in reverse @@ -441,11 +441,17 @@ typedef struct tskTaskControlBlock /* The old naming convention is used to * below to enable the use of older kernel aware debuggers. */ typedef tskTCB TCB_t; -#if configNUMBER_OF_CORES == 1 - #define prvGetCurrentTaskImmutable() pxCurrentTCB - #define prvGetCurrentTask() pxCurrentTCB +/* Retrieving the current task TCB in a multi-core environment involves two steps: + * 1. Obtaining the core ID. + * 2. Using the core ID to index the pxCurrentTCBs array. + * If these two steps are not performed atomically, a race condition may occur. + * To ensure atomicity, prvGetCurrentTaskTCBUnsafe() should be called in a context where + * the core executing the task remains fixed until the operation is completed. */ +#if ( configNUMBER_OF_CORES == 1 ) + #define prvGetCurrentTaskTCBUnsafe() pxCurrentTCB + #define prvGetCurrentTaskTCB() pxCurrentTCB #else - #define prvGetCurrentTaskImmutable() pxCurrentTCBs[ portGET_CORE_ID() ] + #define prvGetCurrentTaskTCBUnsafe() pxCurrentTCBs[ portGET_CORE_ID() ] #endif #if ( configNUMBER_OF_CORES == 1 ) @@ -541,6 +547,15 @@ static BaseType_t prvCreateIdleTasks( void ); #if ( configNUMBER_OF_CORES > 1 ) +/* + * The race condition safe version to get the current task TCB in multiple cores + * environment. + */ + static TCB_t * prvGetCurrentTaskTCB( void ); +#endif /* #if ( configNUMBER_OF_CORES > 1 ) */ + +#if ( configNUMBER_OF_CORES > 1 ) + /* * Checks to see if another task moved the current task out of the ready * list while it was waiting to enter a critical section and yields, if so. @@ -811,7 +826,7 @@ static void prvAddNewTaskToReadyList( TCB_t * pxNewTCB ) PRIVILEGED_FUNCTION; #if ( configNUMBER_OF_CORES > 1 ) - static TCB_t * prvGetCurrentTask( void ) + static TCB_t * prvGetCurrentTaskTCB( void ) { TCB_t * pxTCB; UBaseType_t uxSavedInterruptStatus; @@ -826,6 +841,8 @@ static void prvAddNewTaskToReadyList( TCB_t * pxNewTCB ) PRIVILEGED_FUNCTION; } #endif /* if ( configNUMBER_OF_CORES > 1 ) */ +/*-----------------------------------------------------------*/ + #if ( configNUMBER_OF_CORES > 1 ) static void prvCheckForRunStateChange( void ) { @@ -2793,7 +2810,7 @@ static void prvInitialiseNewTask( TaskFunction_t pxTaskCode, TCB_t * pxTCB; UBaseType_t uxCurrentBasePriority, uxPriorityUsedOnEntry; BaseType_t xYieldRequired = pdFALSE; - TCB_t * const pxConstCurrentTCB = prvGetCurrentTask(); + TCB_t * const pxConstCurrentTCB = prvGetCurrentTaskTCB(); #if ( configNUMBER_OF_CORES > 1 ) BaseType_t xYieldForTask = pdFALSE; @@ -3442,7 +3459,7 @@ static void prvInitialiseNewTask( TaskFunction_t pxTaskCode, BaseType_t xYieldRequired = pdFALSE; TCB_t * const pxTCB = xTaskToResume; UBaseType_t uxSavedInterruptStatus; - TCB_t * const pxConstCurrentTCB = prvGetCurrentTaskImmutable(); + TCB_t * const pxConstCurrentTCB = prvGetCurrentTaskTCBUnsafe(); traceENTER_xTaskResumeFromISR( xTaskToResume ); @@ -3738,7 +3755,7 @@ void vTaskStartScheduler( void ) #if ( configUSE_C_RUNTIME_TLS_SUPPORT == 1 ) { - TCB_t * const pxConstCurrentTCB = prvGetCurrentTask(); + TCB_t * const pxConstCurrentTCB = prvGetCurrentTaskTCB(); /* Switch C-Runtime's TLS Block to point to the TLS * block specific to the task that will run first. */ @@ -3924,7 +3941,7 @@ void vTaskSuspendAll( void ) { TickType_t xReturn; UBaseType_t uxHigherPriorityReadyTasks = pdFALSE; - TCB_t * const pxConstCurrentTCB = prvGetCurrentTask(); + TCB_t * const pxConstCurrentTCB = prvGetCurrentTaskTCB(); /* uxHigherPriorityReadyTasks takes care of the case where * configUSE_PREEMPTION is 0, so there may be tasks above the idle priority @@ -4002,7 +4019,7 @@ BaseType_t xTaskResumeAll( void ) taskENTER_CRITICAL(); { BaseType_t xCoreID; - TCB_t * const pxConstCurrentTCB = prvGetCurrentTaskImmutable(); + TCB_t * const pxConstCurrentTCB = prvGetCurrentTaskTCBUnsafe(); xCoreID = ( BaseType_t ) portGET_CORE_ID(); @@ -4658,7 +4675,7 @@ BaseType_t xTaskCatchUpTicks( TickType_t xTicksToCatchUp ) { #if ( configNUMBER_OF_CORES == 1 ) { - TCB_t * const pxCurrentTCB = prvGetCurrentTask(); + TCB_t * const pxCurrentTCB = prvGetCurrentTaskTCB(); /* Preemption is on, but a context switch should only be * performed if the unblocked task has a priority that is @@ -4706,7 +4723,7 @@ BaseType_t xTaskIncrementTick( void ) TCB_t * pxTCB; TickType_t xItemValue; BaseType_t xSwitchRequired = pdFALSE; - TCB_t * const pxConstCurrentTCB = prvGetCurrentTaskImmutable(); + TCB_t * const pxConstCurrentTCB = prvGetCurrentTaskTCBUnsafe(); #if ( configUSE_PREEMPTION == 1 ) && ( configNUMBER_OF_CORES > 1 ) BaseType_t xYieldRequiredForCore[ configNUMBER_OF_CORES ] = { pdFALSE }; @@ -5264,7 +5281,7 @@ BaseType_t xTaskIncrementTick( void ) void vTaskPlaceOnEventList( List_t * const pxEventList, const TickType_t xTicksToWait ) { - TCB_t * const pxConstCurrentTCB = prvGetCurrentTaskImmutable(); + TCB_t * const pxConstCurrentTCB = prvGetCurrentTaskTCBUnsafe(); traceENTER_vTaskPlaceOnEventList( pxEventList, xTicksToWait ); @@ -5296,7 +5313,7 @@ void vTaskPlaceOnUnorderedEventList( List_t * pxEventList, const TickType_t xItemValue, const TickType_t xTicksToWait ) { - TCB_t * const pxConstCurrentTCB = prvGetCurrentTaskImmutable(); + TCB_t * const pxConstCurrentTCB = prvGetCurrentTaskTCBUnsafe(); traceENTER_vTaskPlaceOnUnorderedEventList( pxEventList, xItemValue, xTicksToWait ); @@ -5330,7 +5347,7 @@ void vTaskPlaceOnUnorderedEventList( List_t * pxEventList, TickType_t xTicksToWait, const BaseType_t xWaitIndefinitely ) { - TCB_t * const pxConstCurrentTCB = prvGetCurrentTaskImmutable(); + TCB_t * const pxConstCurrentTCB = prvGetCurrentTaskTCBUnsafe(); traceENTER_vTaskPlaceOnEventListRestricted( pxEventList, xTicksToWait, xWaitIndefinitely ); @@ -5370,7 +5387,7 @@ BaseType_t xTaskRemoveFromEventList( const List_t * const pxEventList ) TCB_t * pxUnblockedTCB; BaseType_t xReturn; - TCB_t * const pxConstCurrentTCB = prvGetCurrentTaskImmutable(); + TCB_t * const pxConstCurrentTCB = prvGetCurrentTaskTCBUnsafe(); traceENTER_xTaskRemoveFromEventList( pxEventList ); @@ -5464,7 +5481,7 @@ void vTaskRemoveFromUnorderedEventList( ListItem_t * pxEventListItem, const TickType_t xItemValue ) { TCB_t * pxUnblockedTCB; - TCB_t * const pxConstCurrentTCB = prvGetCurrentTaskImmutable(); + TCB_t * const pxConstCurrentTCB = prvGetCurrentTaskTCBUnsafe(); traceENTER_vTaskRemoveFromUnorderedEventList( pxEventListItem, xItemValue ); @@ -5567,7 +5584,7 @@ BaseType_t xTaskCheckForTimeOut( TimeOut_t * const pxTimeOut, BaseType_t xReturn; #if ( INCLUDE_xTaskAbortDelay == 1 ) - TCB_t * const pxConstCurrentTCB = prvGetCurrentTaskImmutable(); + TCB_t * const pxConstCurrentTCB = prvGetCurrentTaskTCBUnsafe(); #endif traceENTER_xTaskCheckForTimeOut( pxTimeOut, pxTicksToWait ); @@ -6522,7 +6539,7 @@ static void prvResetNextTaskUnblockTime( void ) /* In SMP environment, interrupt must be disabled to get the current * task TCB. */ - xReturn = prvGetCurrentTask(); + xReturn = prvGetCurrentTaskTCB(); traceRETURN_xTaskGetCurrentTaskHandle( xReturn ); @@ -6599,7 +6616,7 @@ static void prvResetNextTaskUnblockTime( void ) TCB_t * const pxMutexHolderTCB = pxMutexHolder; BaseType_t xReturn = pdFALSE; - TCB_t * const pxConstCurrentTCB = prvGetCurrentTaskImmutable(); + TCB_t * const pxConstCurrentTCB = prvGetCurrentTaskTCBUnsafe(); traceENTER_xTaskPriorityInherit( pxMutexHolder ); @@ -6946,7 +6963,7 @@ static void prvResetNextTaskUnblockTime( void ) if( xSchedulerRunning != pdFALSE ) { - TCB_t * const pxConstCurrentTCB = prvGetCurrentTaskImmutable(); + TCB_t * const pxConstCurrentTCB = prvGetCurrentTaskTCBUnsafe(); ( pxConstCurrentTCB->uxCriticalNesting )++; @@ -7058,7 +7075,7 @@ static void prvResetNextTaskUnblockTime( void ) void vTaskExitCritical( void ) { - TCB_t * const pxConstCurrentTCB = prvGetCurrentTaskImmutable(); + TCB_t * const pxConstCurrentTCB = prvGetCurrentTaskTCBUnsafe(); traceENTER_vTaskExitCritical(); @@ -7593,7 +7610,7 @@ TickType_t uxTaskResetEventItemValue( void ) { TickType_t uxReturn; - TCB_t * const pxConstCurrentTCB = prvGetCurrentTaskImmutable(); + TCB_t * const pxConstCurrentTCB = prvGetCurrentTaskTCBUnsafe(); traceENTER_uxTaskResetEventItemValue(); @@ -7618,7 +7635,7 @@ TickType_t uxTaskResetEventItemValue( void ) traceENTER_pvTaskIncrementMutexHeldCount(); /* This API should be called in critical section only. */ - pxTCB = prvGetCurrentTaskImmutable(); + pxTCB = prvGetCurrentTaskTCBUnsafe(); /* If xSemaphoreCreateMutex() is called before any tasks have been created * then pxCurrentTCB will be NULL. */ @@ -7643,7 +7660,7 @@ TickType_t uxTaskResetEventItemValue( void ) { uint32_t ulReturn; BaseType_t xAlreadyYielded, xShouldBlock = pdFALSE; - TCB_t * const pxConstCurrentTCB = prvGetCurrentTask(); + TCB_t * const pxConstCurrentTCB = prvGetCurrentTaskTCB(); traceENTER_ulTaskGenericNotifyTake( uxIndexToWaitOn, xClearCountOnExit, xTicksToWait ); @@ -7748,7 +7765,7 @@ TickType_t uxTaskResetEventItemValue( void ) TickType_t xTicksToWait ) { BaseType_t xReturn, xAlreadyYielded, xShouldBlock = pdFALSE; - TCB_t * const pxConstCurrentTCB = prvGetCurrentTask(); + TCB_t * const pxConstCurrentTCB = prvGetCurrentTaskTCB(); traceENTER_xTaskGenericNotifyWait( uxIndexToWaitOn, ulBitsToClearOnEntry, ulBitsToClearOnExit, pulNotificationValue, xTicksToWait ); @@ -7988,7 +8005,7 @@ TickType_t uxTaskResetEventItemValue( void ) uint8_t ucOriginalNotifyState; BaseType_t xReturn = pdPASS; UBaseType_t uxSavedInterruptStatus; - TCB_t * const pxConstCurrentTCB = prvGetCurrentTaskImmutable(); + TCB_t * const pxConstCurrentTCB = prvGetCurrentTaskTCBUnsafe(); traceENTER_xTaskGenericNotifyFromISR( xTaskToNotify, uxIndexToNotify, ulValue, eAction, pulPreviousNotificationValue, pxHigherPriorityTaskWoken ); @@ -8151,7 +8168,7 @@ TickType_t uxTaskResetEventItemValue( void ) TCB_t * pxTCB; uint8_t ucOriginalNotifyState; UBaseType_t uxSavedInterruptStatus; - TCB_t * const pxConstCurrentTCB = prvGetCurrentTaskImmutable(); + TCB_t * const pxConstCurrentTCB = prvGetCurrentTaskTCBUnsafe(); traceENTER_vTaskGenericNotifyGiveFromISR( xTaskToNotify, uxIndexToNotify, pxHigherPriorityTaskWoken ); @@ -8449,7 +8466,7 @@ static void prvAddCurrentTaskToDelayedList( TickType_t xTicksToWait, const TickType_t xConstTickCount = xTickCount; List_t * const pxDelayedList = pxDelayedTaskList; List_t * const pxOverflowDelayedList = pxOverflowDelayedTaskList; - TCB_t * const pxConstCurrentTCB = prvGetCurrentTaskImmutable(); + TCB_t * const pxConstCurrentTCB = prvGetCurrentTaskTCBUnsafe(); #if ( INCLUDE_xTaskAbortDelay == 1 ) { From b2f9dadcc4412af27ee641fe8fd2e11679d42520 Mon Sep 17 00:00:00 2001 From: "Ching-Hsin,Lee" Date: Mon, 20 May 2024 17:01:23 +0800 Subject: [PATCH 03/25] Refine the implementation --- tasks.c | 16 +++++++--------- 1 file changed, 7 insertions(+), 9 deletions(-) diff --git a/tasks.c b/tasks.c index eb616bf5da..180704fa41 100644 --- a/tasks.c +++ b/tasks.c @@ -2320,7 +2320,7 @@ static void prvInitialiseNewTask( TaskFunction_t pxTaskCode, * ready list. */ #if ( configNUMBER_OF_CORES > 1 ) { - if( taskTASK_IS_RUNNING( pxTCB ) != pdFALSE ) + if( taskTASK_IS_RUNNING( pxTCB ) == pdTRUE ) { if( pxTCB->xTaskRunState == ( BaseType_t ) portGET_CORE_ID() ) { @@ -2361,7 +2361,7 @@ static void prvInitialiseNewTask( TaskFunction_t pxTaskCode, { if( xSchedulerRunning != pdFALSE ) { - if( taskTASK_IS_RUNNING( pxTCB ) != pdFALSE ) + if( taskTASK_IS_RUNNING( pxTCB ) == pdTRUE ) { configASSERT( uxSchedulerSuspended == 0 ); taskYIELD_WITHIN_API(); @@ -2856,7 +2856,7 @@ static void prvInitialiseNewTask( TaskFunction_t pxTaskCode, { #if ( configNUMBER_OF_CORES == 1 ) { - if( taskTASK_IS_RUNNING( pxTCB ) != pdTRUE ) + if( taskTASK_IS_RUNNING( pxTCB ) == pdFALSE ) { /* The priority of a task other than the currently * running task is being raised. Is the priority being @@ -3275,7 +3275,7 @@ static void prvInitialiseNewTask( TaskFunction_t pxTaskCode, mtCOVERAGE_TEST_MARKER(); } - if( taskTASK_IS_RUNNING( pxTCB ) != pdFALSE ) + if( taskTASK_IS_RUNNING( pxTCB ) == pdTRUE ) { if( xSchedulerRunning != pdFALSE ) { @@ -3406,7 +3406,7 @@ static void prvInitialiseNewTask( TaskFunction_t pxTaskCode, /* The parameter cannot be NULL as it is impossible to resume the * currently executing task. */ - if( ( taskTASK_IS_RUNNING( pxTCB ) != pdTRUE ) && ( pxTCB != NULL ) ) + if( ( taskTASK_IS_RUNNING( pxTCB ) == pdFALSE ) && ( pxTCB != NULL ) ) #else /* The parameter cannot be NULL as it is impossible to resume the @@ -4018,11 +4018,9 @@ BaseType_t xTaskResumeAll( void ) * tasks from this list into their appropriate ready list. */ taskENTER_CRITICAL(); { - BaseType_t xCoreID; + BaseType_t xCoreID = portGET_CORE_ID(); TCB_t * const pxConstCurrentTCB = prvGetCurrentTaskTCBUnsafe(); - xCoreID = ( BaseType_t ) portGET_CORE_ID(); - /* If uxSchedulerSuspended is zero then this function does not match a * previous call to vTaskSuspendAll(). */ configASSERT( uxSchedulerSuspended != 0U ); @@ -5252,7 +5250,7 @@ BaseType_t xTaskIncrementTick( void ) /* Macro to inject port specific behaviour immediately after * switching tasks, such as setting an end of stack watchpoint * or reconfiguring the MPU. */ - portTASK_SWITCH_HOOK( pxCurrentTCBs[ xCoreID ] ); + portTASK_SWITCH_HOOK( pxCurrentTCBs[ portGET_CORE_ID() ] ); /* After the new task is switched in, update the global errno. */ #if ( configUSE_POSIX_ERRNO == 1 ) From bade1a671a5d98784c6874b7f4ea2bd382afaca8 Mon Sep 17 00:00:00 2001 From: "Ching-Hsin,Lee" Date: Mon, 20 May 2024 17:16:20 +0800 Subject: [PATCH 04/25] Fix typo --- tasks.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tasks.c b/tasks.c index 0f9807fb88..73d27c3029 100644 --- a/tasks.c +++ b/tasks.c @@ -4674,7 +4674,7 @@ BaseType_t xTaskCatchUpTicks( TickType_t xTicksToCatchUp ) { #if ( configNUMBER_OF_CORES == 1 ) { - TCB_t * const pxCurrentTCB = prvGetCurrentTaskTCB(); + TCB_t * const pxConstCurrentTCB = prvGetCurrentTaskTCB(); /* Preemption is on, but a context switch should only be * performed if the unblocked task has a priority that is From c774dfda636907727a544d637a0fa606d3a6228a Mon Sep 17 00:00:00 2001 From: "Ching-Hsin,Lee" Date: Mon, 20 May 2024 17:33:56 +0800 Subject: [PATCH 05/25] Revert select highest priority task --- tasks.c | 17 ++++++++--------- 1 file changed, 8 insertions(+), 9 deletions(-) diff --git a/tasks.c b/tasks.c index 73d27c3029..c55c2781ca 100644 --- a/tasks.c +++ b/tasks.c @@ -217,15 +217,14 @@ /*-----------------------------------------------------------*/ - #define taskSELECT_HIGHEST_PRIORITY_TASK() \ - do { \ - UBaseType_t uxTopPriority; \ - TCB_t * const pxConstCurrentTCB = prvGetCurrentTaskTCB(); \ - \ - /* Find the highest priority list that contains ready tasks. */ \ - portGET_HIGHEST_PRIORITY( uxTopPriority, uxTopReadyPriority ); \ - configASSERT( listCURRENT_LIST_LENGTH( &( pxReadyTasksLists[ uxTopPriority ] ) ) > 0 ); \ - listGET_OWNER_OF_NEXT_ENTRY( pxConstCurrentTCB, &( pxReadyTasksLists[ uxTopPriority ] ) ); \ + #define taskSELECT_HIGHEST_PRIORITY_TASK() \ + do { \ + UBaseType_t uxTopPriority; \ + \ + /* Find the highest priority list that contains ready tasks. */ \ + portGET_HIGHEST_PRIORITY( uxTopPriority, uxTopReadyPriority ); \ + configASSERT( listCURRENT_LIST_LENGTH( &( pxReadyTasksLists[ uxTopPriority ] ) ) > 0 ); \ + listGET_OWNER_OF_NEXT_ENTRY( pxCurrentTCB, &( pxReadyTasksLists[ uxTopPriority ] ) ); \ } while( 0 ) /*-----------------------------------------------------------*/ From 69581faaa078e7b750060723d6cfdcb67958b790 Mon Sep 17 00:00:00 2001 From: "Ching-Hsin,Lee" Date: Mon, 20 May 2024 17:52:50 +0800 Subject: [PATCH 06/25] Fix compiler error --- tasks.c | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-) diff --git a/tasks.c b/tasks.c index c55c2781ca..dc5e09ffd1 100644 --- a/tasks.c +++ b/tasks.c @@ -463,6 +463,7 @@ typedef tskTCB TCB_t; /* More details at: https://github.com/FreeRTOS/FreeRTOS-Kernel/blob/main/MISRA.md#rule-84 */ /* coverity[misra_c_2012_rule_8_4_violation] */ portDONT_DISCARD PRIVILEGED_DATA TCB_t * volatile pxCurrentTCBs[ configNUMBER_OF_CORES ]; + #define pxCurrentTCB prvGetCurrentTCBUnsafe() #endif /* Lists for ready and blocked tasks. -------------------- @@ -2810,7 +2811,6 @@ static void prvInitialiseNewTask( TaskFunction_t pxTaskCode, TCB_t * pxTCB; UBaseType_t uxCurrentBasePriority, uxPriorityUsedOnEntry; BaseType_t xYieldRequired = pdFALSE; - TCB_t * const pxConstCurrentTCB = prvGetCurrentTaskTCB(); #if ( configNUMBER_OF_CORES > 1 ) BaseType_t xYieldForTask = pdFALSE; @@ -2856,6 +2856,8 @@ static void prvInitialiseNewTask( TaskFunction_t pxTaskCode, { #if ( configNUMBER_OF_CORES == 1 ) { + TCB_t * const pxConstCurrentTCB = prvGetCurrentTaskTCB(); + if( taskTASK_IS_RUNNING( pxTCB ) == pdFALSE ) { /* The priority of a task other than the currently @@ -3459,7 +3461,6 @@ static void prvInitialiseNewTask( TaskFunction_t pxTaskCode, BaseType_t xYieldRequired = pdFALSE; TCB_t * const pxTCB = xTaskToResume; UBaseType_t uxSavedInterruptStatus; - TCB_t * const pxConstCurrentTCB = prvGetCurrentTaskTCBUnsafe(); traceENTER_xTaskResumeFromISR( xTaskToResume ); @@ -3497,6 +3498,8 @@ static void prvInitialiseNewTask( TaskFunction_t pxTaskCode, { #if ( configNUMBER_OF_CORES == 1 ) { + TCB_t * const pxConstCurrentTCB = prvGetCurrentTaskTCBUnsafe(); + /* Ready lists can be accessed so move the task from the * suspended list to the ready list directly. */ if( pxTCB->uxPriority > pxConstCurrentTCB->uxPriority ) @@ -4019,7 +4022,6 @@ BaseType_t xTaskResumeAll( void ) taskENTER_CRITICAL(); { BaseType_t xCoreID = portGET_CORE_ID(); - TCB_t * const pxConstCurrentTCB = prvGetCurrentTaskTCBUnsafe(); /* If uxSchedulerSuspended is zero then this function does not match a * previous call to vTaskSuspendAll(). */ @@ -4047,6 +4049,8 @@ BaseType_t xTaskResumeAll( void ) #if ( configNUMBER_OF_CORES == 1 ) { + TCB_t * const pxConstCurrentTCB = prvGetCurrentTaskTCBUnsafe(); + /* If the moved task has a priority higher than the current * task then a yield must be performed. */ if( pxTCB->uxPriority > pxConstCurrentTCB->uxPriority ) @@ -4721,7 +4725,10 @@ BaseType_t xTaskIncrementTick( void ) TCB_t * pxTCB; TickType_t xItemValue; BaseType_t xSwitchRequired = pdFALSE; - TCB_t * const pxConstCurrentTCB = prvGetCurrentTaskTCBUnsafe(); + + #if ( configNUMBER_OF_CORES == 1 ) + TCB_t * const pxConstCurrentTCB = prvGetCurrentTaskTCBUnsafe(); + #endif /* #if ( configNUMBER_OF_CORES == 1 ) */ #if ( configUSE_PREEMPTION == 1 ) && ( configNUMBER_OF_CORES > 1 ) BaseType_t xYieldRequiredForCore[ configNUMBER_OF_CORES ] = { pdFALSE }; From 080b840a01723e0285a5c7956b80a31c621d4abe Mon Sep 17 00:00:00 2001 From: "Ching-Hsin,Lee" Date: Mon, 20 May 2024 17:56:19 +0800 Subject: [PATCH 07/25] Update pxCurrentTCB --- tasks.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tasks.c b/tasks.c index dc5e09ffd1..802c966e69 100644 --- a/tasks.c +++ b/tasks.c @@ -463,7 +463,7 @@ typedef tskTCB TCB_t; /* More details at: https://github.com/FreeRTOS/FreeRTOS-Kernel/blob/main/MISRA.md#rule-84 */ /* coverity[misra_c_2012_rule_8_4_violation] */ portDONT_DISCARD PRIVILEGED_DATA TCB_t * volatile pxCurrentTCBs[ configNUMBER_OF_CORES ]; - #define pxCurrentTCB prvGetCurrentTCBUnsafe() + #define pxCurrentTCB prvGetCurrentTaskTCBUnsafe() #endif /* Lists for ready and blocked tasks. -------------------- From 1c9130fbfa4d9161fcb0190ced469a77a2d80018 Mon Sep 17 00:00:00 2001 From: "Ching-Hsin,Lee" Date: Mon, 20 May 2024 17:59:07 +0800 Subject: [PATCH 08/25] Update usage again --- tasks.c | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/tasks.c b/tasks.c index 802c966e69..2eb6929e3b 100644 --- a/tasks.c +++ b/tasks.c @@ -5392,8 +5392,6 @@ BaseType_t xTaskRemoveFromEventList( const List_t * const pxEventList ) TCB_t * pxUnblockedTCB; BaseType_t xReturn; - TCB_t * const pxConstCurrentTCB = prvGetCurrentTaskTCBUnsafe(); - traceENTER_xTaskRemoveFromEventList( pxEventList ); /* THIS FUNCTION MUST BE CALLED FROM A CRITICAL SECTION. It can also be @@ -5444,6 +5442,8 @@ BaseType_t xTaskRemoveFromEventList( const List_t * const pxEventList ) #if ( configNUMBER_OF_CORES == 1 ) { + TCB_t * const pxConstCurrentTCB = prvGetCurrentTaskTCBUnsafe(); + if( pxUnblockedTCB->uxPriority > pxConstCurrentTCB->uxPriority ) { /* Return true if the task removed from the event list has a higher @@ -5486,7 +5486,6 @@ void vTaskRemoveFromUnorderedEventList( ListItem_t * pxEventListItem, const TickType_t xItemValue ) { TCB_t * pxUnblockedTCB; - TCB_t * const pxConstCurrentTCB = prvGetCurrentTaskTCBUnsafe(); traceENTER_vTaskRemoveFromUnorderedEventList( pxEventListItem, xItemValue ); @@ -5528,6 +5527,8 @@ void vTaskRemoveFromUnorderedEventList( ListItem_t * pxEventListItem, #if ( configNUMBER_OF_CORES == 1 ) { + TCB_t * const pxConstCurrentTCB = prvGetCurrentTaskTCBUnsafe(); + if( pxUnblockedTCB->uxPriority > pxConstCurrentTCB->uxPriority ) { /* The unblocked task has a priority above that of the calling task, so @@ -8010,7 +8011,6 @@ TickType_t uxTaskResetEventItemValue( void ) uint8_t ucOriginalNotifyState; BaseType_t xReturn = pdPASS; UBaseType_t uxSavedInterruptStatus; - TCB_t * const pxConstCurrentTCB = prvGetCurrentTaskTCBUnsafe(); traceENTER_xTaskGenericNotifyFromISR( xTaskToNotify, uxIndexToNotify, ulValue, eAction, pulPreviousNotificationValue, pxHigherPriorityTaskWoken ); @@ -8116,6 +8116,8 @@ TickType_t uxTaskResetEventItemValue( void ) #if ( configNUMBER_OF_CORES == 1 ) { + TCB_t * const pxConstCurrentTCB = prvGetCurrentTaskTCBUnsafe(); + if( pxTCB->uxPriority > pxConstCurrentTCB->uxPriority ) { /* The notified task has a priority above the currently @@ -8173,7 +8175,6 @@ TickType_t uxTaskResetEventItemValue( void ) TCB_t * pxTCB; uint8_t ucOriginalNotifyState; UBaseType_t uxSavedInterruptStatus; - TCB_t * const pxConstCurrentTCB = prvGetCurrentTaskTCBUnsafe(); traceENTER_vTaskGenericNotifyGiveFromISR( xTaskToNotify, uxIndexToNotify, pxHigherPriorityTaskWoken ); @@ -8235,6 +8236,8 @@ TickType_t uxTaskResetEventItemValue( void ) #if ( configNUMBER_OF_CORES == 1 ) { + TCB_t * const pxConstCurrentTCB = prvGetCurrentTaskTCBUnsafe(); + if( pxTCB->uxPriority > pxConstCurrentTCB->uxPriority ) { /* The notified task has a priority above the currently From 8200e590f187fd9c589e45d713b178ddec651613 Mon Sep 17 00:00:00 2001 From: "Ching-Hsin,Lee" Date: Mon, 20 May 2024 18:13:17 +0800 Subject: [PATCH 09/25] Remove pxCurrentTCB define in SMP --- include/stack_macros.h | 71 ++++++++++++++++++++++-------------------- tasks.c | 1 - 2 files changed, 38 insertions(+), 34 deletions(-) diff --git a/include/stack_macros.h b/include/stack_macros.h index c1018b68a2..3b1ca65d49 100644 --- a/include/stack_macros.h +++ b/include/stack_macros.h @@ -56,14 +56,16 @@ #if ( ( configCHECK_FOR_STACK_OVERFLOW == 1 ) && ( portSTACK_GROWTH < 0 ) ) /* Only the current stack state is to be checked. */ - #define taskCHECK_FOR_STACK_OVERFLOW() \ - do { \ - /* Is the currently saved stack pointer within the stack limit? */ \ - if( pxCurrentTCB->pxTopOfStack <= pxCurrentTCB->pxStack + portSTACK_LIMIT_PADDING ) \ - { \ - char * pcOverflowTaskName = pxCurrentTCB->pcTaskName; \ - vApplicationStackOverflowHook( ( TaskHandle_t ) pxCurrentTCB, pcOverflowTaskName ); \ - } \ + #define taskCHECK_FOR_STACK_OVERFLOW() \ + do { \ + TCB_t * const pxConstCurrentTCB = prvGetCurrentTaskTCBUnsafe(); \ + \ + /* Is the currently saved stack pointer within the stack limit? */ \ + if( pxConstCurrentTCB->pxTopOfStack <= pxConstCurrentTCB->pxStack + portSTACK_LIMIT_PADDING ) \ + { \ + char * pcOverflowTaskName = pxConstCurrentTCB->pcTaskName; \ + vApplicationStackOverflowHook( ( TaskHandle_t ) pxConstCurrentTCB, pcOverflowTaskName ); \ + } \ } while( 0 ) #endif /* configCHECK_FOR_STACK_OVERFLOW == 1 */ @@ -72,15 +74,16 @@ #if ( ( configCHECK_FOR_STACK_OVERFLOW == 1 ) && ( portSTACK_GROWTH > 0 ) ) /* Only the current stack state is to be checked. */ - #define taskCHECK_FOR_STACK_OVERFLOW() \ - do { \ - \ - /* Is the currently saved stack pointer within the stack limit? */ \ - if( pxCurrentTCB->pxTopOfStack >= pxCurrentTCB->pxEndOfStack - portSTACK_LIMIT_PADDING ) \ - { \ - char * pcOverflowTaskName = pxCurrentTCB->pcTaskName; \ - vApplicationStackOverflowHook( ( TaskHandle_t ) pxCurrentTCB, pcOverflowTaskName ); \ - } \ + #define taskCHECK_FOR_STACK_OVERFLOW() \ + do { \ + TCB_t * const pxConstCurrentTCB = prvGetCurrentTaskTCBUnsafe(); \ + \ + /* Is the currently saved stack pointer within the stack limit? */ \ + if( pxConstCurrentTCB->pxTopOfStack >= pxConstCurrentTCB->pxEndOfStack - portSTACK_LIMIT_PADDING ) \ + { \ + char * pcOverflowTaskName = pxConstCurrentTCB->pcTaskName; \ + vApplicationStackOverflowHook( ( TaskHandle_t ) pxConstCurrentTCB, pcOverflowTaskName ); \ + } \ } while( 0 ) #endif /* configCHECK_FOR_STACK_OVERFLOW == 1 */ @@ -88,19 +91,20 @@ #if ( ( configCHECK_FOR_STACK_OVERFLOW > 1 ) && ( portSTACK_GROWTH < 0 ) ) - #define taskCHECK_FOR_STACK_OVERFLOW() \ - do { \ - const uint32_t * const pulStack = ( uint32_t * ) pxCurrentTCB->pxStack; \ - const uint32_t ulCheckValue = ( uint32_t ) 0xa5a5a5a5U; \ - \ - if( ( pulStack[ 0 ] != ulCheckValue ) || \ - ( pulStack[ 1 ] != ulCheckValue ) || \ - ( pulStack[ 2 ] != ulCheckValue ) || \ - ( pulStack[ 3 ] != ulCheckValue ) ) \ - { \ - char * pcOverflowTaskName = pxCurrentTCB->pcTaskName; \ - vApplicationStackOverflowHook( ( TaskHandle_t ) pxCurrentTCB, pcOverflowTaskName ); \ - } \ + #define taskCHECK_FOR_STACK_OVERFLOW() \ + do { \ + TCB_t * const pxConstCurrentTCB = prvGetCurrentTaskTCBUnsafe(); \ + const uint32_t * const pulStack = ( uint32_t * ) pxConstCurrentTCB->pxStack; \ + const uint32_t ulCheckValue = ( uint32_t ) 0xa5a5a5a5U; \ + \ + if( ( pulStack[ 0 ] != ulCheckValue ) || \ + ( pulStack[ 1 ] != ulCheckValue ) || \ + ( pulStack[ 2 ] != ulCheckValue ) || \ + ( pulStack[ 3 ] != ulCheckValue ) ) \ + { \ + char * pcOverflowTaskName = pxConstCurrentTCB->pcTaskName; \ + vApplicationStackOverflowHook( ( TaskHandle_t ) pxConstCurrentTCB, pcOverflowTaskName ); \ + } \ } while( 0 ) #endif /* #if( configCHECK_FOR_STACK_OVERFLOW > 1 ) */ @@ -110,7 +114,8 @@ #define taskCHECK_FOR_STACK_OVERFLOW() \ do { \ - int8_t * pcEndOfStack = ( int8_t * ) pxCurrentTCB->pxEndOfStack; \ + TCB_t * const pxConstCurrentTCB = prvGetCurrentTaskTCBUnsafe(); \ + int8_t * pcEndOfStack = ( int8_t * ) pxConstCurrentTCB->pxEndOfStack; \ static const uint8_t ucExpectedStackBytes[] = { tskSTACK_FILL_BYTE, tskSTACK_FILL_BYTE, tskSTACK_FILL_BYTE, tskSTACK_FILL_BYTE, \ tskSTACK_FILL_BYTE, tskSTACK_FILL_BYTE, tskSTACK_FILL_BYTE, tskSTACK_FILL_BYTE, \ tskSTACK_FILL_BYTE, tskSTACK_FILL_BYTE, tskSTACK_FILL_BYTE, tskSTACK_FILL_BYTE, \ @@ -123,8 +128,8 @@ /* Has the extremity of the task stack ever been written over? */ \ if( memcmp( ( void * ) pcEndOfStack, ( void * ) ucExpectedStackBytes, sizeof( ucExpectedStackBytes ) ) != 0 ) \ { \ - char * pcOverflowTaskName = pxCurrentTCB->pcTaskName; \ - vApplicationStackOverflowHook( ( TaskHandle_t ) pxCurrentTCB, pcOverflowTaskName ); \ + char * pcOverflowTaskName = pxConstCurrentTCB->pcTaskName; \ + vApplicationStackOverflowHook( ( TaskHandle_t ) pxConstCurrentTCB, pcOverflowTaskName ); \ } \ } while( 0 ) diff --git a/tasks.c b/tasks.c index 2eb6929e3b..4acb82aff0 100644 --- a/tasks.c +++ b/tasks.c @@ -463,7 +463,6 @@ typedef tskTCB TCB_t; /* More details at: https://github.com/FreeRTOS/FreeRTOS-Kernel/blob/main/MISRA.md#rule-84 */ /* coverity[misra_c_2012_rule_8_4_violation] */ portDONT_DISCARD PRIVILEGED_DATA TCB_t * volatile pxCurrentTCBs[ configNUMBER_OF_CORES ]; - #define pxCurrentTCB prvGetCurrentTaskTCBUnsafe() #endif /* Lists for ready and blocked tasks. -------------------- From 2883653d9acc6841039bf172258ac4324a9dc911 Mon Sep 17 00:00:00 2001 From: "Ching-Hsin,Lee" Date: Tue, 21 May 2024 23:50:33 +0800 Subject: [PATCH 10/25] Update for performance --- tasks.c | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/tasks.c b/tasks.c index 4acb82aff0..670b4db377 100644 --- a/tasks.c +++ b/tasks.c @@ -4725,10 +4725,6 @@ BaseType_t xTaskIncrementTick( void ) TickType_t xItemValue; BaseType_t xSwitchRequired = pdFALSE; - #if ( configNUMBER_OF_CORES == 1 ) - TCB_t * const pxConstCurrentTCB = prvGetCurrentTaskTCBUnsafe(); - #endif /* #if ( configNUMBER_OF_CORES == 1 ) */ - #if ( configUSE_PREEMPTION == 1 ) && ( configNUMBER_OF_CORES > 1 ) BaseType_t xYieldRequiredForCore[ configNUMBER_OF_CORES ] = { pdFALSE }; #endif /* #if ( configUSE_PREEMPTION == 1 ) && ( configNUMBER_OF_CORES > 1 ) */ @@ -4832,6 +4828,8 @@ BaseType_t xTaskIncrementTick( void ) { #if ( configNUMBER_OF_CORES == 1 ) { + TCB_t * const pxConstCurrentTCB = prvGetCurrentTaskTCBUnsafe(); + /* Preemption is on, but a context switch should * only be performed if the unblocked task's * priority is higher than the currently executing @@ -4867,6 +4865,8 @@ BaseType_t xTaskIncrementTick( void ) { #if ( configNUMBER_OF_CORES == 1 ) { + TCB_t * const pxConstCurrentTCB = prvGetCurrentTaskTCBUnsafe(); + if( listCURRENT_LIST_LENGTH( &( pxReadyTasksLists[ pxConstCurrentTCB->uxPriority ] ) ) > 1U ) { xSwitchRequired = pdTRUE; From f08c3c5e8ce6a426dc77485311b664cba945d66e Mon Sep 17 00:00:00 2001 From: "Ching-Hsin,Lee" Date: Mon, 8 Jul 2024 09:48:09 +0800 Subject: [PATCH 11/25] Use unsafe get in critical section directory --- tasks.c | 21 +++++++++++---------- 1 file changed, 11 insertions(+), 10 deletions(-) diff --git a/tasks.c b/tasks.c index 670b4db377..60ad2d31cc 100644 --- a/tasks.c +++ b/tasks.c @@ -188,8 +188,8 @@ } \ \ /* listGET_OWNER_OF_NEXT_ENTRY indexes through the list, so the tasks of \ - * the same priority get an equal share of the processor time. */ \ - listGET_OWNER_OF_NEXT_ENTRY( pxCurrentTCB, &( pxReadyTasksLists[ uxTopPriority ] ) ); \ + * the same priority get an equal share of the processor time. */ \ + listGET_OWNER_OF_NEXT_ENTRY( prvGetCurrentTaskTCBUnsafe(), &( pxReadyTasksLists[ uxTopPriority ] ) ); \ uxTopReadyPriority = uxTopPriority; \ } while( 0 ) /* taskSELECT_HIGHEST_PRIORITY_TASK */ #else /* if ( configNUMBER_OF_CORES == 1 ) */ @@ -224,7 +224,7 @@ /* Find the highest priority list that contains ready tasks. */ \ portGET_HIGHEST_PRIORITY( uxTopPriority, uxTopReadyPriority ); \ configASSERT( listCURRENT_LIST_LENGTH( &( pxReadyTasksLists[ uxTopPriority ] ) ) > 0 ); \ - listGET_OWNER_OF_NEXT_ENTRY( pxCurrentTCB, &( pxReadyTasksLists[ uxTopPriority ] ) ); \ + listGET_OWNER_OF_NEXT_ENTRY( prvGetCurrentTaskTCBUnsafe(), &( pxReadyTasksLists[ uxTopPriority ] ) ); \ } while( 0 ) /*-----------------------------------------------------------*/ @@ -307,8 +307,8 @@ /* Returns pdTRUE if the task is actively running and not scheduled to yield. */ #if ( configNUMBER_OF_CORES == 1 ) - #define taskTASK_IS_RUNNING( pxTCB ) ( ( ( pxTCB ) == pxCurrentTCB ) ? ( pdTRUE ) : ( pdFALSE ) ) - #define taskTASK_IS_RUNNING_OR_SCHEDULED_TO_YIELD( pxTCB ) ( ( ( pxTCB ) == pxCurrentTCB ) ? ( pdTRUE ) : ( pdFALSE ) ) + #define taskTASK_IS_RUNNING( pxTCB ) ( ( ( pxTCB ) == prvGetCurrentTaskTCBUnsafe() ) ? ( pdTRUE ) : ( pdFALSE ) ) + #define taskTASK_IS_RUNNING_OR_SCHEDULED_TO_YIELD( pxTCB ) ( ( ( pxTCB ) == prvGetCurrentTaskTCBUnsafe() ) ? ( pdTRUE ) : ( pdFALSE ) ) #else #define taskTASK_IS_RUNNING( pxTCB ) ( ( ( ( pxTCB )->xTaskRunState >= ( BaseType_t ) 0 ) && ( ( pxTCB )->xTaskRunState < ( BaseType_t ) configNUMBER_OF_CORES ) ) ? ( pdTRUE ) : ( pdFALSE ) ) #define taskTASK_IS_RUNNING_OR_SCHEDULED_TO_YIELD( pxTCB ) ( ( ( pxTCB )->xTaskRunState != taskTASK_NOT_RUNNING ) ? ( pdTRUE ) : ( pdFALSE ) ) @@ -2066,11 +2066,12 @@ static void prvInitialiseNewTask( TaskFunction_t pxTaskCode, { uxCurrentNumberOfTasks = ( UBaseType_t ) ( uxCurrentNumberOfTasks + 1U ); - if( pxCurrentTCB == NULL ) + /* Invoking a functon in a condition here for performance consideration. */ + if( prvGetCurrentTaskTCBUnsafe() == NULL ) { /* There are no other tasks, or all the other tasks are in * the suspended state - make this the current task. */ - pxCurrentTCB = pxNewTCB; + prvSetCurrentTCB( pxNewTCB ); if( uxCurrentNumberOfTasks == ( UBaseType_t ) 1 ) { @@ -3301,7 +3302,7 @@ static void prvInitialiseNewTask( TaskFunction_t pxTaskCode, * NULL so when the next task is created pxCurrentTCB will * be set to point to it no matter what its relative priority * is. */ - pxCurrentTCB = NULL; + prvSetCurrentTCB( NULL ); } else { @@ -4129,7 +4130,7 @@ BaseType_t xTaskResumeAll( void ) #if ( configNUMBER_OF_CORES == 1 ) { - taskYIELD_TASK_CORE_IF_USING_PREEMPTION( pxCurrentTCB ); + taskYIELD_TASK_CORE_IF_USING_PREEMPTION( prvGetCurrentTaskTCBUnsafe() ); } #endif /* #if ( configNUMBER_OF_CORES == 1 ) */ } @@ -8704,7 +8705,7 @@ void vTaskResetState( void ) /* Task control block. */ #if ( configNUMBER_OF_CORES == 1 ) { - pxCurrentTCB = NULL; + prvSetCurrentTCB( NULL ); } #endif /* #if ( configNUMBER_OF_CORES == 1 ) */ From 67b657463359e14f66f3ec41af511872d255ad03 Mon Sep 17 00:00:00 2001 From: "Ching-Hsin,Lee" Date: Mon, 8 Jul 2024 10:14:45 +0800 Subject: [PATCH 12/25] Update pxCurrentTCBs[ portGET_CORE_ID() ] optimization --- tasks.c | 87 ++++++++++++++++++++++++++++----------------------------- 1 file changed, 42 insertions(+), 45 deletions(-) diff --git a/tasks.c b/tasks.c index 6aa1504e8c..36eeddbc47 100644 --- a/tasks.c +++ b/tasks.c @@ -84,17 +84,16 @@ portYIELD_WITHIN_API(); \ } while( 0 ) - #define taskYIELD_ANY_CORE_IF_USING_PREEMPTION( pxTCB ) \ - do { \ - TCB_t * const pxConstCurrentTCB = prvGetCurrentTaskTCB(); \ - if( pxConstCurrentTCB->uxPriority < ( pxTCB )->uxPriority ) \ - { \ - portYIELD_WITHIN_API(); \ - } \ - else \ - { \ - mtCOVERAGE_TEST_MARKER(); \ - } \ + #define taskYIELD_ANY_CORE_IF_USING_PREEMPTION( pxTCB ) \ + do { \ + if( ( prvGetCurrentTaskTCBUnsafe()->uxPriority ) < ( pxTCB )->uxPriority ) \ + { \ + portYIELD_WITHIN_API(); \ + } \ + else \ + { \ + mtCOVERAGE_TEST_MARKER(); \ + } \ } while( 0 ) #else /* if ( configNUMBER_OF_CORES == 1 ) */ @@ -188,9 +187,9 @@ } \ \ /* listGET_OWNER_OF_NEXT_ENTRY indexes through the list, so the tasks of \ - * the same priority get an equal share of the processor time. */ \ + * the same priority get an equal share of the processor time. */ \ listGET_OWNER_OF_NEXT_ENTRY( prvGetCurrentTaskTCBUnsafe(), &( pxReadyTasksLists[ uxTopPriority ] ) ); \ - uxTopReadyPriority = uxTopPriority; \ + uxTopReadyPriority = uxTopPriority; \ } while( 0 ) /* taskSELECT_HIGHEST_PRIORITY_TASK */ #else /* if ( configNUMBER_OF_CORES == 1 ) */ @@ -217,14 +216,14 @@ /*-----------------------------------------------------------*/ - #define taskSELECT_HIGHEST_PRIORITY_TASK() \ - do { \ - UBaseType_t uxTopPriority; \ - \ - /* Find the highest priority list that contains ready tasks. */ \ - portGET_HIGHEST_PRIORITY( uxTopPriority, uxTopReadyPriority ); \ - configASSERT( listCURRENT_LIST_LENGTH( &( pxReadyTasksLists[ uxTopPriority ] ) ) > 0 ); \ - listGET_OWNER_OF_NEXT_ENTRY( prvGetCurrentTaskTCBUnsafe(), &( pxReadyTasksLists[ uxTopPriority ] ) ); \ + #define taskSELECT_HIGHEST_PRIORITY_TASK() \ + do { \ + UBaseType_t uxTopPriority; \ + \ + /* Find the highest priority list that contains ready tasks. */ \ + portGET_HIGHEST_PRIORITY( uxTopPriority, uxTopReadyPriority ); \ + configASSERT( listCURRENT_LIST_LENGTH( &( pxReadyTasksLists[ uxTopPriority ] ) ) > 0 ); \ + listGET_OWNER_OF_NEXT_ENTRY( prvGetCurrentTaskTCBUnsafe(), &( pxReadyTasksLists[ uxTopPriority ] ) ); \ } while( 0 ) /*-----------------------------------------------------------*/ @@ -318,10 +317,10 @@ #define taskATTRIBUTE_IS_IDLE ( UBaseType_t ) ( 1U << 0U ) #if ( ( configNUMBER_OF_CORES > 1 ) && ( portCRITICAL_NESTING_IN_TCB == 1 ) ) - #define portGET_CRITICAL_NESTING_COUNT() ( pxCurrentTCBs[ portGET_CORE_ID() ]->uxCriticalNesting ) - #define portSET_CRITICAL_NESTING_COUNT( x ) ( pxCurrentTCBs[ portGET_CORE_ID() ]->uxCriticalNesting = ( x ) ) - #define portINCREMENT_CRITICAL_NESTING_COUNT() ( pxCurrentTCBs[ portGET_CORE_ID() ]->uxCriticalNesting++ ) - #define portDECREMENT_CRITICAL_NESTING_COUNT() ( pxCurrentTCBs[ portGET_CORE_ID() ]->uxCriticalNesting-- ) + #define portGET_CRITICAL_NESTING_COUNT() ( prvGetCurrentTaskTCBUnsafe()->uxCriticalNesting ) + #define portSET_CRITICAL_NESTING_COUNT( x ) ( prvGetCurrentTaskTCBUnsafe()->uxCriticalNesting = ( x ) ) + #define portINCREMENT_CRITICAL_NESTING_COUNT() ( prvGetCurrentTaskTCBUnsafe()->uxCriticalNesting++ ) + #define portDECREMENT_CRITICAL_NESTING_COUNT() ( prvGetCurrentTaskTCBUnsafe()->uxCriticalNesting-- ) #endif /* #if ( ( configNUMBER_OF_CORES > 1 ) && ( portCRITICAL_NESTING_IN_TCB == 1 ) ) */ #define taskBITS_PER_BYTE ( ( size_t ) 8 ) @@ -833,7 +832,7 @@ static void prvAddNewTaskToReadyList( TCB_t * pxNewTCB ) PRIVILEGED_FUNCTION; uxSavedInterruptStatus = portSET_INTERRUPT_MASK(); { - pxTCB = pxCurrentTCBs[ portGET_CORE_ID() ]; + pxTCB = prvGetCurrentTaskTCBUnsafe(); } portCLEAR_INTERRUPT_MASK( uxSavedInterruptStatus ); @@ -854,7 +853,7 @@ static void prvAddNewTaskToReadyList( TCB_t * pxNewTCB ) PRIVILEGED_FUNCTION; /* This function is always called with interrupts disabled * so this is safe. */ - pxThisTCB = pxCurrentTCBs[ portGET_CORE_ID() ]; + pxThisTCB = prvGetCurrentTaskTCBUnsafe(); while( pxThisTCB->xTaskRunState == taskTASK_SCHEDULED_TO_YIELD ) { @@ -918,13 +917,13 @@ static void prvAddNewTaskToReadyList( TCB_t * pxNewTCB ) PRIVILEGED_FUNCTION; #if ( configRUN_MULTIPLE_PRIORITIES == 0 ) BaseType_t xYieldCount = 0; + TCB_t * const pxConstCurrentTCB = prvGetCurrentTaskTCBUnsafe(); #endif /* #if ( configRUN_MULTIPLE_PRIORITIES == 0 ) */ /* This must be called from a critical section. */ configASSERT( portGET_CRITICAL_NESTING_COUNT() > 0U ); #if ( configRUN_MULTIPLE_PRIORITIES == 0 ) - /* No task should yield for this one if it is a lower priority * than priority level of currently ready tasks. */ if( pxTCB->uxPriority >= uxTopReadyPriority ) @@ -1010,11 +1009,11 @@ static void prvAddNewTaskToReadyList( TCB_t * pxNewTCB ) PRIVILEGED_FUNCTION; #if ( configRUN_MULTIPLE_PRIORITIES == 0 ) /* Verify that the calling core always yields to higher priority tasks. */ - if( ( ( pxCurrentTCBs[ portGET_CORE_ID() ]->uxTaskAttributes & taskATTRIBUTE_IS_IDLE ) == 0U ) && - ( pxTCB->uxPriority > pxCurrentTCBs[ portGET_CORE_ID() ]->uxPriority ) ) + if( ( ( pxConstCurrentTCB->uxTaskAttributes & taskATTRIBUTE_IS_IDLE ) == 0U ) && + ( pxTCB->uxPriority > pxConstCurrentTCB->uxPriority ) ) { configASSERT( ( xYieldPendings[ portGET_CORE_ID() ] == pdTRUE ) || - ( taskTASK_IS_RUNNING( pxCurrentTCBs[ portGET_CORE_ID() ] ) == pdFALSE ) ); + ( taskTASK_IS_RUNNING( pxConstCurrentTCB ) == pdFALSE ) ); } #endif } @@ -2121,19 +2120,19 @@ static void prvInitialiseNewTask( TaskFunction_t pxTaskCode, prvAddTaskToReadyList( pxNewTCB ); portSETUP_TCB( pxNewTCB ); - } - taskEXIT_CRITICAL(); - if( xSchedulerRunning != pdFALSE ) - { - /* If the created task is of a higher priority than the current task - * then it should run now. */ - taskYIELD_ANY_CORE_IF_USING_PREEMPTION( pxNewTCB ); - } - else - { - mtCOVERAGE_TEST_MARKER(); + if( xSchedulerRunning != pdFALSE ) + { + /* If the created task is of a higher priority than the current task + * then it should run now. */ + taskYIELD_ANY_CORE_IF_USING_PREEMPTION( pxNewTCB ); + } + else + { + mtCOVERAGE_TEST_MARKER(); + } } + taskEXIT_CRITICAL(); } #else /* #if ( configNUMBER_OF_CORES == 1 ) */ @@ -3406,12 +3405,10 @@ static void prvInitialiseNewTask( TaskFunction_t pxTaskCode, configASSERT( xTaskToResume ); #if ( configNUMBER_OF_CORES == 1 ) - /* The parameter cannot be NULL as it is impossible to resume the * currently executing task. */ if( ( taskTASK_IS_RUNNING( pxTCB ) == pdFALSE ) && ( pxTCB != NULL ) ) #else - /* The parameter cannot be NULL as it is impossible to resume the * currently executing task. It is also impossible to resume a task * that is actively running on another core but it is not safe @@ -5260,7 +5257,7 @@ BaseType_t xTaskIncrementTick( void ) /* Macro to inject port specific behaviour immediately after * switching tasks, such as setting an end of stack watchpoint * or reconfiguring the MPU. */ - portTASK_SWITCH_HOOK( pxCurrentTCBs[ portGET_CORE_ID() ] ); + portTASK_SWITCH_HOOK( prvGetCurrentTaskTCBUnsafe() ); /* After the new task is switched in, update the global errno. */ #if ( configUSE_POSIX_ERRNO == 1 ) From 4ec5b9d94d7f5ccbbb9edf0c907f39c331272979 Mon Sep 17 00:00:00 2001 From: "Ching-Hsin,Lee" Date: Mon, 8 Jul 2024 10:46:26 +0800 Subject: [PATCH 13/25] Update set current TCB --- tasks.c | 38 ++++++++++++++++++++++++-------------- 1 file changed, 24 insertions(+), 14 deletions(-) diff --git a/tasks.c b/tasks.c index 36eeddbc47..6dcd81dd98 100644 --- a/tasks.c +++ b/tasks.c @@ -448,9 +448,19 @@ typedef tskTCB TCB_t; #if ( configNUMBER_OF_CORES == 1 ) #define prvGetCurrentTaskTCBUnsafe() pxCurrentTCB #define prvGetCurrentTaskTCB() pxCurrentTCB + #define prvSetCurrentTaskTCBUnsafe( pxTCB ) \ + do{ \ + pxCurrentTCB = pxTCB; \ + } while( 0 ) #else #define prvGetCurrentTaskTCBUnsafe() pxCurrentTCBs[ portGET_CORE_ID() ] -#endif + #define prvSetCurrentTaskTCBUnsafe( pxTCB ) \ + do{ \ + pxCurrentTCBs[ portGET_CORE_ID() ] = pxTCB; \ + } while( 0 ) +#endif /* if ( configNUMBER_OF_CORES == 1 ) */ + + #if ( configNUMBER_OF_CORES == 1 ) /* MISRA Ref 8.4.1 [Declaration shall be visible] */ @@ -940,15 +950,17 @@ static void prvAddNewTaskToReadyList( TCB_t * pxNewTCB ) PRIVILEGED_FUNCTION; for( xCoreID = ( BaseType_t ) 0; xCoreID < ( BaseType_t ) configNUMBER_OF_CORES; xCoreID++ ) { - xCurrentCoreTaskPriority = ( BaseType_t ) pxCurrentTCBs[ xCoreID ]->uxPriority; + TCB_t * const pxConstCurrentTCB = pxCurrentTCBs[ xCoreID ]; + + xCurrentCoreTaskPriority = ( BaseType_t ) pxConstCurrentTCB->uxPriority; /* System idle tasks are being assigned a priority of tskIDLE_PRIORITY - 1 here. */ - if( ( pxCurrentTCBs[ xCoreID ]->uxTaskAttributes & taskATTRIBUTE_IS_IDLE ) != 0U ) + if( ( pxConstCurrentTCB->uxTaskAttributes & taskATTRIBUTE_IS_IDLE ) != 0U ) { xCurrentCoreTaskPriority = ( BaseType_t ) ( xCurrentCoreTaskPriority - 1 ); } - if( ( taskTASK_IS_RUNNING( pxCurrentTCBs[ xCoreID ] ) != pdFALSE ) && ( xYieldPendings[ xCoreID ] == pdFALSE ) ) + if( ( taskTASK_IS_RUNNING( pxConstCurrentTCB ) != pdFALSE ) && ( xYieldPendings[ xCoreID ] == pdFALSE ) ) { #if ( configRUN_MULTIPLE_PRIORITIES == 0 ) if( taskTASK_IS_RUNNING( pxTCB ) == pdFALSE ) @@ -961,7 +973,7 @@ static void prvAddNewTaskToReadyList( TCB_t * pxNewTCB ) PRIVILEGED_FUNCTION; #endif { #if ( configUSE_TASK_PREEMPTION_DISABLE == 1 ) - if( pxCurrentTCBs[ xCoreID ]->xPreemptionDisable == pdFALSE ) + if( pxConstCurrentTCB->xPreemptionDisable == pdFALSE ) #endif { xLowestPriorityToPreempt = xCurrentCoreTaskPriority; @@ -2071,7 +2083,7 @@ static void prvInitialiseNewTask( TaskFunction_t pxTaskCode, { /* There are no other tasks, or all the other tasks are in * the suspended state - make this the current task. */ - prvSetCurrentTCB( pxNewTCB ); + prvGetCurrentTaskTCBUnsafe( pxNewTCB ); if( uxCurrentNumberOfTasks == ( UBaseType_t ) 1 ) { @@ -2094,7 +2106,7 @@ static void prvInitialiseNewTask( TaskFunction_t pxTaskCode, { if( pxCurrentTCB->uxPriority <= pxNewTCB->uxPriority ) { - pxCurrentTCB = pxNewTCB; + prvSetCurrentTaskTCBUnsafe( pxNewTCB ); } else { @@ -2856,7 +2868,7 @@ static void prvInitialiseNewTask( TaskFunction_t pxTaskCode, { #if ( configNUMBER_OF_CORES == 1 ) { - TCB_t * const pxConstCurrentTCB = prvGetCurrentTaskTCB(); + TCB_t * const pxConstCurrentTCB = prvGetCurrentTaskTCBUnsafe(); if( taskTASK_IS_RUNNING( pxTCB ) == pdFALSE ) { @@ -3302,7 +3314,7 @@ static void prvInitialiseNewTask( TaskFunction_t pxTaskCode, * NULL so when the next task is created pxCurrentTCB will * be set to point to it no matter what its relative priority * is. */ - prvSetCurrentTCB( NULL ); + prvGetCurrentTaskTCBUnsafe( NULL ); } else { @@ -3760,7 +3772,7 @@ void vTaskStartScheduler( void ) /* Switch C-Runtime's TLS Block to point to the TLS * block specific to the task that will run first. */ - configSET_TLS_BLOCK( pxConstCurrentTCB->xTLSBlock ); + configSET_TLS_BLOCK( prvGetCurrentTaskTCBUnsafe()->xTLSBlock ); } #endif @@ -4866,9 +4878,7 @@ BaseType_t xTaskIncrementTick( void ) { #if ( configNUMBER_OF_CORES == 1 ) { - TCB_t * const pxConstCurrentTCB = prvGetCurrentTaskTCBUnsafe(); - - if( listCURRENT_LIST_LENGTH( &( pxReadyTasksLists[ pxConstCurrentTCB->uxPriority ] ) ) > 1U ) + if( listCURRENT_LIST_LENGTH( &( pxReadyTasksLists[ prvGetCurrentTaskTCBUnsafe()->uxPriority ] ) ) > 1U ) { xSwitchRequired = pdTRUE; } @@ -8705,7 +8715,7 @@ void vTaskResetState( void ) /* Task control block. */ #if ( configNUMBER_OF_CORES == 1 ) { - prvSetCurrentTCB( NULL ); + prvGetCurrentTaskTCBUnsafe( NULL ); } #endif /* #if ( configNUMBER_OF_CORES == 1 ) */ From cb0b1ed882cec67c113ca37fbe38375e3380a1f7 Mon Sep 17 00:00:00 2001 From: "Ching-Hsin,Lee" Date: Mon, 8 Jul 2024 10:50:07 +0800 Subject: [PATCH 14/25] code refine --- tasks.c | 46 +++++++++++++++++++++++----------------------- 1 file changed, 23 insertions(+), 23 deletions(-) diff --git a/tasks.c b/tasks.c index 6dcd81dd98..0d7acc74f4 100644 --- a/tasks.c +++ b/tasks.c @@ -348,6 +348,29 @@ } \ } while( 0 ) #endif /* #if ( configNUMBER_OF_CORES > 1 ) */ + +#if ( configNUMBER_OF_CORES == 1 ) + #define prvGetCurrentTaskTCB() pxCurrentTCB + #define prvGetCurrentTaskTCBUnsafe() pxCurrentTCB + #define prvSetCurrentTaskTCBUnsafe( pxTCB ) \ + do{ \ + pxCurrentTCB = pxTCB; \ + } while( 0 ) +#else + +/* Retrieving the current task TCB in a multi-core environment involves two steps: + * 1. Obtaining the core ID. + * 2. Using the core ID to index the pxCurrentTCBs array. + * If these two steps are not performed atomically, a race condition may occur. + * To ensure atomicity, prvGetCurrentTaskTCBUnsafe() should be called in a context where + * the core executing the task remains fixed until the operation is completed. */ + #define prvGetCurrentTaskTCBUnsafe() pxCurrentTCBs[ portGET_CORE_ID() ] + #define prvSetCurrentTaskTCBUnsafe( pxTCB ) \ + do{ \ + pxCurrentTCBs[ portGET_CORE_ID() ] = pxTCB; \ + } while( 0 ) +#endif /* if ( configNUMBER_OF_CORES == 1 ) */ + /*-----------------------------------------------------------*/ /* @@ -439,29 +462,6 @@ typedef struct tskTaskControlBlock /* The old naming convention is used to * below to enable the use of older kernel aware debuggers. */ typedef tskTCB TCB_t; -/* Retrieving the current task TCB in a multi-core environment involves two steps: - * 1. Obtaining the core ID. - * 2. Using the core ID to index the pxCurrentTCBs array. - * If these two steps are not performed atomically, a race condition may occur. - * To ensure atomicity, prvGetCurrentTaskTCBUnsafe() should be called in a context where - * the core executing the task remains fixed until the operation is completed. */ -#if ( configNUMBER_OF_CORES == 1 ) - #define prvGetCurrentTaskTCBUnsafe() pxCurrentTCB - #define prvGetCurrentTaskTCB() pxCurrentTCB - #define prvSetCurrentTaskTCBUnsafe( pxTCB ) \ - do{ \ - pxCurrentTCB = pxTCB; \ - } while( 0 ) -#else - #define prvGetCurrentTaskTCBUnsafe() pxCurrentTCBs[ portGET_CORE_ID() ] - #define prvSetCurrentTaskTCBUnsafe( pxTCB ) \ - do{ \ - pxCurrentTCBs[ portGET_CORE_ID() ] = pxTCB; \ - } while( 0 ) -#endif /* if ( configNUMBER_OF_CORES == 1 ) */ - - - #if ( configNUMBER_OF_CORES == 1 ) /* MISRA Ref 8.4.1 [Declaration shall be visible] */ /* More details at: https://github.com/FreeRTOS/FreeRTOS-Kernel/blob/main/MISRA.md#rule-84 */ From 9362ac47c17fb48f7b9e834ec282ed111888f3bf Mon Sep 17 00:00:00 2001 From: "Ching-Hsin,Lee" Date: Mon, 8 Jul 2024 11:16:26 +0800 Subject: [PATCH 15/25] Remove set current TCB --- tasks.c | 19 +++++++------------ 1 file changed, 7 insertions(+), 12 deletions(-) diff --git a/tasks.c b/tasks.c index 0d7acc74f4..f6937d20ce 100644 --- a/tasks.c +++ b/tasks.c @@ -352,10 +352,6 @@ #if ( configNUMBER_OF_CORES == 1 ) #define prvGetCurrentTaskTCB() pxCurrentTCB #define prvGetCurrentTaskTCBUnsafe() pxCurrentTCB - #define prvSetCurrentTaskTCBUnsafe( pxTCB ) \ - do{ \ - pxCurrentTCB = pxTCB; \ - } while( 0 ) #else /* Retrieving the current task TCB in a multi-core environment involves two steps: @@ -365,10 +361,6 @@ * To ensure atomicity, prvGetCurrentTaskTCBUnsafe() should be called in a context where * the core executing the task remains fixed until the operation is completed. */ #define prvGetCurrentTaskTCBUnsafe() pxCurrentTCBs[ portGET_CORE_ID() ] - #define prvSetCurrentTaskTCBUnsafe( pxTCB ) \ - do{ \ - pxCurrentTCBs[ portGET_CORE_ID() ] = pxTCB; \ - } while( 0 ) #endif /* if ( configNUMBER_OF_CORES == 1 ) */ /*-----------------------------------------------------------*/ @@ -927,7 +919,6 @@ static void prvAddNewTaskToReadyList( TCB_t * pxNewTCB ) PRIVILEGED_FUNCTION; #if ( configRUN_MULTIPLE_PRIORITIES == 0 ) BaseType_t xYieldCount = 0; - TCB_t * const pxConstCurrentTCB = prvGetCurrentTaskTCBUnsafe(); #endif /* #if ( configRUN_MULTIPLE_PRIORITIES == 0 ) */ /* This must be called from a critical section. */ @@ -1020,13 +1011,17 @@ static void prvAddNewTaskToReadyList( TCB_t * pxNewTCB ) PRIVILEGED_FUNCTION; } #if ( configRUN_MULTIPLE_PRIORITIES == 0 ) + { + TCB_t * const pxConstCurrentTCB = prvGetCurrentTaskTCBUnsafe(); + /* Verify that the calling core always yields to higher priority tasks. */ - if( ( ( pxConstCurrentTCB->uxTaskAttributes & taskATTRIBUTE_IS_IDLE ) == 0U ) && + if( ( ( prvGetCurrentTaskTCBUnsafe->uxTaskAttributes & taskATTRIBUTE_IS_IDLE ) == 0U ) && ( pxTCB->uxPriority > pxConstCurrentTCB->uxPriority ) ) { configASSERT( ( xYieldPendings[ portGET_CORE_ID() ] == pdTRUE ) || ( taskTASK_IS_RUNNING( pxConstCurrentTCB ) == pdFALSE ) ); } + } #endif } } @@ -2083,7 +2078,7 @@ static void prvInitialiseNewTask( TaskFunction_t pxTaskCode, { /* There are no other tasks, or all the other tasks are in * the suspended state - make this the current task. */ - prvGetCurrentTaskTCBUnsafe( pxNewTCB ); + pxCurrentTCB = pxNewTCB; if( uxCurrentNumberOfTasks == ( UBaseType_t ) 1 ) { @@ -2106,7 +2101,7 @@ static void prvInitialiseNewTask( TaskFunction_t pxTaskCode, { if( pxCurrentTCB->uxPriority <= pxNewTCB->uxPriority ) { - prvSetCurrentTaskTCBUnsafe( pxNewTCB ); + pxCurrentTCB = pxTCB; } else { From 0fcb04a41d9131280164c80d220c1a5b940cea25 Mon Sep 17 00:00:00 2001 From: "Ching-Hsin,Lee" Date: Mon, 8 Jul 2024 12:26:26 +0800 Subject: [PATCH 16/25] Refine usage --- tasks.c | 46 +++++++++++++--------------------------------- 1 file changed, 13 insertions(+), 33 deletions(-) diff --git a/tasks.c b/tasks.c index f6937d20ce..99df914d5f 100644 --- a/tasks.c +++ b/tasks.c @@ -1022,7 +1022,7 @@ static void prvAddNewTaskToReadyList( TCB_t * pxNewTCB ) PRIVILEGED_FUNCTION; ( taskTASK_IS_RUNNING( pxConstCurrentTCB ) == pdFALSE ) ); } } - #endif + #endif /* if ( configRUN_MULTIPLE_PRIORITIES == 0 ) */ } } #endif /* #if ( configNUMBER_OF_CORES > 1 ) */ @@ -2863,14 +2863,12 @@ static void prvInitialiseNewTask( TaskFunction_t pxTaskCode, { #if ( configNUMBER_OF_CORES == 1 ) { - TCB_t * const pxConstCurrentTCB = prvGetCurrentTaskTCBUnsafe(); - if( taskTASK_IS_RUNNING( pxTCB ) == pdFALSE ) { /* The priority of a task other than the currently * running task is being raised. Is the priority being * raised above that of the running task? */ - if( uxNewPriority > pxConstCurrentTCB->uxPriority ) + if( uxNewPriority > prvGetCurrentTaskTCBUnsafe()->uxPriority ) { xYieldRequired = pdTRUE; } @@ -3309,7 +3307,7 @@ static void prvInitialiseNewTask( TaskFunction_t pxTaskCode, * NULL so when the next task is created pxCurrentTCB will * be set to point to it no matter what its relative priority * is. */ - prvGetCurrentTaskTCBUnsafe( NULL ); + pxCurrentTCB = NULL; } else { @@ -3503,11 +3501,9 @@ static void prvInitialiseNewTask( TaskFunction_t pxTaskCode, { #if ( configNUMBER_OF_CORES == 1 ) { - TCB_t * const pxConstCurrentTCB = prvGetCurrentTaskTCBUnsafe(); - /* Ready lists can be accessed so move the task from the * suspended list to the ready list directly. */ - if( pxTCB->uxPriority > pxConstCurrentTCB->uxPriority ) + if( pxTCB->uxPriority > prvGetCurrentTaskTCBUnsafe()->uxPriority ) { xYieldRequired = pdTRUE; @@ -3763,8 +3759,6 @@ void vTaskStartScheduler( void ) #if ( configUSE_C_RUNTIME_TLS_SUPPORT == 1 ) { - TCB_t * const pxConstCurrentTCB = prvGetCurrentTaskTCB(); - /* Switch C-Runtime's TLS Block to point to the TLS * block specific to the task that will run first. */ configSET_TLS_BLOCK( prvGetCurrentTaskTCBUnsafe()->xTLSBlock ); @@ -4028,7 +4022,7 @@ BaseType_t xTaskResumeAll( void ) * tasks from this list into their appropriate ready list. */ taskENTER_CRITICAL(); { - BaseType_t xCoreID = portGET_CORE_ID(); + BaseType_t xCoreID = ( BaseType_t ) portGET_CORE_ID(); /* If uxSchedulerSuspended is zero then this function does not match a * previous call to vTaskSuspendAll(). */ @@ -4056,11 +4050,9 @@ BaseType_t xTaskResumeAll( void ) #if ( configNUMBER_OF_CORES == 1 ) { - TCB_t * const pxConstCurrentTCB = prvGetCurrentTaskTCBUnsafe(); - /* If the moved task has a priority higher than the current * task then a yield must be performed. */ - if( pxTCB->uxPriority > pxConstCurrentTCB->uxPriority ) + if( pxTCB->uxPriority > prvGetCurrentTaskTCBUnsafe()->uxPriority ) { xYieldPendings[ xCoreID ] = pdTRUE; } @@ -4684,12 +4676,10 @@ BaseType_t xTaskCatchUpTicks( TickType_t xTicksToCatchUp ) { #if ( configNUMBER_OF_CORES == 1 ) { - TCB_t * const pxConstCurrentTCB = prvGetCurrentTaskTCB(); - /* Preemption is on, but a context switch should only be * performed if the unblocked task has a priority that is * higher than the currently executing task. */ - if( pxTCB->uxPriority > pxConstCurrentTCB->uxPriority ) + if( pxTCB->uxPriority > prvGetCurrentTaskTCB()->uxPriority ) { /* Pend the yield to be performed when the scheduler * is unsuspended. */ @@ -5291,8 +5281,6 @@ BaseType_t xTaskIncrementTick( void ) void vTaskPlaceOnEventList( List_t * const pxEventList, const TickType_t xTicksToWait ) { - TCB_t * const pxConstCurrentTCB = prvGetCurrentTaskTCBUnsafe(); - traceENTER_vTaskPlaceOnEventList( pxEventList, xTicksToWait ); configASSERT( pxEventList ); @@ -5311,7 +5299,7 @@ void vTaskPlaceOnEventList( List_t * const pxEventList, * * The queue that contains the event list is locked, preventing * simultaneous access from interrupts. */ - vListInsert( pxEventList, &( pxConstCurrentTCB->xEventListItem ) ); + vListInsert( pxEventList, &( prvGetCurrentTaskTCBUnsafe()->xEventListItem ) ); prvAddCurrentTaskToDelayedList( xTicksToWait, pdTRUE ); @@ -5357,8 +5345,6 @@ void vTaskPlaceOnUnorderedEventList( List_t * pxEventList, TickType_t xTicksToWait, const BaseType_t xWaitIndefinitely ) { - TCB_t * const pxConstCurrentTCB = prvGetCurrentTaskTCBUnsafe(); - traceENTER_vTaskPlaceOnEventListRestricted( pxEventList, xTicksToWait, xWaitIndefinitely ); configASSERT( pxEventList ); @@ -5373,7 +5359,7 @@ void vTaskPlaceOnUnorderedEventList( List_t * pxEventList, * In this case it is assume that this is the only task that is going to * be waiting on this event list, so the faster vListInsertEnd() function * can be used in place of vListInsert. */ - listINSERT_END( pxEventList, &( pxConstCurrentTCB->xEventListItem ) ); + listINSERT_END( pxEventList, &( prvGetCurrentTaskTCBUnsafe()->xEventListItem ) ); /* If the task should block indefinitely then set the block time to a * value that will be recognised as an indefinite delay inside the @@ -5447,9 +5433,7 @@ BaseType_t xTaskRemoveFromEventList( const List_t * const pxEventList ) #if ( configNUMBER_OF_CORES == 1 ) { - TCB_t * const pxConstCurrentTCB = prvGetCurrentTaskTCBUnsafe(); - - if( pxUnblockedTCB->uxPriority > pxConstCurrentTCB->uxPriority ) + if( pxUnblockedTCB->uxPriority > prvGetCurrentTaskTCBUnsafe()->uxPriority ) { /* Return true if the task removed from the event list has a higher * priority than the calling task. This allows the calling task to know if @@ -5532,9 +5516,7 @@ void vTaskRemoveFromUnorderedEventList( ListItem_t * pxEventListItem, #if ( configNUMBER_OF_CORES == 1 ) { - TCB_t * const pxConstCurrentTCB = prvGetCurrentTaskTCBUnsafe(); - - if( pxUnblockedTCB->uxPriority > pxConstCurrentTCB->uxPriority ) + if( pxUnblockedTCB->uxPriority > prvGetCurrentTaskTCBUnsafe()->uxPriority ) { /* The unblocked task has a priority above that of the calling task, so * a context switch is required. This function is called with the @@ -8121,9 +8103,7 @@ TickType_t uxTaskResetEventItemValue( void ) #if ( configNUMBER_OF_CORES == 1 ) { - TCB_t * const pxConstCurrentTCB = prvGetCurrentTaskTCBUnsafe(); - - if( pxTCB->uxPriority > pxConstCurrentTCB->uxPriority ) + if( pxTCB->uxPriority > prvGetCurrentTaskTCBUnsafe()->uxPriority ) { /* The notified task has a priority above the currently * executing task so a yield is required. */ @@ -8710,7 +8690,7 @@ void vTaskResetState( void ) /* Task control block. */ #if ( configNUMBER_OF_CORES == 1 ) { - prvGetCurrentTaskTCBUnsafe( NULL ); + pxCurrentTCB = NULL; } #endif /* #if ( configNUMBER_OF_CORES == 1 ) */ From 98faa785cb257552468683c1887d098f694681f8 Mon Sep 17 00:00:00 2001 From: "Ching-Hsin,Lee" Date: Mon, 8 Jul 2024 12:34:48 +0800 Subject: [PATCH 17/25] Fix typo --- tasks.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tasks.c b/tasks.c index 99df914d5f..5e3f647b93 100644 --- a/tasks.c +++ b/tasks.c @@ -3907,7 +3907,7 @@ void vTaskSuspendAll( void ) portGET_TASK_LOCK(); /* uxSchedulerSuspended is increased after prvCheckForRunStateChange. The - * purpose is to prevent altering the variable when fromISR APIs are readying + * purpose is to prevent altering the variable when fromISR APIs are reading * it. */ if( uxSchedulerSuspended == 0U ) { From 9a097c51dd29f3a7d51da831a15b16029e28b7c8 Mon Sep 17 00:00:00 2001 From: "Ching-Hsin,Lee" Date: Mon, 8 Jul 2024 12:52:16 +0800 Subject: [PATCH 18/25] Fix typo --- tasks.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tasks.c b/tasks.c index 5e3f647b93..4b7fea505b 100644 --- a/tasks.c +++ b/tasks.c @@ -2101,7 +2101,7 @@ static void prvInitialiseNewTask( TaskFunction_t pxTaskCode, { if( pxCurrentTCB->uxPriority <= pxNewTCB->uxPriority ) { - pxCurrentTCB = pxTCB; + pxCurrentTCB = pxNewTCB; } else { From 7a418ceffe72294ab033cc9c26b48221878684b3 Mon Sep 17 00:00:00 2001 From: "Ching-Hsin,Lee" Date: Mon, 8 Jul 2024 14:36:19 +0800 Subject: [PATCH 19/25] Fix format and spelling --- tasks.c | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/tasks.c b/tasks.c index 4b7fea505b..ba8b6c7b50 100644 --- a/tasks.c +++ b/tasks.c @@ -925,6 +925,7 @@ static void prvAddNewTaskToReadyList( TCB_t * pxNewTCB ) PRIVILEGED_FUNCTION; configASSERT( portGET_CRITICAL_NESTING_COUNT() > 0U ); #if ( configRUN_MULTIPLE_PRIORITIES == 0 ) + /* No task should yield for this one if it is a lower priority * than priority level of currently ready tasks. */ if( pxTCB->uxPriority >= uxTopReadyPriority ) @@ -2073,7 +2074,7 @@ static void prvInitialiseNewTask( TaskFunction_t pxTaskCode, { uxCurrentNumberOfTasks = ( UBaseType_t ) ( uxCurrentNumberOfTasks + 1U ); - /* Invoking a functon in a condition here for performance consideration. */ + /* Invoking a function in a condition here for performance consideration. */ if( prvGetCurrentTaskTCBUnsafe() == NULL ) { /* There are no other tasks, or all the other tasks are in @@ -3410,10 +3411,12 @@ static void prvInitialiseNewTask( TaskFunction_t pxTaskCode, configASSERT( xTaskToResume ); #if ( configNUMBER_OF_CORES == 1 ) + /* The parameter cannot be NULL as it is impossible to resume the * currently executing task. */ if( ( taskTASK_IS_RUNNING( pxTCB ) == pdFALSE ) && ( pxTCB != NULL ) ) #else + /* The parameter cannot be NULL as it is impossible to resume the * currently executing task. It is also impossible to resume a task * that is actively running on another core but it is not safe From f41b6a626b5fad1037be1a91f56c033ab1f25f6a Mon Sep 17 00:00:00 2001 From: "Ching-Hsin,Lee" Date: Mon, 8 Jul 2024 14:37:13 +0800 Subject: [PATCH 20/25] Fix typo in xHigherPriorityReadyTasks --- tasks.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tasks.c b/tasks.c index ba8b6c7b50..dcc26bee3d 100644 --- a/tasks.c +++ b/tasks.c @@ -3947,7 +3947,7 @@ void vTaskSuspendAll( void ) static TickType_t prvGetExpectedIdleTime( void ) { TickType_t xReturn; - BaseType_t uxHigherPriorityReadyTasks = pdFALSE; + BaseType_t xHigherPriorityReadyTasks = pdFALSE; TCB_t * const pxConstCurrentTCB = prvGetCurrentTaskTCB(); /* xHigherPriorityReadyTasks takes care of the case where From dded3f483ebb25d43d99a7aad9bfdf138fa5920f Mon Sep 17 00:00:00 2001 From: "Ching-Hsin,Lee" Date: Mon, 8 Jul 2024 14:52:27 +0800 Subject: [PATCH 21/25] Update typo --- tasks.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tasks.c b/tasks.c index dcc26bee3d..85094f992d 100644 --- a/tasks.c +++ b/tasks.c @@ -1016,7 +1016,7 @@ static void prvAddNewTaskToReadyList( TCB_t * pxNewTCB ) PRIVILEGED_FUNCTION; TCB_t * const pxConstCurrentTCB = prvGetCurrentTaskTCBUnsafe(); /* Verify that the calling core always yields to higher priority tasks. */ - if( ( ( prvGetCurrentTaskTCBUnsafe->uxTaskAttributes & taskATTRIBUTE_IS_IDLE ) == 0U ) && + if( ( ( pxConstCurrentTCB->uxTaskAttributes & taskATTRIBUTE_IS_IDLE ) == 0U ) && ( pxTCB->uxPriority > pxConstCurrentTCB->uxPriority ) ) { configASSERT( ( xYieldPendings[ portGET_CORE_ID() ] == pdTRUE ) || From e43f7d808b42ed09809b329e3ab26095c8874097 Mon Sep 17 00:00:00 2001 From: "Ching-Hsin,Lee" Date: Mon, 8 Jul 2024 16:12:26 +0800 Subject: [PATCH 22/25] Update variable name --- tasks.c | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/tasks.c b/tasks.c index 85094f992d..ca19173e9b 100644 --- a/tasks.c +++ b/tasks.c @@ -942,17 +942,17 @@ static void prvAddNewTaskToReadyList( TCB_t * pxNewTCB ) PRIVILEGED_FUNCTION; for( xCoreID = ( BaseType_t ) 0; xCoreID < ( BaseType_t ) configNUMBER_OF_CORES; xCoreID++ ) { - TCB_t * const pxConstCurrentTCB = pxCurrentTCBs[ xCoreID ]; + TCB_t * const pxConstTCB = pxCurrentTCBs[ xCoreID ]; - xCurrentCoreTaskPriority = ( BaseType_t ) pxConstCurrentTCB->uxPriority; + xCurrentCoreTaskPriority = ( BaseType_t ) pxConstTCB->uxPriority; /* System idle tasks are being assigned a priority of tskIDLE_PRIORITY - 1 here. */ - if( ( pxConstCurrentTCB->uxTaskAttributes & taskATTRIBUTE_IS_IDLE ) != 0U ) + if( ( pxConstTCB->uxTaskAttributes & taskATTRIBUTE_IS_IDLE ) != 0U ) { xCurrentCoreTaskPriority = ( BaseType_t ) ( xCurrentCoreTaskPriority - 1 ); } - if( ( taskTASK_IS_RUNNING( pxConstCurrentTCB ) != pdFALSE ) && ( xYieldPendings[ xCoreID ] == pdFALSE ) ) + if( ( taskTASK_IS_RUNNING( pxConstTCB ) != pdFALSE ) && ( xYieldPendings[ xCoreID ] == pdFALSE ) ) { #if ( configRUN_MULTIPLE_PRIORITIES == 0 ) if( taskTASK_IS_RUNNING( pxTCB ) == pdFALSE ) @@ -965,7 +965,7 @@ static void prvAddNewTaskToReadyList( TCB_t * pxNewTCB ) PRIVILEGED_FUNCTION; #endif { #if ( configUSE_TASK_PREEMPTION_DISABLE == 1 ) - if( pxConstCurrentTCB->xPreemptionDisable == pdFALSE ) + if( pxConstTCB->xPreemptionDisable == pdFALSE ) #endif { xLowestPriorityToPreempt = xCurrentCoreTaskPriority; From 926224e7fd3ebbc6f3da008601316a4a8122f6fe Mon Sep 17 00:00:00 2001 From: "Ching-Hsin,Lee" Date: Mon, 8 Jul 2024 17:25:04 +0800 Subject: [PATCH 23/25] Rename prvGetCurrentTaskTCB to prvGetCurrentTCB --- tasks.c | 94 ++++++++++++++++++++++++++++----------------------------- 1 file changed, 47 insertions(+), 47 deletions(-) diff --git a/tasks.c b/tasks.c index ca19173e9b..ddd591288b 100644 --- a/tasks.c +++ b/tasks.c @@ -86,7 +86,7 @@ #define taskYIELD_ANY_CORE_IF_USING_PREEMPTION( pxTCB ) \ do { \ - if( ( prvGetCurrentTaskTCBUnsafe()->uxPriority ) < ( pxTCB )->uxPriority ) \ + if( ( prvGetCurrentTCBUnsafe()->uxPriority ) < ( pxTCB )->uxPriority ) \ { \ portYIELD_WITHIN_API(); \ } \ @@ -188,7 +188,7 @@ \ /* listGET_OWNER_OF_NEXT_ENTRY indexes through the list, so the tasks of \ * the same priority get an equal share of the processor time. */ \ - listGET_OWNER_OF_NEXT_ENTRY( prvGetCurrentTaskTCBUnsafe(), &( pxReadyTasksLists[ uxTopPriority ] ) ); \ + listGET_OWNER_OF_NEXT_ENTRY( prvGetCurrentTCBUnsafe(), &( pxReadyTasksLists[ uxTopPriority ] ) ); \ uxTopReadyPriority = uxTopPriority; \ } while( 0 ) /* taskSELECT_HIGHEST_PRIORITY_TASK */ #else /* if ( configNUMBER_OF_CORES == 1 ) */ @@ -223,7 +223,7 @@ /* Find the highest priority list that contains ready tasks. */ \ portGET_HIGHEST_PRIORITY( uxTopPriority, uxTopReadyPriority ); \ configASSERT( listCURRENT_LIST_LENGTH( &( pxReadyTasksLists[ uxTopPriority ] ) ) > 0 ); \ - listGET_OWNER_OF_NEXT_ENTRY( prvGetCurrentTaskTCBUnsafe(), &( pxReadyTasksLists[ uxTopPriority ] ) ); \ + listGET_OWNER_OF_NEXT_ENTRY( prvGetCurrentTCBUnsafe(), &( pxReadyTasksLists[ uxTopPriority ] ) ); \ } while( 0 ) /*-----------------------------------------------------------*/ @@ -280,7 +280,7 @@ * task should be used in place of the parameter. This macro simply checks to * see if the parameter is NULL and returns a pointer to the appropriate TCB. */ -#define prvGetTCBFromHandle( pxHandle ) ( ( ( pxHandle ) == NULL ) ? prvGetCurrentTaskTCB() : ( pxHandle ) ) +#define prvGetTCBFromHandle( pxHandle ) ( ( ( pxHandle ) == NULL ) ? prvGetCurrentTCB() : ( pxHandle ) ) /* The item value of the event list item is normally used to hold the priority * of the task to which it belongs (coded to allow it to be held in reverse @@ -306,8 +306,8 @@ /* Returns pdTRUE if the task is actively running and not scheduled to yield. */ #if ( configNUMBER_OF_CORES == 1 ) - #define taskTASK_IS_RUNNING( pxTCB ) ( ( ( pxTCB ) == prvGetCurrentTaskTCBUnsafe() ) ? ( pdTRUE ) : ( pdFALSE ) ) - #define taskTASK_IS_RUNNING_OR_SCHEDULED_TO_YIELD( pxTCB ) ( ( ( pxTCB ) == prvGetCurrentTaskTCBUnsafe() ) ? ( pdTRUE ) : ( pdFALSE ) ) + #define taskTASK_IS_RUNNING( pxTCB ) ( ( ( pxTCB ) == prvGetCurrentTCBUnsafe() ) ? ( pdTRUE ) : ( pdFALSE ) ) + #define taskTASK_IS_RUNNING_OR_SCHEDULED_TO_YIELD( pxTCB ) ( ( ( pxTCB ) == prvGetCurrentTCBUnsafe() ) ? ( pdTRUE ) : ( pdFALSE ) ) #else #define taskTASK_IS_RUNNING( pxTCB ) ( ( ( ( pxTCB )->xTaskRunState >= ( BaseType_t ) 0 ) && ( ( pxTCB )->xTaskRunState < ( BaseType_t ) configNUMBER_OF_CORES ) ) ? ( pdTRUE ) : ( pdFALSE ) ) #define taskTASK_IS_RUNNING_OR_SCHEDULED_TO_YIELD( pxTCB ) ( ( ( pxTCB )->xTaskRunState != taskTASK_NOT_RUNNING ) ? ( pdTRUE ) : ( pdFALSE ) ) @@ -317,10 +317,10 @@ #define taskATTRIBUTE_IS_IDLE ( UBaseType_t ) ( 1U << 0U ) #if ( ( configNUMBER_OF_CORES > 1 ) && ( portCRITICAL_NESTING_IN_TCB == 1 ) ) - #define portGET_CRITICAL_NESTING_COUNT() ( prvGetCurrentTaskTCBUnsafe()->uxCriticalNesting ) - #define portSET_CRITICAL_NESTING_COUNT( x ) ( prvGetCurrentTaskTCBUnsafe()->uxCriticalNesting = ( x ) ) - #define portINCREMENT_CRITICAL_NESTING_COUNT() ( prvGetCurrentTaskTCBUnsafe()->uxCriticalNesting++ ) - #define portDECREMENT_CRITICAL_NESTING_COUNT() ( prvGetCurrentTaskTCBUnsafe()->uxCriticalNesting-- ) + #define portGET_CRITICAL_NESTING_COUNT() ( prvGetCurrentTCBUnsafe()->uxCriticalNesting ) + #define portSET_CRITICAL_NESTING_COUNT( x ) ( prvGetCurrentTCBUnsafe()->uxCriticalNesting = ( x ) ) + #define portINCREMENT_CRITICAL_NESTING_COUNT() ( prvGetCurrentTCBUnsafe()->uxCriticalNesting++ ) + #define portDECREMENT_CRITICAL_NESTING_COUNT() ( prvGetCurrentTCBUnsafe()->uxCriticalNesting-- ) #endif /* #if ( ( configNUMBER_OF_CORES > 1 ) && ( portCRITICAL_NESTING_IN_TCB == 1 ) ) */ #define taskBITS_PER_BYTE ( ( size_t ) 8 ) @@ -350,17 +350,17 @@ #endif /* #if ( configNUMBER_OF_CORES > 1 ) */ #if ( configNUMBER_OF_CORES == 1 ) - #define prvGetCurrentTaskTCB() pxCurrentTCB - #define prvGetCurrentTaskTCBUnsafe() pxCurrentTCB + #define prvGetCurrentTCB() pxCurrentTCB + #define prvGetCurrentTCBUnsafe() pxCurrentTCB #else /* Retrieving the current task TCB in a multi-core environment involves two steps: * 1. Obtaining the core ID. * 2. Using the core ID to index the pxCurrentTCBs array. * If these two steps are not performed atomically, a race condition may occur. - * To ensure atomicity, prvGetCurrentTaskTCBUnsafe() should be called in a context where + * To ensure atomicity, prvGetCurrentTCBUnsafe() should be called in a context where * the core executing the task remains fixed until the operation is completed. */ - #define prvGetCurrentTaskTCBUnsafe() pxCurrentTCBs[ portGET_CORE_ID() ] + #define prvGetCurrentTCBUnsafe() pxCurrentTCBs[ portGET_CORE_ID() ] #endif /* if ( configNUMBER_OF_CORES == 1 ) */ /*-----------------------------------------------------------*/ @@ -551,7 +551,7 @@ static BaseType_t prvCreateIdleTasks( void ); * The race condition safe version to get the current task TCB in multiple cores * environment. */ - static TCB_t * prvGetCurrentTaskTCB( void ); + static TCB_t * prvGetCurrentTCB( void ); #endif /* #if ( configNUMBER_OF_CORES > 1 ) */ #if ( configNUMBER_OF_CORES > 1 ) @@ -827,14 +827,14 @@ static void prvAddNewTaskToReadyList( TCB_t * pxNewTCB ) PRIVILEGED_FUNCTION; #if ( configNUMBER_OF_CORES > 1 ) - static TCB_t * prvGetCurrentTaskTCB( void ) + static TCB_t * prvGetCurrentTCB( void ) { TCB_t * pxTCB; UBaseType_t uxSavedInterruptStatus; uxSavedInterruptStatus = portSET_INTERRUPT_MASK(); { - pxTCB = prvGetCurrentTaskTCBUnsafe(); + pxTCB = prvGetCurrentTCBUnsafe(); } portCLEAR_INTERRUPT_MASK( uxSavedInterruptStatus ); @@ -855,7 +855,7 @@ static void prvAddNewTaskToReadyList( TCB_t * pxNewTCB ) PRIVILEGED_FUNCTION; /* This function is always called with interrupts disabled * so this is safe. */ - pxThisTCB = prvGetCurrentTaskTCBUnsafe(); + pxThisTCB = prvGetCurrentTCBUnsafe(); while( pxThisTCB->xTaskRunState == taskTASK_SCHEDULED_TO_YIELD ) { @@ -1013,7 +1013,7 @@ static void prvAddNewTaskToReadyList( TCB_t * pxNewTCB ) PRIVILEGED_FUNCTION; #if ( configRUN_MULTIPLE_PRIORITIES == 0 ) { - TCB_t * const pxConstCurrentTCB = prvGetCurrentTaskTCBUnsafe(); + TCB_t * const pxConstCurrentTCB = prvGetCurrentTCBUnsafe(); /* Verify that the calling core always yields to higher priority tasks. */ if( ( ( pxConstCurrentTCB->uxTaskAttributes & taskATTRIBUTE_IS_IDLE ) == 0U ) && @@ -2075,7 +2075,7 @@ static void prvInitialiseNewTask( TaskFunction_t pxTaskCode, uxCurrentNumberOfTasks = ( UBaseType_t ) ( uxCurrentNumberOfTasks + 1U ); /* Invoking a function in a condition here for performance consideration. */ - if( prvGetCurrentTaskTCBUnsafe() == NULL ) + if( prvGetCurrentTCBUnsafe() == NULL ) { /* There are no other tasks, or all the other tasks are in * the suspended state - make this the current task. */ @@ -2869,7 +2869,7 @@ static void prvInitialiseNewTask( TaskFunction_t pxTaskCode, /* The priority of a task other than the currently * running task is being raised. Is the priority being * raised above that of the running task? */ - if( uxNewPriority > prvGetCurrentTaskTCBUnsafe()->uxPriority ) + if( uxNewPriority > prvGetCurrentTCBUnsafe()->uxPriority ) { xYieldRequired = pdTRUE; } @@ -3506,7 +3506,7 @@ static void prvInitialiseNewTask( TaskFunction_t pxTaskCode, { /* Ready lists can be accessed so move the task from the * suspended list to the ready list directly. */ - if( pxTCB->uxPriority > prvGetCurrentTaskTCBUnsafe()->uxPriority ) + if( pxTCB->uxPriority > prvGetCurrentTCBUnsafe()->uxPriority ) { xYieldRequired = pdTRUE; @@ -3764,7 +3764,7 @@ void vTaskStartScheduler( void ) { /* Switch C-Runtime's TLS Block to point to the TLS * block specific to the task that will run first. */ - configSET_TLS_BLOCK( prvGetCurrentTaskTCBUnsafe()->xTLSBlock ); + configSET_TLS_BLOCK( prvGetCurrentTCBUnsafe()->xTLSBlock ); } #endif @@ -3948,7 +3948,7 @@ void vTaskSuspendAll( void ) { TickType_t xReturn; BaseType_t xHigherPriorityReadyTasks = pdFALSE; - TCB_t * const pxConstCurrentTCB = prvGetCurrentTaskTCB(); + TCB_t * const pxConstCurrentTCB = prvGetCurrentTCB(); /* xHigherPriorityReadyTasks takes care of the case where * configUSE_PREEMPTION is 0, so there may be tasks above the idle priority @@ -4055,7 +4055,7 @@ BaseType_t xTaskResumeAll( void ) { /* If the moved task has a priority higher than the current * task then a yield must be performed. */ - if( pxTCB->uxPriority > prvGetCurrentTaskTCBUnsafe()->uxPriority ) + if( pxTCB->uxPriority > prvGetCurrentTCBUnsafe()->uxPriority ) { xYieldPendings[ xCoreID ] = pdTRUE; } @@ -4132,7 +4132,7 @@ BaseType_t xTaskResumeAll( void ) #if ( configNUMBER_OF_CORES == 1 ) { - taskYIELD_TASK_CORE_IF_USING_PREEMPTION( prvGetCurrentTaskTCBUnsafe() ); + taskYIELD_TASK_CORE_IF_USING_PREEMPTION( prvGetCurrentTCBUnsafe() ); } #endif /* #if ( configNUMBER_OF_CORES == 1 ) */ } @@ -4682,7 +4682,7 @@ BaseType_t xTaskCatchUpTicks( TickType_t xTicksToCatchUp ) /* Preemption is on, but a context switch should only be * performed if the unblocked task has a priority that is * higher than the currently executing task. */ - if( pxTCB->uxPriority > prvGetCurrentTaskTCB()->uxPriority ) + if( pxTCB->uxPriority > prvGetCurrentTCB()->uxPriority ) { /* Pend the yield to be performed when the scheduler * is unsuspended. */ @@ -4829,7 +4829,7 @@ BaseType_t xTaskIncrementTick( void ) { #if ( configNUMBER_OF_CORES == 1 ) { - TCB_t * const pxConstCurrentTCB = prvGetCurrentTaskTCBUnsafe(); + TCB_t * const pxConstCurrentTCB = prvGetCurrentTCBUnsafe(); /* Preemption is on, but a context switch should * only be performed if the unblocked task's @@ -4866,7 +4866,7 @@ BaseType_t xTaskIncrementTick( void ) { #if ( configNUMBER_OF_CORES == 1 ) { - if( listCURRENT_LIST_LENGTH( &( pxReadyTasksLists[ prvGetCurrentTaskTCBUnsafe()->uxPriority ] ) ) > 1U ) + if( listCURRENT_LIST_LENGTH( &( pxReadyTasksLists[ prvGetCurrentTCBUnsafe()->uxPriority ] ) ) > 1U ) { xSwitchRequired = pdTRUE; } @@ -5255,7 +5255,7 @@ BaseType_t xTaskIncrementTick( void ) /* Macro to inject port specific behaviour immediately after * switching tasks, such as setting an end of stack watchpoint * or reconfiguring the MPU. */ - portTASK_SWITCH_HOOK( prvGetCurrentTaskTCBUnsafe() ); + portTASK_SWITCH_HOOK( prvGetCurrentTCBUnsafe() ); /* After the new task is switched in, update the global errno. */ #if ( configUSE_POSIX_ERRNO == 1 ) @@ -5302,7 +5302,7 @@ void vTaskPlaceOnEventList( List_t * const pxEventList, * * The queue that contains the event list is locked, preventing * simultaneous access from interrupts. */ - vListInsert( pxEventList, &( prvGetCurrentTaskTCBUnsafe()->xEventListItem ) ); + vListInsert( pxEventList, &( prvGetCurrentTCBUnsafe()->xEventListItem ) ); prvAddCurrentTaskToDelayedList( xTicksToWait, pdTRUE ); @@ -5314,7 +5314,7 @@ void vTaskPlaceOnUnorderedEventList( List_t * pxEventList, const TickType_t xItemValue, const TickType_t xTicksToWait ) { - TCB_t * const pxConstCurrentTCB = prvGetCurrentTaskTCBUnsafe(); + TCB_t * const pxConstCurrentTCB = prvGetCurrentTCBUnsafe(); traceENTER_vTaskPlaceOnUnorderedEventList( pxEventList, xItemValue, xTicksToWait ); @@ -5362,7 +5362,7 @@ void vTaskPlaceOnUnorderedEventList( List_t * pxEventList, * In this case it is assume that this is the only task that is going to * be waiting on this event list, so the faster vListInsertEnd() function * can be used in place of vListInsert. */ - listINSERT_END( pxEventList, &( prvGetCurrentTaskTCBUnsafe()->xEventListItem ) ); + listINSERT_END( pxEventList, &( prvGetCurrentTCBUnsafe()->xEventListItem ) ); /* If the task should block indefinitely then set the block time to a * value that will be recognised as an indefinite delay inside the @@ -5436,7 +5436,7 @@ BaseType_t xTaskRemoveFromEventList( const List_t * const pxEventList ) #if ( configNUMBER_OF_CORES == 1 ) { - if( pxUnblockedTCB->uxPriority > prvGetCurrentTaskTCBUnsafe()->uxPriority ) + if( pxUnblockedTCB->uxPriority > prvGetCurrentTCBUnsafe()->uxPriority ) { /* Return true if the task removed from the event list has a higher * priority than the calling task. This allows the calling task to know if @@ -5519,7 +5519,7 @@ void vTaskRemoveFromUnorderedEventList( ListItem_t * pxEventListItem, #if ( configNUMBER_OF_CORES == 1 ) { - if( pxUnblockedTCB->uxPriority > prvGetCurrentTaskTCBUnsafe()->uxPriority ) + if( pxUnblockedTCB->uxPriority > prvGetCurrentTCBUnsafe()->uxPriority ) { /* The unblocked task has a priority above that of the calling task, so * a context switch is required. This function is called with the @@ -5580,7 +5580,7 @@ BaseType_t xTaskCheckForTimeOut( TimeOut_t * const pxTimeOut, BaseType_t xReturn; #if ( INCLUDE_xTaskAbortDelay == 1 ) - TCB_t * const pxConstCurrentTCB = prvGetCurrentTaskTCBUnsafe(); + TCB_t * const pxConstCurrentTCB = prvGetCurrentTCBUnsafe(); #endif traceENTER_xTaskCheckForTimeOut( pxTimeOut, pxTicksToWait ); @@ -6535,7 +6535,7 @@ static void prvResetNextTaskUnblockTime( void ) /* In SMP environment, interrupt must be disabled to get the current * task TCB. */ - xReturn = prvGetCurrentTaskTCB(); + xReturn = prvGetCurrentTCB(); traceRETURN_xTaskGetCurrentTaskHandle( xReturn ); @@ -6612,7 +6612,7 @@ static void prvResetNextTaskUnblockTime( void ) TCB_t * const pxMutexHolderTCB = pxMutexHolder; BaseType_t xReturn = pdFALSE; - TCB_t * const pxConstCurrentTCB = prvGetCurrentTaskTCBUnsafe(); + TCB_t * const pxConstCurrentTCB = prvGetCurrentTCBUnsafe(); traceENTER_xTaskPriorityInherit( pxMutexHolder ); @@ -6959,7 +6959,7 @@ static void prvResetNextTaskUnblockTime( void ) if( xSchedulerRunning != pdFALSE ) { - TCB_t * const pxConstCurrentTCB = prvGetCurrentTaskTCBUnsafe(); + TCB_t * const pxConstCurrentTCB = prvGetCurrentTCBUnsafe(); ( pxConstCurrentTCB->uxCriticalNesting )++; @@ -7071,7 +7071,7 @@ static void prvResetNextTaskUnblockTime( void ) void vTaskExitCritical( void ) { - TCB_t * const pxConstCurrentTCB = prvGetCurrentTaskTCBUnsafe(); + TCB_t * const pxConstCurrentTCB = prvGetCurrentTCBUnsafe(); traceENTER_vTaskExitCritical(); @@ -7606,7 +7606,7 @@ TickType_t uxTaskResetEventItemValue( void ) { TickType_t uxReturn; - TCB_t * const pxConstCurrentTCB = prvGetCurrentTaskTCBUnsafe(); + TCB_t * const pxConstCurrentTCB = prvGetCurrentTCBUnsafe(); traceENTER_uxTaskResetEventItemValue(); @@ -7631,7 +7631,7 @@ TickType_t uxTaskResetEventItemValue( void ) traceENTER_pvTaskIncrementMutexHeldCount(); /* This API should be called in critical section only. */ - pxTCB = prvGetCurrentTaskTCBUnsafe(); + pxTCB = prvGetCurrentTCBUnsafe(); /* If xSemaphoreCreateMutex() is called before any tasks have been created * then pxCurrentTCB will be NULL. */ @@ -7656,7 +7656,7 @@ TickType_t uxTaskResetEventItemValue( void ) { uint32_t ulReturn; BaseType_t xAlreadyYielded, xShouldBlock = pdFALSE; - TCB_t * const pxConstCurrentTCB = prvGetCurrentTaskTCB(); + TCB_t * const pxConstCurrentTCB = prvGetCurrentTCB(); traceENTER_ulTaskGenericNotifyTake( uxIndexToWaitOn, xClearCountOnExit, xTicksToWait ); @@ -7761,7 +7761,7 @@ TickType_t uxTaskResetEventItemValue( void ) TickType_t xTicksToWait ) { BaseType_t xReturn, xAlreadyYielded, xShouldBlock = pdFALSE; - TCB_t * const pxConstCurrentTCB = prvGetCurrentTaskTCB(); + TCB_t * const pxConstCurrentTCB = prvGetCurrentTCB(); traceENTER_xTaskGenericNotifyWait( uxIndexToWaitOn, ulBitsToClearOnEntry, ulBitsToClearOnExit, pulNotificationValue, xTicksToWait ); @@ -8106,7 +8106,7 @@ TickType_t uxTaskResetEventItemValue( void ) #if ( configNUMBER_OF_CORES == 1 ) { - if( pxTCB->uxPriority > prvGetCurrentTaskTCBUnsafe()->uxPriority ) + if( pxTCB->uxPriority > prvGetCurrentTCBUnsafe()->uxPriority ) { /* The notified task has a priority above the currently * executing task so a yield is required. */ @@ -8224,7 +8224,7 @@ TickType_t uxTaskResetEventItemValue( void ) #if ( configNUMBER_OF_CORES == 1 ) { - TCB_t * const pxConstCurrentTCB = prvGetCurrentTaskTCBUnsafe(); + TCB_t * const pxConstCurrentTCB = prvGetCurrentTCBUnsafe(); if( pxTCB->uxPriority > pxConstCurrentTCB->uxPriority ) { @@ -8462,7 +8462,7 @@ static void prvAddCurrentTaskToDelayedList( TickType_t xTicksToWait, const TickType_t xConstTickCount = xTickCount; List_t * const pxDelayedList = pxDelayedTaskList; List_t * const pxOverflowDelayedList = pxOverflowDelayedTaskList; - TCB_t * const pxConstCurrentTCB = prvGetCurrentTaskTCBUnsafe(); + TCB_t * const pxConstCurrentTCB = prvGetCurrentTCBUnsafe(); #if ( INCLUDE_xTaskAbortDelay == 1 ) { From 937dcf11fec26b871ad94cd095e7928a70a30f13 Mon Sep 17 00:00:00 2001 From: "Ching-Hsin,Lee" Date: Mon, 8 Jul 2024 17:26:41 +0800 Subject: [PATCH 24/25] Update format --- tasks.c | 36 ++++++++++++++++++------------------ 1 file changed, 18 insertions(+), 18 deletions(-) diff --git a/tasks.c b/tasks.c index ddd591288b..c02cf7514a 100644 --- a/tasks.c +++ b/tasks.c @@ -84,16 +84,16 @@ portYIELD_WITHIN_API(); \ } while( 0 ) - #define taskYIELD_ANY_CORE_IF_USING_PREEMPTION( pxTCB ) \ - do { \ + #define taskYIELD_ANY_CORE_IF_USING_PREEMPTION( pxTCB ) \ + do { \ if( ( prvGetCurrentTCBUnsafe()->uxPriority ) < ( pxTCB )->uxPriority ) \ - { \ - portYIELD_WITHIN_API(); \ - } \ - else \ - { \ - mtCOVERAGE_TEST_MARKER(); \ - } \ + { \ + portYIELD_WITHIN_API(); \ + } \ + else \ + { \ + mtCOVERAGE_TEST_MARKER(); \ + } \ } while( 0 ) #else /* if ( configNUMBER_OF_CORES == 1 ) */ @@ -187,9 +187,9 @@ } \ \ /* listGET_OWNER_OF_NEXT_ENTRY indexes through the list, so the tasks of \ - * the same priority get an equal share of the processor time. */ \ + * the same priority get an equal share of the processor time. */ \ listGET_OWNER_OF_NEXT_ENTRY( prvGetCurrentTCBUnsafe(), &( pxReadyTasksLists[ uxTopPriority ] ) ); \ - uxTopReadyPriority = uxTopPriority; \ + uxTopReadyPriority = uxTopPriority; \ } while( 0 ) /* taskSELECT_HIGHEST_PRIORITY_TASK */ #else /* if ( configNUMBER_OF_CORES == 1 ) */ @@ -216,13 +216,13 @@ /*-----------------------------------------------------------*/ - #define taskSELECT_HIGHEST_PRIORITY_TASK() \ - do { \ - UBaseType_t uxTopPriority; \ - \ - /* Find the highest priority list that contains ready tasks. */ \ - portGET_HIGHEST_PRIORITY( uxTopPriority, uxTopReadyPriority ); \ - configASSERT( listCURRENT_LIST_LENGTH( &( pxReadyTasksLists[ uxTopPriority ] ) ) > 0 ); \ + #define taskSELECT_HIGHEST_PRIORITY_TASK() \ + do { \ + UBaseType_t uxTopPriority; \ + \ + /* Find the highest priority list that contains ready tasks. */ \ + portGET_HIGHEST_PRIORITY( uxTopPriority, uxTopReadyPriority ); \ + configASSERT( listCURRENT_LIST_LENGTH( &( pxReadyTasksLists[ uxTopPriority ] ) ) > 0 ); \ listGET_OWNER_OF_NEXT_ENTRY( prvGetCurrentTCBUnsafe(), &( pxReadyTasksLists[ uxTopPriority ] ) ); \ } while( 0 ) From b7e9b2da88f5bd07a6b838c904d46637801ad136 Mon Sep 17 00:00:00 2001 From: "Ching-Hsin,Lee" Date: Mon, 8 Jul 2024 17:30:53 +0800 Subject: [PATCH 25/25] Rename function name --- include/stack_macros.h | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/include/stack_macros.h b/include/stack_macros.h index 3b1ca65d49..e2fda53869 100644 --- a/include/stack_macros.h +++ b/include/stack_macros.h @@ -58,7 +58,7 @@ /* Only the current stack state is to be checked. */ #define taskCHECK_FOR_STACK_OVERFLOW() \ do { \ - TCB_t * const pxConstCurrentTCB = prvGetCurrentTaskTCBUnsafe(); \ + TCB_t * const pxConstCurrentTCB = prvGetCurrentTCBUnsafe(); \ \ /* Is the currently saved stack pointer within the stack limit? */ \ if( pxConstCurrentTCB->pxTopOfStack <= pxConstCurrentTCB->pxStack + portSTACK_LIMIT_PADDING ) \ @@ -76,7 +76,7 @@ /* Only the current stack state is to be checked. */ #define taskCHECK_FOR_STACK_OVERFLOW() \ do { \ - TCB_t * const pxConstCurrentTCB = prvGetCurrentTaskTCBUnsafe(); \ + TCB_t * const pxConstCurrentTCB = prvGetCurrentTCBUnsafe(); \ \ /* Is the currently saved stack pointer within the stack limit? */ \ if( pxConstCurrentTCB->pxTopOfStack >= pxConstCurrentTCB->pxEndOfStack - portSTACK_LIMIT_PADDING ) \ @@ -93,7 +93,7 @@ #define taskCHECK_FOR_STACK_OVERFLOW() \ do { \ - TCB_t * const pxConstCurrentTCB = prvGetCurrentTaskTCBUnsafe(); \ + TCB_t * const pxConstCurrentTCB = prvGetCurrentTCBUnsafe(); \ const uint32_t * const pulStack = ( uint32_t * ) pxConstCurrentTCB->pxStack; \ const uint32_t ulCheckValue = ( uint32_t ) 0xa5a5a5a5U; \ \ @@ -114,7 +114,7 @@ #define taskCHECK_FOR_STACK_OVERFLOW() \ do { \ - TCB_t * const pxConstCurrentTCB = prvGetCurrentTaskTCBUnsafe(); \ + TCB_t * const pxConstCurrentTCB = prvGetCurrentTCBUnsafe(); \ int8_t * pcEndOfStack = ( int8_t * ) pxConstCurrentTCB->pxEndOfStack; \ static const uint8_t ucExpectedStackBytes[] = { tskSTACK_FILL_BYTE, tskSTACK_FILL_BYTE, tskSTACK_FILL_BYTE, tskSTACK_FILL_BYTE, \ tskSTACK_FILL_BYTE, tskSTACK_FILL_BYTE, tskSTACK_FILL_BYTE, tskSTACK_FILL_BYTE, \