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