iconv: Suppress array out of bounds warning.
[platform/upstream/glibc.git] / nptl / pthread_rwlock_timedwrlock.c
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.
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 <sys/time.h>
25 #include <kernel-features.h>
26
27
28 /* Try to acquire write lock for RWLOCK or return after specfied time.  */
29 int
30 pthread_rwlock_timedwrlock (rwlock, abstime)
31      pthread_rwlock_t *rwlock;
32      const struct timespec *abstime;
33 {
34   int result = 0;
35
36   /* Make sure we are alone.  */
37   lll_lock (rwlock->__data.__lock, rwlock->__data.__shared);
38
39   while (1)
40     {
41       int err;
42
43       /* Get the rwlock if there is no writer and no reader.  */
44       if (rwlock->__data.__writer == 0 && rwlock->__data.__nr_readers == 0)
45         {
46           /* Mark self as writer.  */
47           rwlock->__data.__writer = THREAD_GETMEM (THREAD_SELF, tid);
48           break;
49         }
50
51       /* Make sure we are not holding the rwlock as a writer.  This is
52          a deadlock situation we recognize and report.  */
53       if (__builtin_expect (rwlock->__data.__writer
54                             == THREAD_GETMEM (THREAD_SELF, tid), 0))
55         {
56           result = EDEADLK;
57           break;
58         }
59
60       /* Make sure the passed in timeout value is valid.  Ideally this
61          test would be executed once.  But since it must not be
62          performed if we would not block at all simply moving the test
63          to the front is no option.  Replicating all the code is
64          costly while this test is not.  */
65       if (__builtin_expect (abstime->tv_nsec >= 1000000000
66                             || abstime->tv_nsec < 0, 0))
67         {
68           result = EINVAL;
69           break;
70         }
71
72       /* Work around the fact that the kernel rejects negative timeout values
73          despite them being valid.  */
74       if (__glibc_unlikely (abstime->tv_sec < 0))
75         {
76           result = ETIMEDOUT;
77           break;
78         }
79
80 #if (!defined __ASSUME_FUTEX_CLOCK_REALTIME \
81      || !defined lll_futex_timed_wait_bitset)
82       /* Get the current time.  So far we support only one clock.  */
83       struct timeval tv;
84       (void) __gettimeofday (&tv, NULL);
85
86       /* Convert the absolute timeout value to a relative timeout.  */
87       struct timespec rt;
88       rt.tv_sec = abstime->tv_sec - tv.tv_sec;
89       rt.tv_nsec = abstime->tv_nsec - tv.tv_usec * 1000;
90       if (rt.tv_nsec < 0)
91         {
92           rt.tv_nsec += 1000000000;
93           --rt.tv_sec;
94         }
95       /* Did we already time out?  */
96       if (rt.tv_sec < 0)
97         {
98           result = ETIMEDOUT;
99           break;
100         }
101 #endif
102
103       /* Remember that we are a writer.  */
104       if (++rwlock->__data.__nr_writers_queued == 0)
105         {
106           /* Overflow on number of queued writers.  */
107           --rwlock->__data.__nr_writers_queued;
108           result = EAGAIN;
109           break;
110         }
111
112       int waitval = rwlock->__data.__writer_wakeup;
113
114       /* Free the lock.  */
115       lll_unlock (rwlock->__data.__lock, rwlock->__data.__shared);
116
117       /* Wait for the writer or reader(s) to finish.  */
118 #if (!defined __ASSUME_FUTEX_CLOCK_REALTIME \
119      || !defined lll_futex_timed_wait_bitset)
120       err = lll_futex_timed_wait (&rwlock->__data.__writer_wakeup,
121                                   waitval, &rt, rwlock->__data.__shared);
122 #else
123       err = lll_futex_timed_wait_bitset (&rwlock->__data.__writer_wakeup,
124                                          waitval, abstime,
125                                          FUTEX_CLOCK_REALTIME,
126                                          rwlock->__data.__shared);
127 #endif
128
129       /* Get the lock.  */
130       lll_lock (rwlock->__data.__lock, rwlock->__data.__shared);
131
132       /* To start over again, remove the thread from the writer list.  */
133       --rwlock->__data.__nr_writers_queued;
134
135       /* Did the futex call time out?  */
136       if (err == -ETIMEDOUT)
137         {
138           result = ETIMEDOUT;
139           break;
140         }
141     }
142
143   /* We are done, free the lock.  */
144   lll_unlock (rwlock->__data.__lock, rwlock->__data.__shared);
145
146   return result;
147 }