Fix asinh missing underflows (bug 16350).
[glibc.git] / nptl / pthread_rwlock_timedrdlock.c
blob63fb313762d44c411726fa5d652e1d74314c151d
1 /* Copyright (C) 2003-2015 Free Software Foundation, Inc.
2 This file is part of the GNU C Library.
3 Contributed by Martin Schwidefsky <schwidefsky@de.ibm.com>, 2003.
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, see
17 <http://www.gnu.org/licenses/>. */
19 #include <errno.h>
20 #include <sysdep.h>
21 #include <lowlevellock.h>
22 #include <pthread.h>
23 #include <pthreadP.h>
24 #include <sys/time.h>
25 #include <kernel-features.h>
26 #include <stdbool.h>
29 /* Try to acquire read lock for RWLOCK or return after specfied time. */
30 int
31 pthread_rwlock_timedrdlock (rwlock, abstime)
32 pthread_rwlock_t *rwlock;
33 const struct timespec *abstime;
35 int result = 0;
36 bool wake = false;
38 /* Make sure we are alone. */
39 lll_lock(rwlock->__data.__lock, rwlock->__data.__shared);
41 while (1)
43 int err;
45 /* Get the rwlock if there is no writer... */
46 if (rwlock->__data.__writer == 0
47 /* ...and if either no writer is waiting or we prefer readers. */
48 && (!rwlock->__data.__nr_writers_queued
49 || PTHREAD_RWLOCK_PREFER_READER_P (rwlock)))
51 /* Increment the reader counter. Avoid overflow. */
52 if (++rwlock->__data.__nr_readers == 0)
54 /* Overflow on number of readers. */
55 --rwlock->__data.__nr_readers;
56 result = EAGAIN;
58 else
60 /* See pthread_rwlock_rdlock. */
61 if (rwlock->__data.__nr_readers == 1
62 && rwlock->__data.__nr_readers_queued > 0
63 && rwlock->__data.__nr_writers_queued > 0)
65 ++rwlock->__data.__readers_wakeup;
66 wake = true;
70 break;
73 /* Make sure we are not holding the rwlock as a writer. This is
74 a deadlock situation we recognize and report. */
75 if (__builtin_expect (rwlock->__data.__writer
76 == THREAD_GETMEM (THREAD_SELF, tid), 0))
78 result = EDEADLK;
79 break;
82 /* Make sure the passed in timeout value is valid. Ideally this
83 test would be executed once. But since it must not be
84 performed if we would not block at all simply moving the test
85 to the front is no option. Replicating all the code is
86 costly while this test is not. */
87 if (__builtin_expect (abstime->tv_nsec >= 1000000000
88 || abstime->tv_nsec < 0, 0))
90 result = EINVAL;
91 break;
94 /* Work around the fact that the kernel rejects negative timeout values
95 despite them being valid. */
96 if (__glibc_unlikely (abstime->tv_sec < 0))
98 result = ETIMEDOUT;
99 break;
102 #if (!defined __ASSUME_FUTEX_CLOCK_REALTIME \
103 || !defined lll_futex_timed_wait_bitset)
104 /* Get the current time. So far we support only one clock. */
105 struct timeval tv;
106 (void) __gettimeofday (&tv, NULL);
108 /* Convert the absolute timeout value to a relative timeout. */
109 struct timespec rt;
110 rt.tv_sec = abstime->tv_sec - tv.tv_sec;
111 rt.tv_nsec = abstime->tv_nsec - tv.tv_usec * 1000;
112 if (rt.tv_nsec < 0)
114 rt.tv_nsec += 1000000000;
115 --rt.tv_sec;
117 /* Did we already time out? */
118 if (rt.tv_sec < 0)
120 /* Yep, return with an appropriate error. */
121 result = ETIMEDOUT;
122 break;
124 #endif
126 /* Remember that we are a reader. */
127 if (++rwlock->__data.__nr_readers_queued == 0)
129 /* Overflow on number of queued readers. */
130 --rwlock->__data.__nr_readers_queued;
131 result = EAGAIN;
132 break;
135 int waitval = rwlock->__data.__readers_wakeup;
137 /* Free the lock. */
138 lll_unlock (rwlock->__data.__lock, rwlock->__data.__shared);
140 /* Wait for the writer to finish. */
141 #if (!defined __ASSUME_FUTEX_CLOCK_REALTIME \
142 || !defined lll_futex_timed_wait_bitset)
143 err = lll_futex_timed_wait (&rwlock->__data.__readers_wakeup,
144 waitval, &rt, rwlock->__data.__shared);
145 #else
146 err = lll_futex_timed_wait_bitset (&rwlock->__data.__readers_wakeup,
147 waitval, abstime,
148 FUTEX_CLOCK_REALTIME,
149 rwlock->__data.__shared);
150 #endif
152 /* Get the lock. */
153 lll_lock (rwlock->__data.__lock, rwlock->__data.__shared);
155 --rwlock->__data.__nr_readers_queued;
157 /* Did the futex call time out? */
158 if (err == -ETIMEDOUT)
160 /* Yep, report it. */
161 result = ETIMEDOUT;
162 break;
166 /* We are done, free the lock. */
167 lll_unlock (rwlock->__data.__lock, rwlock->__data.__shared);
169 if (wake)
170 lll_futex_wake (&rwlock->__data.__readers_wakeup, INT_MAX,
171 rwlock->__data.__shared);
173 return result;