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