Update copyright dates with scripts/update-copyrights.
[glibc.git] / nptl / pthread_rwlock_timedwrlock.c
blob416f6e2bd5bb289f8edffe6a10944e8669247fd9
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>
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;
34 int result = 0;
36 /* Make sure we are alone. */
37 lll_lock (rwlock->__data.__lock, rwlock->__data.__shared);
39 while (1)
41 int err;
43 /* Get the rwlock if there is no writer and no reader. */
44 if (rwlock->__data.__writer == 0 && rwlock->__data.__nr_readers == 0)
46 /* Mark self as writer. */
47 rwlock->__data.__writer = THREAD_GETMEM (THREAD_SELF, tid);
48 break;
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))
56 result = EDEADLK;
57 break;
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))
68 result = EINVAL;
69 break;
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))
76 result = ETIMEDOUT;
77 break;
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);
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)
92 rt.tv_nsec += 1000000000;
93 --rt.tv_sec;
95 /* Did we already time out? */
96 if (rt.tv_sec < 0)
98 result = ETIMEDOUT;
99 break;
101 #endif
103 /* Remember that we are a writer. */
104 if (++rwlock->__data.__nr_writers_queued == 0)
106 /* Overflow on number of queued writers. */
107 --rwlock->__data.__nr_writers_queued;
108 result = EAGAIN;
109 break;
112 int waitval = rwlock->__data.__writer_wakeup;
114 /* Free the lock. */
115 lll_unlock (rwlock->__data.__lock, rwlock->__data.__shared);
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
129 /* Get the lock. */
130 lll_lock (rwlock->__data.__lock, rwlock->__data.__shared);
132 /* To start over again, remove the thread from the writer list. */
133 --rwlock->__data.__nr_writers_queued;
135 /* Did the futex call time out? */
136 if (err == -ETIMEDOUT)
138 result = ETIMEDOUT;
139 break;
143 /* We are done, free the lock. */
144 lll_unlock (rwlock->__data.__lock, rwlock->__data.__shared);
146 return result;