pthread_rwlock_wrlock.S 3.2 KB

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