summaryrefslogtreecommitdiff
path: root/sem_wait.c
diff options
context:
space:
mode:
Diffstat (limited to 'sem_wait.c')
-rw-r--r--sem_wait.c28
1 files changed, 18 insertions, 10 deletions
diff --git a/sem_wait.c b/sem_wait.c
index bcb17e8..05d7326 100644
--- a/sem_wait.c
+++ b/sem_wait.c
@@ -53,19 +53,27 @@ ptw32_sem_wait_cleanup(void * sem)
if (pthread_mutex_lock (&s->lock) == 0)
{
- ++s->value;
-#ifdef NEED_SEM
-
- if (s->value > 0)
- {
- s->leftToUnblock = 0;
- }
-#else
/*
- * Don't release the W32 sema, it doesn't need adjustment
- * because it doesn't record the number of waiters.
+ * If the sema is posted between us being cancelled and us locking
+ * the sema again above then we need to consume that post but cancel
+ * anyway. If we don't get the semaphore we indicate that we're no
+ * longer waiting.
*/
+ if (!(WaitForSingleObject(s->sem, 0) == WAIT_OBJECT_0))
+ {
+ ++s->value;
+#ifdef NEED_SEM
+ if (s->value > 0)
+ {
+ s->leftToUnblock = 0;
+ }
+#else
+ /*
+ * Don't release the W32 sema, it doesn't need adjustment
+ * because it doesn't record the number of waiters.
+ */
#endif /* NEED_SEM */
+ }
(void) pthread_mutex_unlock (&s->lock);
}
}