16 lines
427 B
C
16 lines
427 B
C
#include <zinnia/atomic.h>
|
|
#include <zinnia/spin.h>
|
|
|
|
void spin_lock(struct spinlock* spin) {
|
|
uint32_t my = atomic_fetch_add(&spin->next, 1, ATOMIC_RELAXED);
|
|
|
|
while (atomic_load(&spin->owner, ATOMIC_ACQUIRE) != my) {
|
|
asm volatile("pause");
|
|
}
|
|
}
|
|
|
|
void spin_unlock(struct spinlock* spin) {
|
|
uint32_t val = atomic_load(&spin->owner, ATOMIC_RELAXED);
|
|
atomic_store(&spin->owner, val + 1, ATOMIC_RELEASE);
|
|
}
|