pthread_cond.c revision 1.18 1 1.18 mycroft /* $NetBSD: pthread_cond.c,v 1.18 2005/01/06 17:33:36 mycroft Exp $ */
2 1.2 thorpej
3 1.2 thorpej /*-
4 1.2 thorpej * Copyright (c) 2001 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.2 thorpej * by Nathan J. Williams.
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.18 mycroft __RCSID("$NetBSD: pthread_cond.c,v 1.18 2005/01/06 17:33:36 mycroft 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.2 thorpej static void pthread_cond_wait__callback(void *);
60 1.6 nathanw static int pthread_cond_wait_nothread(pthread_t, pthread_mutex_t *,
61 1.6 nathanw const struct timespec *);
62 1.2 thorpej
63 1.2 thorpej __strong_alias(__libc_cond_init,pthread_cond_init)
64 1.2 thorpej __strong_alias(__libc_cond_signal,pthread_cond_signal)
65 1.2 thorpej __strong_alias(__libc_cond_broadcast,pthread_cond_broadcast)
66 1.2 thorpej __strong_alias(__libc_cond_wait,pthread_cond_wait)
67 1.2 thorpej __strong_alias(__libc_cond_timedwait,pthread_cond_timedwait)
68 1.2 thorpej __strong_alias(__libc_cond_destroy,pthread_cond_destroy)
69 1.2 thorpej
70 1.2 thorpej int
71 1.2 thorpej pthread_cond_init(pthread_cond_t *cond, const pthread_condattr_t *attr)
72 1.2 thorpej {
73 1.2 thorpej
74 1.11 nathanw pthread__error(EINVAL, "Invalid condition variable attribute",
75 1.11 nathanw (attr == NULL) || (attr->ptca_magic == _PT_CONDATTR_MAGIC));
76 1.2 thorpej
77 1.2 thorpej cond->ptc_magic = _PT_COND_MAGIC;
78 1.2 thorpej pthread_lockinit(&cond->ptc_lock);
79 1.2 thorpej PTQ_INIT(&cond->ptc_waiters);
80 1.2 thorpej cond->ptc_mutex = NULL;
81 1.2 thorpej
82 1.2 thorpej return 0;
83 1.2 thorpej }
84 1.2 thorpej
85 1.2 thorpej
86 1.2 thorpej int
87 1.2 thorpej pthread_cond_destroy(pthread_cond_t *cond)
88 1.2 thorpej {
89 1.2 thorpej
90 1.11 nathanw pthread__error(EINVAL, "Invalid condition variable",
91 1.11 nathanw cond->ptc_magic == _PT_COND_MAGIC);
92 1.11 nathanw pthread__error(EBUSY, "Destroying condition variable in use",
93 1.11 nathanw cond->ptc_mutex == NULL);
94 1.2 thorpej
95 1.2 thorpej cond->ptc_magic = _PT_COND_DEAD;
96 1.2 thorpej
97 1.2 thorpej return 0;
98 1.2 thorpej }
99 1.2 thorpej
100 1.2 thorpej
101 1.2 thorpej int
102 1.2 thorpej pthread_cond_wait(pthread_cond_t *cond, pthread_mutex_t *mutex)
103 1.2 thorpej {
104 1.2 thorpej pthread_t self;
105 1.10 nathanw
106 1.11 nathanw pthread__error(EINVAL, "Invalid condition variable",
107 1.11 nathanw cond->ptc_magic == _PT_COND_MAGIC);
108 1.11 nathanw pthread__error(EINVAL, "Invalid mutex",
109 1.11 nathanw mutex->ptm_magic == _PT_MUTEX_MAGIC);
110 1.11 nathanw pthread__error(EPERM, "Mutex not locked in condition wait",
111 1.11 nathanw mutex->ptm_lock == __SIMPLELOCK_LOCKED);
112 1.10 nathanw
113 1.2 thorpej self = pthread__self();
114 1.3 nathanw PTHREADD_ADD(PTHREADD_COND_WAIT);
115 1.6 nathanw
116 1.6 nathanw /* Just hang out for a while if threads aren't running yet. */
117 1.6 nathanw if (__predict_false(pthread__started == 0))
118 1.6 nathanw return pthread_cond_wait_nothread(self, mutex, NULL);
119 1.6 nathanw
120 1.2 thorpej pthread_spinlock(self, &cond->ptc_lock);
121 1.12 nathanw SDPRINTF(("(cond wait %p) Waiting on %p, mutex %p\n",
122 1.12 nathanw self, cond, mutex));
123 1.12 nathanw pthread_spinlock(self, &self->pt_statelock);
124 1.12 nathanw if (__predict_false(self->pt_cancel)) {
125 1.12 nathanw pthread_spinunlock(self, &self->pt_statelock);
126 1.12 nathanw pthread_spinunlock(self, &cond->ptc_lock);
127 1.12 nathanw pthread_exit(PTHREAD_CANCELED);
128 1.12 nathanw }
129 1.2 thorpej #ifdef ERRORCHECK
130 1.2 thorpej if (cond->ptc_mutex == NULL)
131 1.2 thorpej cond->ptc_mutex = mutex;
132 1.10 nathanw else
133 1.11 nathanw pthread__error(EINVAL,
134 1.11 nathanw "Multiple mutexes used for condition wait",
135 1.11 nathanw cond->ptc_mutex == mutex);
136 1.2 thorpej #endif
137 1.2 thorpej self->pt_state = PT_STATE_BLOCKED_QUEUE;
138 1.2 thorpej self->pt_sleepobj = cond;
139 1.2 thorpej self->pt_sleepq = &cond->ptc_waiters;
140 1.2 thorpej self->pt_sleeplock = &cond->ptc_lock;
141 1.2 thorpej pthread_spinunlock(self, &self->pt_statelock);
142 1.9 nathanw PTQ_INSERT_HEAD(&cond->ptc_waiters, self, pt_sleep);
143 1.2 thorpej pthread_mutex_unlock(mutex);
144 1.2 thorpej
145 1.2 thorpej pthread__block(self, &cond->ptc_lock);
146 1.2 thorpej /* Spinlock is unlocked on return */
147 1.2 thorpej pthread_mutex_lock(mutex);
148 1.12 nathanw #ifdef ERRORCHECK
149 1.16 nathanw pthread_spinlock(self, &cond->ptc_lock);
150 1.16 nathanw if (PTQ_EMPTY(&cond->ptc_waiters))
151 1.16 nathanw cond->ptc_mutex = NULL;
152 1.16 nathanw pthread_spinunlock(self, &cond->ptc_lock);
153 1.12 nathanw #endif
154 1.16 nathanw if (__predict_false(self->pt_cancel))
155 1.12 nathanw pthread_exit(PTHREAD_CANCELED);
156 1.12 nathanw
157 1.2 thorpej SDPRINTF(("(cond wait %p) Woke up on %p, mutex %p\n",
158 1.2 thorpej self, cond, mutex));
159 1.2 thorpej
160 1.2 thorpej return 0;
161 1.2 thorpej }
162 1.2 thorpej
163 1.2 thorpej
164 1.2 thorpej struct pthread_cond__waitarg {
165 1.2 thorpej pthread_t ptw_thread;
166 1.2 thorpej pthread_cond_t *ptw_cond;
167 1.2 thorpej };
168 1.2 thorpej
169 1.2 thorpej int
170 1.2 thorpej pthread_cond_timedwait(pthread_cond_t *cond, pthread_mutex_t *mutex,
171 1.2 thorpej const struct timespec *abstime)
172 1.2 thorpej {
173 1.2 thorpej pthread_t self;
174 1.2 thorpej struct pthread_cond__waitarg wait;
175 1.2 thorpej struct pt_alarm_t alarm;
176 1.2 thorpej int retval;
177 1.2 thorpej
178 1.11 nathanw pthread__error(EINVAL, "Invalid condition variable",
179 1.11 nathanw cond->ptc_magic == _PT_COND_MAGIC);
180 1.11 nathanw pthread__error(EINVAL, "Invalid mutex",
181 1.11 nathanw mutex->ptm_magic == _PT_MUTEX_MAGIC);
182 1.11 nathanw pthread__error(EPERM, "Mutex not locked in condition wait",
183 1.11 nathanw mutex->ptm_lock == __SIMPLELOCK_LOCKED);
184 1.11 nathanw pthread__error(EINVAL, "Invalid wait time",
185 1.11 nathanw (abstime->tv_sec >= 0) &&
186 1.10 nathanw (abstime->tv_nsec >= 0) && (abstime->tv_nsec < 1000000000));
187 1.10 nathanw
188 1.2 thorpej self = pthread__self();
189 1.6 nathanw PTHREADD_ADD(PTHREADD_COND_TIMEDWAIT);
190 1.6 nathanw
191 1.6 nathanw /* Just hang out for a while if threads aren't running yet. */
192 1.6 nathanw if (__predict_false(pthread__started == 0))
193 1.6 nathanw return pthread_cond_wait_nothread(self, mutex, abstime);
194 1.6 nathanw
195 1.2 thorpej pthread_spinlock(self, &cond->ptc_lock);
196 1.2 thorpej wait.ptw_thread = self;
197 1.2 thorpej wait.ptw_cond = cond;
198 1.2 thorpej retval = 0;
199 1.2 thorpej SDPRINTF(("(cond timed wait %p) Waiting on %p until %d.%06ld\n",
200 1.2 thorpej self, cond, abstime->tv_sec, abstime->tv_nsec/1000));
201 1.2 thorpej
202 1.2 thorpej pthread_spinlock(self, &self->pt_statelock);
203 1.12 nathanw if (__predict_false(self->pt_cancel)) {
204 1.2 thorpej pthread_spinunlock(self, &self->pt_statelock);
205 1.2 thorpej pthread_spinunlock(self, &cond->ptc_lock);
206 1.2 thorpej pthread_exit(PTHREAD_CANCELED);
207 1.2 thorpej }
208 1.12 nathanw #ifdef ERRORCHECK
209 1.12 nathanw if (cond->ptc_mutex == NULL)
210 1.12 nathanw cond->ptc_mutex = mutex;
211 1.12 nathanw else
212 1.12 nathanw pthread__error(EINVAL,
213 1.12 nathanw "Multiple mutexes used for condition wait",
214 1.12 nathanw cond->ptc_mutex == mutex);
215 1.12 nathanw #endif
216 1.12 nathanw
217 1.2 thorpej pthread__alarm_add(self, &alarm, abstime, pthread_cond_wait__callback,
218 1.2 thorpej &wait);
219 1.2 thorpej self->pt_state = PT_STATE_BLOCKED_QUEUE;
220 1.2 thorpej self->pt_sleepobj = cond;
221 1.2 thorpej self->pt_sleepq = &cond->ptc_waiters;
222 1.2 thorpej self->pt_sleeplock = &cond->ptc_lock;
223 1.2 thorpej pthread_spinunlock(self, &self->pt_statelock);
224 1.2 thorpej
225 1.9 nathanw PTQ_INSERT_HEAD(&cond->ptc_waiters, self, pt_sleep);
226 1.2 thorpej pthread_mutex_unlock(mutex);
227 1.2 thorpej
228 1.2 thorpej pthread__block(self, &cond->ptc_lock);
229 1.2 thorpej /* Spinlock is unlocked on return */
230 1.2 thorpej SDPRINTF(("(cond timed wait %p) Woke up on %p, mutex %p\n",
231 1.2 thorpej self, cond));
232 1.2 thorpej pthread__alarm_del(self, &alarm);
233 1.2 thorpej if (pthread__alarm_fired(&alarm))
234 1.2 thorpej retval = ETIMEDOUT;
235 1.2 thorpej SDPRINTF(("(cond timed wait %p) %s\n",
236 1.2 thorpej self, (retval == ETIMEDOUT) ? "(timed out)" : ""));
237 1.2 thorpej pthread_mutex_lock(mutex);
238 1.12 nathanw #ifdef ERRORCHECK
239 1.16 nathanw pthread_spinlock(self, &cond->ptc_lock);
240 1.16 nathanw if (PTQ_EMPTY(&cond->ptc_waiters))
241 1.16 nathanw cond->ptc_mutex = NULL;
242 1.16 nathanw pthread_spinunlock(self, &cond->ptc_lock);
243 1.12 nathanw #endif
244 1.16 nathanw if (__predict_false(self->pt_cancel))
245 1.12 nathanw pthread_exit(PTHREAD_CANCELED);
246 1.2 thorpej
247 1.2 thorpej return retval;
248 1.2 thorpej }
249 1.2 thorpej
250 1.2 thorpej static void
251 1.2 thorpej pthread_cond_wait__callback(void *arg)
252 1.2 thorpej {
253 1.2 thorpej struct pthread_cond__waitarg *a;
254 1.2 thorpej pthread_t self;
255 1.2 thorpej
256 1.2 thorpej a = arg;
257 1.2 thorpej self = pthread__self();
258 1.2 thorpej
259 1.2 thorpej /*
260 1.2 thorpej * Don't dequeue and schedule the thread if it's already been
261 1.2 thorpej * queued up by a signal or broadcast (but hasn't yet run as far
262 1.2 thorpej * as pthread__alarm_del(), or we wouldn't be here, and hence can't
263 1.2 thorpej * have become blocked on some *other* queue).
264 1.2 thorpej */
265 1.2 thorpej pthread_spinlock(self, &a->ptw_cond->ptc_lock);
266 1.2 thorpej if (a->ptw_thread->pt_state == PT_STATE_BLOCKED_QUEUE) {
267 1.2 thorpej PTQ_REMOVE(&a->ptw_cond->ptc_waiters, a->ptw_thread, pt_sleep);
268 1.2 thorpej #ifdef ERRORCHECK
269 1.2 thorpej if (PTQ_EMPTY(&a->ptw_cond->ptc_waiters))
270 1.2 thorpej a->ptw_cond->ptc_mutex = NULL;
271 1.2 thorpej #endif
272 1.2 thorpej pthread__sched(self, a->ptw_thread);
273 1.2 thorpej }
274 1.2 thorpej pthread_spinunlock(self, &a->ptw_cond->ptc_lock);
275 1.2 thorpej }
276 1.2 thorpej
277 1.2 thorpej int
278 1.2 thorpej pthread_cond_signal(pthread_cond_t *cond)
279 1.2 thorpej {
280 1.2 thorpej pthread_t self, signaled;
281 1.10 nathanw
282 1.11 nathanw pthread__error(EINVAL, "Invalid condition variable",
283 1.11 nathanw cond->ptc_magic == _PT_COND_MAGIC);
284 1.3 nathanw PTHREADD_ADD(PTHREADD_COND_SIGNAL);
285 1.2 thorpej
286 1.2 thorpej SDPRINTF(("(cond signal %p) Signaling %p\n",
287 1.4 nathanw pthread__self(), cond));
288 1.2 thorpej
289 1.4 nathanw if (!PTQ_EMPTY(&cond->ptc_waiters)) {
290 1.4 nathanw self = pthread__self();
291 1.4 nathanw pthread_spinlock(self, &cond->ptc_lock);
292 1.4 nathanw signaled = PTQ_FIRST(&cond->ptc_waiters);
293 1.14 cl if (signaled != NULL) {
294 1.4 nathanw PTQ_REMOVE(&cond->ptc_waiters, signaled, pt_sleep);
295 1.14 cl pthread__sched(self, signaled);
296 1.14 cl PTHREADD_ADD(PTHREADD_COND_WOKEUP);
297 1.14 cl }
298 1.2 thorpej #ifdef ERRORCHECK
299 1.4 nathanw if (PTQ_EMPTY(&cond->ptc_waiters))
300 1.4 nathanw cond->ptc_mutex = NULL;
301 1.2 thorpej #endif
302 1.13 nathanw pthread_spinunlock(self, &cond->ptc_lock);
303 1.4 nathanw }
304 1.2 thorpej
305 1.2 thorpej return 0;
306 1.2 thorpej }
307 1.2 thorpej
308 1.2 thorpej
309 1.2 thorpej int
310 1.2 thorpej pthread_cond_broadcast(pthread_cond_t *cond)
311 1.2 thorpej {
312 1.5 nathanw pthread_t self;
313 1.2 thorpej struct pthread_queue_t blockedq;
314 1.10 nathanw
315 1.11 nathanw pthread__error(EINVAL, "Invalid condition variable", cond->ptc_magic == _PT_COND_MAGIC);
316 1.2 thorpej
317 1.3 nathanw PTHREADD_ADD(PTHREADD_COND_BROADCAST);
318 1.2 thorpej SDPRINTF(("(cond signal %p) Broadcasting %p\n",
319 1.4 nathanw pthread__self(), cond));
320 1.2 thorpej
321 1.4 nathanw if (!PTQ_EMPTY(&cond->ptc_waiters)) {
322 1.4 nathanw self = pthread__self();
323 1.4 nathanw pthread_spinlock(self, &cond->ptc_lock);
324 1.4 nathanw blockedq = cond->ptc_waiters;
325 1.4 nathanw PTQ_INIT(&cond->ptc_waiters);
326 1.2 thorpej #ifdef ERRORCHECK
327 1.4 nathanw cond->ptc_mutex = NULL;
328 1.2 thorpej #endif
329 1.5 nathanw pthread__sched_sleepers(self, &blockedq);
330 1.7 nathanw PTHREADD_ADD(PTHREADD_COND_WOKEUP);
331 1.13 nathanw pthread_spinunlock(self, &cond->ptc_lock);
332 1.4 nathanw }
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
339 1.2 thorpej int
340 1.2 thorpej pthread_condattr_init(pthread_condattr_t *attr)
341 1.2 thorpej {
342 1.2 thorpej
343 1.2 thorpej attr->ptca_magic = _PT_CONDATTR_MAGIC;
344 1.2 thorpej
345 1.2 thorpej return 0;
346 1.2 thorpej }
347 1.2 thorpej
348 1.2 thorpej
349 1.2 thorpej int
350 1.2 thorpej pthread_condattr_destroy(pthread_condattr_t *attr)
351 1.2 thorpej {
352 1.2 thorpej
353 1.11 nathanw pthread__error(EINVAL, "Invalid condition variable attribute",
354 1.11 nathanw attr->ptca_magic == _PT_CONDATTR_MAGIC);
355 1.2 thorpej
356 1.2 thorpej attr->ptca_magic = _PT_CONDATTR_DEAD;
357 1.2 thorpej
358 1.2 thorpej return 0;
359 1.6 nathanw }
360 1.6 nathanw
361 1.6 nathanw /* Utility routine to hang out for a while if threads haven't started yet. */
362 1.6 nathanw static int
363 1.6 nathanw pthread_cond_wait_nothread(pthread_t self, pthread_mutex_t *mutex,
364 1.6 nathanw const struct timespec *abstime)
365 1.6 nathanw {
366 1.18 mycroft struct timespec now, diff;
367 1.6 nathanw int retval;
368 1.6 nathanw
369 1.18 mycroft if (abstime == NULL) {
370 1.18 mycroft diff.tv_sec = 99999999;
371 1.18 mycroft diff.tv_nsec = 0;
372 1.18 mycroft } else {
373 1.18 mycroft clock_gettime(CLOCK_REALTIME, &now);
374 1.18 mycroft if (timespeccmp(abstime, &now, <))
375 1.18 mycroft timespecclear(&diff);
376 1.17 nathanw else
377 1.18 mycroft timespecsub(abstime, &now, &diff);
378 1.6 nathanw }
379 1.6 nathanw
380 1.18 mycroft do {
381 1.18 mycroft pthread__testcancel(self);
382 1.18 mycroft pthread_mutex_unlock(mutex);
383 1.18 mycroft retval = _sys_nanosleep(&diff, NULL);
384 1.18 mycroft pthread_mutex_lock(mutex);
385 1.18 mycroft } while (abstime == NULL && retval == 0);
386 1.6 nathanw pthread__testcancel(self);
387 1.6 nathanw
388 1.6 nathanw if (retval == 0)
389 1.6 nathanw return ETIMEDOUT;
390 1.6 nathanw else
391 1.15 kleink /* spurious wakeup */
392 1.15 kleink return 0;
393 1.2 thorpej }
394