lowlevellock.h 12 KB

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