pthread_mutex_lock.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494
  1. /* Copyright (C) 2002-2007, 2008, 2009 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, write to the Free
  14. Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
  15. 02111-1307 USA. */
  16. #include <assert.h>
  17. #include <errno.h>
  18. #include <stdlib.h>
  19. #include <unistd.h>
  20. #include <not-cancel.h>
  21. #include "pthreadP.h"
  22. #include <lowlevellock.h>
  23. #ifndef LLL_MUTEX_LOCK
  24. # define LLL_MUTEX_LOCK(mutex) \
  25. lll_lock ((mutex)->__data.__lock, PTHREAD_MUTEX_PSHARED (mutex))
  26. # define LLL_MUTEX_TRYLOCK(mutex) \
  27. lll_trylock ((mutex)->__data.__lock)
  28. # define LLL_ROBUST_MUTEX_LOCK(mutex, id) \
  29. lll_robust_lock ((mutex)->__data.__lock, id, \
  30. PTHREAD_ROBUST_MUTEX_PSHARED (mutex))
  31. #endif
  32. static int __pthread_mutex_lock_full (pthread_mutex_t *mutex)
  33. __attribute_noinline__;
  34. int
  35. __pthread_mutex_lock (
  36. pthread_mutex_t *mutex)
  37. {
  38. assert (sizeof (mutex->__size) >= sizeof (mutex->__data));
  39. unsigned int type = PTHREAD_MUTEX_TYPE (mutex);
  40. if (__builtin_expect (type & ~PTHREAD_MUTEX_KIND_MASK_NP, 0))
  41. return __pthread_mutex_lock_full (mutex);
  42. pid_t id = THREAD_GETMEM (THREAD_SELF, tid);
  43. if (__builtin_expect (type, PTHREAD_MUTEX_TIMED_NP)
  44. == PTHREAD_MUTEX_TIMED_NP)
  45. {
  46. simple:
  47. /* Normal mutex. */
  48. LLL_MUTEX_LOCK (mutex);
  49. assert (mutex->__data.__owner == 0);
  50. }
  51. else if (__builtin_expect (type == PTHREAD_MUTEX_RECURSIVE_NP, 1))
  52. {
  53. /* Recursive mutex. */
  54. /* Check whether we already hold the mutex. */
  55. if (mutex->__data.__owner == id)
  56. {
  57. /* Just bump the counter. */
  58. if (__builtin_expect (mutex->__data.__count + 1 == 0, 0))
  59. /* Overflow of the counter. */
  60. return EAGAIN;
  61. ++mutex->__data.__count;
  62. return 0;
  63. }
  64. /* We have to get the mutex. */
  65. LLL_MUTEX_LOCK (mutex);
  66. assert (mutex->__data.__owner == 0);
  67. mutex->__data.__count = 1;
  68. }
  69. else if (__builtin_expect (type == PTHREAD_MUTEX_ADAPTIVE_NP, 1))
  70. {
  71. if (! __is_smp)
  72. goto simple;
  73. if (LLL_MUTEX_TRYLOCK (mutex) != 0)
  74. {
  75. int cnt = 0;
  76. int max_cnt = MIN (MAX_ADAPTIVE_COUNT,
  77. mutex->__data.__spins * 2 + 10);
  78. do
  79. {
  80. if (cnt++ >= max_cnt)
  81. {
  82. LLL_MUTEX_LOCK (mutex);
  83. break;
  84. }
  85. #ifdef BUSY_WAIT_NOP
  86. BUSY_WAIT_NOP;
  87. #endif
  88. }
  89. while (LLL_MUTEX_TRYLOCK (mutex) != 0);
  90. mutex->__data.__spins += (cnt - mutex->__data.__spins) / 8;
  91. }
  92. assert (mutex->__data.__owner == 0);
  93. }
  94. else
  95. {
  96. assert (type == PTHREAD_MUTEX_ERRORCHECK_NP);
  97. /* Check whether we already hold the mutex. */
  98. if (__builtin_expect (mutex->__data.__owner == id, 0))
  99. return EDEADLK;
  100. goto simple;
  101. }
  102. /* Record the ownership. */
  103. mutex->__data.__owner = id;
  104. #ifndef NO_INCR
  105. ++mutex->__data.__nusers;
  106. #endif
  107. return 0;
  108. }
  109. static int
  110. __pthread_mutex_lock_full (pthread_mutex_t *mutex)
  111. {
  112. int oldval;
  113. pid_t id = THREAD_GETMEM (THREAD_SELF, tid);
  114. switch (PTHREAD_MUTEX_TYPE (mutex))
  115. {
  116. case PTHREAD_MUTEX_ROBUST_RECURSIVE_NP:
  117. case PTHREAD_MUTEX_ROBUST_ERRORCHECK_NP:
  118. case PTHREAD_MUTEX_ROBUST_NORMAL_NP:
  119. case PTHREAD_MUTEX_ROBUST_ADAPTIVE_NP:
  120. THREAD_SETMEM (THREAD_SELF, robust_head.list_op_pending,
  121. &mutex->__data.__list.__next);
  122. oldval = mutex->__data.__lock;
  123. do
  124. {
  125. again:
  126. if ((oldval & FUTEX_OWNER_DIED) != 0)
  127. {
  128. /* The previous owner died. Try locking the mutex. */
  129. int newval = id;
  130. #ifdef NO_INCR
  131. newval |= FUTEX_WAITERS;
  132. #else
  133. newval |= (oldval & FUTEX_WAITERS);
  134. #endif
  135. newval
  136. = atomic_compare_and_exchange_val_acq (&mutex->__data.__lock,
  137. newval, oldval);
  138. if (newval != oldval)
  139. {
  140. oldval = newval;
  141. goto again;
  142. }
  143. /* We got the mutex. */
  144. mutex->__data.__count = 1;
  145. /* But it is inconsistent unless marked otherwise. */
  146. mutex->__data.__owner = PTHREAD_MUTEX_INCONSISTENT;
  147. ENQUEUE_MUTEX (mutex);
  148. THREAD_SETMEM (THREAD_SELF, robust_head.list_op_pending, NULL);
  149. /* Note that we deliberately exit here. If we fall
  150. through to the end of the function __nusers would be
  151. incremented which is not correct because the old
  152. owner has to be discounted. If we are not supposed
  153. to increment __nusers we actually have to decrement
  154. it here. */
  155. #ifdef NO_INCR
  156. --mutex->__data.__nusers;
  157. #endif
  158. return EOWNERDEAD;
  159. }
  160. /* Check whether we already hold the mutex. */
  161. if (__builtin_expect ((oldval & FUTEX_TID_MASK) == id, 0))
  162. {
  163. int kind = PTHREAD_MUTEX_TYPE (mutex);
  164. if (kind == PTHREAD_MUTEX_ROBUST_ERRORCHECK_NP)
  165. {
  166. THREAD_SETMEM (THREAD_SELF, robust_head.list_op_pending,
  167. NULL);
  168. return EDEADLK;
  169. }
  170. if (kind == PTHREAD_MUTEX_ROBUST_RECURSIVE_NP)
  171. {
  172. THREAD_SETMEM (THREAD_SELF, robust_head.list_op_pending,
  173. NULL);
  174. /* Just bump the counter. */
  175. if (__builtin_expect (mutex->__data.__count + 1 == 0, 0))
  176. /* Overflow of the counter. */
  177. return EAGAIN;
  178. ++mutex->__data.__count;
  179. return 0;
  180. }
  181. }
  182. oldval = LLL_ROBUST_MUTEX_LOCK (mutex, id);
  183. if (__builtin_expect (mutex->__data.__owner
  184. == PTHREAD_MUTEX_NOTRECOVERABLE, 0))
  185. {
  186. /* This mutex is now not recoverable. */
  187. mutex->__data.__count = 0;
  188. lll_unlock (mutex->__data.__lock,
  189. PTHREAD_ROBUST_MUTEX_PSHARED (mutex));
  190. THREAD_SETMEM (THREAD_SELF, robust_head.list_op_pending, NULL);
  191. return ENOTRECOVERABLE;
  192. }
  193. }
  194. while ((oldval & FUTEX_OWNER_DIED) != 0);
  195. mutex->__data.__count = 1;
  196. ENQUEUE_MUTEX (mutex);
  197. THREAD_SETMEM (THREAD_SELF, robust_head.list_op_pending, NULL);
  198. break;
  199. case PTHREAD_MUTEX_PI_RECURSIVE_NP:
  200. case PTHREAD_MUTEX_PI_ERRORCHECK_NP:
  201. case PTHREAD_MUTEX_PI_NORMAL_NP:
  202. case PTHREAD_MUTEX_PI_ADAPTIVE_NP:
  203. case PTHREAD_MUTEX_PI_ROBUST_RECURSIVE_NP:
  204. case PTHREAD_MUTEX_PI_ROBUST_ERRORCHECK_NP:
  205. case PTHREAD_MUTEX_PI_ROBUST_NORMAL_NP:
  206. case PTHREAD_MUTEX_PI_ROBUST_ADAPTIVE_NP:
  207. {
  208. int kind = mutex->__data.__kind & PTHREAD_MUTEX_KIND_MASK_NP;
  209. int robust = mutex->__data.__kind & PTHREAD_MUTEX_ROBUST_NORMAL_NP;
  210. if (robust)
  211. /* Note: robust PI futexes are signaled by setting bit 0. */
  212. THREAD_SETMEM (THREAD_SELF, robust_head.list_op_pending,
  213. (void *) (((uintptr_t) &mutex->__data.__list.__next)
  214. | 1));
  215. oldval = mutex->__data.__lock;
  216. /* Check whether we already hold the mutex. */
  217. if (__builtin_expect ((oldval & FUTEX_TID_MASK) == id, 0))
  218. {
  219. if (kind == PTHREAD_MUTEX_ERRORCHECK_NP)
  220. {
  221. THREAD_SETMEM (THREAD_SELF, robust_head.list_op_pending, NULL);
  222. return EDEADLK;
  223. }
  224. if (kind == PTHREAD_MUTEX_RECURSIVE_NP)
  225. {
  226. THREAD_SETMEM (THREAD_SELF, robust_head.list_op_pending, NULL);
  227. /* Just bump the counter. */
  228. if (__builtin_expect (mutex->__data.__count + 1 == 0, 0))
  229. /* Overflow of the counter. */
  230. return EAGAIN;
  231. ++mutex->__data.__count;
  232. return 0;
  233. }
  234. }
  235. int newval = id;
  236. #ifdef NO_INCR
  237. newval |= FUTEX_WAITERS;
  238. #endif
  239. oldval = atomic_compare_and_exchange_val_acq (&mutex->__data.__lock,
  240. newval, 0);
  241. if (oldval != 0)
  242. {
  243. /* The mutex is locked. The kernel will now take care of
  244. everything. */
  245. int private = (robust
  246. ? PTHREAD_ROBUST_MUTEX_PSHARED (mutex)
  247. : PTHREAD_MUTEX_PSHARED (mutex));
  248. INTERNAL_SYSCALL_DECL (__err);
  249. int e = INTERNAL_SYSCALL (futex, __err, 4, &mutex->__data.__lock,
  250. __lll_private_flag (FUTEX_LOCK_PI,
  251. private), 1, 0);
  252. if (INTERNAL_SYSCALL_ERROR_P (e, __err)
  253. && (INTERNAL_SYSCALL_ERRNO (e, __err) == ESRCH
  254. || INTERNAL_SYSCALL_ERRNO (e, __err) == EDEADLK))
  255. {
  256. assert (INTERNAL_SYSCALL_ERRNO (e, __err) != EDEADLK
  257. || (kind != PTHREAD_MUTEX_ERRORCHECK_NP
  258. && kind != PTHREAD_MUTEX_RECURSIVE_NP));
  259. /* ESRCH can happen only for non-robust PI mutexes where
  260. the owner of the lock died. */
  261. assert (INTERNAL_SYSCALL_ERRNO (e, __err) != ESRCH || !robust);
  262. /* Delay the thread indefinitely. */
  263. while (1)
  264. pause_not_cancel ();
  265. }
  266. oldval = mutex->__data.__lock;
  267. assert (robust || (oldval & FUTEX_OWNER_DIED) == 0);
  268. }
  269. if (__builtin_expect (oldval & FUTEX_OWNER_DIED, 0))
  270. {
  271. atomic_and (&mutex->__data.__lock, ~FUTEX_OWNER_DIED);
  272. /* We got the mutex. */
  273. mutex->__data.__count = 1;
  274. /* But it is inconsistent unless marked otherwise. */
  275. mutex->__data.__owner = PTHREAD_MUTEX_INCONSISTENT;
  276. ENQUEUE_MUTEX_PI (mutex);
  277. THREAD_SETMEM (THREAD_SELF, robust_head.list_op_pending, NULL);
  278. /* Note that we deliberately exit here. If we fall
  279. through to the end of the function __nusers would be
  280. incremented which is not correct because the old owner
  281. has to be discounted. If we are not supposed to
  282. increment __nusers we actually have to decrement it here. */
  283. #ifdef NO_INCR
  284. --mutex->__data.__nusers;
  285. #endif
  286. return EOWNERDEAD;
  287. }
  288. if (robust
  289. && __builtin_expect (mutex->__data.__owner
  290. == PTHREAD_MUTEX_NOTRECOVERABLE, 0))
  291. {
  292. /* This mutex is now not recoverable. */
  293. mutex->__data.__count = 0;
  294. INTERNAL_SYSCALL_DECL (__err);
  295. INTERNAL_SYSCALL (futex, __err, 4, &mutex->__data.__lock,
  296. __lll_private_flag (FUTEX_UNLOCK_PI,
  297. PTHREAD_ROBUST_MUTEX_PSHARED (mutex)),
  298. 0, 0);
  299. THREAD_SETMEM (THREAD_SELF, robust_head.list_op_pending, NULL);
  300. return ENOTRECOVERABLE;
  301. }
  302. mutex->__data.__count = 1;
  303. if (robust)
  304. {
  305. ENQUEUE_MUTEX_PI (mutex);
  306. THREAD_SETMEM (THREAD_SELF, robust_head.list_op_pending, NULL);
  307. }
  308. }
  309. break;
  310. case PTHREAD_MUTEX_PP_RECURSIVE_NP:
  311. case PTHREAD_MUTEX_PP_ERRORCHECK_NP:
  312. case PTHREAD_MUTEX_PP_NORMAL_NP:
  313. case PTHREAD_MUTEX_PP_ADAPTIVE_NP:
  314. {
  315. int kind = mutex->__data.__kind & PTHREAD_MUTEX_KIND_MASK_NP;
  316. oldval = mutex->__data.__lock;
  317. /* Check whether we already hold the mutex. */
  318. if (mutex->__data.__owner == id)
  319. {
  320. if (kind == PTHREAD_MUTEX_ERRORCHECK_NP)
  321. return EDEADLK;
  322. if (kind == PTHREAD_MUTEX_RECURSIVE_NP)
  323. {
  324. /* Just bump the counter. */
  325. if (__builtin_expect (mutex->__data.__count + 1 == 0, 0))
  326. /* Overflow of the counter. */
  327. return EAGAIN;
  328. ++mutex->__data.__count;
  329. return 0;
  330. }
  331. }
  332. int oldprio = -1, ceilval;
  333. do
  334. {
  335. int ceiling = (oldval & PTHREAD_MUTEX_PRIO_CEILING_MASK)
  336. >> PTHREAD_MUTEX_PRIO_CEILING_SHIFT;
  337. if (__pthread_current_priority () > ceiling)
  338. {
  339. if (oldprio != -1)
  340. __pthread_tpp_change_priority (oldprio, -1);
  341. return EINVAL;
  342. }
  343. int retval = __pthread_tpp_change_priority (oldprio, ceiling);
  344. if (retval)
  345. return retval;
  346. ceilval = ceiling << PTHREAD_MUTEX_PRIO_CEILING_SHIFT;
  347. oldprio = ceiling;
  348. oldval
  349. = atomic_compare_and_exchange_val_acq (&mutex->__data.__lock,
  350. #ifdef NO_INCR
  351. ceilval | 2,
  352. #else
  353. ceilval | 1,
  354. #endif
  355. ceilval);
  356. if (oldval == ceilval)
  357. break;
  358. do
  359. {
  360. oldval
  361. = atomic_compare_and_exchange_val_acq (&mutex->__data.__lock,
  362. ceilval | 2,
  363. ceilval | 1);
  364. if ((oldval & PTHREAD_MUTEX_PRIO_CEILING_MASK) != ceilval)
  365. break;
  366. if (oldval != ceilval)
  367. lll_futex_wait (&mutex->__data.__lock, ceilval | 2,
  368. PTHREAD_MUTEX_PSHARED (mutex));
  369. }
  370. while (atomic_compare_and_exchange_val_acq (&mutex->__data.__lock,
  371. ceilval | 2, ceilval)
  372. != ceilval);
  373. }
  374. while ((oldval & PTHREAD_MUTEX_PRIO_CEILING_MASK) != ceilval);
  375. assert (mutex->__data.__owner == 0);
  376. mutex->__data.__count = 1;
  377. }
  378. break;
  379. default:
  380. /* Correct code cannot set any other type. */
  381. return EINVAL;
  382. }
  383. /* Record the ownership. */
  384. mutex->__data.__owner = id;
  385. #ifndef NO_INCR
  386. ++mutex->__data.__nusers;
  387. #endif
  388. return 0;
  389. }
  390. #ifndef __pthread_mutex_lock
  391. strong_alias (__pthread_mutex_lock, pthread_mutex_lock)
  392. strong_alias (__pthread_mutex_lock, __pthread_mutex_lock_internal)
  393. #endif
  394. #ifdef NO_INCR
  395. void
  396. __pthread_mutex_cond_lock_adjust (
  397. pthread_mutex_t *mutex)
  398. {
  399. assert ((mutex->__data.__kind & PTHREAD_MUTEX_PRIO_INHERIT_NP) != 0);
  400. assert ((mutex->__data.__kind & PTHREAD_MUTEX_ROBUST_NORMAL_NP) == 0);
  401. assert ((mutex->__data.__kind & PTHREAD_MUTEX_PSHARED_BIT) == 0);
  402. /* Record the ownership. */
  403. pid_t id = THREAD_GETMEM (THREAD_SELF, tid);
  404. mutex->__data.__owner = id;
  405. if (mutex->__data.__kind == PTHREAD_MUTEX_PI_RECURSIVE_NP)
  406. ++mutex->__data.__count;
  407. }
  408. #endif