aboutsummaryrefslogtreecommitdiff
path: root/pthreads/pthread_mutex_unlock.c
diff options
context:
space:
mode:
Diffstat (limited to 'pthreads/pthread_mutex_unlock.c')
-rw-r--r--pthreads/pthread_mutex_unlock.c144
1 files changed, 100 insertions, 44 deletions
diff --git a/pthreads/pthread_mutex_unlock.c b/pthreads/pthread_mutex_unlock.c
index 9ebe4e378..3d65d1a95 100644
--- a/pthreads/pthread_mutex_unlock.c
+++ b/pthreads/pthread_mutex_unlock.c
@@ -42,6 +42,7 @@ int
pthread_mutex_unlock (pthread_mutex_t * mutex)
{
int result = 0;
+ int kind;
pthread_mutex_t mx;
/*
@@ -57,60 +58,115 @@ pthread_mutex_unlock (pthread_mutex_t * mutex)
*/
if (mx < PTHREAD_ERRORCHECK_MUTEX_INITIALIZER)
{
- if (mx->kind == PTHREAD_MUTEX_NORMAL)
- {
- LONG idx;
+ kind = mx->kind;
- idx = (LONG) PTW32_INTERLOCKED_EXCHANGE ((LPLONG) &mx->lock_idx,
- (LONG) 0);
- if (idx != 0)
+ if (kind >= 0)
+ {
+ if (kind == PTHREAD_MUTEX_NORMAL)
{
- if (idx < 0)
- {
- /*
- * Someone may be waiting on that mutex.
- */
- if (SetEvent (mx->event) == 0)
- {
- result = EINVAL;
- }
- }
- }
- else
- {
- /*
- * Was not locked (so can't be owned by us).
- */
- result = EPERM;
- }
- }
- else
- {
- if (pthread_equal (mx->ownerThread, pthread_self ()))
- {
- if (mx->kind != PTHREAD_MUTEX_RECURSIVE
- || 0 == --mx->recursive_count)
- {
- mx->ownerThread.p = NULL;
+ LONG idx;
- if ((LONG) PTW32_INTERLOCKED_EXCHANGE ((LPLONG) &mx->lock_idx,
- (LONG) 0) < 0)
+ idx = (LONG) PTW32_INTERLOCKED_EXCHANGE_LONG ((PTW32_INTERLOCKED_LONGPTR)&mx->lock_idx,
+ (PTW32_INTERLOCKED_LONG)0);
+ if (idx != 0)
+ {
+ if (idx < 0)
{
- /* Someone may be waiting on that mutex */
+ /*
+ * Someone may be waiting on that mutex.
+ */
if (SetEvent (mx->event) == 0)
- {
- result = EINVAL;
- }
+ {
+ result = EINVAL;
+ }
}
- }
+ }
}
- else
+ else
{
- result = EPERM;
+ if (pthread_equal (mx->ownerThread, pthread_self()))
+ {
+ if (kind != PTHREAD_MUTEX_RECURSIVE
+ || 0 == --mx->recursive_count)
+ {
+ mx->ownerThread.p = NULL;
+
+ if ((LONG) PTW32_INTERLOCKED_EXCHANGE_LONG ((PTW32_INTERLOCKED_LONGPTR)&mx->lock_idx,
+ (PTW32_INTERLOCKED_LONG)0) < 0L)
+ {
+ /* Someone may be waiting on that mutex */
+ if (SetEvent (mx->event) == 0)
+ {
+ result = EINVAL;
+ }
+ }
+ }
+ }
+ else
+ {
+ result = EPERM;
+ }
}
- }
+ }
+ else
+ {
+ /* Robust types */
+ pthread_t self = pthread_self();
+ kind = -kind - 1; /* Convert to non-robust range */
+
+ /*
+ * The thread must own the lock regardless of type if the mutex
+ * is robust.
+ */
+ if (pthread_equal (mx->ownerThread, self))
+ {
+ PTW32_INTERLOCKED_COMPARE_EXCHANGE_LONG((PTW32_INTERLOCKED_LONGPTR) &mx->robustNode->stateInconsistent,
+ (PTW32_INTERLOCKED_LONG)PTW32_ROBUST_NOTRECOVERABLE,
+ (PTW32_INTERLOCKED_LONG)PTW32_ROBUST_INCONSISTENT);
+ if (PTHREAD_MUTEX_NORMAL == kind)
+ {
+ ptw32_robust_mutex_remove(mutex, NULL);
+
+ if ((LONG) PTW32_INTERLOCKED_EXCHANGE_LONG((PTW32_INTERLOCKED_LONGPTR) &mx->lock_idx,
+ (PTW32_INTERLOCKED_LONG) 0) < 0)
+ {
+ /*
+ * Someone may be waiting on that mutex.
+ */
+ if (SetEvent (mx->event) == 0)
+ {
+ result = EINVAL;
+ }
+ }
+ }
+ else
+ {
+ if (kind != PTHREAD_MUTEX_RECURSIVE
+ || 0 == --mx->recursive_count)
+ {
+ ptw32_robust_mutex_remove(mutex, NULL);
+
+ if ((LONG) PTW32_INTERLOCKED_EXCHANGE_LONG((PTW32_INTERLOCKED_LONGPTR) &mx->lock_idx,
+ (PTW32_INTERLOCKED_LONG) 0) < 0)
+ {
+ /*
+ * Someone may be waiting on that mutex.
+ */
+ if (SetEvent (mx->event) == 0)
+ {
+ result = EINVAL;
+ }
+ }
+ }
+ }
+ }
+ else
+ {
+ result = EPERM;
+ }
+ }
}
- else
+ else if (mx != PTHREAD_MUTEX_INITIALIZER)
{
result = EINVAL;
}