pthread_cond.c revision 1.30 1 1.30 ad /* $NetBSD: pthread_cond.c,v 1.30 2007/03/24 18:52:00 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.30 ad __RCSID("$NetBSD: pthread_cond.c,v 1.30 2007/03/24 18:52:00 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.23 ad self->pt_sleeponq = 1;
135 1.26 ad self->pt_sleepobj = &cond->ptc_waiters;
136 1.19 ad pthread_mutex_unlock(mutex);
137 1.26 ad (void)pthread__park(self, &cond->ptc_lock, &cond->ptc_waiters,
138 1.30 ad NULL, 1, &mutex->ptm_blocked);
139 1.16 nathanw if (PTQ_EMPTY(&cond->ptc_waiters))
140 1.16 nathanw cond->ptc_mutex = NULL;
141 1.16 nathanw pthread_spinunlock(self, &cond->ptc_lock);
142 1.25 ad pthread_mutex_lock(mutex);
143 1.23 ad
144 1.16 nathanw if (__predict_false(self->pt_cancel))
145 1.12 nathanw pthread_exit(PTHREAD_CANCELED);
146 1.12 nathanw
147 1.2 thorpej SDPRINTF(("(cond wait %p) Woke up on %p, mutex %p\n",
148 1.2 thorpej self, cond, mutex));
149 1.2 thorpej
150 1.2 thorpej return 0;
151 1.2 thorpej }
152 1.2 thorpej
153 1.2 thorpej int
154 1.2 thorpej pthread_cond_timedwait(pthread_cond_t *cond, pthread_mutex_t *mutex,
155 1.2 thorpej const struct timespec *abstime)
156 1.2 thorpej {
157 1.2 thorpej pthread_t self;
158 1.2 thorpej int retval;
159 1.2 thorpej
160 1.11 nathanw pthread__error(EINVAL, "Invalid condition variable",
161 1.11 nathanw cond->ptc_magic == _PT_COND_MAGIC);
162 1.11 nathanw pthread__error(EINVAL, "Invalid mutex",
163 1.11 nathanw mutex->ptm_magic == _PT_MUTEX_MAGIC);
164 1.11 nathanw pthread__error(EPERM, "Mutex not locked in condition wait",
165 1.11 nathanw mutex->ptm_lock == __SIMPLELOCK_LOCKED);
166 1.11 nathanw pthread__error(EINVAL, "Invalid wait time",
167 1.11 nathanw (abstime->tv_sec >= 0) &&
168 1.10 nathanw (abstime->tv_nsec >= 0) && (abstime->tv_nsec < 1000000000));
169 1.10 nathanw
170 1.2 thorpej self = pthread__self();
171 1.6 nathanw PTHREADD_ADD(PTHREADD_COND_TIMEDWAIT);
172 1.6 nathanw
173 1.6 nathanw /* Just hang out for a while if threads aren't running yet. */
174 1.6 nathanw if (__predict_false(pthread__started == 0))
175 1.6 nathanw return pthread_cond_wait_nothread(self, mutex, abstime);
176 1.6 nathanw
177 1.21 ad SDPRINTF(("(cond timed wait %p) Waiting on %p until %d.%06ld\n",
178 1.21 ad self, cond, abstime->tv_sec, abstime->tv_nsec/1000));
179 1.21 ad
180 1.25 ad if (__predict_false(self->pt_cancel))
181 1.21 ad pthread_exit(PTHREAD_CANCELED);
182 1.21 ad pthread_spinlock(self, &cond->ptc_lock);
183 1.21 ad if (cond->ptc_mutex == NULL)
184 1.21 ad cond->ptc_mutex = mutex;
185 1.28 ad else {
186 1.28 ad #ifdef ERRORCHECK
187 1.21 ad pthread__error(EINVAL,
188 1.21 ad "Multiple mutexes used for condition wait",
189 1.21 ad cond->ptc_mutex == mutex);
190 1.21 ad #endif
191 1.28 ad }
192 1.23 ad PTQ_INSERT_HEAD(&cond->ptc_waiters, self, pt_sleep);
193 1.23 ad self->pt_sleeponq = 1;
194 1.26 ad self->pt_sleepobj = &cond->ptc_waiters;
195 1.20 ad pthread_mutex_unlock(mutex);
196 1.26 ad retval = pthread__park(self, &cond->ptc_lock, &cond->ptc_waiters,
197 1.30 ad abstime, 1, &mutex->ptm_blocked);
198 1.23 ad if (PTQ_EMPTY(&cond->ptc_waiters))
199 1.23 ad cond->ptc_mutex = NULL;
200 1.19 ad pthread_spinunlock(self, &cond->ptc_lock);
201 1.19 ad
202 1.19 ad SDPRINTF(("(cond timed wait %p) Woke up on %p, mutex %p\n",
203 1.19 ad self, cond));
204 1.2 thorpej SDPRINTF(("(cond timed wait %p) %s\n",
205 1.2 thorpej self, (retval == ETIMEDOUT) ? "(timed out)" : ""));
206 1.25 ad pthread_mutex_lock(mutex);
207 1.16 nathanw if (__predict_false(self->pt_cancel))
208 1.12 nathanw pthread_exit(PTHREAD_CANCELED);
209 1.2 thorpej
210 1.2 thorpej return retval;
211 1.2 thorpej }
212 1.2 thorpej
213 1.2 thorpej int
214 1.2 thorpej pthread_cond_signal(pthread_cond_t *cond)
215 1.2 thorpej {
216 1.2 thorpej pthread_t self, signaled;
217 1.28 ad pthread_mutex_t *mutex;
218 1.10 nathanw
219 1.11 nathanw pthread__error(EINVAL, "Invalid condition variable",
220 1.11 nathanw cond->ptc_magic == _PT_COND_MAGIC);
221 1.3 nathanw PTHREADD_ADD(PTHREADD_COND_SIGNAL);
222 1.2 thorpej
223 1.2 thorpej SDPRINTF(("(cond signal %p) Signaling %p\n",
224 1.4 nathanw pthread__self(), cond));
225 1.2 thorpej
226 1.27 ad if (PTQ_EMPTY(&cond->ptc_waiters))
227 1.27 ad return 0;
228 1.27 ad
229 1.27 ad self = pthread__self();
230 1.27 ad pthread_spinlock(self, &cond->ptc_lock);
231 1.28 ad
232 1.28 ad /*
233 1.28 ad * Find a thread that is still blocked (no pending wakeup).
234 1.28 ad * A wakeup can be pending if we have interrupted unpark_all
235 1.28 ad * as it releases the interlock.
236 1.28 ad */
237 1.28 ad PTQ_FOREACH(signaled, &cond->ptc_waiters, pt_sleep) {
238 1.28 ad if (signaled->pt_sleepobj != NULL)
239 1.28 ad break;
240 1.28 ad }
241 1.28 ad if (__predict_false(signaled == NULL)) {
242 1.28 ad cond->ptc_mutex = NULL;
243 1.28 ad pthread_spinunlock(self, &cond->ptc_lock);
244 1.28 ad return 0;
245 1.28 ad }
246 1.28 ad
247 1.28 ad /*
248 1.28 ad * Pull the thread off the queue.
249 1.28 ad */
250 1.28 ad PTQ_REMOVE(&cond->ptc_waiters, signaled, pt_sleep);
251 1.28 ad mutex = cond->ptc_mutex;
252 1.28 ad if (PTQ_EMPTY(&cond->ptc_waiters))
253 1.28 ad cond->ptc_mutex = NULL;
254 1.28 ad
255 1.28 ad /*
256 1.28 ad * For all valid uses of pthread_cond_signal(), the caller will
257 1.28 ad * hold the mutex that the target is using to synchronize with.
258 1.28 ad * To avoid the target awakening and immediatley blocking on the
259 1.28 ad * mutex, transfer the thread to be awoken to the mutex's waiters
260 1.28 ad * list. The waiter will be set running when the caller (this
261 1.28 ad * thread) releases the mutex.
262 1.28 ad */
263 1.28 ad if (self->pt_mutexhint != NULL && self->pt_mutexhint == mutex) {
264 1.28 ad pthread_spinunlock(self, &cond->ptc_lock);
265 1.28 ad pthread_spinlock(self, &mutex->ptm_interlock);
266 1.28 ad PTQ_INSERT_HEAD(&mutex->ptm_blocked, signaled, pt_sleep);
267 1.28 ad pthread_spinunlock(self, &mutex->ptm_interlock);
268 1.28 ad } else {
269 1.27 ad pthread__unpark(self, &cond->ptc_lock,
270 1.27 ad &cond->ptc_waiters, signaled);
271 1.4 nathanw }
272 1.29 ad PTHREADD_ADD(PTHREADD_COND_WOKEUP);
273 1.22 ad
274 1.2 thorpej return 0;
275 1.2 thorpej }
276 1.2 thorpej
277 1.2 thorpej
278 1.2 thorpej int
279 1.2 thorpej pthread_cond_broadcast(pthread_cond_t *cond)
280 1.2 thorpej {
281 1.28 ad pthread_t self, signaled, next;
282 1.28 ad pthread_mutex_t *mutex;
283 1.10 nathanw
284 1.28 ad pthread__error(EINVAL, "Invalid condition variable",
285 1.28 ad cond->ptc_magic == _PT_COND_MAGIC);
286 1.2 thorpej
287 1.3 nathanw PTHREADD_ADD(PTHREADD_COND_BROADCAST);
288 1.2 thorpej SDPRINTF(("(cond signal %p) Broadcasting %p\n",
289 1.4 nathanw pthread__self(), cond));
290 1.2 thorpej
291 1.27 ad if (PTQ_EMPTY(&cond->ptc_waiters))
292 1.27 ad return 0;
293 1.27 ad
294 1.27 ad self = pthread__self();
295 1.27 ad pthread_spinlock(self, &cond->ptc_lock);
296 1.28 ad mutex = cond->ptc_mutex;
297 1.27 ad cond->ptc_mutex = NULL;
298 1.28 ad
299 1.28 ad /*
300 1.28 ad * Try to transfer waiters to the mutex's waiters list (see
301 1.28 ad * pthread_cond_signal()). Only transfer waiters for which
302 1.28 ad * there is no pending wakeup.
303 1.28 ad */
304 1.28 ad if (self->pt_mutexhint != NULL && self->pt_mutexhint == mutex) {
305 1.28 ad pthread_spinlock(self, &mutex->ptm_interlock);
306 1.28 ad for (signaled = PTQ_FIRST(&cond->ptc_waiters);
307 1.28 ad signaled != NULL;
308 1.28 ad signaled = next) {
309 1.28 ad next = PTQ_NEXT(signaled, pt_sleep);
310 1.28 ad if (__predict_false(signaled->pt_sleepobj == NULL))
311 1.28 ad continue;
312 1.28 ad PTQ_REMOVE(&cond->ptc_waiters, signaled, pt_sleep);
313 1.28 ad PTQ_INSERT_HEAD(&mutex->ptm_blocked, signaled,
314 1.28 ad pt_sleep);
315 1.28 ad }
316 1.28 ad pthread_spinunlock(self, &mutex->ptm_interlock);
317 1.28 ad pthread_spinunlock(self, &cond->ptc_lock);
318 1.28 ad } else
319 1.28 ad pthread__unpark_all(self, &cond->ptc_lock, &cond->ptc_waiters);
320 1.28 ad
321 1.27 ad PTHREADD_ADD(PTHREADD_COND_WOKEUP);
322 1.2 thorpej
323 1.2 thorpej return 0;
324 1.2 thorpej
325 1.2 thorpej }
326 1.2 thorpej
327 1.2 thorpej
328 1.2 thorpej int
329 1.2 thorpej pthread_condattr_init(pthread_condattr_t *attr)
330 1.2 thorpej {
331 1.2 thorpej
332 1.2 thorpej attr->ptca_magic = _PT_CONDATTR_MAGIC;
333 1.2 thorpej
334 1.2 thorpej return 0;
335 1.2 thorpej }
336 1.2 thorpej
337 1.2 thorpej
338 1.2 thorpej int
339 1.2 thorpej pthread_condattr_destroy(pthread_condattr_t *attr)
340 1.2 thorpej {
341 1.2 thorpej
342 1.11 nathanw pthread__error(EINVAL, "Invalid condition variable attribute",
343 1.11 nathanw attr->ptca_magic == _PT_CONDATTR_MAGIC);
344 1.2 thorpej
345 1.2 thorpej attr->ptca_magic = _PT_CONDATTR_DEAD;
346 1.2 thorpej
347 1.2 thorpej return 0;
348 1.6 nathanw }
349 1.6 nathanw
350 1.6 nathanw /* Utility routine to hang out for a while if threads haven't started yet. */
351 1.6 nathanw static int
352 1.6 nathanw pthread_cond_wait_nothread(pthread_t self, pthread_mutex_t *mutex,
353 1.6 nathanw const struct timespec *abstime)
354 1.6 nathanw {
355 1.18 mycroft struct timespec now, diff;
356 1.6 nathanw int retval;
357 1.6 nathanw
358 1.18 mycroft if (abstime == NULL) {
359 1.18 mycroft diff.tv_sec = 99999999;
360 1.18 mycroft diff.tv_nsec = 0;
361 1.18 mycroft } else {
362 1.18 mycroft clock_gettime(CLOCK_REALTIME, &now);
363 1.18 mycroft if (timespeccmp(abstime, &now, <))
364 1.18 mycroft timespecclear(&diff);
365 1.17 nathanw else
366 1.18 mycroft timespecsub(abstime, &now, &diff);
367 1.6 nathanw }
368 1.6 nathanw
369 1.18 mycroft do {
370 1.18 mycroft pthread__testcancel(self);
371 1.18 mycroft pthread_mutex_unlock(mutex);
372 1.18 mycroft retval = _sys_nanosleep(&diff, NULL);
373 1.18 mycroft pthread_mutex_lock(mutex);
374 1.18 mycroft } while (abstime == NULL && retval == 0);
375 1.6 nathanw pthread__testcancel(self);
376 1.6 nathanw
377 1.6 nathanw if (retval == 0)
378 1.6 nathanw return ETIMEDOUT;
379 1.6 nathanw else
380 1.15 kleink /* spurious wakeup */
381 1.15 kleink return 0;
382 1.2 thorpej }
383