lowlevellock.h 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294
  1. /* Copyright (C) 2003, 2004 Free Software Foundation, Inc.
  2. This file is part of the GNU C Library.
  3. The GNU C Library is free software; you can redistribute it and/or
  4. modify it under the terms of the GNU Lesser General Public
  5. License as published by the Free Software Foundation; either
  6. version 2.1 of the License, or (at your option) any later version.
  7. The GNU C Library is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  10. Lesser General Public License for more details.
  11. You should have received a copy of the GNU Lesser General Public
  12. License along with the GNU C Library; if not, write to the Free
  13. Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
  14. 02111-1307 USA. */
  15. #ifndef _LOWLEVELLOCK_H
  16. #define _LOWLEVELLOCK_H 1
  17. #include <syscall.h>
  18. #include <time.h>
  19. #include <sys/param.h>
  20. #include <bits/pthreadtypes.h>
  21. #define FUTEX_WAIT 0
  22. #define FUTEX_WAKE 1
  23. /* Initializer for compatibility lock. */
  24. #define LLL_MUTEX_LOCK_INITIALIZER (0)
  25. #define LLL_MUTEX_LOCK_INITIALIZER_LOCKED (1)
  26. #define LLL_MUTEX_LOCK_INITIALIZER_WAITERS (2)
  27. extern int __lll_mutex_lock_wait (int val, int *__futex) attribute_hidden;
  28. extern int __lll_mutex_timedlock_wait (int val, int *__futex,
  29. const struct timespec *abstime)
  30. attribute_hidden;
  31. extern int __lll_mutex_unlock_wake (int *__futex) attribute_hidden;
  32. #define lll_mutex_trylock(futex) \
  33. ({ unsigned char __result; \
  34. __asm __volatile ("\
  35. .align 2\n\
  36. mova 1f,r0\n\
  37. nop\n\
  38. mov r15,r1\n\
  39. mov #-8,r15\n\
  40. 0: mov.l @%1,r2\n\
  41. cmp/eq r2,%3\n\
  42. bf 1f\n\
  43. mov.l %2,@%1\n\
  44. 1: mov r1,r15\n\
  45. mov #-1,%0\n\
  46. negc %0,%0"\
  47. : "=r" (__result) \
  48. : "r" (&(futex)), \
  49. "r" (LLL_MUTEX_LOCK_INITIALIZER_LOCKED), \
  50. "r" (LLL_MUTEX_LOCK_INITIALIZER) \
  51. : "r0", "r1", "r2", "t", "memory"); \
  52. __result; })
  53. #define lll_mutex_cond_trylock(futex) \
  54. ({ unsigned char __result; \
  55. __asm __volatile ("\
  56. .align 2\n\
  57. mova 1f,r0\n\
  58. nop\n\
  59. mov r15,r1\n\
  60. mov #-8,r15\n\
  61. 0: mov.l @%1,r2\n\
  62. cmp/eq r2,%3\n\
  63. bf 1f\n\
  64. mov.l %2,@%1\n\
  65. 1: mov r1,r15\n\
  66. mov #-1,%0\n\
  67. negc %0,%0"\
  68. : "=r" (__result) \
  69. : "r" (&(futex)), \
  70. "r" (LLL_MUTEX_LOCK_INITIALIZER_WAITERS), \
  71. "r" (LLL_MUTEX_LOCK_INITIALIZER) \
  72. : "r0", "r1", "r2", "t", "memory"); \
  73. __result; })
  74. #define lll_mutex_lock(futex) \
  75. (void) ({ int __result, *__futex = &(futex); \
  76. __asm __volatile ("\
  77. .align 2\n\
  78. mova 1f,r0\n\
  79. nop\n\
  80. mov r15,r1\n\
  81. mov #-8,r15\n\
  82. 0: mov.l @%2,%0\n\
  83. tst %0,%0\n\
  84. bf 1f\n\
  85. mov.l %1,@%2\n\
  86. 1: mov r1,r15"\
  87. : "=&r" (__result) : "r" (1), "r" (__futex) \
  88. : "r0", "r1", "t", "memory"); \
  89. if (__result) \
  90. __lll_mutex_lock_wait (__result, __futex); })
  91. /* Special version of lll_mutex_lock which causes the unlock function to
  92. always wakeup waiters. */
  93. #define lll_mutex_cond_lock(futex) \
  94. (void) ({ int __result, *__futex = &(futex); \
  95. __asm __volatile ("\
  96. .align 2\n\
  97. mova 1f,r0\n\
  98. nop\n\
  99. mov r15,r1\n\
  100. mov #-8,r15\n\
  101. 0: mov.l @%2,%0\n\
  102. tst %0,%0\n\
  103. bf 1f\n\
  104. mov.l %1,@%2\n\
  105. 1: mov r1,r15"\
  106. : "=&r" (__result) : "r" (2), "r" (__futex) \
  107. : "r0", "r1", "t", "memory"); \
  108. if (__result) \
  109. __lll_mutex_lock_wait (__result, __futex); })
  110. #define lll_mutex_timedlock(futex, timeout) \
  111. ({ int __result, *__futex = &(futex); \
  112. __asm __volatile ("\
  113. .align 2\n\
  114. mova 1f,r0\n\
  115. nop\n\
  116. mov r15,r1\n\
  117. mov #-8,r15\n\
  118. 0: mov.l @%2,%0\n\
  119. tst %0,%0\n\
  120. bf 1f\n\
  121. mov.l %1,@%2\n\
  122. 1: mov r1,r15"\
  123. : "=&r" (__result) : "r" (1), "r" (__futex) \
  124. : "r0", "r1", "t", "memory"); \
  125. if (__result) \
  126. __result = __lll_mutex_timedlock_wait (__result, __futex, timeout); \
  127. __result; })
  128. #define lll_mutex_unlock(futex) \
  129. (void) ({ int __result, *__futex = &(futex); \
  130. __asm __volatile ("\
  131. .align 2\n\
  132. mova 1f,r0\n\
  133. mov r15,r1\n\
  134. mov #-6,r15\n\
  135. 0: mov.l @%1,%0\n\
  136. add #-1,%0\n\
  137. mov.l %0,@%1\n\
  138. 1: mov r1,r15"\
  139. : "=&r" (__result) : "r" (__futex) \
  140. : "r0", "r1", "memory"); \
  141. if (__result) \
  142. __lll_mutex_unlock_wake (__futex); })
  143. #define lll_mutex_islocked(futex) \
  144. (futex != 0)
  145. /* We have a separate internal lock implementation which is not tied
  146. to binary compatibility. */
  147. /* Type for lock object. */
  148. typedef int lll_lock_t;
  149. /* Initializers for lock. */
  150. #define LLL_LOCK_INITIALIZER (0)
  151. #define LLL_LOCK_INITIALIZER_LOCKED (1)
  152. # ifdef NEED_SYSCALL_INST_PAD
  153. # define SYSCALL_WITH_INST_PAD "\
  154. trapa #0x14; or r0,r0; or r0,r0; or r0,r0; or r0,r0; or r0,r0"
  155. # else
  156. # define SYSCALL_WITH_INST_PAD "\
  157. trapa #0x14"
  158. # endif
  159. #define lll_futex_wait(futex, val) \
  160. do { \
  161. int __ignore; \
  162. register unsigned long __r3 __asm ("r3") = SYS_futex; \
  163. register unsigned long __r4 __asm ("r4") = (unsigned long) (futex); \
  164. register unsigned long __r5 __asm ("r5") = FUTEX_WAIT; \
  165. register unsigned long __r6 __asm ("r6") = (unsigned long) (val); \
  166. register unsigned long __r7 __asm ("r7") = 0; \
  167. __asm __volatile (SYSCALL_WITH_INST_PAD \
  168. : "=z" (__ignore) \
  169. : "r" (__r3), "r" (__r4), "r" (__r5), \
  170. "r" (__r6), "r" (__r7) \
  171. : "memory", "t"); \
  172. } while (0)
  173. #define lll_futex_timed_wait(futex, val, timeout) \
  174. ({ \
  175. int __status; \
  176. register unsigned long __r3 __asm ("r3") = SYS_futex; \
  177. register unsigned long __r4 __asm ("r4") = (unsigned long) (futex); \
  178. register unsigned long __r5 __asm ("r5") = FUTEX_WAIT; \
  179. register unsigned long __r6 __asm ("r6") = (unsigned long) (val); \
  180. register unsigned long __r7 __asm ("r7") = (timeout); \
  181. __asm __volatile (SYSCALL_WITH_INST_PAD \
  182. : "=z" (__status) \
  183. : "r" (__r3), "r" (__r4), "r" (__r5), \
  184. "r" (__r6), "r" (__r7) \
  185. : "memory", "t"); \
  186. __status; \
  187. })
  188. #define lll_futex_wake(futex, nr) \
  189. do { \
  190. int __ignore; \
  191. register unsigned long __r3 __asm ("r3") = SYS_futex; \
  192. register unsigned long __r4 __asm ("r4") = (unsigned long) (futex); \
  193. register unsigned long __r5 __asm ("r5") = FUTEX_WAKE; \
  194. register unsigned long __r6 __asm ("r6") = (unsigned long) (nr); \
  195. register unsigned long __r7 __asm ("r7") = 0; \
  196. __asm __volatile (SYSCALL_WITH_INST_PAD \
  197. : "=z" (__ignore) \
  198. : "r" (__r3), "r" (__r4), "r" (__r5), \
  199. "r" (__r6), "r" (__r7) \
  200. : "memory", "t"); \
  201. } while (0)
  202. extern int lll_unlock_wake_cb (int *__futex) attribute_hidden;
  203. /* The states of a lock are:
  204. 0 - untaken
  205. 1 - taken by one user
  206. 2 - taken by more users */
  207. #define lll_trylock(futex) lll_mutex_trylock (futex)
  208. #define lll_lock(futex) lll_mutex_lock (futex)
  209. #define lll_unlock(futex) lll_mutex_unlock (futex)
  210. #define lll_islocked(futex) \
  211. (futex != LLL_LOCK_INITIALIZER)
  212. /* The kernel notifies a process with uses CLONE_CLEARTID via futex
  213. wakeup when the clone terminates. The memory location contains the
  214. thread ID while the clone is running and is reset to zero
  215. afterwards. */
  216. extern int __lll_wait_tid (int *tid) attribute_hidden;
  217. #define lll_wait_tid(tid) \
  218. do { \
  219. __typeof (tid) *__tid = &(tid); \
  220. while (*__tid != 0) \
  221. lll_futex_wait (__tid, *__tid); \
  222. } while (0)
  223. extern int __lll_timedwait_tid (int *tid, const struct timespec *abstime)
  224. attribute_hidden;
  225. #define lll_timedwait_tid(tid, abstime) \
  226. ({ \
  227. int __result = 0; \
  228. if (tid != 0) \
  229. { \
  230. if (abstime->tv_nsec < 0 || abstime->tv_nsec >= 1000000000) \
  231. __result = EINVAL; \
  232. else \
  233. __result = __lll_timedwait_tid (&tid, abstime); \
  234. } \
  235. __result; })
  236. /* Conditional variable handling. */
  237. extern void __lll_cond_wait (pthread_cond_t *cond) attribute_hidden;
  238. extern int __lll_cond_timedwait (pthread_cond_t *cond,
  239. const struct timespec *abstime)
  240. attribute_hidden;
  241. extern void __lll_cond_wake (pthread_cond_t *cond) attribute_hidden;
  242. extern void __lll_cond_broadcast (pthread_cond_t *cond) attribute_hidden;
  243. #define lll_cond_wait(cond) \
  244. __lll_cond_wait (cond)
  245. #define lll_cond_timedwait(cond, abstime) \
  246. __lll_cond_timedwait (cond, abstime)
  247. #define lll_cond_wake(cond) \
  248. __lll_cond_wake (cond)
  249. #define lll_cond_broadcast(cond) \
  250. __lll_cond_broadcast (cond)
  251. #endif /* lowlevellock.h */