2.5-18.1
[glibc.git] / nptl / sysdeps / unix / sysv / linux / x86_64 / pthread_cond_broadcast.S
blob006de2696e23ee2a8e806b11d8139d5e309ba12c
1 /* Copyright (C) 2002, 2003, 2004, 2005, 2006 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 <shlib-compat.h>
22 #include <lowlevelcond.h>
23 #include <kernel-features.h>
24 #include <pthread-pi-defines.h>
26 #ifdef UP
27 # define LOCK
28 #else
29 # define LOCK lock
30 #endif
32 #define SYS_futex               202
33 #define FUTEX_WAIT              0
34 #define FUTEX_WAKE              1
35 #define FUTEX_REQUEUE           3
36 #define FUTEX_CMP_REQUEUE       4
38 #define EINVAL                  22
41         .text
43         /* int pthread_cond_broadcast (pthread_cond_t *cond) */
44         .globl  __pthread_cond_broadcast
45         .type   __pthread_cond_broadcast, @function
46         .align  16
47 __pthread_cond_broadcast:
49         /* Get internal lock.  */
50         movl    $1, %esi
51         xorl    %eax, %eax
52         LOCK
53 #if cond_lock == 0
54         cmpxchgl %esi, (%rdi)
55 #else
56         cmpxchgl %esi, cond_lock(%rdi)
57 #endif
58         jnz     1f
60 2:      addq    $cond_futex, %rdi
61         movq    total_seq-cond_futex(%rdi), %r9
62         cmpq    wakeup_seq-cond_futex(%rdi), %r9
63         jna     4f
65         /* Cause all currently waiting threads to recognize they are
66            woken up.  */
67         movq    %r9, wakeup_seq-cond_futex(%rdi)
68         movq    %r9, woken_seq-cond_futex(%rdi)
69         addq    %r9, %r9
70         movl    %r9d, (%rdi)
71         incl    broadcast_seq-cond_futex(%rdi)
73         /* Get the address of the mutex used.  */
74         movq    dep_mutex-cond_futex(%rdi), %r8
76         /* Unlock.  */
77         LOCK
78         decl    cond_lock-cond_futex(%rdi)
79         jne     7f
81 8:      cmpq    $-1, %r8
82         je      9f
84         /* XXX: The kernel so far doesn't support requeue to PI futex.  */
85         testl   $PI_BIT, MUTEX_KIND(%r8)
86         jne     9f
88         /* Wake up all threads.  */
89         movl    $FUTEX_CMP_REQUEUE, %esi
90         movl    $SYS_futex, %eax
91         movl    $1, %edx
92         movl    $0x7fffffff, %r10d
93         syscall
95         /* For any kind of error, which mainly is EAGAIN, we try again
96            with WAKE.  The general test also covers running on old
97            kernels.  */
98         cmpq    $-4095, %rax
99         jae     9f
101 10:     xorl    %eax, %eax
102         retq
104         .align  16
105         /* Unlock.  */
106 4:      LOCK
107         decl    cond_lock-cond_futex(%rdi)
108         jne     5f
110 6:      xorl    %eax, %eax
111         retq
113         /* Initial locking failed.  */
115 #if cond_lock != 0
116         addq    $cond_lock, %rdi
117 #endif
118         callq   __lll_mutex_lock_wait
119 #if cond_lock != 0
120         subq    $cond_lock, %rdi
121 #endif
122         jmp     2b
124         /* Unlock in loop requires wakeup.  */
125 5:      addq    $cond_lock-cond_futex, %rdi
126         callq   __lll_mutex_unlock_wake
127         jmp     6b
129         /* Unlock in loop requires wakeup.  */
130 7:      addq    $cond_lock-cond_futex, %rdi
131         callq   __lll_mutex_unlock_wake
132         subq    $cond_lock-cond_futex, %rdi
133         jmp     8b
135 9:      /* The futex requeue functionality is not available.  */
136         movl    $0x7fffffff, %edx
137         movl    $FUTEX_WAKE, %esi
138         movl    $SYS_futex, %eax
139         syscall
140         jmp     10b
141         .size   __pthread_cond_broadcast, .-__pthread_cond_broadcast
142 versioned_symbol (libpthread, __pthread_cond_broadcast, pthread_cond_broadcast,
143                   GLIBC_2_3_2)