lowlevellock.h 9.7 KB

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