lowlevellock.h 12 KB

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