lowlevellock.h 11 KB

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