Fix ecvt_r, fcvt_r namespace (bug 18522).
[glibc.git] / nptl / pthread_rwlock_timedwrlock.c
blobc54253450b9339c1afcc6084c1dd54a6d20f0c09
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 write lock for RWLOCK or return after specfied time. */
30 int
31 pthread_rwlock_timedwrlock (rwlock, abstime)
32 pthread_rwlock_t *rwlock;
33 const struct timespec *abstime;
35 int result = 0;
36 bool wake_readers = 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 and no reader. */
46 if (rwlock->__data.__writer == 0 && rwlock->__data.__nr_readers == 0)
48 /* Mark self as writer. */
49 rwlock->__data.__writer = THREAD_GETMEM (THREAD_SELF, tid);
50 break;
53 /* Make sure we are not holding the rwlock as a writer. This is
54 a deadlock situation we recognize and report. */
55 if (__builtin_expect (rwlock->__data.__writer
56 == THREAD_GETMEM (THREAD_SELF, tid), 0))
58 result = EDEADLK;
59 break;
62 /* Make sure the passed in timeout value is valid. Ideally this
63 test would be executed once. But since it must not be
64 performed if we would not block at all simply moving the test
65 to the front is no option. Replicating all the code is
66 costly while this test is not. */
67 if (__builtin_expect (abstime->tv_nsec >= 1000000000
68 || abstime->tv_nsec < 0, 0))
70 result = EINVAL;
71 break;
74 /* Work around the fact that the kernel rejects negative timeout values
75 despite them being valid. */
76 if (__glibc_unlikely (abstime->tv_sec < 0))
78 result = ETIMEDOUT;
79 break;
82 #if (!defined __ASSUME_FUTEX_CLOCK_REALTIME \
83 || !defined lll_futex_timed_wait_bitset)
84 /* Get the current time. So far we support only one clock. */
85 struct timeval tv;
86 (void) __gettimeofday (&tv, NULL);
88 /* Convert the absolute timeout value to a relative timeout. */
89 struct timespec rt;
90 rt.tv_sec = abstime->tv_sec - tv.tv_sec;
91 rt.tv_nsec = abstime->tv_nsec - tv.tv_usec * 1000;
92 if (rt.tv_nsec < 0)
94 rt.tv_nsec += 1000000000;
95 --rt.tv_sec;
97 /* Did we already time out? */
98 if (rt.tv_sec < 0)
100 result = ETIMEDOUT;
101 break;
103 #endif
105 /* Remember that we are a writer. */
106 if (++rwlock->__data.__nr_writers_queued == 0)
108 /* Overflow on number of queued writers. */
109 --rwlock->__data.__nr_writers_queued;
110 result = EAGAIN;
111 break;
114 int waitval = rwlock->__data.__writer_wakeup;
116 /* Free the lock. */
117 lll_unlock (rwlock->__data.__lock, rwlock->__data.__shared);
119 /* Wait for the writer or reader(s) to finish. */
120 #if (!defined __ASSUME_FUTEX_CLOCK_REALTIME \
121 || !defined lll_futex_timed_wait_bitset)
122 err = lll_futex_timed_wait (&rwlock->__data.__writer_wakeup,
123 waitval, &rt, rwlock->__data.__shared);
124 #else
125 err = lll_futex_timed_wait_bitset (&rwlock->__data.__writer_wakeup,
126 waitval, abstime,
127 FUTEX_CLOCK_REALTIME,
128 rwlock->__data.__shared);
129 #endif
131 /* Get the lock. */
132 lll_lock (rwlock->__data.__lock, rwlock->__data.__shared);
134 /* To start over again, remove the thread from the writer list. */
135 --rwlock->__data.__nr_writers_queued;
137 /* Did the futex call time out? */
138 if (err == -ETIMEDOUT)
140 result = ETIMEDOUT;
141 /* If we prefer writers, it can have happened that readers blocked
142 for us to acquire the lock first. If we have timed out, we need
143 to wake such readers if there are any, and if there is no writer
144 currently (otherwise, the writer will take care of wake-up).
145 Likewise, even if we prefer readers, we can be responsible for
146 wake-up (see pthread_rwlock_unlock) if no reader or writer has
147 acquired the lock. We have timed out and thus not consumed a
148 futex wake-up; therefore, if there is no other blocked writer
149 that would consume the wake-up and thus take over responsibility,
150 we need to wake blocked readers. */
151 if ((!PTHREAD_RWLOCK_PREFER_READER_P (rwlock)
152 || ((rwlock->__data.__nr_readers == 0)
153 && (rwlock->__data.__nr_writers_queued == 0)))
154 && (rwlock->__data.__nr_readers_queued > 0)
155 && (rwlock->__data.__writer == 0))
157 ++rwlock->__data.__readers_wakeup;
158 wake_readers = true;
160 break;
164 /* We are done, free the lock. */
165 lll_unlock (rwlock->__data.__lock, rwlock->__data.__shared);
167 /* Might be required after timeouts. */
168 if (wake_readers)
169 lll_futex_wake (&rwlock->__data.__readers_wakeup, INT_MAX,
170 rwlock->__data.__shared);
172 return result;