pthread_mutex_timedlock.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324
  1. /*
  2. * pthread_mutex_timedlock.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. static INLINE int
  39. ptw32_timed_eventwait (HANDLE event, const struct timespec *abstime)
  40. /*
  41. * ------------------------------------------------------
  42. * DESCRIPTION
  43. * This function waits on an event until signaled or until
  44. * abstime passes.
  45. * If abstime has passed when this routine is called then
  46. * it returns a result to indicate this.
  47. *
  48. * If 'abstime' is a NULL pointer then this function will
  49. * block until it can successfully decrease the value or
  50. * until interrupted by a signal.
  51. *
  52. * This routine is not a cancelation point.
  53. *
  54. * RESULTS
  55. * 0 successfully signaled,
  56. * ETIMEDOUT abstime passed
  57. * EINVAL 'event' is not a valid event,
  58. *
  59. * ------------------------------------------------------
  60. */
  61. {
  62. DWORD milliseconds;
  63. DWORD status;
  64. if (event == NULL)
  65. {
  66. return EINVAL;
  67. }
  68. else
  69. {
  70. if (abstime == NULL)
  71. {
  72. milliseconds = INFINITE;
  73. }
  74. else
  75. {
  76. /*
  77. * Calculate timeout as milliseconds from current system time.
  78. */
  79. milliseconds = ptw32_relmillisecs (abstime);
  80. }
  81. status = WaitForSingleObject (event, milliseconds);
  82. if (status == WAIT_OBJECT_0)
  83. {
  84. return 0;
  85. }
  86. else if (status == WAIT_TIMEOUT)
  87. {
  88. return ETIMEDOUT;
  89. }
  90. else
  91. {
  92. return EINVAL;
  93. }
  94. }
  95. return 0;
  96. } /* ptw32_timed_semwait */
  97. int
  98. pthread_mutex_timedlock (pthread_mutex_t * mutex,
  99. const struct timespec *abstime)
  100. {
  101. pthread_mutex_t mx;
  102. int kind;
  103. int result = 0;
  104. /*
  105. * Let the system deal with invalid pointers.
  106. */
  107. /*
  108. * We do a quick check to see if we need to do more work
  109. * to initialise a static mutex. We check
  110. * again inside the guarded section of ptw32_mutex_check_need_init()
  111. * to avoid race conditions.
  112. */
  113. if (*mutex >= PTHREAD_ERRORCHECK_MUTEX_INITIALIZER)
  114. {
  115. if ((result = ptw32_mutex_check_need_init (mutex)) != 0)
  116. {
  117. return (result);
  118. }
  119. }
  120. mx = *mutex;
  121. kind = mx->kind;
  122. if (kind >= 0)
  123. {
  124. if (mx->kind == PTHREAD_MUTEX_NORMAL)
  125. {
  126. if ((PTW32_INTERLOCKED_LONG) PTW32_INTERLOCKED_EXCHANGE_LONG(
  127. (PTW32_INTERLOCKED_LONGPTR) &mx->lock_idx,
  128. (PTW32_INTERLOCKED_LONG) 1) != 0)
  129. {
  130. while ((PTW32_INTERLOCKED_LONG) PTW32_INTERLOCKED_EXCHANGE_LONG(
  131. (PTW32_INTERLOCKED_LONGPTR) &mx->lock_idx,
  132. (PTW32_INTERLOCKED_LONG) -1) != 0)
  133. {
  134. if (0 != (result = ptw32_timed_eventwait (mx->event, abstime)))
  135. {
  136. return result;
  137. }
  138. }
  139. }
  140. }
  141. else
  142. {
  143. pthread_t self = pthread_self();
  144. if ((PTW32_INTERLOCKED_LONG) PTW32_INTERLOCKED_COMPARE_EXCHANGE_LONG(
  145. (PTW32_INTERLOCKED_LONGPTR) &mx->lock_idx,
  146. (PTW32_INTERLOCKED_LONG) 1,
  147. (PTW32_INTERLOCKED_LONG) 0) == 0)
  148. {
  149. mx->recursive_count = 1;
  150. mx->ownerThread = self;
  151. }
  152. else
  153. {
  154. if (pthread_equal (mx->ownerThread, self))
  155. {
  156. if (mx->kind == PTHREAD_MUTEX_RECURSIVE)
  157. {
  158. mx->recursive_count++;
  159. }
  160. else
  161. {
  162. return EDEADLK;
  163. }
  164. }
  165. else
  166. {
  167. while ((PTW32_INTERLOCKED_LONG) PTW32_INTERLOCKED_EXCHANGE_LONG(
  168. (PTW32_INTERLOCKED_LONGPTR) &mx->lock_idx,
  169. (PTW32_INTERLOCKED_LONG) -1) != 0)
  170. {
  171. if (0 != (result = ptw32_timed_eventwait (mx->event, abstime)))
  172. {
  173. return result;
  174. }
  175. }
  176. mx->recursive_count = 1;
  177. mx->ownerThread = self;
  178. }
  179. }
  180. }
  181. }
  182. else
  183. {
  184. /*
  185. * Robust types
  186. * All types record the current owner thread.
  187. * The mutex is added to a per thread list when ownership is acquired.
  188. */
  189. ptw32_robust_state_t* statePtr = &mx->robustNode->stateInconsistent;
  190. if ((PTW32_INTERLOCKED_LONG)PTW32_ROBUST_NOTRECOVERABLE == PTW32_INTERLOCKED_EXCHANGE_ADD_LONG(
  191. (PTW32_INTERLOCKED_LONGPTR)statePtr,
  192. (PTW32_INTERLOCKED_LONG)0))
  193. {
  194. result = ENOTRECOVERABLE;
  195. }
  196. else
  197. {
  198. pthread_t self = pthread_self();
  199. kind = -kind - 1; /* Convert to non-robust range */
  200. if (PTHREAD_MUTEX_NORMAL == kind)
  201. {
  202. if ((PTW32_INTERLOCKED_LONG) PTW32_INTERLOCKED_EXCHANGE_LONG(
  203. (PTW32_INTERLOCKED_LONGPTR) &mx->lock_idx,
  204. (PTW32_INTERLOCKED_LONG) 1) != 0)
  205. {
  206. while (0 == (result = ptw32_robust_mutex_inherit(mutex))
  207. && (PTW32_INTERLOCKED_LONG) PTW32_INTERLOCKED_EXCHANGE_LONG(
  208. (PTW32_INTERLOCKED_LONGPTR) &mx->lock_idx,
  209. (PTW32_INTERLOCKED_LONG) -1) != 0)
  210. {
  211. if (0 != (result = ptw32_timed_eventwait (mx->event, abstime)))
  212. {
  213. return result;
  214. }
  215. if ((PTW32_INTERLOCKED_LONG)PTW32_ROBUST_NOTRECOVERABLE ==
  216. PTW32_INTERLOCKED_EXCHANGE_ADD_LONG(
  217. (PTW32_INTERLOCKED_LONGPTR)statePtr,
  218. (PTW32_INTERLOCKED_LONG)0))
  219. {
  220. /* Unblock the next thread */
  221. SetEvent(mx->event);
  222. result = ENOTRECOVERABLE;
  223. break;
  224. }
  225. }
  226. if (0 == result || EOWNERDEAD == result)
  227. {
  228. /*
  229. * Add mutex to the per-thread robust mutex currently-held list.
  230. * If the thread terminates, all mutexes in this list will be unlocked.
  231. */
  232. ptw32_robust_mutex_add(mutex, self);
  233. }
  234. }
  235. }
  236. else
  237. {
  238. pthread_t self = pthread_self();
  239. if (0 == (PTW32_INTERLOCKED_LONG) PTW32_INTERLOCKED_COMPARE_EXCHANGE_LONG(
  240. (PTW32_INTERLOCKED_LONGPTR) &mx->lock_idx,
  241. (PTW32_INTERLOCKED_LONG) 1,
  242. (PTW32_INTERLOCKED_LONG) 0))
  243. {
  244. mx->recursive_count = 1;
  245. /*
  246. * Add mutex to the per-thread robust mutex currently-held list.
  247. * If the thread terminates, all mutexes in this list will be unlocked.
  248. */
  249. ptw32_robust_mutex_add(mutex, self);
  250. }
  251. else
  252. {
  253. if (pthread_equal (mx->ownerThread, self))
  254. {
  255. if (PTHREAD_MUTEX_RECURSIVE == kind)
  256. {
  257. mx->recursive_count++;
  258. }
  259. else
  260. {
  261. return EDEADLK;
  262. }
  263. }
  264. else
  265. {
  266. while (0 == (result = ptw32_robust_mutex_inherit(mutex))
  267. && (PTW32_INTERLOCKED_LONG) PTW32_INTERLOCKED_EXCHANGE_LONG(
  268. (PTW32_INTERLOCKED_LONGPTR) &mx->lock_idx,
  269. (PTW32_INTERLOCKED_LONG) -1) != 0)
  270. {
  271. if (0 != (result = ptw32_timed_eventwait (mx->event, abstime)))
  272. {
  273. return result;
  274. }
  275. }
  276. if ((PTW32_INTERLOCKED_LONG)PTW32_ROBUST_NOTRECOVERABLE ==
  277. PTW32_INTERLOCKED_EXCHANGE_ADD_LONG(
  278. (PTW32_INTERLOCKED_LONGPTR)statePtr,
  279. (PTW32_INTERLOCKED_LONG)0))
  280. {
  281. /* Unblock the next thread */
  282. SetEvent(mx->event);
  283. result = ENOTRECOVERABLE;
  284. }
  285. else if (0 == result || EOWNERDEAD == result)
  286. {
  287. mx->recursive_count = 1;
  288. /*
  289. * Add mutex to the per-thread robust mutex currently-held list.
  290. * If the thread terminates, all mutexes in this list will be unlocked.
  291. */
  292. ptw32_robust_mutex_add(mutex, self);
  293. }
  294. }
  295. }
  296. }
  297. }
  298. }
  299. return result;
  300. }