]> git.ipfire.org Git - thirdparty/glibc.git/blob - nptl/sysdeps/unix/sysv/linux/x86_64/pthread_barrier_wait.S
Update.
[thirdparty/glibc.git] / nptl / sysdeps / unix / sysv / linux / x86_64 / pthread_barrier_wait.S
1 /* Copyright (C) 2002, 2003 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, write to the Free
17 Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
18 02111-1307 USA. */
19
20 #include <sysdep.h>
21 #include <lowlevelbarrier.h>
22
23 #define SYS_futex 202
24 #define FUTEX_WAIT 0
25 #define FUTEX_WAKE 1
26
27 #ifndef UP
28 # define LOCK lock
29 #else
30 # define LOCK
31 #endif
32
33
34 .text
35
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
46
47 /* One less waiter. If this was the last one needed wake
48 everybody. */
49 2: decl LEFT(%rdi)
50 je 3f
51
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
58
59 /* Release the mutex. */
60 LOCK
61 decl MUTEX(%rdi)
62 jne 6f
63
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
70
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
80
81 /* Note: %esi is still zero. */
82 movl %esi, %eax /* != PTHREAD_BARRIER_SERIAL_THREAD */
83
84 retq
85
86 /* The necessary number of threads arrived. */
87 3: movl INIT_COUNT(%rdi), %eax
88 movl %eax, LEFT(%rdi)
89 #if CURR_EVENT == 0
90 incl (%rdi)
91 #else
92 incl CURR_EVENT(%rdi)
93 #endif
94
95 /* Wake up all waiters. The count is a signed number in the kernel
96 so 0x7fffffff is the highest value. */
97 movl $0x7fffffff, %edx
98 movq $FUTEX_WAKE, %rsi
99 movq $SYS_futex, %rax
100 syscall
101
102 /* Release the mutex. We cannot release the lock before
103 waking the waiting threads since otherwise a new thread might
104 arrive and gets waken up, too. */
105 LOCK
106 decl MUTEX(%rdi)
107 jne 4f
108
109 5: orl $-1, %eax /* == PTHREAD_BARRIER_SERIAL_THREAD */
110
111 retq
112
113 1: addq $MUTEX, %rdi
114 callq __lll_mutex_lock_wait
115 subq $MUTEX, %rdi
116 jmp 2b
117
118 4: addq $MUTEX, %rdi
119 callq __lll_mutex_unlock_wake
120 subq $MUTEX, %rdi
121 jmp 5b
122
123 6: addq $MUTEX, %rdi
124 callq __lll_mutex_unlock_wake
125 subq $MUTEX, %rdi
126 jmp 7b
127 .size pthread_barrier_wait,.-pthread_barrier_wait