pthread_cond.c revision 1.21 1 /* $NetBSD: pthread_cond.c,v 1.21 2007/03/02 17:47:40 ad Exp $ */
2
3 /*-
4 * Copyright (c) 2001 The NetBSD Foundation, Inc.
5 * All rights reserved.
6 *
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Nathan J. Williams.
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 * 3. All advertising materials mentioning features or use of this software
19 * must display the following acknowledgement:
20 * This product includes software developed by the NetBSD
21 * Foundation, Inc. and its contributors.
22 * 4. Neither the name of The NetBSD Foundation nor the names of its
23 * contributors may be used to endorse or promote products derived
24 * from this software without specific prior written permission.
25 *
26 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
27 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
28 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
29 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
30 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
31 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
32 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
33 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
34 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
35 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
36 * POSSIBILITY OF SUCH DAMAGE.
37 */
38
39 #include <sys/cdefs.h>
40 __RCSID("$NetBSD: pthread_cond.c,v 1.21 2007/03/02 17:47:40 ad Exp $");
41
42 #include <errno.h>
43 #include <sys/time.h>
44 #include <sys/types.h>
45
46 #include "pthread.h"
47 #include "pthread_int.h"
48
49 #ifdef PTHREAD_COND_DEBUG
50 #define SDPRINTF(x) DPRINTF(x)
51 #else
52 #define SDPRINTF(x)
53 #endif
54
55 int _sys_nanosleep(const struct timespec *, struct timespec *);
56
57 extern int pthread__started;
58
59 #ifdef PTHREAD_SA
60 static void pthread_cond_wait__callback(void *);
61 #endif
62 static int pthread_cond_wait_nothread(pthread_t, pthread_mutex_t *,
63 const struct timespec *);
64
65 __strong_alias(__libc_cond_init,pthread_cond_init)
66 __strong_alias(__libc_cond_signal,pthread_cond_signal)
67 __strong_alias(__libc_cond_broadcast,pthread_cond_broadcast)
68 __strong_alias(__libc_cond_wait,pthread_cond_wait)
69 __strong_alias(__libc_cond_timedwait,pthread_cond_timedwait)
70 __strong_alias(__libc_cond_destroy,pthread_cond_destroy)
71
72 int
73 pthread_cond_init(pthread_cond_t *cond, const pthread_condattr_t *attr)
74 {
75
76 pthread__error(EINVAL, "Invalid condition variable attribute",
77 (attr == NULL) || (attr->ptca_magic == _PT_CONDATTR_MAGIC));
78
79 cond->ptc_magic = _PT_COND_MAGIC;
80 pthread_lockinit(&cond->ptc_lock);
81 PTQ_INIT(&cond->ptc_waiters);
82 cond->ptc_mutex = NULL;
83
84 return 0;
85 }
86
87
88 int
89 pthread_cond_destroy(pthread_cond_t *cond)
90 {
91
92 pthread__error(EINVAL, "Invalid condition variable",
93 cond->ptc_magic == _PT_COND_MAGIC);
94 pthread__error(EBUSY, "Destroying condition variable in use",
95 cond->ptc_mutex == NULL);
96
97 cond->ptc_magic = _PT_COND_DEAD;
98
99 return 0;
100 }
101
102
103 int
104 pthread_cond_wait(pthread_cond_t *cond, pthread_mutex_t *mutex)
105 {
106 pthread_t self;
107
108 pthread__error(EINVAL, "Invalid condition variable",
109 cond->ptc_magic == _PT_COND_MAGIC);
110 pthread__error(EINVAL, "Invalid mutex",
111 mutex->ptm_magic == _PT_MUTEX_MAGIC);
112 pthread__error(EPERM, "Mutex not locked in condition wait",
113 mutex->ptm_lock == __SIMPLELOCK_LOCKED);
114
115 self = pthread__self();
116 PTHREADD_ADD(PTHREADD_COND_WAIT);
117
118 /* Just hang out for a while if threads aren't running yet. */
119 if (__predict_false(pthread__started == 0))
120 return pthread_cond_wait_nothread(self, mutex, NULL);
121
122 #ifdef PTHREAD_SA
123 pthread_spinlock(self, &cond->ptc_lock);
124 SDPRINTF(("(cond wait %p) Waiting on %p, mutex %p\n",
125 self, cond, mutex));
126
127 #ifdef ERRORCHECK
128 if (cond->ptc_mutex == NULL)
129 cond->ptc_mutex = mutex;
130 else
131 pthread__error(EINVAL,
132 "Multiple mutexes used for condition wait",
133 cond->ptc_mutex == mutex);
134 #endif
135
136 pthread_spinlock(self, &self->pt_statelock);
137 if (__predict_false(self->pt_cancel)) {
138 pthread_spinunlock(self, &self->pt_statelock);
139 pthread_spinunlock(self, &cond->ptc_lock);
140 pthread_exit(PTHREAD_CANCELED);
141 }
142
143 self->pt_sleepobj = cond;
144 self->pt_state = PT_STATE_BLOCKED_QUEUE;
145 self->pt_sleepq = &cond->ptc_waiters;
146 self->pt_sleeplock = &cond->ptc_lock;
147 pthread_spinunlock(self, &self->pt_statelock);
148 PTQ_INSERT_HEAD(&cond->ptc_waiters, self, pt_sleep);
149 pthread_mutex_unlock(mutex);
150
151 pthread__block(self, &cond->ptc_lock);
152 /* Spinlock is unlocked on return */
153 #else /* PTHREAD_SA */
154 SDPRINTF(("(cond wait %p) Waiting on %p, mutex %p\n",
155 self, cond, mutex));
156 if (__predict_false(self->pt_cancel))
157 pthread_exit(PTHREAD_CANCELED);
158 pthread_spinlock(self, &cond->ptc_lock);
159 #ifdef ERRORCHECK
160 if (cond->ptc_mutex == NULL)
161 cond->ptc_mutex = mutex;
162 else
163 pthread__error(EINVAL,
164 "Multiple mutexes used for condition wait",
165 cond->ptc_mutex == mutex);
166 #endif
167 pthread_mutex_unlock(mutex);
168 (void)pthread__park(self, &cond->ptc_lock, cond,
169 &cond->ptc_waiters, NULL, 0, 1);
170 pthread_spinunlock(self, &cond->ptc_lock);
171 #endif /* PTHREAD_SA */
172
173 pthread_mutex_lock(mutex);
174 #ifdef ERRORCHECK
175 pthread_spinlock(self, &cond->ptc_lock);
176 if (PTQ_EMPTY(&cond->ptc_waiters))
177 cond->ptc_mutex = NULL;
178 pthread_spinunlock(self, &cond->ptc_lock);
179 #endif
180 if (__predict_false(self->pt_cancel))
181 pthread_exit(PTHREAD_CANCELED);
182
183 SDPRINTF(("(cond wait %p) Woke up on %p, mutex %p\n",
184 self, cond, mutex));
185
186 return 0;
187 }
188
189
190 struct pthread_cond__waitarg {
191 pthread_t ptw_thread;
192 pthread_cond_t *ptw_cond;
193 };
194
195 int
196 pthread_cond_timedwait(pthread_cond_t *cond, pthread_mutex_t *mutex,
197 const struct timespec *abstime)
198 {
199 pthread_t self;
200 struct pthread_cond__waitarg wait;
201 #ifdef PTHREAD_SA
202 struct pt_alarm_t alarm;
203 #endif
204 int retval;
205
206 pthread__error(EINVAL, "Invalid condition variable",
207 cond->ptc_magic == _PT_COND_MAGIC);
208 pthread__error(EINVAL, "Invalid mutex",
209 mutex->ptm_magic == _PT_MUTEX_MAGIC);
210 pthread__error(EPERM, "Mutex not locked in condition wait",
211 mutex->ptm_lock == __SIMPLELOCK_LOCKED);
212 pthread__error(EINVAL, "Invalid wait time",
213 (abstime->tv_sec >= 0) &&
214 (abstime->tv_nsec >= 0) && (abstime->tv_nsec < 1000000000));
215
216 self = pthread__self();
217 PTHREADD_ADD(PTHREADD_COND_TIMEDWAIT);
218
219 /* Just hang out for a while if threads aren't running yet. */
220 if (__predict_false(pthread__started == 0))
221 return pthread_cond_wait_nothread(self, mutex, abstime);
222
223 wait.ptw_thread = self;
224 wait.ptw_cond = cond;
225 retval = 0;
226
227 #ifdef PTHREAD_SA
228 pthread_spinlock(self, &cond->ptc_lock);
229 SDPRINTF(("(cond timed wait %p) Waiting on %p until %d.%06ld\n",
230 self, cond, abstime->tv_sec, abstime->tv_nsec/1000));
231
232 pthread_spinlock(self, &self->pt_statelock);
233 if (__predict_false(self->pt_cancel)) {
234 pthread_spinunlock(self, &self->pt_statelock);
235 pthread_spinunlock(self, &cond->ptc_lock);
236 pthread_exit(PTHREAD_CANCELED);
237 }
238 #ifdef ERRORCHECK
239 if (cond->ptc_mutex == NULL)
240 cond->ptc_mutex = mutex;
241 else
242 pthread__error(EINVAL,
243 "Multiple mutexes used for condition wait",
244 cond->ptc_mutex == mutex);
245 #endif
246
247 pthread__alarm_add(self, &alarm, abstime, pthread_cond_wait__callback,
248 &wait);
249 self->pt_state = PT_STATE_BLOCKED_QUEUE;
250 self->pt_sleepobj = cond;
251 self->pt_sleepq = &cond->ptc_waiters;
252 self->pt_sleeplock = &cond->ptc_lock;
253 pthread_spinunlock(self, &self->pt_statelock);
254
255 PTQ_INSERT_HEAD(&cond->ptc_waiters, self, pt_sleep);
256 pthread_mutex_unlock(mutex);
257
258 pthread__block(self, &cond->ptc_lock);
259 /* Spinlock is unlocked on return */
260 pthread__alarm_del(self, &alarm);
261 if (pthread__alarm_fired(&alarm))
262 retval = ETIMEDOUT;
263 #else /* PTHREAD_SA */
264 SDPRINTF(("(cond timed wait %p) Waiting on %p until %d.%06ld\n",
265 self, cond, abstime->tv_sec, abstime->tv_nsec/1000));
266
267 if (__predict_false(self->pt_cancel))
268 pthread_exit(PTHREAD_CANCELED);
269 pthread_spinlock(self, &cond->ptc_lock);
270 #ifdef ERRORCHECK
271 if (cond->ptc_mutex == NULL)
272 cond->ptc_mutex = mutex;
273 else
274 pthread__error(EINVAL,
275 "Multiple mutexes used for condition wait",
276 cond->ptc_mutex == mutex);
277 #endif
278 pthread_mutex_unlock(mutex);
279 retval = pthread__park(self, &cond->ptc_lock, cond,
280 &cond->ptc_waiters, abstime, 0, 1);
281 pthread_spinunlock(self, &cond->ptc_lock);
282 #endif /* PTHREAD_SA */
283
284 SDPRINTF(("(cond timed wait %p) Woke up on %p, mutex %p\n",
285 self, cond));
286 SDPRINTF(("(cond timed wait %p) %s\n",
287 self, (retval == ETIMEDOUT) ? "(timed out)" : ""));
288 pthread_mutex_lock(mutex);
289 #ifdef ERRORCHECK
290 pthread_spinlock(self, &cond->ptc_lock);
291 if (PTQ_EMPTY(&cond->ptc_waiters))
292 cond->ptc_mutex = NULL;
293 pthread_spinunlock(self, &cond->ptc_lock);
294 #endif
295 if (__predict_false(self->pt_cancel))
296 pthread_exit(PTHREAD_CANCELED);
297
298 return retval;
299 }
300
301 #ifdef PTHREAD_SA
302 static void
303 pthread_cond_wait__callback(void *arg)
304 {
305 struct pthread_cond__waitarg *a;
306 pthread_t self;
307
308 a = arg;
309 self = pthread__self();
310
311 /*
312 * Don't dequeue and schedule the thread if it's already been
313 * queued up by a signal or broadcast (but hasn't yet run as far
314 * as pthread__alarm_del(), or we wouldn't be here, and hence can't
315 * have become blocked on some *other* queue).
316 */
317 pthread_spinlock(self, &a->ptw_cond->ptc_lock);
318 if (a->ptw_thread->pt_state == PT_STATE_BLOCKED_QUEUE) {
319 PTQ_REMOVE(&a->ptw_cond->ptc_waiters, a->ptw_thread, pt_sleep);
320 #ifdef ERRORCHECK
321 if (PTQ_EMPTY(&a->ptw_cond->ptc_waiters))
322 a->ptw_cond->ptc_mutex = NULL;
323 #endif
324 pthread__sched(self, a->ptw_thread);
325 }
326 pthread_spinunlock(self, &a->ptw_cond->ptc_lock);
327 }
328 #endif /* PTHREAD_SA */
329
330 int
331 pthread_cond_signal(pthread_cond_t *cond)
332 {
333 pthread_t self, signaled;
334
335 pthread__error(EINVAL, "Invalid condition variable",
336 cond->ptc_magic == _PT_COND_MAGIC);
337 PTHREADD_ADD(PTHREADD_COND_SIGNAL);
338
339 SDPRINTF(("(cond signal %p) Signaling %p\n",
340 pthread__self(), cond));
341
342 #ifdef PTHREAD_SA
343 if (!PTQ_EMPTY(&cond->ptc_waiters)) {
344 self = pthread__self();
345 pthread_spinlock(self, &cond->ptc_lock);
346 signaled = PTQ_FIRST(&cond->ptc_waiters);
347 if (signaled != NULL) {
348 PTQ_REMOVE(&cond->ptc_waiters, signaled, pt_sleep);
349 pthread__sched(self, signaled);
350 PTHREADD_ADD(PTHREADD_COND_WOKEUP);
351 }
352 #ifdef ERRORCHECK
353 if (PTQ_EMPTY(&cond->ptc_waiters))
354 cond->ptc_mutex = NULL;
355 #endif
356 pthread_spinunlock(self, &cond->ptc_lock);
357 }
358 #else /* PTHREAD_SA */
359 if (!PTQ_EMPTY(&cond->ptc_waiters)) {
360 self = pthread__self();
361 pthread_spinlock(self, &cond->ptc_lock);
362 signaled = PTQ_FIRST(&cond->ptc_waiters);
363 if (signaled != NULL) {
364 PTQ_REMOVE(&cond->ptc_waiters, signaled, pt_sleep);
365 #ifdef ERRORCHECK
366 if (PTQ_EMPTY(&cond->ptc_waiters))
367 cond->ptc_mutex = NULL;
368 #endif
369 pthread__unpark(self, &cond->ptc_lock, cond, signaled);
370 PTHREADD_ADD(PTHREADD_COND_WOKEUP);
371 } else {
372 #ifdef ERRORCHECK
373 cond->ptc_mutex = NULL;
374 #endif
375 pthread_spinunlock(self, &cond->ptc_lock);
376 }
377 }
378 #endif
379 return 0;
380 }
381
382
383 int
384 pthread_cond_broadcast(pthread_cond_t *cond)
385 {
386 pthread_t self;
387 #ifdef PTHREAD_SA
388 struct pthread_queue_t blockedq;
389 #endif
390
391 pthread__error(EINVAL, "Invalid condition variable", cond->ptc_magic == _PT_COND_MAGIC);
392
393 PTHREADD_ADD(PTHREADD_COND_BROADCAST);
394 SDPRINTF(("(cond signal %p) Broadcasting %p\n",
395 pthread__self(), cond));
396
397 if (!PTQ_EMPTY(&cond->ptc_waiters)) {
398 self = pthread__self();
399 pthread_spinlock(self, &cond->ptc_lock);
400 #ifdef ERRORCHECK
401 cond->ptc_mutex = NULL;
402 #endif
403 #ifdef PTHREAD_SA
404 blockedq = cond->ptc_waiters;
405 PTQ_INIT(&cond->ptc_waiters);
406 pthread__sched_sleepers(self, &blockedq);
407 PTHREADD_ADD(PTHREADD_COND_WOKEUP);
408 pthread_spinunlock(self, &cond->ptc_lock);
409 #else /* PTHREAD_SA */
410 pthread__unpark_all(self, &cond->ptc_lock, cond, &cond->ptc_waiters);
411 PTHREADD_ADD(PTHREADD_COND_WOKEUP);
412 #endif /* PTHREAD_SA */
413 }
414
415 return 0;
416
417 }
418
419
420 int
421 pthread_condattr_init(pthread_condattr_t *attr)
422 {
423
424 attr->ptca_magic = _PT_CONDATTR_MAGIC;
425
426 return 0;
427 }
428
429
430 int
431 pthread_condattr_destroy(pthread_condattr_t *attr)
432 {
433
434 pthread__error(EINVAL, "Invalid condition variable attribute",
435 attr->ptca_magic == _PT_CONDATTR_MAGIC);
436
437 attr->ptca_magic = _PT_CONDATTR_DEAD;
438
439 return 0;
440 }
441
442 /* Utility routine to hang out for a while if threads haven't started yet. */
443 static int
444 pthread_cond_wait_nothread(pthread_t self, pthread_mutex_t *mutex,
445 const struct timespec *abstime)
446 {
447 struct timespec now, diff;
448 int retval;
449
450 if (abstime == NULL) {
451 diff.tv_sec = 99999999;
452 diff.tv_nsec = 0;
453 } else {
454 clock_gettime(CLOCK_REALTIME, &now);
455 if (timespeccmp(abstime, &now, <))
456 timespecclear(&diff);
457 else
458 timespecsub(abstime, &now, &diff);
459 }
460
461 do {
462 pthread__testcancel(self);
463 pthread_mutex_unlock(mutex);
464 retval = _sys_nanosleep(&diff, NULL);
465 pthread_mutex_lock(mutex);
466 } while (abstime == NULL && retval == 0);
467 pthread__testcancel(self);
468
469 if (retval == 0)
470 return ETIMEDOUT;
471 else
472 /* spurious wakeup */
473 return 0;
474 }
475