Update copyright notices with scripts/update-copyrights
[glibc.git] / nptl / sysdeps / unix / sysv / linux / i386 / pthread_once.S
blobdacd724d8bf745d81653858632983f00edc5712e
1 /* Copyright (C) 2002-2014 Free Software Foundation, Inc.
2    This file is part of the GNU C Library.
3    Contributed by Ulrich Drepper <drepper@redhat.com>, 2002.
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.
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.
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/>.  */
19 #include <unwindbuf.h>
20 #include <sysdep.h>
21 #include <kernel-features.h>
22 #include <lowlevellock.h>
25         .comm   __fork_generation, 4, 4
27         .text
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
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
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
57 5:      movl    %eax, %edx
59         testl   $2, %eax
60         jnz     4f
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
70         LOCK
71         cmpxchgl %edx, (%ebx)
72         jnz     5b
74         /* Check whether another thread already runs the initializer.  */
75         testl   $1, %eax
76         jz      3f      /* No -> do it.  */
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.  */
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
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.  */
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
116         leal    8(%esp), %eax
117         call    HIDDEN_JUMPTARGET(__pthread_register_cancel)
119         /* Call the user-provided initialization function.  */
120         call    *24+UNWINDBUFSIZE(%esp)
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)
128         /* Sucessful run of the initializer.  Signal that we are done.  */
129         movl    12(%esp), %ebx
130         LOCK
131         addl    $1, (%ebx)
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
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
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)
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
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
177 hidden_def (__pthread_once)
178 strong_alias (__pthread_once, pthread_once)