* sysdeps/i386/tls.h (THREAD_GSCOPE_RESET_FLAG): Use explicit
[glibc.git] / nptl / sysdeps / unix / sysv / linux / i386 / i486 / pthread_rwlock_timedrdlock.S
blobc6a584fed0b7265794883e56c2312e078cc33e62
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 <lowlevelrwlock.h>
22 #include <pthread-errnos.h>
25 #define SYS_gettimeofday        __NR_gettimeofday
26 #define SYS_futex               240
27 #define FUTEX_WAIT              0
28 #define FUTEX_WAKE              1
30 #ifndef UP
31 # define LOCK lock
32 #else
33 # define LOCK
34 #endif
37         .text
39         .globl  pthread_rwlock_timedrdlock
40         .type   pthread_rwlock_timedrdlock,@function
41         .align  16
42 pthread_rwlock_timedrdlock:
43         pushl   %esi
44         pushl   %edi
45         pushl   %ebx
46         pushl   %ebp
47         subl    $8, %esp
49         movl    28(%esp), %ebp
50         movl    32(%esp), %edi
52         /* Get the lock.  */
53         movl    $1, %edx
54         xorl    %eax, %eax
55         LOCK
56 #if MUTEX == 0
57         cmpxchgl %edx, (%ebp)
58 #else
59         cmpxchgl %edx, MUTEX(%ebp)
60 #endif
61         jnz     1f
63 2:      movl    WRITER(%ebp), %eax
64         testl   %eax, %eax
65         jne     14f
66         cmpl    $0, WRITERS_QUEUED(%ebp)
67         je      5f
68         cmpb    $0, FLAGS(%ebp)
69         je      5f
71         /* Check the value of the timeout parameter.  */
72 3:      cmpl    $1000000000, 4(%edi)
73         jae     19f
75         addl    $1, READERS_QUEUED(%ebp)
76         je      4f
78         movl    READERS_WAKEUP(%ebp), %esi
80         LOCK
81 #if MUTEX == 0
82         subl    $1, (%ebp)
83 #else
84         subl    $1, MUTEX(%ebp)
85 #endif
86         jne     10f
88         /* Get current time.  */
89 11:     movl    %esp, %ebx
90         xorl    %ecx, %ecx
91         movl    $SYS_gettimeofday, %eax
92         ENTER_KERNEL
94         /* Compute relative timeout.  */
95         movl    4(%esp), %eax
96         movl    $1000, %edx
97         mul     %edx            /* Milli seconds to nano seconds.  */
98         movl    (%edi), %ecx
99         movl    4(%edi), %edx
100         subl    (%esp), %ecx
101         subl    %eax, %edx
102         jns     15f
103         addl    $1000000000, %edx
104         subl    $1, %ecx
105 15:     testl   %ecx, %ecx
106         js      16f             /* Time is already up.  */
108         /* Futex call.  */
109         movl    %ecx, (%esp)    /* Store relative timeout.  */
110         movl    %edx, 4(%esp)
112         movl    %esi, %edx
113 #if __ASSUME_PRIVATE_FUTEX
114         movzbl  PSHARED(%ebp), %ecx
115         xorl    $FUTEX_PRIVATE_FLAG|FUTEX_WAIT, %ecx
116 #else
117         movzbl  PSHARED(%ebp), %ecx
118 # if FUTEX_WAIT != 0
119         orl     $FUTEX_WAIT, %ecx
120 # endif
121         xorl    %gs:PRIVATE_FUTEX, %ecx
122 #endif
123         movl    %esp, %esi
124         leal    READERS_WAKEUP(%ebp), %ebx
125         movl    $SYS_futex, %eax
126         ENTER_KERNEL
127         movl    %eax, %ecx
130         /* Reget the lock.  */
131         movl    $1, %edx
132         xorl    %eax, %eax
133         LOCK
134 #if MUTEX == 0
135         cmpxchgl %edx, (%ebp)
136 #else
137         cmpxchgl %edx, MUTEX(%ebp)
138 #endif
139         jnz     12f
141 13:     subl    $1, READERS_QUEUED(%ebp)
142         cmpl    $-ETIMEDOUT, %ecx
143         jne     2b
145 18:     movl    $ETIMEDOUT, %ecx
146         jmp     9f
149 5:      xorl    %ecx, %ecx
150         addl    $1, NR_READERS(%ebp)
151         je      8f
152 9:      LOCK
153 #if MUTEX == 0
154         subl    $1, (%ebp)
155 #else
156         subl    $1, MUTEX(%ebp)
157 #endif
158         jne     6f
160 7:      movl    %ecx, %eax
162         addl    $8, %esp
163         popl    %ebp
164         popl    %ebx
165         popl    %edi
166         popl    %esi
167         ret
170 #if MUTEX == 0
171         movl    %ebp, %ecx
172 #else
173         leal    MUTEX(%ebp), %ecx
174 #endif
175         call    __lll_mutex_lock_wait
176         jmp     2b
178 14:     cmpl    %gs:TID, %eax
179         jne     3b
180         movl    $EDEADLK, %ecx
181         jmp     9b
184 #if MUTEX == 0
185         movl    %ebp, %eax
186 #else
187         leal    MUTEX(%ebp), %eax
188 #endif
189         call    __lll_mutex_unlock_wake
190         jmp     7b
192         /* Overflow.  */
193 8:      subl    $1, NR_READERS(%ebp)
194         movl    $EAGAIN, %ecx
195         jmp     9b
197         /* Overflow.  */
198 4:      subl    $1, READERS_QUEUED(%ebp)
199         movl    $EAGAIN, %ecx
200         jmp     9b
203 #if MUTEX == 0
204         movl    %ebp, %eax
205 #else
206         leal    MUTEX(%ebp), %eax
207 #endif
208         call    __lll_mutex_unlock_wake
209         jmp     11b
212 #if MUTEX == 0
213         movl    %ebp, %ecx
214 #else
215         leal    MUTEX(%ebp), %ecx
216 #endif
217         call    __lll_mutex_lock_wait
218         jmp     13b
220 16:     movl    $-ETIMEDOUT, %ecx
221         jmp     17b
223 19:     movl    $EINVAL, %ecx
224         jmp     9b
225         .size   pthread_rwlock_timedrdlock,.-pthread_rwlock_timedrdlock