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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
|
#include "u.h"
#include "../port/lib.h"
#include "mem.h"
#include "dat.h"
#include "fns.h"
#include "../port/error.h"
static void
lockloop(Lock *l, ulong pc)
{
setpanic();
print("lock loop 0x%lux key 0x%lux pc 0x%lux held by pc 0x%lux\n", l, l->key, pc, l->pc);
panic("lockloop");
}
void
lock(Lock *l)
{
int i;
ulong pc;
pc = getcallerpc(&l);
if(up == 0) {
if (_tas(&l->key) != 0) {
for(i=0; ; i++) {
if(_tas(&l->key) == 0)
break;
if (i >= 1000000) {
lockloop(l, pc);
break;
}
}
}
l->pc = pc;
return;
}
for(i=0; ; i++) {
if(_tas(&l->key) == 0)
break;
if (i >= 1000) {
lockloop(l, pc);
break;
}
if(conf.nmach == 1 && up->state == Running && islo()) {
up->pc = pc;
sched();
}
}
l->pri = up->pri;
up->pri = PriLock;
l->pc = pc;
}
void
ilock(Lock *l)
{
ulong x, pc;
int i;
pc = getcallerpc(&l);
x = splhi();
for(;;) {
if(_tas(&l->key) == 0) {
l->sr = x;
l->pc = pc;
return;
}
if(conf.nmach < 2)
panic("ilock: no way out: pc 0x%lux: lock 0x%lux held by pc 0x%lux", pc, l, l->pc);
for(i=0; ; i++) {
if(l->key == 0)
break;
clockcheck();
if (i > 100000) {
lockloop(l, pc);
break;
}
}
}
}
int
canlock(Lock *l)
{
if(_tas(&l->key))
return 0;
if(up){
l->pri = up->pri;
up->pri = PriLock;
}
l->pc = getcallerpc(&l);
return 1;
}
void
unlock(Lock *l)
{
int p;
if(l->key == 0)
print("unlock: not locked: pc %lux\n", getcallerpc(&l));
p = l->pri;
l->pc = 0;
l->key = 0;
coherence();
if(up){
up->pri = p;
if(up->state == Running && anyhigher())
sched();
}
}
void
iunlock(Lock *l)
{
ulong sr;
if(l->key == 0)
print("iunlock: not locked: pc %lux\n", getcallerpc(&l));
sr = l->sr;
l->pc = 0;
l->key = 0;
coherence();
splxpc(sr);
}
|