From 8a6f98fee6c0d57e11f3bfe90381be906e66fabb Mon Sep 17 00:00:00 2001 From: Ching-Hsin Lee Date: Fri, 19 Jan 2024 16:42:24 +0800 Subject: [PATCH 1/6] Modify unpaired critical section for readability * Update the implementation for readability * Move prvDeleteTCB out of critical section for SMP --- tasks.c | 71 ++++++++++++++++++++++++++++----------------------------- 1 file changed, 35 insertions(+), 36 deletions(-) diff --git a/tasks.c b/tasks.c index 93e9c02c47..e2526b0f03 100644 --- a/tasks.c +++ b/tasks.c @@ -2183,6 +2183,7 @@ static void prvInitialiseNewTask( TaskFunction_t pxTaskCode, void vTaskDelete( TaskHandle_t xTaskToDelete ) { TCB_t * pxTCB; + BaseType_t xDeleteTaskInIdleTask = pdFALSE; traceENTER_vTaskDelete( xTaskToDelete ); @@ -2240,6 +2241,9 @@ static void prvInitialiseNewTask( TaskFunction_t pxTaskCode, * portPRE_TASK_DELETE_HOOK() does not return in the Win32 port. */ traceTASK_DELETE( pxTCB ); + /* Delete the task in idle task. */ + xDeleteTaskInIdleTask = pdTRUE; + /* The pre-delete hook is primarily for the Windows simulator, * in which Windows specific clean up operations are performed, * after which it is not possible to yield away from this task - @@ -2261,22 +2265,21 @@ static void prvInitialiseNewTask( TaskFunction_t pxTaskCode, prvResetNextTaskUnblockTime(); } } + taskEXIT_CRITICAL(); - #if ( configNUMBER_OF_CORES == 1 ) + /* If the task is not deleting itself, call prvDeleteTCB from outside of + * critical section. If a task deletes itself, prvDeleteTCB is called + * from prvCheckTasksWaitingTermination which is called from Idle task. */ + if( xDeleteTaskInIdleTask != pdTRUE ) { - taskEXIT_CRITICAL(); - - /* If the task is not deleting itself, call prvDeleteTCB from outside of - * critical section. If a task deletes itself, prvDeleteTCB is called - * from prvCheckTasksWaitingTermination which is called from Idle task. */ - if( pxTCB != pxCurrentTCB ) - { - prvDeleteTCB( pxTCB ); - } + prvDeleteTCB( pxTCB ); + } - /* Force a reschedule if it is the currently running task that has just - * been deleted. */ - if( xSchedulerRunning != pdFALSE ) + /* Force a reschedule if it is the currently running task that has just + * been deleted. */ + if( xSchedulerRunning != pdFALSE ) + { + #if ( configNUMBER_OF_CORES == 1 ) { if( pxTCB == pxCurrentTCB ) { @@ -2288,34 +2291,30 @@ static void prvInitialiseNewTask( TaskFunction_t pxTaskCode, mtCOVERAGE_TEST_MARKER(); } } - } - #else /* #if ( configNUMBER_OF_CORES == 1 ) */ - { - /* If a running task is not deleting itself, call prvDeleteTCB. If a running - * task deletes itself, prvDeleteTCB is called from prvCheckTasksWaitingTermination - * which is called from Idle task. */ - if( pxTCB->xTaskRunState == taskTASK_NOT_RUNNING ) - { - prvDeleteTCB( pxTCB ); - } - - /* Force a reschedule if the task that has just been deleted was running. */ - if( ( xSchedulerRunning != pdFALSE ) && ( taskTASK_IS_RUNNING( pxTCB ) == pdTRUE ) ) + #else /* #if ( configNUMBER_OF_CORES == 1 ) */ { - if( pxTCB->xTaskRunState == ( BaseType_t ) portGET_CORE_ID() ) - { - configASSERT( uxSchedulerSuspended == 0 ); - vTaskYieldWithinAPI(); - } - else + /* Rescheduling a running task is handled differently if it is running + * on other cores. Checking a task running core needs to be performed + * in critical section. */ + taskENTER_CRITICAL(); { - prvYieldCore( pxTCB->xTaskRunState ); + if( taskTASK_IS_RUNNING( pxTCB ) == pdTRUE ) + { + if( pxTCB->xTaskRunState == ( BaseType_t ) portGET_CORE_ID() ) + { + configASSERT( uxSchedulerSuspended == 0 ); + vTaskYieldWithinAPI(); + } + else + { + prvYieldCore( pxTCB->xTaskRunState ); + } + } } + taskEXIT_CRITICAL(); } - - taskEXIT_CRITICAL(); + #endif /* #if ( configNUMBER_OF_CORES == 1 ) */ } - #endif /* #if ( configNUMBER_OF_CORES == 1 ) */ traceRETURN_vTaskDelete(); } From f52cfc91eef2637dd2a3d80c5734949b561e3f25 Mon Sep 17 00:00:00 2001 From: chinglee-iot <61685396+chinglee-iot@users.noreply.github.com> Date: Fri, 19 Jan 2024 16:58:47 +0800 Subject: [PATCH 2/6] Update tasks.c comment --- tasks.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tasks.c b/tasks.c index 894cad9edc..4cff500f36 100644 --- a/tasks.c +++ b/tasks.c @@ -2301,8 +2301,8 @@ static void prvInitialiseNewTask( TaskFunction_t pxTaskCode, #else /* #if ( configNUMBER_OF_CORES == 1 ) */ { /* Rescheduling a running task is handled differently if it is running - * on other cores. Checking a task running core needs to be performed - * in critical section. */ + * on core other than current core. Checking a task running core needs + * to be performed in critical section. */ taskENTER_CRITICAL(); { if( taskTASK_IS_RUNNING( pxTCB ) == pdTRUE ) From 07804b8bd44255c11cba2eb1851beebe8d6b71ed Mon Sep 17 00:00:00 2001 From: chinglee-iot <61685396+chinglee-iot@users.noreply.github.com> Date: Fri, 19 Jan 2024 17:37:55 +0800 Subject: [PATCH 3/6] Update xDeleteTCBInIdleTask --- tasks.c | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/tasks.c b/tasks.c index 4cff500f36..83ccbbf702 100644 --- a/tasks.c +++ b/tasks.c @@ -2190,7 +2190,7 @@ static void prvInitialiseNewTask( TaskFunction_t pxTaskCode, void vTaskDelete( TaskHandle_t xTaskToDelete ) { TCB_t * pxTCB; - BaseType_t xDeleteTaskInIdleTask = pdFALSE; + BaseType_t xDeleteTCBInIdleTask = pdFALSE; traceENTER_vTaskDelete( xTaskToDelete ); @@ -2248,8 +2248,8 @@ static void prvInitialiseNewTask( TaskFunction_t pxTaskCode, * portPRE_TASK_DELETE_HOOK() does not return in the Win32 port. */ traceTASK_DELETE( pxTCB ); - /* Delete the task in idle task. */ - xDeleteTaskInIdleTask = pdTRUE; + /* Delete the task TCB in idle task. */ + xDeleteTCBInIdleTask = pdTRUE; /* The pre-delete hook is primarily for the Windows simulator, * in which Windows specific clean up operations are performed, @@ -2277,7 +2277,7 @@ static void prvInitialiseNewTask( TaskFunction_t pxTaskCode, /* If the task is not deleting itself, call prvDeleteTCB from outside of * critical section. If a task deletes itself, prvDeleteTCB is called * from prvCheckTasksWaitingTermination which is called from Idle task. */ - if( xDeleteTaskInIdleTask != pdTRUE ) + if( xDeleteTCBInIdleTask != pdTRUE ) { prvDeleteTCB( pxTCB ); } From a31a9a7a7e87b428ebaecaabdd00e58ff22f1b06 Mon Sep 17 00:00:00 2001 From: chinglee-iot <61685396+chinglee-iot@users.noreply.github.com> Date: Fri, 19 Jan 2024 23:39:10 +0800 Subject: [PATCH 4/6] Update tasks.c comment --- tasks.c | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/tasks.c b/tasks.c index 83ccbbf702..6d27f2f5e4 100644 --- a/tasks.c +++ b/tasks.c @@ -2291,7 +2291,7 @@ static void prvInitialiseNewTask( TaskFunction_t pxTaskCode, if( pxTCB == pxCurrentTCB ) { configASSERT( uxSchedulerSuspended == 0 ); - portYIELD_WITHIN_API(); + taskYIELD_WITHIN_API(); } else { @@ -2300,9 +2300,8 @@ static void prvInitialiseNewTask( TaskFunction_t pxTaskCode, } #else /* #if ( configNUMBER_OF_CORES == 1 ) */ { - /* Rescheduling a running task is handled differently if it is running - * on core other than current core. Checking a task running core needs - * to be performed in critical section. */ + /* Checking running state of a task needs to be performed in + * critical section. */ taskENTER_CRITICAL(); { if( taskTASK_IS_RUNNING( pxTCB ) == pdTRUE ) @@ -2310,7 +2309,7 @@ static void prvInitialiseNewTask( TaskFunction_t pxTaskCode, if( pxTCB->xTaskRunState == ( BaseType_t ) portGET_CORE_ID() ) { configASSERT( uxSchedulerSuspended == 0 ); - vTaskYieldWithinAPI(); + taskYIELD_WITHIN_API(); } else { From 6be9ad0c0ea73a7653be2ce8259cfc2a105b5af0 Mon Sep 17 00:00:00 2001 From: Gaurav Aggarwal Date: Tue, 23 Jan 2024 08:05:18 +0000 Subject: [PATCH 5/6] Minor comment update Signed-off-by: Gaurav Aggarwal --- tasks.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tasks.c b/tasks.c index 6d27f2f5e4..71ba99fe6c 100644 --- a/tasks.c +++ b/tasks.c @@ -2300,8 +2300,8 @@ static void prvInitialiseNewTask( TaskFunction_t pxTaskCode, } #else /* #if ( configNUMBER_OF_CORES == 1 ) */ { - /* Checking running state of a task needs to be performed in - * critical section. */ + /* It is important to use critical section here because checking + * run state of a task must be done inside a critical section. */ taskENTER_CRITICAL(); { if( taskTASK_IS_RUNNING( pxTCB ) == pdTRUE ) From 216ea3915caed42b5baaf6735a34d6f1a13fa7cd Mon Sep 17 00:00:00 2001 From: Ching-Hsin Lee Date: Tue, 23 Jan 2024 16:33:44 +0800 Subject: [PATCH 6/6] Update for format --- tasks.c | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/tasks.c b/tasks.c index 71ba99fe6c..4a640d611c 100644 --- a/tasks.c +++ b/tasks.c @@ -2300,8 +2300,9 @@ static void prvInitialiseNewTask( TaskFunction_t pxTaskCode, } #else /* #if ( configNUMBER_OF_CORES == 1 ) */ { - /* It is important to use critical section here because checking - * run state of a task must be done inside a critical section. */ + /* It is important to use critical section here because + * checking run state of a task must be done inside a + * critical section. */ taskENTER_CRITICAL(); { if( taskTASK_IS_RUNNING( pxTCB ) == pdTRUE )