32 lines
624 B
C
32 lines
624 B
C
#ifndef SPINLOCK_SPINLOCK_H_
|
|
#define SPINLOCK_SPINLOCK_H_
|
|
|
|
#include <stdatomic.h>
|
|
#include "hal/hal.h"
|
|
|
|
typedef atomic_bool SpinLock;
|
|
|
|
// Spin more efficiently - cpu dependant
|
|
#if defined(__x86_64__)
|
|
# define SPINLOCK_HINT() asm volatile("pause")
|
|
#else
|
|
# define SPINLOCK_HINT()
|
|
#endif
|
|
|
|
#define SPINLOCK_ACQUIRE(sl) \
|
|
do { \
|
|
bool __unlocked = false; \
|
|
while (!atomic_compare_exchange_weak((sl), &__unlocked, true)) { \
|
|
SPINLOCK_HINT(); \
|
|
} \
|
|
} while(0)
|
|
|
|
#define SPINLOCK_RELEASE(sl) \
|
|
do { \
|
|
atomic_store((sl), false); \
|
|
} while(0)
|
|
|
|
#define SPINLOCK_INIT() false
|
|
|
|
#endif // SPINLOCK_SPINLOCK_H_
|