pthread_once.S 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  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, see
  14. <http://www.gnu.org/licenses/>. */
  15. #include <sysdep.h>
  16. #include <bits/kernel-features.h>
  17. #include <tcb-offsets.h>
  18. #include <lowlevellock.h>
  19. .comm __fork_generation, 4, 4
  20. .text
  21. .globl __pthread_once
  22. .type __pthread_once,@function
  23. .protected __pthread_once
  24. .align 16
  25. __pthread_once:
  26. .LSTARTCODE:
  27. cfi_startproc
  28. testl $2, (%rdi)
  29. jz 1f
  30. xorl %eax, %eax
  31. retq
  32. /* Preserve the function pointer. */
  33. 1: pushq %rsi
  34. cfi_adjust_cfa_offset(8)
  35. xorq %r10, %r10
  36. /* Not yet initialized or initialization in progress.
  37. Get the fork generation counter now. */
  38. 6: movl (%rdi), %eax
  39. 5: movl %eax, %edx
  40. testl $2, %eax
  41. jnz 4f
  42. andl $3, %edx
  43. orl __fork_generation(%rip), %edx
  44. orl $1, %edx
  45. LOCK
  46. cmpxchgl %edx, (%rdi)
  47. jnz 5b
  48. /* Check whether another thread already runs the initializer. */
  49. testl $1, %eax
  50. jz 3f /* No -> do it. */
  51. /* Check whether the initializer execution was interrupted
  52. by a fork. */
  53. xorl %edx, %eax
  54. testl $0xfffffffc, %eax
  55. jnz 3f /* Different for generation -> run initializer. */
  56. /* Somebody else got here first. Wait. */
  57. #ifdef __ASSUME_PRIVATE_FUTEX
  58. movl $FUTEX_WAIT|FUTEX_PRIVATE_FLAG, %esi
  59. #else
  60. # if FUTEX_WAIT == 0
  61. movl %fs:PRIVATE_FUTEX, %esi
  62. # else
  63. movl $FUTEX_WAIT, %esi
  64. orl %fs:PRIVATE_FUTEX, %esi
  65. # endif
  66. #endif
  67. movl $SYS_futex, %eax
  68. syscall
  69. jmp 6b
  70. /* Preserve the pointer to the control variable. */
  71. 3: pushq %rdi
  72. cfi_adjust_cfa_offset(8)
  73. pushq %rdi
  74. cfi_adjust_cfa_offset(8)
  75. .LcleanupSTART:
  76. callq *16(%rsp)
  77. .LcleanupEND:
  78. /* Get the control variable address back. */
  79. popq %rdi
  80. cfi_adjust_cfa_offset(-8)
  81. /* Sucessful run of the initializer. Signal that we are done. */
  82. LOCK
  83. incl (%rdi)
  84. addq $8, %rsp
  85. cfi_adjust_cfa_offset(-8)
  86. /* Wake up all other threads. */
  87. movl $0x7fffffff, %edx
  88. #ifdef __ASSUME_PRIVATE_FUTEX
  89. movl $FUTEX_WAKE|FUTEX_PRIVATE_FLAG, %esi
  90. #else
  91. movl $FUTEX_WAKE, %esi
  92. orl %fs:PRIVATE_FUTEX, %esi
  93. #endif
  94. movl $SYS_futex, %eax
  95. syscall
  96. 4: addq $8, %rsp
  97. cfi_adjust_cfa_offset(-8)
  98. xorl %eax, %eax
  99. retq
  100. .size __pthread_once,.-__pthread_once
  101. .globl __pthread_once_internal
  102. __pthread_once_internal = __pthread_once
  103. .globl pthread_once
  104. pthread_once = __pthread_once
  105. .type clear_once_control,@function
  106. .align 16
  107. clear_once_control:
  108. cfi_adjust_cfa_offset(3 * 8)
  109. movq (%rsp), %rdi
  110. movq %rax, %r8
  111. movl $0, (%rdi)
  112. movl $0x7fffffff, %edx
  113. #ifdef __ASSUME_PRIVATE_FUTEX
  114. movl $FUTEX_WAKE|FUTEX_PRIVATE_FLAG, %esi
  115. #else
  116. movl $FUTEX_WAKE, %esi
  117. orl %fs:PRIVATE_FUTEX, %esi
  118. #endif
  119. movl $SYS_futex, %eax
  120. syscall
  121. movq %r8, %rdi
  122. .LcallUR:
  123. call _Unwind_Resume@PLT
  124. hlt
  125. .LENDCODE:
  126. cfi_endproc
  127. .size clear_once_control,.-clear_once_control
  128. .section .gcc_except_table,"a",@progbits
  129. .LexceptSTART:
  130. .byte DW_EH_PE_omit # @LPStart format
  131. .byte DW_EH_PE_omit # @TType format
  132. .byte DW_EH_PE_uleb128 # call-site format
  133. .uleb128 .Lcstend-.Lcstbegin
  134. .Lcstbegin:
  135. .uleb128 .LcleanupSTART-.LSTARTCODE
  136. .uleb128 .LcleanupEND-.LcleanupSTART
  137. .uleb128 clear_once_control-.LSTARTCODE
  138. .uleb128 0
  139. .uleb128 .LcallUR-.LSTARTCODE
  140. .uleb128 .LENDCODE-.LcallUR
  141. .uleb128 0
  142. .uleb128 0
  143. .Lcstend:
  144. #ifdef SHARED
  145. .hidden DW.ref.__gcc_personality_v0
  146. .weak DW.ref.__gcc_personality_v0
  147. .section .gnu.linkonce.d.DW.ref.__gcc_personality_v0,"aw",@progbits
  148. .align 8
  149. .type DW.ref.__gcc_personality_v0, @object
  150. .size DW.ref.__gcc_personality_v0, 8
  151. DW.ref.__gcc_personality_v0:
  152. .quad __gcc_personality_v0
  153. #endif