lowlevellock.h 9.1 KB

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