pthread_once.S 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. /* Copyright (C) 2002, 2003, 2005, 2007, 2009 Free Software Foundation, Inc.
  2. This file is part of the GNU C Library.
  3. Contributed by Ulrich Drepper <drepper@redhat.com>, 2002.
  4. The GNU C Library is free software; you can redistribute it and/or
  5. modify it under the terms of the GNU Lesser General Public
  6. License as published by the Free Software Foundation; either
  7. version 2.1 of the License, or (at your option) any later version.
  8. The GNU C Library is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. Lesser General Public License for more details.
  12. You should have received a copy of the GNU Lesser General Public
  13. License along with the GNU C Library; if not, write to the Free
  14. Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
  15. 02111-1307 USA. */
  16. #include <sysdep.h>
  17. #include <bits/kernel-features.h>
  18. #include <tcb-offsets.h>
  19. #include <lowlevellock.h>
  20. .comm __fork_generation, 4, 4
  21. .text
  22. .globl __pthread_once
  23. .type __pthread_once,@function
  24. .align 16
  25. __pthread_once:
  26. .LSTARTCODE:
  27. cfi_startproc
  28. #ifdef SHARED
  29. cfi_personality(DW_EH_PE_pcrel | DW_EH_PE_sdata4 | DW_EH_PE_indirect,
  30. DW.ref.__gcc_personality_v0)
  31. cfi_lsda(DW_EH_PE_pcrel | DW_EH_PE_sdata4, .LexceptSTART)
  32. #else
  33. cfi_personality(DW_EH_PE_udata4, __gcc_personality_v0)
  34. cfi_lsda(DW_EH_PE_udata4, .LexceptSTART)
  35. #endif
  36. testl $2, (%rdi)
  37. jz 1f
  38. xorl %eax, %eax
  39. retq
  40. /* Preserve the function pointer. */
  41. 1: pushq %rsi
  42. cfi_adjust_cfa_offset(8)
  43. xorq %r10, %r10
  44. /* Not yet initialized or initialization in progress.
  45. Get the fork generation counter now. */
  46. 6: movl (%rdi), %eax
  47. 5: movl %eax, %edx
  48. testl $2, %eax
  49. jnz 4f
  50. andl $3, %edx
  51. orl __fork_generation(%rip), %edx
  52. orl $1, %edx
  53. LOCK
  54. cmpxchgl %edx, (%rdi)
  55. jnz 5b
  56. /* Check whether another thread already runs the initializer. */
  57. testl $1, %eax
  58. jz 3f /* No -> do it. */
  59. /* Check whether the initializer execution was interrupted
  60. by a fork. */
  61. xorl %edx, %eax
  62. testl $0xfffffffc, %eax
  63. jnz 3f /* Different for generation -> run initializer. */
  64. /* Somebody else got here first. Wait. */
  65. #ifdef __ASSUME_PRIVATE_FUTEX
  66. movl $FUTEX_WAIT|FUTEX_PRIVATE_FLAG, %esi
  67. #else
  68. # if FUTEX_WAIT == 0
  69. movl %fs:PRIVATE_FUTEX, %esi
  70. # else
  71. movl $FUTEX_WAIT, %esi
  72. orl %fs:PRIVATE_FUTEX, %esi
  73. # endif
  74. #endif
  75. movl $SYS_futex, %eax
  76. syscall
  77. jmp 6b
  78. /* Preserve the pointer to the control variable. */
  79. 3: pushq %rdi
  80. cfi_adjust_cfa_offset(8)
  81. pushq %rdi
  82. cfi_adjust_cfa_offset(8)
  83. .LcleanupSTART:
  84. callq *16(%rsp)
  85. .LcleanupEND:
  86. /* Get the control variable address back. */
  87. popq %rdi
  88. cfi_adjust_cfa_offset(-8)
  89. /* Sucessful run of the initializer. Signal that we are done. */
  90. LOCK
  91. incl (%rdi)
  92. addq $8, %rsp
  93. cfi_adjust_cfa_offset(-8)
  94. /* Wake up all other threads. */
  95. movl $0x7fffffff, %edx
  96. #ifdef __ASSUME_PRIVATE_FUTEX
  97. movl $FUTEX_WAKE|FUTEX_PRIVATE_FLAG, %esi
  98. #else
  99. movl $FUTEX_WAKE, %esi
  100. orl %fs:PRIVATE_FUTEX, %esi
  101. #endif
  102. movl $SYS_futex, %eax
  103. syscall
  104. 4: addq $8, %rsp
  105. cfi_adjust_cfa_offset(-8)
  106. xorl %eax, %eax
  107. retq
  108. .size __pthread_once,.-__pthread_once
  109. .globl __pthread_once_internal
  110. __pthread_once_internal = __pthread_once
  111. .globl pthread_once
  112. pthread_once = __pthread_once
  113. .type clear_once_control,@function
  114. .align 16
  115. clear_once_control:
  116. cfi_adjust_cfa_offset(3 * 8)
  117. movq (%rsp), %rdi
  118. movq %rax, %r8
  119. movl $0, (%rdi)
  120. movl $0x7fffffff, %edx
  121. #ifdef __ASSUME_PRIVATE_FUTEX
  122. movl $FUTEX_WAKE|FUTEX_PRIVATE_FLAG, %esi
  123. #else
  124. movl $FUTEX_WAKE, %esi
  125. orl %fs:PRIVATE_FUTEX, %esi
  126. #endif
  127. movl $SYS_futex, %eax
  128. syscall
  129. movq %r8, %rdi
  130. .LcallUR:
  131. call _Unwind_Resume@PLT
  132. hlt
  133. .LENDCODE:
  134. cfi_endproc
  135. .size clear_once_control,.-clear_once_control
  136. .section .gcc_except_table,"a",@progbits
  137. .LexceptSTART:
  138. .byte DW_EH_PE_omit # @LPStart format
  139. .byte DW_EH_PE_omit # @TType format
  140. .byte DW_EH_PE_uleb128 # call-site format
  141. .uleb128 .Lcstend-.Lcstbegin
  142. .Lcstbegin:
  143. .uleb128 .LcleanupSTART-.LSTARTCODE
  144. .uleb128 .LcleanupEND-.LcleanupSTART
  145. .uleb128 clear_once_control-.LSTARTCODE
  146. .uleb128 0
  147. .uleb128 .LcallUR-.LSTARTCODE
  148. .uleb128 .LENDCODE-.LcallUR
  149. .uleb128 0
  150. .uleb128 0
  151. .Lcstend:
  152. #ifdef SHARED
  153. .hidden DW.ref.__gcc_personality_v0
  154. .weak DW.ref.__gcc_personality_v0
  155. .section .gnu.linkonce.d.DW.ref.__gcc_personality_v0,"aw",@progbits
  156. .align 8
  157. .type DW.ref.__gcc_personality_v0, @object
  158. .size DW.ref.__gcc_personality_v0, 8
  159. DW.ref.__gcc_personality_v0:
  160. .quad __gcc_personality_v0
  161. #endif