Blob


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