pthread_mutex_trylock.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394
  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. #if defined(__UCLIBC_USE_TIME64__) && defined(__NR_futex_time64)
  195. int e = INTERNAL_SYSCALL (futex_time64, __err, 4, &mutex->__data.__lock,
  196. __lll_private_flag (FUTEX_TRYLOCK_PI,
  197. private), 0, 0);
  198. #else
  199. int e = INTERNAL_SYSCALL (futex, __err, 4, &mutex->__data.__lock,
  200. __lll_private_flag (FUTEX_TRYLOCK_PI,
  201. private), 0, 0);
  202. #endif
  203. if (INTERNAL_SYSCALL_ERROR_P (e, __err)
  204. && INTERNAL_SYSCALL_ERRNO (e, __err) == EWOULDBLOCK)
  205. {
  206. THREAD_SETMEM (THREAD_SELF, robust_head.list_op_pending, NULL);
  207. return EBUSY;
  208. }
  209. oldval = mutex->__data.__lock;
  210. }
  211. if (__builtin_expect (oldval & FUTEX_OWNER_DIED, 0))
  212. {
  213. atomic_and (&mutex->__data.__lock, ~FUTEX_OWNER_DIED);
  214. /* We got the mutex. */
  215. mutex->__data.__count = 1;
  216. /* But it is inconsistent unless marked otherwise. */
  217. mutex->__data.__owner = PTHREAD_MUTEX_INCONSISTENT;
  218. ENQUEUE_MUTEX (mutex);
  219. THREAD_SETMEM (THREAD_SELF, robust_head.list_op_pending, NULL);
  220. /* Note that we deliberately exit here. If we fall
  221. through to the end of the function __nusers would be
  222. incremented which is not correct because the old owner
  223. has to be discounted. */
  224. return EOWNERDEAD;
  225. }
  226. if (robust
  227. && __builtin_expect (mutex->__data.__owner
  228. == PTHREAD_MUTEX_NOTRECOVERABLE, 0))
  229. {
  230. /* This mutex is now not recoverable. */
  231. mutex->__data.__count = 0;
  232. INTERNAL_SYSCALL_DECL (__err);
  233. #if defined(__UCLIBC_USE_TIME64__) && defined(__NR_futex_time64)
  234. INTERNAL_SYSCALL (futex_time64, __err, 4, &mutex->__data.__lock,
  235. __lll_private_flag (FUTEX_UNLOCK_PI,
  236. PTHREAD_ROBUST_MUTEX_PSHARED (mutex)),
  237. 0, 0);
  238. #else
  239. INTERNAL_SYSCALL (futex, __err, 4, &mutex->__data.__lock,
  240. __lll_private_flag (FUTEX_UNLOCK_PI,
  241. PTHREAD_ROBUST_MUTEX_PSHARED (mutex)),
  242. 0, 0);
  243. #endif
  244. THREAD_SETMEM (THREAD_SELF, robust_head.list_op_pending, NULL);
  245. return ENOTRECOVERABLE;
  246. }
  247. if (robust)
  248. {
  249. ENQUEUE_MUTEX_PI (mutex);
  250. THREAD_SETMEM (THREAD_SELF, robust_head.list_op_pending, NULL);
  251. }
  252. mutex->__data.__owner = id;
  253. ++mutex->__data.__nusers;
  254. mutex->__data.__count = 1;
  255. return 0;
  256. }
  257. case PTHREAD_MUTEX_PP_RECURSIVE_NP:
  258. case PTHREAD_MUTEX_PP_ERRORCHECK_NP:
  259. case PTHREAD_MUTEX_PP_NORMAL_NP:
  260. case PTHREAD_MUTEX_PP_ADAPTIVE_NP:
  261. {
  262. int kind = mutex->__data.__kind & PTHREAD_MUTEX_KIND_MASK_NP;
  263. oldval = mutex->__data.__lock;
  264. /* Check whether we already hold the mutex. */
  265. if (mutex->__data.__owner == id)
  266. {
  267. if (kind == PTHREAD_MUTEX_ERRORCHECK_NP)
  268. return EDEADLK;
  269. if (kind == PTHREAD_MUTEX_RECURSIVE_NP)
  270. {
  271. /* Just bump the counter. */
  272. if (__builtin_expect (mutex->__data.__count + 1 == 0, 0))
  273. /* Overflow of the counter. */
  274. return EAGAIN;
  275. ++mutex->__data.__count;
  276. return 0;
  277. }
  278. }
  279. int oldprio = -1, ceilval;
  280. do
  281. {
  282. int ceiling = (oldval & PTHREAD_MUTEX_PRIO_CEILING_MASK)
  283. >> PTHREAD_MUTEX_PRIO_CEILING_SHIFT;
  284. if (__pthread_current_priority () > ceiling)
  285. {
  286. if (oldprio != -1)
  287. __pthread_tpp_change_priority (oldprio, -1);
  288. return EINVAL;
  289. }
  290. int retval = __pthread_tpp_change_priority (oldprio, ceiling);
  291. if (retval)
  292. return retval;
  293. ceilval = ceiling << PTHREAD_MUTEX_PRIO_CEILING_SHIFT;
  294. oldprio = ceiling;
  295. oldval
  296. = atomic_compare_and_exchange_val_acq (&mutex->__data.__lock,
  297. ceilval | 1, ceilval);
  298. if (oldval == ceilval)
  299. break;
  300. }
  301. while ((oldval & PTHREAD_MUTEX_PRIO_CEILING_MASK) != ceilval);
  302. if (oldval != ceilval)
  303. {
  304. __pthread_tpp_change_priority (oldprio, -1);
  305. break;
  306. }
  307. assert (mutex->__data.__owner == 0);
  308. /* Record the ownership. */
  309. mutex->__data.__owner = id;
  310. ++mutex->__data.__nusers;
  311. mutex->__data.__count = 1;
  312. return 0;
  313. }
  314. break;
  315. default:
  316. /* Correct code cannot set any other type. */
  317. return EINVAL;
  318. }
  319. return EBUSY;
  320. }
  321. strong_alias (__pthread_mutex_trylock, pthread_mutex_trylock)