pthread_rwlock_timedrdlock.S 5.1 KB

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