pthread_mutex_unlock.c 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. /*
  2. * pthread_mutex_unlock.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_unlock (pthread_mutex_t * mutex)
  40. {
  41. int result = 0;
  42. int kind;
  43. pthread_mutex_t mx;
  44. /*
  45. * Let the system deal with invalid pointers.
  46. */
  47. mx = *mutex;
  48. /*
  49. * If the thread calling us holds the mutex then there is no
  50. * race condition. If another thread holds the
  51. * lock then we shouldn't be in here.
  52. */
  53. if (mx < PTHREAD_ERRORCHECK_MUTEX_INITIALIZER)
  54. {
  55. kind = mx->kind;
  56. if (kind >= 0)
  57. {
  58. if (kind == PTHREAD_MUTEX_NORMAL)
  59. {
  60. LONG idx;
  61. idx = (LONG) PTW32_INTERLOCKED_EXCHANGE_LONG ((PTW32_INTERLOCKED_LONGPTR)&mx->lock_idx,
  62. (PTW32_INTERLOCKED_LONG)0);
  63. if (idx != 0)
  64. {
  65. if (idx < 0)
  66. {
  67. /*
  68. * Someone may be waiting on that mutex.
  69. */
  70. if (SetEvent (mx->event) == 0)
  71. {
  72. result = EINVAL;
  73. }
  74. }
  75. }
  76. }
  77. else
  78. {
  79. if (pthread_equal (mx->ownerThread, pthread_self()))
  80. {
  81. if (kind != PTHREAD_MUTEX_RECURSIVE
  82. || 0 == --mx->recursive_count)
  83. {
  84. mx->ownerThread.p = NULL;
  85. if ((LONG) PTW32_INTERLOCKED_EXCHANGE_LONG ((PTW32_INTERLOCKED_LONGPTR)&mx->lock_idx,
  86. (PTW32_INTERLOCKED_LONG)0) < 0L)
  87. {
  88. /* Someone may be waiting on that mutex */
  89. if (SetEvent (mx->event) == 0)
  90. {
  91. result = EINVAL;
  92. }
  93. }
  94. }
  95. }
  96. else
  97. {
  98. result = EPERM;
  99. }
  100. }
  101. }
  102. else
  103. {
  104. /* Robust types */
  105. pthread_t self = pthread_self();
  106. kind = -kind - 1; /* Convert to non-robust range */
  107. /*
  108. * The thread must own the lock regardless of type if the mutex
  109. * is robust.
  110. */
  111. if (pthread_equal (mx->ownerThread, self))
  112. {
  113. PTW32_INTERLOCKED_COMPARE_EXCHANGE_LONG((PTW32_INTERLOCKED_LONGPTR) &mx->robustNode->stateInconsistent,
  114. (PTW32_INTERLOCKED_LONG)PTW32_ROBUST_NOTRECOVERABLE,
  115. (PTW32_INTERLOCKED_LONG)PTW32_ROBUST_INCONSISTENT);
  116. if (PTHREAD_MUTEX_NORMAL == kind)
  117. {
  118. ptw32_robust_mutex_remove(mutex, NULL);
  119. if ((LONG) PTW32_INTERLOCKED_EXCHANGE_LONG((PTW32_INTERLOCKED_LONGPTR) &mx->lock_idx,
  120. (PTW32_INTERLOCKED_LONG) 0) < 0)
  121. {
  122. /*
  123. * Someone may be waiting on that mutex.
  124. */
  125. if (SetEvent (mx->event) == 0)
  126. {
  127. result = EINVAL;
  128. }
  129. }
  130. }
  131. else
  132. {
  133. if (kind != PTHREAD_MUTEX_RECURSIVE
  134. || 0 == --mx->recursive_count)
  135. {
  136. ptw32_robust_mutex_remove(mutex, NULL);
  137. if ((LONG) PTW32_INTERLOCKED_EXCHANGE_LONG((PTW32_INTERLOCKED_LONGPTR) &mx->lock_idx,
  138. (PTW32_INTERLOCKED_LONG) 0) < 0)
  139. {
  140. /*
  141. * Someone may be waiting on that mutex.
  142. */
  143. if (SetEvent (mx->event) == 0)
  144. {
  145. result = EINVAL;
  146. }
  147. }
  148. }
  149. }
  150. }
  151. else
  152. {
  153. result = EPERM;
  154. }
  155. }
  156. }
  157. else if (mx != PTHREAD_MUTEX_INITIALIZER)
  158. {
  159. result = EINVAL;
  160. }
  161. return (result);
  162. }