2.9
[glibc/nacl-glibc.git] / nptl / sysdeps / unix / sysv / linux / i386 / i486 / pthread_rwlock_rdlock.S
blob4e5f0c5b2ad9815d279cd13465f0c2e6e5ec567f
1 /* Copyright (C) 2002, 2003, 2007 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 <lowlevellock.h>
22 #include <lowlevelrwlock.h>
23 #include <pthread-errnos.h>
24 #include <kernel-features.h>
27         .text
29         .globl  __pthread_rwlock_rdlock
30         .type   __pthread_rwlock_rdlock,@function
31         .align  16
32 __pthread_rwlock_rdlock:
33         cfi_startproc
34         pushl   %esi
35         cfi_adjust_cfa_offset(4)
36         pushl   %ebx
37         cfi_adjust_cfa_offset(4)
38         cfi_offset(%esi, -8)
39         cfi_offset(%ebx, -12)
41         xorl    %esi, %esi
42         movl    12(%esp), %ebx
44         /* Get the lock.  */
45         movl    $1, %edx
46         xorl    %eax, %eax
47         LOCK
48 #if MUTEX == 0
49         cmpxchgl %edx, (%ebx)
50 #else
51         cmpxchgl %edx, MUTEX(%ebx)
52 #endif
53         jnz     1f
55 2:      movl    WRITER(%ebx), %eax
56         testl   %eax, %eax
57         jne     14f
58         cmpl    $0, WRITERS_QUEUED(%ebx)
59         je      5f
60         cmpb    $0, FLAGS(%ebx)
61         je      5f
63 3:      addl    $1, READERS_QUEUED(%ebx)
64         je      4f
66         movl    READERS_WAKEUP(%ebx), %edx
68         LOCK
69 #if MUTEX == 0
70         subl    $1, (%ebx)
71 #else
72         subl    $1, MUTEX(%ebx)
73 #endif
74         jne     10f
76 11:
77 #ifdef __ASSUME_PRIVATE_FUTEX
78         movzbl  PSHARED(%ebx), %ecx
79         xorl    $FUTEX_PRIVATE_FLAG|FUTEX_WAIT, %ecx
80 #else
81         movzbl  PSHARED(%ebx), %ecx
82 # if FUTEX_WAIT != 0
83         orl     $FUTEX_WAIT, %ecx
84 # endif
85         xorl    %gs:PRIVATE_FUTEX, %ecx
86 #endif
87         addl    $READERS_WAKEUP, %ebx
88         movl    $SYS_futex, %eax
89         ENTER_KERNEL
91         subl    $READERS_WAKEUP, %ebx
93         /* Reget the lock.  */
94         movl    $1, %edx
95         xorl    %eax, %eax
96         LOCK
97 #if MUTEX == 0
98         cmpxchgl %edx, (%ebx)
99 #else
100         cmpxchgl %edx, MUTEX(%ebx)
101 #endif
102         jnz     12f
104 13:     subl    $1, READERS_QUEUED(%ebx)
105         jmp     2b
107 5:      xorl    %edx, %edx
108         addl    $1, NR_READERS(%ebx)
109         je      8f
110 9:      LOCK
111 #if MUTEX == 0
112         subl    $1, (%ebx)
113 #else
114         subl    $1, MUTEX(%ebx)
115 #endif
116         jne     6f
119         movl    %edx, %eax
120         popl    %ebx
121         cfi_adjust_cfa_offset(-4)
122         cfi_restore(%ebx)
123         popl    %esi
124         cfi_adjust_cfa_offset(-4)
125         cfi_restore(%esi)
126         ret
128         cfi_adjust_cfa_offset(8)
129         cfi_offset(%esi, -8)
130         cfi_offset(%ebx, -12)
132 #if MUTEX == 0
133         movl    %ebx, %edx
134 #else
135         leal    MUTEX(%ebx), %edx
136 #endif
137         movzbl  PSHARED(%ebx), %ecx
138         call    __lll_lock_wait
139         jmp     2b
141 14:     cmpl    %gs:TID, %eax
142         jne     3b
143         /* Deadlock detected.  */
144         movl    $EDEADLK, %edx
145         jmp     9b
148 #if MUTEX == 0
149         movl    %ebx, %eax
150 #else
151         leal    MUTEX(%ebx), %eax
152 #endif
153         movzbl  PSHARED(%ebx), %ecx
154         call    __lll_unlock_wake
155         jmp     7b
157         /* Overflow.  */
158 8:      subl    $1, NR_READERS(%ebx)
159         movl    $EAGAIN, %edx
160         jmp     9b
162         /* Overflow.  */
163 4:      subl    $1, READERS_QUEUED(%ebx)
164         movl    $EAGAIN, %edx
165         jmp     9b
168 #if MUTEX == 0
169         movl    %ebx, %eax
170 #else
171         leal    MUTEX(%ebx), %eax
172 #endif
173         movzbl  PSHARED(%ebx), %ecx
174         call    __lll_unlock_wake
175         jmp     11b
178 #if MUTEX == 0
179         movl    %ebx, %edx
180 #else
181         leal    MUTEX(%ebx), %edx
182 #endif
183         movzbl  PSHARED(%ebx), %ecx
184         call    __lll_lock_wait
185         jmp     13b
186         cfi_endproc
187         .size   __pthread_rwlock_rdlock,.-__pthread_rwlock_rdlock
189         .globl  pthread_rwlock_rdlock
190 pthread_rwlock_rdlock = __pthread_rwlock_rdlock
192         .globl  __pthread_rwlock_rdlock_internal
193 __pthread_rwlock_rdlock_internal = __pthread_rwlock_rdlock