1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94
|
#include "u.h"
#include "lib.h"
#include "dat.h"
#include "fns.h"
static void
queue(Proc **first, Proc **last)
{
Proc *t;
t = *last;
if(t == 0)
*first = up;
else
t->qnext = up;
*last = up;
up->qnext = 0;
}
static Proc*
dequeue(Proc **first, Proc **last)
{
Proc *t;
t = *first;
if(t == 0)
return 0;
*first = t->qnext;
if(*first == 0)
*last = 0;
return t;
}
void
qlock(QLock *q)
{
lock(&q->lk);
if(q->hold == 0) {
q->hold = up;
unlock(&q->lk);
return;
}
/*
* Can't assert this because of RWLock
assert(q->hold != up);
*/
queue((Proc**)&q->first, (Proc**)&q->last);
unlock(&q->lk);
procsleep();
}
int
canqlock(QLock *q)
{
lock(&q->lk);
if(q->hold == 0) {
q->hold = up;
unlock(&q->lk);
return 1;
}
unlock(&q->lk);
return 0;
}
void
qunlock(QLock *q)
{
Proc *p;
lock(&q->lk);
/*
* Can't assert this because of RWlock
assert(q->hold == CT);
*/
p = dequeue((Proc**)&q->first, (Proc**)&q->last);
if(p) {
q->hold = p;
unlock(&q->lk);
procwakeup(p);
} else {
q->hold = 0;
unlock(&q->lk);
}
}
int
holdqlock(QLock *q)
{
return q->hold == up;
}
|