pthread_once.S 4.3 KB

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