Copy-edit NEWS and fixup ChangeLog entries.
[glibc.git] / nptl / pthread_rwlock_timedrdlock.c
blob86a1d29bcbe5783424ba34015702518cb493bbad
1 /* Copyright (C) 2003-2013 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>
26 /* Try to acquire read lock for RWLOCK or return after specfied time. */
27 int
28 pthread_rwlock_timedrdlock (rwlock, abstime)
29 pthread_rwlock_t *rwlock;
30 const struct timespec *abstime;
32 int result = 0;
34 /* Make sure we are alone. */
35 lll_lock(rwlock->__data.__lock, rwlock->__data.__shared);
37 while (1)
39 int err;
41 /* Get the rwlock if there is no writer... */
42 if (rwlock->__data.__writer == 0
43 /* ...and if either no writer is waiting or we prefer readers. */
44 && (!rwlock->__data.__nr_writers_queued
45 || PTHREAD_RWLOCK_PREFER_READER_P (rwlock)))
47 /* Increment the reader counter. Avoid overflow. */
48 if (++rwlock->__data.__nr_readers == 0)
50 /* Overflow on number of readers. */
51 --rwlock->__data.__nr_readers;
52 result = EAGAIN;
55 break;
58 /* Make sure we are not holding the rwlock as a writer. This is
59 a deadlock situation we recognize and report. */
60 if (__builtin_expect (rwlock->__data.__writer
61 == THREAD_GETMEM (THREAD_SELF, tid), 0))
63 result = EDEADLK;
64 break;
67 /* Make sure the passed in timeout value is valid. Ideally this
68 test would be executed once. But since it must not be
69 performed if we would not block at all simply moving the test
70 to the front is no option. Replicating all the code is
71 costly while this test is not. */
72 if (__builtin_expect (abstime->tv_nsec >= 1000000000
73 || abstime->tv_nsec < 0, 0))
75 result = EINVAL;
76 break;
79 /* Work around the fact that the kernel rejects negative timeout values
80 despite them being valid. */
81 if (__builtin_expect (abstime->tv_sec < 0, 0))
83 result = ETIMEDOUT;
84 break;
87 #if (!defined __ASSUME_FUTEX_CLOCK_REALTIME \
88 || !defined lll_futex_timed_wait_bitset)
89 /* Get the current time. So far we support only one clock. */
90 struct timeval tv;
91 (void) gettimeofday (&tv, NULL);
93 /* Convert the absolute timeout value to a relative timeout. */
94 struct timespec rt;
95 rt.tv_sec = abstime->tv_sec - tv.tv_sec;
96 rt.tv_nsec = abstime->tv_nsec - tv.tv_usec * 1000;
97 if (rt.tv_nsec < 0)
99 rt.tv_nsec += 1000000000;
100 --rt.tv_sec;
102 /* Did we already time out? */
103 if (rt.tv_sec < 0)
105 /* Yep, return with an appropriate error. */
106 result = ETIMEDOUT;
107 break;
109 #endif
111 /* Remember that we are a reader. */
112 if (++rwlock->__data.__nr_readers_queued == 0)
114 /* Overflow on number of queued readers. */
115 --rwlock->__data.__nr_readers_queued;
116 result = EAGAIN;
117 break;
120 int waitval = rwlock->__data.__readers_wakeup;
122 /* Free the lock. */
123 lll_unlock (rwlock->__data.__lock, rwlock->__data.__shared);
125 /* Wait for the writer to finish. */
126 #if (!defined __ASSUME_FUTEX_CLOCK_REALTIME \
127 || !defined lll_futex_timed_wait_bitset)
128 err = lll_futex_timed_wait (&rwlock->__data.__readers_wakeup,
129 waitval, &rt, rwlock->__data.__shared);
130 #else
131 err = lll_futex_timed_wait_bitset (&rwlock->__data.__readers_wakeup,
132 waitval, abstime,
133 FUTEX_CLOCK_REALTIME,
134 rwlock->__data.__shared);
135 #endif
137 /* Get the lock. */
138 lll_lock (rwlock->__data.__lock, rwlock->__data.__shared);
140 --rwlock->__data.__nr_readers_queued;
142 /* Did the futex call time out? */
143 if (err == -ETIMEDOUT)
145 /* Yep, report it. */
146 result = ETIMEDOUT;
147 break;
151 /* We are done, free the lock. */
152 lll_unlock (rwlock->__data.__lock, rwlock->__data.__shared);
154 return result;