pthread_mutex_lock.c 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269
  1. /*
  2. * pthread_mutex_lock.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. #if !defined(_UWIN)
  37. /*# include <process.h> */
  38. #endif
  39. #include "pthread.h"
  40. #include "implement.h"
  41. int
  42. pthread_mutex_lock (pthread_mutex_t * mutex)
  43. {
  44. int kind;
  45. pthread_mutex_t mx;
  46. int result = 0;
  47. /*
  48. * Let the system deal with invalid pointers.
  49. */
  50. if (*mutex == NULL)
  51. {
  52. return EINVAL;
  53. }
  54. /*
  55. * We do a quick check to see if we need to do more work
  56. * to initialise a static mutex. We check
  57. * again inside the guarded section of ptw32_mutex_check_need_init()
  58. * to avoid race conditions.
  59. */
  60. if (*mutex >= PTHREAD_ERRORCHECK_MUTEX_INITIALIZER)
  61. {
  62. if ((result = ptw32_mutex_check_need_init (mutex)) != 0)
  63. {
  64. return (result);
  65. }
  66. }
  67. mx = *mutex;
  68. kind = mx->kind;
  69. if (kind >= 0)
  70. {
  71. /* Non-robust */
  72. if (PTHREAD_MUTEX_NORMAL == kind)
  73. {
  74. if ((PTW32_INTERLOCKED_LONG) PTW32_INTERLOCKED_EXCHANGE_LONG(
  75. (PTW32_INTERLOCKED_LONGPTR) &mx->lock_idx,
  76. (PTW32_INTERLOCKED_LONG) 1) != 0)
  77. {
  78. while ((PTW32_INTERLOCKED_LONG) PTW32_INTERLOCKED_EXCHANGE_LONG(
  79. (PTW32_INTERLOCKED_LONGPTR) &mx->lock_idx,
  80. (PTW32_INTERLOCKED_LONG) -1) != 0)
  81. {
  82. if (WAIT_OBJECT_0 != WaitForSingleObject (mx->event, INFINITE))
  83. {
  84. result = EINVAL;
  85. break;
  86. }
  87. }
  88. }
  89. }
  90. else
  91. {
  92. pthread_t self = pthread_self();
  93. if ((PTW32_INTERLOCKED_LONG) PTW32_INTERLOCKED_COMPARE_EXCHANGE_LONG(
  94. (PTW32_INTERLOCKED_LONGPTR) &mx->lock_idx,
  95. (PTW32_INTERLOCKED_LONG) 1,
  96. (PTW32_INTERLOCKED_LONG) 0) == 0)
  97. {
  98. mx->recursive_count = 1;
  99. mx->ownerThread = self;
  100. }
  101. else
  102. {
  103. if (pthread_equal (mx->ownerThread, self))
  104. {
  105. if (kind == PTHREAD_MUTEX_RECURSIVE)
  106. {
  107. mx->recursive_count++;
  108. }
  109. else
  110. {
  111. result = EDEADLK;
  112. }
  113. }
  114. else
  115. {
  116. while ((PTW32_INTERLOCKED_LONG) PTW32_INTERLOCKED_EXCHANGE_LONG(
  117. (PTW32_INTERLOCKED_LONGPTR) &mx->lock_idx,
  118. (PTW32_INTERLOCKED_LONG) -1) != 0)
  119. {
  120. if (WAIT_OBJECT_0 != WaitForSingleObject (mx->event, INFINITE))
  121. {
  122. result = EINVAL;
  123. break;
  124. }
  125. }
  126. if (0 == result)
  127. {
  128. mx->recursive_count = 1;
  129. mx->ownerThread = self;
  130. }
  131. }
  132. }
  133. }
  134. }
  135. else
  136. {
  137. /*
  138. * Robust types
  139. * All types record the current owner thread.
  140. * The mutex is added to a per thread list when ownership is acquired.
  141. */
  142. ptw32_robust_state_t* statePtr = &mx->robustNode->stateInconsistent;
  143. if ((PTW32_INTERLOCKED_LONG)PTW32_ROBUST_NOTRECOVERABLE == PTW32_INTERLOCKED_EXCHANGE_ADD_LONG(
  144. (PTW32_INTERLOCKED_LONGPTR)statePtr,
  145. (PTW32_INTERLOCKED_LONG)0))
  146. {
  147. result = ENOTRECOVERABLE;
  148. }
  149. else
  150. {
  151. pthread_t self = pthread_self();
  152. kind = -kind - 1; /* Convert to non-robust range */
  153. if (PTHREAD_MUTEX_NORMAL == kind)
  154. {
  155. if ((PTW32_INTERLOCKED_LONG) PTW32_INTERLOCKED_EXCHANGE_LONG(
  156. (PTW32_INTERLOCKED_LONGPTR) &mx->lock_idx,
  157. (PTW32_INTERLOCKED_LONG) 1) != 0)
  158. {
  159. while (0 == (result = ptw32_robust_mutex_inherit(mutex))
  160. && (PTW32_INTERLOCKED_LONG) PTW32_INTERLOCKED_EXCHANGE_LONG(
  161. (PTW32_INTERLOCKED_LONGPTR) &mx->lock_idx,
  162. (PTW32_INTERLOCKED_LONG) -1) != 0)
  163. {
  164. if (WAIT_OBJECT_0 != WaitForSingleObject (mx->event, INFINITE))
  165. {
  166. result = EINVAL;
  167. break;
  168. }
  169. if ((PTW32_INTERLOCKED_LONG)PTW32_ROBUST_NOTRECOVERABLE ==
  170. PTW32_INTERLOCKED_EXCHANGE_ADD_LONG(
  171. (PTW32_INTERLOCKED_LONGPTR)statePtr,
  172. (PTW32_INTERLOCKED_LONG)0))
  173. {
  174. /* Unblock the next thread */
  175. SetEvent(mx->event);
  176. result = ENOTRECOVERABLE;
  177. break;
  178. }
  179. }
  180. }
  181. if (0 == result || EOWNERDEAD == result)
  182. {
  183. /*
  184. * Add mutex to the per-thread robust mutex currently-held list.
  185. * If the thread terminates, all mutexes in this list will be unlocked.
  186. */
  187. ptw32_robust_mutex_add(mutex, self);
  188. }
  189. }
  190. else
  191. {
  192. if ((PTW32_INTERLOCKED_LONG) PTW32_INTERLOCKED_COMPARE_EXCHANGE_LONG(
  193. (PTW32_INTERLOCKED_LONGPTR) &mx->lock_idx,
  194. (PTW32_INTERLOCKED_LONG) 1,
  195. (PTW32_INTERLOCKED_LONG) 0) == 0)
  196. {
  197. mx->recursive_count = 1;
  198. /*
  199. * Add mutex to the per-thread robust mutex currently-held list.
  200. * If the thread terminates, all mutexes in this list will be unlocked.
  201. */
  202. ptw32_robust_mutex_add(mutex, self);
  203. }
  204. else
  205. {
  206. if (pthread_equal (mx->ownerThread, self))
  207. {
  208. if (PTHREAD_MUTEX_RECURSIVE == kind)
  209. {
  210. mx->recursive_count++;
  211. }
  212. else
  213. {
  214. result = EDEADLK;
  215. }
  216. }
  217. else
  218. {
  219. while (0 == (result = ptw32_robust_mutex_inherit(mutex))
  220. && (PTW32_INTERLOCKED_LONG) PTW32_INTERLOCKED_EXCHANGE_LONG(
  221. (PTW32_INTERLOCKED_LONGPTR) &mx->lock_idx,
  222. (PTW32_INTERLOCKED_LONG) -1) != 0)
  223. {
  224. if (WAIT_OBJECT_0 != WaitForSingleObject (mx->event, INFINITE))
  225. {
  226. result = EINVAL;
  227. break;
  228. }
  229. if ((PTW32_INTERLOCKED_LONG)PTW32_ROBUST_NOTRECOVERABLE ==
  230. PTW32_INTERLOCKED_EXCHANGE_ADD_LONG(
  231. (PTW32_INTERLOCKED_LONGPTR)statePtr,
  232. (PTW32_INTERLOCKED_LONG)0))
  233. {
  234. /* Unblock the next thread */
  235. SetEvent(mx->event);
  236. result = ENOTRECOVERABLE;
  237. break;
  238. }
  239. }
  240. if (0 == result || EOWNERDEAD == result)
  241. {
  242. mx->recursive_count = 1;
  243. /*
  244. * Add mutex to the per-thread robust mutex currently-held list.
  245. * If the thread terminates, all mutexes in this list will be unlocked.
  246. */
  247. ptw32_robust_mutex_add(mutex, self);
  248. }
  249. }
  250. }
  251. }
  252. }
  253. }
  254. return (result);
  255. }