summaryrefslogtreecommitdiff
path: root/pthread_mutex_unlock.c
diff options
context:
space:
mode:
authorrpj <rpj>2011-03-24 23:33:14 +0000
committerrpj <rpj>2011-03-24 23:33:14 +0000
commitdb171f2f9435b98f05f33fcbc0dcf0c5cc1cb917 (patch)
tree9d617a20a9e0ad1fcf415e353057c53e6d77d0f3 /pthread_mutex_unlock.c
parente5229a33f8724a90cbb0b56c3ecc1d6691bf54d7 (diff)
First pass of robust mutexes
Diffstat (limited to 'pthread_mutex_unlock.c')
-rw-r--r--pthread_mutex_unlock.c158
1 files changed, 114 insertions, 44 deletions
diff --git a/pthread_mutex_unlock.c b/pthread_mutex_unlock.c
index 9ebe4e3..7a3f009 100644
--- a/pthread_mutex_unlock.c
+++ b/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,129 @@ 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 ((LPLONG) &mx->lock_idx,
+ (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 ((LPLONG) &mx->lock_idx,
+ (LONG) 0) < 0)
+ {
+ /* 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((LPLONG) &mx->robustNode->stateInconsistent,
+ (LONG)PTW32_ROBUST_NOTRECOVERABLE,
+ (LONG)PTW32_ROBUST_INCONSISTENT);
+ if (PTHREAD_MUTEX_NORMAL == kind)
+ {
+#if 1
+ ptw32_robust_mutex_remove(mutex);
+#else
+ mx->ownerThread.p = NULL;
+#endif
+
+ if ((LONG) PTW32_INTERLOCKED_EXCHANGE((LPLONG) &mx->lock_idx,
+ (LONG) 0) < 0)
+ {
+#if 0
+ ptw32_robust_mutex_remove(mutex, self);
+#endif
+ /*
+ * Someone may be waiting on that mutex.
+ */
+ if (SetEvent (mx->event) == 0)
+ {
+ result = EINVAL;
+ }
+ }
+ }
+ else
+ {
+ if (kind != PTHREAD_MUTEX_RECURSIVE
+ || 0 == --mx->recursive_count)
+ {
+#if 1
+ ptw32_robust_mutex_remove(mutex);
+#else
+ mx->ownerThread.p = NULL;
+#endif
+
+ if ((LONG) PTW32_INTERLOCKED_EXCHANGE((LPLONG) &mx->lock_idx,
+ (LONG) 0) < 0)
+ {
+#if 0
+ ptw32_robust_mutex_remove(mutex, self);
+#endif
+ /*
+ * 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;
}