AquilaOS documentation
How it works, and its code
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
24static volatile uint32_t next_ticket, serving;
25
26/* the CPU that holds the lock, or -1 */
27static volatile int owner = -1;
28
32void 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)
38
39 owner = curcpu()->id;
41}
42
46void kernel_unlock(void)
47{
48 owner = -1;
49 __atomic_store_n(&serving, serving + 1, __ATOMIC_RELEASE);
50}
51
58{
59 return owner == curcpu()->id;
60}
61
68int kernel_enter(void)
69{
70 if (kernel_lock_held())
71 return 0;
72
74 return 1;
75}
76
83void kernel_leave(int took)
84{
85 if (took)
87}
void arch_lock_spin(void)
Runs in the kernel lock's wait loop, on each turn.
Definition smp.c:20
void arch_lock_taken(void)
Runs once this CPU has taken the kernel lock.
Definition smp.c:25
static volatile uint32_t serving
Definition lock.c:24
static volatile uint32_t next_ticket
Definition lock.c:24
static volatile int owner
Definition lock.c:27
void kernel_unlock(void)
Lets go of the kernel lock, for the next CPU waiting for it.
Definition lock.c:46
int kernel_lock_held(void)
Says whether this CPU holds the kernel lock.
Definition lock.c:57
void kernel_lock(void)
Takes the kernel lock, waiting for its turn if another CPU holds it.
Definition lock.c:32
void kernel_leave(int took)
Lets go of the kernel lock on leaving the kernel, if kernel_enter() took it.
Definition lock.c:83
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
unsigned int uint32_t
Definition stddef.h:22