2004-03-25 23:03:57 +00:00
|
|
|
#include <u.h>
|
2003-09-30 17:47:41 +00:00
|
|
|
#include <unistd.h>
|
|
|
|
#include <sched.h>
|
2004-03-25 23:03:57 +00:00
|
|
|
#include <libc.h>
|
2003-09-30 17:47:41 +00:00
|
|
|
|
|
|
|
int _ntas;
|
|
|
|
static int
|
|
|
|
_xtas(void *v)
|
|
|
|
{
|
|
|
|
int x;
|
|
|
|
|
2003-10-01 02:50:57 +00:00
|
|
|
_ntas++;
|
2003-09-30 17:47:41 +00:00
|
|
|
x = _tas(v);
|
2003-10-01 02:50:57 +00:00
|
|
|
return x;
|
2003-09-30 17:47:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
canlock(Lock *l)
|
|
|
|
{
|
|
|
|
return !_xtas(&l->val);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
unlock(Lock *l)
|
|
|
|
{
|
|
|
|
l->val = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
lock(Lock *lk)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
|
|
|
|
/* once fast */
|
|
|
|
if(!_xtas(&lk->val))
|
|
|
|
return;
|
|
|
|
/* a thousand times pretty fast */
|
|
|
|
for(i=0; i<1000; i++){
|
|
|
|
if(!_xtas(&lk->val))
|
|
|
|
return;
|
|
|
|
sched_yield();
|
|
|
|
}
|
|
|
|
/* now nice and slow */
|
|
|
|
for(i=0; i<1000; i++){
|
|
|
|
if(!_xtas(&lk->val))
|
|
|
|
return;
|
|
|
|
usleep(100*1000);
|
|
|
|
}
|
|
|
|
/* take your time */
|
|
|
|
while(_xtas(&lk->val))
|
|
|
|
usleep(1000*1000);
|
|
|
|
}
|