pthread_rwlock_wrlock.S 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  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_wrlock
  23. .type __pthread_rwlock_wrlock,@function
  24. .protected __pthread_rwlock_wrlock
  25. .align 5
  26. __pthread_rwlock_wrlock:
  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 @(NR_READERS,r8), r0
  46. tst r0, r0
  47. bt 5f
  48. 3:
  49. mov.l @(WRITERS_QUEUED,r8), r0
  50. add #1, r0
  51. mov.l r0, @(WRITERS_QUEUED,r8)
  52. tst r0, r0
  53. bt 4f
  54. mov.l @(WRITERS_WAKEUP,r8), r9
  55. #if MUTEX == 0
  56. DEC (@r8, r2)
  57. #else
  58. DEC (@(MUTEX,r8), r2)
  59. #endif
  60. tst r2, r2
  61. bf 10f
  62. 11:
  63. mov r8, r4
  64. add #WRITERS_WAKEUP, r4
  65. #ifdef __ASSUME_PRIVATE_FUTEX
  66. mov #PSHARED, r0
  67. mov.b @(r0,r8), r5
  68. mov #(FUTEX_PRIVATE_FLAG|FUTEX_WAIT), r0
  69. xor r0, r5
  70. extu.b r5, r5
  71. #else
  72. mov #PSHARED, r0
  73. mov.b @(r0,r8), r5
  74. extu.b r5, r5
  75. # if FUTEX_WAIT != 0
  76. mov #FUTEX_WAIT, r0
  77. or r0, r5
  78. # endif
  79. stc gbr, r1
  80. mov.w .Lpfoff, r2
  81. add r2, r1
  82. mov.l @r1, r0
  83. xor r0, r5
  84. #endif
  85. mov r9, r6
  86. mov #0, r7
  87. mov #SYS_futex, r3
  88. extu.b r3, r3
  89. trapa #0x14
  90. SYSCALL_INST_PAD
  91. /* Reget the lock. */
  92. mov #0, r3
  93. mov #1, r4
  94. #if MUTEX == 0
  95. CMPXCHG (r3, @r8, r4, r2)
  96. #else
  97. CMPXCHG (r3, @(MUTEX,r8), r4, r2)
  98. #endif
  99. bf 12f
  100. 13:
  101. mov.l @(WRITERS_QUEUED,r8), r0
  102. add #-1, r0
  103. bra 2b
  104. mov.l r0, @(WRITERS_QUEUED,r8)
  105. 5:
  106. mov #0, r3
  107. stc gbr, r0
  108. mov.w .Ltidoff, r1
  109. mov.l @(r0,r1), r0
  110. mov.l r0, @(WRITER,r8)
  111. 9:
  112. #if MUTEX == 0
  113. DEC (@r8, r2)
  114. #else
  115. DEC (@(MUTEX,r8), r2)
  116. #endif
  117. tst r2, r2
  118. bf 6f
  119. 7:
  120. lds.l @r15+, pr
  121. mov.l @r15+, r8
  122. mov.l @r15+, r9
  123. mov.l @r15+, r12
  124. rts
  125. mov r3, r0
  126. 1:
  127. mov r8, r5
  128. #if MUTEX != 0
  129. add #MUTEX, r5
  130. #endif
  131. mov #PSHARED, r0
  132. mov.b @(r0,r8), r6
  133. extu.b r6, r6
  134. mov.l .Lwait4, r1
  135. bsrf r1
  136. mov r2, r4
  137. .Lwait4b:
  138. bra 2b
  139. nop
  140. 14:
  141. stc gbr, r1
  142. mov.w .Ltidoff, r2
  143. add r2, r1
  144. mov.l @r1, r1
  145. cmp/eq r1, r0
  146. bf 3b
  147. bra 9b
  148. mov #EDEADLK, r3
  149. 6:
  150. mov r8, r4
  151. #if MUTEX != 0
  152. add #MUTEX, r4
  153. #endif
  154. mov #PSHARED, r0
  155. mov.b @(r0,r8), r5
  156. extu.b r5, r5
  157. mov.l .Lwake4, r1
  158. bsrf r1
  159. nop
  160. .Lwake4b:
  161. bra 7b
  162. mov #0, r3
  163. #ifndef __ASSUME_PRIVATE_FUTEX
  164. .Lpfoff:
  165. .word PRIVATE_FUTEX - TLS_PRE_TCB_SIZE
  166. #endif
  167. .Ltidoff:
  168. .word TID - TLS_PRE_TCB_SIZE
  169. 4:
  170. mov.l @(WRITERS_QUEUED,r8), r1
  171. add #-1, r1
  172. mov.l r1, @(WRITERS_QUEUED,r8)
  173. bra 9b
  174. mov #EAGAIN, r3
  175. 10:
  176. mov r8, r4
  177. #if MUTEX != 0
  178. add #MUTEX, r4
  179. #endif
  180. mov #PSHARED, r0
  181. mov.b @(r0,r8), r5
  182. extu.b r5, r5
  183. mov.l .Lwake5, r1
  184. bsrf r1
  185. nop
  186. .Lwake5b:
  187. bra 11b
  188. nop
  189. 12:
  190. mov r8, r5
  191. #if MUTEX != 0
  192. add #MUTEX, r5
  193. #endif
  194. mov #PSHARED, r0
  195. mov.b @(r0,r8), r6
  196. extu.b r6, r6
  197. mov.l .Lwait5, r1
  198. bsrf r1
  199. mov r2, r4
  200. .Lwait5b:
  201. bra 13b
  202. nop
  203. .align 2
  204. .Lwait4:
  205. .long __lll_lock_wait-.Lwait4b
  206. .Lwake4:
  207. .long __lll_unlock_wake-.Lwake4b
  208. .Lwait5:
  209. .long __lll_lock_wait-.Lwait5b
  210. .Lwake5:
  211. .long __lll_unlock_wake-.Lwake5b
  212. .globl pthread_rwlock_wrlock
  213. pthread_rwlock_wrlock = __pthread_rwlock_wrlock
  214. .globl __pthread_rwlock_wrlock_internal
  215. __pthread_rwlock_wrlock_internal = __pthread_rwlock_wrlock