lowlevellock.h 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278
  1. /* Copyright (C) 2005, 2006, 2007, 2008, 2009 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; see the file COPYING.LIB. If
  13. not, see <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_wake(futexp, nr, private) \
  76. ({ \
  77. INTERNAL_SYSCALL_DECL (__err); \
  78. long int __ret; \
  79. __ret = INTERNAL_SYSCALL (futex, __err, 4, (futexp), \
  80. __lll_private_flag (FUTEX_WAKE, private), \
  81. (nr), 0); \
  82. __ret; \
  83. })
  84. #define lll_robust_dead(futexv, private) \
  85. do \
  86. { \
  87. int *__futexp = &(futexv); \
  88. atomic_or (__futexp, FUTEX_OWNER_DIED); \
  89. lll_futex_wake (__futexp, 1, private); \
  90. } \
  91. while (0)
  92. /* Returns non-zero if error happened, zero if success. */
  93. #define lll_futex_requeue(futexp, nr_wake, nr_move, mutex, val, private) \
  94. ({ \
  95. INTERNAL_SYSCALL_DECL (__err); \
  96. long int __ret; \
  97. __ret = INTERNAL_SYSCALL (futex, __err, 6, (futexp), \
  98. __lll_private_flag (FUTEX_CMP_REQUEUE, private),\
  99. (nr_wake), (nr_move), (mutex), (val)); \
  100. INTERNAL_SYSCALL_ERROR_P (__ret, __err); \
  101. })
  102. /* Returns non-zero if error happened, zero if success. */
  103. #define lll_futex_wake_unlock(futexp, nr_wake, nr_wake2, futexp2, private) \
  104. ({ \
  105. INTERNAL_SYSCALL_DECL (__err); \
  106. long int __ret; \
  107. __ret = INTERNAL_SYSCALL (futex, __err, 6, (futexp), \
  108. __lll_private_flag (FUTEX_WAKE_OP, private), \
  109. (nr_wake), (nr_wake2), (futexp2), \
  110. FUTEX_OP_CLEAR_WAKE_IF_GT_ONE); \
  111. INTERNAL_SYSCALL_ERROR_P (__ret, __err); \
  112. })
  113. #define lll_trylock(lock) \
  114. atomic_compare_and_exchange_val_acq(&(lock), 1, 0)
  115. #define lll_cond_trylock(lock) \
  116. atomic_compare_and_exchange_val_acq(&(lock), 2, 0)
  117. #define __lll_robust_trylock(futex, id) \
  118. (atomic_compare_and_exchange_val_acq (futex, id, 0) != 0)
  119. #define lll_robust_trylock(lock, id) \
  120. __lll_robust_trylock (&(lock), id)
  121. extern void __lll_lock_wait_private (int *futex) attribute_hidden;
  122. extern void __lll_lock_wait (int *futex, int private) attribute_hidden;
  123. extern int __lll_robust_lock_wait (int *futex, int private) attribute_hidden;
  124. #define __lll_lock(futex, private) \
  125. ((void) ({ \
  126. int *__futex = (futex); \
  127. if (unlikely(atomic_compare_and_exchange_val_acq (__futex, 1, 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 (unlikely(atomic_compare_and_exchange_bool_acq (__futex, id, 0))) \
  142. __val = __lll_robust_lock_wait (__futex, private); \
  143. __val; \
  144. })
  145. #define lll_robust_lock(futex, id, private) \
  146. __lll_robust_lock (&(futex), id, private)
  147. #define __lll_cond_lock(futex, private) \
  148. ((void) ({ \
  149. int *__futex = (futex); \
  150. if (unlikely(atomic_exchange_acq (__futex, 2))) \
  151. __lll_lock_wait (__futex, private); \
  152. }))
  153. #define lll_cond_lock(futex, private) __lll_cond_lock (&(futex), private)
  154. #define lll_robust_cond_lock(futex, id, private) \
  155. __lll_robust_lock (&(futex), (id) | FUTEX_WAITERS, private)
  156. extern int __lll_timedlock_wait (int *futex, const struct timespec *,
  157. int private) attribute_hidden;
  158. extern int __lll_robust_timedlock_wait (int *futex, const struct timespec *,
  159. int private) attribute_hidden;
  160. #define __lll_timedlock(futex, abstime, private) \
  161. ({ \
  162. int *__futex = (futex); \
  163. int __val = 0; \
  164. \
  165. if (unlikely(atomic_exchange_acq (__futex, 1))) \
  166. __val = __lll_timedlock_wait (__futex, abstime, private); \
  167. __val; \
  168. })
  169. #define lll_timedlock(futex, abstime, private) \
  170. __lll_timedlock (&(futex), abstime, private)
  171. #define __lll_robust_timedlock(futex, abstime, id, private) \
  172. ({ \
  173. int *__futex = (futex); \
  174. int __val = 0; \
  175. \
  176. if (unlikely(atomic_compare_and_exchange_bool_acq (__futex, id, 0))) \
  177. __val = __lll_robust_timedlock_wait (__futex, abstime, private); \
  178. __val; \
  179. })
  180. #define lll_robust_timedlock(futex, abstime, id, private) \
  181. __lll_robust_timedlock (&(futex), abstime, id, private)
  182. #define __lll_unlock(futex, private) \
  183. (void) \
  184. ({ int *__futex = (futex); \
  185. int __oldval = atomic_exchange_rel (__futex, 0); \
  186. if (unlikely(__oldval > 1)) \
  187. lll_futex_wake (__futex, 1, private); \
  188. })
  189. #define lll_unlock(futex, private) __lll_unlock(&(futex), private)
  190. #define __lll_robust_unlock(futex, private) \
  191. (void) \
  192. ({ int *__futex = (futex); \
  193. int __oldval = atomic_exchange_rel (__futex, 0); \
  194. if (unlikely(__oldval & FUTEX_WAITERS)) \
  195. lll_futex_wake (__futex, 1, private); \
  196. })
  197. #define lll_robust_unlock(futex, private) \
  198. __lll_robust_unlock(&(futex), private)
  199. #define lll_islocked(futex) \
  200. (futex != 0)
  201. /* Our internal lock implementation is identical to the binary-compatible
  202. mutex implementation. */
  203. /* Initializers for lock. */
  204. #define LLL_LOCK_INITIALIZER (0)
  205. #define LLL_LOCK_INITIALIZER_LOCKED (1)
  206. /* The states of a lock are:
  207. 0 - untaken
  208. 1 - taken by one user
  209. >1 - taken by more users */
  210. /* The kernel notifies a process which uses CLONE_CLEARTID via futex
  211. wakeup when the clone terminates. The memory location contains the
  212. thread ID while the clone is running and is reset to zero
  213. afterwards. */
  214. #define lll_wait_tid(tid) \
  215. do { \
  216. __typeof (tid) __tid; \
  217. while ((__tid = (tid)) != 0) \
  218. lll_futex_wait (&(tid), __tid, LLL_SHARED);\
  219. } while (0)
  220. extern int __lll_timedwait_tid (int *, const struct timespec *)
  221. attribute_hidden;
  222. #define lll_timedwait_tid(tid, abstime) \
  223. ({ \
  224. int __res = 0; \
  225. if ((tid) != 0) \
  226. __res = __lll_timedwait_tid (&(tid), (abstime)); \
  227. __res; \
  228. })
  229. #endif /* lowlevellock.h */