]> git.ipfire.org Git - thirdparty/glibc.git/blame - nptl/pthread_rwlock_timedwrlock.c
Update copyright dates with scripts/update-copyrights.
[thirdparty/glibc.git] / nptl / pthread_rwlock_timedwrlock.c
CommitLineData
bfff8b1b 1/* Copyright (C) 2003-2017 Free Software Foundation, Inc.
a88c9263
UD
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
59ba27a6
PE
16 License along with the GNU C Library; if not, see
17 <http://www.gnu.org/licenses/>. */
a88c9263
UD
18
19#include <errno.h>
20#include <sysdep.h>
21#include <lowlevellock.h>
a2f0363f 22#include <futex-internal.h>
a88c9263
UD
23#include <pthread.h>
24#include <pthreadP.h>
6af246cf 25#include <sys/time.h>
3c9c61fe 26#include <stdbool.h>
a88c9263
UD
27
28
29/* Try to acquire write lock for RWLOCK or return after specfied time. */
30int
f63f2bfd
JM
31pthread_rwlock_timedwrlock (pthread_rwlock_t *rwlock,
32 const struct timespec *abstime)
a88c9263
UD
33{
34 int result = 0;
3c9c61fe 35 bool wake_readers = false;
a2f0363f
TR
36 int futex_shared =
37 rwlock->__data.__shared == LLL_PRIVATE ? FUTEX_PRIVATE : FUTEX_SHARED;
a88c9263 38
c5be0f71 39 /* Make sure we are alone. */
e51deae7 40 lll_lock (rwlock->__data.__lock, rwlock->__data.__shared);
a88c9263
UD
41
42 while (1)
43 {
be095ffa
UD
44 int err;
45
a88c9263 46 /* Get the rwlock if there is no writer and no reader. */
b80770b2 47 if (rwlock->__data.__writer == 0 && rwlock->__data.__nr_readers == 0)
a88c9263
UD
48 {
49 /* Mark self as writer. */
4ad0bbf4 50 rwlock->__data.__writer = THREAD_GETMEM (THREAD_SELF, tid);
a88c9263
UD
51 break;
52 }
53
54 /* Make sure we are not holding the rwlock as a writer. This is
55 a deadlock situation we recognize and report. */
4ad0bbf4
UD
56 if (__builtin_expect (rwlock->__data.__writer
57 == THREAD_GETMEM (THREAD_SELF, tid), 0))
a88c9263
UD
58 {
59 result = EDEADLK;
60 break;
61 }
62
63 /* Make sure the passed in timeout value is valid. Ideally this
64 test would be executed once. But since it must not be
65 performed if we would not block at all simply moving the test
66 to the front is no option. Replicating all the code is
67 costly while this test is not. */
46f4c578 68 if (__builtin_expect (abstime->tv_nsec >= 1000000000
f3a19754 69 || abstime->tv_nsec < 0, 0))
a88c9263
UD
70 {
71 result = EINVAL;
72 break;
73 }
74
a88c9263
UD
75 /* Remember that we are a writer. */
76 if (++rwlock->__data.__nr_writers_queued == 0)
77 {
78 /* Overflow on number of queued writers. */
79 --rwlock->__data.__nr_writers_queued;
80 result = EAGAIN;
81 break;
82 }
83
18a53579
UD
84 int waitval = rwlock->__data.__writer_wakeup;
85
a88c9263 86 /* Free the lock. */
e51deae7 87 lll_unlock (rwlock->__data.__lock, rwlock->__data.__shared);
a88c9263 88
a2f0363f
TR
89 /* Wait for the writer or reader(s) to finish. We handle ETIMEDOUT
90 below; on other return values, we decide how to continue based on
91 the state of the rwlock. */
92 err = futex_abstimed_wait (&rwlock->__data.__writer_wakeup, waitval,
93 abstime, futex_shared);
a88c9263
UD
94
95 /* Get the lock. */
e51deae7 96 lll_lock (rwlock->__data.__lock, rwlock->__data.__shared);
a88c9263
UD
97
98 /* To start over again, remove the thread from the writer list. */
99 --rwlock->__data.__nr_writers_queued;
a88c9263
UD
100
101 /* Did the futex call time out? */
a2f0363f 102 if (err == ETIMEDOUT)
a88c9263
UD
103 {
104 result = ETIMEDOUT;
3c9c61fe
TR
105 /* If we prefer writers, it can have happened that readers blocked
106 for us to acquire the lock first. If we have timed out, we need
107 to wake such readers if there are any, and if there is no writer
b634486d
TR
108 currently (otherwise, the writer will take care of wake-up).
109 Likewise, even if we prefer readers, we can be responsible for
110 wake-up (see pthread_rwlock_unlock) if no reader or writer has
111 acquired the lock. We have timed out and thus not consumed a
112 futex wake-up; therefore, if there is no other blocked writer
113 that would consume the wake-up and thus take over responsibility,
114 we need to wake blocked readers. */
115 if ((!PTHREAD_RWLOCK_PREFER_READER_P (rwlock)
116 || ((rwlock->__data.__nr_readers == 0)
117 && (rwlock->__data.__nr_writers_queued == 0)))
3c9c61fe
TR
118 && (rwlock->__data.__nr_readers_queued > 0)
119 && (rwlock->__data.__writer == 0))
120 {
121 ++rwlock->__data.__readers_wakeup;
122 wake_readers = true;
123 }
a88c9263
UD
124 break;
125 }
126 }
127
128 /* We are done, free the lock. */
e51deae7 129 lll_unlock (rwlock->__data.__lock, rwlock->__data.__shared);
a88c9263 130
3c9c61fe
TR
131 /* Might be required after timeouts. */
132 if (wake_readers)
a2f0363f 133 futex_wake (&rwlock->__data.__readers_wakeup, INT_MAX, futex_shared);
3c9c61fe 134
a88c9263
UD
135 return result;
136}