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 int9 _xtas(void *v)10 {11 int x;13 _ntas++;14 x = _tas(v);15 if(x != 0 && x != 0xcafebabe){16 print("bad tas value %d\n", x);17 abort();18 }19 return x;20 }22 int23 canlock(Lock *l)24 {25 return !_xtas(&l->val);26 }28 void29 unlock(Lock *l)30 {31 l->val = 0;32 }34 void35 lock(Lock *lk)36 {37 int i;39 /* once fast */40 if(!_xtas(&lk->val))41 return;42 /* a thousand times pretty fast */43 for(i=0; i<1000; i++){44 if(!_xtas(&lk->val))45 return;46 sched_yield();47 }48 /* now nice and slow */49 for(i=0; i<1000; i++){50 if(!_xtas(&lk->val))51 return;52 usleep(100*1000);53 }54 /* take your time */55 while(_xtas(&lk->val))56 usleep(1000*1000);57 }