pthread_mutex_lock.c 13 KB

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