pthread_rwlock_timedrdlock.S 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276
  1. /* Copyright (C) 2002-2005, 2007, 2009 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. #include <sysdep.h>
  17. #include <lowlevellock.h>
  18. #include <lowlevelrwlock.h>
  19. #include <pthread-errnos.h>
  20. #include <bits/kernel-features.h>
  21. #include <tcb-offsets.h>
  22. /* For the calculation see asm/vsyscall.h. */
  23. #define VSYSCALL_ADDR_vgettimeofday 0xffffffffff600000
  24. .text
  25. .globl pthread_rwlock_timedrdlock
  26. .type pthread_rwlock_timedrdlock,@function
  27. .align 16
  28. pthread_rwlock_timedrdlock:
  29. cfi_startproc
  30. pushq %r12
  31. cfi_adjust_cfa_offset(8)
  32. cfi_rel_offset(%r12, 0)
  33. pushq %r13
  34. cfi_adjust_cfa_offset(8)
  35. cfi_rel_offset(%r13, 0)
  36. #ifdef __ASSUME_FUTEX_CLOCK_REALTIME
  37. # define VALREG %edx
  38. #else
  39. pushq %r14
  40. cfi_adjust_cfa_offset(8)
  41. cfi_rel_offset(%r14, 0)
  42. subq $16, %rsp
  43. cfi_adjust_cfa_offset(16)
  44. # define VALREG %r14d
  45. #endif
  46. movq %rdi, %r12
  47. movq %rsi, %r13
  48. /* Get the lock. */
  49. movl $1, %esi
  50. xorl %eax, %eax
  51. LOCK
  52. #if MUTEX == 0
  53. cmpxchgl %esi, (%rdi)
  54. #else
  55. cmpxchgl %esi, MUTEX(%rdi)
  56. #endif
  57. jnz 1f
  58. 2: movl WRITER(%r12), %eax
  59. testl %eax, %eax
  60. jne 14f
  61. cmpl $0, WRITERS_QUEUED(%r12)
  62. je 5f
  63. cmpl $0, FLAGS(%r12)
  64. je 5f
  65. /* Check the value of the timeout parameter. */
  66. 3: cmpq $1000000000, 8(%r13)
  67. jae 19f
  68. incl READERS_QUEUED(%r12)
  69. je 4f
  70. movl READERS_WAKEUP(%r12), VALREG
  71. /* Unlock. */
  72. LOCK
  73. #if MUTEX == 0
  74. decl (%r12)
  75. #else
  76. decl MUTEX(%r12)
  77. #endif
  78. jne 10f
  79. 11:
  80. #ifndef __ASSUME_FUTEX_CLOCK_REALTIME
  81. # ifdef __PIC__
  82. cmpl $0, __have_futex_clock_realtime(%rip)
  83. # else
  84. cmpl $0, __have_futex_clock_realtime
  85. # endif
  86. je .Lreltmo
  87. #endif
  88. movl $FUTEX_PRIVATE_FLAG|FUTEX_WAIT_BITSET|FUTEX_CLOCK_REALTIME, %esi
  89. xorl PSHARED(%r12), %esi
  90. movq %r13, %r10
  91. movl $0xffffffff, %r9d
  92. #ifndef __ASSUME_FUTEX_CLOCK_REALTIME
  93. movl %r14d, %edx
  94. #endif
  95. 21: leaq READERS_WAKEUP(%r12), %rdi
  96. movl $SYS_futex, %eax
  97. syscall
  98. movq %rax, %rdx
  99. #ifndef __ASSUME_FUTEX_CLOCK_REALTIME
  100. .subsection 2
  101. .Lreltmo:
  102. /* Get current time. */
  103. movq %rsp, %rdi
  104. xorl %esi, %esi
  105. movq $VSYSCALL_ADDR_vgettimeofday, %rax
  106. callq *%rax
  107. /* Compute relative timeout. */
  108. movq 8(%rsp), %rax
  109. movl $1000, %edi
  110. mul %rdi /* Milli seconds to nano seconds. */
  111. movq (%r13), %rcx
  112. movq 8(%r13), %rdi
  113. subq (%rsp), %rcx
  114. subq %rax, %rdi
  115. jns 15f
  116. addq $1000000000, %rdi
  117. decq %rcx
  118. 15: testq %rcx, %rcx
  119. js 16f /* Time is already up. */
  120. /* Futex call. */
  121. movq %rcx, (%rsp) /* Store relative timeout. */
  122. movq %rdi, 8(%rsp)
  123. # ifdef __ASSUME_PRIVATE_FUTEX
  124. movl $FUTEX_PRIVATE_FLAG|FUTEX_WAIT, %esi
  125. xorl PSHARED(%r12), %esi
  126. # else
  127. # if FUTEX_WAIT == 0
  128. movl PSHARED(%r12), %esi
  129. # else
  130. movl $FUTEX_WAIT, %esi
  131. orl PSHARED(%r12), %esi
  132. # endif
  133. xorl %fs:PRIVATE_FUTEX, %esi
  134. # endif
  135. movq %rsp, %r10
  136. movl %r14d, %edx
  137. jmp 21b
  138. .previous
  139. #endif
  140. 17: /* Reget the lock. */
  141. movl $1, %esi
  142. xorl %eax, %eax
  143. LOCK
  144. #if MUTEX == 0
  145. cmpxchgl %esi, (%r12)
  146. #else
  147. cmpxchgl %esi, MUTEX(%r12)
  148. #endif
  149. jnz 12f
  150. 13: decl READERS_QUEUED(%r12)
  151. cmpq $-ETIMEDOUT, %rdx
  152. jne 2b
  153. 18: movl $ETIMEDOUT, %edx
  154. jmp 9f
  155. 5: xorl %edx, %edx
  156. incl NR_READERS(%r12)
  157. je 8f
  158. 9: LOCK
  159. #if MUTEX == 0
  160. decl (%r12)
  161. #else
  162. decl MUTEX(%r12)
  163. #endif
  164. jne 6f
  165. 7: movq %rdx, %rax
  166. #ifndef __ASSUME_FUTEX_CLOCK_REALTIME
  167. addq $16, %rsp
  168. cfi_adjust_cfa_offset(-16)
  169. popq %r14
  170. cfi_adjust_cfa_offset(-8)
  171. cfi_restore(%r14)
  172. #endif
  173. popq %r13
  174. cfi_adjust_cfa_offset(-8)
  175. cfi_restore(%r13)
  176. popq %r12
  177. cfi_adjust_cfa_offset(-8)
  178. cfi_restore(%r12)
  179. retq
  180. #ifdef __ASSUME_PRIVATE_FUTEX
  181. cfi_adjust_cfa_offset(16)
  182. cfi_rel_offset(%r12, 8)
  183. cfi_rel_offset(%r13, 0)
  184. #else
  185. cfi_adjust_cfa_offset(40)
  186. cfi_offset(%r12, -16)
  187. cfi_offset(%r13, -24)
  188. cfi_offset(%r14, -32)
  189. #endif
  190. 1: movl PSHARED(%rdi), %esi
  191. #if MUTEX != 0
  192. addq $MUTEX, %rdi
  193. #endif
  194. callq __lll_lock_wait
  195. jmp 2b
  196. 14: cmpl %fs:TID, %eax
  197. jne 3b
  198. movl $EDEADLK, %edx
  199. jmp 9b
  200. 6: movl PSHARED(%r12), %esi
  201. #if MUTEX == 0
  202. movq %r12, %rdi
  203. #else
  204. leal MUTEX(%r12), %rdi
  205. #endif
  206. callq __lll_unlock_wake
  207. jmp 7b
  208. /* Overflow. */
  209. 8: decl NR_READERS(%r12)
  210. movl $EAGAIN, %edx
  211. jmp 9b
  212. /* Overflow. */
  213. 4: decl READERS_QUEUED(%r12)
  214. movl $EAGAIN, %edx
  215. jmp 9b
  216. 10: movl PSHARED(%r12), %esi
  217. #if MUTEX == 0
  218. movq %r12, %rdi
  219. #else
  220. leaq MUTEX(%r12), %rdi
  221. #endif
  222. callq __lll_unlock_wake
  223. jmp 11b
  224. 12: movl PSHARED(%r12), %esi
  225. #if MUTEX == 0
  226. movq %r12, %rdi
  227. #else
  228. leaq MUTEX(%r12), %rdi
  229. #endif
  230. callq __lll_lock_wait
  231. jmp 13b
  232. 16: movq $-ETIMEDOUT, %rdx
  233. jmp 17b
  234. 19: movl $EINVAL, %edx
  235. jmp 9b
  236. cfi_endproc
  237. .size pthread_rwlock_timedrdlock,.-pthread_rwlock_timedrdlock