2.9
[glibc/nacl-glibc.git] / nptl / sysdeps / unix / sysv / linux / x86_64 / pthread_cond_broadcast.S
blob6155255eb0cadb9394a622973ac365cebda3de9c
1 /* Copyright (C) 2002, 2003, 2004, 2005, 2006, 2007
2    Free Software Foundation, Inc.
3    This file is part of the GNU C Library.
4    Contributed by Ulrich Drepper <drepper@redhat.com>, 2002.
6    The GNU C Library is free software; you can redistribute it and/or
7    modify it under the terms of the GNU Lesser General Public
8    License as published by the Free Software Foundation; either
9    version 2.1 of the License, or (at your option) any later version.
11    The GNU C Library is distributed in the hope that it will be useful,
12    but WITHOUT ANY WARRANTY; without even the implied warranty of
13    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14    Lesser General Public License for more details.
16    You should have received a copy of the GNU Lesser General Public
17    License along with the GNU C Library; if not, write to the Free
18    Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
19    02111-1307 USA.  */
21 #include <sysdep.h>
22 #include <shlib-compat.h>
23 #include <lowlevellock.h>
24 #include <lowlevelcond.h>
25 #include <kernel-features.h>
26 #include <pthread-pi-defines.h>
27 #include <pthread-errnos.h>
30         .text
32         /* int pthread_cond_broadcast (pthread_cond_t *cond) */
33         .globl  __pthread_cond_broadcast
34         .type   __pthread_cond_broadcast, @function
35         .align  16
36 __pthread_cond_broadcast:
38         /* Get internal lock.  */
39         movl    $1, %esi
40         xorl    %eax, %eax
41         LOCK
42 #if cond_lock == 0
43         cmpxchgl %esi, (%rdi)
44 #else
45         cmpxchgl %esi, cond_lock(%rdi)
46 #endif
47         jnz     1f
49 2:      addq    $cond_futex, %rdi
50         movq    total_seq-cond_futex(%rdi), %r9
51         cmpq    wakeup_seq-cond_futex(%rdi), %r9
52         jna     4f
54         /* Cause all currently waiting threads to recognize they are
55            woken up.  */
56         movq    %r9, wakeup_seq-cond_futex(%rdi)
57         movq    %r9, woken_seq-cond_futex(%rdi)
58         addq    %r9, %r9
59         movl    %r9d, (%rdi)
60         incl    broadcast_seq-cond_futex(%rdi)
62         /* Get the address of the mutex used.  */
63         movq    dep_mutex-cond_futex(%rdi), %r8
65         /* Unlock.  */
66         LOCK
67         decl    cond_lock-cond_futex(%rdi)
68         jne     7f
70 8:      cmpq    $-1, %r8
71         je      9f
73         /* XXX: The kernel so far doesn't support requeue to PI futex.  */
74         /* XXX: The kernel only supports FUTEX_CMP_REQUEUE to the same
75            type of futex (private resp. shared).  */
76         testl   $(PI_BIT | PS_BIT), MUTEX_KIND(%r8)
77         jne     9f
79         /* Wake up all threads.  */
80 #ifdef __ASSUME_PRIVATE_FUTEX
81         movl    $(FUTEX_CMP_REQUEUE|FUTEX_PRIVATE_FLAG), %esi
82 #else
83         movl    %fs:PRIVATE_FUTEX, %esi
84         orl     $FUTEX_CMP_REQUEUE, %esi
85 #endif
86         movl    $SYS_futex, %eax
87         movl    $1, %edx
88         movl    $0x7fffffff, %r10d
89         syscall
91         /* For any kind of error, which mainly is EAGAIN, we try again
92            with WAKE.  The general test also covers running on old
93            kernels.  */
94         cmpq    $-4095, %rax
95         jae     9f
97 10:     xorl    %eax, %eax
98         retq
100         .align  16
101         /* Unlock.  */
102 4:      LOCK
103         decl    cond_lock-cond_futex(%rdi)
104         jne     5f
106 6:      xorl    %eax, %eax
107         retq
109         /* Initial locking failed.  */
111 #if cond_lock != 0
112         addq    $cond_lock, %rdi
113 #endif
114         cmpq    $-1, dep_mutex-cond_lock(%rdi)
115         movl    $LLL_PRIVATE, %eax
116         movl    $LLL_SHARED, %esi
117         cmovne  %eax, %esi
118         callq   __lll_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         cmpq    $-1, dep_mutex-cond_lock(%rdi)
127         movl    $LLL_PRIVATE, %eax
128         movl    $LLL_SHARED, %esi
129         cmovne  %eax, %esi
130         callq   __lll_unlock_wake
131         jmp     6b
133         /* Unlock in loop requires wakeup.  */
134 7:      addq    $cond_lock-cond_futex, %rdi
135         cmpq    $-1, %r8
136         movl    $LLL_PRIVATE, %eax
137         movl    $LLL_SHARED, %esi
138         cmovne  %eax, %esi
139         callq   __lll_unlock_wake
140         subq    $cond_lock-cond_futex, %rdi
141         jmp     8b
143 9:      /* The futex requeue functionality is not available.  */
144         cmpq    $-1, %r8
145         movl    $0x7fffffff, %edx
146 #ifdef __ASSUME_PRIVATE_FUTEX
147         movl    $FUTEX_WAKE, %eax
148         movl    $(FUTEX_WAKE|FUTEX_PRIVATE_FLAG), %esi
149         cmove   %eax, %esi
150 #else
151         movl    $0, %eax
152         movl    %fs:PRIVATE_FUTEX, %esi
153         cmove   %eax, %esi
154         orl     $FUTEX_WAKE, %esi
155 #endif
156         movl    $SYS_futex, %eax
157         syscall
158         jmp     10b
159         .size   __pthread_cond_broadcast, .-__pthread_cond_broadcast
160 versioned_symbol (libpthread, __pthread_cond_broadcast, pthread_cond_broadcast,
161                   GLIBC_2_3_2)