From 7d5a53d8df5271bc426b7774a68298807c522154 Mon Sep 17 00:00:00 2001 From: Ching-Hsin Lee Date: Thu, 31 Aug 2023 17:05:23 +0800 Subject: [PATCH 1/7] Add taskYIELD_FOR_TASK_IF_USING_PREEMPTION macro * taskYIELD_IF_USING_PREEMPTION is removed --- tasks.c | 113 +++++++++++++++++++++----------------------------------- 1 file changed, 42 insertions(+), 71 deletions(-) diff --git a/tasks.c b/tasks.c index f9bb48930a..52fb896c12 100644 --- a/tasks.c +++ b/tasks.c @@ -58,16 +58,30 @@ #include #endif /* configUSE_STATS_FORMATTING_FUNCTIONS == 1 ) */ -#if ( configNUMBER_OF_CORES == 1 ) - #if ( configUSE_PREEMPTION == 0 ) +#if ( configUSE_PREEMPTION == 0 ) /* If the cooperative scheduler is being used then a yield should not be * performed just because a higher priority task has been woken. */ - #define taskYIELD_IF_USING_PREEMPTION() - #else - #define taskYIELD_IF_USING_PREEMPTION() portYIELD_WITHIN_API() - #endif -#endif /* if ( configNUMBER_OF_CORES == 1 ) */ + #define taskYIELD_FOR_TASK_IF_USING_PREEMPTION( pxTCB ) +#else + + #if ( configNUMBER_OF_CORES == 1 ) + #define taskYIELD_FOR_TASK_IF_USING_PREEMPTION( pxTCB ) \ + do { \ + if( pxCurrentTCB->uxPriority < ( pxTCB )->uxPriority ) \ + { \ + portYIELD_WITHIN_API(); \ + } \ + else \ + { \ + mtCOVERAGE_TEST_MARKER(); \ + } \ + } while( 0 ) + #else /* if ( configNUMBER_OF_CORES == 1 ) */ + #define taskYIELD_FOR_TASK_IF_USING_PREEMPTION( pxTCB ) prvYieldForTask( pxTCB ) + #endif /* #if ( configNUMBER_OF_CORES == 1 ) */ + +#endif /* if ( configUSE_PREEMPTION == 0 ) */ /* Values that can be assigned to the ucNotifyState member of the TCB. */ #define taskNOT_WAITING_NOTIFICATION ( ( uint8_t ) 0 ) /* Must be zero as it is the initialised value. */ @@ -1779,14 +1793,7 @@ static void prvInitialiseNewTask( TaskFunction_t pxTaskCode, { /* If the created task is of a higher priority than the current task * then it should run now. */ - if( pxCurrentTCB->uxPriority < pxNewTCB->uxPriority ) - { - taskYIELD_IF_USING_PREEMPTION(); - } - else - { - mtCOVERAGE_TEST_MARKER(); - } + taskYIELD_FOR_TASK_IF_USING_PREEMPTION( pxNewTCB ); } else { @@ -1862,9 +1869,7 @@ static void prvInitialiseNewTask( TaskFunction_t pxTaskCode, /* If the created task is of a higher priority than another * currently running task and preemption is on then it should * run now. */ - #if ( configUSE_PREEMPTION == 1 ) - prvYieldForTask( pxNewTCB ); - #endif + taskYIELD_FOR_TASK_IF_USING_PREEMPTION( pxNewTCB ); } else { @@ -2534,7 +2539,12 @@ static void prvInitialiseNewTask( TaskFunction_t pxTaskCode, { if( xYieldRequired != pdFALSE ) { - taskYIELD_IF_USING_PREEMPTION(); + #if ( configUSE_PREEMPTION == 1 ) + { + /* Priority of current running task is set down. */ + portYIELD_WITHIN_API(); + } + #endif /* #if ( configUSE_PREEMPTION == 1 ) */ } else { @@ -2944,30 +2954,10 @@ static void prvInitialiseNewTask( TaskFunction_t pxTaskCode, ( void ) uxListRemove( &( pxTCB->xStateListItem ) ); prvAddTaskToReadyList( pxTCB ); - #if ( configNUMBER_OF_CORES == 1 ) - { - /* A higher priority task may have just been resumed. */ - if( pxTCB->uxPriority > pxCurrentTCB->uxPriority ) - { - /* This yield may not cause the task just resumed to run, - * but will leave the lists in the correct state for the - * next yield. */ - taskYIELD_IF_USING_PREEMPTION(); - } - else - { - mtCOVERAGE_TEST_MARKER(); - } - } - #else /* #if ( configNUMBER_OF_CORES == 1 ) */ - { - #if ( configUSE_PREEMPTION == 1 ) - { - prvYieldForTask( pxTCB ); - } - #endif /* #if ( configUSE_PREEMPTION == 1 ) */ - } - #endif /* #if ( configNUMBER_OF_CORES == 1 ) */ + /* This yield may not cause the task just resumed to run, + * but will leave the lists in the correct state for the + * next yield. */ + taskYIELD_FOR_TASK_IF_USING_PREEMPTION( pxTCB ); } else { @@ -3610,14 +3600,14 @@ BaseType_t xTaskResumeAll( void ) #if ( configUSE_PREEMPTION != 0 ) { xAlreadyYielded = pdTRUE; - } - #endif /* #if ( configUSE_PREEMPTION != 0 ) */ - #if ( configNUMBER_OF_CORES == 1 ) - { - taskYIELD_IF_USING_PREEMPTION(); + #if ( configNUMBER_OF_CORES == 1 ) + { + portYIELD_WITHIN_API(); + } + #endif /* #if ( configNUMBER_OF_CORES == 1 ) */ } - #endif /* #if ( configNUMBER_OF_CORES == 1 ) */ + #endif /* #if ( configUSE_PREEMPTION != 0 ) */ } else { @@ -7060,28 +7050,9 @@ TickType_t uxTaskResetEventItemValue( void ) } #endif - #if ( configNUMBER_OF_CORES == 1 ) - { - if( pxTCB->uxPriority > pxCurrentTCB->uxPriority ) - { - /* The notified task has a priority above the currently - * executing task so a yield is required. */ - taskYIELD_IF_USING_PREEMPTION(); - } - else - { - mtCOVERAGE_TEST_MARKER(); - } - } - #else /* #if ( configNUMBER_OF_CORES == 1 ) */ - { - #if ( configUSE_PREEMPTION == 1 ) - { - prvYieldForTask( pxTCB ); - } - #endif - } - #endif /* #if ( configNUMBER_OF_CORES == 1 ) */ + /* Check if the notified task has a priority above the currently + * executing task. */ + taskYIELD_FOR_TASK_IF_USING_PREEMPTION( pxTCB ); } else { From 2dcae64c9df9adfdd7407856b06aa541fa124671 Mon Sep 17 00:00:00 2001 From: Ching-Hsin Lee Date: Thu, 31 Aug 2023 20:51:57 +0800 Subject: [PATCH 2/7] Add back taskYIELD_IF_USING_PREEMPTION --- tasks.c | 61 ++++++++++++++++++++++++--------------------------------- 1 file changed, 26 insertions(+), 35 deletions(-) diff --git a/tasks.c b/tasks.c index 52fb896c12..0112ca0e8e 100644 --- a/tasks.c +++ b/tasks.c @@ -62,10 +62,14 @@ /* If the cooperative scheduler is being used then a yield should not be * performed just because a higher priority task has been woken. */ + #define taskYIELD_IF_USING_PREEMPTION( pxTCB ) #define taskYIELD_FOR_TASK_IF_USING_PREEMPTION( pxTCB ) #else #if ( configNUMBER_OF_CORES == 1 ) + /* pxTCB is not used since the task can only runs on core 0 in single core. */ + #define taskYIELD_IF_USING_PREEMPTION( pxTCB ) portYIELD_WITHIN_API() + #define taskYIELD_FOR_TASK_IF_USING_PREEMPTION( pxTCB ) \ do { \ if( pxCurrentTCB->uxPriority < ( pxTCB )->uxPriority ) \ @@ -77,7 +81,9 @@ mtCOVERAGE_TEST_MARKER(); \ } \ } while( 0 ) - #else /* if ( configNUMBER_OF_CORES == 1 ) */ + #else /* if ( configNUMBER_OF_CORES == 1 ) */ + /* Yield the core on which the task is running. */ + #define taskYIELD_IF_USING_PREEMPTION( pxTCB ) prvYieldCore( ( pxTCB )->xTaskRunState ) #define taskYIELD_FOR_TASK_IF_USING_PREEMPTION( pxTCB ) prvYieldForTask( pxTCB ) #endif /* #if ( configNUMBER_OF_CORES == 1 ) */ @@ -2535,42 +2541,27 @@ static void prvInitialiseNewTask( TaskFunction_t pxTaskCode, #endif } - #if ( configNUMBER_OF_CORES == 1 ) + if( xYieldRequired != pdFALSE ) { - if( xYieldRequired != pdFALSE ) - { - #if ( configUSE_PREEMPTION == 1 ) - { - /* Priority of current running task is set down. */ - portYIELD_WITHIN_API(); - } - #endif /* #if ( configUSE_PREEMPTION == 1 ) */ - } - else - { - mtCOVERAGE_TEST_MARKER(); - } + /* The running task priority is set down. Request the task to yield. */ + taskYIELD_IF_USING_PREEMPTION( pxTCB ); } - #else /* #if ( configNUMBER_OF_CORES == 1 ) */ + else { - #if ( configUSE_PREEMPTION == 1 ) - { - if( xYieldRequired != pdFALSE ) - { - prvYieldCore( pxTCB->xTaskRunState ); - } - else if( xYieldForTask != pdFALSE ) + #if ( configNUMBER_OF_CORES > 1 ) + if( xYieldForTask != pdFALSE ) { - prvYieldForTask( pxTCB ); + /* The priority of the task is being raised. If a running + * has priority lower than this task, it should yield + * for this task. */ + taskYIELD_FOR_TASK_IF_USING_PREEMPTION( pxTCB ); } else - { - mtCOVERAGE_TEST_MARKER(); - } + #endif /* if ( configNUMBER_OF_CORES > 1 ) */ + { + mtCOVERAGE_TEST_MARKER(); } - #endif /* #if ( configUSE_PREEMPTION == 1 ) */ } - #endif /* #if ( configNUMBER_OF_CORES == 1 ) */ /* Remove compiler warning about unused variables when the port * optimised task selection is not being used. */ @@ -3600,14 +3591,14 @@ BaseType_t xTaskResumeAll( void ) #if ( configUSE_PREEMPTION != 0 ) { xAlreadyYielded = pdTRUE; - - #if ( configNUMBER_OF_CORES == 1 ) - { - portYIELD_WITHIN_API(); - } - #endif /* #if ( configNUMBER_OF_CORES == 1 ) */ } #endif /* #if ( configUSE_PREEMPTION != 0 ) */ + + #if ( configNUMBER_OF_CORES == 1 ) + { + taskYIELD_IF_USING_PREEMPTION(); + } + #endif /* #if ( configNUMBER_OF_CORES == 1 ) */ } else { From e9d7831132318311fd572899642a67d7f8af0358 Mon Sep 17 00:00:00 2001 From: Ching-Hsin Lee Date: Thu, 31 Aug 2023 21:00:14 +0800 Subject: [PATCH 3/7] Update format --- tasks.c | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/tasks.c b/tasks.c index 0112ca0e8e..f88ac3a21c 100644 --- a/tasks.c +++ b/tasks.c @@ -67,7 +67,8 @@ #else #if ( configNUMBER_OF_CORES == 1 ) - /* pxTCB is not used since the task can only runs on core 0 in single core. */ + +/* pxTCB is not used since the task can only runs on core 0 in single core. */ #define taskYIELD_IF_USING_PREEMPTION( pxTCB ) portYIELD_WITHIN_API() #define taskYIELD_FOR_TASK_IF_USING_PREEMPTION( pxTCB ) \ @@ -81,10 +82,15 @@ mtCOVERAGE_TEST_MARKER(); \ } \ } while( 0 ) + #else /* if ( configNUMBER_OF_CORES == 1 ) */ - /* Yield the core on which the task is running. */ + +/* Yield the core on which this task is running. */ #define taskYIELD_IF_USING_PREEMPTION( pxTCB ) prvYieldCore( ( pxTCB )->xTaskRunState ) + +/* Yield for the task if a running task has priority lower than this task. */ #define taskYIELD_FOR_TASK_IF_USING_PREEMPTION( pxTCB ) prvYieldForTask( pxTCB ) + #endif /* #if ( configNUMBER_OF_CORES == 1 ) */ #endif /* if ( configUSE_PREEMPTION == 0 ) */ From 9946fbbd39f89821cf4257cd64a46be31a5ba41c Mon Sep 17 00:00:00 2001 From: Ching-Hsin Lee Date: Thu, 31 Aug 2023 21:02:55 +0800 Subject: [PATCH 4/7] Fix comment --- tasks.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tasks.c b/tasks.c index f88ac3a21c..6def24ef19 100644 --- a/tasks.c +++ b/tasks.c @@ -2558,7 +2558,7 @@ static void prvInitialiseNewTask( TaskFunction_t pxTaskCode, if( xYieldForTask != pdFALSE ) { /* The priority of the task is being raised. If a running - * has priority lower than this task, it should yield + * task has priority lower than this task, it should yield * for this task. */ taskYIELD_FOR_TASK_IF_USING_PREEMPTION( pxTCB ); } From 3c38895bc7104a1e18c12ce41e785e4b04b548cc Mon Sep 17 00:00:00 2001 From: Ching-Hsin Lee Date: Fri, 1 Sep 2023 13:21:57 +0800 Subject: [PATCH 5/7] Add parameter check for taskYIELD_IF_USING_PREEMPTION --- tasks.c | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/tasks.c b/tasks.c index 483307bbd4..150371b4c1 100644 --- a/tasks.c +++ b/tasks.c @@ -68,8 +68,15 @@ #if ( configNUMBER_OF_CORES == 1 ) -/* pxTCB is not used since the task can only runs on core 0 in single core. */ - #define taskYIELD_IF_USING_PREEMPTION( pxTCB ) portYIELD_WITHIN_API() +/* This macro requests the running task pxTCB to yield. In single core + * scheduler, a running task always runs on core 0 and portYIELD_WITHIN_API() + * can be used to request the task running on core 0 to yield. Therefore, pxTCB + * is not used in this macro. */ + #define taskYIELD_IF_USING_PREEMPTION( pxTCB ) \ + do { \ + ( void ) ( pxTCB ); \ + portYIELD_WITHIN_API(); \ + } while( 0 ) #define taskYIELD_FOR_TASK_IF_USING_PREEMPTION( pxTCB ) \ do { \ From 9493a45ac9f20426ae3e75c8ab61b5242e30d814 Mon Sep 17 00:00:00 2001 From: Ching-Hsin Lee Date: Fri, 1 Sep 2023 13:38:15 +0800 Subject: [PATCH 6/7] Fix compilation warning --- tasks.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tasks.c b/tasks.c index 150371b4c1..1573047acf 100644 --- a/tasks.c +++ b/tasks.c @@ -3611,7 +3611,7 @@ BaseType_t xTaskResumeAll( void ) #if ( configNUMBER_OF_CORES == 1 ) { - taskYIELD_IF_USING_PREEMPTION(); + taskYIELD_IF_USING_PREEMPTION( pxCurrentTCB ); } #endif /* #if ( configNUMBER_OF_CORES == 1 ) */ } From 9bfc5a10595c0ad1987195b702ff8288033afc0d Mon Sep 17 00:00:00 2001 From: Ching-Hsin Lee Date: Wed, 6 Sep 2023 15:25:14 +0800 Subject: [PATCH 7/7] Rename the taskYIELD preemption macros --- tasks.c | 32 ++++++++++++++++---------------- 1 file changed, 16 insertions(+), 16 deletions(-) diff --git a/tasks.c b/tasks.c index 469ac2d869..75ba9503f7 100644 --- a/tasks.c +++ b/tasks.c @@ -62,8 +62,8 @@ /* If the cooperative scheduler is being used then a yield should not be * performed just because a higher priority task has been woken. */ - #define taskYIELD_IF_USING_PREEMPTION( pxTCB ) - #define taskYIELD_FOR_TASK_IF_USING_PREEMPTION( pxTCB ) + #define taskYIELD_TASK_CORE_IF_USING_PREEMPTION( pxTCB ) + #define taskYIELD_ANY_CORE_IF_USING_PREEMPTION( pxTCB ) #else #if ( configNUMBER_OF_CORES == 1 ) @@ -72,13 +72,13 @@ * scheduler, a running task always runs on core 0 and portYIELD_WITHIN_API() * can be used to request the task running on core 0 to yield. Therefore, pxTCB * is not used in this macro. */ - #define taskYIELD_IF_USING_PREEMPTION( pxTCB ) \ - do { \ - ( void ) ( pxTCB ); \ - portYIELD_WITHIN_API(); \ + #define taskYIELD_TASK_CORE_IF_USING_PREEMPTION( pxTCB ) \ + do { \ + ( void ) ( pxTCB ); \ + portYIELD_WITHIN_API(); \ } while( 0 ) - #define taskYIELD_FOR_TASK_IF_USING_PREEMPTION( pxTCB ) \ + #define taskYIELD_ANY_CORE_IF_USING_PREEMPTION( pxTCB ) \ do { \ if( pxCurrentTCB->uxPriority < ( pxTCB )->uxPriority ) \ { \ @@ -93,10 +93,10 @@ #else /* if ( configNUMBER_OF_CORES == 1 ) */ /* Yield the core on which this task is running. */ - #define taskYIELD_IF_USING_PREEMPTION( pxTCB ) prvYieldCore( ( pxTCB )->xTaskRunState ) + #define taskYIELD_TASK_CORE_IF_USING_PREEMPTION( pxTCB ) prvYieldCore( ( pxTCB )->xTaskRunState ) /* Yield for the task if a running task has priority lower than this task. */ - #define taskYIELD_FOR_TASK_IF_USING_PREEMPTION( pxTCB ) prvYieldForTask( pxTCB ) + #define taskYIELD_ANY_CORE_IF_USING_PREEMPTION( pxTCB ) prvYieldForTask( pxTCB ) #endif /* #if ( configNUMBER_OF_CORES == 1 ) */ @@ -1813,7 +1813,7 @@ static void prvInitialiseNewTask( TaskFunction_t pxTaskCode, { /* If the created task is of a higher priority than the current task * then it should run now. */ - taskYIELD_FOR_TASK_IF_USING_PREEMPTION( pxNewTCB ); + taskYIELD_ANY_CORE_IF_USING_PREEMPTION( pxNewTCB ); } else { @@ -1889,7 +1889,7 @@ static void prvInitialiseNewTask( TaskFunction_t pxTaskCode, /* If the created task is of a higher priority than another * currently running task and preemption is on then it should * run now. */ - taskYIELD_FOR_TASK_IF_USING_PREEMPTION( pxNewTCB ); + taskYIELD_ANY_CORE_IF_USING_PREEMPTION( pxNewTCB ); } else { @@ -2559,7 +2559,7 @@ static void prvInitialiseNewTask( TaskFunction_t pxTaskCode, if( xYieldRequired != pdFALSE ) { /* The running task priority is set down. Request the task to yield. */ - taskYIELD_IF_USING_PREEMPTION( pxTCB ); + taskYIELD_TASK_CORE_IF_USING_PREEMPTION( pxTCB ); } else { @@ -2569,7 +2569,7 @@ static void prvInitialiseNewTask( TaskFunction_t pxTaskCode, /* The priority of the task is being raised. If a running * task has priority lower than this task, it should yield * for this task. */ - taskYIELD_FOR_TASK_IF_USING_PREEMPTION( pxTCB ); + taskYIELD_ANY_CORE_IF_USING_PREEMPTION( pxTCB ); } else #endif /* if ( configNUMBER_OF_CORES > 1 ) */ @@ -2963,7 +2963,7 @@ static void prvInitialiseNewTask( TaskFunction_t pxTaskCode, /* This yield may not cause the task just resumed to run, * but will leave the lists in the correct state for the * next yield. */ - taskYIELD_FOR_TASK_IF_USING_PREEMPTION( pxTCB ); + taskYIELD_ANY_CORE_IF_USING_PREEMPTION( pxTCB ); } else { @@ -3611,7 +3611,7 @@ BaseType_t xTaskResumeAll( void ) #if ( configNUMBER_OF_CORES == 1 ) { - taskYIELD_IF_USING_PREEMPTION( pxCurrentTCB ); + taskYIELD_TASK_CORE_IF_USING_PREEMPTION( pxCurrentTCB ); } #endif /* #if ( configNUMBER_OF_CORES == 1 ) */ } @@ -7058,7 +7058,7 @@ TickType_t uxTaskResetEventItemValue( void ) /* Check if the notified task has a priority above the currently * executing task. */ - taskYIELD_FOR_TASK_IF_USING_PREEMPTION( pxTCB ); + taskYIELD_ANY_CORE_IF_USING_PREEMPTION( pxTCB ); } else {