summaryrefslogtreecommitdiff
path: root/pthread_mutex_trylock.c
diff options
context:
space:
mode:
Diffstat (limited to 'pthread_mutex_trylock.c')
-rw-r--r--pthread_mutex_trylock.c47
1 files changed, 23 insertions, 24 deletions
diff --git a/pthread_mutex_trylock.c b/pthread_mutex_trylock.c
index 6f137f0..bba8ed2 100644
--- a/pthread_mutex_trylock.c
+++ b/pthread_mutex_trylock.c
@@ -44,10 +44,9 @@ pthread_mutex_trylock (pthread_mutex_t * mutex)
int result = 0;
pthread_mutex_t mx;
- if (mutex == NULL || *mutex == NULL)
- {
- return EINVAL;
- }
+ /*
+ * Let the system deal with invalid pointers.
+ */
/*
* We do a quick check to see if we need to do more work
@@ -57,36 +56,36 @@ pthread_mutex_trylock (pthread_mutex_t * mutex)
*/
if (*mutex >= PTHREAD_ERRORCHECK_MUTEX_INITIALIZER)
{
- result = ptw32_mutex_check_need_init (mutex);
+ if ((result = ptw32_mutex_check_need_init (mutex)) != 0)
+ {
+ return (result);
+ }
}
mx = *mutex;
- if (result == 0)
+ if ((PTW32_INTERLOCKED_LONG) -1 ==
+ ptw32_interlocked_compare_exchange ((PTW32_INTERLOCKED_LPLONG) &
+ mx->lock_idx,
+ (PTW32_INTERLOCKED_LONG) 0,
+ (PTW32_INTERLOCKED_LONG) -1))
{
- if ((PTW32_INTERLOCKED_LONG) PTW32_MUTEX_LOCK_IDX_INIT ==
- ptw32_interlocked_compare_exchange ((PTW32_INTERLOCKED_LPLONG) &
- mx->lock_idx,
- (PTW32_INTERLOCKED_LONG) 0,
- (PTW32_INTERLOCKED_LONG)
- PTW32_MUTEX_LOCK_IDX_INIT))
+ if (mx->kind != PTHREAD_MUTEX_NORMAL)
{
mx->recursive_count = 1;
- mx->ownerThread = (mx->kind != PTHREAD_MUTEX_FAST_NP
- ? pthread_self ()
- : (pthread_t) PTW32_MUTEX_OWNER_ANONYMOUS);
+ mx->ownerThread = pthread_self ();
+ }
+ }
+ else
+ {
+ if (mx->kind == PTHREAD_MUTEX_RECURSIVE &&
+ pthread_equal (mx->ownerThread, pthread_self ()))
+ {
+ mx->recursive_count++;
}
else
{
- if (mx->kind == PTHREAD_MUTEX_RECURSIVE_NP &&
- pthread_equal (mx->ownerThread, pthread_self ()))
- {
- mx->recursive_count++;
- }
- else
- {
- result = EBUSY;
- }
+ result = EBUSY;
}
}