From 045278e11b53fc1ad59945427feab1cd9275988f Mon Sep 17 00:00:00 2001 From: rpj Date: Fri, 22 Oct 2004 15:06:41 +0000 Subject: Changes to mutexes and semaphores - considered alpha for now --- sem_destroy.c | 43 ++++++++++++++++++++++++++++++++++++++----- 1 file changed, 38 insertions(+), 5 deletions(-) (limited to 'sem_destroy.c') diff --git a/sem_destroy.c b/sem_destroy.c index 733afd3..3463981 100644 --- a/sem_destroy.c +++ b/sem_destroy.c @@ -98,11 +98,44 @@ sem_destroy (sem_t * sem) #else /* NEED_SEM */ - if (!CloseHandle (s->sem)) - { - *sem = s; - result = EINVAL; - } + if ((result = pthread_mutex_trylock (&s->lock)) == 0) + { + if (s->value >= 0) + { + (void) pthread_mutex_unlock (&s->lock); + + if (!CloseHandle (s->sem)) + { + *sem = s; + result = EINVAL; + } + else if ((result = pthread_mutex_destroy (&s->lock)) != 0) + { + s->sem = CreateSemaphore (NULL, /* Always NULL */ + (long) 0, /* Force threads to wait */ + (long) _POSIX_SEM_VALUE_MAX, /* Maximum value */ + NULL); /* Name */ + if (s->sem == 0) + { + /* We just have to pretend that we've destroyed the semaphore + * even though we're leaving a mutex around. + */ + result = 0; + } + else + { + *sem = s; + if (result != EBUSY) + result = EINVAL; + } + } + } + else + { + (void) pthread_mutex_unlock (&s->lock); + result = EBUSY; + } + } #endif /* NEED_SEM */ -- cgit v1.2.3