]> sourceware.org Git - glibc.git/blob - nptl/sysdeps/unix/sysv/linux/i386/pthread_once.S
db11921060425d5a50744edfaaf84bb8f9bacfbd
[glibc.git] / nptl / sysdeps / unix / sysv / linux / i386 / pthread_once.S
1 /* Copyright (C) 2002, 2003, 2007, 2012 Free Software Foundation, Inc.
2 This file is part of the GNU C Library.
3 Contributed by Ulrich Drepper <drepper@redhat.com>, 2002.
4
5 The GNU C Library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Lesser General Public
7 License as published by the Free Software Foundation; either
8 version 2.1 of the License, or (at your option) any later version.
9
10 The GNU C Library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Lesser General Public License for more details.
14
15 You should have received a copy of the GNU Lesser General Public
16 License along with the GNU C Library; if not, see
17 <http://www.gnu.org/licenses/>. */
18
19 #include <unwindbuf.h>
20 #include <sysdep.h>
21 #include <kernel-features.h>
22 #include <lowlevellock.h>
23
24
25 .comm __fork_generation, 4, 4
26
27 .text
28
29
30 .globl __pthread_once
31 .type __pthread_once,@function
32 .align 16
33 cfi_startproc
34 __pthread_once:
35 movl 4(%esp), %ecx
36 testl $2, (%ecx)
37 jz 1f
38 xorl %eax, %eax
39 ret
40
41 1: pushl %ebx
42 cfi_adjust_cfa_offset (4)
43 cfi_rel_offset (3, 0)
44 pushl %esi
45 cfi_adjust_cfa_offset (4)
46 cfi_rel_offset (6, 0)
47 movl %ecx, %ebx
48 xorl %esi, %esi
49
50 /* Not yet initialized or initialization in progress.
51 Get the fork generation counter now. */
52 6: movl (%ebx), %eax
53 #ifdef PIC
54 LOAD_PIC_REG(cx)
55 #endif
56
57 5: movl %eax, %edx
58
59 testl $2, %eax
60 jnz 4f
61
62 andl $3, %edx
63 #ifdef PIC
64 orl __fork_generation@GOTOFF(%ecx), %edx
65 #else
66 orl __fork_generation, %edx
67 #endif
68 orl $1, %edx
69
70 LOCK
71 cmpxchgl %edx, (%ebx)
72 jnz 5b
73
74 /* Check whether another thread already runs the initializer. */
75 testl $1, %eax
76 jz 3f /* No -> do it. */
77
78 /* Check whether the initializer execution was interrupted
79 by a fork. */
80 xorl %edx, %eax
81 testl $0xfffffffc, %eax
82 jnz 3f /* Different for generation -> run initializer. */
83
84 /* Somebody else got here first. Wait. */
85 #ifdef __ASSUME_PRIVATE_FUTEX
86 movl $FUTEX_WAIT|FUTEX_PRIVATE_FLAG, %ecx
87 #else
88 # if FUTEX_WAIT == 0
89 movl %gs:PRIVATE_FUTEX, %ecx
90 # else
91 movl $FUTEX_WAIT, %ecx
92 orl %gs:PRIVATE_FUTEX, %ecx
93 # endif
94 #endif
95 movl $SYS_futex, %eax
96 ENTER_KERNEL
97 jmp 6b
98
99 3: /* Call the initializer function after setting up the
100 cancellation handler. Note that it is not possible here
101 to use the unwind-based cleanup handling. This would require
102 that the user-provided function and all the code it calls
103 is compiled with exceptions. Unfortunately this cannot be
104 guaranteed. */
105 subl $UNWINDBUFSIZE+8, %esp
106 cfi_adjust_cfa_offset (UNWINDBUFSIZE+8)
107 movl %ecx, %ebx /* PIC register value. */
108
109 leal 8+UWJMPBUF(%esp), %eax
110 movl $0, 4(%esp)
111 movl %eax, (%esp)
112 call __sigsetjmp@PLT
113 testl %eax, %eax
114 jne 7f
115
116 leal 8(%esp), %eax
117 call HIDDEN_JUMPTARGET(__pthread_register_cancel)
118
119 /* Call the user-provided initialization function. */
120 call *24+UNWINDBUFSIZE(%esp)
121
122 /* Pop the cleanup handler. */
123 leal 8(%esp), %eax
124 call HIDDEN_JUMPTARGET(__pthread_unregister_cancel)
125 addl $UNWINDBUFSIZE+8, %esp
126 cfi_adjust_cfa_offset (-UNWINDBUFSIZE-8)
127
128 /* Sucessful run of the initializer. Signal that we are done. */
129 movl 12(%esp), %ebx
130 LOCK
131 addl $1, (%ebx)
132
133 /* Wake up all other threads. */
134 movl $0x7fffffff, %edx
135 #ifdef __ASSUME_PRIVATE_FUTEX
136 movl $FUTEX_WAKE|FUTEX_PRIVATE_FLAG, %ecx
137 #else
138 movl $FUTEX_WAKE, %ecx
139 orl %gs:PRIVATE_FUTEX, %ecx
140 #endif
141 movl $SYS_futex, %eax
142 ENTER_KERNEL
143
144 4: popl %esi
145 cfi_adjust_cfa_offset (-4)
146 cfi_restore (6)
147 popl %ebx
148 cfi_adjust_cfa_offset (-4)
149 cfi_restore (3)
150 xorl %eax, %eax
151 ret
152
153 7: /* __sigsetjmp returned for the second time. */
154 movl 20+UNWINDBUFSIZE(%esp), %ebx
155 cfi_adjust_cfa_offset (UNWINDBUFSIZE+16)
156 cfi_offset (3, -8)
157 cfi_offset (6, -12)
158 movl $0, (%ebx)
159
160 movl $0x7fffffff, %edx
161 #ifdef __ASSUME_PRIVATE_FUTEX
162 movl $FUTEX_WAKE|FUTEX_PRIVATE_FLAG, %ecx
163 #else
164 movl $FUTEX_WAKE, %ecx
165 orl %gs:PRIVATE_FUTEX, %ecx
166 #endif
167 movl $SYS_futex, %eax
168 ENTER_KERNEL
169
170 leal 8(%esp), %eax
171 call HIDDEN_JUMPTARGET (__pthread_unwind_next)
172 /* NOTREACHED */
173 hlt
174 cfi_endproc
175 .size __pthread_once,.-__pthread_once
176
177 .globl __pthread_once_internal
178 __pthread_once_internal = __pthread_once
179
180 .globl pthread_once
181 pthread_once = __pthread_once
This page took 0.046921 seconds and 4 git commands to generate.