lowlevellock.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342
  1. /* Copyright (C) 2002, 2003, 2004 Free Software Foundation, Inc.
  2. This file is part of the GNU C Library.
  3. Contributed by Ulrich Drepper <drepper@redhat.com>, 2002.
  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. #include <time.h>
  19. #include <sys/param.h>
  20. #include <bits/pthreadtypes.h>
  21. #include <atomic.h>
  22. #include <sysdep.h>
  23. #ifndef LOCK_INSTR
  24. # ifdef UP
  25. # define LOCK_INSTR /* nothing */
  26. # else
  27. # define LOCK_INSTR "lock;"
  28. # endif
  29. #endif
  30. #define FUTEX_WAIT 0
  31. #define FUTEX_WAKE 1
  32. /* Initializer for compatibility lock. */
  33. #define LLL_MUTEX_LOCK_INITIALIZER (0)
  34. #define LLL_MUTEX_LOCK_INITIALIZER_LOCKED (1)
  35. #define LLL_MUTEX_LOCK_INITIALIZER_WAITERS (2)
  36. /* Delay in spinlock loop. */
  37. #define BUSY_WAIT_NOP __asm__ ("rep; nop")
  38. #define lll_futex_wait(futex, val) \
  39. do { \
  40. int __ignore; \
  41. register __typeof (val) _val __asm__ ("edx") = (val); \
  42. __asm__ __volatile ("xorq %%r10, %%r10\n\t" \
  43. "syscall" \
  44. : "=a" (__ignore) \
  45. : "0" (SYS_futex), "D" (futex), "S" (FUTEX_WAIT), \
  46. "d" (_val) \
  47. : "memory", "cc", "r10", "r11", "cx"); \
  48. } while (0)
  49. #define lll_futex_wake(futex, nr) \
  50. do { \
  51. int __ignore; \
  52. register __typeof (nr) _nr __asm__ ("edx") = (nr); \
  53. __asm__ __volatile ("syscall" \
  54. : "=a" (__ignore) \
  55. : "0" (SYS_futex), "D" (futex), "S" (FUTEX_WAKE), \
  56. "d" (_nr) \
  57. : "memory", "cc", "r10", "r11", "cx"); \
  58. } while (0)
  59. /* Does not preserve %eax and %ecx. */
  60. extern int __lll_mutex_lock_wait (int *__futex, int __val) attribute_hidden;
  61. /* Does not preserver %eax, %ecx, and %edx. */
  62. extern int __lll_mutex_timedlock_wait (int *__futex, int __val,
  63. const struct timespec *__abstime)
  64. attribute_hidden;
  65. /* Preserves all registers but %eax. */
  66. extern int __lll_mutex_unlock_wait (int *__futex) attribute_hidden;
  67. /* NB: in the lll_mutex_trylock macro we simply return the value in %eax
  68. after the cmpxchg instruction. In case the operation succeded this
  69. value is zero. In case the operation failed, the cmpxchg instruction
  70. has loaded the current value of the memory work which is guaranteed
  71. to be nonzero. */
  72. #define lll_mutex_trylock(futex) \
  73. ({ int ret; \
  74. __asm__ __volatile (LOCK_INSTR "cmpxchgl %2, %1" \
  75. : "=a" (ret), "=m" (futex) \
  76. : "r" (LLL_MUTEX_LOCK_INITIALIZER_LOCKED), "m" (futex),\
  77. "0" (LLL_MUTEX_LOCK_INITIALIZER) \
  78. : "memory"); \
  79. ret; })
  80. #define lll_mutex_cond_trylock(futex) \
  81. ({ int ret; \
  82. __asm__ __volatile (LOCK_INSTR "cmpxchgl %2, %1" \
  83. : "=a" (ret), "=m" (futex) \
  84. : "r" (LLL_MUTEX_LOCK_INITIALIZER_WAITERS), \
  85. "m" (futex), "0" (LLL_MUTEX_LOCK_INITIALIZER) \
  86. : "memory"); \
  87. ret; })
  88. #define lll_mutex_lock(futex) \
  89. (void) ({ int ignore1, ignore2, ignore3; \
  90. __asm__ __volatile (LOCK_INSTR "cmpxchgl %0, %2\n\t" \
  91. "jnz 1f\n\t" \
  92. ".subsection 1\n" \
  93. "1:\tleaq %2, %%rdi\n\t" \
  94. "subq $128, %%rsp\n\t" \
  95. "callq __lll_mutex_lock_wait\n\t" \
  96. "addq $128, %%rsp\n\t" \
  97. "jmp 2f\n\t" \
  98. ".previous\n" \
  99. "2:" \
  100. : "=S" (ignore1), "=&D" (ignore2), "=m" (futex),\
  101. "=a" (ignore3) \
  102. : "0" (1), "m" (futex), "3" (0) \
  103. : "cx", "r11", "cc", "memory"); })
  104. #define lll_mutex_cond_lock(futex) \
  105. (void) ({ int ignore1, ignore2, ignore3; \
  106. __asm__ __volatile (LOCK_INSTR "cmpxchgl %0, %2\n\t" \
  107. "jnz 1f\n\t" \
  108. ".subsection 1\n" \
  109. "1:\tleaq %2, %%rdi\n\t" \
  110. "subq $128, %%rsp\n\t" \
  111. "callq __lll_mutex_lock_wait\n\t" \
  112. "addq $128, %%rsp\n\t" \
  113. "jmp 2f\n\t" \
  114. ".previous\n" \
  115. "2:" \
  116. : "=S" (ignore1), "=&D" (ignore2), "=m" (futex),\
  117. "=a" (ignore3) \
  118. : "0" (2), "m" (futex), "3" (0) \
  119. : "cx", "r11", "cc", "memory"); })
  120. #define lll_mutex_timedlock(futex, timeout) \
  121. ({ int _result, ignore1, ignore2, ignore3; \
  122. __asm__ __volatile (LOCK_INSTR "cmpxchgl %2, %4\n\t" \
  123. "jnz 1f\n\t" \
  124. ".subsection 1\n" \
  125. "1:\tleaq %4, %%rdi\n\t" \
  126. "movq %8, %%rdx\n\t" \
  127. "subq $128, %%rsp\n\t" \
  128. "callq __lll_mutex_timedlock_wait\n\t" \
  129. "addq $128, %%rsp\n\t" \
  130. "jmp 2f\n\t" \
  131. ".previous\n" \
  132. "2:" \
  133. : "=a" (_result), "=&D" (ignore1), "=S" (ignore2), \
  134. "=&d" (ignore3), "=m" (futex) \
  135. : "0" (0), "2" (1), "m" (futex), "m" (timeout) \
  136. : "memory", "cx", "cc", "r10", "r11"); \
  137. _result; })
  138. #define lll_mutex_unlock(futex) \
  139. (void) ({ int ignore; \
  140. __asm__ __volatile (LOCK_INSTR "decl %0\n\t" \
  141. "jne 1f\n\t" \
  142. ".subsection 1\n" \
  143. "1:\tleaq %0, %%rdi\n\t" \
  144. "subq $128, %%rsp\n\t" \
  145. "callq __lll_mutex_unlock_wake\n\t" \
  146. "addq $128, %%rsp\n\t" \
  147. "jmp 2f\n\t" \
  148. ".previous\n" \
  149. "2:" \
  150. : "=m" (futex), "=&D" (ignore) \
  151. : "m" (futex) \
  152. : "ax", "cx", "r11", "cc", "memory"); })
  153. #define lll_mutex_islocked(futex) \
  154. (futex != LLL_MUTEX_LOCK_INITIALIZER)
  155. /* We have a separate internal lock implementation which is not tied
  156. to binary compatibility. */
  157. /* Type for lock object. */
  158. typedef int lll_lock_t;
  159. /* Initializers for lock. */
  160. #define LLL_LOCK_INITIALIZER (0)
  161. #define LLL_LOCK_INITIALIZER_LOCKED (1)
  162. extern int lll_unlock_wake_cb (int *__futex) attribute_hidden;
  163. /* The states of a lock are:
  164. 0 - untaken
  165. 1 - taken by one user
  166. 2 - taken by more users */
  167. #if defined NOT_IN_libc || defined UP
  168. # define lll_trylock(futex) lll_mutex_trylock (futex)
  169. # define lll_lock(futex) lll_mutex_lock (futex)
  170. # define lll_unlock(futex) lll_mutex_unlock (futex)
  171. #else
  172. /* Special versions of the macros for use in libc itself. They avoid
  173. the lock prefix when the thread library is not used.
  174. The code sequence to avoid unnecessary lock prefixes is what the AMD
  175. guys suggested. If you do not like it, bring it up with AMD.
  176. XXX In future we might even want to avoid it on UP machines. */
  177. # define lll_trylock(futex) \
  178. ({ unsigned char ret; \
  179. __asm__ __volatile ("cmpl $0, __libc_multiple_threads(%%rip)\n\t" \
  180. "je 0f\n\t" \
  181. "lock; cmpxchgl %2, %1\n\t" \
  182. "jmp 1f\n" \
  183. "0:\tcmpxchgl %2, %1\n\t" \
  184. "1:setne %0" \
  185. : "=a" (ret), "=m" (futex) \
  186. : "r" (LLL_MUTEX_LOCK_INITIALIZER_LOCKED), "m" (futex),\
  187. "0" (LLL_MUTEX_LOCK_INITIALIZER) \
  188. : "memory"); \
  189. ret; })
  190. # define lll_lock(futex) \
  191. (void) ({ int ignore1, ignore2, ignore3; \
  192. __asm__ __volatile ("cmpl $0, __libc_multiple_threads(%%rip)\n\t" \
  193. "je 0f\n\t" \
  194. "lock; cmpxchgl %0, %2\n\t" \
  195. "jnz 1f\n\t" \
  196. "jmp 2f\n" \
  197. "0:\tcmpxchgl %0, %2\n\t" \
  198. "jnz 1f\n\t" \
  199. ".subsection 1\n" \
  200. "1:\tleaq %2, %%rdi\n\t" \
  201. "subq $128, %%rsp\n\t" \
  202. "callq __lll_mutex_lock_wait\n\t" \
  203. "addq $128, %%rsp\n\t" \
  204. "jmp 2f\n\t" \
  205. ".previous\n" \
  206. "2:" \
  207. : "=S" (ignore1), "=&D" (ignore2), "=m" (futex),\
  208. "=a" (ignore3) \
  209. : "0" (1), "m" (futex), "3" (0) \
  210. : "cx", "r11", "cc", "memory"); })
  211. # define lll_unlock(futex) \
  212. (void) ({ int ignore; \
  213. __asm__ __volatile ("cmpl $0, __libc_multiple_threads(%%rip)\n\t" \
  214. "je 0f\n\t" \
  215. "lock; decl %0\n\t" \
  216. "jne 1f\n\t" \
  217. "jmp 2f\n" \
  218. "0:\tdecl %0\n\t" \
  219. "jne 1f\n\t" \
  220. ".subsection 1\n" \
  221. "1:\tleaq %0, %%rdi\n\t" \
  222. "subq $128, %%rsp\n\t" \
  223. "callq __lll_mutex_unlock_wake\n\t" \
  224. "addq $128, %%rsp\n\t" \
  225. "jmp 2f\n\t" \
  226. ".previous\n" \
  227. "2:" \
  228. : "=m" (futex), "=&D" (ignore) \
  229. : "m" (futex) \
  230. : "ax", "cx", "r11", "cc", "memory"); })
  231. #endif
  232. #define lll_islocked(futex) \
  233. (futex != LLL_MUTEX_LOCK_INITIALIZER)
  234. /* The kernel notifies a process with uses CLONE_CLEARTID via futex
  235. wakeup when the clone terminates. The memory location contains the
  236. thread ID while the clone is running and is reset to zero
  237. afterwards.
  238. The macro parameter must not have any side effect. */
  239. #define lll_wait_tid(tid) \
  240. do { \
  241. int __ignore; \
  242. register __typeof (tid) _tid __asm__ ("edx") = (tid); \
  243. if (_tid != 0) \
  244. __asm__ __volatile ("xorq %%r10, %%r10\n\t" \
  245. "1:\tmovq %2, %%rax\n\t" \
  246. "syscall\n\t" \
  247. "cmpl $0, (%%rdi)\n\t" \
  248. "jne 1b" \
  249. : "=&a" (__ignore) \
  250. : "S" (FUTEX_WAIT), "i" (SYS_futex), "D" (&tid), \
  251. "d" (_tid) \
  252. : "memory", "cc", "r10", "r11", "cx"); \
  253. } while (0)
  254. extern int __lll_timedwait_tid (int *tid, const struct timespec *abstime)
  255. attribute_hidden;
  256. #define lll_timedwait_tid(tid, abstime) \
  257. ({ \
  258. int __result = 0; \
  259. if (tid != 0) \
  260. { \
  261. if (abstime->tv_nsec < 0 || abstime->tv_nsec >= 1000000000) \
  262. __result = EINVAL; \
  263. else \
  264. __result = __lll_timedwait_tid (&tid, abstime); \
  265. } \
  266. __result; })
  267. /* Conditional variable handling. */
  268. extern void __lll_cond_wait (pthread_cond_t *cond) attribute_hidden;
  269. extern int __lll_cond_timedwait (pthread_cond_t *cond,
  270. const struct timespec *abstime)
  271. attribute_hidden;
  272. extern void __lll_cond_wake (pthread_cond_t *cond) attribute_hidden;
  273. extern void __lll_cond_broadcast (pthread_cond_t *cond) attribute_hidden;
  274. #define lll_cond_wait(cond) \
  275. __lll_cond_wait (cond)
  276. #define lll_cond_timedwait(cond, abstime) \
  277. __lll_cond_timedwait (cond, abstime)
  278. #define lll_cond_wake(cond) \
  279. __lll_cond_wake (cond)
  280. #define lll_cond_broadcast(cond) \
  281. __lll_cond_broadcast (cond)
  282. #endif /* lowlevellock.h */