pthread_rwlock_rdlock.S 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. /* Copyright (C) 2002, 2003, 2007 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 <tls.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. pushl %esi
  29. cfi_adjust_cfa_offset(4)
  30. pushl %ebx
  31. cfi_adjust_cfa_offset(4)
  32. cfi_offset(%esi, -8)
  33. cfi_offset(%ebx, -12)
  34. xorl %esi, %esi
  35. movl 12(%esp), %ebx
  36. /* Get the lock. */
  37. movl $1, %edx
  38. xorl %eax, %eax
  39. LOCK
  40. #if MUTEX == 0
  41. cmpxchgl %edx, (%ebx)
  42. #else
  43. cmpxchgl %edx, MUTEX(%ebx)
  44. #endif
  45. jnz 1f
  46. 2: movl WRITER(%ebx), %eax
  47. testl %eax, %eax
  48. jne 14f
  49. cmpl $0, WRITERS_QUEUED(%ebx)
  50. je 5f
  51. cmpb $0, FLAGS(%ebx)
  52. je 5f
  53. 3: addl $1, READERS_QUEUED(%ebx)
  54. je 4f
  55. movl READERS_WAKEUP(%ebx), %edx
  56. LOCK
  57. #if MUTEX == 0
  58. subl $1, (%ebx)
  59. #else
  60. subl $1, MUTEX(%ebx)
  61. #endif
  62. jne 10f
  63. 11:
  64. #ifdef __ASSUME_PRIVATE_FUTEX
  65. movzbl PSHARED(%ebx), %ecx
  66. xorl $FUTEX_PRIVATE_FLAG|FUTEX_WAIT, %ecx
  67. #else
  68. movzbl PSHARED(%ebx), %ecx
  69. # if FUTEX_WAIT != 0
  70. orl $FUTEX_WAIT, %ecx
  71. # endif
  72. xorl %gs:PRIVATE_FUTEX, %ecx
  73. #endif
  74. addl $READERS_WAKEUP, %ebx
  75. movl $SYS_futex, %eax
  76. ENTER_KERNEL
  77. subl $READERS_WAKEUP, %ebx
  78. /* Reget the lock. */
  79. movl $1, %edx
  80. xorl %eax, %eax
  81. LOCK
  82. #if MUTEX == 0
  83. cmpxchgl %edx, (%ebx)
  84. #else
  85. cmpxchgl %edx, MUTEX(%ebx)
  86. #endif
  87. jnz 12f
  88. 13: subl $1, READERS_QUEUED(%ebx)
  89. jmp 2b
  90. 5: xorl %edx, %edx
  91. addl $1, NR_READERS(%ebx)
  92. je 8f
  93. 9: LOCK
  94. #if MUTEX == 0
  95. subl $1, (%ebx)
  96. #else
  97. subl $1, MUTEX(%ebx)
  98. #endif
  99. jne 6f
  100. 7:
  101. movl %edx, %eax
  102. popl %ebx
  103. cfi_adjust_cfa_offset(-4)
  104. cfi_restore(%ebx)
  105. popl %esi
  106. cfi_adjust_cfa_offset(-4)
  107. cfi_restore(%esi)
  108. ret
  109. cfi_adjust_cfa_offset(8)
  110. cfi_offset(%esi, -8)
  111. cfi_offset(%ebx, -12)
  112. 1:
  113. #if MUTEX == 0
  114. movl %ebx, %edx
  115. #else
  116. leal MUTEX(%ebx), %edx
  117. #endif
  118. movzbl PSHARED(%ebx), %ecx
  119. call __lll_lock_wait
  120. jmp 2b
  121. 14: cmpl %gs:TID, %eax
  122. jne 3b
  123. /* Deadlock detected. */
  124. movl $EDEADLK, %edx
  125. jmp 9b
  126. 6:
  127. #if MUTEX == 0
  128. movl %ebx, %eax
  129. #else
  130. leal MUTEX(%ebx), %eax
  131. #endif
  132. movzbl PSHARED(%ebx), %ecx
  133. call __lll_unlock_wake
  134. jmp 7b
  135. /* Overflow. */
  136. 8: subl $1, NR_READERS(%ebx)
  137. movl $EAGAIN, %edx
  138. jmp 9b
  139. /* Overflow. */
  140. 4: subl $1, READERS_QUEUED(%ebx)
  141. movl $EAGAIN, %edx
  142. jmp 9b
  143. 10:
  144. #if MUTEX == 0
  145. movl %ebx, %eax
  146. #else
  147. leal MUTEX(%ebx), %eax
  148. #endif
  149. movzbl PSHARED(%ebx), %ecx
  150. call __lll_unlock_wake
  151. jmp 11b
  152. 12:
  153. #if MUTEX == 0
  154. movl %ebx, %edx
  155. #else
  156. leal MUTEX(%ebx), %edx
  157. #endif
  158. movzbl PSHARED(%ebx), %ecx
  159. call __lll_lock_wait
  160. jmp 13b
  161. cfi_endproc
  162. .size __pthread_rwlock_rdlock,.-__pthread_rwlock_rdlock
  163. .globl pthread_rwlock_rdlock
  164. pthread_rwlock_rdlock = __pthread_rwlock_rdlock
  165. .globl __pthread_rwlock_rdlock_internal
  166. __pthread_rwlock_rdlock_internal = __pthread_rwlock_rdlock