pthread_once.S 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  1. /* Copyright (C) 2003, 2004, 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, write to the Free
  13. Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
  14. 02111-1307 USA. */
  15. #include <unwindbuf.h>
  16. #include <sysdep.h>
  17. #include <bits/kernel-features.h>
  18. #include <lowlevellock.h>
  19. #include "lowlevel-atomic.h"
  20. .comm __fork_generation, 4, 4
  21. .text
  22. .globl __pthread_once
  23. .type __pthread_once,@function
  24. .align 5
  25. cfi_startproc
  26. __pthread_once:
  27. mov.l @r4, r0
  28. tst #2, r0
  29. bt 1f
  30. rts
  31. mov #0, r0
  32. 1:
  33. mov.l r12, @-r15
  34. cfi_adjust_cfa_offset (4)
  35. cfi_rel_offset (r12, 0)
  36. mov.l r9, @-r15
  37. cfi_adjust_cfa_offset (4)
  38. cfi_rel_offset (r9, 0)
  39. mov.l r8, @-r15
  40. cfi_adjust_cfa_offset (4)
  41. cfi_rel_offset (r8, 0)
  42. sts.l pr, @-r15
  43. cfi_adjust_cfa_offset (4)
  44. cfi_rel_offset (pr, 0)
  45. mov r5, r8
  46. mov r4, r9
  47. /* Not yet initialized or initialization in progress.
  48. Get the fork generation counter now. */
  49. 6:
  50. mov.l @r4, r1
  51. mova .Lgot, r0
  52. mov.l .Lgot, r12
  53. add r0, r12
  54. 5:
  55. mov r1, r0
  56. tst #2, r0
  57. bf 4f
  58. and #3, r0
  59. mov.l .Lfgen, r2
  60. #ifdef __PIC__
  61. add r12, r2
  62. #endif
  63. mov.l @r2, r3
  64. or r3, r0
  65. or #1, r0
  66. mov r0, r3
  67. mov r1, r5
  68. CMPXCHG (r5, @r4, r3, r2)
  69. bf 5b
  70. /* Check whether another thread already runs the initializer. */
  71. mov r2, r0
  72. tst #1, r0
  73. bt 3f /* No -> do it. */
  74. /* Check whether the initializer execution was interrupted
  75. by a fork. */
  76. xor r3, r0
  77. mov #-4, r1 /* -4 = 0xfffffffc */
  78. tst r1, r0
  79. bf 3f /* Different for generation -> run initializer. */
  80. /* Somebody else got here first. Wait. */
  81. #ifdef __ASSUME_PRIVATE_FUTEX
  82. mov #(FUTEX_PRIVATE_FLAG|FUTEX_WAIT), r5
  83. extu.b r5, r5
  84. #else
  85. stc gbr, r1
  86. mov.w .Lpfoff, r2
  87. add r2, r1
  88. mov.l @r1, r5
  89. # if FUTEX_WAIT != 0
  90. mov #FUTEX_WAIT, r0
  91. or r0, r5
  92. # endif
  93. #endif
  94. mov r3, r6
  95. mov #0, r7
  96. mov #SYS_futex, r3
  97. extu.b r3, r3
  98. trapa #0x14
  99. SYSCALL_INST_PAD
  100. bra 6b
  101. nop
  102. .align 2
  103. .Lgot:
  104. .long _GLOBAL_OFFSET_TABLE_
  105. #ifdef __PIC__
  106. .Lfgen:
  107. .long __fork_generation@GOTOFF
  108. #else
  109. .Lfgen:
  110. .long __fork_generation
  111. #endif
  112. 3:
  113. /* Call the initializer function after setting up the
  114. cancellation handler. Note that it is not possible here
  115. to use the unwind-based cleanup handling. This would require
  116. that the user-provided function and all the code it calls
  117. is compiled with exceptions. Unfortunately this cannot be
  118. guaranteed. */
  119. add #-UNWINDBUFSIZE, r15
  120. cfi_adjust_cfa_offset (UNWINDBUFSIZE)
  121. mov.l .Lsigsetjmp, r1
  122. mov #UWJMPBUF, r4
  123. add r15, r4
  124. bsrf r1
  125. mov #0, r5
  126. .Lsigsetjmp0:
  127. tst r0, r0
  128. bf 7f
  129. mov.l .Lcpush, r1
  130. bsrf r1
  131. mov r15, r4
  132. .Lcpush0:
  133. /* Call the user-provided initialization function. */
  134. jsr @r8
  135. nop
  136. /* Pop the cleanup handler. */
  137. mov.l .Lcpop, r1
  138. bsrf r1
  139. mov r15, r4
  140. .Lcpop0:
  141. add #UNWINDBUFSIZE, r15
  142. cfi_adjust_cfa_offset (-UNWINDBUFSIZE)
  143. /* Sucessful run of the initializer. Signal that we are done. */
  144. INC (@r9, r2)
  145. /* Wake up all other threads. */
  146. mov r9, r4
  147. #ifdef __ASSUME_PRIVATE_FUTEX
  148. mov #(FUTEX_PRIVATE_FLAG|FUTEX_WAKE), r5
  149. extu.b r5, r5
  150. #else
  151. stc gbr, r1
  152. mov.w .Lpfoff, r2
  153. add r2, r1
  154. mov.l @r1, r5
  155. mov #FUTEX_WAKE, r0
  156. or r0, r5
  157. #endif
  158. mov #-1, r6
  159. shlr r6 /* r6 = 0x7fffffff */
  160. mov #0, r7
  161. mov #SYS_futex, r3
  162. extu.b r3, r3
  163. trapa #0x14
  164. SYSCALL_INST_PAD
  165. 4:
  166. lds.l @r15+, pr
  167. cfi_adjust_cfa_offset (-4)
  168. cfi_restore (pr)
  169. mov.l @r15+, r8
  170. cfi_adjust_cfa_offset (-4)
  171. cfi_restore (r8)
  172. mov.l @r15+, r9
  173. cfi_adjust_cfa_offset (-4)
  174. cfi_restore (r9)
  175. mov.l @r15+, r12
  176. cfi_adjust_cfa_offset (-4)
  177. cfi_restore (r12)
  178. rts
  179. mov #0, r0
  180. 7:
  181. /* __sigsetjmp returned for the second time. */
  182. cfi_adjust_cfa_offset (UNWINDBUFSIZE+16)
  183. cfi_offset (r12, -4)
  184. cfi_offset (r9, -8)
  185. cfi_offset (r8, -12)
  186. cfi_offset (pr, -16)
  187. mov #0, r7
  188. mov.l r7, @r9
  189. mov r9, r4
  190. #ifdef __ASSUME_PRIVATE_FUTEX
  191. mov #(FUTEX_PRIVATE_FLAG|FUTEX_WAKE), r5
  192. #else
  193. stc gbr, r1
  194. mov.w .Lpfoff, r2
  195. add r2, r1
  196. mov.l @r1, r5
  197. mov #FUTEX_WAKE, r0
  198. or r0, r5
  199. #endif
  200. extu.b r5, r5
  201. mov #-1, r6
  202. shlr r6 /* r6 = 0x7fffffff */
  203. mov #SYS_futex, r3
  204. extu.b r3, r3
  205. trapa #0x14
  206. SYSCALL_INST_PAD
  207. mov.l .Lunext, r1
  208. bsrf r1
  209. mov r15, r4
  210. .Lunext0:
  211. /* NOTREACHED */
  212. sleep
  213. cfi_endproc
  214. #ifndef __ASSUME_PRIVATE_FUTEX
  215. .Lpfoff:
  216. .word PRIVATE_FUTEX - TLS_PRE_TCB_SIZE
  217. #endif
  218. .align 2
  219. .Lsigsetjmp:
  220. .long __sigsetjmp@PLT-(.Lsigsetjmp0-.)
  221. .Lcpush:
  222. .long HIDDEN_JUMPTARGET(__pthread_register_cancel)-.Lcpush0
  223. .Lcpop:
  224. .long HIDDEN_JUMPTARGET(__pthread_unregister_cancel)-.Lcpop0
  225. .Lunext:
  226. .long HIDDEN_JUMPTARGET(__pthread_unwind_next)-.Lunext0
  227. .size __pthread_once,.-__pthread_once
  228. .globl __pthread_once_internal
  229. __pthread_once_internal = __pthread_once
  230. .globl pthread_once
  231. pthread_once = __pthread_once