]>
Commit | Line | Data |
---|---|---|
568035b7 | 1 | /* Copyright (C) 2003-2013 Free Software Foundation, Inc. |
a88c9263 UD |
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 | |
59ba27a6 PE |
16 | License along with the GNU C Library; if not, see |
17 | <http://www.gnu.org/licenses/>. */ | |
a88c9263 UD |
18 | |
19 | #include <endian.h> | |
20 | #include <errno.h> | |
21 | #include <sysdep.h> | |
22 | #include <lowlevellock.h> | |
23 | #include <pthread.h> | |
24 | #include <pthreadP.h> | |
25 | ||
26 | #include <shlib-compat.h> | |
5acf7263 | 27 | #include <stap-probe.h> |
a88c9263 UD |
28 | |
29 | ||
7ce5c164 UD |
30 | struct _condvar_cleanup_buffer |
31 | { | |
32 | int oldtype; | |
33 | pthread_cond_t *cond; | |
34 | pthread_mutex_t *mutex; | |
893a3511 | 35 | unsigned int bc_seq; |
7ce5c164 UD |
36 | }; |
37 | ||
3e976b96 | 38 | |
a88c9263 UD |
39 | void |
40 | __attribute__ ((visibility ("hidden"))) | |
41 | __condvar_cleanup (void *arg) | |
42 | { | |
7ce5c164 UD |
43 | struct _condvar_cleanup_buffer *cbuffer = |
44 | (struct _condvar_cleanup_buffer *) arg; | |
73f7c32c | 45 | unsigned int destroying; |
5bd8a249 | 46 | int pshared = (cbuffer->cond->__data.__mutex == (void *) ~0l) |
5acf7263 | 47 | ? LLL_SHARED : LLL_PRIVATE; |
a88c9263 UD |
48 | |
49 | /* We are going to modify shared data. */ | |
5bd8a249 | 50 | lll_lock (cbuffer->cond->__data.__lock, pshared); |
a88c9263 | 51 | |
893a3511 UD |
52 | if (cbuffer->bc_seq == cbuffer->cond->__data.__broadcast_seq) |
53 | { | |
54 | /* This thread is not waiting anymore. Adjust the sequence counters | |
346e6ad4 | 55 | appropriately. We do not increment WAKEUP_SEQ if this would |
2b6a801e | 56 | bump it over the value of TOTAL_SEQ. This can happen if a thread |
346e6ad4 UD |
57 | was woken and then canceled. */ |
58 | if (cbuffer->cond->__data.__wakeup_seq | |
59 | < cbuffer->cond->__data.__total_seq) | |
2b6a801e UD |
60 | { |
61 | ++cbuffer->cond->__data.__wakeup_seq; | |
62 | ++cbuffer->cond->__data.__futex; | |
63 | } | |
893a3511 UD |
64 | ++cbuffer->cond->__data.__woken_seq; |
65 | } | |
a88c9263 | 66 | |
ee5d5755 | 67 | cbuffer->cond->__data.__nwaiters -= 1 << COND_NWAITERS_SHIFT; |
73f7c32c UD |
68 | |
69 | /* If pthread_cond_destroy was called on this variable already, | |
70 | notify the pthread_cond_destroy caller all waiters have left | |
71 | and it can be successfully destroyed. */ | |
72 | destroying = 0; | |
73 | if (cbuffer->cond->__data.__total_seq == -1ULL | |
ee5d5755 | 74 | && cbuffer->cond->__data.__nwaiters < (1 << COND_NWAITERS_SHIFT)) |
73f7c32c | 75 | { |
5bd8a249 | 76 | lll_futex_wake (&cbuffer->cond->__data.__nwaiters, 1, pshared); |
73f7c32c UD |
77 | destroying = 1; |
78 | } | |
79 | ||
24a49f38 | 80 | /* We are done. */ |
5bd8a249 | 81 | lll_unlock (cbuffer->cond->__data.__lock, pshared); |
24a49f38 | 82 | |
3e976b96 | 83 | /* Wake everybody to make sure no condvar signal gets lost. */ |
73f7c32c | 84 | if (! destroying) |
5bd8a249 | 85 | lll_futex_wake (&cbuffer->cond->__data.__futex, INT_MAX, pshared); |
3e976b96 | 86 | |
7ce5c164 UD |
87 | /* Get the mutex before returning unless asynchronous cancellation |
88 | is in effect. */ | |
69431c9a | 89 | __pthread_mutex_cond_lock (cbuffer->mutex); |
a88c9263 UD |
90 | } |
91 | ||
92 | ||
93 | int | |
94 | __pthread_cond_wait (cond, mutex) | |
95 | pthread_cond_t *cond; | |
96 | pthread_mutex_t *mutex; | |
97 | { | |
98 | struct _pthread_cleanup_buffer buffer; | |
7ce5c164 UD |
99 | struct _condvar_cleanup_buffer cbuffer; |
100 | int err; | |
5bd8a249 | 101 | int pshared = (cond->__data.__mutex == (void *) ~0l) |
5acf7263 RM |
102 | ? LLL_SHARED : LLL_PRIVATE; |
103 | ||
104 | LIBC_PROBE (cond_wait, 2, cond, mutex); | |
a88c9263 | 105 | |
c5be0f71 | 106 | /* Make sure we are alone. */ |
5bd8a249 | 107 | lll_lock (cond->__data.__lock, pshared); |
a88c9263 UD |
108 | |
109 | /* Now we can release the mutex. */ | |
61623643 | 110 | err = __pthread_mutex_unlock_usercnt (mutex, 0); |
b078c591 | 111 | if (__builtin_expect (err, 0)) |
7ce5c164 | 112 | { |
5bd8a249 | 113 | lll_unlock (cond->__data.__lock, pshared); |
7ce5c164 UD |
114 | return err; |
115 | } | |
a88c9263 UD |
116 | |
117 | /* We have one new user of the condvar. */ | |
118 | ++cond->__data.__total_seq; | |
75fccede | 119 | ++cond->__data.__futex; |
ee5d5755 | 120 | cond->__data.__nwaiters += 1 << COND_NWAITERS_SHIFT; |
a88c9263 | 121 | |
69431c9a | 122 | /* Remember the mutex we are using here. If there is already a |
e42a990e UD |
123 | different address store this is a bad user bug. Do not store |
124 | anything for pshared condvars. */ | |
125 | if (cond->__data.__mutex != (void *) ~0l) | |
126 | cond->__data.__mutex = mutex; | |
69431c9a | 127 | |
7ce5c164 UD |
128 | /* Prepare structure passed to cancellation handler. */ |
129 | cbuffer.cond = cond; | |
130 | cbuffer.mutex = mutex; | |
131 | ||
a88c9263 UD |
132 | /* Before we block we enable cancellation. Therefore we have to |
133 | install a cancellation handler. */ | |
7ce5c164 | 134 | __pthread_cleanup_push (&buffer, __condvar_cleanup, &cbuffer); |
a88c9263 UD |
135 | |
136 | /* The current values of the wakeup counter. The "woken" counter | |
137 | must exceed this value. */ | |
138 | unsigned long long int val; | |
139 | unsigned long long int seq; | |
140 | val = seq = cond->__data.__wakeup_seq; | |
893a3511 UD |
141 | /* Remember the broadcast counter. */ |
142 | cbuffer.bc_seq = cond->__data.__broadcast_seq; | |
a88c9263 | 143 | |
46a32546 | 144 | do |
a88c9263 | 145 | { |
75fccede UD |
146 | unsigned int futex_val = cond->__data.__futex; |
147 | ||
a88c9263 | 148 | /* Prepare to wait. Release the condvar futex. */ |
5bd8a249 | 149 | lll_unlock (cond->__data.__lock, pshared); |
a88c9263 UD |
150 | |
151 | /* Enable asynchronous cancellation. Required by the standard. */ | |
69431c9a | 152 | cbuffer.oldtype = __pthread_enable_asynccancel (); |
a88c9263 | 153 | |
75fccede | 154 | /* Wait until woken by signal or broadcast. */ |
5bd8a249 | 155 | lll_futex_wait (&cond->__data.__futex, futex_val, pshared); |
a88c9263 UD |
156 | |
157 | /* Disable asynchronous cancellation. */ | |
7ce5c164 | 158 | __pthread_disable_asynccancel (cbuffer.oldtype); |
a88c9263 | 159 | |
3abc82c8 | 160 | /* We are going to look at shared data again, so get the lock. */ |
5bd8a249 | 161 | lll_lock (cond->__data.__lock, pshared); |
3abc82c8 | 162 | |
893a3511 UD |
163 | /* If a broadcast happened, we are done. */ |
164 | if (cbuffer.bc_seq != cond->__data.__broadcast_seq) | |
165 | goto bc_out; | |
166 | ||
a88c9263 UD |
167 | /* Check whether we are eligible for wakeup. */ |
168 | val = cond->__data.__wakeup_seq; | |
a88c9263 | 169 | } |
cff08c81 | 170 | while (val == seq || cond->__data.__woken_seq == val); |
a88c9263 UD |
171 | |
172 | /* Another thread woken up. */ | |
173 | ++cond->__data.__woken_seq; | |
174 | ||
893a3511 | 175 | bc_out: |
73f7c32c | 176 | |
ee5d5755 | 177 | cond->__data.__nwaiters -= 1 << COND_NWAITERS_SHIFT; |
73f7c32c UD |
178 | |
179 | /* If pthread_cond_destroy was called on this varaible already, | |
180 | notify the pthread_cond_destroy caller all waiters have left | |
181 | and it can be successfully destroyed. */ | |
182 | if (cond->__data.__total_seq == -1ULL | |
ee5d5755 | 183 | && cond->__data.__nwaiters < (1 << COND_NWAITERS_SHIFT)) |
5bd8a249 | 184 | lll_futex_wake (&cond->__data.__nwaiters, 1, pshared); |
73f7c32c | 185 | |
a88c9263 | 186 | /* We are done with the condvar. */ |
5bd8a249 | 187 | lll_unlock (cond->__data.__lock, pshared); |
a88c9263 UD |
188 | |
189 | /* The cancellation handling is back to normal, remove the handler. */ | |
190 | __pthread_cleanup_pop (&buffer, 0); | |
191 | ||
192 | /* Get the mutex before returning. */ | |
69431c9a | 193 | return __pthread_mutex_cond_lock (mutex); |
a88c9263 UD |
194 | } |
195 | ||
196 | versioned_symbol (libpthread, __pthread_cond_wait, pthread_cond_wait, | |
197 | GLIBC_2_3_2); |