2.5-18.1
[glibc.git] / nptl / sysdeps / unix / sysv / linux / x86_64 / pthread_barrier_wait.S
blobfa8125dd87fda4bffb5c89a4cbf967d43f8f2adb
1 /* Copyright (C) 2002, 2003, 2004, 2005 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.  */
67 #if FUTEX_WAIT == 0
68         xorl    %esi, %esi
69 #else
70         movl    $FUTEX_WAIT, %esi
71 #endif
72         xorq    %r10, %r10
73 8:      movl    $SYS_futex, %eax
74         syscall
76         /* Don't return on spurious wakeups.  The syscall does not change
77            any register except %eax so there is no need to reload any of
78            them.  */
79 #if CURR_EVENT == 0
80         cmpl    %edx, (%rdi)
81 #else
82         cmpl    %edx, CURR_EVENT(%rdi)
83 #endif
84         je      8b
86         /* Increment LEFT.  If this brings the count back to the
87            initial count unlock the object.  */
88         movl    $1, %edx
89         movl    INIT_COUNT(%rdi), %eax
90         LOCK
91         xaddl   %edx, LEFT(%rdi)
92         subl    $1, %eax
93         cmpl    %eax, %edx
94         jne,pt  10f
96         /* Release the mutex.  We cannot release the lock before
97            waking the waiting threads since otherwise a new thread might
98            arrive and gets waken up, too.  */
99         LOCK
100         decl    MUTEX(%rdi)
101         jne     9f
103 10:     xorl    %eax, %eax              /* != PTHREAD_BARRIER_SERIAL_THREAD */
105         retq
107         /* The necessary number of threads arrived.  */
109 #if CURR_EVENT == 0
110         incl    (%rdi)
111 #else
112         incl    CURR_EVENT(%rdi)
113 #endif
115         /* Wake up all waiters.  The count is a signed number in the kernel
116            so 0x7fffffff is the highest value.  */
117         movl    $0x7fffffff, %edx
118         movl    $FUTEX_WAKE, %esi
119         movl    $SYS_futex, %eax
120         syscall
122         /* Increment LEFT.  If this brings the count back to the
123            initial count unlock the object.  */
124         movl    $1, %edx
125         movl    INIT_COUNT(%rdi), %eax
126         LOCK
127         xaddl   %edx, LEFT(%rdi)
128         subl    $1, %eax
129         cmpl    %eax, %edx
130         jne,pt  5f
132         /* Release the mutex.  We cannot release the lock before
133            waking the waiting threads since otherwise a new thread might
134            arrive and gets waken up, too.  */
135         LOCK
136         decl    MUTEX(%rdi)
137         jne     4f
139 5:      orl     $-1, %eax               /* == PTHREAD_BARRIER_SERIAL_THREAD */
141         retq
143 1:      addq    $MUTEX, %rdi
144         callq   __lll_mutex_lock_wait
145         subq    $MUTEX, %rdi
146         jmp     2b
148 4:      addq    $MUTEX, %rdi
149         callq   __lll_mutex_unlock_wake
150         jmp     5b
152 6:      addq    $MUTEX, %rdi
153         callq   __lll_mutex_unlock_wake
154         subq    $MUTEX, %rdi
155         jmp     7b
157 9:      addq    $MUTEX, %rdi
158         callq   __lll_mutex_unlock_wake
159         jmp     10b
160         .size   pthread_barrier_wait,.-pthread_barrier_wait