pthread_rwlock_rdlock.S 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  1. /* Copyright (C) 2003 Free Software Foundation, Inc.
  2. This file is part of the GNU C Library.
  3. The GNU C Library is free software; you can redistribute it and/or
  4. modify it under the terms of the GNU Lesser General Public
  5. License as published by the Free Software Foundation; either
  6. version 2.1 of the License, or (at your option) any later version.
  7. The GNU C Library is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  10. Lesser General Public License for more details.
  11. You should have received a copy of the GNU Lesser General Public
  12. License along with the GNU C Library; if not, write to the Free
  13. Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
  14. 02111-1307 USA. */
  15. #include <sysdep.h>
  16. #include <lowlevelrwlock.h>
  17. #include <pthread-errnos.h>
  18. #include <tcb-offsets.h>
  19. #include "lowlevel-atomic.h"
  20. #define FUTEX_WAIT 0
  21. #define FUTEX_WAKE 1
  22. .text
  23. .globl __pthread_rwlock_rdlock
  24. .type __pthread_rwlock_rdlock,@function
  25. .align 5
  26. __pthread_rwlock_rdlock:
  27. mov.l r12, @-r15
  28. mov.l r9, @-r15
  29. mov.l r8, @-r15
  30. sts.l pr, @-r15
  31. mov r4, r8
  32. /* Get the lock. */
  33. mov #0, r3
  34. mov #1, r4
  35. #if MUTEX == 0
  36. CMPXCHG (r3, @r8, r4, r2)
  37. #else
  38. CMPXCHG (r3, @(MUTEX,r8), r4, r2)
  39. #endif
  40. bf 1f
  41. 2:
  42. mov.l @(WRITER,r8), r0
  43. tst r0, r0
  44. bf 14f
  45. mov.l @(WRITERS_QUEUED,r8), r0
  46. tst r0, r0
  47. bt 5f
  48. mov.l @(FLAGS,r8), r0
  49. tst r0, r0
  50. bt 5f
  51. 3:
  52. mov.l @(READERS_QUEUED,r8), r0
  53. add #1, r0
  54. mov.l r0, @(READERS_QUEUED,r8)
  55. tst r0, r0
  56. bt 4f
  57. mov.l @(READERS_WAKEUP,r8), r9
  58. #if MUTEX == 0
  59. DEC (@r8, r2)
  60. #else
  61. DEC (@(MUTEX,r8), r2)
  62. #endif
  63. tst r2, r2
  64. bf 10f
  65. 11:
  66. mov r8, r4
  67. add #READERS_WAKEUP, r4
  68. mov #FUTEX_WAIT, r5
  69. mov r9, r6
  70. mov #0, r7
  71. mov #SYS_futex, r3
  72. extu.b r3, r3
  73. trapa #0x14
  74. SYSCALL_INST_PAD
  75. /* Reget the lock. */
  76. mov #0, r3
  77. mov #1, r4
  78. #if MUTEX == 0
  79. CMPXCHG (r3, @r8, r4, r2)
  80. #else
  81. CMPXCHG (r3, @(MUTEX,r8), r4, r2)
  82. #endif
  83. bf 12f
  84. 13:
  85. mov.l @(READERS_QUEUED,r8), r0
  86. add #-1, r0
  87. bra 2b
  88. mov.l r0, @(READERS_QUEUED,r8)
  89. 5:
  90. mov #0, r3
  91. mov.l @(NR_READERS,r8), r0
  92. add #1, r0
  93. mov.l r0, @(NR_READERS,r8)
  94. tst r0, r0
  95. bt 8f
  96. 9:
  97. #if MUTEX == 0
  98. DEC (@r8, r2)
  99. #else
  100. DEC (@(MUTEX,r8), r2)
  101. #endif
  102. tst r2, r2
  103. bf 6f
  104. 7:
  105. lds.l @r15+, pr
  106. mov.l @r15+, r8
  107. mov.l @r15+, r9
  108. mov.l @r15+, r12
  109. rts
  110. mov r3, r0
  111. 1:
  112. mov r8, r5
  113. #if MUTEX != 0
  114. add #MUTEX, r5
  115. #endif
  116. mov r2, r4
  117. mov.l .Lwait0, r1
  118. bsrf r1
  119. nop
  120. .Lwait0b:
  121. bra 2b
  122. nop
  123. 14:
  124. stc gbr, r1
  125. mov.w .Ltidoff, r2
  126. add r2, r1
  127. mov.l @r1, r1
  128. cmp/eq r1, r0
  129. bf 3b
  130. /* Deadlock detected. */
  131. bra 9b
  132. mov #EDEADLK, r3
  133. .Ltidoff:
  134. .word TID - TLS_PRE_TCB_SIZE
  135. 6:
  136. mov r8, r4
  137. #if MUTEX != 0
  138. add #MUTEX, r4
  139. #endif
  140. mov.l .Lwake0, r1
  141. bsrf r1
  142. nop
  143. .Lwake0b:
  144. bra 7b
  145. mov #0, r3
  146. 8:
  147. /* Overflow. */
  148. mov.l @(NR_READERS,r8), r1
  149. add #-1, r1
  150. mov.l r1, @(NR_READERS,r8)
  151. bra 9b
  152. mov #EAGAIN, r3
  153. 4:
  154. /* Overflow. */
  155. mov.l @(READERS_QUEUED,r8), r1
  156. add #-1, r1
  157. mov.l r1, @(READERS_QUEUED,r8)
  158. bra 9b
  159. mov #EAGAIN, r3
  160. 10:
  161. mov r8, r4
  162. #if MUTEX != 0
  163. add #MUTEX, r4
  164. #endif
  165. mov.l .Lwake1, r1
  166. bsrf r1
  167. nop
  168. .Lwake1b:
  169. bra 11b
  170. nop
  171. 12:
  172. mov r8, r5
  173. #if MUTEX != 0
  174. add #MUTEX, r5
  175. #endif
  176. mov r2, r4
  177. mov.l .Lwait1, r1
  178. bsrf r1
  179. nop
  180. .Lwait1b:
  181. bra 13b
  182. nop
  183. .align 2
  184. .Lwait0:
  185. .long __lll_mutex_lock_wait-.Lwait0b
  186. .Lwake0:
  187. .long __lll_mutex_unlock_wake-.Lwake0b
  188. .Lwait1:
  189. .long __lll_mutex_lock_wait-.Lwait1b
  190. .Lwake1:
  191. .long __lll_mutex_unlock_wake-.Lwake1b
  192. .size __pthread_rwlock_rdlock,.-__pthread_rwlock_rdlock
  193. .globl pthread_rwlock_rdlock
  194. pthread_rwlock_rdlock = __pthread_rwlock_rdlock
  195. .globl __pthread_rwlock_rdlock_internal
  196. __pthread_rwlock_rdlock_internal = __pthread_rwlock_rdlock