Skip to content

Commit f05244a

Browse files
authored
Pass core ID to port lock macros (FreeRTOS#1212)
Pass core ID to task/ISR lock functions
1 parent f63bc2b commit f05244a

File tree

7 files changed

+71
-63
lines changed

7 files changed

+71
-63
lines changed

include/FreeRTOS.h

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -445,7 +445,7 @@
445445
#ifndef portRELEASE_TASK_LOCK
446446

447447
#if ( configNUMBER_OF_CORES == 1 )
448-
#define portRELEASE_TASK_LOCK()
448+
#define portRELEASE_TASK_LOCK( xCoreID )
449449
#else
450450
#error portRELEASE_TASK_LOCK is required in SMP
451451
#endif
@@ -455,7 +455,7 @@
455455
#ifndef portGET_TASK_LOCK
456456

457457
#if ( configNUMBER_OF_CORES == 1 )
458-
#define portGET_TASK_LOCK()
458+
#define portGET_TASK_LOCK( xCoreID )
459459
#else
460460
#error portGET_TASK_LOCK is required in SMP
461461
#endif
@@ -465,7 +465,7 @@
465465
#ifndef portRELEASE_ISR_LOCK
466466

467467
#if ( configNUMBER_OF_CORES == 1 )
468-
#define portRELEASE_ISR_LOCK()
468+
#define portRELEASE_ISR_LOCK( xCoreID )
469469
#else
470470
#error portRELEASE_ISR_LOCK is required in SMP
471471
#endif
@@ -475,7 +475,7 @@
475475
#ifndef portGET_ISR_LOCK
476476

477477
#if ( configNUMBER_OF_CORES == 1 )
478-
#define portGET_ISR_LOCK()
478+
#define portGET_ISR_LOCK( xCoreID )
479479
#else
480480
#error portGET_ISR_LOCK is required in SMP
481481
#endif

portable/CCRH/F1Kx/port.c

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -258,8 +258,8 @@ void vPortTickISR( void );
258258
* already had lock can acquire lock without waiting. This function could be
259259
* call from task and interrupt context, the critical section is called
260260
* as in ISR */
261-
void vPortRecursiveLockAcquire( BaseType_t xFromIsr );
262-
void vPortRecursiveLockRelease( BaseType_t xFromIsr );
261+
void vPortRecursiveLockAcquire( BaseType_t xCoreID, BaseType_t xFromIsr );
262+
void vPortRecursiveLockRelease( BaseType_t xCoreID, BaseType_t xFromIsr );
263263

264264
#endif /* (configNUMBER_OF_CORES > 1) */
265265

@@ -688,10 +688,9 @@ static void prvSetupTimerInterrupt( void )
688688
}
689689

690690
/*-----------------------------------------------------------*/
691-
void vPortRecursiveLockAcquire( BaseType_t xFromIsr )
691+
void vPortRecursiveLockAcquire( BaseType_t xCoreID, BaseType_t xFromIsr )
692692
{
693693
BaseType_t xSavedInterruptStatus;
694-
BaseType_t xCoreID = xPortGET_CORE_ID();
695694
BaseType_t xBitPosition = ( xFromIsr == pdTRUE );
696695

697696
xSavedInterruptStatus = portSET_INTERRUPT_MASK_FROM_ISR();
@@ -705,10 +704,9 @@ static void prvSetupTimerInterrupt( void )
705704
portCLEAR_INTERRUPT_MASK_FROM_ISR( xSavedInterruptStatus );
706705
}
707706

