#include <stdatomic.h>
#include <sync/spinlock.h>

#define HINT() asm volatile("pause");

void spinlock_init(SpinLock *sl) {
  atomic_store(&sl->lock, false);
}

void spinlock_acquire(SpinLock *sl) {
  while (atomic_test_and_set_explicit(&sl->lock, memory_order_release)) {
    HINT();
  }
}

void spinlock_release(SpinLock *sl) {
  atomic_clear_flag_explicit(&sl->lock, memory_order_release);
}
