pthread_mutex_trylock.c 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. /*
  2. * pthread_mutex_trylock.c
  3. *
  4. * Description:
  5. * This translation unit implements mutual exclusion (mutex) primitives.
  6. *
  7. * --------------------------------------------------------------------------
  8. *
  9. * Pthreads-win32 - POSIX Threads Library for Win32
  10. * Copyright(C) 1998 John E. Bossom
  11. * Copyright(C) 1999,2005 Pthreads-win32 contributors
  12. *
  13. * Contact Email: [email protected]
  14. *
  15. * The current list of contributors is contained
  16. * in the file CONTRIBUTORS included with the source
  17. * code distribution. The list can also be seen at the
  18. * following World Wide Web location:
  19. * http://sources.redhat.com/pthreads-win32/contributors.html
  20. *
  21. * This library is free software; you can redistribute it and/or
  22. * modify it under the terms of the GNU Lesser General Public
  23. * License as published by the Free Software Foundation; either
  24. * version 2 of the License, or (at your option) any later version.
  25. *
  26. * This library is distributed in the hope that it will be useful,
  27. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  28. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  29. * Lesser General Public License for more details.
  30. *
  31. * You should have received a copy of the GNU Lesser General Public
  32. * License along with this library in the file COPYING.LIB;
  33. * if not, write to the Free Software Foundation, Inc.,
  34. * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
  35. */
  36. #include "pthread.h"
  37. #include "implement.h"
  38. int
  39. pthread_mutex_trylock (pthread_mutex_t * mutex)
  40. {
  41. pthread_mutex_t mx;
  42. int kind;
  43. int result = 0;
  44. /*
  45. * Let the system deal with invalid pointers.
  46. */
  47. /*
  48. * We do a quick check to see if we need to do more work
  49. * to initialise a static mutex. We check
  50. * again inside the guarded section of ptw32_mutex_check_need_init()
  51. * to avoid race conditions.
  52. */
  53. if (*mutex >= PTHREAD_ERRORCHECK_MUTEX_INITIALIZER)
  54. {
  55. if ((result = ptw32_mutex_check_need_init (mutex)) != 0)
  56. {
  57. return (result);
  58. }
  59. }
  60. mx = *mutex;
  61. kind = mx->kind;
  62. if (kind >= 0)
  63. {
  64. /* Non-robust */
  65. if (0 == (PTW32_INTERLOCKED_LONG) PTW32_INTERLOCKED_COMPARE_EXCHANGE_LONG (
  66. (PTW32_INTERLOCKED_LONGPTR) &mx->lock_idx,
  67. (PTW32_INTERLOCKED_LONG) 1,
  68. (PTW32_INTERLOCKED_LONG) 0))
  69. {
  70. if (kind != PTHREAD_MUTEX_NORMAL)
  71. {
  72. mx->recursive_count = 1;
  73. mx->ownerThread = pthread_self ();
  74. }
  75. }
  76. else
  77. {
  78. if (kind == PTHREAD_MUTEX_RECURSIVE &&
  79. pthread_equal (mx->ownerThread, pthread_self ()))
  80. {
  81. mx->recursive_count++;
  82. }
  83. else
  84. {
  85. result = EBUSY;
  86. }
  87. }
  88. }
  89. else
  90. {
  91. /*
  92. * Robust types
  93. * All types record the current owner thread.
  94. * The mutex is added to a per thread list when ownership is acquired.
  95. */
  96. pthread_t self;
  97. ptw32_robust_state_t* statePtr = &mx->robustNode->stateInconsistent;
  98. if ((PTW32_INTERLOCKED_LONG)PTW32_ROBUST_NOTRECOVERABLE ==
  99. PTW32_INTERLOCKED_EXCHANGE_ADD_LONG(
  100. (PTW32_INTERLOCKED_LONGPTR)statePtr,
  101. (PTW32_INTERLOCKED_LONG)0))
  102. {
  103. return ENOTRECOVERABLE;
  104. }
  105. self = pthread_self();
  106. kind = -kind - 1; /* Convert to non-robust range */
  107. if (0 == (PTW32_INTERLOCKED_LONG) PTW32_INTERLOCKED_COMPARE_EXCHANGE_LONG (
  108. (PTW32_INTERLOCKED_LONGPTR) &mx->lock_idx,
  109. (PTW32_INTERLOCKED_LONG) 1,
  110. (PTW32_INTERLOCKED_LONG) 0))
  111. {
  112. if (kind != PTHREAD_MUTEX_NORMAL)
  113. {
  114. mx->recursive_count = 1;
  115. }
  116. ptw32_robust_mutex_add(mutex, self);
  117. }
  118. else
  119. {
  120. if (PTHREAD_MUTEX_RECURSIVE == kind &&
  121. pthread_equal (mx->ownerThread, pthread_self ()))
  122. {
  123. mx->recursive_count++;
  124. }
  125. else
  126. {
  127. if (EOWNERDEAD == (result = ptw32_robust_mutex_inherit(mutex)))
  128. {
  129. mx->recursive_count = 1;
  130. ptw32_robust_mutex_add(mutex, self);
  131. }
  132. else
  133. {
  134. if (0 == result)
  135. {
  136. result = EBUSY;
  137. }
  138. }
  139. }
  140. }
  141. }
  142. return (result);
  143. }