pthread_cond.c revision 1.31 1 1.31 ad /* $NetBSD: pthread_cond.c,v 1.31 2007/04/12 21:36:06 ad Exp $ */
2 1.2 thorpej
3 1.2 thorpej /*-
4 1.22 ad * Copyright (c) 2001, 2006, 2007 The NetBSD Foundation, Inc.
5 1.2 thorpej * All rights reserved.
6 1.2 thorpej *
7 1.2 thorpej * This code is derived from software contributed to The NetBSD Foundation
8 1.26 ad * by Nathan J. Williams and Andrew Doran.
9 1.2 thorpej *
10 1.2 thorpej * Redistribution and use in source and binary forms, with or without
11 1.2 thorpej * modification, are permitted provided that the following conditions
12 1.2 thorpej * are met:
13 1.2 thorpej * 1. Redistributions of source code must retain the above copyright
14 1.2 thorpej * notice, this list of conditions and the following disclaimer.
15 1.2 thorpej * 2. Redistributions in binary form must reproduce the above copyright
16 1.2 thorpej * notice, this list of conditions and the following disclaimer in the
17 1.2 thorpej * documentation and/or other materials provided with the distribution.
18 1.2 thorpej * 3. All advertising materials mentioning features or use of this software
19 1.2 thorpej * must display the following acknowledgement:
20 1.2 thorpej * This product includes software developed by the NetBSD
21 1.2 thorpej * Foundation, Inc. and its contributors.
22 1.2 thorpej * 4. Neither the name of The NetBSD Foundation nor the names of its
23 1.2 thorpej * contributors may be used to endorse or promote products derived
24 1.2 thorpej * from this software without specific prior written permission.
25 1.2 thorpej *
26 1.2 thorpej * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
27 1.2 thorpej * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
28 1.2 thorpej * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
29 1.2 thorpej * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
30 1.2 thorpej * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
31 1.2 thorpej * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
32 1.2 thorpej * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
33 1.2 thorpej * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
34 1.2 thorpej * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
35 1.2 thorpej * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
36 1.2 thorpej * POSSIBILITY OF SUCH DAMAGE.
37 1.2 thorpej */
38 1.2 thorpej
39 1.8 lukem #include <sys/cdefs.h>
40 1.31 ad __RCSID("$NetBSD: pthread_cond.c,v 1.31 2007/04/12 21:36:06 ad Exp $");
41 1.8 lukem
42 1.2 thorpej #include <errno.h>
43 1.6 nathanw #include <sys/time.h>
44 1.6 nathanw #include <sys/types.h>
45 1.2 thorpej
46 1.2 thorpej #include "pthread.h"
47 1.2 thorpej #include "pthread_int.h"
48 1.2 thorpej
49 1.2 thorpej #ifdef PTHREAD_COND_DEBUG
50 1.2 thorpej #define SDPRINTF(x) DPRINTF(x)
51 1.2 thorpej #else
52 1.2 thorpej #define SDPRINTF(x)
53 1.2 thorpej #endif
54 1.2 thorpej
55 1.18 mycroft int _sys_nanosleep(const struct timespec *, struct timespec *);
56 1.6 nathanw
57 1.6 nathanw extern int pthread__started;
58 1.6 nathanw
59 1.6 nathanw static int pthread_cond_wait_nothread(pthread_t, pthread_mutex_t *,
60 1.6 nathanw const struct timespec *);
61 1.2 thorpej
62 1.2 thorpej __strong_alias(__libc_cond_init,pthread_cond_init)
63 1.2 thorpej __strong_alias(__libc_cond_signal,pthread_cond_signal)
64 1.2 thorpej __strong_alias(__libc_cond_broadcast,pthread_cond_broadcast)
65 1.2 thorpej __strong_alias(__libc_cond_wait,pthread_cond_wait)
66 1.2 thorpej __strong_alias(__libc_cond_timedwait,pthread_cond_timedwait)
67 1.2 thorpej __strong_alias(__libc_cond_destroy,pthread_cond_destroy)
68 1.2 thorpej
69 1.2 thorpej int
70 1.2 thorpej pthread_cond_init(pthread_cond_t *cond, const pthread_condattr_t *attr)
71 1.2 thorpej {
72 1.2 thorpej
73 1.11 nathanw pthread__error(EINVAL, "Invalid condition variable attribute",
74 1.11 nathanw (attr == NULL) || (attr->ptca_magic == _PT_CONDATTR_MAGIC));
75 1.2 thorpej
76 1.2 thorpej cond->ptc_magic = _PT_COND_MAGIC;
77 1.2 thorpej pthread_lockinit(&cond->ptc_lock);
78 1.2 thorpej PTQ_INIT(&cond->ptc_waiters);
79 1.2 thorpej cond->ptc_mutex = NULL;
80 1.2 thorpej
81 1.2 thorpej return 0;
82 1.2 thorpej }
83 1.2 thorpej
84 1.2 thorpej
85 1.2 thorpej int
86 1.2 thorpej pthread_cond_destroy(pthread_cond_t *cond)
87 1.2 thorpej {
88 1.2 thorpej
89 1.11 nathanw pthread__error(EINVAL, "Invalid condition variable",
90 1.11 nathanw cond->ptc_magic == _PT_COND_MAGIC);
91 1.11 nathanw pthread__error(EBUSY, "Destroying condition variable in use",
92 1.11 nathanw cond->ptc_mutex == NULL);
93 1.2 thorpej
94 1.2 thorpej cond->ptc_magic = _PT_COND_DEAD;
95 1.2 thorpej
96 1.2 thorpej return 0;
97 1.2 thorpej }
98 1.2 thorpej
99 1.2 thorpej
100 1.2 thorpej int
101 1.2 thorpej pthread_cond_wait(pthread_cond_t *cond, pthread_mutex_t *mutex)
102 1.2 thorpej {
103 1.2 thorpej pthread_t self;
104 1.10 nathanw
105 1.11 nathanw pthread__error(EINVAL, "Invalid condition variable",
106 1.11 nathanw cond->ptc_magic == _PT_COND_MAGIC);
107 1.11 nathanw pthread__error(EINVAL, "Invalid mutex",
108 1.11 nathanw mutex->ptm_magic == _PT_MUTEX_MAGIC);
109 1.11 nathanw pthread__error(EPERM, "Mutex not locked in condition wait",
110 1.11 nathanw mutex->ptm_lock == __SIMPLELOCK_LOCKED);
111 1.10 nathanw
112 1.2 thorpej self = pthread__self();
113 1.3 nathanw PTHREADD_ADD(PTHREADD_COND_WAIT);
114 1.6 nathanw
115 1.6 nathanw /* Just hang out for a while if threads aren't running yet. */
116 1.6 nathanw if (__predict_false(pthread__started == 0))
117 1.6 nathanw return pthread_cond_wait_nothread(self, mutex, NULL);
118 1.6 nathanw
119 1.21 ad SDPRINTF(("(cond wait %p) Waiting on %p, mutex %p\n",
120 1.21 ad self, cond, mutex));
121 1.25 ad if (__predict_false(self->pt_cancel))
122 1.21 ad pthread_exit(PTHREAD_CANCELED);
123 1.21 ad pthread_spinlock(self, &cond->ptc_lock);
124 1.21 ad if (cond->ptc_mutex == NULL)
125 1.21 ad cond->ptc_mutex = mutex;
126 1.28 ad else {
127 1.28 ad #ifdef ERRORCHECK
128 1.21 ad pthread__error(EINVAL,
129 1.21 ad "Multiple mutexes used for condition wait",
130 1.21 ad cond->ptc_mutex == mutex);
131 1.21 ad #endif
132 1.28 ad }
133 1.23 ad PTQ_INSERT_HEAD(&cond->ptc_waiters, self, pt_sleep);
134 1.31 ad self->pt_signalled = 0;
135 1.23 ad self->pt_sleeponq = 1;
136 1.26 ad self->pt_sleepobj = &cond->ptc_waiters;
137 1.19 ad pthread_mutex_unlock(mutex);
138 1.26 ad (void)pthread__park(self, &cond->ptc_lock, &cond->ptc_waiters,
139 1.30 ad NULL, 1, &mutex->ptm_blocked);
140 1.16 nathanw if (PTQ_EMPTY(&cond->ptc_waiters))
141 1.16 nathanw cond->ptc_mutex = NULL;
142 1.16 nathanw pthread_spinunlock(self, &cond->ptc_lock);
143 1.25 ad pthread_mutex_lock(mutex);
144 1.23 ad
145 1.31 ad if (__predict_false(self->pt_cancel)) {
146 1.31 ad if (self->pt_signalled)
147 1.31 ad pthread_cond_signal(cond);
148 1.12 nathanw pthread_exit(PTHREAD_CANCELED);
149 1.31 ad }
150 1.12 nathanw
151 1.2 thorpej SDPRINTF(("(cond wait %p) Woke up on %p, mutex %p\n",
152 1.2 thorpej self, cond, mutex));
153 1.2 thorpej
154 1.2 thorpej return 0;
155 1.2 thorpej }
156 1.2 thorpej
157 1.2 thorpej int
158 1.2 thorpej pthread_cond_timedwait(pthread_cond_t *cond, pthread_mutex_t *mutex,
159 1.2 thorpej const struct timespec *abstime)
160 1.2 thorpej {
161 1.2 thorpej pthread_t self;
162 1.2 thorpej int retval;
163 1.2 thorpej
164 1.11 nathanw pthread__error(EINVAL, "Invalid condition variable",
165 1.11 nathanw cond->ptc_magic == _PT_COND_MAGIC);
166 1.11 nathanw pthread__error(EINVAL, "Invalid mutex",
167 1.11 nathanw mutex->ptm_magic == _PT_MUTEX_MAGIC);
168 1.11 nathanw pthread__error(EPERM, "Mutex not locked in condition wait",
169 1.11 nathanw mutex->ptm_lock == __SIMPLELOCK_LOCKED);
170 1.11 nathanw pthread__error(EINVAL, "Invalid wait time",
171 1.11 nathanw (abstime->tv_sec >= 0) &&
172 1.10 nathanw (abstime->tv_nsec >= 0) && (abstime->tv_nsec < 1000000000));
173 1.10 nathanw
174 1.2 thorpej self = pthread__self();
175 1.6 nathanw PTHREADD_ADD(PTHREADD_COND_TIMEDWAIT);
176 1.6 nathanw
177 1.6 nathanw /* Just hang out for a while if threads aren't running yet. */
178 1.6 nathanw if (__predict_false(pthread__started == 0))
179 1.6 nathanw return pthread_cond_wait_nothread(self, mutex, abstime);
180 1.6 nathanw
181 1.21 ad SDPRINTF(("(cond timed wait %p) Waiting on %p until %d.%06ld\n",
182 1.21 ad self, cond, abstime->tv_sec, abstime->tv_nsec/1000));
183 1.21 ad
184 1.25 ad if (__predict_false(self->pt_cancel))
185 1.21 ad pthread_exit(PTHREAD_CANCELED);
186 1.21 ad pthread_spinlock(self, &cond->ptc_lock);
187 1.21 ad if (cond->ptc_mutex == NULL)
188 1.21 ad cond->ptc_mutex = mutex;
189 1.28 ad else {
190 1.28 ad #ifdef ERRORCHECK
191 1.21 ad pthread__error(EINVAL,
192 1.21 ad "Multiple mutexes used for condition wait",
193 1.21 ad cond->ptc_mutex == mutex);
194 1.21 ad #endif
195 1.28 ad }
196 1.23 ad PTQ_INSERT_HEAD(&cond->ptc_waiters, self, pt_sleep);
197 1.31 ad self->pt_signalled = 0;
198 1.23 ad self->pt_sleeponq = 1;
199 1.26 ad self->pt_sleepobj = &cond->ptc_waiters;
200 1.20 ad pthread_mutex_unlock(mutex);
201 1.26 ad retval = pthread__park(self, &cond->ptc_lock, &cond->ptc_waiters,
202 1.30 ad abstime, 1, &mutex->ptm_blocked);
203 1.23 ad if (PTQ_EMPTY(&cond->ptc_waiters))
204 1.23 ad cond->ptc_mutex = NULL;
205 1.19 ad pthread_spinunlock(self, &cond->ptc_lock);
206 1.19 ad
207 1.19 ad SDPRINTF(("(cond timed wait %p) Woke up on %p, mutex %p\n",
208 1.19 ad self, cond));
209 1.2 thorpej SDPRINTF(("(cond timed wait %p) %s\n",
210 1.2 thorpej self, (retval == ETIMEDOUT) ? "(timed out)" : ""));
211 1.25 ad pthread_mutex_lock(mutex);
212 1.31 ad if (__predict_false(self->pt_cancel | retval)) {
213 1.31 ad if (self->pt_signalled)
214 1.31 ad pthread_cond_signal(cond);
215 1.31 ad if (self->pt_cancel)
216 1.31 ad pthread_exit(PTHREAD_CANCELED);
217 1.31 ad }
218 1.2 thorpej
219 1.2 thorpej return retval;
220 1.2 thorpej }
221 1.2 thorpej
222 1.2 thorpej int
223 1.2 thorpej pthread_cond_signal(pthread_cond_t *cond)
224 1.2 thorpej {
225 1.2 thorpej pthread_t self, signaled;
226 1.28 ad pthread_mutex_t *mutex;
227 1.10 nathanw
228 1.11 nathanw pthread__error(EINVAL, "Invalid condition variable",
229 1.11 nathanw cond->ptc_magic == _PT_COND_MAGIC);
230 1.3 nathanw PTHREADD_ADD(PTHREADD_COND_SIGNAL);
231 1.2 thorpej
232 1.2 thorpej SDPRINTF(("(cond signal %p) Signaling %p\n",
233 1.4 nathanw pthread__self(), cond));
234 1.2 thorpej
235 1.27 ad if (PTQ_EMPTY(&cond->ptc_waiters))
236 1.27 ad return 0;
237 1.27 ad
238 1.27 ad self = pthread__self();
239 1.27 ad pthread_spinlock(self, &cond->ptc_lock);
240 1.28 ad
241 1.28 ad /*
242 1.28 ad * Find a thread that is still blocked (no pending wakeup).
243 1.28 ad * A wakeup can be pending if we have interrupted unpark_all
244 1.28 ad * as it releases the interlock.
245 1.28 ad */
246 1.28 ad PTQ_FOREACH(signaled, &cond->ptc_waiters, pt_sleep) {
247 1.28 ad if (signaled->pt_sleepobj != NULL)
248 1.28 ad break;
249 1.28 ad }
250 1.28 ad if (__predict_false(signaled == NULL)) {
251 1.28 ad cond->ptc_mutex = NULL;
252 1.28 ad pthread_spinunlock(self, &cond->ptc_lock);
253 1.28 ad return 0;
254 1.28 ad }
255 1.28 ad
256 1.28 ad /*
257 1.31 ad * Pull the thread off the queue, and set pt_signalled.
258 1.31 ad *
259 1.31 ad * After resuming execution, the thread must check to see if it
260 1.31 ad * has been restarted as a result of pthread_cond_signal(). If it
261 1.31 ad * has, but cannot take the wakeup (because of eg a pending Unix
262 1.31 ad * signal or timeout) then try to ensure that another thread sees
263 1.31 ad * it. This is necessary because there may be multiple waiters,
264 1.31 ad * and at least one should take the wakeup if possible.
265 1.28 ad */
266 1.28 ad PTQ_REMOVE(&cond->ptc_waiters, signaled, pt_sleep);
267 1.28 ad mutex = cond->ptc_mutex;
268 1.28 ad if (PTQ_EMPTY(&cond->ptc_waiters))
269 1.28 ad cond->ptc_mutex = NULL;
270 1.31 ad signaled->pt_signalled = 1;
271 1.28 ad
272 1.28 ad /*
273 1.28 ad * For all valid uses of pthread_cond_signal(), the caller will
274 1.28 ad * hold the mutex that the target is using to synchronize with.
275 1.28 ad * To avoid the target awakening and immediatley blocking on the
276 1.28 ad * mutex, transfer the thread to be awoken to the mutex's waiters
277 1.28 ad * list. The waiter will be set running when the caller (this
278 1.28 ad * thread) releases the mutex.
279 1.28 ad */
280 1.28 ad if (self->pt_mutexhint != NULL && self->pt_mutexhint == mutex) {
281 1.28 ad pthread_spinunlock(self, &cond->ptc_lock);
282 1.28 ad pthread_spinlock(self, &mutex->ptm_interlock);
283 1.28 ad PTQ_INSERT_HEAD(&mutex->ptm_blocked, signaled, pt_sleep);
284 1.28 ad pthread_spinunlock(self, &mutex->ptm_interlock);
285 1.28 ad } else {
286 1.27 ad pthread__unpark(self, &cond->ptc_lock,
287 1.27 ad &cond->ptc_waiters, signaled);
288 1.4 nathanw }
289 1.29 ad PTHREADD_ADD(PTHREADD_COND_WOKEUP);
290 1.22 ad
291 1.2 thorpej return 0;
292 1.2 thorpej }
293 1.2 thorpej
294 1.2 thorpej
295 1.2 thorpej int
296 1.2 thorpej pthread_cond_broadcast(pthread_cond_t *cond)
297 1.2 thorpej {
298 1.28 ad pthread_t self, signaled, next;
299 1.28 ad pthread_mutex_t *mutex;
300 1.10 nathanw
301 1.28 ad pthread__error(EINVAL, "Invalid condition variable",
302 1.28 ad cond->ptc_magic == _PT_COND_MAGIC);
303 1.2 thorpej
304 1.3 nathanw PTHREADD_ADD(PTHREADD_COND_BROADCAST);
305 1.2 thorpej SDPRINTF(("(cond signal %p) Broadcasting %p\n",
306 1.4 nathanw pthread__self(), cond));
307 1.2 thorpej
308 1.27 ad if (PTQ_EMPTY(&cond->ptc_waiters))
309 1.27 ad return 0;
310 1.27 ad
311 1.27 ad self = pthread__self();
312 1.27 ad pthread_spinlock(self, &cond->ptc_lock);
313 1.28 ad mutex = cond->ptc_mutex;
314 1.27 ad cond->ptc_mutex = NULL;
315 1.28 ad
316 1.28 ad /*
317 1.28 ad * Try to transfer waiters to the mutex's waiters list (see
318 1.28 ad * pthread_cond_signal()). Only transfer waiters for which
319 1.28 ad * there is no pending wakeup.
320 1.28 ad */
321 1.28 ad if (self->pt_mutexhint != NULL && self->pt_mutexhint == mutex) {
322 1.28 ad pthread_spinlock(self, &mutex->ptm_interlock);
323 1.28 ad for (signaled = PTQ_FIRST(&cond->ptc_waiters);
324 1.28 ad signaled != NULL;
325 1.28 ad signaled = next) {
326 1.28 ad next = PTQ_NEXT(signaled, pt_sleep);
327 1.28 ad if (__predict_false(signaled->pt_sleepobj == NULL))
328 1.28 ad continue;
329 1.28 ad PTQ_REMOVE(&cond->ptc_waiters, signaled, pt_sleep);
330 1.28 ad PTQ_INSERT_HEAD(&mutex->ptm_blocked, signaled,
331 1.28 ad pt_sleep);
332 1.28 ad }
333 1.28 ad pthread_spinunlock(self, &mutex->ptm_interlock);
334 1.28 ad pthread_spinunlock(self, &cond->ptc_lock);
335 1.28 ad } else
336 1.28 ad pthread__unpark_all(self, &cond->ptc_lock, &cond->ptc_waiters);
337 1.28 ad
338 1.27 ad PTHREADD_ADD(PTHREADD_COND_WOKEUP);
339 1.2 thorpej
340 1.2 thorpej return 0;
341 1.2 thorpej
342 1.2 thorpej }
343 1.2 thorpej
344 1.2 thorpej
345 1.2 thorpej int
346 1.2 thorpej pthread_condattr_init(pthread_condattr_t *attr)
347 1.2 thorpej {
348 1.2 thorpej
349 1.2 thorpej attr->ptca_magic = _PT_CONDATTR_MAGIC;
350 1.2 thorpej
351 1.2 thorpej return 0;
352 1.2 thorpej }
353 1.2 thorpej
354 1.2 thorpej
355 1.2 thorpej int
356 1.2 thorpej pthread_condattr_destroy(pthread_condattr_t *attr)
357 1.2 thorpej {
358 1.2 thorpej
359 1.11 nathanw pthread__error(EINVAL, "Invalid condition variable attribute",
360 1.11 nathanw attr->ptca_magic == _PT_CONDATTR_MAGIC);
361 1.2 thorpej
362 1.2 thorpej attr->ptca_magic = _PT_CONDATTR_DEAD;
363 1.2 thorpej
364 1.2 thorpej return 0;
365 1.6 nathanw }
366 1.6 nathanw
367 1.6 nathanw /* Utility routine to hang out for a while if threads haven't started yet. */
368 1.6 nathanw static int
369 1.6 nathanw pthread_cond_wait_nothread(pthread_t self, pthread_mutex_t *mutex,
370 1.6 nathanw const struct timespec *abstime)
371 1.6 nathanw {
372 1.18 mycroft struct timespec now, diff;
373 1.6 nathanw int retval;
374 1.6 nathanw
375 1.18 mycroft if (abstime == NULL) {
376 1.18 mycroft diff.tv_sec = 99999999;
377 1.18 mycroft diff.tv_nsec = 0;
378 1.18 mycroft } else {
379 1.18 mycroft clock_gettime(CLOCK_REALTIME, &now);
380 1.18 mycroft if (timespeccmp(abstime, &now, <))
381 1.18 mycroft timespecclear(&diff);
382 1.17 nathanw else
383 1.18 mycroft timespecsub(abstime, &now, &diff);
384 1.6 nathanw }
385 1.6 nathanw
386 1.18 mycroft do {
387 1.18 mycroft pthread__testcancel(self);
388 1.18 mycroft pthread_mutex_unlock(mutex);
389 1.18 mycroft retval = _sys_nanosleep(&diff, NULL);
390 1.18 mycroft pthread_mutex_lock(mutex);
391 1.18 mycroft } while (abstime == NULL && retval == 0);
392 1.6 nathanw pthread__testcancel(self);
393 1.6 nathanw
394 1.6 nathanw if (retval == 0)
395 1.6 nathanw return ETIMEDOUT;
396 1.6 nathanw else
397 1.15 kleink /* spurious wakeup */
398 1.15 kleink return 0;
399 1.2 thorpej }
400