pthread_once.S 4.7 KB

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