From 72c7d862762425832d9fce2df7b03d2ad355cb1e Mon Sep 17 00:00:00 2001 From: chinglee-iot <61685396+chinglee-iot@users.noreply.github.com> Date: Wed, 24 Jan 2024 19:48:31 +0800 Subject: Update for unpaired critical section in vTaskSuspend (#959) * Move the taskEXIT_CRITICAL out of the configNUMBER_OF_CORES macro block to improve readability. --- tasks.c | 85 ++++++++++++++++++++++++++--------------------------------------- 1 file changed, 34 insertions(+), 51 deletions(-) diff --git a/tasks.c b/tasks.c index 4a640d611..26a442637 100644 --- a/tasks.c +++ b/tasks.c @@ -3107,10 +3107,6 @@ static void prvInitialiseNewTask( TaskFunction_t pxTaskCode, { TCB_t * pxTCB; - #if ( configNUMBER_OF_CORES > 1 ) - BaseType_t xTaskRunningOnCore; - #endif - traceENTER_vTaskSuspend( xTaskToSuspend ); taskENTER_CRITICAL(); @@ -3121,10 +3117,6 @@ static void prvInitialiseNewTask( TaskFunction_t pxTaskCode, traceTASK_SUSPEND( pxTCB ); - #if ( configNUMBER_OF_CORES > 1 ) - xTaskRunningOnCore = pxTCB->xTaskRunState; - #endif - /* Remove task from the ready/delayed list and place in the * suspended list. */ if( uxListRemove( &( pxTCB->xStateListItem ) ) == ( UBaseType_t ) 0 ) @@ -3164,26 +3156,25 @@ static void prvInitialiseNewTask( TaskFunction_t pxTaskCode, } #endif /* if ( configUSE_TASK_NOTIFICATIONS == 1 ) */ } + taskEXIT_CRITICAL(); - #if ( configNUMBER_OF_CORES == 1 ) + if( xSchedulerRunning != pdFALSE ) { - taskEXIT_CRITICAL(); - - if( xSchedulerRunning != pdFALSE ) - { - /* Reset the next expected unblock time in case it referred to the - * task that is now in the Suspended state. */ - taskENTER_CRITICAL(); - { - prvResetNextTaskUnblockTime(); - } - taskEXIT_CRITICAL(); - } - else + /* Reset the next expected unblock time in case it referred to the + * task that is now in the Suspended state. */ + taskENTER_CRITICAL(); { - mtCOVERAGE_TEST_MARKER(); + prvResetNextTaskUnblockTime(); } + taskEXIT_CRITICAL(); + } + else + { + mtCOVERAGE_TEST_MARKER(); + } + #if ( configNUMBER_OF_CORES == 1 ) + { if( pxTCB == pxCurrentTCB ) { if( xSchedulerRunning != pdFALSE ) @@ -3218,47 +3209,39 @@ static void prvInitialiseNewTask( TaskFunction_t pxTaskCode, } #else /* #if ( configNUMBER_OF_CORES == 1 ) */ { - if( xSchedulerRunning != pdFALSE ) - { - /* Reset the next expected unblock time in case it referred to the - * task that is now in the Suspended state. */ - prvResetNextTaskUnblockTime(); - } - else - { - mtCOVERAGE_TEST_MARKER(); - } - - if( taskTASK_IS_RUNNING( pxTCB ) == pdTRUE ) + /* Enter critical section here to check run state of a task. */ + taskENTER_CRITICAL(); { - if( xSchedulerRunning != pdFALSE ) + if( taskTASK_IS_RUNNING( pxTCB ) == pdTRUE ) { - if( xTaskRunningOnCore == ( BaseType_t ) portGET_CORE_ID() ) + if( xSchedulerRunning != pdFALSE ) { - /* The current task has just been suspended. */ - configASSERT( uxSchedulerSuspended == 0 ); - vTaskYieldWithinAPI(); + if( pxTCB->xTaskRunState == ( BaseType_t ) portGET_CORE_ID() ) + { + /* The current task has just been suspended. */ + configASSERT( uxSchedulerSuspended == 0 ); + vTaskYieldWithinAPI(); + } + else + { + prvYieldCore( pxTCB->xTaskRunState ); + } } else { - prvYieldCore( xTaskRunningOnCore ); + /* This code path is not possible because only Idle tasks are + * assigned a core before the scheduler is started ( i.e. + * taskTASK_IS_RUNNING is only true for idle tasks before + * the scheduler is started ) and idle tasks cannot be + * suspended. */ + mtCOVERAGE_TEST_MARKER(); } } else { - /* This code path is not possible because only Idle tasks are - * assigned a core before the scheduler is started ( i.e. - * taskTASK_IS_RUNNING is only true for idle tasks before - * the scheduler is started ) and idle tasks cannot be - * suspended. */ mtCOVERAGE_TEST_MARKER(); } } - else - { - mtCOVERAGE_TEST_MARKER(); - } - taskEXIT_CRITICAL(); } #endif /* #if ( configNUMBER_OF_CORES == 1 ) */ -- cgit v1.2.3