-
Notifications
You must be signed in to change notification settings - Fork 1
/
spinlock.c
81 lines (67 loc) · 1.43 KB
/
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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
// Mutual exclusion spin locks.
#include <types.h>
#include <defs.h>
//#include "param.h"
//#include "x86.h"
//#include "memlayout.h"
//#include "mmu.h"
#include <proc.h>
#include <spinlock.h>
extern struct cpu *cpu;
void
initlock(struct spinlock *lk, char *name)
{
lk->name = name;
lk->locked = 0;
}
// Acquire the lock.
// Loops (spins) until the lock is acquired.
// Holding a lock for a long time may cause
// other CPUs to waste time spinning to acquire it.
void
acquire(struct spinlock *lk)
{
pushcli(); // disable interrupts to avoid deadlock.
if(holding(lk))
for(;;) // panic("acquire");
// take the spinlock
_spinlock_take(&lk->locked);
}
// Release the lock.
void
release(struct spinlock *lk)
{
if(!holding(lk))
for(;;) // panic("release");
// release the spinlock
_spinlock_give(&lk->locked);
popcli();
}
// Check whether this lock is held.
int
holding(struct spinlock *lock)
{
return lock->locked;
}
// 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)
{
uint idisable;
idisable = _checki();
_cli();
if(cpu->ncli++ == 0)
cpu->intena = idisable;
}
void
popcli(void)
{
if(!_checki())
for(;;); // panic("popcli - interruptible");
if(--cpu->ncli < 0)
for(;;); // panic("popcli");
if(cpu->ncli == 0 && (!cpu->intena))
_sti();
}