throw("lock count");
m->locks++;
- // Allocate semaphore if needed.
- if(l->sema == 0)
- initsema(&l->sema);
-
- if(xadd(&l->key, 1) > 1) // someone else has it; wait
+ if(xadd(&l->key, 1) > 1) { // someone else has it; wait
+ // Allocate semaphore if needed.
+ if(l->sema == 0)
+ initsema(&l->sema);
mach_semacquire(l->sema);
+ }
}
void
if(m->locks < 0)
throw("lock count");
- if(xadd(&l->key, -1) > 0) // someone else is waiting
+ if(xadd(&l->key, -1) > 0) { // someone else is waiting
+ // Allocate semaphore if needed.
+ if(l->sema == 0)
+ initsema(&l->sema);
mach_semrelease(l->sema);
+ }
}
void
usemacquire(Usema *s)
{
- if((int32)xadd(&s->u, -1) < 0)
+ if((int32)xadd(&s->u, -1) < 0) {
+ if(s->k == 0)
+ initsema(&s->k);
mach_semacquire(s->k);
+ }
}
void
usemrelease(Usema *s)
{
- if((int32)xadd(&s->u, 1) <= 0)
+ if((int32)xadd(&s->u, 1) <= 0) {
+ if(s->k == 0)
+ initsema(&s->k);
mach_semrelease(s->k);
+ }
}
void
notesleep(Note *n)
{
- if(n->sema.k == 0)
- initsema(&n->sema.k);
while(!n->wakeup)
usemacquire(&n->sema);
}
void
notewakeup(Note *n)
{
- if(n->sema.k == 0)
- initsema(&n->sema.k);
n->wakeup = 1;
usemrelease(&n->sema);
}