// // SEMAPHORE: PSEUDO-CODE // sem_init(sem_t *s, int initvalue) { s->value = initvalue; s->lock = phtread_mutex_init(); s->cond = phtread_cond_init(); } sem_wait(sem_t *s) { pthread_mutex_lock(&s->lock); while (s->value <= 0) phtread_cond_wait(&s->cond, &s->lock) //put_self_to_sleep(); // put self to sleep s->value--; pthread_mutex_unlock(&s->lock); } sem_post(sem_t *s) { s->value++; wake_one_waiting_thread(); // if there is one } // // IMPORTANT: each is done atomically // (i.e., body of post() and wait() happen all at once) //