* sysdeps/unix/sysv/linux/i386/i486/pthread_rwlock_timedrdlock.S
[glibc.git] / nptl / sysdeps / unix / sysv / linux / i386 / i486 / pthread_rwlock_timedwrlock.S
blobcb5bceefec6160a1aaf84429c04e08862815d060
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_timedwrlock
40         .type   pthread_rwlock_timedwrlock,@function
41         .align  16
42 pthread_rwlock_timedwrlock:
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, NR_READERS(%ebp)
67         je      5f
69         /* Check the value of the timeout parameter.  */
70 3:      cmpl    $1000000000, 4(%edi)
71         jae     19f
73         addl    $1, WRITERS_QUEUED(%ebp)
74         je      4f
76         movl    WRITERS_WAKEUP(%ebp), %esi
78         LOCK
79 #if MUTEX == 0
80         subl    $1, (%ebp)
81 #else
82         subl    $1, MUTEX(%ebp)
83 #endif
84         jne     10f
86         /* Get current time.  */
87 11:     movl    %esp, %ebx
88         xorl    %ecx, %ecx
89         movl    $SYS_gettimeofday, %eax
90         ENTER_KERNEL
92         /* Compute relative timeout.  */
93         movl    4(%esp), %eax
94         movl    $1000, %edx
95         mul     %edx            /* Milli seconds to nano seconds.  */
96         movl    (%edi), %ecx
97         movl    4(%edi), %edx
98         subl    (%esp), %ecx
99         subl    %eax, %edx
100         jns     15f
101         addl    $1000000000, %edx
102         subl    $1, %ecx
103 15:     testl   %ecx, %ecx
104         js      16f             /* Time is already up.  */
106         /* Futex call.  */
107         movl    %ecx, (%esp)    /* Store relative timeout.  */
108         movl    %edx, 4(%esp)
110         movl    %esi, %edx
111 #if __ASSUME_PRIVATE_FUTEX
112         movzbl  PSHARED(%ebp), %ecx
113         xorl    $FUTEX_PRIVATE_FLAG|FUTEX_WAIT, %ecx
114 #else
115         movzbl  PSHARED(%ebp), %ecx
116 # if FUTEX_WAIT != 0
117         orl     $FUTEX_WAIT, %ecx
118 # endif
119         xorl    %gs:PRIVATE_FUTEX, %ecx
120 #endif
121         movl    %esp, %esi
122         leal    WRITERS_WAKEUP(%ebp), %ebx
123         movl    $SYS_futex, %eax
124         ENTER_KERNEL
125         movl    %eax, %esi
128         /* Reget the lock.  */
129         movl    $1, %edx
130         xorl    %eax, %eax
131         LOCK
132 #if MUTEX == 0
133         cmpxchgl %edx, (%ebp)
134 #else
135         cmpxchgl %edx, MUTEX(%ebp)
136 #endif
137         jnz     12f
139 13:     subl    $1, WRITERS_QUEUED(%ebp)
140         cmpl    $-ETIMEDOUT, %esi
141         jne     2b
143 18:     movl    $ETIMEDOUT, %ecx
144         jmp     9f
147 5:      xorl    %ecx, %ecx
148         movl    %gs:TID, %eax
149         movl    %eax, WRITER(%ebp)
150 9:      LOCK
151 #if MUTEX == 0
152         subl    $1, (%ebp)
153 #else
154         subl    $1, MUTEX(%ebp)
155 #endif
156         jne     6f
158 7:      movl    %ecx, %eax
160         addl    $8, %esp
161         popl    %ebp
162         popl    %ebx
163         popl    %edi
164         popl    %esi
165         ret
168 #if MUTEX == 0
169         movl    %ebp, %ecx
170 #else
171         leal    MUTEX(%ebp), %ecx
172 #endif
173         call    __lll_mutex_lock_wait
174         jmp     2b
176 14:     cmpl    %gs:TID, %eax
177         jne     3b
178 20:     movl    $EDEADLK, %ecx
179         jmp     9b
182 #if MUTEX == 0
183         movl    %ebp, %eax
184 #else
185         leal    MUTEX(%ebp), %eax
186 #endif
187         call    __lll_mutex_unlock_wake
188         jmp     7b
190         /* Overflow.  */
191 4:      subl    $1, WRITERS_QUEUED(%ebp)
192         movl    $EAGAIN, %ecx
193         jmp     9b
196 #if MUTEX == 0
197         movl    %ebp, %eax
198 #else
199         leal    MUTEX(%ebp), %eax
200 #endif
201         call    __lll_mutex_unlock_wake
202         jmp     11b
205 #if MUTEX == 0
206         movl    %ebp, %ecx
207 #else
208         leal    MUTEX(%ebp), %ecx
209 #endif
210         call    __lll_mutex_lock_wait
211         jmp     13b
213 16:     movl    $-ETIMEDOUT, %esi
214         jmp     17b
216 19:     movl    $EINVAL, %ecx
217         jmp     9b
218         .size   pthread_rwlock_timedwrlock,.-pthread_rwlock_timedwrlock