From 8e898b7e5ccb609104d41563a76eed37cac29c5d Mon Sep 17 00:00:00 2001 From: Ching-Hsin Lee Date: Tue, 5 Sep 2023 22:00:32 +0800 Subject: [PATCH 1/4] Add macro taskTASK_IS_RUNNING_OR_YIELDING * Add taskTASK_IS_RUNNING_OR_YIELDING macro to align single core and SMP --- tasks.c | 12 +++++------- 1 file changed, 5 insertions(+), 7 deletions(-) diff --git a/tasks.c b/tasks.c index 6cb233e7a5..db781ddcdf 100644 --- a/tasks.c +++ b/tasks.c @@ -270,9 +270,11 @@ typedef BaseType_t TaskRunning_t; /* 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( pxTCB ) ( ( ( pxTCB ) == pxCurrentTCB ) ? ( pdTRUE ) : ( pdFALSE ) ) + #define taskTASK_IS_RUNNING_OR_YIELDING( pxTCB ) ( ( ( pxTCB ) == pxCurrentTCB ) ? ( 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( pxTCB ) ( ( ( ( pxTCB )->xTaskRunState >= ( BaseType_t ) 0 ) && ( ( pxTCB )->xTaskRunState < ( BaseType_t ) configNUMBER_OF_CORES ) ) ? ( pdTRUE ) : ( pdFALSE ) ) + #define taskTASK_IS_RUNNING_OR_YIELDING( pxTCB ) ( ( ( pxTCB )->xTaskRunState != taskTASK_NOT_RUNNING ) ? ( pdTRUE ) : ( pdFALSE ) ) #endif /* Indicates that the task is an Idle task. */ @@ -1919,11 +1921,7 @@ static void prvInitialiseNewTask( TaskFunction_t pxTaskCode, /* If the task is running (or yielding), we must add it to the * termination list so that an idle task can delete it when it is * no longer running. */ - #if ( configNUMBER_OF_CORES == 1 ) - if( pxTCB == pxCurrentTCB ) - #else - if( pxTCB->xTaskRunState != taskTASK_NOT_RUNNING ) - #endif + if( taskTASK_IS_RUNNING_OR_YIELDING( pxTCB ) != pdFALSE ) { /* A running task is being deleted. This cannot complete within the * task itself, as a context switch to another task is required. From ea9a44f82d86d9fb1d1e8215886fa173e9b41db6 Mon Sep 17 00:00:00 2001 From: Ching-Hsin Lee Date: Tue, 5 Sep 2023 22:27:19 +0800 Subject: [PATCH 2/4] Add yield within API macro * Add yield within API macro for readibility --- event_groups.c | 26 ++++++++------------------ queue.c | 46 ++++++++++------------------------------------ tasks.c | 38 ++++++++++---------------------------- timers.c | 16 +++++++--------- 4 files changed, 35 insertions(+), 91 deletions(-) diff --git a/event_groups.c b/event_groups.c index 24d818711a..5b3f587926 100644 --- a/event_groups.c +++ b/event_groups.c @@ -46,6 +46,12 @@ * correct privileged Vs unprivileged linkage and placement. */ #undef MPU_WRAPPERS_INCLUDED_FROM_API_FILE /*lint !e961 !e750 !e9021 See comment above. */ +#if ( configNUMBER_OF_CORES == 1 ) + #define eventgroupYIELD_WITHIN_API() portYIELD_WITHIN_API() +#else /* #if ( configNUMBER_OF_CORES == 1 ) */ + #define eventgroupYIELD_WITHIN_API() vTaskYieldWithinAPI() +#endif /* #if ( configNUMBER_OF_CORES == 1 ) */ + typedef struct EventGroupDef_t { EventBits_t uxEventBits; @@ -243,15 +249,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 ) */ + eventgroupYIELD_WITHIN_API(); } else { @@ -403,15 +401,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 ) */ + eventgroupYIELD_WITHIN_API(); } else { diff --git a/queue.c b/queue.c index 9d300f861a..00ce903c10 100755 --- a/queue.c +++ b/queue.c @@ -96,6 +96,12 @@ typedef struct SemaphoreData #endif /* #if ( configNUMBER_OF_CORES == 1 ) */ #endif +#if ( configNUMBER_OF_CORES == 1 ) + #define queueYIELD_WITHIN_API() portYIELD_WITHIN_API() +#else /* #if ( configNUMBER_OF_CORES == 1 ) */ + #define queueYIELD_WITHIN_API() vTaskYieldWithinAPI() +#endif /* #if ( configNUMBER_OF_CORES == 1 ) */ + /* * Definition of the queue used by the scheduler. * Items are queued by copy, not reference. See the following link for the @@ -1074,15 +1080,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 ) */ + queueYIELD_WITHIN_API(); } } else @@ -1543,15 +1541,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 ) */ + queueYIELD_WITHIN_API(); } else { @@ -1734,15 +1724,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 ) */ + queueYIELD_WITHIN_API(); } else { @@ -1920,15 +1902,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 ) */ + queueYIELD_WITHIN_API(); } else { diff --git a/tasks.c b/tasks.c index db781ddcdf..19ba098ebd 100644 --- a/tasks.c +++ b/tasks.c @@ -69,6 +69,12 @@ #endif #endif /* if ( configNUMBER_OF_CORES == 1 ) */ +#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 ) */ + /* 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. */ #define taskWAITING_NOTIFICATION ( ( uint8_t ) 1 ) @@ -2095,11 +2101,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 { @@ -2147,11 +2149,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 { @@ -6814,15 +6812,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 { @@ -6901,15 +6891,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 f2a210c511..e3f0abe054 100644 --- a/timers.c +++ b/timers.c @@ -71,6 +71,12 @@ #define tmrSTATUS_IS_STATICALLY_ALLOCATED ( 0x02U ) #define tmrSTATUS_IS_AUTORELOAD ( 0x04U ) + #if ( configNUMBER_OF_CORES == 1 ) + #define timerYIELD_WITHIN_API() portYIELD_WITHIN_API() + #else /* #if ( configNUMBER_OF_CORES == 1 ) */ + #define timerYIELD_WITHIN_API() vTaskYieldWithinAPI() + #endif /* #if ( configNUMBER_OF_CORES == 1 ) */ + /* The definition of the timers themselves. */ typedef struct tmrTimerControl /* The old naming convention is used to prevent breaking kernel aware debuggers. */ { @@ -710,15 +716,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 ) */ + timerYIELD_WITHIN_API(); } else { From 3884ce656c819a35f22af4f0853a1da53d731b7a Mon Sep 17 00:00:00 2001 From: Ching-Hsin Lee Date: Wed, 6 Sep 2023 10:07:11 +0800 Subject: [PATCH 3/4] Revert "Add macro taskTASK_IS_RUNNING_OR_YIELDING" This reverts commit 8e898b7e5ccb609104d41563a76eed37cac29c5d. --- tasks.c | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/tasks.c b/tasks.c index 19ba098ebd..8b480a7e08 100644 --- a/tasks.c +++ b/tasks.c @@ -276,11 +276,9 @@ typedef BaseType_t TaskRunning_t; /* 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_YIELDING( pxTCB ) ( ( ( pxTCB ) == pxCurrentTCB ) ? ( pdTRUE ) : ( pdFALSE ) ) + #define taskTASK_IS_RUNNING( pxTCB ) ( ( ( pxTCB ) == pxCurrentTCB ) ? ( 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_YIELDING( pxTCB ) ( ( ( pxTCB )->xTaskRunState != taskTASK_NOT_RUNNING ) ? ( pdTRUE ) : ( pdFALSE ) ) + #define taskTASK_IS_RUNNING( pxTCB ) ( ( ( ( pxTCB )->xTaskRunState >= ( BaseType_t ) 0 ) && ( ( pxTCB )->xTaskRunState < ( BaseType_t ) configNUMBER_OF_CORES ) ) ? ( pdTRUE ) : ( pdFALSE ) ) #endif /* Indicates that the task is an Idle task. */ @@ -1927,7 +1925,11 @@ static void prvInitialiseNewTask( TaskFunction_t pxTaskCode, /* If the task is running (or yielding), we must add it to the * termination list so that an idle task can delete it when it is * no longer running. */ - if( taskTASK_IS_RUNNING_OR_YIELDING( pxTCB ) != pdFALSE ) + #if ( configNUMBER_OF_CORES == 1 ) + if( pxTCB == pxCurrentTCB ) + #else + if( pxTCB->xTaskRunState != taskTASK_NOT_RUNNING ) + #endif { /* A running task is being deleted. This cannot complete within the * task itself, as a context switch to another task is required. From ae48454de08d86d4fea4f6d8507c5ab15b65ce70 Mon Sep 17 00:00:00 2001 From: Ching-Hsin Lee Date: Wed, 6 Sep 2023 18:43:27 +0800 Subject: [PATCH 4/4] Use taskYIELD_WITHIN_API in all files --- event_groups.c | 10 ++-------- include/task.h | 6 ++++++ queue.c | 14 ++++---------- tasks.c | 6 ------ timers.c | 8 +------- 5 files changed, 13 insertions(+), 31 deletions(-) diff --git a/event_groups.c b/event_groups.c index 5b3f587926..45e304d480 100644 --- a/event_groups.c +++ b/event_groups.c @@ -46,12 +46,6 @@ * correct privileged Vs unprivileged linkage and placement. */ #undef MPU_WRAPPERS_INCLUDED_FROM_API_FILE /*lint !e961 !e750 !e9021 See comment above. */ -#if ( configNUMBER_OF_CORES == 1 ) - #define eventgroupYIELD_WITHIN_API() portYIELD_WITHIN_API() -#else /* #if ( configNUMBER_OF_CORES == 1 ) */ - #define eventgroupYIELD_WITHIN_API() vTaskYieldWithinAPI() -#endif /* #if ( configNUMBER_OF_CORES == 1 ) */ - typedef struct EventGroupDef_t { EventBits_t uxEventBits; @@ -249,7 +243,7 @@ EventBits_t xEventGroupSync( EventGroupHandle_t xEventGroup, { if( xAlreadyYielded == pdFALSE ) { - eventgroupYIELD_WITHIN_API(); + taskYIELD_WITHIN_API(); } else { @@ -401,7 +395,7 @@ EventBits_t xEventGroupWaitBits( EventGroupHandle_t xEventGroup, { if( xAlreadyYielded == pdFALSE ) { - eventgroupYIELD_WITHIN_API(); + taskYIELD_WITHIN_API(); } else { diff --git a/include/task.h b/include/task.h index 97809fc33c..1a97dd07bf 100644 --- a/include/task.h +++ b/include/task.h @@ -3206,6 +3206,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 00ce903c10..1f35acc189 100755 --- a/queue.c +++ b/queue.c @@ -96,12 +96,6 @@ typedef struct SemaphoreData #endif /* #if ( configNUMBER_OF_CORES == 1 ) */ #endif -#if ( configNUMBER_OF_CORES == 1 ) - #define queueYIELD_WITHIN_API() portYIELD_WITHIN_API() -#else /* #if ( configNUMBER_OF_CORES == 1 ) */ - #define queueYIELD_WITHIN_API() vTaskYieldWithinAPI() -#endif /* #if ( configNUMBER_OF_CORES == 1 ) */ - /* * Definition of the queue used by the scheduler. * Items are queued by copy, not reference. See the following link for the @@ -1080,7 +1074,7 @@ BaseType_t xQueueGenericSend( QueueHandle_t xQueue, * is also a higher priority task in the pending ready list. */ if( xTaskResumeAll() == pdFALSE ) { - queueYIELD_WITHIN_API(); + taskYIELD_WITHIN_API(); } } else @@ -1541,7 +1535,7 @@ BaseType_t xQueueReceive( QueueHandle_t xQueue, if( xTaskResumeAll() == pdFALSE ) { - queueYIELD_WITHIN_API(); + taskYIELD_WITHIN_API(); } else { @@ -1724,7 +1718,7 @@ BaseType_t xQueueSemaphoreTake( QueueHandle_t xQueue, if( xTaskResumeAll() == pdFALSE ) { - queueYIELD_WITHIN_API(); + taskYIELD_WITHIN_API(); } else { @@ -1902,7 +1896,7 @@ BaseType_t xQueuePeek( QueueHandle_t xQueue, if( xTaskResumeAll() == pdFALSE ) { - queueYIELD_WITHIN_API(); + taskYIELD_WITHIN_API(); } else { diff --git a/tasks.c b/tasks.c index 8b480a7e08..416c2b18b5 100644 --- a/tasks.c +++ b/tasks.c @@ -69,12 +69,6 @@ #endif #endif /* if ( configNUMBER_OF_CORES == 1 ) */ -#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 ) */ - /* 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. */ #define taskWAITING_NOTIFICATION ( ( uint8_t ) 1 ) diff --git a/timers.c b/timers.c index e3f0abe054..f5e06c273f 100644 --- a/timers.c +++ b/timers.c @@ -71,12 +71,6 @@ #define tmrSTATUS_IS_STATICALLY_ALLOCATED ( 0x02U ) #define tmrSTATUS_IS_AUTORELOAD ( 0x04U ) - #if ( configNUMBER_OF_CORES == 1 ) - #define timerYIELD_WITHIN_API() portYIELD_WITHIN_API() - #else /* #if ( configNUMBER_OF_CORES == 1 ) */ - #define timerYIELD_WITHIN_API() vTaskYieldWithinAPI() - #endif /* #if ( configNUMBER_OF_CORES == 1 ) */ - /* The definition of the timers themselves. */ typedef struct tmrTimerControl /* The old naming convention is used to prevent breaking kernel aware debuggers. */ { @@ -716,7 +710,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. */ - timerYIELD_WITHIN_API(); + taskYIELD_WITHIN_API(); } else {