lowlevellock.h 9.3 KB

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