From 7e2e924362df5556ff55ee7e1b8738a05d067ec4 Mon Sep 17 00:00:00 2001 From: rpj Date: Tue, 4 Jun 2002 07:14:57 +0000 Subject: Another attempt to get sem_getvalue working. --- sem_getvalue.c | 28 ++++++++++------------------ 1 file changed, 10 insertions(+), 18 deletions(-) (limited to 'sem_getvalue.c') diff --git a/sem_getvalue.c b/sem_getvalue.c index b3033d3..297bc6c 100644 --- a/sem_getvalue.c +++ b/sem_getvalue.c @@ -76,6 +76,7 @@ sem_getvalue(sem_t * sem, int * sval) */ { int result = 0; + long value; if ( sem == NULL || *sem == NULL || sval == NULL ) { @@ -89,27 +90,17 @@ sem_getvalue(sem_t * sem, int * sval) result = ENOSYS; #else - long value = *sval; - - /* From "Win32 System Programming - Second Edition" - * by Johnson M Hart, chapter 9, page 256 : - * - * "The release count must be greater than zero, but if it - * would cause the semaphore count to exceed the maximum, - * the call will fail, returning FALSE, and the count will - * remain unchanged. Releasing a semaphore with a large count - * is a method used to obtain the current count atomically - * (of course, another thread might change the value immediately)." - */ - - if ( ! ReleaseSemaphore( (*sem)->sem, _POSIX_SEM_VALUE_MAX + 1, &value) ) + + if ( WaitForSingleObject( (*sem)->sem, 0 ) == WAIT_TIMEOUT ) { - *sval = value; + /* Failed - must be zero. */ + value = 0; } else { - /* This should NEVER occur. */ - result = ENOSYS; + /* Decremented sema - release it and note the value. */ + (void) ReleaseSemaphore( (*sem)->sem, 1L, &value ); + value++; } #endif @@ -121,7 +112,8 @@ sem_getvalue(sem_t * sem, int * sval) errno = result; return -1; } - + + *sval = value; return 0; } /* sem_getvalue */ -- cgit v1.2.3