pthread_once.S 4.2 KB

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