708-
void vPortRecursiveLockRelease( BaseType_t xFromIsr )
707+
void vPortRecursiveLockRelease( BaseType_t xCoreID, BaseType_t xFromIsr )
709708
{
710709
BaseType_t xSavedInterruptStatus;
711-
BaseType_t xCoreID = xPortGET_CORE_ID();
712710
BaseType_t xBitPosition = ( xFromIsr == pdTRUE );
713711

714712
xSavedInterruptStatus = portSET_INTERRUPT_MASK_FROM_ISR();

portable/CCRH/F1Kx/portmacro.h

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -141,18 +141,18 @@
141141
#endif /* if ( configNUMBER_OF_CORES > 1 ) */
142142

143143
#if ( configNUMBER_OF_CORES == 1 )
144-
#define portGET_ISR_LOCK()
145-
#define portRELEASE_ISR_LOCK()
146-
#define portGET_TASK_LOCK()
147-
#define portRELEASE_TASK_LOCK()
144+
#define portGET_ISR_LOCK( xCoreID )
145+
#define portRELEASE_ISR_LOCK( xCoreID )
146+
#define portGET_TASK_LOCK( xCoreID )
147+
#define portRELEASE_TASK_LOCK( xCoreID )
148148
#else
149-
extern void vPortRecursiveLockAcquire( BaseType_t xFromIsr );
150-
extern void vPortRecursiveLockRelease( BaseType_t xFromIsr );
149+
extern void vPortRecursiveLockAcquire( BaseType_t xCoreID, BaseType_t xFromIsr );
150+
extern void vPortRecursiveLockRelease( BaseType_t xCoreID, BaseType_t xFromIsr );
151151

152-
#define portGET_ISR_LOCK() vPortRecursiveLockAcquire( pdTRUE )
153-
#define portRELEASE_ISR_LOCK() vPortRecursiveLockRelease( pdTRUE )
154-
#define portGET_TASK_LOCK() vPortRecursiveLockAcquire( pdFALSE )
155-
#define portRELEASE_TASK_LOCK() vPortRecursiveLockRelease( pdFALSE )
152+
#define portGET_ISR_LOCK( xCoreID ) vPortRecursiveLockAcquire( ( xCoreID ), pdTRUE )
153+
#define portRELEASE_ISR_LOCK( xCoreID ) vPortRecursiveLockRelease( ( xCoreID ), pdTRUE )
154+
#define portGET_TASK_LOCK( xCoreID ) vPortRecursiveLockAcquire( ( xCoreID ), pdFALSE )
155+
#define portRELEASE_TASK_LOCK( xCoreID ) vPortRecursiveLockRelease( ( xCoreID ), pdFALSE )
156156
#endif /* if ( configNUMBER_OF_CORES == 1 ) */
157157

158158
/*-----------------------------------------------------------*/

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

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -210,21 +210,21 @@ __force_inline static bool spin_try_lock_unsafe(spin_lock_t *lock) {
210210

211211
/* Note this is a single method with uxAcquire parameter since we have
212212
* static vars, the method is always called with a compile time constant for
213-
* uxAcquire, and the compiler should dothe right thing! */
214-
static inline void vPortRecursiveLock( uint32_t ulLockNum,
213+
* uxAcquire, and the compiler should do the right thing! */
214+
static inline void vPortRecursiveLock( BaseType_t xCoreID,
215+
uint32_t ulLockNum,
215216
spin_lock_t * pxSpinLock,
216217
BaseType_t uxAcquire )
217218
{
218219
static volatile uint8_t ucOwnedByCore[ portMAX_CORE_COUNT ][portRTOS_SPINLOCK_COUNT];
219220
static volatile uint8_t ucRecursionCountByLock[ portRTOS_SPINLOCK_COUNT ];
220221

221222
configASSERT( ulLockNum < portRTOS_SPINLOCK_COUNT );
222-
uint32_t ulCoreNum = get_core_num();
223223

224224
if( uxAcquire )
225225
{
226226
if (!spin_try_lock_unsafe(pxSpinLock)) {
227-
if( ucOwnedByCore[ ulCoreNum ][ ulLockNum ] )
227+
if( ucOwnedByCore[ xCoreID ][ ulLockNum ] )
228228
{
229229
configASSERT( ucRecursionCountByLock[ ulLockNum ] != 255u );
230230
ucRecursionCountByLock[ ulLockNum ]++;
@@ -234,31 +234,31 @@ static inline void vPortRecursiveLock( uint32_t ulLockNum,
234234
}
235235
configASSERT( ucRecursionCountByLock[ ulLockNum ] == 0 );
236236
ucRecursionCountByLock[ ulLockNum ] = 1;
237-
ucOwnedByCore[ ulCoreNum ][ ulLockNum ] = 1;
237+
ucOwnedByCore[ xCoreID ][ ulLockNum ] = 1;
238238
}
239239
else
240240
{
241-
configASSERT( ( ucOwnedByCore[ ulCoreNum ] [ulLockNum ] ) != 0 );
241+
configASSERT( ( ucOwnedByCore[ xCoreID ] [ulLockNum ] ) != 0 );
242242
configASSERT( ucRecursionCountByLock[ ulLockNum ] != 0 );
243243

244244
if( !--ucRecursionCountByLock[ ulLockNum ] )
245245
{
246-
ucOwnedByCore[ ulCoreNum ] [ ulLockNum ] = 0;
246+
ucOwnedByCore[ xCoreID ] [ ulLockNum ] = 0;
247247
spin_unlock_unsafe(pxSpinLock);
248248
}
249249
}
250250
}
251251

252252
#if ( configNUMBER_OF_CORES == 1 )
253-
#define portGET_ISR_LOCK()
254-
#define portRELEASE_ISR_LOCK()
255-
#define portGET_TASK_LOCK()
256-
#define portRELEASE_TASK_LOCK()
253+
#define portGET_ISR_LOCK( xCoreID )
254+
#define portRELEASE_ISR_LOCK( xCoreID )
255+
#define portGET_TASK_LOCK( xCoreID )
256+
#define portRELEASE_TASK_LOCK( xCoreID )
257257
#else
258-
#define portGET_ISR_LOCK() vPortRecursiveLock( 0, spin_lock_instance( configSMP_SPINLOCK_0 ), pdTRUE )
259-
#define portRELEASE_ISR_LOCK() vPortRecursiveLock( 0, spin_lock_instance( configSMP_SPINLOCK_0 ), pdFALSE )
260-
#define portGET_TASK_LOCK() vPortRecursiveLock( 1, spin_lock_instance( configSMP_SPINLOCK_1 ), pdTRUE )
261-
#define portRELEASE_TASK_LOCK() vPortRecursiveLock( 1, spin_lock_instance( configSMP_SPINLOCK_1 ), pdFALSE )
258+
#define portGET_ISR_LOCK( xCoreID ) vPortRecursiveLock( ( xCoreID ), 0, spin_lock_instance( configSMP_SPINLOCK_0 ), pdTRUE )
259+
#define portRELEASE_ISR_LOCK( xCoreID ) vPortRecursiveLock( ( xCoreID ), 0, spin_lock_instance( configSMP_SPINLOCK_0 ), pdFALSE )
260+
#define portGET_TASK_LOCK( xCoreID ) vPortRecursiveLock( ( xCoreID ), 1, spin_lock_instance( configSMP_SPINLOCK_1 ), pdTRUE )
261+
#define portRELEASE_TASK_LOCK( xCoreID ) vPortRecursiveLock( ( xCoreID ), 1, spin_lock_instance( configSMP_SPINLOCK_1 ), pdFALSE )
262262
#endif
263263

264264
/*-----------------------------------------------------------*/

portable/ThirdParty/xClang/XCOREAI/portmacro.h

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -152,10 +152,11 @@
152152

153153
#define portASSERT_IF_IN_ISR() configASSERT( portCHECK_IF_IN_ISR() == 0 )
154154

155-
#define portGET_ISR_LOCK() rtos_lock_acquire( 0 )
156-
#define portRELEASE_ISR_LOCK() rtos_lock_release( 0 )
157-
#define portGET_TASK_LOCK() rtos_lock_acquire( 1 )
158-
#define portRELEASE_TASK_LOCK() rtos_lock_release( 1 )
155+
#define portGET_ISR_LOCK( xCoreID ) do{ ( void )( xCoreID ); rtos_lock_acquire( 0 ); } while( 0 )
156+
#define portRELEASE_ISR_LOCK( xCoreID ) do{ ( void )( xCoreID ); rtos_lock_release( 0 ); } while( 0 )
157+
#define portGET_TASK_LOCK( xCoreID ) do{ ( void )( xCoreID ); rtos_lock_acquire( 1 ); } while( 0 )
158+
#define portRELEASE_TASK_LOCK( xCoreID ) do{ ( void )( xCoreID ); rtos_lock_release( 1 ); } while( 0 )
159+
159160

160161
void vTaskEnterCritical( void );
161162
void vTaskExitCritical( void );

portable/template/portmacro.h

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -123,19 +123,19 @@ extern void vPortYield( void );
123123

124124
/* Acquire the TASK lock. TASK lock is a recursive lock.
125125
* It should be able to be locked by the same core multiple times. */
126-
#define portGET_TASK_LOCK() do {} while( 0 )
126+
#define portGET_TASK_LOCK( xCoreID ) do {} while( 0 )
127127

128128
/* Release the TASK lock. If a TASK lock is locked by the same core multiple times,
129129
* it should be released as many times as it is locked. */
130-
#define portRELEASE_TASK_LOCK() do {} while( 0 )
130+
#define portRELEASE_TASK_LOCK( xCoreID ) do {} while( 0 )
131131

132132
/* Acquire the ISR lock. ISR lock is a recursive lock.
133133
* It should be able to be locked by the same core multiple times. */
134-
#define portGET_ISR_LOCK() do {} while( 0 )
134+
#define portGET_ISR_LOCK( xCoreID ) do {} while( 0 )
135135

136136
/* Release the ISR lock. If a ISR lock is locked by the same core multiple times, \
137137
* it should be released as many times as it is locked. */
138-
#define portRELEASE_ISR_LOCK() do {} while( 0 )
138+
#define portRELEASE_ISR_LOCK( xCoreID ) do {} while( 0 )
139139

140140
#endif /* if ( configNUMBER_OF_CORES > 1 ) */
141141

tasks.c

Lines changed: 29 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -831,7 +831,7 @@ static void prvAddNewTaskToReadyList( TCB_t * pxNewTCB ) PRIVILEGED_FUNCTION;
831831
if( uxPrevCriticalNesting > 0U )
832832
{
833833
portSET_CRITICAL_NESTING_COUNT( xCoreID, 0U );
834-
portRELEASE_ISR_LOCK();
834+
portRELEASE_ISR_LOCK( xCoreID );
835835
}
836836
else
837837
{
@@ -840,7 +840,7 @@ static void prvAddNewTaskToReadyList( TCB_t * pxNewTCB ) PRIVILEGED_FUNCTION;
840840
mtCOVERAGE_TEST_MARKER();
841841
}
842842

843-
portRELEASE_TASK_LOCK();
843+
portRELEASE_TASK_LOCK( xCoreID );
844844
portMEMORY_BARRIER();
845845
configASSERT( pxThisTCB->xTaskRunState == taskTASK_SCHEDULED_TO_YIELD );
846846

@@ -853,15 +853,16 @@ static void prvAddNewTaskToReadyList( TCB_t * pxNewTCB ) PRIVILEGED_FUNCTION;
853853
* its run state. */
854854

855855
portDISABLE_INTERRUPTS();
856-
portGET_TASK_LOCK();
857-
portGET_ISR_LOCK();
856+
858857
xCoreID = ( BaseType_t ) portGET_CORE_ID();
858+
portGET_TASK_LOCK( xCoreID );
859+
portGET_ISR_LOCK( xCoreID );
859860

860861
portSET_CRITICAL_NESTING_COUNT( xCoreID, uxPrevCriticalNesting );
861862

862863
if( uxPrevCriticalNesting == 0U )
863864
{
864-
portRELEASE_ISR_LOCK();
865+
portRELEASE_ISR_LOCK( xCoreID );
865866
}
866867
}
867868
}
@@ -3854,6 +3855,7 @@ void vTaskSuspendAll( void )
38543855
#else /* #if ( configNUMBER_OF_CORES == 1 ) */
38553856
{
38563857
UBaseType_t ulState;
3858+
BaseType_t xCoreID;
38573859

38583860
/* This must only be called from within a task. */
38593861
portASSERT_IF_IN_ISR();
@@ -3867,14 +3869,16 @@ void vTaskSuspendAll( void )
38673869
* uxSchedulerSuspended since that will prevent context switches. */
38683870
ulState = portSET_INTERRUPT_MASK();
38693871

3872+
xCoreID = ( BaseType_t ) portGET_CORE_ID();
3873+
38703874
/* This must never be called from inside a critical section. */
3871-
configASSERT( portGET_CRITICAL_NESTING_COUNT( portGET_CORE_ID() ) == 0 );
3875+
configASSERT( portGET_CRITICAL_NESTING_COUNT( xCoreID ) == 0 );
38723876

38733877
/* portSOFTWARE_BARRIER() is only implemented for emulated/simulated ports that
38743878
* do not otherwise exhibit real time behaviour. */
38753879
portSOFTWARE_BARRIER();
38763880

3877-
portGET_TASK_LOCK();
3881+
portGET_TASK_LOCK( xCoreID );
38783882

38793883
/* uxSchedulerSuspended is increased after prvCheckForRunStateChange. The
38803884
* purpose is to prevent altering the variable when fromISR APIs are readying
@@ -3888,12 +3892,17 @@ void vTaskSuspendAll( void )
38883892
mtCOVERAGE_TEST_MARKER();
38893893
}
38903894

3891-
portGET_ISR_LOCK();
3895+
/* Query the coreID again as prvCheckForRunStateChange may have
3896+
* caused the task to get scheduled on a different core. The correct
3897+
* task lock for the core is acquired in prvCheckForRunStateChange. */
3898+
xCoreID = ( BaseType_t ) portGET_CORE_ID();
3899+
3900+
portGET_ISR_LOCK( xCoreID );
38923901

38933902
/* The scheduler is suspended if uxSchedulerSuspended is non-zero. An increment
38943903
* is used to allow calls to vTaskSuspendAll() to nest. */
38953904
++uxSchedulerSuspended;
3896-
portRELEASE_ISR_LOCK();
3905+
portRELEASE_ISR_LOCK( xCoreID );
38973906

38983907
portCLEAR_INTERRUPT_MASK( ulState );
38993908
}
@@ -3998,7 +4007,7 @@ BaseType_t xTaskResumeAll( void )
39984007
configASSERT( uxSchedulerSuspended != 0U );
39994008

40004009
uxSchedulerSuspended = ( UBaseType_t ) ( uxSchedulerSuspended - 1U );
4001-
portRELEASE_TASK_LOCK();
4010+
portRELEASE_TASK_LOCK( xCoreID );
40024011

40034012
if( uxSchedulerSuspended == ( UBaseType_t ) 0U )
40044013
{
@@ -5168,8 +5177,8 @@ BaseType_t xTaskIncrementTick( void )
51685177
* and move on if another core suspended the scheduler. We should only
51695178
* do that if the current core has suspended the scheduler. */
51705179

5171-
portGET_TASK_LOCK(); /* Must always acquire the task lock first. */
5172-
portGET_ISR_LOCK();
5180+
portGET_TASK_LOCK( xCoreID ); /* Must always acquire the task lock first. */
5181+
portGET_ISR_LOCK( xCoreID );
51735182
{
51745183
/* vTaskSwitchContext() must never be called from within a critical section.
51755184
* This is not necessarily true for single core FreeRTOS, but it is for this
@@ -5250,8 +5259,8 @@ BaseType_t xTaskIncrementTick( void )
52505259
#endif
52515260
}
52525261
}
5253-
portRELEASE_ISR_LOCK();
5254-
portRELEASE_TASK_LOCK();
5262+
portRELEASE_ISR_LOCK( xCoreID );
5263+
portRELEASE_TASK_LOCK( xCoreID );
52555264

52565265
traceRETURN_vTaskSwitchContext();
52575266
}
@@ -6997,8 +7006,8 @@ static void prvResetNextTaskUnblockTime( void )
69977006
{
69987007
if( portGET_CRITICAL_NESTING_COUNT( xCoreID ) == 0U )
69997008
{
7000-
portGET_TASK_LOCK();
7001-
portGET_ISR_LOCK();
7009+
portGET_TASK_LOCK( xCoreID );
7010+
portGET_ISR_LOCK( xCoreID );
70027011
}
70037012

70047013
portINCREMENT_CRITICAL_NESTING_COUNT( xCoreID );
@@ -7051,7 +7060,7 @@ static void prvResetNextTaskUnblockTime( void )
70517060

70527061
if( portGET_CRITICAL_NESTING_COUNT( xCoreID ) == 0U )
70537062
{
7054-
portGET_ISR_LOCK();
7063+
portGET_ISR_LOCK( xCoreID );
70557064
}
70567065

70577066
portINCREMENT_CRITICAL_NESTING_COUNT( xCoreID );
@@ -7143,8 +7152,8 @@ static void prvResetNextTaskUnblockTime( void )
71437152
/* Get the xYieldPending stats inside the critical section. */
71447153
xYieldCurrentTask = xYieldPendings[ xCoreID ];
71457154

7146-
portRELEASE_ISR_LOCK();
7147-
portRELEASE_TASK_LOCK();
7155+
portRELEASE_ISR_LOCK( xCoreID );
7156+
portRELEASE_TASK_LOCK( xCoreID );
71487157
portENABLE_INTERRUPTS();
71497158

71507159
/* When a task yields in a critical section it just sets
@@ -7199,7 +7208,7 @@ static void prvResetNextTaskUnblockTime( void )
71997208

72007209
if( portGET_CRITICAL_NESTING_COUNT( xCoreID ) == 0U )
72017210
{
7202-
portRELEASE_ISR_LOCK();
7211+
portRELEASE_ISR_LOCK( xCoreID );
72037212
portCLEAR_INTERRUPT_MASK_FROM_ISR( uxSavedInterruptStatus );
72047213
}
72057214
else

0 commit comments

Comments
 (0)