lowlevellock.h 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293
  1. /* Copyright (C) 2005-2013 Free Software Foundation, Inc.
  2. This file is part of the GNU C Library.
  3. The GNU C Library is free software; you can redistribute it and/or
  4. modify it under the terms of the GNU Lesser General Public
  5. License as published by the Free Software Foundation; either
  6. version 2.1 of the License, or (at your option) any later version.
  7. The GNU C Library is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  10. Lesser General Public License for more details.
  11. You should have received a copy of the GNU Lesser General Public
  12. License along with the GNU C Library. If not, see
  13. <http://www.gnu.org/licenses/>. */
  14. #ifndef _LOWLEVELLOCK_H
  15. #define _LOWLEVELLOCK_H 1
  16. #include <time.h>
  17. #include <sys/param.h>
  18. #include <bits/pthreadtypes.h>
  19. #include <atomic.h>
  20. #include <sysdep.h>
  21. #include <bits/kernel-features.h>
  22. #define FUTEX_WAIT 0
  23. #define FUTEX_WAKE 1
  24. #define FUTEX_REQUEUE 3
  25. #define FUTEX_CMP_REQUEUE 4
  26. #define FUTEX_WAKE_OP 5
  27. #define FUTEX_OP_CLEAR_WAKE_IF_GT_ONE ((4 << 24) | 1)
  28. #define FUTEX_LOCK_PI 6
  29. #define FUTEX_UNLOCK_PI 7
  30. #define FUTEX_TRYLOCK_PI 8
  31. #define FUTEX_WAIT_BITSET 9
  32. #define FUTEX_WAKE_BITSET 10
  33. #define FUTEX_PRIVATE_FLAG 128
  34. #define FUTEX_CLOCK_REALTIME 256
  35. #define FUTEX_BITSET_MATCH_ANY 0xffffffff
  36. /* Values for 'private' parameter of locking macros. Yes, the
  37. definition seems to be backwards. But it is not. The bit will be
  38. reversed before passing to the system call. */
  39. #define LLL_PRIVATE 0
  40. #define LLL_SHARED FUTEX_PRIVATE_FLAG
  41. #if !defined NOT_IN_libc || defined IS_IN_rtld
  42. /* In libc.so or ld.so all futexes are private. */
  43. # ifdef __ASSUME_PRIVATE_FUTEX
  44. # define __lll_private_flag(fl, private) \
  45. ((fl) | FUTEX_PRIVATE_FLAG)
  46. # else
  47. # define __lll_private_flag(fl, private) \
  48. ((fl) | THREAD_GETMEM (THREAD_SELF, header.private_futex))
  49. # endif
  50. #else
  51. # ifdef __ASSUME_PRIVATE_FUTEX
  52. # define __lll_private_flag(fl, private) \
  53. (((fl) | FUTEX_PRIVATE_FLAG) ^ (private))
  54. # else
  55. # define __lll_private_flag(fl, private) \
  56. (__builtin_constant_p (private) \
  57. ? ((private) == 0 \
  58. ? ((fl) | THREAD_GETMEM (THREAD_SELF, header.private_futex)) \
  59. : (fl)) \
  60. : ((fl) | (((private) ^ FUTEX_PRIVATE_FLAG) \
  61. & THREAD_GETMEM (THREAD_SELF, header.private_futex))))
  62. # endif
  63. #endif
  64. #define lll_futex_wait(futexp, val, private) \
  65. lll_futex_timed_wait(futexp, val, NULL, private)
  66. #define lll_futex_timed_wait(futexp, val, timespec, private) \
  67. ({ \
  68. INTERNAL_SYSCALL_DECL (__err); \
  69. long int __ret; \
  70. __ret = INTERNAL_SYSCALL (futex, __err, 4, (futexp), \
  71. __lll_private_flag (FUTEX_WAIT, private), \
  72. (val), (timespec)); \
  73. __ret; \
  74. })
  75. #define lll_futex_timed_wait_bitset(futexp, val, timespec, clockbit, private) \
  76. ({ \
  77. INTERNAL_SYSCALL_DECL (__err); \
  78. long int __ret; \
  79. int __op = FUTEX_WAIT_BITSET | clockbit; \
  80. __ret = INTERNAL_SYSCALL (futex, __err, 6, (futexp), \
  81. __lll_private_flag (__op, private), \
  82. (val), (timespec), NULL /* Unused. */, \
  83. FUTEX_BITSET_MATCH_ANY); \
  84. __ret; \
  85. })
  86. #define lll_futex_wake(futexp, nr, private) \
  87. ({ \
  88. INTERNAL_SYSCALL_DECL (__err); \
  89. long int __ret; \
  90. __ret = INTERNAL_SYSCALL (futex, __err, 4, (futexp), \
  91. __lll_private_flag (FUTEX_WAKE, private), \
  92. (nr), 0); \
  93. __ret; \
  94. })
  95. #define lll_robust_dead(futexv, private) \
  96. do \
  97. { \
  98. int *__futexp = &(futexv); \
  99. atomic_or (__futexp, FUTEX_OWNER_DIED); \
  100. lll_futex_wake (__futexp, 1, private); \
  101. } \
  102. while (0)
  103. /* Returns non-zero if error happened, zero if success. */
  104. #define lll_futex_requeue(futexp, nr_wake, nr_move, mutex, val, private) \
  105. ({ \
  106. INTERNAL_SYSCALL_DECL (__err); \
  107. long int __ret; \
  108. __ret = INTERNAL_SYSCALL (futex, __err, 6, (futexp), \
  109. __lll_private_flag (FUTEX_CMP_REQUEUE, private),\
  110. (nr_wake), (nr_move), (mutex), (val)); \
  111. INTERNAL_SYSCALL_ERROR_P (__ret, __err); \
  112. })
  113. /* Returns non-zero if error happened, zero if success. */
  114. #define lll_futex_wake_unlock(futexp, nr_wake, nr_wake2, futexp2, private) \
  115. ({ \
  116. INTERNAL_SYSCALL_DECL (__err); \
  117. long int __ret; \
  118. __ret = INTERNAL_SYSCALL (futex, __err, 6, (futexp), \
  119. __lll_private_flag (FUTEX_WAKE_OP, private), \
  120. (nr_wake), (nr_wake2), (futexp2), \
  121. FUTEX_OP_CLEAR_WAKE_IF_GT_ONE); \
  122. INTERNAL_SYSCALL_ERROR_P (__ret, __err); \
  123. })
  124. #define lll_trylock(lock) \
  125. atomic_compare_and_exchange_val_acq(&(lock), 1, 0)
  126. #define lll_cond_trylock(lock) \
  127. atomic_compare_and_exchange_val_acq(&(lock), 2, 0)
  128. #define __lll_robust_trylock(futex, id) \
  129. (atomic_compare_and_exchange_val_acq (futex, id, 0) != 0)
  130. #define lll_robust_trylock(lock, id) \
  131. __lll_robust_trylock (&(lock), id)
  132. extern void __lll_lock_wait_private (int *futex) attribute_hidden;
  133. extern void __lll_lock_wait (int *futex, int private) attribute_hidden;
  134. extern int __lll_robust_lock_wait (int *futex, int private) attribute_hidden;
  135. #define __lll_lock(futex, private) \
  136. ((void) ({ \
  137. int *__futex = (futex); \
  138. if (__builtin_expect (atomic_compare_and_exchange_val_acq (__futex, \
  139. 1, 0), 0)) \
  140. { \
  141. if (__builtin_constant_p (private) && (private) == LLL_PRIVATE) \
  142. __lll_lock_wait_private (__futex); \
  143. else \
  144. __lll_lock_wait (__futex, private); \
  145. } \
  146. }))
  147. #define lll_lock(futex, private) __lll_lock (&(futex), private)
  148. #define __lll_robust_lock(futex, id, private) \
  149. ({ \
  150. int *__futex = (futex); \
  151. int __val = 0; \
  152. \
  153. if (__builtin_expect (atomic_compare_and_exchange_bool_acq (__futex, id, \
  154. 0), 0)) \
  155. __val = __lll_robust_lock_wait (__futex, private); \
  156. __val; \
  157. })
  158. #define lll_robust_lock(futex, id, private) \
  159. __lll_robust_lock (&(futex), id, private)
  160. #define __lll_cond_lock(futex, private) \
  161. ((void) ({ \
  162. int *__futex = (futex); \
  163. if (__builtin_expect (atomic_exchange_acq (__futex, 2), 0)) \
  164. __lll_lock_wait (__futex, private); \
  165. }))
  166. #define lll_cond_lock(futex, private) __lll_cond_lock (&(futex), private)
  167. #define lll_robust_cond_lock(futex, id, private) \
  168. __lll_robust_lock (&(futex), (id) | FUTEX_WAITERS, private)
  169. extern int __lll_timedlock_wait (int *futex, const struct timespec *,
  170. int private) attribute_hidden;
  171. extern int __lll_robust_timedlock_wait (int *futex, const struct timespec *,
  172. int private) attribute_hidden;
  173. #define __lll_timedlock(futex, abstime, private) \
  174. ({ \
  175. int *__futex = (futex); \
  176. int __val = 0; \
  177. \
  178. if (__builtin_expect (atomic_exchange_acq (__futex, 1), 0)) \
  179. __val = __lll_timedlock_wait (__futex, abstime, private); \
  180. __val; \
  181. })
  182. #define lll_timedlock(futex, abstime, private) \
  183. __lll_timedlock (&(futex), abstime, private)
  184. #define __lll_robust_timedlock(futex, abstime, id, private) \
  185. ({ \
  186. int *__futex = (futex); \
  187. int __val = 0; \
  188. \
  189. if (__builtin_expect (atomic_compare_and_exchange_bool_acq (__futex, id, \
  190. 0), 0)) \
  191. __val = __lll_robust_timedlock_wait (__futex, abstime, private); \
  192. __val; \
  193. })
  194. #define lll_robust_timedlock(futex, abstime, id, private) \
  195. __lll_robust_timedlock (&(futex), abstime, id, private)
  196. #define __lll_unlock(futex, private) \
  197. (void) \
  198. ({ int *__futex = (futex); \
  199. int __oldval = atomic_exchange_rel (__futex, 0); \
  200. if (__builtin_expect (__oldval > 1, 0)) \
  201. lll_futex_wake (__futex, 1, private); \
  202. })
  203. #define lll_unlock(futex, private) __lll_unlock(&(futex), private)
  204. #define __lll_robust_unlock(futex, private) \
  205. (void) \
  206. ({ int *__futex = (futex); \
  207. int __oldval = atomic_exchange_rel (__futex, 0); \
  208. if (__builtin_expect (__oldval & FUTEX_WAITERS, 0)) \
  209. lll_futex_wake (__futex, 1, private); \
  210. })
  211. #define lll_robust_unlock(futex, private) \
  212. __lll_robust_unlock(&(futex), private)
  213. #define lll_islocked(futex) \
  214. (futex != 0)
  215. /* Our internal lock implementation is identical to the binary-compatible
  216. mutex implementation. */
  217. /* Initializers for lock. */
  218. #define LLL_LOCK_INITIALIZER (0)
  219. #define LLL_LOCK_INITIALIZER_LOCKED (1)
  220. /* The states of a lock are:
  221. 0 - untaken
  222. 1 - taken by one user
  223. >1 - taken by more users */
  224. /* The kernel notifies a process which uses CLONE_CHILD_CLEARTID via futex
  225. wakeup when the clone terminates. The memory location contains the
  226. thread ID while the clone is running and is reset to zero
  227. afterwards. */
  228. #define lll_wait_tid(tid) \
  229. do { \
  230. __typeof (tid) __tid; \
  231. while ((__tid = (tid)) != 0) \
  232. lll_futex_wait (&(tid), __tid, LLL_SHARED);\
  233. } while (0)
  234. extern int __lll_timedwait_tid (int *, const struct timespec *)
  235. attribute_hidden;
  236. #define lll_timedwait_tid(tid, abstime) \
  237. ({ \
  238. int __res = 0; \
  239. if ((tid) != 0) \
  240. __res = __lll_timedwait_tid (&(tid), (abstime)); \
  241. __res; \
  242. })
  243. #endif /* lowlevellock.h */