Initial revision
[glibc.git] / nptl / sysdeps / unix / sysv / linux / i386 / i486 / pthread_barrier_wait.S
bloba385adc5f979a7dfcb8b4a9bfcf025f7525e934e
1 /* Copyright (C) 2002 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>
22 #define SYS_futex       240
23 #define FUTEX_WAIT      0
24 #define FUTEX_WAKE      1
26 #ifndef UP
27 # define LOCK lock
28 #else
29 # define LOCK
30 #endif
32 #define CURR_EVENT      0
33 #define MUTEX           4
34 #define LEFT            8
35 #define INIT_COUNT      12
38         .text
40         .globl  pthread_barrier_wait
41         .type   pthread_barrier_wait,@function
42         .align  16
43 pthread_barrier_wait:
44         pushl   %esi
45         pushl   %ebx
47         movl    12(%esp), %ebx
48         xorl    %esi, %esi
50         /* Get the mutex.  */
51         orl     $-1, %eax
52         LOCK
53         xaddl   %eax, MUTEX(%ebx)
54         jne     1f
56         /* One less waiter.  If this was the last one needed wake
57            everybody.  */
58 2:      decl    LEFT(%ebx)
59         je      3f
61         /* There are more threads to come.  */
62         movl    CURR_EVENT(%ebx), %edx
64         /* Release the mutex.  */
65         LOCK
66         incl    MUTEX(%ebx)
67         jng     6f
69         /* Wait for the remaining threads.  The call will return immediately
70            if the CURR_EVENT memory has meanwhile been changed.  */
71 7:      movl    %esi, %ecx              /* movl $FUTEX_WAIT, %ecx */
72 8:      movl    $SYS_futex, %eax
73         int     $0x80
75         /* Don't return on spurious wakeups.  The syscall does not change
76            any register except %eax so there is no need to reload any of
77            them.  */
78         cmpl    %edx, CURR_EVENT(%ebx)
79         je,pn   8b
81         /* Note: %esi is still zero.  */
82         movl    %esi, %eax              /* != PTHREAD_BARRIER_SERIAL_THREAD */
84         popl    %ebx
85         popl    %esi
86         ret
88         /* The necessary number of threads arrived.  */
89 3:      movl    INIT_COUNT(%ebx), %eax
90         movl    %eax, LEFT(%ebx)
91         incl    CURR_EVENT(%ebx)
93         /* Wake up all waiters.  The count is a signed number in the kernel
94            so 0x7fffffff is the highest value.  */
95         movl    $0x7fffffff, %edx
96         movl    $FUTEX_WAKE, %ecx
97         movl    $SYS_futex, %eax
98         int     $0x80
100         /* Release the mutex.  */
101         LOCK
102         incl    MUTEX(%ebx)
103         jng     4f
105 5:      orl     $-1, %eax               /* == PTHREAD_BARRIER_SERIAL_THREAD */
107         popl    %ebx
108         popl    %esi
109         ret
111 1:      leal    MUTEX(%ebx), %ecx
112         call    __lll_lock_wait
113         jmp     2b
115 4:      leal    MUTEX(%ebx), %eax
116         call    __lll_unlock_wake
117         jmp     5b
119 6:      leal    MUTEX(%ebx), %eax
120         call    __lll_unlock_wake
121         jmp     7b
122         .size   pthread_barrier_wait,.-pthread_barrier_wait