lowlevellock.h 12 KB

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