lowlevellock.h 11 KB

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