(CFLAGS-tst-align.c): Add -mpreferred-stack-boundary=4.
[glibc.git] / nptl / sysdeps / unix / sysv / linux / x86_64 / pthread_barrier_wait.S
blobe1593f32ff2d7a79faf7c4cc1fbe1e9de3aacdf3
1 /* Copyright (C) 2002, 2003, 2004 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, write to the Free
17    Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
18    02111-1307 USA.  */
20 #include <sysdep.h>
21 #include <lowlevelbarrier.h>
23 #define SYS_futex       202
24 #define FUTEX_WAIT      0
25 #define FUTEX_WAKE      1
27 #ifndef UP
28 # define LOCK lock
29 #else
30 # define LOCK
31 #endif
34         .text
36         .globl  pthread_barrier_wait
37         .type   pthread_barrier_wait,@function
38         .align  16
39 pthread_barrier_wait:
40         /* Get the mutex.  */
41         xorl    %eax, %eax
42         movl    $1, %esi
43         LOCK
44         cmpxchgl %esi, MUTEX(%rdi)
45         jnz     1f
47         /* One less waiter.  If this was the last one needed wake
48            everybody.  */
49 2:      decl    LEFT(%rdi)
50         je      3f
52         /* There are more threads to come.  */
53 #if CURR_EVENT == 0
54         movl    (%rdi), %edx
55 #else
56         movl    CURR_EVENT(%rdi), %edx
57 #endif
59         /* Release the mutex.  */
60         LOCK
61         decl    MUTEX(%rdi)
62         jne     6f
64         /* Wait for the remaining threads.  The call will return immediately
65            if the CURR_EVENT memory has meanwhile been changed.  */
66 7:      xorq    %rsi, %rsi              /* movq $FUTEX_WAIT, %rsi */
67         xorq    %r10, %r10
68 8:      movq    $SYS_futex, %rax
69         syscall
71         /* Don't return on spurious wakeups.  The syscall does not change
72            any register except %eax so there is no need to reload any of
73            them.  */
74 #if CURR_EVENT == 0
75         cmpl    %edx, (%rdi)
76 #else
77         cmpl    %edx, CURR_EVENT(%rdi)
78 #endif
79         je      8b
81         /* Increment LEFT.  If this brings the count back to the
82            initial count unlock the object.  */
83         movl    $1, %edx
84         movl    INIT_COUNT(%rdi), %eax
85         LOCK
86         xaddl   %edx, LEFT(%rdi)
87         subl    $1, %eax
88         cmpl    %eax, %edx
89         jne,pt  10f
91         /* Release the mutex.  We cannot release the lock before
92            waking the waiting threads since otherwise a new thread might
93            arrive and gets waken up, too.  */
94         LOCK
95         decl    MUTEX(%rdi)
96         jne     9f
98 10:     xorl    %eax, %eax              /* != PTHREAD_BARRIER_SERIAL_THREAD */
100         retq
102         /* The necessary number of threads arrived.  */
104 #if CURR_EVENT == 0
105         incl    (%rdi)
106 #else
107         incl    CURR_EVENT(%rdi)
108 #endif
110         /* Wake up all waiters.  The count is a signed number in the kernel
111            so 0x7fffffff is the highest value.  */
112         movl    $0x7fffffff, %edx
113         movq    $FUTEX_WAKE, %rsi
114         movq    $SYS_futex, %rax
115         syscall
117         /* Increment LEFT.  If this brings the count back to the
118            initial count unlock the object.  */
119         movl    $1, %edx
120         movl    INIT_COUNT(%rdi), %eax
121         LOCK
122         xaddl   %edx, LEFT(%rdi)
123         subl    $1, %eax
124         cmpl    %eax, %edx
125         jne,pt  5f
127         /* Release the mutex.  We cannot release the lock before
128            waking the waiting threads since otherwise a new thread might
129            arrive and gets waken up, too.  */
130         LOCK
131         decl    MUTEX(%rdi)
132         jne     4f
134 5:      orl     $-1, %eax               /* == PTHREAD_BARRIER_SERIAL_THREAD */
136         retq
138 1:      addq    $MUTEX, %rdi
139         callq   __lll_mutex_lock_wait
140         subq    $MUTEX, %rdi
141         jmp     2b
143 4:      addq    $MUTEX, %rdi
144         callq   __lll_mutex_unlock_wake
145         jmp     5b
147 6:      addq    $MUTEX, %rdi
148         callq   __lll_mutex_unlock_wake
149         subq    $MUTEX, %rdi
150         jmp     7b
152 9:      addq    $MUTEX, %rdi
153         callq   __lll_mutex_unlock_wake
154         jmp     10b
155         .size   pthread_barrier_wait,.-pthread_barrier_wait