Skip to content

Commit 31dd1e3

Browse files
felixvanoostchinglee-iotaggarg
authored
Pass core ID to critical nesting count macros (FreeRTOS#1206)
* Pass core ID to CRITICAL_NESTING_COUNT macros * Match existing data type for xCoreID * Get core ID when interrupts are disabled * Implement get core ID with interrupt disabled * Get core ID inline within vTaskSuspendAll() to resolve compiler warning * Fix formatting check Signed-off-by: Gaurav Aggarwal <aggarg@amazon.com> --------- Signed-off-by: Gaurav Aggarwal <aggarg@amazon.com> Co-authored-by: Ching-Hsin,Lee <chinglee@amazon.com> Co-authored-by: Gaurav Aggarwal <aggarg@amazon.com> Co-authored-by: Gaurav-Aggarwal-AWS <33462878+aggarg@users.noreply.github.com>
1 parent e169442 commit 31dd1e3

File tree

2 files changed

+85
-65
lines changed

2 files changed

+85
-65
lines changed

portable/ThirdParty/GCC/RP2040/include/portmacro.h

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -158,10 +158,10 @@ void vYieldCore( int xCoreID );
158158
#define portCRITICAL_NESTING_IN_TCB 0
159159

160160
extern UBaseType_t uxCriticalNestings[ configNUMBER_OF_CORES ];
161-
#define portGET_CRITICAL_NESTING_COUNT() ( uxCriticalNestings[ portGET_CORE_ID() ] )
162-
#define portSET_CRITICAL_NESTING_COUNT( x ) ( uxCriticalNestings[ portGET_CORE_ID() ] = ( x ) )
163-
#define portINCREMENT_CRITICAL_NESTING_COUNT() ( uxCriticalNestings[ portGET_CORE_ID() ]++ )
164-
#define portDECREMENT_CRITICAL_NESTING_COUNT() ( uxCriticalNestings[ portGET_CORE_ID() ]-- )
161+
#define portGET_CRITICAL_NESTING_COUNT( xCoreID ) ( uxCriticalNestings[ ( xCoreID ) ] )
162+
#define portSET_CRITICAL_NESTING_COUNT( xCoreID, x ) ( uxCriticalNestings[ ( xCoreID ) ] = ( x ) )
163+
#define portINCREMENT_CRITICAL_NESTING_COUNT( xCoreID ) ( uxCriticalNestings[ ( xCoreID ) ]++ )
164+
#define portDECREMENT_CRITICAL_NESTING_COUNT( xCoreID ) ( uxCriticalNestings[ ( xCoreID ) ]-- )
165165

166166
/*-----------------------------------------------------------*/
167167

tasks.c

Lines changed: 81 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -317,10 +317,10 @@
317317
#define taskATTRIBUTE_IS_IDLE ( UBaseType_t ) ( 1U << 0U )
318318

319319
#if ( ( configNUMBER_OF_CORES > 1 ) && ( portCRITICAL_NESTING_IN_TCB == 1 ) )
320-
#define portGET_CRITICAL_NESTING_COUNT() ( pxCurrentTCBs[ portGET_CORE_ID() ]->uxCriticalNesting )
321-
#define portSET_CRITICAL_NESTING_COUNT( x ) ( pxCurrentTCBs[ portGET_CORE_ID() ]->uxCriticalNesting = ( x ) )
322-
#define portINCREMENT_CRITICAL_NESTING_COUNT() ( pxCurrentTCBs[ portGET_CORE_ID() ]->uxCriticalNesting++ )
323-
#define portDECREMENT_CRITICAL_NESTING_COUNT() ( pxCurrentTCBs[ portGET_CORE_ID() ]->uxCriticalNesting-- )
320+
#define portGET_CRITICAL_NESTING_COUNT( xCoreID ) ( pxCurrentTCBs[ ( xCoreID ) ]->uxCriticalNesting )
321+
#define portSET_CRITICAL_NESTING_COUNT( xCoreID, x ) ( pxCurrentTCBs[ ( xCoreID ) ]->uxCriticalNesting = ( x ) )
322+
#define portINCREMENT_CRITICAL_NESTING_COUNT( xCoreID ) ( pxCurrentTCBs[ ( xCoreID ) ]->uxCriticalNesting++ )
323+
#define portDECREMENT_CRITICAL_NESTING_COUNT( xCoreID ) ( pxCurrentTCBs[ ( xCoreID ) ]->uxCriticalNesting-- )
324324
#endif /* #if ( ( configNUMBER_OF_CORES > 1 ) && ( portCRITICAL_NESTING_IN_TCB == 1 ) ) */
325325

326326
#define taskBITS_PER_BYTE ( ( size_t ) 8 )
@@ -807,13 +807,14 @@ static void prvAddNewTaskToReadyList( TCB_t * pxNewTCB ) PRIVILEGED_FUNCTION;
807807
{
808808
UBaseType_t uxPrevCriticalNesting;
809809
const TCB_t * pxThisTCB;
810+
BaseType_t xCoreID = ( BaseType_t ) portGET_CORE_ID();
810811

811812
/* This must only be called from within a task. */
812813
portASSERT_IF_IN_ISR();
813814

814815
/* This function is always called with interrupts disabled
815816
* so this is safe. */
816-
pxThisTCB = pxCurrentTCBs[ portGET_CORE_ID() ];
817+
pxThisTCB = pxCurrentTCBs[ xCoreID ];
817818

818819
while( pxThisTCB->xTaskRunState == taskTASK_SCHEDULED_TO_YIELD )
819820
{
@@ -825,11 +826,11 @@ static void prvAddNewTaskToReadyList( TCB_t * pxNewTCB ) PRIVILEGED_FUNCTION;
825826
* the suspension and critical nesting counts, as well as release
826827
* and reacquire the correct locks. And then, do it all over again
827828
* if our state changed again during the reacquisition. */
828-
uxPrevCriticalNesting = portGET_CRITICAL_NESTING_COUNT();
829+
uxPrevCriticalNesting = portGET_CRITICAL_NESTING_COUNT( xCoreID );
829830

830831
if( uxPrevCriticalNesting > 0U )
831832
{
832-
portSET_CRITICAL_NESTING_COUNT( 0U );
833+
portSET_CRITICAL_NESTING_COUNT( xCoreID, 0U );
833834
portRELEASE_ISR_LOCK();
834835
}
835836
else
@@ -854,8 +855,9 @@ static void prvAddNewTaskToReadyList( TCB_t * pxNewTCB ) PRIVILEGED_FUNCTION;
854855
portDISABLE_INTERRUPTS();
855856
portGET_TASK_LOCK();
856857
portGET_ISR_LOCK();
858+
xCoreID = ( BaseType_t ) portGET_CORE_ID();
857859

858-
portSET_CRITICAL_NESTING_COUNT( uxPrevCriticalNesting );
860+
portSET_CRITICAL_NESTING_COUNT( xCoreID, uxPrevCriticalNesting );
859861

860862
if( uxPrevCriticalNesting == 0U )
861863
{
@@ -874,13 +876,14 @@ static void prvAddNewTaskToReadyList( TCB_t * pxNewTCB ) PRIVILEGED_FUNCTION;
874876
BaseType_t xCurrentCoreTaskPriority;
875877
BaseType_t xLowestPriorityCore = ( BaseType_t ) -1;
876878
BaseType_t xCoreID;
879+
const BaseType_t xCurrentCoreID = portGET_CORE_ID();
877880

878881
#if ( configRUN_MULTIPLE_PRIORITIES == 0 )
879882
BaseType_t xYieldCount = 0;
880883
#endif /* #if ( configRUN_MULTIPLE_PRIORITIES == 0 ) */
881884

882885
/* This must be called from a critical section. */
883-
configASSERT( portGET_CRITICAL_NESTING_COUNT() > 0U );
886+
configASSERT( portGET_CRITICAL_NESTING_COUNT( xCurrentCoreID ) > 0U );
884887

885888
#if ( configRUN_MULTIPLE_PRIORITIES == 0 )
886889

@@ -969,11 +972,11 @@ static void prvAddNewTaskToReadyList( TCB_t * pxNewTCB ) PRIVILEGED_FUNCTION;
969972

970973
#if ( configRUN_MULTIPLE_PRIORITIES == 0 )
971974
/* Verify that the calling core always yields to higher priority tasks. */
972-
if( ( ( pxCurrentTCBs[ portGET_CORE_ID() ]->uxTaskAttributes & taskATTRIBUTE_IS_IDLE ) == 0U ) &&
973-
( pxTCB->uxPriority > pxCurrentTCBs[ portGET_CORE_ID() ]->uxPriority ) )
975+
if( ( ( pxCurrentTCBs[ xCurrentCoreID ]->uxTaskAttributes & taskATTRIBUTE_IS_IDLE ) == 0U ) &&
976+
( pxTCB->uxPriority > pxCurrentTCBs[ xCurrentCoreID ]->uxPriority ) )
974977
{
975-
configASSERT( ( xYieldPendings[ portGET_CORE_ID() ] == pdTRUE ) ||
976-
( taskTASK_IS_RUNNING( pxCurrentTCBs[ portGET_CORE_ID() ] ) == pdFALSE ) );
978+
configASSERT( ( xYieldPendings[ xCurrentCoreID ] == pdTRUE ) ||
979+
( taskTASK_IS_RUNNING( pxCurrentTCBs[ xCurrentCoreID ] ) == pdFALSE ) );
977980
}
978981
#endif
979982
}
@@ -3880,7 +3883,7 @@ void vTaskSuspendAll( void )
38803883
ulState = portSET_INTERRUPT_MASK();
38813884

38823885
/* This must never be called from inside a critical section. */
3883-
configASSERT( portGET_CRITICAL_NESTING_COUNT() == 0 );
3886+
configASSERT( portGET_CRITICAL_NESTING_COUNT( portGET_CORE_ID() ) == 0 );
38843887

38853888
/* portSOFTWARE_BARRIER() is only implemented for emulated/simulated ports that
38863889
* do not otherwise exhibit real time behaviour. */
@@ -4003,8 +4006,7 @@ BaseType_t xTaskResumeAll( void )
40034006
* tasks from this list into their appropriate ready list. */
40044007
taskENTER_CRITICAL();
40054008
{
4006-
BaseType_t xCoreID;
4007-
xCoreID = ( BaseType_t ) portGET_CORE_ID();
4009+
const BaseType_t xCoreID = ( BaseType_t ) portGET_CORE_ID();
40084010

40094011
/* If uxSchedulerSuspended is zero then this function does not match a
40104012
* previous call to vTaskSuspendAll(). */
@@ -5187,7 +5189,7 @@ BaseType_t xTaskIncrementTick( void )
51875189
/* vTaskSwitchContext() must never be called from within a critical section.
51885190
* This is not necessarily true for single core FreeRTOS, but it is for this
51895191
* SMP port. */
5190-
configASSERT( portGET_CRITICAL_NESTING_COUNT() == 0 );
5192+
configASSERT( portGET_CRITICAL_NESTING_COUNT( xCoreID ) == 0 );
51915193

51925194
if( uxSchedulerSuspended != ( UBaseType_t ) 0U )
51935195
{
@@ -6937,16 +6939,24 @@ static void prvResetNextTaskUnblockTime( void )
69376939
*/
69386940
void vTaskYieldWithinAPI( void )
69396941
{
6942+
UBaseType_t ulState;
6943+
69406944
traceENTER_vTaskYieldWithinAPI();
69416945

6942-
if( portGET_CRITICAL_NESTING_COUNT() == 0U )
6943-
{
6944-
portYIELD();
6945-
}
6946-
else
6946+
ulState = portSET_INTERRUPT_MASK();
69476947
{
6948-
xYieldPendings[ portGET_CORE_ID() ] = pdTRUE;
6948+
const BaseType_t xCoreID = ( BaseType_t ) portGET_CORE_ID();
6949+
6950+
if( portGET_CRITICAL_NESTING_COUNT( xCoreID ) == 0U )
6951+
{
6952+
portYIELD();
6953+
}
6954+
else
6955+
{
6956+
xYieldPendings[ xCoreID ] = pdTRUE;
6957+
}
69496958
}
6959+
portCLEAR_INTERRUPT_MASK( ulState );
69506960

69516961
traceRETURN_vTaskYieldWithinAPI();
69526962
}
@@ -6995,40 +7005,43 @@ static void prvResetNextTaskUnblockTime( void )
69957005
traceENTER_vTaskEnterCritical();
69967006

69977007
portDISABLE_INTERRUPTS();
6998-
6999-
if( xSchedulerRunning != pdFALSE )
70007008
{
7001-
if( portGET_CRITICAL_NESTING_COUNT() == 0U )
7002-
{
7003-
portGET_TASK_LOCK();
7004-
portGET_ISR_LOCK();
7005-
}
7006-
7007-
portINCREMENT_CRITICAL_NESTING_COUNT();
7009+
const BaseType_t xCoreID = ( BaseType_t ) portGET_CORE_ID();
70087010

7009-
/* This is not the interrupt safe version of the enter critical
7010-
* function so assert() if it is being called from an interrupt
7011-
* context. Only API functions that end in "FromISR" can be used in an
7012-
* interrupt. Only assert if the critical nesting count is 1 to
7013-
* protect against recursive calls if the assert function also uses a
7014-
* critical section. */
7015-
if( portGET_CRITICAL_NESTING_COUNT() == 1U )
7011+
if( xSchedulerRunning != pdFALSE )
70167012
{
7017-
portASSERT_IF_IN_ISR();
7013+
if( portGET_CRITICAL_NESTING_COUNT( xCoreID ) == 0U )
7014+
{
7015+
portGET_TASK_LOCK();
7016+
portGET_ISR_LOCK();
7017+
}
70187018

7019-
if( uxSchedulerSuspended == 0U )
7019+
portINCREMENT_CRITICAL_NESTING_COUNT( xCoreID );
7020+
7021+
/* This is not the interrupt safe version of the enter critical
7022+
* function so assert() if it is being called from an interrupt
7023+
* context. Only API functions that end in "FromISR" can be used in an
7024+
* interrupt. Only assert if the critical nesting count is 1 to
7025+
* protect against recursive calls if the assert function also uses a
7026+
* critical section. */
7027+
if( portGET_CRITICAL_NESTING_COUNT( xCoreID ) == 1U )
70207028
{
7021-
/* The only time there would be a problem is if this is called
7022-
* before a context switch and vTaskExitCritical() is called
7023-
* after pxCurrentTCB changes. Therefore this should not be
7024-
* used within vTaskSwitchContext(). */
7025-
prvCheckForRunStateChange();
7029+
portASSERT_IF_IN_ISR();
7030+
7031+
if( uxSchedulerSuspended == 0U )
7032+
{
7033+
/* The only time there would be a problem is if this is called
7034+
* before a context switch and vTaskExitCritical() is called
7035+
* after pxCurrentTCB changes. Therefore this should not be
7036+
* used within vTaskSwitchContext(). */
7037+
prvCheckForRunStateChange();
7038+
}
70267039
}
70277040
}
7028-
}
7029-
else
7030-
{
7031-
mtCOVERAGE_TEST_MARKER();
7041+
else
7042+
{
7043+
mtCOVERAGE_TEST_MARKER();
7044+
}
70327045
}
70337046

70347047
traceRETURN_vTaskEnterCritical();
@@ -7043,19 +7056,20 @@ static void prvResetNextTaskUnblockTime( void )
70437056
UBaseType_t vTaskEnterCriticalFromISR( void )
70447057
{
70457058
UBaseType_t uxSavedInterruptStatus = 0;
7059+
const BaseType_t xCoreID = ( BaseType_t ) portGET_CORE_ID();
70467060

70477061
traceENTER_vTaskEnterCriticalFromISR();
70487062

70497063
if( xSchedulerRunning != pdFALSE )
70507064
{
70517065
uxSavedInterruptStatus = portSET_INTERRUPT_MASK_FROM_ISR();
70527066

7053-
if( portGET_CRITICAL_NESTING_COUNT() == 0U )
7067+
if( portGET_CRITICAL_NESTING_COUNT( xCoreID ) == 0U )
70547068
{
70557069
portGET_ISR_LOCK();
70567070
}
70577071

7058-
portINCREMENT_CRITICAL_NESTING_COUNT();
7072+
portINCREMENT_CRITICAL_NESTING_COUNT( xCoreID );
70597073
}
70607074
else
70617075
{
@@ -7119,28 +7133,30 @@ static void prvResetNextTaskUnblockTime( void )
71197133

71207134
void vTaskExitCritical( void )
71217135
{
7136+
const BaseType_t xCoreID = ( BaseType_t ) portGET_CORE_ID();
7137+
71227138
traceENTER_vTaskExitCritical();
71237139

71247140
if( xSchedulerRunning != pdFALSE )
71257141
{
71267142
/* If critical nesting count is zero then this function
71277143
* does not match a previous call to vTaskEnterCritical(). */
7128-
configASSERT( portGET_CRITICAL_NESTING_COUNT() > 0U );
7144+
configASSERT( portGET_CRITICAL_NESTING_COUNT( xCoreID ) > 0U );
71297145

71307146
/* This function should not be called in ISR. Use vTaskExitCriticalFromISR
71317147
* to exit critical section from ISR. */
71327148
portASSERT_IF_IN_ISR();
71337149

7134-
if( portGET_CRITICAL_NESTING_COUNT() > 0U )
7150+
if( portGET_CRITICAL_NESTING_COUNT( xCoreID ) > 0U )
71357151
{
7136-
portDECREMENT_CRITICAL_NESTING_COUNT();
7152+
portDECREMENT_CRITICAL_NESTING_COUNT( xCoreID );
71377153

7138-
if( portGET_CRITICAL_NESTING_COUNT() == 0U )
7154+
if( portGET_CRITICAL_NESTING_COUNT( xCoreID ) == 0U )
71397155
{
71407156
BaseType_t xYieldCurrentTask;
71417157

71427158
/* Get the xYieldPending stats inside the critical section. */
7143-
xYieldCurrentTask = xYieldPendings[ portGET_CORE_ID() ];
7159+
xYieldCurrentTask = xYieldPendings[ xCoreID ];
71447160

71457161
portRELEASE_ISR_LOCK();
71467162
portRELEASE_TASK_LOCK();
@@ -7180,19 +7196,23 @@ static void prvResetNextTaskUnblockTime( void )
71807196

71817197
void vTaskExitCriticalFromISR( UBaseType_t uxSavedInterruptStatus )
71827198
{
7199+
BaseType_t xCoreID;
7200+
71837201
traceENTER_vTaskExitCriticalFromISR( uxSavedInterruptStatus );
71847202

71857203
if( xSchedulerRunning != pdFALSE )
71867204
{
7205+
xCoreID = ( BaseType_t ) portGET_CORE_ID();
7206+
71877207
/* If critical nesting count is zero then this function
71887208
* does not match a previous call to vTaskEnterCritical(). */
7189-
configASSERT( portGET_CRITICAL_NESTING_COUNT() > 0U );
7209+
configASSERT( portGET_CRITICAL_NESTING_COUNT( xCoreID ) > 0U );
71907210

7191-
if( portGET_CRITICAL_NESTING_COUNT() > 0U )
7211+
if( portGET_CRITICAL_NESTING_COUNT( xCoreID ) > 0U )
71927212
{
7193-
portDECREMENT_CRITICAL_NESTING_COUNT();
7213+
portDECREMENT_CRITICAL_NESTING_COUNT( xCoreID );
71947214

7195-
if( portGET_CRITICAL_NESTING_COUNT() == 0U )
7215+
if( portGET_CRITICAL_NESTING_COUNT( xCoreID ) == 0U )
71967216
{
71977217
portRELEASE_ISR_LOCK();
71987218
portCLEAR_INTERRUPT_MASK_FROM_ISR( uxSavedInterruptStatus );

0 commit comments

Comments
 (0)