pthread_once.S 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  1. /* Copyright (C) 2003, 2004 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 "lowlevel-atomic.h"
  18. #define FUTEX_WAIT 0
  19. #define FUTEX_WAKE 1
  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. mov #FUTEX_WAIT, r5
  82. mov r3, r6
  83. mov #0, r7
  84. mov #SYS_futex, r3
  85. extu.b r3, r3
  86. trapa #0x14
  87. SYSCALL_INST_PAD
  88. bra 6b
  89. nop
  90. .align 2
  91. .Lgot:
  92. .long _GLOBAL_OFFSET_TABLE_
  93. #ifdef __PIC__
  94. .Lfgen:
  95. .long __fork_generation@GOTOFF
  96. #else
  97. .Lfgen:
  98. .long __fork_generation
  99. #endif
  100. 3:
  101. /* Call the initializer function after setting up the
  102. cancellation handler. Note that it is not possible here
  103. to use the unwind-based cleanup handling. This would require
  104. that the user-provided function and all the code it calls
  105. is compiled with exceptions. Unfortunately this cannot be
  106. guaranteed. */
  107. add #-UNWINDBUFSIZE, r15
  108. cfi_adjust_cfa_offset (UNWINDBUFSIZE)
  109. mov.l .Lsigsetjmp, r1
  110. mov #UWJMPBUF, r4
  111. add r15, r4
  112. bsrf r1
  113. mov #0, r5
  114. .Lsigsetjmp0:
  115. tst r0, r0
  116. bf 7f
  117. mov.l .Lcpush, r1
  118. bsrf r1
  119. mov r15, r4
  120. .Lcpush0:
  121. /* Call the user-provided initialization function. */
  122. jsr @r8
  123. nop
  124. /* Pop the cleanup handler. */
  125. mov.l .Lcpop, r1
  126. bsrf r1
  127. mov r15, r4
  128. .Lcpop0:
  129. add #UNWINDBUFSIZE, r15
  130. cfi_adjust_cfa_offset (-UNWINDBUFSIZE)
  131. /* Sucessful run of the initializer. Signal that we are done. */
  132. INC (@r9, r2)
  133. /* Wake up all other threads. */
  134. mov r9, r4
  135. mov #FUTEX_WAKE, r5
  136. mov #-1, r6
  137. shlr r6 /* r6 = 0x7fffffff */
  138. mov #0, r7
  139. mov #SYS_futex, r3
  140. extu.b r3, r3
  141. trapa #0x14
  142. SYSCALL_INST_PAD
  143. 4:
  144. lds.l @r15+, pr
  145. cfi_adjust_cfa_offset (-4)
  146. cfi_restore (pr)
  147. mov.l @r15+, r8
  148. cfi_adjust_cfa_offset (-4)
  149. cfi_restore (r8)
  150. mov.l @r15+, r9
  151. cfi_adjust_cfa_offset (-4)
  152. cfi_restore (r9)
  153. mov.l @r15+, r12
  154. cfi_adjust_cfa_offset (-4)
  155. cfi_restore (r12)
  156. rts
  157. mov #0, r0
  158. 7:
  159. /* __sigsetjmp returned for the second time. */
  160. cfi_adjust_cfa_offset (UNWINDBUFSIZE+16)
  161. cfi_offset (r12, -4)
  162. cfi_offset (r9, -8)
  163. cfi_offset (r8, -12)
  164. cfi_offset (pr, -16)
  165. mov #0, r7
  166. mov.l r7, @r9
  167. mov r9, r4
  168. mov #FUTEX_WAKE, r5
  169. mov #-1, r6
  170. shlr r6 /* r6 = 0x7fffffff */
  171. mov #SYS_futex, r3
  172. extu.b r3, r3
  173. trapa #0x14
  174. SYSCALL_INST_PAD
  175. mov.l .Lunext, r1
  176. bsrf r1
  177. mov r15, r4
  178. .Lunext0:
  179. /* NOTREACHED */
  180. sleep
  181. cfi_endproc
  182. .align 2
  183. .Lsigsetjmp:
  184. .long __sigsetjmp@PLT-(.Lsigsetjmp0-.)
  185. .Lcpush:
  186. .long HIDDEN_JUMPTARGET(__pthread_register_cancel)-.Lcpush0
  187. .Lcpop:
  188. .long HIDDEN_JUMPTARGET(__pthread_unregister_cancel)-.Lcpop0
  189. .Lunext:
  190. .long HIDDEN_JUMPTARGET(__pthread_unwind_next)-.Lunext0
  191. .size __pthread_once,.-__pthread_once
  192. .globl __pthread_once_internal
  193. __pthread_once_internal = __pthread_once
  194. .globl pthread_once
  195. pthread_once = __pthread_once
  196. .type clear_once_control,@function
  197. .align 5
  198. clear_once_control:
  199. mov #0, r0
  200. mov.l r0, @r4
  201. mov #FUTEX_WAKE, r5
  202. mov #-1, r6
  203. shlr r6 /* r6 = 0x7fffffff */
  204. mov #0, r7
  205. mov #SYS_futex, r3
  206. extu.b r3, r3
  207. trapa #0x14
  208. SYSCALL_INST_PAD
  209. rts
  210. nop
  211. .size clear_once_control,.-clear_once_control