Skip to content

Commit 30e2e70

Browse files
committed
OS/FreeRTOS: add volatile to recursive spinlock tracking arrays
The `ucOwnedByCore` and `ucRecursionCountByLock` arrays are used to manage recursive spinlocks and are accessed by multiple cores. Without the `volatile` keyword, the compiler might optimize away memory accesses and cache the array values in registers. This could lead to a core reading a stale value, causing incorrect lock behavior, race conditions, or deadlocks in a multi-core environment. Marking these arrays as `volatile` ensures that every access reads from or writes to main memory, guaranteeing the correct and up-to-date state is observed by all cores. Signed-off-by: Huaqi Fang <[email protected]>
1 parent 4648578 commit 30e2e70

File tree

1 file changed

+2
-2
lines changed
  • OS/FreeRTOS/Source/portable

1 file changed

+2
-2
lines changed

OS/FreeRTOS/Source/portable/port.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -177,9 +177,9 @@ spin_lock_t hw_sync_locks[portRTOS_SPINLOCK_COUNT] = {0, 0};
177177
void vPortRecursiveLock(unsigned long ulLockNum, spin_lock_t *pxSpinLock, BaseType_t uxAcquire)
178178
{
179179
/* Track, per-core, which locks this core currently owns. */
180-
static uint8_t ucOwnedByCore[portMAX_CORE_COUNT];
180+
static volatile uint8_t ucOwnedByCore[portMAX_CORE_COUNT];
181181
/* Track, per-lock, how many times it has been recursively taken. */
182-
static uint8_t ucRecursionCountByLock[portRTOS_SPINLOCK_COUNT];
182+
static volatile uint8_t ucRecursionCountByLock[portRTOS_SPINLOCK_COUNT];
183183

184184
configASSERT(ulLockNum < portRTOS_SPINLOCK_COUNT);
185185
unsigned long ulCoreNum = __get_hart_index(); /* ID of current hart */

0 commit comments

Comments
 (0)