pthread_rwlock_rdlock.S 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  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. .text
  22. .globl __pthread_rwlock_rdlock
  23. .type __pthread_rwlock_rdlock,@function
  24. .protected __pthread_rwlock_rdlock
  25. .align 16
  26. __pthread_rwlock_rdlock:
  27. cfi_startproc
  28. xorq %r10, %r10
  29. /* Get the lock. */
  30. movl $1, %esi
  31. xorl %eax, %eax
  32. LOCK
  33. #if MUTEX == 0
  34. cmpxchgl %esi, (%rdi)
  35. #else
  36. cmpxchgl %esi, MUTEX(%rdi)
  37. #endif
  38. jnz 1f
  39. 2: movl WRITER(%rdi), %eax
  40. testl %eax, %eax
  41. jne 14f
  42. cmpl $0, WRITERS_QUEUED(%rdi)
  43. je 5f
  44. cmpl $0, FLAGS(%rdi)
  45. je 5f
  46. 3: incl READERS_QUEUED(%rdi)
  47. je 4f
  48. movl READERS_WAKEUP(%rdi), %edx
  49. LOCK
  50. #if MUTEX == 0
  51. decl (%rdi)
  52. #else
  53. decl MUTEX(%rdi)
  54. #endif
  55. jne 10f
  56. 11:
  57. #ifdef __ASSUME_PRIVATE_FUTEX
  58. movl $FUTEX_PRIVATE_FLAG|FUTEX_WAIT, %esi
  59. xorl PSHARED(%rdi), %esi
  60. #else
  61. # if FUTEX_WAIT == 0
  62. movl PSHARED(%rdi), %esi
  63. # else
  64. movl $FUTEX_WAIT, %esi
  65. orl PSHARED(%rdi), %esi
  66. # endif
  67. xorl %fs:PRIVATE_FUTEX, %esi
  68. #endif
  69. addq $READERS_WAKEUP, %rdi
  70. movl $SYS_futex, %eax
  71. syscall
  72. subq $READERS_WAKEUP, %rdi
  73. /* Reget the lock. */
  74. movl $1, %esi
  75. xorl %eax, %eax
  76. LOCK
  77. #if MUTEX == 0
  78. cmpxchgl %esi, (%rdi)
  79. #else
  80. cmpxchgl %esi, MUTEX(%rdi)
  81. #endif
  82. jnz 12f
  83. 13: decl READERS_QUEUED(%rdi)
  84. jmp 2b
  85. 5: xorl %edx, %edx
  86. incl NR_READERS(%rdi)
  87. je 8f
  88. 9: LOCK
  89. #if MUTEX == 0
  90. decl (%rdi)
  91. #else
  92. decl MUTEX(%rdi)
  93. #endif
  94. jne 6f
  95. 7:
  96. movq %rdx, %rax
  97. retq
  98. 1: movl PSHARED(%rdi), %esi
  99. #if MUTEX != 0
  100. addq $MUTEX, %rdi
  101. #endif
  102. callq __lll_lock_wait
  103. #if MUTEX != 0
  104. subq $MUTEX, %rdi
  105. #endif
  106. jmp 2b
  107. 14: cmpl %fs:TID, %eax
  108. jne 3b
  109. /* Deadlock detected. */
  110. movl $EDEADLK, %edx
  111. jmp 9b
  112. 6: movl PSHARED(%rdi), %esi
  113. #if MUTEX != 0
  114. addq $MUTEX, %rdi
  115. #endif
  116. callq __lll_unlock_wake
  117. #if MUTEX != 0
  118. subq $MUTEX, %rdi
  119. #endif
  120. jmp 7b
  121. /* Overflow. */
  122. 8: decl NR_READERS(%rdi)
  123. movl $EAGAIN, %edx
  124. jmp 9b
  125. /* Overflow. */
  126. 4: decl READERS_QUEUED(%rdi)
  127. movl $EAGAIN, %edx
  128. jmp 9b
  129. 10: movl PSHARED(%rdi), %esi
  130. #if MUTEX != 0
  131. addq $MUTEX, %rdi
  132. #endif
  133. callq __lll_unlock_wake
  134. #if MUTEX != 0
  135. subq $MUTEX, %rdi
  136. #endif
  137. jmp 11b
  138. 12: movl PSHARED(%rdi), %esi
  139. #if MUTEX == 0
  140. addq $MUTEX, %rdi
  141. #endif
  142. callq __lll_lock_wait
  143. #if MUTEX != 0
  144. subq $MUTEX, %rdi
  145. #endif
  146. jmp 13b
  147. cfi_endproc
  148. .size __pthread_rwlock_rdlock,.-__pthread_rwlock_rdlock
  149. .globl pthread_rwlock_rdlock
  150. pthread_rwlock_rdlock = __pthread_rwlock_rdlock
  151. .globl __pthread_rwlock_rdlock_internal
  152. __pthread_rwlock_rdlock_internal = __pthread_rwlock_rdlock