lowlevellock.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420
  1. /* Copyright (C) 2003, 2004, 2006, 2007, 2008, 2009
  2. 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, write to the Free
  14. Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
  15. 02111-1307 USA. */
  16. #ifndef _LOWLEVELLOCK_H
  17. #define _LOWLEVELLOCK_H 1
  18. #ifndef __ASSEMBLER__
  19. #include <time.h>
  20. #include <sys/param.h>
  21. #include <bits/pthreadtypes.h>
  22. #include <bits/kernel-features.h>
  23. #endif
  24. #define FUTEX_WAIT 0
  25. #define FUTEX_WAKE 1
  26. #define FUTEX_CMP_REQUEUE 4
  27. #define FUTEX_WAKE_OP 5
  28. #define FUTEX_LOCK_PI 6
  29. #define FUTEX_UNLOCK_PI 7
  30. #define FUTEX_TRYLOCK_PI 8
  31. #define FUTEX_WAIT_BITSET 9
  32. #define FUTEX_WAKE_BITSET 10
  33. #define FUTEX_PRIVATE_FLAG 128
  34. #define FUTEX_CLOCK_REALTIME 256
  35. #define FUTEX_BITSET_MATCH_ANY 0xffffffff
  36. #define FUTEX_OP_CLEAR_WAKE_IF_GT_ONE ((4 << 24) | 1)
  37. /* Values for 'private' parameter of locking macros. Yes, the
  38. definition seems to be backwards. But it is not. The bit will be
  39. reversed before passing to the system call. */
  40. #define LLL_PRIVATE 0
  41. #define LLL_SHARED FUTEX_PRIVATE_FLAG
  42. #if !defined NOT_IN_libc || defined IS_IN_rtld
  43. /* In libc.so or ld.so all futexes are private. */
  44. # ifdef __ASSUME_PRIVATE_FUTEX
  45. # define __lll_private_flag(fl, private) \
  46. ((fl) | FUTEX_PRIVATE_FLAG)
  47. # else
  48. # define __lll_private_flag(fl, private) \
  49. ((fl) | THREAD_GETMEM (THREAD_SELF, header.private_futex))
  50. # endif
  51. #else
  52. # ifdef __ASSUME_PRIVATE_FUTEX
  53. # define __lll_private_flag(fl, private) \
  54. (((fl) | FUTEX_PRIVATE_FLAG) ^ (private))
  55. # else
  56. # define __lll_private_flag(fl, private) \
  57. (__builtin_constant_p (private) \
  58. ? ((private) == 0 \
  59. ? ((fl) | THREAD_GETMEM (THREAD_SELF, header.private_futex)) \
  60. : (fl)) \
  61. : ((fl) | (((private) ^ FUTEX_PRIVATE_FLAG) \
  62. & THREAD_GETMEM (THREAD_SELF, header.private_futex))))
  63. # endif
  64. #endif
  65. #ifndef __ASSEMBLER__
  66. /* Initializer for compatibility lock. */
  67. #define LLL_LOCK_INITIALIZER (0)
  68. #define LLL_LOCK_INITIALIZER_LOCKED (1)
  69. #define LLL_LOCK_INITIALIZER_WAITERS (2)
  70. extern int __lll_lock_wait_private (int val, int *__futex)
  71. attribute_hidden;
  72. extern int __lll_lock_wait (int val, int *__futex, int private)
  73. attribute_hidden;
  74. extern int __lll_timedlock_wait (int val, int *__futex,
  75. const struct timespec *abstime, int private)
  76. attribute_hidden;
  77. extern int __lll_robust_lock_wait (int val, int *__futex, int private)
  78. attribute_hidden;
  79. extern int __lll_robust_timedlock_wait (int val, int *__futex,
  80. const struct timespec *abstime,
  81. int private)
  82. attribute_hidden;
  83. extern int __lll_unlock_wake_private (int *__futex) attribute_hidden;
  84. extern int __lll_unlock_wake (int *__futex, int private) attribute_hidden;
  85. #define lll_trylock(futex) \
  86. ({ unsigned char __result; \
  87. __asm__ __volatile__ ("\
  88. .align 2\n\
  89. mova 1f,r0\n\
  90. nop\n\
  91. mov r15,r1\n\
  92. mov #-8,r15\n\
  93. 0: mov.l @%1,r2\n\
  94. cmp/eq r2,%3\n\
  95. bf 1f\n\
  96. mov.l %2,@%1\n\
  97. 1: mov r1,r15\n\
  98. mov #-1,%0\n\
  99. negc %0,%0"\
  100. : "=r" (__result) \
  101. : "r" (&(futex)), \
  102. "r" (LLL_LOCK_INITIALIZER_LOCKED), \
  103. "r" (LLL_LOCK_INITIALIZER) \
  104. : "r0", "r1", "r2", "t", "memory"); \
  105. __result; })
  106. #define lll_robust_trylock(futex, id) \
  107. ({ unsigned char __result; \
  108. __asm__ __volatile__ ("\
  109. .align 2\n\
  110. mova 1f,r0\n\
  111. nop\n\
  112. mov r15,r1\n\
  113. mov #-8,r15\n\
  114. 0: mov.l @%1,r2\n\
  115. cmp/eq r2,%3\n\
  116. bf 1f\n\
  117. mov.l %2,@%1\n\
  118. 1: mov r1,r15\n\
  119. mov #-1,%0\n\
  120. negc %0,%0"\
  121. : "=r" (__result) \
  122. : "r" (&(futex)), \
  123. "r" (id), \
  124. "r" (LLL_LOCK_INITIALIZER) \
  125. : "r0", "r1", "r2", "t", "memory"); \
  126. __result; })
  127. #define lll_cond_trylock(futex) \
  128. ({ unsigned char __result; \
  129. __asm__ __volatile__ ("\
  130. .align 2\n\
  131. mova 1f,r0\n\
  132. nop\n\
  133. mov r15,r1\n\
  134. mov #-8,r15\n\
  135. 0: mov.l @%1,r2\n\
  136. cmp/eq r2,%3\n\
  137. bf 1f\n\
  138. mov.l %2,@%1\n\
  139. 1: mov r1,r15\n\
  140. mov #-1,%0\n\
  141. negc %0,%0"\
  142. : "=r" (__result) \
  143. : "r" (&(futex)), \
  144. "r" (LLL_LOCK_INITIALIZER_WAITERS), \
  145. "r" (LLL_LOCK_INITIALIZER) \
  146. : "r0", "r1", "r2", "t", "memory"); \
  147. __result; })
  148. #define lll_lock(futex, private) \
  149. (void) ({ int __result, *__futex = &(futex); \
  150. __asm__ __volatile__ ("\
  151. .align 2\n\
  152. mova 1f,r0\n\
  153. nop\n\
  154. mov r15,r1\n\
  155. mov #-8,r15\n\
  156. 0: mov.l @%2,%0\n\
  157. tst %0,%0\n\
  158. bf 1f\n\
  159. mov.l %1,@%2\n\
  160. 1: mov r1,r15"\
  161. : "=&r" (__result) : "r" (1), "r" (__futex) \
  162. : "r0", "r1", "t", "memory"); \
  163. if (__result) \
  164. { \
  165. if (__builtin_constant_p (private) \
  166. && (private) == LLL_PRIVATE) \
  167. __lll_lock_wait_private (__result, __futex); \
  168. else \
  169. __lll_lock_wait (__result, __futex, (private)); \
  170. } \
  171. })
  172. #define lll_robust_lock(futex, id, private) \
  173. ({ int __result, *__futex = &(futex); \
  174. __asm__ __volatile__ ("\
  175. .align 2\n\
  176. mova 1f,r0\n\
  177. nop\n\
  178. mov r15,r1\n\
  179. mov #-8,r15\n\
  180. 0: mov.l @%2,%0\n\
  181. tst %0,%0\n\
  182. bf 1f\n\
  183. mov.l %1,@%2\n\
  184. 1: mov r1,r15"\
  185. : "=&r" (__result) : "r" (id), "r" (__futex) \
  186. : "r0", "r1", "t", "memory"); \
  187. if (__result) \
  188. __result = __lll_robust_lock_wait (__result, __futex, private); \
  189. __result; })
  190. /* Special version of lll_mutex_lock which causes the unlock function to
  191. always wakeup waiters. */
  192. #define lll_cond_lock(futex, private) \
  193. (void) ({ int __result, *__futex = &(futex); \
  194. __asm__ __volatile__ ("\
  195. .align 2\n\
  196. mova 1f,r0\n\
  197. nop\n\
  198. mov r15,r1\n\
  199. mov #-8,r15\n\
  200. 0: mov.l @%2,%0\n\
  201. tst %0,%0\n\
  202. bf 1f\n\
  203. mov.l %1,@%2\n\
  204. 1: mov r1,r15"\
  205. : "=&r" (__result) : "r" (2), "r" (__futex) \
  206. : "r0", "r1", "t", "memory"); \
  207. if (__result) \
  208. __lll_lock_wait (__result, __futex, private); })
  209. #define lll_robust_cond_lock(futex, id, private) \
  210. ({ int __result, *__futex = &(futex); \
  211. __asm__ __volatile__ ("\
  212. .align 2\n\
  213. mova 1f,r0\n\
  214. nop\n\
  215. mov r15,r1\n\
  216. mov #-8,r15\n\
  217. 0: mov.l @%2,%0\n\
  218. tst %0,%0\n\
  219. bf 1f\n\
  220. mov.l %1,@%2\n\
  221. 1: mov r1,r15"\
  222. : "=&r" (__result) : "r" (id | FUTEX_WAITERS), "r" (__futex) \
  223. : "r0", "r1", "t", "memory"); \
  224. if (__result) \
  225. __result = __lll_robust_lock_wait (__result, __futex, private); \
  226. __result; })
  227. #define lll_timedlock(futex, timeout, private) \
  228. ({ int __result, *__futex = &(futex); \
  229. __asm__ __volatile__ ("\
  230. .align 2\n\
  231. mova 1f,r0\n\
  232. nop\n\
  233. mov r15,r1\n\
  234. mov #-8,r15\n\
  235. 0: mov.l @%2,%0\n\
  236. tst %0,%0\n\
  237. bf 1f\n\
  238. mov.l %1,@%2\n\
  239. 1: mov r1,r15"\
  240. : "=&r" (__result) : "r" (1), "r" (__futex) \
  241. : "r0", "r1", "t", "memory"); \
  242. if (__result) \
  243. __result = __lll_timedlock_wait (__result, __futex, timeout, private); \
  244. __result; })
  245. #define lll_robust_timedlock(futex, timeout, id, private) \
  246. ({ int __result, *__futex = &(futex); \
  247. __asm__ __volatile__ ("\
  248. .align 2\n\
  249. mova 1f,r0\n\
  250. nop\n\
  251. mov r15,r1\n\
  252. mov #-8,r15\n\
  253. 0: mov.l @%2,%0\n\
  254. tst %0,%0\n\
  255. bf 1f\n\
  256. mov.l %1,@%2\n\
  257. 1: mov r1,r15"\
  258. : "=&r" (__result) : "r" (id), "r" (__futex) \
  259. : "r0", "r1", "t", "memory"); \
  260. if (__result) \
  261. __result = __lll_robust_timedlock_wait (__result, __futex, \
  262. timeout, private); \
  263. __result; })
  264. #define lll_unlock(futex, private) \
  265. (void) ({ int __result, *__futex = &(futex); \
  266. __asm__ __volatile__ ("\
  267. .align 2\n\
  268. mova 1f,r0\n\
  269. mov r15,r1\n\
  270. mov #-6,r15\n\
  271. 0: mov.l @%1,%0\n\
  272. add #-1,%0\n\
  273. mov.l %0,@%1\n\
  274. 1: mov r1,r15"\
  275. : "=&r" (__result) : "r" (__futex) \
  276. : "r0", "r1", "memory"); \
  277. if (__result) \
  278. { \
  279. if (__builtin_constant_p (private) \
  280. && (private) == LLL_PRIVATE) \
  281. __lll_unlock_wake_private (__futex); \
  282. else \
  283. __lll_unlock_wake (__futex, (private)); \
  284. } \
  285. })
  286. #define lll_robust_unlock(futex, private) \
  287. (void) ({ int __result, *__futex = &(futex); \
  288. __asm__ __volatile__ ("\
  289. .align 2\n\
  290. mova 1f,r0\n\
  291. mov r15,r1\n\
  292. mov #-6,r15\n\
  293. 0: mov.l @%1,%0\n\
  294. and %2,%0\n\
  295. mov.l %0,@%1\n\
  296. 1: mov r1,r15"\
  297. : "=&r" (__result) : "r" (__futex), "r" (FUTEX_WAITERS) \
  298. : "r0", "r1", "memory"); \
  299. if (__result) \
  300. __lll_unlock_wake (__futex, private); })
  301. #define lll_robust_dead(futex, private) \
  302. (void) ({ int __ignore, *__futex = &(futex); \
  303. __asm__ __volatile__ ("\
  304. .align 2\n\
  305. mova 1f,r0\n\
  306. mov r15,r1\n\
  307. mov #-6,r15\n\
  308. 0: mov.l @%1,%0\n\
  309. or %2,%0\n\
  310. mov.l %0,@%1\n\
  311. 1: mov r1,r15"\
  312. : "=&r" (__ignore) : "r" (__futex), "r" (FUTEX_OWNER_DIED) \
  313. : "r0", "r1", "memory"); \
  314. lll_futex_wake (__futex, 1, private); })
  315. # ifdef NEED_SYSCALL_INST_PAD
  316. # define SYSCALL_WITH_INST_PAD "\
  317. trapa #0x14; or r0,r0; or r0,r0; or r0,r0; or r0,r0; or r0,r0"
  318. # else
  319. # define SYSCALL_WITH_INST_PAD "\
  320. trapa #0x14"
  321. # endif
  322. #define lll_futex_wait(futex, val, private) \
  323. lll_futex_timed_wait (futex, val, NULL, private)
  324. #define lll_futex_timed_wait(futex, val, timeout, private) \
  325. ({ \
  326. int __status; \
  327. register unsigned long __r3 __asm__ ("r3") = SYS_futex; \
  328. register unsigned long __r4 __asm__ ("r4") = (unsigned long) (futex); \
  329. register unsigned long __r5 __asm__ ("r5") \
  330. = __lll_private_flag (FUTEX_WAIT, private); \
  331. register unsigned long __r6 __asm__ ("r6") = (unsigned long) (val); \
  332. register unsigned long __r7 __asm__ ("r7") = (timeout); \
  333. __asm__ __volatile__ (SYSCALL_WITH_INST_PAD \
  334. : "=z" (__status) \
  335. : "r" (__r3), "r" (__r4), "r" (__r5), \
  336. "r" (__r6), "r" (__r7) \
  337. : "memory", "t"); \
  338. __status; \
  339. })
  340. #define lll_futex_wake(futex, nr, private) \
  341. do { \
  342. int __ignore; \
  343. register unsigned long __r3 __asm__ ("r3") = SYS_futex; \
  344. register unsigned long __r4 __asm__ ("r4") = (unsigned long) (futex); \
  345. register unsigned long __r5 __asm__ ("r5") \
  346. = __lll_private_flag (FUTEX_WAKE, private); \
  347. register unsigned long __r6 __asm__ ("r6") = (unsigned long) (nr); \
  348. register unsigned long __r7 __asm__ ("r7") = 0; \
  349. __asm__ __volatile__ (SYSCALL_WITH_INST_PAD \
  350. : "=z" (__ignore) \
  351. : "r" (__r3), "r" (__r4), "r" (__r5), \
  352. "r" (__r6), "r" (__r7) \
  353. : "memory", "t"); \
  354. } while (0)
  355. #define lll_islocked(futex) \
  356. (futex != LLL_LOCK_INITIALIZER)
  357. /* The kernel notifies a process with uses CLONE_CLEARTID via futex
  358. wakeup when the clone terminates. The memory location contains the
  359. thread ID while the clone is running and is reset to zero
  360. afterwards. */
  361. #define lll_wait_tid(tid) \
  362. do { \
  363. __typeof (tid) *__tid = &(tid); \
  364. while (*__tid != 0) \
  365. lll_futex_wait (__tid, *__tid, LLL_SHARED); \
  366. } while (0)
  367. extern int __lll_timedwait_tid (int *tid, const struct timespec *abstime)
  368. attribute_hidden;
  369. #define lll_timedwait_tid(tid, abstime) \
  370. ({ \
  371. int __result = 0; \
  372. if (tid != 0) \
  373. { \
  374. if (abstime->tv_nsec < 0 || abstime->tv_nsec >= 1000000000) \
  375. __result = EINVAL; \
  376. else \
  377. __result = __lll_timedwait_tid (&tid, abstime); \
  378. } \
  379. __result; })
  380. #endif /* !__ASSEMBLER__ */
  381. #endif /* lowlevellock.h */