-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathspinlock.c
59 lines (49 loc) · 975 Bytes
/
spinlock.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
#include "mmu.h"
#include "x86.h"
#include "proc.h"
extern struct cpu *mcpu;
void initlock(struct spinlock *lk, char *name)
{
lk->locked = 0;
lk->name = name;
}
void acquire(struct spinlock *lk)
{
pushcli();
if(lk->locked){
kprintf("acquire: %s", lk->name);
PANIC("acquire");
}
lk->locked = 1;
}
void release(struct spinlock *lk)
{
if(!lk->locked)
PANIC("release");
lk->locked = 0;
popcli();
}
// Pushcli/popcli are like cli/sti except that they are matched:
// it takes two popcli to undo two pushcli. Also, if interrupts
// are off, then pushcli, popcli leaves them off.
void pushcli(void)
{
int eflags;
eflags = readeflags();
cli();
if(mcpu->ncli++ == 0)
mcpu->intena = eflags & FL_IF;
}
void popcli(void)
{
if(readeflags() & FL_IF)
PANIC("popcli: interruptible");
if(--mcpu->ncli < 0)
PANIC("popccli: ncli < 0");
if(mcpu->ncli == 0 && mcpu->intena)
sti();
}
int holding(struct spinlock *lk)
{
return lk->locked;
}