pthread_rwlock_timedrdlock.S 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  1. /* Copyright (C) 2002, 2003, 2004, 2005 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 <lowlevelrwlock.h>
  18. #include <pthread-errnos.h>
  19. #include <tcb-offsets.h>
  20. #define FUTEX_WAIT 0
  21. #define FUTEX_WAKE 1
  22. /* For the calculation see asm/vsyscall.h. */
  23. #define VSYSCALL_ADDR_vgettimeofday 0xffffffffff600000
  24. #ifndef UP
  25. # define LOCK lock
  26. #else
  27. # define LOCK
  28. #endif
  29. .text
  30. .globl pthread_rwlock_timedrdlock
  31. .type pthread_rwlock_timedrdlock,@function
  32. .align 16
  33. pthread_rwlock_timedrdlock:
  34. pushq %r12
  35. pushq %r13
  36. pushq %r14
  37. subq $16, %rsp
  38. movq %rdi, %r12
  39. movq %rsi, %r13
  40. /* Get the lock. */
  41. movl $1, %esi
  42. xorl %eax, %eax
  43. LOCK
  44. #if MUTEX == 0
  45. cmpxchgl %esi, (%rdi)
  46. #else
  47. cmpxchgl %esi, MUTEX(%rdi)
  48. #endif
  49. jnz 1f
  50. 2: movl WRITER(%r12), %eax
  51. testl %eax, %eax
  52. jne 14f
  53. cmpl $0, WRITERS_QUEUED(%r12)
  54. je 5f
  55. cmpl $0, FLAGS(%r12)
  56. je 5f
  57. /* Check the value of the timeout parameter. */
  58. 3: cmpq $1000000000, 8(%r13)
  59. jae 19f
  60. incl READERS_QUEUED(%r12)
  61. je 4f
  62. movl READERS_WAKEUP(%r12), %r14d
  63. /* Unlock. */
  64. LOCK
  65. #if MUTEX == 0
  66. decl (%r12)
  67. #else
  68. decl MUTEX(%r12)
  69. #endif
  70. jne 10f
  71. /* Get current time. */
  72. 11: movq %rsp, %rdi
  73. xorl %esi, %esi
  74. movq $VSYSCALL_ADDR_vgettimeofday, %rax
  75. callq *%rax
  76. /* Compute relative timeout. */
  77. movq 8(%rsp), %rax
  78. movl $1000, %edi
  79. mul %rdi /* Milli seconds to nano seconds. */
  80. movq (%r13), %rcx
  81. movq 8(%r13), %rdi
  82. subq (%rsp), %rcx
  83. subq %rax, %rdi
  84. jns 15f
  85. addq $1000000000, %rdi
  86. decq %rcx
  87. 15: testq %rcx, %rcx
  88. js 16f /* Time is already up. */
  89. /* Futex call. */
  90. movq %rcx, (%rsp) /* Store relative timeout. */
  91. movq %rdi, 8(%rsp)
  92. #if FUTEX_WAIT == 0
  93. xorl %esi, %esi
  94. #else
  95. movl $FUTEX_WAIT, %esi
  96. #endif
  97. movq %rsp, %r10
  98. movl %r14d, %edx
  99. leaq READERS_WAKEUP(%r12), %rdi
  100. movl $SYS_futex, %eax
  101. syscall
  102. movq %rax, %rdx
  103. 17:
  104. /* Reget the lock. */
  105. movl $1, %esi
  106. xorl %eax, %eax
  107. LOCK
  108. #if MUTEX == 0
  109. cmpxchgl %esi, (%r12)
  110. #else
  111. cmpxchgl %esi, MUTEX(%r12)
  112. #endif
  113. jnz 12f
  114. 13: decl READERS_QUEUED(%r12)
  115. cmpq $-ETIMEDOUT, %rdx
  116. jne 2b
  117. 18: movl $ETIMEDOUT, %edx
  118. jmp 9f
  119. 5: xorl %edx, %edx
  120. incl NR_READERS(%r12)
  121. je 8f
  122. 9: LOCK
  123. #if MUTEX == 0
  124. decl (%r12)
  125. #else
  126. decl MUTEX(%r12)
  127. #endif
  128. jne 6f
  129. 7: movq %rdx, %rax
  130. addq $16, %rsp
  131. popq %r14
  132. popq %r13
  133. popq %r12
  134. retq
  135. 1:
  136. #if MUTEX != 0
  137. addq $MUTEX, %rdi
  138. #endif
  139. callq __lll_mutex_lock_wait
  140. jmp 2b
  141. 14: cmpl %fs:TID, %eax
  142. jne 3b
  143. movl $EDEADLK, %edx
  144. jmp 9b
  145. 6:
  146. #if MUTEX == 0
  147. movq %r12, %rdi
  148. #else
  149. leal MUTEX(%r12), %rdi
  150. #endif
  151. callq __lll_mutex_unlock_wake
  152. jmp 7b
  153. /* Overflow. */
  154. 8: decl NR_READERS(%r12)
  155. movl $EAGAIN, %edx
  156. jmp 9b
  157. /* Overflow. */
  158. 4: decl READERS_QUEUED(%r12)
  159. movl $EAGAIN, %edx
  160. jmp 9b
  161. 10:
  162. #if MUTEX == 0
  163. movq %r12, %rdi
  164. #else
  165. leaq MUTEX(%r12), %rdi
  166. #endif
  167. callq __lll_mutex_unlock_wake
  168. jmp 11b
  169. 12:
  170. #if MUTEX == 0
  171. movq %r12, %rdi
  172. #else
  173. leaq MUTEX(%r12), %rdi
  174. #endif
  175. callq __lll_mutex_lock_wait
  176. jmp 13b
  177. 16: movq $-ETIMEDOUT, %rdx
  178. jmp 17b
  179. 19: movl $EINVAL, %edx
  180. jmp 9b
  181. .size pthread_rwlock_timedrdlock,.-pthread_rwlock_timedrdlock