AquilaOS documentation
How it works, and its code
Toggle main menu visibility
Loading...
Searching...
No Matches
lock.c
Go to the documentation of this file.
1
19
20
#include <
core/system.h
>
21
#include <
sys/cpu.h
>
22
#include <
sys/lock.h
>
23
24
static
volatile
uint32_t
next_ticket
,
serving
;
25
26
/* the CPU that holds the lock, or -1 */
27
static
volatile
int
owner
= -1;
28
32
void
kernel_lock
(
void
)
33
{
34
uint32_t
ticket = __atomic_fetch_add(&
next_ticket
, 1, __ATOMIC_RELAXED);
35
36
while
(__atomic_load_n(&
serving
, __ATOMIC_ACQUIRE) != ticket)
37
arch_lock_spin
();
38
39
owner
= curcpu()->id;
40
arch_lock_taken
();
41
}
42
46
void
kernel_unlock
(
void
)
47
{
48
owner
= -1;
49
__atomic_store_n(&
serving
,
serving
+ 1, __ATOMIC_RELEASE);
50
}
51
57
int
kernel_lock_held
(
void
)
58
{
59
return
owner
== curcpu()->id;
60
}
61
68
int
kernel_enter
(
void
)
69
{
70
if
(
kernel_lock_held
())
71
return
0;
72
73
kernel_lock
();
74
return
1;
75
}
76
83
void
kernel_leave
(
int
took)
84
{
85
if
(took)
86
kernel_unlock
();
87
}
arch_lock_spin
void arch_lock_spin(void)
Runs in the kernel lock's wait loop, on each turn.
Definition
smp.c:20
arch_lock_taken
void arch_lock_taken(void)
Runs once this CPU has taken the kernel lock.
Definition
smp.c:25
system.h
cpu.h
serving
static volatile uint32_t serving
Definition
lock.c:24
next_ticket
static volatile uint32_t next_ticket
Definition
lock.c:24
owner
static volatile int owner
Definition
lock.c:27
lock.h
kernel_unlock
void kernel_unlock(void)
Lets go of the kernel lock, for the next CPU waiting for it.
Definition
lock.c:46
kernel_lock_held
int kernel_lock_held(void)
Says whether this CPU holds the kernel lock.
Definition
lock.c:57
kernel_lock
void kernel_lock(void)
Takes the kernel lock, waiting for its turn if another CPU holds it.
Definition
lock.c:32
kernel_leave
void kernel_leave(int took)
Lets go of the kernel lock on leaving the kernel, if kernel_enter() took it.
Definition
lock.c:83
kernel_enter
int kernel_enter(void)
Takes the kernel lock on entering the kernel, unless this CPU holds it already (an exception inside t...
Definition
lock.c:68
uint32_t
unsigned int uint32_t
Definition
stddef.h:22
kernel
sys
lock.c
Generated by
1.18.0