/* lock functions */ void lock_init(lock_t * l) { spinlock_init(&l->spinlock); l->waiting = NULL; l->status = UNLOCKED; } void lock_acquire(lock_t * l) { spinlock_acquire(&l->spinlock); if (l->status == LOCKED) { block(&l->waiting, &l->spinlock); } else { l->status = LOCKED; spinlock_release(&l->spinlock); } } void lock_release(lock_t * l) { spinlock_acquire(&l->spinlock); if (l->waiting) unblock(&l->waiting); else l->status = UNLOCKED; spinlock_release(&l->spinlock); } /* condition functions */ void condition_init(condition_t * c) { spinlock_init(&c->spinlock); c->waiting = NULL; } /* * unlock m and block the thread (enqued on c), when unblocked acquire * lock m */ void condition_wait(lock_t * m, condition_t * c) { spinlock_acquire(&c->spinlock); lock_release(m); //TODO revise it all block(&c->waiting, &c->spinlock); lock_acquire(m); } /* unblock first thread enqued on c */ void condition_signal(condition_t * c) { spinlock_acquire(&c->spinlock); if (c->waiting) unblock(&c->waiting); spinlock_release(&c->spinlock); } /* unblock all threads enqued on c */ void condition_broadcast(condition_t * c) { spinlock_acquire(&c->spinlock); while (c->waiting) unblock(&c->waiting); spinlock_release(&c->spinlock); }