21 lines
396 B
C
21 lines
396 B
C
#include <stdatomic.h>
|
|
#include <stdint.h>
|
|
#include "spinlock.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) {
|
|
atomic_store(&sl->lock, false);
|
|
}
|
|
|