lowlevellock.h 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283
  1. /* Copyright (C) 2003, 2004, 2006, 2007 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. INTERNAL_SYSCALL_ERROR_P (__ret, __err)? -__ret : __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. INTERNAL_SYSCALL_ERROR_P (__ret, __err)? -__ret : __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. static inline int __attribute__((always_inline))
  114. __lll_trylock(int *futex)
  115. {
  116. return atomic_compare_and_exchange_val_acq (futex, 1, 0) != 0;
  117. }
  118. #define lll_trylock(lock) __lll_trylock (&(lock))
  119. static inline int __attribute__((always_inline))
  120. __lll_cond_trylock(int *futex)
  121. {
  122. return atomic_compare_and_exchange_val_acq (futex, 2, 0) != 0;
  123. }
  124. #define lll_cond_trylock(lock) __lll_cond_trylock (&(lock))
  125. static inline int __attribute__((always_inline))
  126. __lll_robust_trylock(int *futex, int id)
  127. {
  128. return atomic_compare_and_exchange_val_acq (futex, id, 0) != 0;
  129. }
  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. static inline void __attribute__((always_inline))
  136. __lll_lock(int *futex, int private)
  137. {
  138. if (atomic_compare_and_exchange_bool_acq (futex, 1, 0) != 0)
  139. {
  140. if (__builtin_constant_p (private) && private == LLL_PRIVATE)
  141. __lll_lock_wait_private (futex);
  142. else
  143. __lll_lock_wait (futex, private);
  144. }
  145. }
  146. #define lll_lock(futex, private) __lll_lock (&(futex), private)
  147. static inline int __attribute__ ((always_inline))
  148. __lll_robust_lock (int *futex, int id, int private)
  149. {
  150. int result = 0;
  151. if (atomic_compare_and_exchange_bool_acq (futex, id, 0) != 0)
  152. result = __lll_robust_lock_wait (futex, private);
  153. return result;
  154. }
  155. #define lll_robust_lock(futex, id, private) \
  156. __lll_robust_lock (&(futex), id, private)
  157. static inline void __attribute__ ((always_inline))
  158. __lll_cond_lock (int *futex, int private)
  159. {
  160. if (atomic_compare_and_exchange_bool_acq (futex, 2, 0) != 0)
  161. __lll_lock_wait (futex, private);
  162. }
  163. #define lll_cond_lock(futex, private) __lll_cond_lock (&(futex), private)
  164. #define lll_robust_cond_lock(futex, id, private) \
  165. __lll_robust_lock (&(futex), (id) | FUTEX_WAITERS, private)
  166. extern int __lll_timedlock_wait (int *futex, const struct timespec *,
  167. int private) attribute_hidden;
  168. extern int __lll_robust_timedlock_wait (int *futex, const struct timespec *,
  169. int private) attribute_hidden;
  170. static inline int __attribute__ ((always_inline))
  171. __lll_timedlock (int *futex, const struct timespec *abstime, int private)
  172. {
  173. int result = 0;
  174. if (atomic_compare_and_exchange_bool_acq (futex, 1, 0) != 0)
  175. result = __lll_timedlock_wait (futex, abstime, private);
  176. return result;
  177. }
  178. #define lll_timedlock(futex, abstime, private) \
  179. __lll_timedlock (&(futex), abstime, private)
  180. static inline int __attribute__ ((always_inline))
  181. __lll_robust_timedlock (int *futex, const struct timespec *abstime,
  182. int id, int private)
  183. {
  184. int result = 0;
  185. if (atomic_compare_and_exchange_bool_acq (futex, id, 0) != 0)
  186. result = __lll_robust_timedlock_wait (futex, abstime, private);
  187. return result;
  188. }
  189. #define lll_robust_timedlock(futex, abstime, id, private) \
  190. __lll_robust_timedlock (&(futex), abstime, id, private)
  191. #define __lll_unlock(futex, private) \
  192. (void) \
  193. ({ int *__futex = (futex); \
  194. int __oldval = atomic_exchange_rel (__futex, 0); \
  195. if (__builtin_expect (__oldval > 1, 0)) \
  196. lll_futex_wake (__futex, 1, private); \
  197. })
  198. #define lll_unlock(futex, private) __lll_unlock(&(futex), private)
  199. #define __lll_robust_unlock(futex, private) \
  200. (void) \
  201. ({ int *__futex = (futex); \
  202. int __oldval = atomic_exchange_rel (__futex, 0); \
  203. if (__builtin_expect (__oldval & FUTEX_WAITERS, 0)) \
  204. lll_futex_wake (__futex, 1, private); \
  205. })
  206. #define lll_robust_unlock(futex, private) \
  207. __lll_robust_unlock(&(futex), private)
  208. #define lll_islocked(futex) \
  209. (futex != 0)
  210. /* Initializers for lock. */
  211. #define LLL_LOCK_INITIALIZER (0)
  212. #define LLL_LOCK_INITIALIZER_LOCKED (1)
  213. /* The kernel notifies a process which uses CLONE_CLEARTID via futex
  214. wakeup when the clone terminates. The memory location contains the
  215. thread ID while the clone is running and is reset to zero
  216. afterwards. */
  217. #define lll_wait_tid(tid) \
  218. do { \
  219. __typeof (tid) __tid; \
  220. while ((__tid = (tid)) != 0) \
  221. lll_futex_wait (&(tid), __tid, LLL_SHARED); \
  222. } while (0)
  223. extern int __lll_timedwait_tid (int *, const struct timespec *)
  224. attribute_hidden;
  225. #define lll_timedwait_tid(tid, abstime) \
  226. ({ \
  227. int __res = 0; \
  228. if ((tid) != 0) \
  229. __res = __lll_timedwait_tid (&(tid), (abstime)); \
  230. __res; \
  231. })
  232. #endif /* lowlevellock.h */