Blob


1 #include <u.h>
2 #include <unistd.h>
3 #include <sys/time.h>
4 #include <sched.h>
5 #include <libc.h>
7 int _ntas;
8 static int
9 _xtas(void *v)
10 {
11 int x;
13 _ntas++;
14 x = _tas(v);
15 return x;
16 }
18 int
19 canlock(Lock *l)
20 {
21 return !_xtas(&l->val);
22 }
24 void
25 unlock(Lock *l)
26 {
27 l->val = 0;
28 }
30 void
31 lock(Lock *lk)
32 {
33 int i;
35 /* once fast */
36 if(!_xtas(&lk->val))
37 return;
38 /* a thousand times pretty fast */
39 for(i=0; i<1000; i++){
40 if(!_xtas(&lk->val))
41 return;
42 sched_yield();
43 }
44 /* now nice and slow */
45 for(i=0; i<1000; i++){
46 if(!_xtas(&lk->val))
47 return;
48 usleep(100*1000);
49 }
50 /* take your time */
51 while(_xtas(&lk->val))
52 usleep(1000*1000);
53 }