pthread_rwlock_wrlock.S 3.1 KB

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