Patch [1/4] async-signal safe TLS.
[glibc.git] / nptl / pthread_cond_wait.c
blob01d42d7834cc3e7e16816d1da067f5adc778147a
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.
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 <endian.h>
20 #include <errno.h>
21 #include <sysdep.h>
22 #include <lowlevellock.h>
23 #include <pthread.h>
24 #include <pthreadP.h>
26 #include <shlib-compat.h>
27 #include <stap-probe.h>
29 struct _condvar_cleanup_buffer
31 int oldtype;
32 pthread_cond_t *cond;
33 pthread_mutex_t *mutex;
34 unsigned int bc_seq;
38 void
39 __attribute__ ((visibility ("hidden")))
40 __condvar_cleanup (void *arg)
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;
48 /* We are going to modify shared data. */
49 lll_lock (cbuffer->cond->__data.__lock, pshared);
51 if (cbuffer->bc_seq == cbuffer->cond->__data.__broadcast_seq)
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)
60 ++cbuffer->cond->__data.__wakeup_seq;
61 ++cbuffer->cond->__data.__futex;
63 ++cbuffer->cond->__data.__woken_seq;
66 cbuffer->cond->__data.__nwaiters -= 1 << COND_NWAITERS_SHIFT;
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))
75 lll_futex_wake (&cbuffer->cond->__data.__nwaiters, 1, pshared);
76 destroying = 1;
79 /* We are done. */
80 lll_unlock (cbuffer->cond->__data.__lock, pshared);
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);
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)))
92 __pthread_mutex_cond_lock (cbuffer->mutex);
94 else
95 __pthread_mutex_cond_lock_adjust (cbuffer->mutex);
99 int
100 __pthread_cond_wait (cond, mutex)
101 pthread_cond_t *cond;
102 pthread_mutex_t *mutex;
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;
110 #if (defined lll_futex_wait_requeue_pi \
111 && defined __ASSUME_REQUEUE_PI)
112 int pi_flag = 0;
113 #endif
115 LIBC_PROBE (cond_wait, 2, cond, mutex);
117 /* Make sure we are alone. */
118 lll_lock (cond->__data.__lock, pshared);
120 /* Now we can release the mutex. */
121 err = __pthread_mutex_unlock_usercnt (mutex, 0);
122 if (__builtin_expect (err, 0))
124 lll_unlock (cond->__data.__lock, pshared);
125 return err;
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;
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;
139 /* Prepare structure passed to cancellation handler. */
140 cbuffer.cond = cond;
141 cbuffer.mutex = mutex;
143 /* Before we block we enable cancellation. Therefore we have to
144 install a cancellation handler. */
145 __pthread_cleanup_push (&buffer, __condvar_cleanup, &cbuffer);
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;
157 unsigned int futex_val = cond->__data.__futex;
158 /* Prepare to wait. Release the condvar futex. */
159 lll_unlock (cond->__data.__lock, pshared);
161 /* Enable asynchronous cancellation. Required by the standard. */
162 cbuffer.oldtype = __pthread_enable_asynccancel ();
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)
171 __pthread_mutex_cond_lock_adjust (mutex);
172 __pthread_mutex_unlock_usercnt (mutex, 0);
174 pi_flag = USE_REQUEUE_PI (mutex);
176 if (pi_flag)
178 err = lll_futex_wait_requeue_pi (&cond->__data.__futex,
179 futex_val, &mutex->__data.__lock,
180 pshared);
182 pi_flag = (err == 0);
184 else
185 #endif
186 /* Wait until woken by signal or broadcast. */
187 lll_futex_wait (&cond->__data.__futex, futex_val, pshared);
189 /* Disable asynchronous cancellation. */
190 __pthread_disable_asynccancel (cbuffer.oldtype);
192 /* We are going to look at shared data again, so get the lock. */
193 lll_lock (cond->__data.__lock, pshared);
195 /* If a broadcast happened, we are done. */
196 if (cbuffer.bc_seq != cond->__data.__broadcast_seq)
197 goto bc_out;
199 /* Check whether we are eligible for wakeup. */
200 val = cond->__data.__wakeup_seq;
202 while (val == seq || cond->__data.__woken_seq == val);
204 /* Another thread woken up. */
205 ++cond->__data.__woken_seq;
207 bc_out:
209 cond->__data.__nwaiters -= 1 << COND_NWAITERS_SHIFT;
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);
218 /* We are done with the condvar. */
219 lll_unlock (cond->__data.__lock, pshared);
221 /* The cancellation handling is back to normal, remove the handler. */
222 __pthread_cleanup_pop (&buffer, 0);
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)
229 __pthread_mutex_cond_lock_adjust (mutex);
230 return 0;
232 else
233 #endif
234 return __pthread_mutex_cond_lock (mutex);
237 versioned_symbol (libpthread, __pthread_cond_wait, pthread_cond_wait,
238 GLIBC_2_3_2);