1 | /* Copyright (C) 2003-2016 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 <futex-internal.h> |
23 | #include <pthread.h> |
24 | #include <pthreadP.h> |
25 | #include <stap-probe.h> |
26 | #include <elide.h> |
27 | |
28 | |
29 | /* Acquire write lock for RWLOCK. */ |
30 | static int __attribute__((noinline)) |
31 | __pthread_rwlock_wrlock_slow (pthread_rwlock_t *rwlock) |
32 | { |
33 | int result = 0; |
34 | int futex_shared = |
35 | rwlock->__data.__shared == LLL_PRIVATE ? FUTEX_PRIVATE : FUTEX_SHARED; |
36 | |
37 | /* Caller has taken the lock. */ |
38 | |
39 | while (1) |
40 | { |
41 | /* Make sure we are not holding the rwlock as a writer. This is |
42 | a deadlock situation we recognize and report. */ |
43 | if (__builtin_expect (rwlock->__data.__writer |
44 | == THREAD_GETMEM (THREAD_SELF, tid), 0)) |
45 | { |
46 | result = EDEADLK; |
47 | break; |
48 | } |
49 | |
50 | /* Remember that we are a writer. */ |
51 | if (++rwlock->__data.__nr_writers_queued == 0) |
52 | { |
53 | /* Overflow on number of queued writers. */ |
54 | --rwlock->__data.__nr_writers_queued; |
55 | result = EAGAIN; |
56 | break; |
57 | } |
58 | |
59 | int waitval = rwlock->__data.__writer_wakeup; |
60 | |
61 | /* Free the lock. */ |
62 | lll_unlock (rwlock->__data.__lock, rwlock->__data.__shared); |
63 | |
64 | /* Wait for the writer or reader(s) to finish. We do not check the |
65 | return value because we decide how to continue based on the state of |
66 | the rwlock. */ |
67 | futex_wait_simple (&rwlock->__data.__writer_wakeup, waitval, |
68 | futex_shared); |
69 | |
70 | /* Get the lock. */ |
71 | lll_lock (rwlock->__data.__lock, rwlock->__data.__shared); |
72 | |
73 | /* To start over again, remove the thread from the writer list. */ |
74 | --rwlock->__data.__nr_writers_queued; |
75 | |
76 | /* Get the rwlock if there is no writer and no reader. */ |
77 | if (rwlock->__data.__writer == 0 && rwlock->__data.__nr_readers == 0) |
78 | { |
79 | /* Mark self as writer. */ |
80 | rwlock->__data.__writer = THREAD_GETMEM (THREAD_SELF, tid); |
81 | |
82 | LIBC_PROBE (wrlock_acquire_write, 1, rwlock); |
83 | break; |
84 | } |
85 | } |
86 | |
87 | /* We are done, free the lock. */ |
88 | lll_unlock (rwlock->__data.__lock, rwlock->__data.__shared); |
89 | |
90 | return result; |
91 | } |
92 | |
93 | /* Fast path of acquiring write lock for RWLOCK. */ |
94 | |
95 | int |
96 | __pthread_rwlock_wrlock (pthread_rwlock_t *rwlock) |
97 | { |
98 | LIBC_PROBE (wrlock_entry, 1, rwlock); |
99 | |
100 | if (ELIDE_LOCK (rwlock->__data.__rwelision, |
101 | rwlock->__data.__lock == 0 |
102 | && rwlock->__data.__writer == 0 |
103 | && rwlock->__data.__nr_readers == 0)) |
104 | return 0; |
105 | |
106 | /* Make sure we are alone. */ |
107 | lll_lock (rwlock->__data.__lock, rwlock->__data.__shared); |
108 | |
109 | /* Get the rwlock if there is no writer and no reader. */ |
110 | if (__glibc_likely((rwlock->__data.__writer | |
111 | rwlock->__data.__nr_readers) == 0)) |
112 | { |
113 | /* Mark self as writer. */ |
114 | rwlock->__data.__writer = THREAD_GETMEM (THREAD_SELF, tid); |
115 | |
116 | LIBC_PROBE (wrlock_acquire_write, 1, rwlock); |
117 | |
118 | /* We are done, free the lock. */ |
119 | lll_unlock (rwlock->__data.__lock, rwlock->__data.__shared); |
120 | |
121 | return 0; |
122 | } |
123 | |
124 | return __pthread_rwlock_wrlock_slow (rwlock); |
125 | } |
126 | |
127 | |
128 | weak_alias (__pthread_rwlock_wrlock, pthread_rwlock_wrlock) |
129 | hidden_def (__pthread_rwlock_wrlock) |
130 | |