struct stat is not posix conform
[glibc.git] / nptl / pthread_rwlock_rdlock.c
blob0edca654a3181d8e466ec8f054c11cb41a2874a4
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 <stap-probe.h>
25 #include <elide.h>
28 /* Acquire read lock for RWLOCK. Slow path. */
29 static int __attribute__((noinline))
30 __pthread_rwlock_rdlock_slow (pthread_rwlock_t *rwlock)
32 int result = 0;
34 /* Lock is taken in caller. */
36 while (1)
38 /* Make sure we are not holding the rwlock as a writer. This is
39 a deadlock situation we recognize and report. */
40 if (__builtin_expect (rwlock->__data.__writer
41 == THREAD_GETMEM (THREAD_SELF, tid), 0))
43 result = EDEADLK;
44 break;
47 /* Remember that we are a reader. */
48 if (__glibc_unlikely (++rwlock->__data.__nr_readers_queued == 0))
50 /* Overflow on number of queued readers. */
51 --rwlock->__data.__nr_readers_queued;
52 result = EAGAIN;
53 break;
56 int waitval = rwlock->__data.__readers_wakeup;
58 /* Free the lock. */
59 lll_unlock (rwlock->__data.__lock, rwlock->__data.__shared);
61 /* Wait for the writer to finish. */
62 lll_futex_wait (&rwlock->__data.__readers_wakeup, waitval,
63 rwlock->__data.__shared);
65 /* Get the lock. */
66 lll_lock (rwlock->__data.__lock, rwlock->__data.__shared);
68 --rwlock->__data.__nr_readers_queued;
70 /* Get the rwlock if there is no writer... */
71 if (rwlock->__data.__writer == 0
72 /* ...and if either no writer is waiting or we prefer readers. */
73 && (!rwlock->__data.__nr_writers_queued
74 || PTHREAD_RWLOCK_PREFER_READER_P (rwlock)))
76 /* Increment the reader counter. Avoid overflow. */
77 if (__glibc_unlikely (++rwlock->__data.__nr_readers == 0))
79 /* Overflow on number of readers. */
80 --rwlock->__data.__nr_readers;
81 result = EAGAIN;
83 else
84 LIBC_PROBE (rdlock_acquire_read, 1, rwlock);
86 break;
90 /* We are done, free the lock. */
91 lll_unlock (rwlock->__data.__lock, rwlock->__data.__shared);
93 return result;
97 /* Fast path of acquiring read lock on RWLOCK. */
99 int
100 __pthread_rwlock_rdlock (pthread_rwlock_t *rwlock)
102 int result = 0;
104 LIBC_PROBE (rdlock_entry, 1, rwlock);
106 if (ELIDE_LOCK (rwlock->__data.__rwelision,
107 rwlock->__data.__lock == 0
108 && rwlock->__data.__writer == 0
109 && rwlock->__data.__nr_readers == 0))
110 return 0;
112 /* Make sure we are alone. */
113 lll_lock (rwlock->__data.__lock, rwlock->__data.__shared);
115 /* Get the rwlock if there is no writer... */
116 if (rwlock->__data.__writer == 0
117 /* ...and if either no writer is waiting or we prefer readers. */
118 && (!rwlock->__data.__nr_writers_queued
119 || PTHREAD_RWLOCK_PREFER_READER_P (rwlock)))
121 /* Increment the reader counter. Avoid overflow. */
122 if (__glibc_unlikely (++rwlock->__data.__nr_readers == 0))
124 /* Overflow on number of readers. */
125 --rwlock->__data.__nr_readers;
126 result = EAGAIN;
128 else
129 LIBC_PROBE (rdlock_acquire_read, 1, rwlock);
131 /* We are done, free the lock. */
132 lll_unlock (rwlock->__data.__lock, rwlock->__data.__shared);
134 return result;
137 return __pthread_rwlock_rdlock_slow (rwlock);
140 weak_alias (__pthread_rwlock_rdlock, pthread_rwlock_rdlock)
141 hidden_def (__pthread_rwlock_rdlock)