pthread_mutex_lock.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512
  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. #if defined(__UCLIBC_USE_TIME64__) && defined(__NR_futex_time64)
  254. int e = INTERNAL_SYSCALL (futex_time64, __err, 4, &mutex->__data.__lock,
  255. __lll_private_flag (FUTEX_LOCK_PI,
  256. private), 1, 0);
  257. #else
  258. int e = INTERNAL_SYSCALL (futex, __err, 4, &mutex->__data.__lock,
  259. __lll_private_flag (FUTEX_LOCK_PI,
  260. private), 1, 0);
  261. #endif
  262. if (INTERNAL_SYSCALL_ERROR_P (e, __err)
  263. && (INTERNAL_SYSCALL_ERRNO (e, __err) == ESRCH
  264. || INTERNAL_SYSCALL_ERRNO (e, __err) == EDEADLK))
  265. {
  266. assert (INTERNAL_SYSCALL_ERRNO (e, __err) != EDEADLK
  267. || (kind != PTHREAD_MUTEX_ERRORCHECK_NP
  268. && kind != PTHREAD_MUTEX_RECURSIVE_NP));
  269. /* ESRCH can happen only for non-robust PI mutexes where
  270. the owner of the lock died. */
  271. assert (INTERNAL_SYSCALL_ERRNO (e, __err) != ESRCH || !robust);
  272. /* Delay the thread indefinitely. */
  273. while (1)
  274. pause_not_cancel ();
  275. }
  276. oldval = mutex->__data.__lock;
  277. assert (robust || (oldval & FUTEX_OWNER_DIED) == 0);
  278. }
  279. if (__builtin_expect (oldval & FUTEX_OWNER_DIED, 0))
  280. {
  281. atomic_and (&mutex->__data.__lock, ~FUTEX_OWNER_DIED);
  282. /* We got the mutex. */
  283. mutex->__data.__count = 1;
  284. /* But it is inconsistent unless marked otherwise. */
  285. mutex->__data.__owner = PTHREAD_MUTEX_INCONSISTENT;
  286. ENQUEUE_MUTEX_PI (mutex);
  287. THREAD_SETMEM (THREAD_SELF, robust_head.list_op_pending, NULL);
  288. /* Note that we deliberately exit here. If we fall
  289. through to the end of the function __nusers would be
  290. incremented which is not correct because the old owner
  291. has to be discounted. If we are not supposed to
  292. increment __nusers we actually have to decrement it here. */
  293. #ifdef NO_INCR
  294. --mutex->__data.__nusers;
  295. #endif
  296. return EOWNERDEAD;
  297. }
  298. if (robust
  299. && __builtin_expect (mutex->__data.__owner
  300. == PTHREAD_MUTEX_NOTRECOVERABLE, 0))
  301. {
  302. /* This mutex is now not recoverable. */
  303. mutex->__data.__count = 0;
  304. INTERNAL_SYSCALL_DECL (__err);
  305. #if defined(__UCLIBC_USE_TIME64__) && defined(__NR_futex_time64)
  306. INTERNAL_SYSCALL (futex_time64, __err, 4, &mutex->__data.__lock,
  307. __lll_private_flag (FUTEX_UNLOCK_PI,
  308. PTHREAD_ROBUST_MUTEX_PSHARED (mutex)),
  309. 0, 0);
  310. #else
  311. INTERNAL_SYSCALL (futex, __err, 4, &mutex->__data.__lock,
  312. __lll_private_flag (FUTEX_UNLOCK_PI,
  313. PTHREAD_ROBUST_MUTEX_PSHARED (mutex)),
  314. 0, 0);
  315. #endif
  316. THREAD_SETMEM (THREAD_SELF, robust_head.list_op_pending, NULL);
  317. return ENOTRECOVERABLE;
  318. }
  319. mutex->__data.__count = 1;
  320. if (robust)
  321. {
  322. ENQUEUE_MUTEX_PI (mutex);
  323. THREAD_SETMEM (THREAD_SELF, robust_head.list_op_pending, NULL);
  324. }
  325. }
  326. break;
  327. case PTHREAD_MUTEX_PP_RECURSIVE_NP:
  328. case PTHREAD_MUTEX_PP_ERRORCHECK_NP:
  329. case PTHREAD_MUTEX_PP_NORMAL_NP:
  330. case PTHREAD_MUTEX_PP_ADAPTIVE_NP:
  331. {
  332. int kind = mutex->__data.__kind & PTHREAD_MUTEX_KIND_MASK_NP;
  333. oldval = mutex->__data.__lock;
  334. /* Check whether we already hold the mutex. */
  335. if (mutex->__data.__owner == id)
  336. {
  337. if (kind == PTHREAD_MUTEX_ERRORCHECK_NP)
  338. return EDEADLK;
  339. if (kind == PTHREAD_MUTEX_RECURSIVE_NP)
  340. {
  341. /* Just bump the counter. */
  342. if (__builtin_expect (mutex->__data.__count + 1 == 0, 0))
  343. /* Overflow of the counter. */
  344. return EAGAIN;
  345. ++mutex->__data.__count;
  346. return 0;
  347. }
  348. }
  349. int oldprio = -1, ceilval;
  350. do
  351. {
  352. int ceiling = (oldval & PTHREAD_MUTEX_PRIO_CEILING_MASK)
  353. >> PTHREAD_MUTEX_PRIO_CEILING_SHIFT;
  354. if (__pthread_current_priority () > ceiling)
  355. {
  356. if (oldprio != -1)
  357. __pthread_tpp_change_priority (oldprio, -1);
  358. return EINVAL;
  359. }
  360. int retval = __pthread_tpp_change_priority (oldprio, ceiling);
  361. if (retval)
  362. return retval;
  363. ceilval = ceiling << PTHREAD_MUTEX_PRIO_CEILING_SHIFT;
  364. oldprio = ceiling;
  365. oldval
  366. = atomic_compare_and_exchange_val_acq (&mutex->__data.__lock,
  367. #ifdef NO_INCR
  368. ceilval | 2,
  369. #else
  370. ceilval | 1,
  371. #endif
  372. ceilval);
  373. if (oldval == ceilval)
  374. break;
  375. do
  376. {
  377. oldval
  378. = atomic_compare_and_exchange_val_acq (&mutex->__data.__lock,
  379. ceilval | 2,
  380. ceilval | 1);
  381. if ((oldval & PTHREAD_MUTEX_PRIO_CEILING_MASK) != ceilval)
  382. break;
  383. if (oldval != ceilval)
  384. lll_futex_wait (&mutex->__data.__lock, ceilval | 2,
  385. PTHREAD_MUTEX_PSHARED (mutex));
  386. }
  387. while (atomic_compare_and_exchange_val_acq (&mutex->__data.__lock,
  388. ceilval | 2, ceilval)
  389. != ceilval);
  390. }
  391. while ((oldval & PTHREAD_MUTEX_PRIO_CEILING_MASK) != ceilval);
  392. assert (mutex->__data.__owner == 0);
  393. mutex->__data.__count = 1;
  394. }
  395. break;
  396. default:
  397. /* Correct code cannot set any other type. */
  398. return EINVAL;
  399. }
  400. /* Record the ownership. */
  401. mutex->__data.__owner = id;
  402. #ifndef NO_INCR
  403. ++mutex->__data.__nusers;
  404. #endif
  405. return 0;
  406. }
  407. #ifndef __pthread_mutex_lock
  408. strong_alias (__pthread_mutex_lock, pthread_mutex_lock)
  409. strong_alias (__pthread_mutex_lock, __pthread_mutex_lock_internal)
  410. #endif
  411. #ifdef NO_INCR
  412. void
  413. attribute_hidden internal_function
  414. __pthread_mutex_cond_lock_adjust (
  415. pthread_mutex_t *mutex)
  416. {
  417. assert ((mutex->__data.__kind & PTHREAD_MUTEX_PRIO_INHERIT_NP) != 0);
  418. assert ((mutex->__data.__kind & PTHREAD_MUTEX_ROBUST_NORMAL_NP) == 0);
  419. assert ((mutex->__data.__kind & PTHREAD_MUTEX_PSHARED_BIT) == 0);
  420. /* Record the ownership. */
  421. pid_t id = THREAD_GETMEM (THREAD_SELF, tid);
  422. mutex->__data.__owner = id;
  423. if (mutex->__data.__kind == PTHREAD_MUTEX_PI_RECURSIVE_NP)
  424. ++mutex->__data.__count;
  425. }
  426. #endif