pthread_once.S 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. /* Copyright (C) 2002, 2003, 2007 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 <unwindbuf.h>
  16. #include <sysdep.h>
  17. #include <bits/kernel-features.h>
  18. #include <lowlevellock.h>
  19. #include <tls.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. cfi_startproc
  27. __pthread_once:
  28. movl 4(%esp), %ecx
  29. testl $2, (%ecx)
  30. jz 1f
  31. xorl %eax, %eax
  32. ret
  33. 1: pushl %ebx
  34. cfi_adjust_cfa_offset (4)
  35. cfi_rel_offset (3, 0)
  36. pushl %esi
  37. cfi_adjust_cfa_offset (4)
  38. cfi_rel_offset (6, 0)
  39. movl %ecx, %ebx
  40. xorl %esi, %esi
  41. /* Not yet initialized or initialization in progress.
  42. Get the fork generation counter now. */
  43. 6: movl (%ebx), %eax
  44. #ifdef __PIC__
  45. call __x86.get_pc_thunk.cx
  46. addl $_GLOBAL_OFFSET_TABLE_, %ecx
  47. #endif
  48. 5: movl %eax, %edx
  49. testl $2, %eax
  50. jnz 4f
  51. andl $3, %edx
  52. #ifdef __PIC__
  53. orl __fork_generation@GOTOFF(%ecx), %edx
  54. #else
  55. orl __fork_generation, %edx
  56. #endif
  57. orl $1, %edx
  58. LOCK
  59. cmpxchgl %edx, (%ebx)
  60. jnz 5b
  61. /* Check whether another thread already runs the initializer. */
  62. testl $1, %eax
  63. jz 3f /* No -> do it. */
  64. /* Check whether the initializer execution was interrupted
  65. by a fork. */
  66. xorl %edx, %eax
  67. testl $0xfffffffc, %eax
  68. jnz 3f /* Different for generation -> run initializer. */
  69. /* Somebody else got here first. Wait. */
  70. #ifdef __ASSUME_PRIVATE_FUTEX
  71. movl $FUTEX_WAIT|FUTEX_PRIVATE_FLAG, %ecx
  72. #else
  73. # if FUTEX_WAIT == 0
  74. movl %gs:PRIVATE_FUTEX, %ecx
  75. # else
  76. movl $FUTEX_WAIT, %ecx
  77. orl %gs:PRIVATE_FUTEX, %ecx
  78. # endif
  79. #endif
  80. movl $SYS_futex, %eax
  81. ENTER_KERNEL
  82. jmp 6b
  83. 3: /* Call the initializer function after setting up the
  84. cancellation handler. Note that it is not possible here
  85. to use the unwind-based cleanup handling. This would require
  86. that the user-provided function and all the code it calls
  87. is compiled with exceptions. Unfortunately this cannot be
  88. guaranteed. */
  89. subl $UNWINDBUFSIZE+8, %esp
  90. cfi_adjust_cfa_offset (UNWINDBUFSIZE+8)
  91. movl %ecx, %ebx /* PIC register value. */
  92. leal 8+UWJMPBUF(%esp), %eax
  93. movl $0, 4(%esp)
  94. movl %eax, (%esp)
  95. call __sigsetjmp@PLT
  96. testl %eax, %eax
  97. jne 7f
  98. leal 8(%esp), %eax
  99. call HIDDEN_JUMPTARGET(__pthread_register_cancel)
  100. /* Call the user-provided initialization function. */
  101. call *24+UNWINDBUFSIZE(%esp)
  102. /* Pop the cleanup handler. */
  103. leal 8(%esp), %eax
  104. call HIDDEN_JUMPTARGET(__pthread_unregister_cancel)
  105. addl $UNWINDBUFSIZE+8, %esp
  106. cfi_adjust_cfa_offset (-UNWINDBUFSIZE-8)
  107. /* Sucessful run of the initializer. Signal that we are done. */
  108. movl 12(%esp), %ebx
  109. LOCK
  110. addl $1, (%ebx)
  111. /* Wake up all other threads. */
  112. movl $0x7fffffff, %edx
  113. #ifdef __ASSUME_PRIVATE_FUTEX
  114. movl $FUTEX_WAKE|FUTEX_PRIVATE_FLAG, %ecx
  115. #else
  116. movl $FUTEX_WAKE, %ecx
  117. orl %gs:PRIVATE_FUTEX, %ecx
  118. #endif
  119. movl $SYS_futex, %eax
  120. ENTER_KERNEL
  121. 4: popl %esi
  122. cfi_adjust_cfa_offset (-4)
  123. cfi_restore (6)
  124. popl %ebx
  125. cfi_adjust_cfa_offset (-4)
  126. cfi_restore (3)
  127. xorl %eax, %eax
  128. ret
  129. 7: /* __sigsetjmp returned for the second time. */
  130. movl 20+UNWINDBUFSIZE(%esp), %ebx
  131. cfi_adjust_cfa_offset (UNWINDBUFSIZE+16)
  132. cfi_offset (3, -8)
  133. cfi_offset (6, -12)
  134. movl $0, (%ebx)
  135. movl $0x7fffffff, %edx
  136. #ifdef __ASSUME_PRIVATE_FUTEX
  137. movl $FUTEX_WAKE|FUTEX_PRIVATE_FLAG, %ecx
  138. #else
  139. movl $FUTEX_WAKE, %ecx
  140. orl %gs:PRIVATE_FUTEX, %ecx
  141. #endif
  142. movl $SYS_futex, %eax
  143. ENTER_KERNEL
  144. leal 8(%esp), %eax
  145. call HIDDEN_JUMPTARGET (__pthread_unwind_next)
  146. /* NOTREACHED */
  147. hlt
  148. cfi_endproc
  149. .size __pthread_once,.-__pthread_once
  150. .globl __pthread_once_internal
  151. __pthread_once_internal = __pthread_once
  152. .globl pthread_once
  153. pthread_once = __pthread_once
  154. #ifdef __PIC__
  155. .section .gnu.linkonce.t.__x86.get_pc_thunk.cx,"ax",@progbits
  156. .globl __x86.get_pc_thunk.cx
  157. .hidden __x86.get_pc_thunk.cx
  158. .type __x86.get_pc_thunk.cx,@function
  159. __x86.get_pc_thunk.cx:
  160. movl (%esp), %ecx;
  161. ret
  162. .size __x86.get_pc_thunk.cx,.-__x86.get_pc_thunk.cx
  163. #endif