From 830533d49e70806b4f46dade245c2f05f0c06b0f Mon Sep 17 00:00:00 2001 From: chinglee-iot <61685396+chinglee-iot@users.noreply.github.com> Date: Wed, 4 Oct 2023 17:38:27 +0800 Subject: [PATCH 1/2] Add taskYIELD_WITHIN_API macro (#782) Add taskYIELD_WITHIN_API macro for readability improvement. --- event_groups.c | 20 ++------------------ include/task.h | 6 ++++++ queue.c | 40 ++++------------------------------------ tasks.c | 32 ++++---------------------------- timers.c | 10 +--------- 5 files changed, 17 insertions(+), 91 deletions(-) diff --git a/event_groups.c b/event_groups.c index 6b68c087a10..556637b4cda 100644 --- a/event_groups.c +++ b/event_groups.c @@ -253,15 +253,7 @@ EventBits_t xEventGroupSync( EventGroupHandle_t xEventGroup, { if( xAlreadyYielded == pdFALSE ) { - #if ( configNUMBER_OF_CORES == 1 ) - { - portYIELD_WITHIN_API(); - } - #else /* #if ( configNUMBER_OF_CORES == 1 ) */ - { - vTaskYieldWithinAPI(); - } - #endif /* #if ( configNUMBER_OF_CORES == 1 ) */ + taskYIELD_WITHIN_API(); } else { @@ -417,15 +409,7 @@ EventBits_t xEventGroupWaitBits( EventGroupHandle_t xEventGroup, { if( xAlreadyYielded == pdFALSE ) { - #if ( configNUMBER_OF_CORES == 1 ) - { - portYIELD_WITHIN_API(); - } - #else /* #if ( configNUMBER_OF_CORES == 1 ) */ - { - vTaskYieldWithinAPI(); - } - #endif /* #if ( configNUMBER_OF_CORES == 1 ) */ + taskYIELD_WITHIN_API(); } else { diff --git a/include/task.h b/include/task.h index 8d6742c7f43..44835187676 100644 --- a/include/task.h +++ b/include/task.h @@ -3330,6 +3330,12 @@ BaseType_t xTaskCatchUpTicks( TickType_t xTicksToCatchUp ) PRIVILEGED_FUNCTION; * SCHEDULER INTERNALS AVAILABLE FOR PORTING PURPOSES *----------------------------------------------------------*/ +#if ( configNUMBER_OF_CORES == 1 ) + #define taskYIELD_WITHIN_API() portYIELD_WITHIN_API() +#else /* #if ( configNUMBER_OF_CORES == 1 ) */ + #define taskYIELD_WITHIN_API() vTaskYieldWithinAPI() +#endif /* #if ( configNUMBER_OF_CORES == 1 ) */ + /* * THIS FUNCTION MUST NOT BE USED FROM APPLICATION CODE. IT IS ONLY * INTENDED FOR USE WHEN IMPLEMENTING A PORT OF THE SCHEDULER AND IS diff --git a/queue.c b/queue.c index 91b9a0d6758..3bc959721ed 100644 --- a/queue.c +++ b/queue.c @@ -1129,15 +1129,7 @@ BaseType_t xQueueGenericSend( QueueHandle_t xQueue, * is also a higher priority task in the pending ready list. */ if( xTaskResumeAll() == pdFALSE ) { - #if ( configNUMBER_OF_CORES == 1 ) - { - portYIELD_WITHIN_API(); - } - #else /* #if ( configNUMBER_OF_CORES == 1 ) */ - { - vTaskYieldWithinAPI(); - } - #endif /* #if ( configNUMBER_OF_CORES == 1 ) */ + taskYIELD_WITHIN_API(); } } else @@ -1616,15 +1608,7 @@ BaseType_t xQueueReceive( QueueHandle_t xQueue, if( xTaskResumeAll() == pdFALSE ) { - #if ( configNUMBER_OF_CORES == 1 ) - { - portYIELD_WITHIN_API(); - } - #else /* #if ( configNUMBER_OF_CORES == 1 ) */ - { - vTaskYieldWithinAPI(); - } - #endif /* #if ( configNUMBER_OF_CORES == 1 ) */ + taskYIELD_WITHIN_API(); } else { @@ -1817,15 +1801,7 @@ BaseType_t xQueueSemaphoreTake( QueueHandle_t xQueue, if( xTaskResumeAll() == pdFALSE ) { - #if ( configNUMBER_OF_CORES == 1 ) - { - portYIELD_WITHIN_API(); - } - #else /* #if ( configNUMBER_OF_CORES == 1 ) */ - { - vTaskYieldWithinAPI(); - } - #endif /* #if ( configNUMBER_OF_CORES == 1 ) */ + taskYIELD_WITHIN_API(); } else { @@ -2013,15 +1989,7 @@ BaseType_t xQueuePeek( QueueHandle_t xQueue, if( xTaskResumeAll() == pdFALSE ) { - #if ( configNUMBER_OF_CORES == 1 ) - { - portYIELD_WITHIN_API(); - } - #else /* #if ( configNUMBER_OF_CORES == 1 ) */ - { - vTaskYieldWithinAPI(); - } - #endif /* #if ( configNUMBER_OF_CORES == 1 ) */ + taskYIELD_WITHIN_API(); } else { diff --git a/tasks.c b/tasks.c index 84bc87bf210..000767bb5e5 100644 --- a/tasks.c +++ b/tasks.c @@ -2195,11 +2195,7 @@ static void prvInitialiseNewTask( TaskFunction_t pxTaskCode, * have put ourselves to sleep. */ if( xAlreadyYielded == pdFALSE ) { - #if ( configNUMBER_OF_CORES == 1 ) - portYIELD_WITHIN_API(); - #else - vTaskYieldWithinAPI(); - #endif + taskYIELD_WITHIN_API(); } else { @@ -2251,11 +2247,7 @@ static void prvInitialiseNewTask( TaskFunction_t pxTaskCode, * have put ourselves to sleep. */ if( xAlreadyYielded == pdFALSE ) { - #if ( configNUMBER_OF_CORES == 1 ) - portYIELD_WITHIN_API(); - #else - vTaskYieldWithinAPI(); - #endif + taskYIELD_WITHIN_API(); } else { @@ -7253,15 +7245,7 @@ TickType_t uxTaskResetEventItemValue( void ) * section (some will yield immediately, others wait until the * critical section exits) - but it is not something that * application code should ever do. */ - #if ( configNUMBER_OF_CORES == 1 ) - { - portYIELD_WITHIN_API(); - } - #else - { - vTaskYieldWithinAPI(); - } - #endif + taskYIELD_WITHIN_API(); } else { @@ -7344,15 +7328,7 @@ TickType_t uxTaskResetEventItemValue( void ) * section (some will yield immediately, others wait until the * critical section exits) - but it is not something that * application code should ever do. */ - #if ( configNUMBER_OF_CORES == 1 ) - { - portYIELD_WITHIN_API(); - } - #else - { - vTaskYieldWithinAPI(); - } - #endif + taskYIELD_WITHIN_API(); } else { diff --git a/timers.c b/timers.c index 0d7a0c0fe22..f27caba3f46 100644 --- a/timers.c +++ b/timers.c @@ -771,15 +771,7 @@ * block time to expire. If a command arrived between the * critical section being exited and this yield then the yield * will not cause the task to block. */ - #if ( configNUMBER_OF_CORES == 1 ) - { - portYIELD_WITHIN_API(); - } - #else /* #if ( configNUMBER_OF_CORES == 1 ) */ - { - vTaskYieldWithinAPI(); - } - #endif /* #if ( configNUMBER_OF_CORES == 1 ) */ + taskYIELD_WITHIN_API(); } else { From 30e13dac2bd97b7c1429cdead03406d94a83a042 Mon Sep 17 00:00:00 2001 From: chinglee-iot <61685396+chinglee-iot@users.noreply.github.com> Date: Wed, 4 Oct 2023 19:33:28 +0800 Subject: [PATCH 2/2] Implement prvYieldCore with macro (#785) * Implement prvYieldCore with macro for performance and memory * Remove the portCHECK_IF_IN_ISR macro check. It is not required in SMP now Co-authored-by: kar-rahul-aws <118818625+kar-rahul-aws@users.noreply.github.com> Co-authored-by: Gaurav-Aggarwal-AWS <33462878+aggarg@users.noreply.github.com> --- include/FreeRTOS.h | 8 ------- tasks.c | 60 +++++++++++++++++++--------------------------- 2 files changed, 25 insertions(+), 43 deletions(-) diff --git a/include/FreeRTOS.h b/include/FreeRTOS.h index d0d63f1a22f..712d8cb52fe 100644 --- a/include/FreeRTOS.h +++ b/include/FreeRTOS.h @@ -453,14 +453,6 @@ #endif /* portGET_ISR_LOCK */ -#ifndef portCHECK_IF_IN_ISR - - #if ( configNUMBER_OF_CORES > 1 ) - #error portCHECK_IF_IN_ISR is required in SMP - #endif - -#endif /* portCHECK_IF_IN_ISR */ - #ifndef portENTER_CRITICAL_FROM_ISR #if ( configNUMBER_OF_CORES > 1 ) diff --git a/tasks.c b/tasks.c index 000767bb5e5..e0fc15bcc65 100644 --- a/tasks.c +++ b/tasks.c @@ -325,6 +325,31 @@ #define taskBITS_PER_BYTE ( ( size_t ) 8 ) +#if ( configNUMBER_OF_CORES > 1 ) + +/* Yields the given core. This must be called from a critical section and xCoreID + * must be valid. This macro is not required in single core since there is only + * one core to yield. */ + #define prvYieldCore( xCoreID ) \ + do { \ + if( xCoreID == ( BaseType_t ) portGET_CORE_ID() ) \ + { \ + /* Pending a yield for this core since it is in the critical section. */ \ + xYieldPendings[ xCoreID ] = pdTRUE; \ + } \ + else \ + { \ + /* Request other core to yield if it is not requested before. */ \ + if( pxCurrentTCBs[ xCoreID ]->xTaskRunState != taskTASK_SCHEDULED_TO_YIELD ) \ + { \ + portYIELD_CORE( xCoreID ); \ + pxCurrentTCBs[ xCoreID ]->xTaskRunState = taskTASK_SCHEDULED_TO_YIELD; \ + } \ + } \ + } while( 0 ) +#endif /* #if ( configNUMBER_OF_CORES > 1 ) */ +/*-----------------------------------------------------------*/ + /* * Task control block. A task control block (TCB) is allocated for each task, * and stores task state information, including a pointer to the task's context @@ -527,14 +552,6 @@ static BaseType_t prvCreateIdleTasks( void ); #if ( configNUMBER_OF_CORES > 1 ) -/* - * Yields the given core. - */ - static void prvYieldCore( BaseType_t xCoreID ); -#endif /* #if ( configNUMBER_OF_CORES > 1 ) */ - -#if ( configNUMBER_OF_CORES > 1 ) - /* * Yields a core, or cores if multiple priorities are not allowed to run * simultaneously, to allow the task pxTCB to run. @@ -811,33 +828,6 @@ static void prvAddNewTaskToReadyList( TCB_t * pxNewTCB ) PRIVILEGED_FUNCTION; /*-----------------------------------------------------------*/ -#if ( configNUMBER_OF_CORES > 1 ) - static void prvYieldCore( BaseType_t xCoreID ) - { - /* This must be called from a critical section and xCoreID must be valid. */ - if( ( portCHECK_IF_IN_ISR() == pdTRUE ) && ( xCoreID == ( BaseType_t ) portGET_CORE_ID() ) ) - { - xYieldPendings[ xCoreID ] = pdTRUE; - } - else - { - if( pxCurrentTCBs[ xCoreID ]->xTaskRunState != taskTASK_SCHEDULED_TO_YIELD ) - { - if( xCoreID == ( BaseType_t ) portGET_CORE_ID() ) - { - xYieldPendings[ xCoreID ] = pdTRUE; - } - else - { - portYIELD_CORE( xCoreID ); - pxCurrentTCBs[ xCoreID ]->xTaskRunState = taskTASK_SCHEDULED_TO_YIELD; - } - } - } - } -#endif /* #if ( configNUMBER_OF_CORES > 1 ) */ -/*-----------------------------------------------------------*/ - #if ( configNUMBER_OF_CORES > 1 ) static void prvYieldForTask( const TCB_t * pxTCB ) {