Voxia OS
v0.0.1
Hobby Project Operating System Targeting x86-64
Loading...
Searching...
No Matches
spinlock.c
Go to the documentation of this file.
1
#include "
./spinlock.h
"
2
3
static
inline
void
cpu_relax
(
void
) {
4
__asm__
volatile
(
"pause"
:::
"memory"
);
5
__asm__
volatile
(
""
:::
"memory"
);
6
}
7
8
void
spin_acquire
(
spinlock_t
*
lock
) {
9
10
uint16_t
my_ticket =
11
__atomic_fetch_add(&
lock
->next_ticket, 1, __ATOMIC_RELAXED);
12
13
while
(__atomic_load_n(&
lock
->now_serving, __ATOMIC_ACQUIRE) !=
14
my_ticket) {
15
cpu_relax
();
16
}
17
}
18
19
void
spin_release
(
spinlock_t
*
lock
) {
20
21
uint16_t
next
=
22
__atomic_load_n(&
lock
->now_serving, __ATOMIC_RELAXED) + 1;
23
24
__atomic_store_n(&
lock
->now_serving,
next
, __ATOMIC_RELEASE);
25
}
26
27
bool
spin_is_locked
(
const
spinlock_t
*
lock
) {
28
uint16_t
serving =
29
__atomic_load_n(&
lock
->now_serving, __ATOMIC_RELAXED);
30
uint16_t
next
= __atomic_load_n(&
lock
->next_ticket, __ATOMIC_RELAXED);
31
return
serving !=
next
;
32
}
lock
uint8_t lock
Definition
cache.h:1
next
struct cdev * next
Definition
dev.h:4
cpu_relax
static void cpu_relax(void)
Definition
spinlock.c:3
spin_acquire
void spin_acquire(spinlock_t *lock)
Definition
spinlock.c:8
spin_is_locked
bool spin_is_locked(const spinlock_t *lock)
Definition
spinlock.c:27
spin_release
void spin_release(spinlock_t *lock)
Definition
spinlock.c:19
spinlock.h
spinlock_t
Definition
spinlock.h:10
uint16_t
unsigned short uint16_t
Definition
type.h:13
kernel
hal
cpu
spinlock.c
Generated on Sat May 30 2026 11:09:57 for Voxia OS by
1.13.2