// sync_primitives.c #include "spinlock.h" // Initialize the spinlock void spinlock_init(Spinlock *spinlock) { spinlock->lock = 0; // Unlocked state } // Acquire the spinlock void spinlock_acquire(Spinlock *spinlock) { int status; do { // Wait until the lock is available do { __asm__ volatile ("ldrex %0, [%1]" : "=&r" (status) : "r" (&spinlock->lock) : "memory"); } while (status != 0); // Try to acquire the lock using strex __asm__ volatile ("strex %0, %2, [%1]" : "=&r" (status) : "r" (&spinlock->lock), "r" (1) : "memory"); } while (status != 0); // Retry if strex failed } // Release the spinlock void spinlock_release(Spinlock *spinlock) { // Use a memory barrier to ensure proper ordering __asm__ volatile ("dmb" ::: "memory"); spinlock->lock = 0; // Release the lock }