lowlevellock.h 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295
  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, write to the Free
  14. Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
  15. 02111-1307 USA. */
  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. __ret = INTERNAL_SYSCALL (futex, __err, 4, (long) (futexp), \
  73. __lll_private_flag (FUTEX_WAIT, private), \
  74. (val), (timespec)); \
  75. INTERNAL_SYSCALL_ERROR_P (__ret, __err) ? -__ret : __ret; \
  76. })
  77. #define lll_futex_wake(futexp, nr, private) \
  78. ({ \
  79. INTERNAL_SYSCALL_DECL (__err); \
  80. long int __ret; \
  81. __ret = INTERNAL_SYSCALL (futex, __err, 4, (long) (futexp), \
  82. __lll_private_flag (FUTEX_WAKE, private), \
  83. (nr), 0); \
  84. INTERNAL_SYSCALL_ERROR_P (__ret, __err) ? -__ret : __ret; \
  85. })
  86. #define lll_robust_dead(futexv, private) \
  87. do \
  88. { \
  89. int *__futexp = &(futexv); \
  90. atomic_or (__futexp, FUTEX_OWNER_DIED); \
  91. lll_futex_wake (__futexp, 1, private); \
  92. } \
  93. while (0)
  94. /* Returns non-zero if error happened, zero if success. */
  95. #define lll_futex_requeue(futexp, nr_wake, nr_move, mutex, val, private) \
  96. ({ \
  97. INTERNAL_SYSCALL_DECL (__err); \
  98. long int __ret; \
  99. __ret = INTERNAL_SYSCALL (futex, __err, 6, (long) (futexp), \
  100. __lll_private_flag (FUTEX_CMP_REQUEUE, private),\
  101. (nr_wake), (nr_move), (mutex), (val)); \
  102. INTERNAL_SYSCALL_ERROR_P (__ret, __err); \
  103. })
  104. /* Returns non-zero if error happened, zero if success. */
  105. #define lll_futex_wake_unlock(futexp, nr_wake, nr_wake2, futexp2, private) \
  106. ({ \
  107. INTERNAL_SYSCALL_DECL (__err); \
  108. long int __ret; \
  109. \
  110. __ret = INTERNAL_SYSCALL (futex, __err, 6, (futexp), \
  111. __lll_private_flag (FUTEX_WAKE_OP, private), \
  112. (nr_wake), (nr_wake2), (futexp2), \
  113. FUTEX_OP_CLEAR_WAKE_IF_GT_ONE); \
  114. INTERNAL_SYSCALL_ERROR_P (__ret, __err); \
  115. })
  116. static inline int __attribute__((always_inline))
  117. __lll_trylock(int *futex)
  118. {
  119. return atomic_compare_and_exchange_val_acq (futex, 1, 0) != 0;
  120. }
  121. #define lll_trylock(lock) __lll_trylock (&(lock))
  122. static inline int __attribute__((always_inline))
  123. __lll_cond_trylock(int *futex)
  124. {
  125. return atomic_compare_and_exchange_val_acq (futex, 2, 0) != 0;
  126. }
  127. #define lll_cond_trylock(lock) __lll_cond_trylock (&(lock))
  128. static inline int __attribute__((always_inline))
  129. __lll_robust_trylock(int *futex, int id)
  130. {
  131. return atomic_compare_and_exchange_val_acq (futex, id, 0) != 0;
  132. }
  133. #define lll_robust_trylock(lock, id) \
  134. __lll_robust_trylock (&(lock), id)
  135. extern void __lll_lock_wait_private (int *futex) attribute_hidden;
  136. extern void __lll_lock_wait (int *futex, int private) attribute_hidden;
  137. extern int __lll_robust_lock_wait (int *futex, int private) attribute_hidden;
  138. #define __lll_lock(futex, private) \
  139. ((void) ({ \
  140. int *__futex = (futex); \
  141. if (__builtin_expect (atomic_compare_and_exchange_bool_acq (__futex, \
  142. 1, 0), 0)) \
  143. { \
  144. if (__builtin_constant_p (private) && (private) == LLL_PRIVATE) \
  145. __lll_lock_wait_private (__futex); \
  146. else \
  147. __lll_lock_wait (__futex, private); \
  148. } \
  149. }))
  150. #define lll_lock(futex, private) __lll_lock (&(futex), private)
  151. #define __lll_robust_lock(futex, id, private) \
  152. ({ \
  153. int *__futex = (futex); \
  154. int __val = 0; \
  155. \
  156. if (__builtin_expect (atomic_compare_and_exchange_bool_acq (__futex, id, \
  157. 0), 0)) \
  158. __val = __lll_robust_lock_wait (__futex, private); \
  159. __val; \
  160. })
  161. #define lll_robust_lock(futex, id, private) \
  162. __lll_robust_lock (&(futex), id, private)
  163. static inline void __attribute__ ((always_inline))
  164. __lll_cond_lock (int *futex, int private)
  165. {
  166. if (atomic_compare_and_exchange_bool_acq (futex, 2, 0) != 0)
  167. __lll_lock_wait (futex, private);
  168. }
  169. #define lll_cond_lock(futex, private) __lll_cond_lock (&(futex), private)
  170. #define lll_robust_cond_lock(futex, id, private) \
  171. __lll_robust_lock (&(futex), (id) | FUTEX_WAITERS, private)
  172. extern int __lll_timedlock_wait (int *futex, const struct timespec *,
  173. int private) attribute_hidden;
  174. extern int __lll_robust_timedlock_wait (int *futex, const struct timespec *,
  175. int private) attribute_hidden;
  176. static inline int __attribute__ ((always_inline))
  177. __lll_timedlock (int *futex, const struct timespec *abstime, int private)
  178. {
  179. int result = 0;
  180. if (atomic_compare_and_exchange_bool_acq (futex, 1, 0) != 0)
  181. result = __lll_timedlock_wait (futex, abstime, private);
  182. return result;
  183. }
  184. #define lll_timedlock(futex, abstime, private) \
  185. __lll_timedlock (&(futex), abstime, private)
  186. static inline int __attribute__ ((always_inline))
  187. __lll_robust_timedlock (int *futex, const struct timespec *abstime,
  188. int id, int private)
  189. {
  190. int result = 0;
  191. if (atomic_compare_and_exchange_bool_acq (futex, id, 0) != 0)
  192. result = __lll_robust_timedlock_wait (futex, abstime, private);
  193. return result;
  194. }
  195. #define lll_robust_timedlock(futex, abstime, id, private) \
  196. __lll_robust_timedlock (&(futex), abstime, id, private)
  197. #define __lll_unlock(futex, private) \
  198. ((void) ({ \
  199. int *__futex = (futex); \
  200. int __val = atomic_exchange_rel (__futex, 0); \
  201. \
  202. if (__builtin_expect (__val > 1, 0)) \
  203. lll_futex_wake (__futex, 1, private); \
  204. }))
  205. #define lll_unlock(futex, private) __lll_unlock(&(futex), private)
  206. #define __lll_robust_unlock(futex, private) \
  207. ((void) ({ \
  208. int *__futex = (futex); \
  209. int __val = atomic_exchange_rel (__futex, 0); \
  210. \
  211. if (__builtin_expect (__val & FUTEX_WAITERS, 0)) \
  212. lll_futex_wake (__futex, 1, private); \
  213. }))
  214. #define lll_robust_unlock(futex, private) \
  215. __lll_robust_unlock(&(futex), private)
  216. #define lll_islocked(futex) \
  217. (futex != 0)
  218. /* Our internal lock implementation is identical to the binary-compatible
  219. mutex implementation. */
  220. /* Initializers for lock. */
  221. #define LLL_LOCK_INITIALIZER (0)
  222. #define LLL_LOCK_INITIALIZER_LOCKED (1)
  223. /* The states of a lock are:
  224. 0 - untaken
  225. 1 - taken by one user
  226. >1 - taken by more users */
  227. /* The kernel notifies a process which uses CLONE_CLEARTID via futex
  228. wakeup when the clone terminates. The memory location contains the
  229. thread ID while the clone is running and is reset to zero
  230. afterwards. */
  231. #define lll_wait_tid(tid) \
  232. do { \
  233. __typeof (tid) __tid; \
  234. while ((__tid = (tid)) != 0) \
  235. lll_futex_wait (&(tid), __tid, LLL_SHARED); \
  236. } while (0)
  237. extern int __lll_timedwait_tid (int *, const struct timespec *)
  238. attribute_hidden;
  239. #define lll_timedwait_tid(tid, abstime) \
  240. ({ \
  241. int __res = 0; \
  242. if ((tid) != 0) \
  243. __res = __lll_timedwait_tid (&(tid), (abstime)); \
  244. __res; \
  245. })
  246. #endif /* lowlevellock.h */