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/>. */
21 #include <lowlevellock.h>
22 #include <futex-internal.h>
29 /* Try to acquire write lock for RWLOCK or return after specfied time. */
31 pthread_rwlock_timedwrlock (rwlock
, abstime
)
32 pthread_rwlock_t
*rwlock
;
33 const struct timespec
*abstime
;
36 bool wake_readers
= false;
38 rwlock
->__data
.__shared
== LLL_PRIVATE
? FUTEX_PRIVATE
: FUTEX_SHARED
;
40 /* Make sure we are alone. */
41 lll_lock (rwlock
->__data
.__lock
, rwlock
->__data
.__shared
);
47 /* Get the rwlock if there is no writer and no reader. */
48 if (rwlock
->__data
.__writer
== 0 && rwlock
->__data
.__nr_readers
== 0)
50 /* Mark self as writer. */
51 rwlock
->__data
.__writer
= THREAD_GETMEM (THREAD_SELF
, tid
);
55 /* Make sure we are not holding the rwlock as a writer. This is
56 a deadlock situation we recognize and report. */
57 if (__builtin_expect (rwlock
->__data
.__writer
58 == THREAD_GETMEM (THREAD_SELF
, tid
), 0))
64 /* Make sure the passed in timeout value is valid. Ideally this
65 test would be executed once. But since it must not be
66 performed if we would not block at all simply moving the test
67 to the front is no option. Replicating all the code is
68 costly while this test is not. */
69 if (__builtin_expect (abstime
->tv_nsec
>= 1000000000
70 || abstime
->tv_nsec
< 0, 0))
76 /* Remember that we are a writer. */
77 if (++rwlock
->__data
.__nr_writers_queued
== 0)
79 /* Overflow on number of queued writers. */
80 --rwlock
->__data
.__nr_writers_queued
;
85 int waitval
= rwlock
->__data
.__writer_wakeup
;
88 lll_unlock (rwlock
->__data
.__lock
, rwlock
->__data
.__shared
);
90 /* Wait for the writer or reader(s) to finish. We handle ETIMEDOUT
91 below; on other return values, we decide how to continue based on
92 the state of the rwlock. */
93 err
= futex_abstimed_wait (&rwlock
->__data
.__writer_wakeup
, waitval
,
94 abstime
, futex_shared
);
97 lll_lock (rwlock
->__data
.__lock
, rwlock
->__data
.__shared
);
99 /* To start over again, remove the thread from the writer list. */
100 --rwlock
->__data
.__nr_writers_queued
;
102 /* Did the futex call time out? */
103 if (err
== ETIMEDOUT
)
106 /* If we prefer writers, it can have happened that readers blocked
107 for us to acquire the lock first. If we have timed out, we need
108 to wake such readers if there are any, and if there is no writer
109 currently (otherwise, the writer will take care of wake-up).
110 Likewise, even if we prefer readers, we can be responsible for
111 wake-up (see pthread_rwlock_unlock) if no reader or writer has
112 acquired the lock. We have timed out and thus not consumed a
113 futex wake-up; therefore, if there is no other blocked writer
114 that would consume the wake-up and thus take over responsibility,
115 we need to wake blocked readers. */
116 if ((!PTHREAD_RWLOCK_PREFER_READER_P (rwlock
)
117 || ((rwlock
->__data
.__nr_readers
== 0)
118 && (rwlock
->__data
.__nr_writers_queued
== 0)))
119 && (rwlock
->__data
.__nr_readers_queued
> 0)
120 && (rwlock
->__data
.__writer
== 0))
122 ++rwlock
->__data
.__readers_wakeup
;
129 /* We are done, free the lock. */
130 lll_unlock (rwlock
->__data
.__lock
, rwlock
->__data
.__shared
);
132 /* Might be required after timeouts. */
134 futex_wake (&rwlock
->__data
.__readers_wakeup
, INT_MAX
, futex_shared
);