lowlevellock.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363
  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. /* We have a separate internal lock implementation which is not tied
  24. to binary compatibility. */
  25. /* Type for lock object. */
  26. typedef int lll_lock_t;
  27. /* Initializers for lock. */
  28. #define LLL_LOCK_INITIALIZER (0)
  29. #define LLL_LOCK_INITIALIZER_LOCKED (1)
  30. #include <tls.h>
  31. #ifndef LOCK_INSTR
  32. # define LOCK_INSTR "lock;"
  33. #endif
  34. #define FUTEX_WAIT 0
  35. #define FUTEX_WAKE 1
  36. /* Initializer for compatibility lock. */
  37. #define LLL_MUTEX_LOCK_INITIALIZER (0)
  38. #define LLL_MUTEX_LOCK_INITIALIZER_LOCKED (1)
  39. #define LLL_MUTEX_LOCK_INITIALIZER_WAITERS (2)
  40. #ifdef PIC
  41. # define LLL_EBX_LOAD "xchgl %2, %%ebx\n"
  42. # define LLL_EBX_REG "D"
  43. #else
  44. # define LLL_EBX_LOAD
  45. # define LLL_EBX_REG "b"
  46. #endif
  47. #define LLL_ENTER_KERNEL "int $0x80\n\t"
  48. /* Delay in spinlock loop. */
  49. #define BUSY_WAIT_NOP __asm__ ("rep; nop")
  50. #define lll_futex_wait(futex, val) \
  51. ({ \
  52. int __ret; \
  53. register __typeof (val) _val __asm__ ("edx") = (val); \
  54. __asm__ __volatile (LLL_EBX_LOAD \
  55. LLL_ENTER_KERNEL \
  56. LLL_EBX_LOAD \
  57. : "=a" (__ret) \
  58. : "0" (SYS_futex), LLL_EBX_REG (futex), "S" (0), \
  59. "c" (FUTEX_WAIT), "d" (_val), \
  60. "i" (offsetof (tcbhead_t, sysinfo))); \
  61. __ret; })
  62. #define lll_futex_wake(futex, nr) \
  63. ({ \
  64. int __ret; \
  65. register __typeof (nr) _nr __asm__ ("edx") = (nr); \
  66. __asm__ __volatile (LLL_EBX_LOAD \
  67. LLL_ENTER_KERNEL \
  68. LLL_EBX_LOAD \
  69. : "=a" (__ret) \
  70. : "0" (SYS_futex), LLL_EBX_REG (futex), \
  71. "c" (FUTEX_WAKE), "d" (_nr), \
  72. "i" (0) /* phony, to align next arg's number */, \
  73. "i" (offsetof (tcbhead_t, sysinfo))); \
  74. __ret; })
  75. /* Does not preserve %eax and %ecx. */
  76. extern int __lll_mutex_lock_wait (int val, int *__futex)
  77. __attribute ((regparm (2))) attribute_hidden;
  78. /* Does not preserve %eax, %ecx, and %edx. */
  79. extern int __lll_mutex_timedlock_wait (int val, int *__futex,
  80. const struct timespec *abstime)
  81. __attribute ((regparm (3))) attribute_hidden;
  82. /* Preserves all registers but %eax. */
  83. extern int __lll_mutex_unlock_wake (int *__futex)
  84. __attribute ((regparm (1))) attribute_hidden;
  85. /* NB: in the lll_mutex_trylock macro we simply return the value in %eax
  86. after the cmpxchg instruction. In case the operation succeded this
  87. value is zero. In case the operation failed, the cmpxchg instruction
  88. has loaded the current value of the memory work which is guaranteed
  89. to be nonzero. */
  90. #define lll_mutex_trylock(futex) \
  91. ({ int ret; \
  92. __asm__ __volatile (LOCK_INSTR "cmpxchgl %2, %1" \
  93. : "=a" (ret), "=m" (futex) \
  94. : "r" (LLL_MUTEX_LOCK_INITIALIZER_LOCKED), "m" (futex),\
  95. "0" (LLL_MUTEX_LOCK_INITIALIZER) \
  96. : "memory"); \
  97. ret; })
  98. #define lll_mutex_cond_trylock(futex) \
  99. ({ int ret; \
  100. __asm__ __volatile (LOCK_INSTR "cmpxchgl %2, %1" \
  101. : "=a" (ret), "=m" (futex) \
  102. : "r" (LLL_MUTEX_LOCK_INITIALIZER_WAITERS), \
  103. "m" (futex), "0" (LLL_MUTEX_LOCK_INITIALIZER) \
  104. : "memory"); \
  105. ret; })
  106. #define lll_mutex_lock(futex) \
  107. (void) ({ int ignore1, ignore2; \
  108. __asm__ __volatile (LOCK_INSTR "cmpxchgl %1, %2\n\t" \
  109. "jnz _L_mutex_lock_%=\n\t" \
  110. ".subsection 1\n\t" \
  111. ".type _L_mutex_lock_%=,@function\n" \
  112. "_L_mutex_lock_%=:\n\t" \
  113. "leal %2, %%ecx\n\t" \
  114. "call __lll_mutex_lock_wait\n\t" \
  115. "jmp 1f\n\t" \
  116. ".size _L_mutex_lock_%=,.-_L_mutex_lock_%=\n" \
  117. ".previous\n" \
  118. "1:" \
  119. : "=a" (ignore1), "=c" (ignore2), "=m" (futex) \
  120. : "0" (0), "1" (1), "m" (futex) \
  121. : "memory"); })
  122. /* Special version of lll_mutex_lock which causes the unlock function to
  123. always wakeup waiters. */
  124. #define lll_mutex_cond_lock(futex) \
  125. (void) ({ int ignore1, ignore2; \
  126. __asm__ __volatile (LOCK_INSTR "cmpxchgl %1, %2\n\t" \
  127. "jnz _L_mutex_cond_lock_%=\n\t" \
  128. ".subsection 1\n\t" \
  129. ".type _L_mutex_cond_lock_%=,@function\n" \
  130. "_L_mutex_cond_lock_%=:\n\t" \
  131. "leal %2, %%ecx\n\t" \
  132. "call __lll_mutex_lock_wait\n\t" \
  133. "jmp 1f\n\t" \
  134. ".size _L_mutex_cond_lock_%=,.-_L_mutex_cond_lock_%=\n" \
  135. ".previous\n" \
  136. "1:" \
  137. : "=a" (ignore1), "=c" (ignore2), "=m" (futex) \
  138. : "0" (0), "1" (2), "m" (futex) \
  139. : "memory"); })
  140. #define lll_mutex_timedlock(futex, timeout) \
  141. ({ int result, ignore1, ignore2; \
  142. __asm__ __volatile (LOCK_INSTR "cmpxchgl %1, %3\n\t" \
  143. "jnz _L_mutex_timedlock_%=\n\t" \
  144. ".subsection 1\n\t" \
  145. ".type _L_mutex_timedlock_%=,@function\n" \
  146. "_L_mutex_timedlock_%=:\n\t" \
  147. "leal %3, %%ecx\n\t" \
  148. "movl %7, %%edx\n\t" \
  149. "call __lll_mutex_timedlock_wait\n\t" \
  150. "jmp 1f\n\t" \
  151. ".size _L_mutex_timedlock_%=,.-_L_mutex_timedlock_%=\n"\
  152. ".previous\n" \
  153. "1:" \
  154. : "=a" (result), "=c" (ignore1), "=&d" (ignore2), \
  155. "=m" (futex) \
  156. : "0" (0), "1" (1), "m" (futex), "m" (timeout) \
  157. : "memory"); \
  158. result; })
  159. #define lll_mutex_unlock(futex) \
  160. (void) ({ int ignore; \
  161. __asm__ __volatile (LOCK_INSTR "subl $1,%0\n\t" \
  162. "jne _L_mutex_unlock_%=\n\t" \
  163. ".subsection 1\n\t" \
  164. ".type _L_mutex_unlock_%=,@function\n" \
  165. "_L_mutex_unlock_%=:\n\t" \
  166. "leal %0, %%eax\n\t" \
  167. "call __lll_mutex_unlock_wake\n\t" \
  168. "jmp 1f\n\t" \
  169. ".size _L_mutex_unlock_%=,.-_L_mutex_unlock_%=\n" \
  170. ".previous\n" \
  171. "1:" \
  172. : "=m" (futex), "=&a" (ignore) \
  173. : "m" (futex) \
  174. : "memory"); })
  175. #define lll_mutex_islocked(futex) \
  176. (futex != 0)
  177. extern int __lll_lock_wait (int val, int *__futex)
  178. __attribute ((regparm (2))) attribute_hidden;
  179. extern int __lll_unlock_wake (int *__futex)
  180. __attribute ((regparm (1))) attribute_hidden;
  181. extern int lll_unlock_wake_cb (int *__futex) attribute_hidden;
  182. /* The states of a lock are:
  183. 0 - untaken
  184. 1 - taken by one user
  185. 2 - taken by more users */
  186. #if defined NOT_IN_libc
  187. # define lll_trylock(futex) lll_mutex_trylock (futex)
  188. # define lll_lock(futex) lll_mutex_lock (futex)
  189. # define lll_unlock(futex) lll_mutex_unlock (futex)
  190. #else
  191. /* Special versions of the macros for use in libc itself. They avoid
  192. the lock prefix when the thread library is not used. */
  193. # define lll_trylock(futex) \
  194. ({ unsigned char ret; \
  195. __asm__ __volatile ("cmpl $0, %%gs:%P5\n\t" \
  196. "je,pt 0f\n\t" \
  197. "lock\n" \
  198. "0:\tcmpxchgl %2, %1; setne %0" \
  199. : "=a" (ret), "=m" (futex) \
  200. : "r" (LLL_MUTEX_LOCK_INITIALIZER_LOCKED), "m" (futex),\
  201. "0" (LLL_MUTEX_LOCK_INITIALIZER), \
  202. "i" (offsetof (tcbhead_t, multiple_threads)) \
  203. : "memory"); \
  204. ret; })
  205. # define lll_lock(futex) \
  206. (void) ({ int ignore1, ignore2; \
  207. __asm__ __volatile ("cmpl $0, %%gs:%P6\n\t" \
  208. "je,pt 0f\n\t" \
  209. "lock\n" \
  210. "0:\tcmpxchgl %1, %2\n\t" \
  211. "jnz _L_mutex_lock_%=\n\t" \
  212. ".subsection 1\n\t" \
  213. ".type _L_mutex_lock_%=,@function\n" \
  214. "_L_mutex_lock_%=:\n\t" \
  215. "leal %2, %%ecx\n\t" \
  216. "call __lll_mutex_lock_wait\n\t" \
  217. "jmp 1f\n\t" \
  218. ".size _L_mutex_lock_%=,.-_L_mutex_lock_%=\n" \
  219. ".previous\n" \
  220. "1:" \
  221. : "=a" (ignore1), "=c" (ignore2), "=m" (futex) \
  222. : "0" (0), "1" (1), "m" (futex), \
  223. "i" (offsetof (tcbhead_t, multiple_threads)) \
  224. : "memory"); })
  225. # define lll_unlock(futex) \
  226. (void) ({ int ignore; \
  227. __asm__ __volatile ("cmpl $0, %%gs:%P3\n\t" \
  228. "je,pt 0f\n\t" \
  229. "lock\n" \
  230. "0:\tsubl $1,%0\n\t" \
  231. "jne _L_mutex_unlock_%=\n\t" \
  232. ".subsection 1\n\t" \
  233. ".type _L_mutex_unlock_%=,@function\n" \
  234. "_L_mutex_unlock_%=:\n\t" \
  235. "leal %0, %%eax\n\t" \
  236. "call __lll_mutex_unlock_wake\n\t" \
  237. "jmp 1f\n\t" \
  238. ".size _L_mutex_unlock_%=,.-_L_mutex_unlock_%=\n" \
  239. ".previous\n" \
  240. "1:" \
  241. : "=m" (futex), "=&a" (ignore) \
  242. : "m" (futex), \
  243. "i" (offsetof (tcbhead_t, multiple_threads)) \
  244. : "memory"); })
  245. #endif
  246. #define lll_islocked(futex) \
  247. (futex != LLL_LOCK_INITIALIZER)
  248. /* The kernel notifies a process with uses CLONE_CLEARTID via futex
  249. wakeup when the clone terminates. The memory location contains the
  250. thread ID while the clone is running and is reset to zero
  251. afterwards.
  252. The macro parameter must not have any side effect. */
  253. #define lll_wait_tid(tid) \
  254. ({ \
  255. int __ret; \
  256. register __typeof (tid) _tid __asm__ ("edx") = (tid); \
  257. if (_tid != 0) \
  258. __asm__ __volatile (LLL_EBX_LOAD \
  259. "1:\tmovl %1, %%eax\n\t" \
  260. LLL_ENTER_KERNEL \
  261. "cmpl $0, (%%ebx)\n\t" \
  262. "jne,pn 1b\n\t" \
  263. LLL_EBX_LOAD \
  264. : "=&a" (__ret) \
  265. : "i" (SYS_futex), LLL_EBX_REG (&tid), "S" (0), \
  266. "c" (FUTEX_WAIT), "d" (_tid), \
  267. "i" (offsetof (tcbhead_t, sysinfo))); \
  268. __ret; })
  269. extern int __lll_timedwait_tid (int *tid, const struct timespec *abstime)
  270. __attribute__ ((regparm (2))) attribute_hidden;
  271. #define lll_timedwait_tid(tid, abstime) \
  272. ({ \
  273. int __result = 0; \
  274. if (tid != 0) \
  275. { \
  276. if (abstime->tv_nsec < 0 || abstime->tv_nsec >= 1000000000) \
  277. __result = EINVAL; \
  278. else \
  279. __result = __lll_timedwait_tid (&tid, abstime); \
  280. } \
  281. __result; })
  282. /* Conditional variable handling. */
  283. extern void __lll_cond_wait (pthread_cond_t *cond)
  284. __attribute ((regparm (1))) attribute_hidden;
  285. extern int __lll_cond_timedwait (pthread_cond_t *cond,
  286. const struct timespec *abstime)
  287. __attribute ((regparm (2))) attribute_hidden;
  288. extern void __lll_cond_wake (pthread_cond_t *cond)
  289. __attribute ((regparm (1))) attribute_hidden;
  290. extern void __lll_cond_broadcast (pthread_cond_t *cond)
  291. __attribute ((regparm (1))) attribute_hidden;
  292. #define lll_cond_wait(cond) \
  293. __lll_cond_wait (cond)
  294. #define lll_cond_timedwait(cond, abstime) \
  295. __lll_cond_timedwait (cond, abstime)
  296. #define lll_cond_wake(cond) \
  297. __lll_cond_wake (cond)
  298. #define lll_cond_broadcast(cond) \
  299. __lll_cond_broadcast (cond)
  300. #endif /* lowlevellock.h */