=================================================================== RCS file: /cvs/prex-old/sys/sync/sem.c,v retrieving revision 1.1.1.1 retrieving revision 1.1.1.1.2.1 diff -u -r1.1.1.1 -r1.1.1.1.2.1 --- prex-old/sys/sync/sem.c 2008/06/03 10:38:46 1.1.1.1 +++ prex-old/sys/sync/sem.c 2008/08/13 17:12:33 1.1.1.1.2.1 @@ -32,10 +32,10 @@ */ /* - * All of the Prex semaphore is un-named semaphore. Instead, the - * named semaphore is implemented by a file system server. - * In order to access the other task's semaphore, the task must - * have CAP_SEMAPHORE capability. + * All of the Prex semaphore is un-named semaphore. Instead, + * the named semaphore is implemented by a file system server. + * In order to access the other task's semaphore, the task + * must have CAP_SEMAPHORE capability. */ #include @@ -47,39 +47,38 @@ /* * sem_init - initialize a semaphore. - * @sem: ID for new semaphore. - * @value: initial semaphore count. * - * sem_init() creates a new semaphore if the specified semaphore does - * not exist yet. If the semaphore already exists, it is re-initialized - * only if nobody is waiting for it. The initial semaphore value is set - * to the requested value. + * sem_init() creates a new semaphore if the specified + * semaphore does not exist yet. If the semaphore already + * exists, it is re-initialized only if nobody is waiting for + * it. The initial semaphore value is set to the requested + * value. */ int sem_init(sem_t *sem, u_int value) { - struct sem *s, *sem_org; + struct sem *s; int err = 0; if (value > MAXSEMVAL) return EINVAL; - if (umem_copyin(sem, &sem_org, sizeof(sem_t))) + if (umem_copyin(sem, &s, sizeof(sem_t))) return EFAULT; /* * An application can call sem_init() to reset the * value of existing semaphore. So, we have to check - * the semaphore is already allocated. + * whether the semaphore is already allocated. */ sched_lock(); - if (sem_valid(sem_org)) { + if (sem_valid(s)) { /* * Semaphore already exists. */ - if (sem_org->task != cur_task() && + if (s->task != cur_task() && !task_capable(CAP_SEMAPHORE)) err = EPERM; - else if (event_waiting(&sem_org->event)) + else if (event_waiting(&s->event)) err = EBUSY; else s->value = value; @@ -106,10 +105,8 @@ /* * sem_copyin - copy a semaphore from user space. - * @usem: pointer to semaphore in user space. - * @ksem: pointer to semaphore in kernel space. * - * It also checks if the passed semaphore is valid. + * It also checks whether the passed semaphore is valid. */ static int sem_copyin(sem_t *usem, sem_t *ksem) @@ -146,7 +143,7 @@ sched_unlock(); return err; } - if (event_waiting(&s->event) || s->value <= 0) { + if (event_waiting(&s->event) || s->value == 0) { sched_unlock(); return EBUSY; } @@ -158,19 +155,19 @@ /* * sem_wait - lock a semaphore. - * @sem: semaphore ID - * @timeout: time out value in msec. 0 for no timeout. * + * The value of timeout is msec unit. 0 for no timeout. + * * sem_wait() locks the semaphore referred by sem only if the - * semaphore value is currently positive. The thread will sleep - * while the semaphore value is zero. It decrements the semaphore - * value in return. + * semaphore value is currently positive. The thread will + * sleep while the semaphore value is zero. It decrements the + * semaphore value in return. * - * If waiting thread receives any exception, this routine returns - * with EINTR in order to invoke exception handler. But, an - * application assumes this call does NOT return with error. So, - * system call stub routine must re-call automatically if it gets - * EINTR. + * If waiting thread receives any exception, this routine + * returns with EINTR in order to invoke exception + * handler. But, an application assumes this call does NOT + * return with error. So, system call stub routine must + * re-call automatically if it gets EINTR. */ int sem_wait(sem_t *sem, u_long timeout) @@ -182,12 +179,13 @@ if ((err = sem_copyin(sem, &s))) goto out; - while (s->value <= 0) { + while (s->value == 0) { rc = sched_tsleep(&s->event, timeout); if (rc == SLP_TIMEOUT) { err = ETIMEDOUT; goto out; - } else if (rc == SLP_INTR) { + } + if (rc == SLP_INTR) { err = EINTR; goto out; } @@ -227,9 +225,9 @@ /* * Unlock a semaphore. * - * If the semaphore value becomes non zero, then one of the threads - * blocked waiting for the semaphore will be unblocked. - * This is non-blocking operation. + * If the semaphore value becomes non zero, then one of + * the threads blocked waiting for the semaphore will be + * unblocked. This is non-blocking operation. */ int sem_post(sem_t *sem)