pthread_mutex_trylock.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381
  1. /* Copyright (C) 2002, 2003, 2005-2007, 2008 Free Software Foundation, Inc.
  2. This file is part of the GNU C Library.
  3. Contributed by Ulrich Drepper <drepper@redhat.com>, 2002.
  4. The GNU C Library is free software; you can redistribute it and/or
  5. modify it under the terms of the GNU Lesser General Public
  6. License as published by the Free Software Foundation; either
  7. version 2.1 of the License, or (at your option) any later version.
  8. The GNU C Library is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. Lesser General Public License for more details.
  12. You should have received a copy of the GNU Lesser General Public
  13. License along with the GNU C Library; if not, see
  14. <http://www.gnu.org/licenses/>. */
  15. #include <assert.h>
  16. #include <errno.h>
  17. #include <stdlib.h>
  18. #include "pthreadP.h"
  19. #include <lowlevellock.h>
  20. int
  21. __pthread_mutex_trylock (
  22. pthread_mutex_t *mutex)
  23. {
  24. int oldval;
  25. pid_t id = THREAD_GETMEM (THREAD_SELF, tid);
  26. switch (__builtin_expect (PTHREAD_MUTEX_TYPE (mutex),
  27. PTHREAD_MUTEX_TIMED_NP))
  28. {
  29. /* Recursive mutex. */
  30. case PTHREAD_MUTEX_RECURSIVE_NP:
  31. /* Check whether we already hold the mutex. */
  32. if (mutex->__data.__owner == id)
  33. {
  34. /* Just bump the counter. */
  35. if (__builtin_expect (mutex->__data.__count + 1 == 0, 0))
  36. /* Overflow of the counter. */
  37. return EAGAIN;
  38. ++mutex->__data.__count;
  39. return 0;
  40. }
  41. if (lll_trylock (mutex->__data.__lock) == 0)
  42. {
  43. /* Record the ownership. */
  44. mutex->__data.__owner = id;
  45. mutex->__data.__count = 1;
  46. ++mutex->__data.__nusers;
  47. return 0;
  48. }
  49. break;
  50. case PTHREAD_MUTEX_ERRORCHECK_NP:
  51. case PTHREAD_MUTEX_TIMED_NP:
  52. case PTHREAD_MUTEX_ADAPTIVE_NP:
  53. /* Normal mutex. */
  54. if (lll_trylock (mutex->__data.__lock) != 0)
  55. break;
  56. /* Record the ownership. */
  57. mutex->__data.__owner = id;
  58. ++mutex->__data.__nusers;
  59. return 0;
  60. case PTHREAD_MUTEX_ROBUST_RECURSIVE_NP:
  61. case PTHREAD_MUTEX_ROBUST_ERRORCHECK_NP:
  62. case PTHREAD_MUTEX_ROBUST_NORMAL_NP:
  63. case PTHREAD_MUTEX_ROBUST_ADAPTIVE_NP:
  64. THREAD_SETMEM (THREAD_SELF, robust_head.list_op_pending,
  65. &mutex->__data.__list.__next);
  66. oldval = mutex->__data.__lock;
  67. do
  68. {
  69. again:
  70. if ((oldval & FUTEX_OWNER_DIED) != 0)
  71. {
  72. /* The previous owner died. Try locking the mutex. */
  73. int newval = id | (oldval & FUTEX_WAITERS);
  74. newval
  75. = atomic_compare_and_exchange_val_acq (&mutex->__data.__lock,
  76. newval, oldval);
  77. if (newval != oldval)
  78. {
  79. oldval = newval;
  80. goto again;
  81. }
  82. /* We got the mutex. */
  83. mutex->__data.__count = 1;
  84. /* But it is inconsistent unless marked otherwise. */
  85. mutex->__data.__owner = PTHREAD_MUTEX_INCONSISTENT;
  86. ENQUEUE_MUTEX (mutex);
  87. THREAD_SETMEM (THREAD_SELF, robust_head.list_op_pending, NULL);
  88. /* Note that we deliberately exist here. If we fall
  89. through to the end of the function __nusers would be
  90. incremented which is not correct because the old
  91. owner has to be discounted. */
  92. return EOWNERDEAD;
  93. }
  94. /* Check whether we already hold the mutex. */
  95. if (__builtin_expect ((oldval & FUTEX_TID_MASK) == id, 0))
  96. {
  97. int kind = PTHREAD_MUTEX_TYPE (mutex);
  98. if (kind == PTHREAD_MUTEX_ROBUST_ERRORCHECK_NP)
  99. {
  100. THREAD_SETMEM (THREAD_SELF, robust_head.list_op_pending,
  101. NULL);
  102. return EDEADLK;
  103. }
  104. if (kind == PTHREAD_MUTEX_ROBUST_RECURSIVE_NP)
  105. {
  106. THREAD_SETMEM (THREAD_SELF, robust_head.list_op_pending,
  107. NULL);
  108. /* Just bump the counter. */
  109. if (__builtin_expect (mutex->__data.__count + 1 == 0, 0))
  110. /* Overflow of the counter. */
  111. return EAGAIN;
  112. ++mutex->__data.__count;
  113. return 0;
  114. }
  115. }
  116. oldval = lll_robust_trylock (mutex->__data.__lock, id);
  117. if (oldval != 0 && (oldval & FUTEX_OWNER_DIED) == 0)
  118. {
  119. THREAD_SETMEM (THREAD_SELF, robust_head.list_op_pending, NULL);
  120. return EBUSY;
  121. }
  122. if (__builtin_expect (mutex->__data.__owner
  123. == PTHREAD_MUTEX_NOTRECOVERABLE, 0))
  124. {
  125. /* This mutex is now not recoverable. */
  126. mutex->__data.__count = 0;
  127. if (oldval == id)
  128. lll_unlock (mutex->__data.__lock,
  129. PTHREAD_ROBUST_MUTEX_PSHARED (mutex));
  130. THREAD_SETMEM (THREAD_SELF, robust_head.list_op_pending, NULL);
  131. return ENOTRECOVERABLE;
  132. }
  133. }
  134. while ((oldval & FUTEX_OWNER_DIED) != 0);
  135. ENQUEUE_MUTEX (mutex);
  136. THREAD_SETMEM (THREAD_SELF, robust_head.list_op_pending, NULL);
  137. mutex->__data.__owner = id;
  138. ++mutex->__data.__nusers;
  139. mutex->__data.__count = 1;
  140. return 0;
  141. case PTHREAD_MUTEX_PI_RECURSIVE_NP:
  142. case PTHREAD_MUTEX_PI_ERRORCHECK_NP:
  143. case PTHREAD_MUTEX_PI_NORMAL_NP:
  144. case PTHREAD_MUTEX_PI_ADAPTIVE_NP:
  145. case PTHREAD_MUTEX_PI_ROBUST_RECURSIVE_NP:
  146. case PTHREAD_MUTEX_PI_ROBUST_ERRORCHECK_NP:
  147. case PTHREAD_MUTEX_PI_ROBUST_NORMAL_NP:
  148. case PTHREAD_MUTEX_PI_ROBUST_ADAPTIVE_NP:
  149. {
  150. int kind = mutex->__data.__kind & PTHREAD_MUTEX_KIND_MASK_NP;
  151. int robust = mutex->__data.__kind & PTHREAD_MUTEX_ROBUST_NORMAL_NP;
  152. if (robust)
  153. /* Note: robust PI futexes are signaled by setting bit 0. */
  154. THREAD_SETMEM (THREAD_SELF, robust_head.list_op_pending,
  155. (void *) (((uintptr_t) &mutex->__data.__list.__next)
  156. | 1));
  157. oldval = mutex->__data.__lock;
  158. /* Check whether we already hold the mutex. */
  159. if (__builtin_expect ((oldval & FUTEX_TID_MASK) == id, 0))
  160. {
  161. if (kind == PTHREAD_MUTEX_ERRORCHECK_NP)
  162. {
  163. THREAD_SETMEM (THREAD_SELF, robust_head.list_op_pending, NULL);
  164. return EDEADLK;
  165. }
  166. if (kind == PTHREAD_MUTEX_RECURSIVE_NP)
  167. {
  168. THREAD_SETMEM (THREAD_SELF, robust_head.list_op_pending, NULL);
  169. /* Just bump the counter. */
  170. if (__builtin_expect (mutex->__data.__count + 1 == 0, 0))
  171. /* Overflow of the counter. */
  172. return EAGAIN;
  173. ++mutex->__data.__count;
  174. return 0;
  175. }
  176. }
  177. oldval
  178. = atomic_compare_and_exchange_val_acq (&mutex->__data.__lock,
  179. id, 0);
  180. if (oldval != 0)
  181. {
  182. if ((oldval & FUTEX_OWNER_DIED) == 0)
  183. {
  184. THREAD_SETMEM (THREAD_SELF, robust_head.list_op_pending, NULL);
  185. return EBUSY;
  186. }
  187. assert (robust);
  188. /* The mutex owner died. The kernel will now take care of
  189. everything. */
  190. int private = (robust
  191. ? PTHREAD_ROBUST_MUTEX_PSHARED (mutex)
  192. : PTHREAD_MUTEX_PSHARED (mutex));
  193. INTERNAL_SYSCALL_DECL (__err);
  194. int e = INTERNAL_SYSCALL (futex, __err, 4, &mutex->__data.__lock,
  195. __lll_private_flag (FUTEX_TRYLOCK_PI,
  196. private), 0, 0);
  197. if (INTERNAL_SYSCALL_ERROR_P (e, __err)
  198. && INTERNAL_SYSCALL_ERRNO (e, __err) == EWOULDBLOCK)
  199. {
  200. THREAD_SETMEM (THREAD_SELF, robust_head.list_op_pending, NULL);
  201. return EBUSY;
  202. }
  203. oldval = mutex->__data.__lock;
  204. }
  205. if (__builtin_expect (oldval & FUTEX_OWNER_DIED, 0))
  206. {
  207. atomic_and (&mutex->__data.__lock, ~FUTEX_OWNER_DIED);
  208. /* We got the mutex. */
  209. mutex->__data.__count = 1;
  210. /* But it is inconsistent unless marked otherwise. */
  211. mutex->__data.__owner = PTHREAD_MUTEX_INCONSISTENT;
  212. ENQUEUE_MUTEX (mutex);
  213. THREAD_SETMEM (THREAD_SELF, robust_head.list_op_pending, NULL);
  214. /* Note that we deliberately exit here. If we fall
  215. through to the end of the function __nusers would be
  216. incremented which is not correct because the old owner
  217. has to be discounted. */
  218. return EOWNERDEAD;
  219. }
  220. if (robust
  221. && __builtin_expect (mutex->__data.__owner
  222. == PTHREAD_MUTEX_NOTRECOVERABLE, 0))
  223. {
  224. /* This mutex is now not recoverable. */
  225. mutex->__data.__count = 0;
  226. INTERNAL_SYSCALL_DECL (__err);
  227. INTERNAL_SYSCALL (futex, __err, 4, &mutex->__data.__lock,
  228. __lll_private_flag (FUTEX_UNLOCK_PI,
  229. PTHREAD_ROBUST_MUTEX_PSHARED (mutex)),
  230. 0, 0);
  231. THREAD_SETMEM (THREAD_SELF, robust_head.list_op_pending, NULL);
  232. return ENOTRECOVERABLE;
  233. }
  234. if (robust)
  235. {
  236. ENQUEUE_MUTEX_PI (mutex);
  237. THREAD_SETMEM (THREAD_SELF, robust_head.list_op_pending, NULL);
  238. }
  239. mutex->__data.__owner = id;
  240. ++mutex->__data.__nusers;
  241. mutex->__data.__count = 1;
  242. return 0;
  243. }
  244. case PTHREAD_MUTEX_PP_RECURSIVE_NP:
  245. case PTHREAD_MUTEX_PP_ERRORCHECK_NP:
  246. case PTHREAD_MUTEX_PP_NORMAL_NP:
  247. case PTHREAD_MUTEX_PP_ADAPTIVE_NP:
  248. {
  249. int kind = mutex->__data.__kind & PTHREAD_MUTEX_KIND_MASK_NP;
  250. oldval = mutex->__data.__lock;
  251. /* Check whether we already hold the mutex. */
  252. if (mutex->__data.__owner == id)
  253. {
  254. if (kind == PTHREAD_MUTEX_ERRORCHECK_NP)
  255. return EDEADLK;
  256. if (kind == PTHREAD_MUTEX_RECURSIVE_NP)
  257. {
  258. /* Just bump the counter. */
  259. if (__builtin_expect (mutex->__data.__count + 1 == 0, 0))
  260. /* Overflow of the counter. */
  261. return EAGAIN;
  262. ++mutex->__data.__count;
  263. return 0;
  264. }
  265. }
  266. int oldprio = -1, ceilval;
  267. do
  268. {
  269. int ceiling = (oldval & PTHREAD_MUTEX_PRIO_CEILING_MASK)
  270. >> PTHREAD_MUTEX_PRIO_CEILING_SHIFT;
  271. if (__pthread_current_priority () > ceiling)
  272. {
  273. if (oldprio != -1)
  274. __pthread_tpp_change_priority (oldprio, -1);
  275. return EINVAL;
  276. }
  277. int retval = __pthread_tpp_change_priority (oldprio, ceiling);
  278. if (retval)
  279. return retval;
  280. ceilval = ceiling << PTHREAD_MUTEX_PRIO_CEILING_SHIFT;
  281. oldprio = ceiling;
  282. oldval
  283. = atomic_compare_and_exchange_val_acq (&mutex->__data.__lock,
  284. ceilval | 1, ceilval);
  285. if (oldval == ceilval)
  286. break;
  287. }
  288. while ((oldval & PTHREAD_MUTEX_PRIO_CEILING_MASK) != ceilval);
  289. if (oldval != ceilval)
  290. {
  291. __pthread_tpp_change_priority (oldprio, -1);
  292. break;
  293. }
  294. assert (mutex->__data.__owner == 0);
  295. /* Record the ownership. */
  296. mutex->__data.__owner = id;
  297. ++mutex->__data.__nusers;
  298. mutex->__data.__count = 1;
  299. return 0;
  300. }
  301. break;
  302. default:
  303. /* Correct code cannot set any other type. */
  304. return EINVAL;
  305. }
  306. return EBUSY;
  307. }
  308. strong_alias (__pthread_mutex_trylock, pthread_mutex_trylock)