20 lines
444 B
C
20 lines
444 B
C
#pragma once
|
|
|
|
#include <stdatomic.h>
|
|
#include <stdint.h>
|
|
|
|
// Busy-waits in a loop until the lock is freed.
|
|
// Does not put the CPU to sleep.
|
|
struct spinlock {
|
|
_Atomic(uint32_t) owner;
|
|
_Atomic(uint32_t) next;
|
|
};
|
|
|
|
void spin_init(struct spinlock* spin);
|
|
|
|
// Attempts to lock a spinlock. If it's already locked, waits until it's freed.
|
|
void spin_lock(struct spinlock* spin);
|
|
|
|
// Unlocks a spinlock.
|
|
void spin_unlock(struct spinlock* spin);
|