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