25 lines
512 B
C
25 lines
512 B
C
#include <stdatomic.h>
|
|
#include <stdint.h>
|
|
#include "spinlock.h"
|
|
#include "hal/hal.h"
|
|
|
|
void spinlock_init(SpinLock *sl) {
|
|
atomic_store(&sl->lock, false);
|
|
}
|
|
|
|
void spinlock_acquire(SpinLock *sl) {
|
|
bool unlocked = false;
|
|
while (!atomic_compare_exchange_weak(&sl->lock, &unlocked, true)) {
|
|
unlocked = false;
|
|
SPINLOCK_HINT();
|
|
}
|
|
}
|
|
|
|
void spinlock_release(SpinLock *sl) {
|
|
bool locked = true;
|
|
if (atomic_compare_exchange_strong(&sl->lock, &locked, false)) {
|
|
atomic_store(&sl->lock, false);
|
|
}
|
|
}
|
|
|