lowlevellock.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314
  1. /* Copyright (C) 2003, 2004, 2006-2008, 2009 Free Software Foundation, Inc.
  2. This file is part of the GNU C Library.
  3. Contributed by Paul Mackerras <paulus@au.ibm.com>, 2003.
  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 <bits/kernel-features.h>
  22. #include <sysdep.h>
  23. #ifndef __NR_futex
  24. # define __NR_futex 221
  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. #define lll_futex_timed_wait(futexp, val, timespec, private) \
  71. ({ \
  72. INTERNAL_SYSCALL_DECL (__err); \
  73. long int __ret; \
  74. \
  75. __ret = INTERNAL_SYSCALL (futex, __err, 4, (futexp), \
  76. __lll_private_flag (FUTEX_WAIT, private), \
  77. (val), (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; \
  84. \
  85. __ret = INTERNAL_SYSCALL (futex, __err, 4, (futexp), \
  86. __lll_private_flag (FUTEX_WAKE, private), \
  87. (nr), 0); \
  88. INTERNAL_SYSCALL_ERROR_P (__ret, __err) ? -__ret : __ret; \
  89. })
  90. #define lll_robust_dead(futexv, private) \
  91. do \
  92. { \
  93. INTERNAL_SYSCALL_DECL (__err); \
  94. int *__futexp = &(futexv); \
  95. \
  96. atomic_or (__futexp, FUTEX_OWNER_DIED); \
  97. INTERNAL_SYSCALL (futex, __err, 4, __futexp, \
  98. __lll_private_flag (FUTEX_WAKE, private), 1, 0); \
  99. } \
  100. while (0)
  101. /* Returns non-zero if error happened, zero if success. */
  102. #define lll_futex_requeue(futexp, nr_wake, nr_move, mutex, val, private) \
  103. ({ \
  104. INTERNAL_SYSCALL_DECL (__err); \
  105. long int __ret; \
  106. \
  107. __ret = INTERNAL_SYSCALL (futex, __err, 6, (futexp), \
  108. __lll_private_flag (FUTEX_CMP_REQUEUE, private),\
  109. (nr_wake), (nr_move), (mutex), (val)); \
  110. INTERNAL_SYSCALL_ERROR_P (__ret, __err); \
  111. })
  112. /* Returns non-zero if error happened, zero if success. */
  113. #define lll_futex_wake_unlock(futexp, nr_wake, nr_wake2, futexp2, private) \
  114. ({ \
  115. INTERNAL_SYSCALL_DECL (__err); \
  116. long int __ret; \
  117. \
  118. __ret = INTERNAL_SYSCALL (futex, __err, 6, (futexp), \
  119. __lll_private_flag (FUTEX_WAKE_OP, private), \
  120. (nr_wake), (nr_wake2), (futexp2), \
  121. FUTEX_OP_CLEAR_WAKE_IF_GT_ONE); \
  122. INTERNAL_SYSCALL_ERROR_P (__ret, __err); \
  123. })
  124. #ifdef UP
  125. # define __lll_acq_instr ""
  126. # define __lll_rel_instr ""
  127. #else
  128. # define __lll_acq_instr "isync"
  129. # ifdef _ARCH_PWR4
  130. /*
  131. * Newer powerpc64 processors support the new "light weight" sync (lwsync)
  132. * So if the build is using -mcpu=[power4,power5,power5+,970] we can
  133. * safely use lwsync.
  134. */
  135. # define __lll_rel_instr "lwsync"
  136. # else
  137. /*
  138. * Older powerpc32 processors don't support the new "light weight"
  139. * sync (lwsync). So the only safe option is to use normal sync
  140. * for all powerpc32 applications.
  141. */
  142. # define __lll_rel_instr "sync"
  143. # endif
  144. #endif
  145. /* Set *futex to ID if it is 0, atomically. Returns the old value */
  146. #define __lll_robust_trylock(futex, id) \
  147. ({ int __val; \
  148. __asm__ __volatile__ ("1: lwarx %0,0,%2" MUTEX_HINT_ACQ "\n" \
  149. " cmpwi 0,%0,0\n" \
  150. " bne 2f\n" \
  151. " stwcx. %3,0,%2\n" \
  152. " bne- 1b\n" \
  153. "2: " __lll_acq_instr \
  154. : "=&r" (__val), "=m" (*futex) \
  155. : "r" (futex), "r" (id), "m" (*futex) \
  156. : "cr0", "memory"); \
  157. __val; \
  158. })
  159. #define lll_robust_trylock(lock, id) __lll_robust_trylock (&(lock), id)
  160. /* Set *futex to 1 if it is 0, atomically. Returns the old value */
  161. #define __lll_trylock(futex) __lll_robust_trylock (futex, 1)
  162. #define lll_trylock(lock) __lll_trylock (&(lock))
  163. /* Set *futex to 2 if it is 0, atomically. Returns the old value */
  164. #define __lll_cond_trylock(futex) __lll_robust_trylock (futex, 2)
  165. #define lll_cond_trylock(lock) __lll_cond_trylock (&(lock))
  166. extern void __lll_lock_wait_private (int *futex) attribute_hidden;
  167. extern void __lll_lock_wait (int *futex, int private) attribute_hidden;
  168. extern int __lll_robust_lock_wait (int *futex, int private) attribute_hidden;
  169. #define lll_lock(lock, private) \
  170. (void) ({ \
  171. int *__futex = &(lock); \
  172. if (__builtin_expect (atomic_compare_and_exchange_val_acq (__futex, 1, 0),\
  173. 0) != 0) \
  174. { \
  175. if (__builtin_constant_p (private) && (private) == LLL_PRIVATE) \
  176. __lll_lock_wait_private (__futex); \
  177. else \
  178. __lll_lock_wait (__futex, private); \
  179. } \
  180. })
  181. #define lll_robust_lock(lock, id, private) \
  182. ({ \
  183. int *__futex = &(lock); \
  184. int __val = 0; \
  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_cond_lock(lock, private) \
  191. (void) ({ \
  192. int *__futex = &(lock); \
  193. if (__builtin_expect (atomic_compare_and_exchange_val_acq (__futex, 2, 0),\
  194. 0) != 0) \
  195. __lll_lock_wait (__futex, private); \
  196. })
  197. #define lll_robust_cond_lock(lock, id, private) \
  198. ({ \
  199. int *__futex = &(lock); \
  200. int __val = 0; \
  201. int __id = id | FUTEX_WAITERS; \
  202. if (__builtin_expect (atomic_compare_and_exchange_bool_acq (__futex, __id,\
  203. 0), 0)) \
  204. __val = __lll_robust_lock_wait (__futex, private); \
  205. __val; \
  206. })
  207. extern int __lll_timedlock_wait
  208. (int *futex, const struct timespec *, int private) attribute_hidden;
  209. extern int __lll_robust_timedlock_wait
  210. (int *futex, const struct timespec *, int private) attribute_hidden;
  211. #define lll_timedlock(lock, abstime, private) \
  212. ({ \
  213. int *__futex = &(lock); \
  214. int __val = 0; \
  215. if (__builtin_expect (atomic_compare_and_exchange_val_acq (__futex, 1, 0),\
  216. 0) != 0) \
  217. __val = __lll_timedlock_wait (__futex, abstime, private); \
  218. __val; \
  219. })
  220. #define lll_robust_timedlock(lock, abstime, id, private) \
  221. ({ \
  222. int *__futex = &(lock); \
  223. int __val = 0; \
  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_unlock(lock, private) \
  230. ((void) ({ \
  231. int *__futex = &(lock); \
  232. int __val = atomic_exchange_rel (__futex, 0); \
  233. if (__builtin_expect (__val > 1, 0)) \
  234. lll_futex_wake (__futex, 1, private); \
  235. }))
  236. #define lll_robust_unlock(lock, private) \
  237. ((void) ({ \
  238. int *__futex = &(lock); \
  239. int __val = atomic_exchange_rel (__futex, 0); \
  240. if (__builtin_expect (__val & FUTEX_WAITERS, 0)) \
  241. lll_futex_wake (__futex, 1, private); \
  242. }))
  243. #define lll_islocked(futex) \
  244. (futex != 0)
  245. /* Initializers for lock. */
  246. #define LLL_LOCK_INITIALIZER (0)
  247. #define LLL_LOCK_INITIALIZER_LOCKED (1)
  248. /* The states of a lock are:
  249. 0 - untaken
  250. 1 - taken by one user
  251. >1 - taken by more users */
  252. /* The kernel notifies a process which uses CLONE_CLEARTID via futex
  253. wakeup when the clone terminates. The memory location contains the
  254. thread ID while the clone is running and is reset to zero
  255. afterwards. */
  256. #define lll_wait_tid(tid) \
  257. do { \
  258. __typeof (tid) __tid; \
  259. while ((__tid = (tid)) != 0) \
  260. lll_futex_wait (&(tid), __tid, LLL_SHARED); \
  261. } while (0)
  262. extern int __lll_timedwait_tid (int *, const struct timespec *)
  263. attribute_hidden;
  264. #define lll_timedwait_tid(tid, abstime) \
  265. ({ \
  266. int __res = 0; \
  267. if ((tid) != 0) \
  268. __res = __lll_timedwait_tid (&(tid), (abstime)); \
  269. __res; \
  270. })
  271. #endif /* lowlevellock.h */