1/* Copyright (C) 2003-2016 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 <futex-internal.h>
23#include <pthread.h>
24#include <pthreadP.h>
25#include <sys/time.h>
26#include <stdbool.h>
27
28
29/* Try to acquire read lock for RWLOCK or return after specfied time. */
30int
31pthread_rwlock_timedrdlock (pthread_rwlock_t *rwlock,
32 const struct timespec *abstime)
33{
34 int result = 0;
35 bool wake = false;
36 int futex_shared =
37 rwlock->__data.__shared == LLL_PRIVATE ? FUTEX_PRIVATE : FUTEX_SHARED;
38
39 /* Make sure we are alone. */
40 lll_lock(rwlock->__data.__lock, rwlock->__data.__shared);
41
42 while (1)
43 {
44 int err;
45
46 /* Get the rwlock if there is no writer... */
47 if (rwlock->__data.__writer == 0
48 /* ...and if either no writer is waiting or we prefer readers. */
49 && (!rwlock->__data.__nr_writers_queued
50 || PTHREAD_RWLOCK_PREFER_READER_P (rwlock)))
51 {
52 /* Increment the reader counter. Avoid overflow. */
53 if (++rwlock->__data.__nr_readers == 0)
54 {
55 /* Overflow on number of readers. */
56 --rwlock->__data.__nr_readers;
57 result = EAGAIN;
58 }
59 else
60 {
61 /* See pthread_rwlock_rdlock. */
62 if (rwlock->__data.__nr_readers == 1
63 && rwlock->__data.__nr_readers_queued > 0
64 && rwlock->__data.__nr_writers_queued > 0)
65 {
66 ++rwlock->__data.__readers_wakeup;
67 wake = true;
68 }
69 }
70
71 break;
72 }
73
74 /* Make sure we are not holding the rwlock as a writer. This is
75 a deadlock situation we recognize and report. */
76 if (__builtin_expect (rwlock->__data.__writer
77 == THREAD_GETMEM (THREAD_SELF, tid), 0))
78 {
79 result = EDEADLK;
80 break;
81 }
82
83 /* Make sure the passed in timeout value is valid. Ideally this
84 test would be executed once. But since it must not be
85 performed if we would not block at all simply moving the test
86 to the front is no option. Replicating all the code is
87 costly while this test is not. */
88 if (__builtin_expect (abstime->tv_nsec >= 1000000000
89 || abstime->tv_nsec < 0, 0))
90 {
91 result = EINVAL;
92 break;
93 }
94
95 /* Remember that we are a reader. */
96 if (++rwlock->__data.__nr_readers_queued == 0)
97 {
98 /* Overflow on number of queued readers. */
99 --rwlock->__data.__nr_readers_queued;
100 result = EAGAIN;
101 break;
102 }
103
104 int waitval = rwlock->__data.__readers_wakeup;
105
106 /* Free the lock. */
107 lll_unlock (rwlock->__data.__lock, rwlock->__data.__shared);
108
109 /* Wait for the writer to finish. We handle ETIMEDOUT below; on other
110 return values, we decide how to continue based on the state of the
111 rwlock. */
112 err = futex_abstimed_wait (&rwlock->__data.__readers_wakeup, waitval,
113 abstime, futex_shared);
114
115 /* Get the lock. */
116 lll_lock (rwlock->__data.__lock, rwlock->__data.__shared);
117
118 --rwlock->__data.__nr_readers_queued;
119
120 /* Did the futex call time out? */
121 if (err == ETIMEDOUT)
122 {
123 /* Yep, report it. */
124 result = ETIMEDOUT;
125 break;
126 }
127 }
128
129 /* We are done, free the lock. */
130 lll_unlock (rwlock->__data.__lock, rwlock->__data.__shared);
131
132 if (wake)
133 futex_wake (&rwlock->__data.__readers_wakeup, INT_MAX, futex_shared);
134
135 return result;
136}
137