pthread_mutex_unlock.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359
  1. /* Copyright (C) 2002-2019 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. #include <stap-probe.h>
  21. #ifndef lll_unlock_elision
  22. #define lll_unlock_elision(a,b,c) ({ lll_unlock (a,c); 0; })
  23. #endif
  24. static int
  25. __pthread_mutex_unlock_full (pthread_mutex_t *mutex, int decr)
  26. __attribute_noinline__;
  27. int
  28. attribute_hidden
  29. __pthread_mutex_unlock_usercnt (pthread_mutex_t *mutex, int decr)
  30. {
  31. /* See concurrency notes regarding mutex type which is loaded from __kind
  32. in struct __pthread_mutex_s in sysdeps/nptl/bits/thread-shared-types.h. */
  33. int type = PTHREAD_MUTEX_TYPE_ELISION (mutex);
  34. if (__builtin_expect (type &
  35. ~(PTHREAD_MUTEX_KIND_MASK_NP|PTHREAD_MUTEX_ELISION_FLAGS_NP), 0))
  36. return __pthread_mutex_unlock_full (mutex, decr);
  37. if (__builtin_expect (type, PTHREAD_MUTEX_TIMED_NP)
  38. == PTHREAD_MUTEX_TIMED_NP)
  39. {
  40. /* Always reset the owner field. */
  41. normal:
  42. mutex->__data.__owner = 0;
  43. if (decr)
  44. /* One less user. */
  45. --mutex->__data.__nusers;
  46. /* Unlock. */
  47. lll_unlock (mutex->__data.__lock, PTHREAD_MUTEX_PSHARED (mutex));
  48. LIBC_PROBE (mutex_release, 1, mutex);
  49. return 0;
  50. }
  51. else if (__glibc_likely (type == PTHREAD_MUTEX_TIMED_ELISION_NP))
  52. {
  53. /* Don't reset the owner/users fields for elision. */
  54. return lll_unlock_elision (mutex->__data.__lock, mutex->__data.__elision,
  55. PTHREAD_MUTEX_PSHARED (mutex));
  56. }
  57. else if (__builtin_expect (PTHREAD_MUTEX_TYPE (mutex)
  58. == PTHREAD_MUTEX_RECURSIVE_NP, 1))
  59. {
  60. /* Recursive mutex. */
  61. if (mutex->__data.__owner != THREAD_GETMEM (THREAD_SELF, tid))
  62. return EPERM;
  63. if (--mutex->__data.__count != 0)
  64. /* We still hold the mutex. */
  65. return 0;
  66. goto normal;
  67. }
  68. else if (__builtin_expect (PTHREAD_MUTEX_TYPE (mutex)
  69. == PTHREAD_MUTEX_ADAPTIVE_NP, 1))
  70. goto normal;
  71. else
  72. {
  73. /* Error checking mutex. */
  74. assert (type == PTHREAD_MUTEX_ERRORCHECK_NP);
  75. if (mutex->__data.__owner != THREAD_GETMEM (THREAD_SELF, tid)
  76. || ! lll_islocked (mutex->__data.__lock))
  77. return EPERM;
  78. goto normal;
  79. }
  80. }
  81. static int
  82. __pthread_mutex_unlock_full (pthread_mutex_t *mutex, int decr)
  83. {
  84. int newowner = 0;
  85. int private;
  86. switch (PTHREAD_MUTEX_TYPE (mutex))
  87. {
  88. case PTHREAD_MUTEX_ROBUST_RECURSIVE_NP:
  89. /* Recursive mutex. */
  90. if ((mutex->__data.__lock & FUTEX_TID_MASK)
  91. == THREAD_GETMEM (THREAD_SELF, tid)
  92. && __builtin_expect (mutex->__data.__owner
  93. == PTHREAD_MUTEX_INCONSISTENT, 0))
  94. {
  95. if (--mutex->__data.__count != 0)
  96. /* We still hold the mutex. */
  97. return ENOTRECOVERABLE;
  98. goto notrecoverable;
  99. }
  100. if (mutex->__data.__owner != THREAD_GETMEM (THREAD_SELF, tid))
  101. return EPERM;
  102. if (--mutex->__data.__count != 0)
  103. /* We still hold the mutex. */
  104. return 0;
  105. goto robust;
  106. case PTHREAD_MUTEX_ROBUST_ERRORCHECK_NP:
  107. case PTHREAD_MUTEX_ROBUST_NORMAL_NP:
  108. case PTHREAD_MUTEX_ROBUST_ADAPTIVE_NP:
  109. if ((mutex->__data.__lock & FUTEX_TID_MASK)
  110. != THREAD_GETMEM (THREAD_SELF, tid)
  111. || ! lll_islocked (mutex->__data.__lock))
  112. return EPERM;
  113. /* If the previous owner died and the caller did not succeed in
  114. making the state consistent, mark the mutex as unrecoverable
  115. and make all waiters. */
  116. if (__builtin_expect (mutex->__data.__owner
  117. == PTHREAD_MUTEX_INCONSISTENT, 0))
  118. notrecoverable:
  119. newowner = PTHREAD_MUTEX_NOTRECOVERABLE;
  120. robust:
  121. /* Remove mutex from the list. */
  122. THREAD_SETMEM (THREAD_SELF, robust_head.list_op_pending,
  123. &mutex->__data.__list.__next);
  124. /* We must set op_pending before we dequeue the mutex. Also see
  125. comments at ENQUEUE_MUTEX. */
  126. __asm ("" ::: "memory");
  127. DEQUEUE_MUTEX (mutex);
  128. mutex->__data.__owner = newowner;
  129. if (decr)
  130. /* One less user. */
  131. --mutex->__data.__nusers;
  132. /* Unlock by setting the lock to 0 (not acquired); if the lock had
  133. FUTEX_WAITERS set previously, then wake any waiters.
  134. The unlock operation must be the last access to the mutex to not
  135. violate the mutex destruction requirements (see __lll_unlock). */
  136. private = PTHREAD_ROBUST_MUTEX_PSHARED (mutex);
  137. if (__glibc_unlikely ((atomic_exchange_rel (&mutex->__data.__lock, 0)
  138. & FUTEX_WAITERS) != 0))
  139. lll_futex_wake (&mutex->__data.__lock, 1, private);
  140. /* We must clear op_pending after we release the mutex.
  141. FIXME However, this violates the mutex destruction requirements
  142. because another thread could acquire the mutex, destroy it, and
  143. reuse the memory for something else; then, if this thread crashes,
  144. and the memory happens to have a value equal to the TID, the kernel
  145. will believe it is still related to the mutex (which has been
  146. destroyed already) and will modify some other random object. */
  147. __asm ("" ::: "memory");
  148. THREAD_SETMEM (THREAD_SELF, robust_head.list_op_pending, NULL);
  149. break;
  150. /* The PI support requires the Linux futex system call. If that's not
  151. available, pthread_mutex_init should never have allowed the type to
  152. be set. So it will get the default case for an invalid type. */
  153. #ifdef __NR_futex
  154. case PTHREAD_MUTEX_PI_RECURSIVE_NP:
  155. /* Recursive mutex. */
  156. if (mutex->__data.__owner != THREAD_GETMEM (THREAD_SELF, tid))
  157. return EPERM;
  158. if (--mutex->__data.__count != 0)
  159. /* We still hold the mutex. */
  160. return 0;
  161. goto continue_pi_non_robust;
  162. case PTHREAD_MUTEX_PI_ROBUST_RECURSIVE_NP:
  163. /* Recursive mutex. */
  164. if ((mutex->__data.__lock & FUTEX_TID_MASK)
  165. == THREAD_GETMEM (THREAD_SELF, tid)
  166. && __builtin_expect (mutex->__data.__owner
  167. == PTHREAD_MUTEX_INCONSISTENT, 0))
  168. {
  169. if (--mutex->__data.__count != 0)
  170. /* We still hold the mutex. */
  171. return ENOTRECOVERABLE;
  172. goto pi_notrecoverable;
  173. }
  174. if (mutex->__data.__owner != THREAD_GETMEM (THREAD_SELF, tid))
  175. return EPERM;
  176. if (--mutex->__data.__count != 0)
  177. /* We still hold the mutex. */
  178. return 0;
  179. goto continue_pi_robust;
  180. case PTHREAD_MUTEX_PI_ERRORCHECK_NP:
  181. case PTHREAD_MUTEX_PI_NORMAL_NP:
  182. case PTHREAD_MUTEX_PI_ADAPTIVE_NP:
  183. case PTHREAD_MUTEX_PI_ROBUST_ERRORCHECK_NP:
  184. case PTHREAD_MUTEX_PI_ROBUST_NORMAL_NP:
  185. case PTHREAD_MUTEX_PI_ROBUST_ADAPTIVE_NP:
  186. if ((mutex->__data.__lock & FUTEX_TID_MASK)
  187. != THREAD_GETMEM (THREAD_SELF, tid)
  188. || ! lll_islocked (mutex->__data.__lock))
  189. return EPERM;
  190. /* If the previous owner died and the caller did not succeed in
  191. making the state consistent, mark the mutex as unrecoverable
  192. and make all waiters. */
  193. /* See concurrency notes regarding __kind in struct __pthread_mutex_s
  194. in sysdeps/nptl/bits/thread-shared-types.h. */
  195. if ((atomic_load_relaxed (&(mutex->__data.__kind))
  196. & PTHREAD_MUTEX_ROBUST_NORMAL_NP) != 0
  197. && __builtin_expect (mutex->__data.__owner
  198. == PTHREAD_MUTEX_INCONSISTENT, 0))
  199. pi_notrecoverable:
  200. newowner = PTHREAD_MUTEX_NOTRECOVERABLE;
  201. /* See concurrency notes regarding __kind in struct __pthread_mutex_s
  202. in sysdeps/nptl/bits/thread-shared-types.h. */
  203. if ((atomic_load_relaxed (&(mutex->__data.__kind))
  204. & PTHREAD_MUTEX_ROBUST_NORMAL_NP) != 0)
  205. {
  206. continue_pi_robust:
  207. /* Remove mutex from the list.
  208. Note: robust PI futexes are signaled by setting bit 0. */
  209. THREAD_SETMEM (THREAD_SELF, robust_head.list_op_pending,
  210. (void *) (((uintptr_t) &mutex->__data.__list.__next)
  211. | 1));
  212. /* We must set op_pending before we dequeue the mutex. Also see
  213. comments at ENQUEUE_MUTEX. */
  214. __asm ("" ::: "memory");
  215. DEQUEUE_MUTEX (mutex);
  216. }
  217. continue_pi_non_robust:
  218. mutex->__data.__owner = newowner;
  219. if (decr)
  220. /* One less user. */
  221. --mutex->__data.__nusers;
  222. /* Unlock. Load all necessary mutex data before releasing the mutex
  223. to not violate the mutex destruction requirements (see
  224. lll_unlock). */
  225. /* See concurrency notes regarding __kind in struct __pthread_mutex_s
  226. in sysdeps/nptl/bits/thread-shared-types.h. */
  227. int robust = atomic_load_relaxed (&(mutex->__data.__kind))
  228. & PTHREAD_MUTEX_ROBUST_NORMAL_NP;
  229. private = (robust
  230. ? PTHREAD_ROBUST_MUTEX_PSHARED (mutex)
  231. : PTHREAD_MUTEX_PSHARED (mutex));
  232. /* Unlock the mutex using a CAS unless there are futex waiters or our
  233. TID is not the value of __lock anymore, in which case we let the
  234. kernel take care of the situation. Use release MO in the CAS to
  235. synchronize with acquire MO in lock acquisitions. */
  236. int l = atomic_load_relaxed (&mutex->__data.__lock);
  237. do
  238. {
  239. if (((l & FUTEX_WAITERS) != 0)
  240. || (l != THREAD_GETMEM (THREAD_SELF, tid)))
  241. {
  242. INTERNAL_SYSCALL_DECL (__err);
  243. INTERNAL_SYSCALL (futex, __err, 2, &mutex->__data.__lock,
  244. __lll_private_flag (FUTEX_UNLOCK_PI, private));
  245. break;
  246. }
  247. }
  248. while (!atomic_compare_exchange_weak_release (&mutex->__data.__lock,
  249. &l, 0));
  250. /* This happens after the kernel releases the mutex but violates the
  251. mutex destruction requirements; see comments in the code handling
  252. PTHREAD_MUTEX_ROBUST_NORMAL_NP. */
  253. THREAD_SETMEM (THREAD_SELF, robust_head.list_op_pending, NULL);
  254. break;
  255. #endif /* __NR_futex. */
  256. case PTHREAD_MUTEX_PP_RECURSIVE_NP:
  257. /* Recursive mutex. */
  258. if (mutex->__data.__owner != THREAD_GETMEM (THREAD_SELF, tid))
  259. return EPERM;
  260. if (--mutex->__data.__count != 0)
  261. /* We still hold the mutex. */
  262. return 0;
  263. goto pp;
  264. case PTHREAD_MUTEX_PP_ERRORCHECK_NP:
  265. /* Error checking mutex. */
  266. if (mutex->__data.__owner != THREAD_GETMEM (THREAD_SELF, tid)
  267. || (mutex->__data.__lock & ~ PTHREAD_MUTEX_PRIO_CEILING_MASK) == 0)
  268. return EPERM;
  269. /* FALLTHROUGH */
  270. case PTHREAD_MUTEX_PP_NORMAL_NP:
  271. case PTHREAD_MUTEX_PP_ADAPTIVE_NP:
  272. /* Always reset the owner field. */
  273. pp:
  274. mutex->__data.__owner = 0;
  275. if (decr)
  276. /* One less user. */
  277. --mutex->__data.__nusers;
  278. /* Unlock. Use release MO in the CAS to synchronize with acquire MO in
  279. lock acquisitions. */
  280. int newval;
  281. int oldval = atomic_load_relaxed (&mutex->__data.__lock);
  282. do
  283. {
  284. newval = oldval & PTHREAD_MUTEX_PRIO_CEILING_MASK;
  285. }
  286. while (!atomic_compare_exchange_weak_release (&mutex->__data.__lock,
  287. &oldval, newval));
  288. if ((oldval & ~PTHREAD_MUTEX_PRIO_CEILING_MASK) > 1)
  289. lll_futex_wake (&mutex->__data.__lock, 1,
  290. PTHREAD_MUTEX_PSHARED (mutex));
  291. int oldprio = newval >> PTHREAD_MUTEX_PRIO_CEILING_SHIFT;
  292. LIBC_PROBE (mutex_release, 1, mutex);
  293. return __pthread_tpp_change_priority (oldprio, -1);
  294. default:
  295. /* Correct code cannot set any other type. */
  296. return EINVAL;
  297. }
  298. LIBC_PROBE (mutex_release, 1, mutex);
  299. return 0;
  300. }
  301. int
  302. __pthread_mutex_unlock (pthread_mutex_t *mutex)
  303. {
  304. return __pthread_mutex_unlock_usercnt (mutex, 1);
  305. }
  306. weak_alias (__pthread_mutex_unlock, pthread_mutex_unlock)
  307. hidden_def (__pthread_mutex_unlock)