Remove i486 subdirectory
[glibc.git] / nptl / pthread_rwlock_timedrdlock.c
blob93d235e9e1cf03241f79dce2714574c70c1ada15
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 <futex-internal.h>
23 #include <pthread.h>
24 #include <pthreadP.h>
25 #include <sys/time.h>
26 #include <stdbool.h>
29 /* Try to acquire read lock for RWLOCK or return after specfied time. */
30 int
31 pthread_rwlock_timedrdlock (rwlock, abstime)
32 pthread_rwlock_t *rwlock;
33 const struct timespec *abstime;
35 int result = 0;
36 bool wake = false;
37 int futex_shared =
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);
43 while (1)
45 int err;
47 /* Get the rwlock if there is no writer... */
48 if (rwlock->__data.__writer == 0
49 /* ...and if either no writer is waiting or we prefer readers. */
50 && (!rwlock->__data.__nr_writers_queued
51 || PTHREAD_RWLOCK_PREFER_READER_P (rwlock)))
53 /* Increment the reader counter. Avoid overflow. */
54 if (++rwlock->__data.__nr_readers == 0)
56 /* Overflow on number of readers. */
57 --rwlock->__data.__nr_readers;
58 result = EAGAIN;
60 else
62 /* See pthread_rwlock_rdlock. */
63 if (rwlock->__data.__nr_readers == 1
64 && rwlock->__data.__nr_readers_queued > 0
65 && rwlock->__data.__nr_writers_queued > 0)
67 ++rwlock->__data.__readers_wakeup;
68 wake = true;
72 break;
75 /* Make sure we are not holding the rwlock as a writer. This is
76 a deadlock situation we recognize and report. */
77 if (__builtin_expect (rwlock->__data.__writer
78 == THREAD_GETMEM (THREAD_SELF, tid), 0))
80 result = EDEADLK;
81 break;
84 /* Make sure the passed in timeout value is valid. Ideally this
85 test would be executed once. But since it must not be
86 performed if we would not block at all simply moving the test
87 to the front is no option. Replicating all the code is
88 costly while this test is not. */
89 if (__builtin_expect (abstime->tv_nsec >= 1000000000
90 || abstime->tv_nsec < 0, 0))
92 result = EINVAL;
93 break;
96 /* Remember that we are a reader. */
97 if (++rwlock->__data.__nr_readers_queued == 0)
99 /* Overflow on number of queued readers. */
100 --rwlock->__data.__nr_readers_queued;
101 result = EAGAIN;
102 break;
105 int waitval = rwlock->__data.__readers_wakeup;
107 /* Free the lock. */
108 lll_unlock (rwlock->__data.__lock, rwlock->__data.__shared);
110 /* Wait for the writer to finish. We handle ETIMEDOUT below; on other
111 return values, we decide how to continue based on the state of the
112 rwlock. */
113 err = futex_abstimed_wait (&rwlock->__data.__readers_wakeup, waitval,
114 abstime, futex_shared);
116 /* Get the lock. */
117 lll_lock (rwlock->__data.__lock, rwlock->__data.__shared);
119 --rwlock->__data.__nr_readers_queued;
121 /* Did the futex call time out? */
122 if (err == ETIMEDOUT)
124 /* Yep, report it. */
125 result = ETIMEDOUT;
126 break;
130 /* We are done, free the lock. */
131 lll_unlock (rwlock->__data.__lock, rwlock->__data.__shared);
133 if (wake)
134 futex_wake (&rwlock->__data.__readers_wakeup, INT_MAX, futex_shared);
136 return result;