universally apply our cflags (no vsx, no altivec..)
[glibc.git] / fbtl / pthread_rwlock_rdlock.c
1 /* Copyright (C) 2003-2013 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 <pthread.h>
23 #include <pthreadP.h>
24 #include <stap-probe.h>
25
26
27 /* Acquire read lock for RWLOCK. */
28 int
29 __pthread_rwlock_rdlock (pthread_rwlock_t *rwlock)
30 {
31 int result = 0;
32
33 LIBC_PROBE (rdlock_entry, 1, rwlock);
34
35 /* Make sure we are alone. */
36 lll_lock (rwlock->__data.__lock, rwlock->__data.__shared);
37
38 while (1)
39 {
40 /* Get the rwlock if there is no writer... */
41 if (rwlock->__data.__writer == 0
42 /* ...and if either no writer is waiting or we prefer readers. */
43 && (!rwlock->__data.__nr_writers_queued
44 || PTHREAD_RWLOCK_PREFER_READER_P (rwlock)))
45 {
46 /* Increment the reader counter. Avoid overflow. */
47 if (__glibc_unlikely (++rwlock->__data.__nr_readers == 0))
48 {
49 /* Overflow on number of readers. */
50 --rwlock->__data.__nr_readers;
51 result = EAGAIN;
52 }
53 else
54 LIBC_PROBE (rdlock_acquire_read, 1, rwlock);
55
56 break;
57 }
58
59 /* Make sure we are not holding the rwlock as a writer. This is
60 a deadlock situation we recognize and report. */
61 if (__builtin_expect (rwlock->__data.__writer
62 == THREAD_GETMEM (THREAD_SELF, tid), 0))
63 {
64 result = EDEADLK;
65 break;
66 }
67
68 /* Remember that we are a reader. */
69 if (__glibc_unlikely (++rwlock->__data.__nr_readers_queued == 0))
70 {
71 /* Overflow on number of queued readers. */
72 --rwlock->__data.__nr_readers_queued;
73 result = EAGAIN;
74 break;
75 }
76
77 int waitval = rwlock->__data.__readers_wakeup;
78
79 /* Free the lock. */
80 lll_unlock (rwlock->__data.__lock, rwlock->__data.__shared);
81
82 /* Wait for the writer to finish. */
83 lll_futex_wait (&rwlock->__data.__readers_wakeup, waitval,
84 rwlock->__data.__shared);
85
86 /* Get the lock. */
87 lll_lock (rwlock->__data.__lock, rwlock->__data.__shared);
88
89 --rwlock->__data.__nr_readers_queued;
90 }
91
92 /* We are done, free the lock. */
93 lll_unlock (rwlock->__data.__lock, rwlock->__data.__shared);
94
95 return result;
96 }
97
98 weak_alias (__pthread_rwlock_rdlock, pthread_rwlock_rdlock)
99 hidden_def (__pthread_rwlock_rdlock)