pthread_mutex_unlock.c 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298
  1. /* Copyright (C) 2002, 2003, 2005-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 "pthreadP.h"
  19. #include <lowlevellock.h>
  20. static int
  21. internal_function
  22. __pthread_mutex_unlock_full (pthread_mutex_t *mutex, int decr)
  23. __attribute_noinline__;
  24. int
  25. internal_function attribute_hidden
  26. __pthread_mutex_unlock_usercnt (
  27. pthread_mutex_t *mutex,
  28. int decr)
  29. {
  30. int type = PTHREAD_MUTEX_TYPE (mutex);
  31. if (__builtin_expect (type & ~PTHREAD_MUTEX_KIND_MASK_NP, 0))
  32. return __pthread_mutex_unlock_full (mutex, decr);
  33. if (__builtin_expect (type, PTHREAD_MUTEX_TIMED_NP)
  34. == PTHREAD_MUTEX_TIMED_NP)
  35. {
  36. /* Always reset the owner field. */
  37. normal:
  38. mutex->__data.__owner = 0;
  39. if (decr)
  40. /* One less user. */
  41. --mutex->__data.__nusers;
  42. /* Unlock. */
  43. lll_unlock (mutex->__data.__lock, PTHREAD_MUTEX_PSHARED (mutex));
  44. return 0;
  45. }
  46. else if (__builtin_expect (type == PTHREAD_MUTEX_RECURSIVE_NP, 1))
  47. {
  48. /* Recursive mutex. */
  49. if (mutex->__data.__owner != THREAD_GETMEM (THREAD_SELF, tid))
  50. return EPERM;
  51. if (--mutex->__data.__count != 0)
  52. /* We still hold the mutex. */
  53. return 0;
  54. goto normal;
  55. }
  56. else if (__builtin_expect (type == PTHREAD_MUTEX_ADAPTIVE_NP, 1))
  57. goto normal;
  58. else
  59. {
  60. /* Error checking mutex. */
  61. assert (type == PTHREAD_MUTEX_ERRORCHECK_NP);
  62. if (mutex->__data.__owner != THREAD_GETMEM (THREAD_SELF, tid)
  63. || ! lll_islocked (mutex->__data.__lock))
  64. return EPERM;
  65. goto normal;
  66. }
  67. }
  68. static int
  69. internal_function
  70. __pthread_mutex_unlock_full (pthread_mutex_t *mutex, int decr)
  71. {
  72. int newowner = 0;
  73. switch (PTHREAD_MUTEX_TYPE (mutex))
  74. {
  75. case PTHREAD_MUTEX_ROBUST_RECURSIVE_NP:
  76. /* Recursive mutex. */
  77. if ((mutex->__data.__lock & FUTEX_TID_MASK)
  78. == THREAD_GETMEM (THREAD_SELF, tid)
  79. && __builtin_expect (mutex->__data.__owner
  80. == PTHREAD_MUTEX_INCONSISTENT, 0))
  81. {
  82. if (--mutex->__data.__count != 0)
  83. /* We still hold the mutex. */
  84. return ENOTRECOVERABLE;
  85. goto notrecoverable;
  86. }
  87. if (mutex->__data.__owner != THREAD_GETMEM (THREAD_SELF, tid))
  88. return EPERM;
  89. if (--mutex->__data.__count != 0)
  90. /* We still hold the mutex. */
  91. return 0;
  92. goto robust;
  93. case PTHREAD_MUTEX_ROBUST_ERRORCHECK_NP:
  94. case PTHREAD_MUTEX_ROBUST_NORMAL_NP:
  95. case PTHREAD_MUTEX_ROBUST_ADAPTIVE_NP:
  96. if ((mutex->__data.__lock & FUTEX_TID_MASK)
  97. != THREAD_GETMEM (THREAD_SELF, tid)
  98. || ! lll_islocked (mutex->__data.__lock))
  99. return EPERM;
  100. /* If the previous owner died and the caller did not succeed in
  101. making the state consistent, mark the mutex as unrecoverable
  102. and make all waiters. */
  103. if (__builtin_expect (mutex->__data.__owner
  104. == PTHREAD_MUTEX_INCONSISTENT, 0))
  105. notrecoverable:
  106. newowner = PTHREAD_MUTEX_NOTRECOVERABLE;
  107. robust:
  108. /* Remove mutex from the list. */
  109. THREAD_SETMEM (THREAD_SELF, robust_head.list_op_pending,
  110. &mutex->__data.__list.__next);
  111. DEQUEUE_MUTEX (mutex);
  112. mutex->__data.__owner = newowner;
  113. if (decr)
  114. /* One less user. */
  115. --mutex->__data.__nusers;
  116. /* Unlock. */
  117. lll_robust_unlock (mutex->__data.__lock,
  118. PTHREAD_ROBUST_MUTEX_PSHARED (mutex));
  119. THREAD_SETMEM (THREAD_SELF, robust_head.list_op_pending, NULL);
  120. break;
  121. case PTHREAD_MUTEX_PI_RECURSIVE_NP:
  122. /* Recursive mutex. */
  123. if (mutex->__data.__owner != THREAD_GETMEM (THREAD_SELF, tid))
  124. return EPERM;
  125. if (--mutex->__data.__count != 0)
  126. /* We still hold the mutex. */
  127. return 0;
  128. goto continue_pi_non_robust;
  129. case PTHREAD_MUTEX_PI_ROBUST_RECURSIVE_NP:
  130. /* Recursive mutex. */
  131. if ((mutex->__data.__lock & FUTEX_TID_MASK)
  132. == THREAD_GETMEM (THREAD_SELF, tid)
  133. && __builtin_expect (mutex->__data.__owner
  134. == PTHREAD_MUTEX_INCONSISTENT, 0))
  135. {
  136. if (--mutex->__data.__count != 0)
  137. /* We still hold the mutex. */
  138. return ENOTRECOVERABLE;
  139. goto pi_notrecoverable;
  140. }
  141. if (mutex->__data.__owner != THREAD_GETMEM (THREAD_SELF, tid))
  142. return EPERM;
  143. if (--mutex->__data.__count != 0)
  144. /* We still hold the mutex. */
  145. return 0;
  146. goto continue_pi_robust;
  147. case PTHREAD_MUTEX_PI_ERRORCHECK_NP:
  148. case PTHREAD_MUTEX_PI_NORMAL_NP:
  149. case PTHREAD_MUTEX_PI_ADAPTIVE_NP:
  150. case PTHREAD_MUTEX_PI_ROBUST_ERRORCHECK_NP:
  151. case PTHREAD_MUTEX_PI_ROBUST_NORMAL_NP:
  152. case PTHREAD_MUTEX_PI_ROBUST_ADAPTIVE_NP:
  153. if ((mutex->__data.__lock & FUTEX_TID_MASK)
  154. != THREAD_GETMEM (THREAD_SELF, tid)
  155. || ! lll_islocked (mutex->__data.__lock))
  156. return EPERM;
  157. /* If the previous owner died and the caller did not succeed in
  158. making the state consistent, mark the mutex as unrecoverable
  159. and make all waiters. */
  160. if ((mutex->__data.__kind & PTHREAD_MUTEX_ROBUST_NORMAL_NP) != 0
  161. && __builtin_expect (mutex->__data.__owner
  162. == PTHREAD_MUTEX_INCONSISTENT, 0))
  163. pi_notrecoverable:
  164. newowner = PTHREAD_MUTEX_NOTRECOVERABLE;
  165. if ((mutex->__data.__kind & PTHREAD_MUTEX_ROBUST_NORMAL_NP) != 0)
  166. {
  167. continue_pi_robust:
  168. /* Remove mutex from the list.
  169. Note: robust PI futexes are signaled by setting bit 0. */
  170. THREAD_SETMEM (THREAD_SELF, robust_head.list_op_pending,
  171. (void *) (((uintptr_t) &mutex->__data.__list.__next)
  172. | 1));
  173. DEQUEUE_MUTEX (mutex);
  174. }
  175. continue_pi_non_robust:
  176. mutex->__data.__owner = newowner;
  177. if (decr)
  178. /* One less user. */
  179. --mutex->__data.__nusers;
  180. /* Unlock. */
  181. if ((mutex->__data.__lock & FUTEX_WAITERS) != 0
  182. || atomic_compare_and_exchange_bool_rel (&mutex->__data.__lock, 0,
  183. THREAD_GETMEM (THREAD_SELF,
  184. tid)))
  185. {
  186. int robust = mutex->__data.__kind & PTHREAD_MUTEX_ROBUST_NORMAL_NP;
  187. int private = (robust
  188. ? PTHREAD_ROBUST_MUTEX_PSHARED (mutex)
  189. : PTHREAD_MUTEX_PSHARED (mutex));
  190. INTERNAL_SYSCALL_DECL (__err);
  191. #if defined(__UCLIBC_USE_TIME64__) && defined(__NR_futex_time64)
  192. INTERNAL_SYSCALL (futex_time64, __err, 2, &mutex->__data.__lock,
  193. __lll_private_flag (FUTEX_UNLOCK_PI, private));
  194. #else
  195. INTERNAL_SYSCALL (futex, __err, 2, &mutex->__data.__lock,
  196. __lll_private_flag (FUTEX_UNLOCK_PI, private));
  197. #endif
  198. }
  199. THREAD_SETMEM (THREAD_SELF, robust_head.list_op_pending, NULL);
  200. break;
  201. case PTHREAD_MUTEX_PP_RECURSIVE_NP:
  202. /* Recursive mutex. */
  203. if (mutex->__data.__owner != THREAD_GETMEM (THREAD_SELF, tid))
  204. return EPERM;
  205. if (--mutex->__data.__count != 0)
  206. /* We still hold the mutex. */
  207. return 0;
  208. goto pp;
  209. case PTHREAD_MUTEX_PP_ERRORCHECK_NP:
  210. /* Error checking mutex. */
  211. if (mutex->__data.__owner != THREAD_GETMEM (THREAD_SELF, tid)
  212. || (mutex->__data.__lock & ~ PTHREAD_MUTEX_PRIO_CEILING_MASK) == 0)
  213. return EPERM;
  214. /* FALLTHROUGH */
  215. case PTHREAD_MUTEX_PP_NORMAL_NP:
  216. case PTHREAD_MUTEX_PP_ADAPTIVE_NP:
  217. /* Always reset the owner field. */
  218. pp:
  219. mutex->__data.__owner = 0;
  220. if (decr)
  221. /* One less user. */
  222. --mutex->__data.__nusers;
  223. /* Unlock. */
  224. int newval, oldval;
  225. do
  226. {
  227. oldval = mutex->__data.__lock;
  228. newval = oldval & PTHREAD_MUTEX_PRIO_CEILING_MASK;
  229. }
  230. while (atomic_compare_and_exchange_bool_rel (&mutex->__data.__lock,
  231. newval, oldval));
  232. if ((oldval & ~PTHREAD_MUTEX_PRIO_CEILING_MASK) > 1)
  233. lll_futex_wake (&mutex->__data.__lock, 1,
  234. PTHREAD_MUTEX_PSHARED (mutex));
  235. int oldprio = newval >> PTHREAD_MUTEX_PRIO_CEILING_SHIFT;
  236. return __pthread_tpp_change_priority (oldprio, -1);
  237. default:
  238. /* Correct code cannot set any other type. */
  239. return EINVAL;
  240. }
  241. return 0;
  242. }
  243. int
  244. attribute_protected
  245. __pthread_mutex_unlock (
  246. pthread_mutex_t *mutex)
  247. {
  248. return __pthread_mutex_unlock_usercnt (mutex, 1);
  249. }
  250. strong_alias (__pthread_mutex_unlock, pthread_mutex_unlock)
  251. strong_alias (__pthread_mutex_unlock, __pthread_mutex_unlock_internal)