pthread_rwlock_rdlock.S 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  1. /* Copyright (C) 2003, 2007 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, see
  13. <http://www.gnu.org/licenses/>. */
  14. #include <sysdep.h>
  15. #include <lowlevellock.h>
  16. #include <lowlevelrwlock.h>
  17. #include <pthread-errnos.h>
  18. #include <tcb-offsets.h>
  19. #include <bits/kernel-features.h>
  20. #include "lowlevel-atomic.h"
  21. .text
  22. .globl __pthread_rwlock_rdlock
  23. .type __pthread_rwlock_rdlock,@function
  24. .protected __pthread_rwlock_rdlock
  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 #FLAGS, r0
  49. mov.b @(r0,r8), r0
  50. tst r0, r0
  51. bt 5f
  52. 3:
  53. mov.l @(READERS_QUEUED,r8), r0
  54. add #1, r0
  55. mov.l r0, @(READERS_QUEUED,r8)
  56. tst r0, r0
  57. bt 4f
  58. mov.l @(READERS_WAKEUP,r8), r9
  59. #if MUTEX == 0
  60. DEC (@r8, r2)
  61. #else
  62. DEC (@(MUTEX,r8), r2)
  63. #endif
  64. tst r2, r2
  65. bf 10f
  66. 11:
  67. #ifdef __ASSUME_PRIVATE_FUTEX
  68. mov #PSHARED, r0
  69. mov.b @(r0,r8), r5
  70. mov #(FUTEX_PRIVATE_FLAG|FUTEX_WAIT), r0
  71. xor r0, r5
  72. extu.b r5, r5
  73. #else
  74. mov #PSHARED, r0
  75. mov.b @(r0,r8), r5
  76. extu.b r5, r5
  77. # if FUTEX_WAIT != 0
  78. mov #FUTEX_WAIT, r0
  79. or r0, r5
  80. # endif
  81. stc gbr, r1
  82. mov.w .Lpfoff, r2
  83. add r2, r1
  84. mov.l @r1, r0
  85. xor r0, r5
  86. #endif
  87. mov r8, r4
  88. add #READERS_WAKEUP, r4
  89. mov r9, r6
  90. mov #0, r7
  91. mov #SYS_futex, r3
  92. extu.b r3, r3
  93. trapa #0x14
  94. SYSCALL_INST_PAD
  95. /* Reget the lock. */
  96. mov #0, r3
  97. mov #1, r4
  98. #if MUTEX == 0
  99. CMPXCHG (r3, @r8, r4, r2)
  100. #else
  101. CMPXCHG (r3, @(MUTEX,r8), r4, r2)
  102. #endif
  103. bf 12f
  104. 13:
  105. mov.l @(READERS_QUEUED,r8), r0
  106. add #-1, r0
  107. bra 2b
  108. mov.l r0, @(READERS_QUEUED,r8)
  109. 5:
  110. mov #0, r3
  111. mov.l @(NR_READERS,r8), r0
  112. add #1, r0
  113. mov.l r0, @(NR_READERS,r8)
  114. tst r0, r0
  115. bt 8f
  116. 9:
  117. #if MUTEX == 0
  118. DEC (@r8, r2)
  119. #else
  120. DEC (@(MUTEX,r8), r2)
  121. #endif
  122. tst r2, r2
  123. bf 6f
  124. 7:
  125. lds.l @r15+, pr
  126. mov.l @r15+, r8
  127. mov.l @r15+, r9
  128. mov.l @r15+, r12
  129. rts
  130. mov r3, r0
  131. #ifndef __ASSUME_PRIVATE_FUTEX
  132. .Lpfoff:
  133. .word PRIVATE_FUTEX - TLS_PRE_TCB_SIZE
  134. #endif
  135. 1:
  136. mov r8, r5
  137. #if MUTEX != 0
  138. add #MUTEX, r5
  139. #endif
  140. mov #PSHARED, r0
  141. mov.b @(r0,r8), r6
  142. extu.b r6, r6
  143. mov.l .Lwait0, r1
  144. bsrf r1
  145. mov r2, r4
  146. .Lwait0b:
  147. bra 2b
  148. nop
  149. 14:
  150. stc gbr, r1
  151. mov.w .Ltidoff, r2
  152. add r2, r1
  153. mov.l @r1, r1
  154. cmp/eq r1, r0
  155. bf 3b
  156. /* Deadlock detected. */
  157. bra 9b
  158. mov #EDEADLK, r3
  159. .Ltidoff:
  160. .word TID - TLS_PRE_TCB_SIZE
  161. 6:
  162. mov r8, r4
  163. #if MUTEX != 0
  164. add #MUTEX, r4
  165. #endif
  166. mov #PSHARED, r0
  167. mov.b @(r0,r8), r5
  168. extu.b r5, r5
  169. mov.l .Lwake0, r1
  170. bsrf r1
  171. nop
  172. .Lwake0b:
  173. bra 7b
  174. mov #0, r3
  175. 8:
  176. /* Overflow. */
  177. mov.l @(NR_READERS,r8), r1
  178. add #-1, r1
  179. mov.l r1, @(NR_READERS,r8)
  180. bra 9b
  181. mov #EAGAIN, r3
  182. 4:
  183. /* Overflow. */
  184. mov.l @(READERS_QUEUED,r8), r1
  185. add #-1, r1
  186. mov.l r1, @(READERS_QUEUED,r8)
  187. bra 9b
  188. mov #EAGAIN, r3
  189. 10:
  190. mov r8, r4
  191. #if MUTEX != 0
  192. add #MUTEX, r4
  193. #endif
  194. mov #PSHARED, r0
  195. mov.b @(r0,r8), r5
  196. extu.b r5, r5
  197. mov.l .Lwake1, r1
  198. bsrf r1
  199. nop
  200. .Lwake1b:
  201. bra 11b
  202. nop
  203. 12:
  204. mov r8, r5
  205. #if MUTEX != 0
  206. add #MUTEX, r5
  207. #endif
  208. mov #PSHARED, r0
  209. mov.b @(r0,r8), r6
  210. extu.b r6, r6
  211. mov.l .Lwait1, r1
  212. bsrf r1
  213. mov r2, r4
  214. .Lwait1b:
  215. bra 13b
  216. nop
  217. .align 2
  218. .Lwait0:
  219. .long __lll_lock_wait-.Lwait0b
  220. .Lwake0:
  221. .long __lll_unlock_wake-.Lwake0b
  222. .Lwait1:
  223. .long __lll_lock_wait-.Lwait1b
  224. .Lwake1:
  225. .long __lll_unlock_wake-.Lwake1b
  226. .size __pthread_rwlock_rdlock,.-__pthread_rwlock_rdlock
  227. .globl pthread_rwlock_rdlock
  228. pthread_rwlock_rdlock = __pthread_rwlock_rdlock
  229. .globl __pthread_rwlock_rdlock_internal
  230. __pthread_rwlock_rdlock_internal = __pthread_rwlock_rdlock