diff options
author | rpj <rpj> | 2004-10-22 15:06:41 +0000 |
---|---|---|
committer | rpj <rpj> | 2004-10-22 15:06:41 +0000 |
commit | 045278e11b53fc1ad59945427feab1cd9275988f (patch) | |
tree | da8570a7a8962d9563814c4910e8a9d5fb6fa685 /sem_destroy.c | |
parent | f84df26e12431bb9ecd07fbc52c804538635901f (diff) |
Changes to mutexes and semaphores - considered alpha for now
Diffstat (limited to 'sem_destroy.c')
-rw-r--r-- | sem_destroy.c | 43 |
1 files changed, 38 insertions, 5 deletions
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 */ |