]> git.ipfire.org Git - thirdparty/glibc.git/blob - nptl/pthread_cond_wait.c
6222d922ffdc962dfaa8c81a58aa870767a9fba9
[thirdparty/glibc.git] / nptl / pthread_cond_wait.c
1 /* Copyright (C) 2003-2014 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 <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>
27 #include <stap-probe.h>
28
29 struct _condvar_cleanup_buffer
30 {
31 int oldtype;
32 pthread_cond_t *cond;
33 pthread_mutex_t *mutex;
34 unsigned int bc_seq;
35 };
36
37
38 void
39 __attribute__ ((visibility ("hidden")))
40 __condvar_cleanup (void *arg)
41 {
42 struct _condvar_cleanup_buffer *cbuffer =
43 (struct _condvar_cleanup_buffer *) arg;
44 unsigned int destroying;
45 int pshared = (cbuffer->cond->__data.__mutex == (void *) ~0l)
46 ? LLL_SHARED : LLL_PRIVATE;
47
48 /* We are going to modify shared data. */
49 lll_lock (cbuffer->cond->__data.__lock, pshared);
50
51 if (cbuffer->bc_seq == cbuffer->cond->__data.__broadcast_seq)
52 {
53 /* This thread is not waiting anymore. Adjust the sequence counters
54 appropriately. We do not increment WAKEUP_SEQ if this would
55 bump it over the value of TOTAL_SEQ. This can happen if a thread
56 was woken and then canceled. */
57 if (cbuffer->cond->__data.__wakeup_seq
58 < cbuffer->cond->__data.__total_seq)
59 {
60 ++cbuffer->cond->__data.__wakeup_seq;
61 ++cbuffer->cond->__data.__futex;
62 }
63 ++cbuffer->cond->__data.__woken_seq;
64 }
65
66 cbuffer->cond->__data.__nwaiters -= 1 << COND_NWAITERS_SHIFT;
67
68 /* If pthread_cond_destroy was called on this variable already,
69 notify the pthread_cond_destroy caller all waiters have left
70 and it can be successfully destroyed. */
71 destroying = 0;
72 if (cbuffer->cond->__data.__total_seq == -1ULL
73 && cbuffer->cond->__data.__nwaiters < (1 << COND_NWAITERS_SHIFT))
74 {
75 lll_futex_wake (&cbuffer->cond->__data.__nwaiters, 1, pshared);
76 destroying = 1;
77 }
78
79 /* We are done. */
80 lll_unlock (cbuffer->cond->__data.__lock, pshared);
81
82 /* Wake everybody to make sure no condvar signal gets lost. */
83 if (! destroying)
84 lll_futex_wake (&cbuffer->cond->__data.__futex, INT_MAX, pshared);
85
86 /* Get the mutex before returning unless asynchronous cancellation
87 is in effect. We don't try to get the mutex if we already own it. */
88 if (!(USE_REQUEUE_PI (cbuffer->mutex))
89 || ((cbuffer->mutex->__data.__lock & FUTEX_TID_MASK)
90 != THREAD_GETMEM (THREAD_SELF, tid)))
91 {
92 __pthread_mutex_cond_lock (cbuffer->mutex);
93 }
94 else
95 __pthread_mutex_cond_lock_adjust (cbuffer->mutex);
96 }
97
98
99 int
100 __pthread_cond_wait (cond, mutex)
101 pthread_cond_t *cond;
102 pthread_mutex_t *mutex;
103 {
104 struct _pthread_cleanup_buffer buffer;
105 struct _condvar_cleanup_buffer cbuffer;
106 int err;
107 int pshared = (cond->__data.__mutex == (void *) ~0l)
108 ? LLL_SHARED : LLL_PRIVATE;
109
110 #if (defined lll_futex_wait_requeue_pi \
111 && defined __ASSUME_REQUEUE_PI)
112 int pi_flag = 0;
113 #endif
114
115 LIBC_PROBE (cond_wait, 2, cond, mutex);
116
117 /* Make sure we are alone. */
118 lll_lock (cond->__data.__lock, pshared);
119
120 /* Now we can release the mutex. */
121 err = __pthread_mutex_unlock_usercnt (mutex, 0);
122 if (__builtin_expect (err, 0))
123 {
124 lll_unlock (cond->__data.__lock, pshared);
125 return err;
126 }
127
128 /* We have one new user of the condvar. */
129 ++cond->__data.__total_seq;
130 ++cond->__data.__futex;
131 cond->__data.__nwaiters += 1 << COND_NWAITERS_SHIFT;
132
133 /* Remember the mutex we are using here. If there is already a
134 different address store this is a bad user bug. Do not store
135 anything for pshared condvars. */
136 if (cond->__data.__mutex != (void *) ~0l)
137 cond->__data.__mutex = mutex;
138
139 /* Prepare structure passed to cancellation handler. */
140 cbuffer.cond = cond;
141 cbuffer.mutex = mutex;
142
143 /* Before we block we enable cancellation. Therefore we have to
144 install a cancellation handler. */
145 __pthread_cleanup_push (&buffer, __condvar_cleanup, &cbuffer);
146
147 /* The current values of the wakeup counter. The "woken" counter
148 must exceed this value. */
149 unsigned long long int val;
150 unsigned long long int seq;
151 val = seq = cond->__data.__wakeup_seq;
152 /* Remember the broadcast counter. */
153 cbuffer.bc_seq = cond->__data.__broadcast_seq;
154
155 do
156 {
157 unsigned int futex_val = cond->__data.__futex;
158 /* Prepare to wait. Release the condvar futex. */
159 lll_unlock (cond->__data.__lock, pshared);
160
161 /* Enable asynchronous cancellation. Required by the standard. */
162 cbuffer.oldtype = __pthread_enable_asynccancel ();
163
164 #if (defined lll_futex_wait_requeue_pi \
165 && defined __ASSUME_REQUEUE_PI)
166 /* If pi_flag remained 1 then it means that we had the lock and the mutex
167 but a spurious waker raced ahead of us. Give back the mutex before
168 going into wait again. */
169 if (pi_flag)
170 {
171 __pthread_mutex_cond_lock_adjust (mutex);
172 __pthread_mutex_unlock_usercnt (mutex, 0);
173 }
174 pi_flag = USE_REQUEUE_PI (mutex);
175
176 if (pi_flag)
177 {
178 err = lll_futex_wait_requeue_pi (&cond->__data.__futex,
179 futex_val, &mutex->__data.__lock,
180 pshared);
181
182 pi_flag = (err == 0);
183 }
184 else
185 #endif
186 /* Wait until woken by signal or broadcast. */
187 lll_futex_wait (&cond->__data.__futex, futex_val, pshared);
188
189 /* Disable asynchronous cancellation. */
190 __pthread_disable_asynccancel (cbuffer.oldtype);
191
192 /* We are going to look at shared data again, so get the lock. */
193 lll_lock (cond->__data.__lock, pshared);
194
195 /* If a broadcast happened, we are done. */
196 if (cbuffer.bc_seq != cond->__data.__broadcast_seq)
197 goto bc_out;
198
199 /* Check whether we are eligible for wakeup. */
200 val = cond->__data.__wakeup_seq;
201 }
202 while (val == seq || cond->__data.__woken_seq == val);
203
204 /* Another thread woken up. */
205 ++cond->__data.__woken_seq;
206
207 bc_out:
208
209 cond->__data.__nwaiters -= 1 << COND_NWAITERS_SHIFT;
210
211 /* If pthread_cond_destroy was called on this varaible already,
212 notify the pthread_cond_destroy caller all waiters have left
213 and it can be successfully destroyed. */
214 if (cond->__data.__total_seq == -1ULL
215 && cond->__data.__nwaiters < (1 << COND_NWAITERS_SHIFT))
216 lll_futex_wake (&cond->__data.__nwaiters, 1, pshared);
217
218 /* We are done with the condvar. */
219 lll_unlock (cond->__data.__lock, pshared);
220
221 /* The cancellation handling is back to normal, remove the handler. */
222 __pthread_cleanup_pop (&buffer, 0);
223
224 /* Get the mutex before returning. Not needed for PI. */
225 #if (defined lll_futex_wait_requeue_pi \
226 && defined __ASSUME_REQUEUE_PI)
227 if (pi_flag)
228 {
229 __pthread_mutex_cond_lock_adjust (mutex);
230 return 0;
231 }
232 else
233 #endif
234 return __pthread_mutex_cond_lock (mutex);
235 }
236
237 versioned_symbol (libpthread, __pthread_cond_wait, pthread_cond_wait,
238 GLIBC_2_3_2);