xref: /plan9-contrib/sys/src/9/port/taslock.c (revision 61d44851dbae9c6db4696bac4b180d884ecea735)
1 #include "u.h"
2 #include "../port/lib.h"
3 #include "mem.h"
4 #include "dat.h"
5 #include "fns.h"
6 #include "../port/error.h"
7 #include "../port/edf.h"
8 
9 long maxlockcycles;
10 long maxilockcycles;
11 long cumlockcycles;
12 long cumilockcycles;
13 ulong maxlockpc;
14 ulong maxilockpc;
15 
16 struct
17 {
18 	ulong	locks;
19 	ulong	glare;
20 	ulong	inglare;
21 } lockstats;
22 
23 static void
inccnt(Ref * r)24 inccnt(Ref *r)
25 {
26 	ainc(&r->ref);
27 }
28 
29 static int
deccnt(Ref * r)30 deccnt(Ref *r)
31 {
32 	int x;
33 
34 	x = adec(&r->ref);
35 	if(x < 0)
36 		panic("deccnt pc=%#p", getcallerpc(&r));
37 	return x;
38 }
39 
40 static void
dumplockmem(char * tag,Lock * l)41 dumplockmem(char *tag, Lock *l)
42 {
43 	uchar *cp;
44 	int i;
45 
46 	iprint("%s: ", tag);
47 	cp = (uchar*)l;
48 	for(i = 0; i < 64; i++)
49 		iprint("%2.2ux ", cp[i]);
50 	iprint("\n");
51 }
52 
53 void
lockloop(Lock * l,ulong pc)54 lockloop(Lock *l, ulong pc)
55 {
56 	Proc *p;
57 
58 	p = l->p;
59 	print("lock %#p loop key %#lux pc %#lux held by pc %#lux proc %lud\n",
60 		l, l->key, pc, l->pc, p ? p->pid : 0);
61 	dumpaproc(up);
62 	if(p != nil)
63 		dumpaproc(p);
64 }
65 
66 int
lock(Lock * l)67 lock(Lock *l)
68 {
69 	int i;
70 	ulong pc;
71 
72 	pc = getcallerpc(&l);
73 
74 	lockstats.locks++;
75 	if(up)
76 		inccnt(&up->nlocks);	/* prevent being scheded */
77 	if(tas(&l->key) == 0){
78 		if(up)
79 			up->lastlock = l;
80 		l->pc = pc;
81 		l->p = up;
82 		l->isilock = 0;
83 		l->m = MACHP(m->machno);
84 #ifdef LOCKCYCLES
85 		l->lockcycles = -lcycles();
86 #endif
87 		return 0;
88 	}
89 	if(up)
90 		deccnt(&up->nlocks);
91 
92 	lockstats.glare++;
93 	for(;;){
94 		lockstats.inglare++;
95 		i = 0;
96 		while(l->key){
97 			if(conf.nmach < 2 && up && up->edf && (up->edf->flags & Admitted)){
98 				/*
99 				 * Priority inversion, yield on a uniprocessor; on a
100 				 * multiprocessor, the other processor will unlock
101 				 */
102 				print("inversion %#p pc %#lux proc %lud held by pc %#lux proc %lud\n",
103 					l, pc, up ? up->pid : 0, l->pc, l->p ? l->p->pid : 0);
104 				up->edf->d = todget(nil);	/* yield to process with lock */
105 			}
106 			if(i++ > 100000000){
107 				i = 0;
108 				lockloop(l, pc);
109 			}
110 		}
111 		if(up)
112 			inccnt(&up->nlocks);
113 		if(tas(&l->key) == 0){
114 			if(up)
115 				up->lastlock = l;
116 			l->pc = pc;
117 			l->p = up;
118 			l->isilock = 0;
119 			l->m = MACHP(m->machno);
120 #ifdef LOCKCYCLES
121 			l->lockcycles = -lcycles();
122 #endif
123 			return 1;
124 		}
125 		if(up)
126 			deccnt(&up->nlocks);
127 	}
128 }
129 
130 void
ilock(Lock * l)131 ilock(Lock *l)
132 {
133 	ulong x;
134 	ulong pc;
135 
136 	pc = getcallerpc(&l);
137 	lockstats.locks++;
138 
139 	x = splhi();
140 	if(tas(&l->key) != 0){
141 		lockstats.glare++;
142 		/*
143 		 * Cannot also check l->pc, l->m, or l->isilock here
144 		 * because they might just not be set yet, or
145 		 * (for pc and m) the lock might have just been unlocked.
146 		 */
147 		for(;;){
148 			lockstats.inglare++;
149 			splx(x);
150 			while(l->key)
151 				;
152 			x = splhi();
153 			if(tas(&l->key) == 0)
154 				goto acquire;
155 		}
156 	}
157 acquire:
158 	m->ilockdepth++;
159 	if(up)
160 		up->lastilock = l;
161 	l->sr = x;
162 	l->pc = pc;
163 	l->p = up;
164 	l->isilock = 1;
165 	l->m = MACHP(m->machno);
166 #ifdef LOCKCYCLES
167 	l->lockcycles = -lcycles();
168 #endif
169 }
170 
171 int
canlock(Lock * l)172 canlock(Lock *l)
173 {
174 	if(up)
175 		inccnt(&up->nlocks);
176 	if(tas(&l->key)){
177 		if(up)
178 			deccnt(&up->nlocks);
179 		return 0;
180 	}
181 
182 	if(up)
183 		up->lastlock = l;
184 	l->pc = getcallerpc(&l);
185 	l->p = up;
186 	l->m = MACHP(m->machno);
187 	l->isilock = 0;
188 #ifdef LOCKCYCLES
189 	l->lockcycles = -lcycles();
190 #endif
191 	return 1;
192 }
193 
194 void
unlock(Lock * l)195 unlock(Lock *l)
196 {
197 #ifdef LOCKCYCLES
198 	l->lockcycles += lcycles();
199 	cumlockcycles += l->lockcycles;
200 	if(l->lockcycles > maxlockcycles){
201 		maxlockcycles = l->lockcycles;
202 		maxlockpc = l->pc;
203 	}
204 #endif
205 	if(l->key == 0)
206 		print("unlock: not locked: pc %#p\n", getcallerpc(&l));
207 	if(l->isilock)
208 		print("unlock of ilock: pc %lux, held by %lux\n", getcallerpc(&l), l->pc);
209 	if(l->p != up)
210 		print("unlock: up changed: pc %#p, acquired at pc %lux, lock p %#p, unlock up %#p\n", getcallerpc(&l), l->pc, l->p, up);
211 	l->m = nil;
212 	coherence();
213 	l->key = 0;
214 	coherence();
215 
216 	if(up && deccnt(&up->nlocks) == 0 && up->delaysched && islo()){
217 		/*
218 		 * Call sched if the need arose while locks were held
219 		 * But, don't do it from interrupt routines, hence the islo() test
220 		 */
221 		sched();
222 	}
223 }
224 
225 ulong ilockpcs[0x100] = { [0xff] = 1 };
226 static int n;
227 
228 void
iunlock(Lock * l)229 iunlock(Lock *l)
230 {
231 	ulong sr;
232 
233 #ifdef LOCKCYCLES
234 	l->lockcycles += lcycles();
235 	cumilockcycles += l->lockcycles;
236 	if(l->lockcycles > maxilockcycles){
237 		maxilockcycles = l->lockcycles;
238 		maxilockpc = l->pc;
239 	}
240 	if(l->lockcycles > 2400)
241 		ilockpcs[n++ & 0xff]  = l->pc;
242 #endif
243 	if(l->key == 0)
244 		print("iunlock: not locked: pc %#p\n", getcallerpc(&l));
245 	if(!l->isilock)
246 		print("iunlock of lock: pc %#p, held by %#lux\n", getcallerpc(&l), l->pc);
247 	if(islo())
248 		print("iunlock while lo: pc %#p, held by %#lux\n", getcallerpc(&l), l->pc);
249 
250 	sr = l->sr;
251 	l->m = nil;
252 	coherence();
253 	l->key = 0;
254 	coherence();
255 	m->ilockdepth--;
256 	if(up)
257 		up->lastilock = nil;
258 	splx(sr);
259 }
260