Include <kernel-features.h> explicitly where required.
[platform/upstream/glibc.git] / nptl / pthread_rwlock_timedrdlock.c
1 /* Copyright (C) 2003-2014 Free Software Foundation, Inc.
2    This file is part of the GNU C Library.
3    Contributed by Martin Schwidefsky <schwidefsky@de.ibm.com>, 2003.
4
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.
9
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.
14
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/>.  */
18
19 #include <errno.h>
20 #include <sysdep.h>
21 #include <lowlevellock.h>
22 #include <pthread.h>
23 #include <pthreadP.h>
24 #include <kernel-features.h>
25
26
27 /* Try to acquire read lock for RWLOCK or return after specfied time.  */
28 int
29 pthread_rwlock_timedrdlock (rwlock, abstime)
30      pthread_rwlock_t *rwlock;
31      const struct timespec *abstime;
32 {
33   int result = 0;
34
35   /* Make sure we are alone.  */
36   lll_lock(rwlock->__data.__lock, rwlock->__data.__shared);
37
38   while (1)
39     {
40       int err;
41
42       /* Get the rwlock if there is no writer...  */
43       if (rwlock->__data.__writer == 0
44           /* ...and if either no writer is waiting or we prefer readers.  */
45           && (!rwlock->__data.__nr_writers_queued
46               || PTHREAD_RWLOCK_PREFER_READER_P (rwlock)))
47         {
48           /* Increment the reader counter.  Avoid overflow.  */
49           if (++rwlock->__data.__nr_readers == 0)
50             {
51               /* Overflow on number of readers.  */
52               --rwlock->__data.__nr_readers;
53               result = EAGAIN;
54             }
55
56           break;
57         }
58
59       /* Make sure we are not holding the rwlock as a writer.  This is
60          a deadlock situation we recognize and report.  */
61       if (__builtin_expect (rwlock->__data.__writer
62                             == THREAD_GETMEM (THREAD_SELF, tid), 0))
63         {
64           result = EDEADLK;
65           break;
66         }
67
68       /* Make sure the passed in timeout value is valid.  Ideally this
69          test would be executed once.  But since it must not be
70          performed if we would not block at all simply moving the test
71          to the front is no option.  Replicating all the code is
72          costly while this test is not.  */
73       if (__builtin_expect (abstime->tv_nsec >= 1000000000
74                             || abstime->tv_nsec < 0, 0))
75         {
76           result = EINVAL;
77           break;
78         }
79
80       /* Work around the fact that the kernel rejects negative timeout values
81          despite them being valid.  */
82       if (__glibc_unlikely (abstime->tv_sec < 0))
83         {
84           result = ETIMEDOUT;
85           break;
86         }
87
88 #if (!defined __ASSUME_FUTEX_CLOCK_REALTIME \
89      || !defined lll_futex_timed_wait_bitset)
90       /* Get the current time.  So far we support only one clock.  */
91       struct timeval tv;
92       (void) gettimeofday (&tv, NULL);
93
94       /* Convert the absolute timeout value to a relative timeout.  */
95       struct timespec rt;
96       rt.tv_sec = abstime->tv_sec - tv.tv_sec;
97       rt.tv_nsec = abstime->tv_nsec - tv.tv_usec * 1000;
98       if (rt.tv_nsec < 0)
99         {
100           rt.tv_nsec += 1000000000;
101           --rt.tv_sec;
102         }
103       /* Did we already time out?  */
104       if (rt.tv_sec < 0)
105         {
106           /* Yep, return with an appropriate error.  */
107           result = ETIMEDOUT;
108           break;
109         }
110 #endif
111
112       /* Remember that we are a reader.  */
113       if (++rwlock->__data.__nr_readers_queued == 0)
114         {
115           /* Overflow on number of queued readers.  */
116           --rwlock->__data.__nr_readers_queued;
117           result = EAGAIN;
118           break;
119         }
120
121       int waitval = rwlock->__data.__readers_wakeup;
122
123       /* Free the lock.  */
124       lll_unlock (rwlock->__data.__lock, rwlock->__data.__shared);
125
126       /* Wait for the writer to finish.  */
127 #if (!defined __ASSUME_FUTEX_CLOCK_REALTIME \
128      || !defined lll_futex_timed_wait_bitset)
129       err = lll_futex_timed_wait (&rwlock->__data.__readers_wakeup,
130                                   waitval, &rt, rwlock->__data.__shared);
131 #else
132       err = lll_futex_timed_wait_bitset (&rwlock->__data.__readers_wakeup,
133                                          waitval, abstime,
134                                          FUTEX_CLOCK_REALTIME,
135                                          rwlock->__data.__shared);
136 #endif
137
138       /* Get the lock.  */
139       lll_lock (rwlock->__data.__lock, rwlock->__data.__shared);
140
141       --rwlock->__data.__nr_readers_queued;
142
143       /* Did the futex call time out?  */
144       if (err == -ETIMEDOUT)
145         {
146           /* Yep, report it.  */
147           result = ETIMEDOUT;
148           break;
149         }
150     }
151
152   /* We are done, free the lock.  */
153   lll_unlock (rwlock->__data.__lock, rwlock->__data.__shared);
154
155   return result;
156 }