lowlevellock.h 9.2 KB

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