summaryrefslogtreecommitdiff
path: root/sem_getvalue.c
diff options
context:
space:
mode:
authorrpj <rpj>2002-06-04 07:14:57 +0000
committerrpj <rpj>2002-06-04 07:14:57 +0000
commit7e2e924362df5556ff55ee7e1b8738a05d067ec4 (patch)
tree3de0bb7796ad39d02c27d1eec4a3e14d52c64732 /sem_getvalue.c
parentb2bca276f0fdb43e7ba4aa6baefe835d23820164 (diff)
Another attempt to get sem_getvalue working.
Diffstat (limited to 'sem_getvalue.c')
-rw-r--r--sem_getvalue.c28
1 files changed, 10 insertions, 18 deletions
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 */