pthread_rwlock_timedwrlock.S 5.0 KB

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