Only allow the semaphore to be taken when the value is positive.

Don't decrement it twice when it is taken.

Reported by:	kris
This commit is contained in:
deischen 2004-02-06 13:54:38 +00:00
parent 3ef89b60eb
commit f8f3e115d3

View File

@ -304,14 +304,13 @@ __sem_trywait(sem_t *sem)
retval = ksem_trywait((*sem)->semid);
else {
_pthread_mutex_lock(&(*sem)->lock);
if ((*sem)->count != 0) {
if ((*sem)->count > 0) {
(*sem)->count--;
retval = 0;
} else {
errno = EAGAIN;
retval = -1;
}
(*sem)->count--;
_pthread_mutex_unlock(&(*sem)->lock);
}
return (retval);