lowlevellock.h 11 KB

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