pthread_once.S 5.3 KB

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