Files
my-os-project2/ulib/sync/spinlock.c

19 lines
396 B
C

#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);
}