pthread_cond.c revision 1.10 1 1.10 nathanw /* $NetBSD: pthread_cond.c,v 1.10 2003/04/18 21:36:38 nathanw 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.10 nathanw __RCSID("$NetBSD: pthread_cond.c,v 1.10 2003/04/18 21:36:38 nathanw 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.6 nathanw int _sys_select(int, fd_set *, fd_set *, fd_set *, struct timeval *);
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.10 nathanw pthread__assert((attr == NULL) ||
75 1.10 nathanw (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.10 nathanw pthread__assert(cond->ptc_magic == _PT_COND_MAGIC);
91 1.10 nathanw pthread__assert(cond->ptc_mutex == NULL);
92 1.2 thorpej
93 1.2 thorpej cond->ptc_magic = _PT_COND_DEAD;
94 1.2 thorpej
95 1.2 thorpej return 0;
96 1.2 thorpej }
97 1.2 thorpej
98 1.2 thorpej
99 1.2 thorpej int
100 1.2 thorpej pthread_cond_wait(pthread_cond_t *cond, pthread_mutex_t *mutex)
101 1.2 thorpej {
102 1.2 thorpej pthread_t self;
103 1.10 nathanw
104 1.10 nathanw pthread__assert(cond->ptc_magic == _PT_COND_MAGIC);
105 1.10 nathanw pthread__assert(mutex->ptm_magic == _PT_MUTEX_MAGIC);
106 1.10 nathanw pthread__assert(mutex->ptm_lock == __SIMPLELOCK_LOCKED);
107 1.10 nathanw
108 1.2 thorpej self = pthread__self();
109 1.3 nathanw PTHREADD_ADD(PTHREADD_COND_WAIT);
110 1.6 nathanw
111 1.6 nathanw /* Just hang out for a while if threads aren't running yet. */
112 1.6 nathanw if (__predict_false(pthread__started == 0))
113 1.6 nathanw return pthread_cond_wait_nothread(self, mutex, NULL);
114 1.6 nathanw
115 1.2 thorpej pthread_spinlock(self, &cond->ptc_lock);
116 1.2 thorpej #ifdef ERRORCHECK
117 1.2 thorpej if (cond->ptc_mutex == NULL)
118 1.2 thorpej cond->ptc_mutex = mutex;
119 1.10 nathanw else
120 1.10 nathanw pthread__assert(cond->ptc_mutex == mutex);
121 1.2 thorpej #endif
122 1.10 nathanw
123 1.2 thorpej SDPRINTF(("(cond wait %p) Waiting on %p, mutex %p\n",
124 1.2 thorpej self, cond, mutex));
125 1.2 thorpej pthread_spinlock(self, &self->pt_statelock);
126 1.2 thorpej if (self->pt_cancel) {
127 1.2 thorpej pthread_spinunlock(self, &self->pt_statelock);
128 1.2 thorpej pthread_spinunlock(self, &cond->ptc_lock);
129 1.2 thorpej pthread_exit(PTHREAD_CANCELED);
130 1.2 thorpej }
131 1.2 thorpej self->pt_state = PT_STATE_BLOCKED_QUEUE;
132 1.2 thorpej self->pt_sleepobj = cond;
133 1.2 thorpej self->pt_sleepq = &cond->ptc_waiters;
134 1.2 thorpej self->pt_sleeplock = &cond->ptc_lock;
135 1.2 thorpej pthread_spinunlock(self, &self->pt_statelock);
136 1.2 thorpej
137 1.9 nathanw PTQ_INSERT_HEAD(&cond->ptc_waiters, self, pt_sleep);
138 1.2 thorpej pthread_mutex_unlock(mutex);
139 1.2 thorpej
140 1.2 thorpej pthread__block(self, &cond->ptc_lock);
141 1.2 thorpej /* Spinlock is unlocked on return */
142 1.2 thorpej pthread_mutex_lock(mutex);
143 1.2 thorpej pthread__testcancel(self);
144 1.2 thorpej SDPRINTF(("(cond wait %p) Woke up on %p, mutex %p\n",
145 1.2 thorpej self, cond, mutex));
146 1.2 thorpej
147 1.2 thorpej return 0;
148 1.2 thorpej }
149 1.2 thorpej
150 1.2 thorpej
151 1.2 thorpej struct pthread_cond__waitarg {
152 1.2 thorpej pthread_t ptw_thread;
153 1.2 thorpej pthread_cond_t *ptw_cond;
154 1.2 thorpej };
155 1.2 thorpej
156 1.2 thorpej int
157 1.2 thorpej pthread_cond_timedwait(pthread_cond_t *cond, pthread_mutex_t *mutex,
158 1.2 thorpej const struct timespec *abstime)
159 1.2 thorpej {
160 1.2 thorpej pthread_t self;
161 1.2 thorpej struct pthread_cond__waitarg wait;
162 1.2 thorpej struct pt_alarm_t alarm;
163 1.2 thorpej int retval;
164 1.2 thorpej
165 1.10 nathanw pthread__assert(cond->ptc_magic == _PT_COND_MAGIC);
166 1.10 nathanw pthread__assert(mutex->ptm_magic == _PT_MUTEX_MAGIC);
167 1.10 nathanw pthread__assert(mutex->ptm_lock == __SIMPLELOCK_LOCKED);
168 1.10 nathanw pthread__assert((abstime->tv_sec >= 0) &&
169 1.10 nathanw (abstime->tv_nsec >= 0) && (abstime->tv_nsec < 1000000000));
170 1.10 nathanw
171 1.2 thorpej self = pthread__self();
172 1.6 nathanw PTHREADD_ADD(PTHREADD_COND_TIMEDWAIT);
173 1.6 nathanw
174 1.6 nathanw /* Just hang out for a while if threads aren't running yet. */
175 1.6 nathanw if (__predict_false(pthread__started == 0))
176 1.6 nathanw return pthread_cond_wait_nothread(self, mutex, abstime);
177 1.6 nathanw
178 1.2 thorpej pthread_spinlock(self, &cond->ptc_lock);
179 1.2 thorpej #ifdef ERRORCHECK
180 1.2 thorpej if (cond->ptc_mutex == NULL)
181 1.2 thorpej cond->ptc_mutex = mutex;
182 1.10 nathanw else
183 1.10 nathanw pthread__assert(cond->ptc_mutex == mutex);
184 1.2 thorpej #endif
185 1.6 nathanw
186 1.2 thorpej wait.ptw_thread = self;
187 1.2 thorpej wait.ptw_cond = cond;
188 1.2 thorpej retval = 0;
189 1.2 thorpej SDPRINTF(("(cond timed wait %p) Waiting on %p until %d.%06ld\n",
190 1.2 thorpej self, cond, abstime->tv_sec, abstime->tv_nsec/1000));
191 1.2 thorpej
192 1.2 thorpej pthread_spinlock(self, &self->pt_statelock);
193 1.2 thorpej if (self->pt_cancel) {
194 1.2 thorpej pthread_spinunlock(self, &self->pt_statelock);
195 1.2 thorpej pthread_spinunlock(self, &cond->ptc_lock);
196 1.2 thorpej pthread_exit(PTHREAD_CANCELED);
197 1.2 thorpej }
198 1.2 thorpej pthread__alarm_add(self, &alarm, abstime, pthread_cond_wait__callback,
199 1.2 thorpej &wait);
200 1.2 thorpej self->pt_state = PT_STATE_BLOCKED_QUEUE;
201 1.2 thorpej self->pt_sleepobj = cond;
202 1.2 thorpej self->pt_sleepq = &cond->ptc_waiters;
203 1.2 thorpej self->pt_sleeplock = &cond->ptc_lock;
204 1.2 thorpej pthread_spinunlock(self, &self->pt_statelock);
205 1.2 thorpej
206 1.9 nathanw PTQ_INSERT_HEAD(&cond->ptc_waiters, self, pt_sleep);
207 1.2 thorpej pthread_mutex_unlock(mutex);
208 1.2 thorpej
209 1.2 thorpej pthread__block(self, &cond->ptc_lock);
210 1.2 thorpej /* Spinlock is unlocked on return */
211 1.2 thorpej SDPRINTF(("(cond timed wait %p) Woke up on %p, mutex %p\n",
212 1.2 thorpej self, cond));
213 1.2 thorpej pthread__alarm_del(self, &alarm);
214 1.2 thorpej if (pthread__alarm_fired(&alarm))
215 1.2 thorpej retval = ETIMEDOUT;
216 1.2 thorpej SDPRINTF(("(cond timed wait %p) %s\n",
217 1.2 thorpej self, (retval == ETIMEDOUT) ? "(timed out)" : ""));
218 1.2 thorpej pthread_mutex_lock(mutex);
219 1.2 thorpej pthread__testcancel(self);
220 1.2 thorpej
221 1.2 thorpej return retval;
222 1.2 thorpej }
223 1.2 thorpej
224 1.2 thorpej static void
225 1.2 thorpej pthread_cond_wait__callback(void *arg)
226 1.2 thorpej {
227 1.2 thorpej struct pthread_cond__waitarg *a;
228 1.2 thorpej pthread_t self;
229 1.2 thorpej
230 1.2 thorpej a = arg;
231 1.2 thorpej self = pthread__self();
232 1.2 thorpej
233 1.2 thorpej /*
234 1.2 thorpej * Don't dequeue and schedule the thread if it's already been
235 1.2 thorpej * queued up by a signal or broadcast (but hasn't yet run as far
236 1.2 thorpej * as pthread__alarm_del(), or we wouldn't be here, and hence can't
237 1.2 thorpej * have become blocked on some *other* queue).
238 1.2 thorpej */
239 1.2 thorpej pthread_spinlock(self, &a->ptw_cond->ptc_lock);
240 1.2 thorpej if (a->ptw_thread->pt_state == PT_STATE_BLOCKED_QUEUE) {
241 1.2 thorpej PTQ_REMOVE(&a->ptw_cond->ptc_waiters, a->ptw_thread, pt_sleep);
242 1.2 thorpej #ifdef ERRORCHECK
243 1.2 thorpej if (PTQ_EMPTY(&a->ptw_cond->ptc_waiters))
244 1.2 thorpej a->ptw_cond->ptc_mutex = NULL;
245 1.2 thorpej #endif
246 1.2 thorpej pthread__sched(self, a->ptw_thread);
247 1.2 thorpej }
248 1.2 thorpej pthread_spinunlock(self, &a->ptw_cond->ptc_lock);
249 1.2 thorpej }
250 1.2 thorpej
251 1.2 thorpej int
252 1.2 thorpej pthread_cond_signal(pthread_cond_t *cond)
253 1.2 thorpej {
254 1.2 thorpej pthread_t self, signaled;
255 1.10 nathanw
256 1.10 nathanw pthread__assert(cond->ptc_magic == _PT_COND_MAGIC);
257 1.3 nathanw PTHREADD_ADD(PTHREADD_COND_SIGNAL);
258 1.2 thorpej
259 1.2 thorpej SDPRINTF(("(cond signal %p) Signaling %p\n",
260 1.4 nathanw pthread__self(), cond));
261 1.2 thorpej
262 1.4 nathanw if (!PTQ_EMPTY(&cond->ptc_waiters)) {
263 1.4 nathanw self = pthread__self();
264 1.4 nathanw pthread_spinlock(self, &cond->ptc_lock);
265 1.4 nathanw signaled = PTQ_FIRST(&cond->ptc_waiters);
266 1.4 nathanw if (signaled != NULL)
267 1.4 nathanw PTQ_REMOVE(&cond->ptc_waiters, signaled, pt_sleep);
268 1.2 thorpej #ifdef ERRORCHECK
269 1.4 nathanw if (PTQ_EMPTY(&cond->ptc_waiters))
270 1.4 nathanw cond->ptc_mutex = NULL;
271 1.2 thorpej #endif
272 1.5 nathanw pthread_spinunlock(self, &cond->ptc_lock);
273 1.7 nathanw if (signaled != NULL) {
274 1.4 nathanw pthread__sched(self, signaled);
275 1.7 nathanw PTHREADD_ADD(PTHREADD_COND_WOKEUP);
276 1.7 nathanw }
277 1.4 nathanw }
278 1.2 thorpej
279 1.2 thorpej return 0;
280 1.2 thorpej }
281 1.2 thorpej
282 1.2 thorpej
283 1.2 thorpej int
284 1.2 thorpej pthread_cond_broadcast(pthread_cond_t *cond)
285 1.2 thorpej {
286 1.5 nathanw pthread_t self;
287 1.2 thorpej struct pthread_queue_t blockedq;
288 1.10 nathanw
289 1.10 nathanw pthread__assert(cond->ptc_magic == _PT_COND_MAGIC);
290 1.2 thorpej
291 1.3 nathanw PTHREADD_ADD(PTHREADD_COND_BROADCAST);
292 1.2 thorpej SDPRINTF(("(cond signal %p) Broadcasting %p\n",
293 1.4 nathanw pthread__self(), cond));
294 1.2 thorpej
295 1.4 nathanw if (!PTQ_EMPTY(&cond->ptc_waiters)) {
296 1.4 nathanw self = pthread__self();
297 1.4 nathanw pthread_spinlock(self, &cond->ptc_lock);
298 1.4 nathanw blockedq = cond->ptc_waiters;
299 1.4 nathanw PTQ_INIT(&cond->ptc_waiters);
300 1.2 thorpej #ifdef ERRORCHECK
301 1.4 nathanw cond->ptc_mutex = NULL;
302 1.2 thorpej #endif
303 1.4 nathanw pthread_spinunlock(self, &cond->ptc_lock);
304 1.5 nathanw pthread__sched_sleepers(self, &blockedq);
305 1.7 nathanw PTHREADD_ADD(PTHREADD_COND_WOKEUP);
306 1.4 nathanw }
307 1.2 thorpej
308 1.2 thorpej return 0;
309 1.2 thorpej
310 1.2 thorpej }
311 1.2 thorpej
312 1.2 thorpej
313 1.2 thorpej int
314 1.2 thorpej pthread_condattr_init(pthread_condattr_t *attr)
315 1.2 thorpej {
316 1.2 thorpej
317 1.2 thorpej attr->ptca_magic = _PT_CONDATTR_MAGIC;
318 1.2 thorpej
319 1.2 thorpej return 0;
320 1.2 thorpej }
321 1.2 thorpej
322 1.2 thorpej
323 1.2 thorpej int
324 1.2 thorpej pthread_condattr_destroy(pthread_condattr_t *attr)
325 1.2 thorpej {
326 1.2 thorpej
327 1.10 nathanw pthread__assert(attr->ptca_magic == _PT_CONDATTR_MAGIC);
328 1.2 thorpej
329 1.2 thorpej attr->ptca_magic = _PT_CONDATTR_DEAD;
330 1.2 thorpej
331 1.2 thorpej return 0;
332 1.6 nathanw }
333 1.6 nathanw
334 1.6 nathanw /* Utility routine to hang out for a while if threads haven't started yet. */
335 1.6 nathanw static int
336 1.6 nathanw pthread_cond_wait_nothread(pthread_t self, pthread_mutex_t *mutex,
337 1.6 nathanw const struct timespec *abstime)
338 1.6 nathanw {
339 1.6 nathanw struct timeval now, tv, *tvp;
340 1.6 nathanw int retval;
341 1.6 nathanw
342 1.6 nathanw if (abstime == NULL)
343 1.6 nathanw tvp = NULL;
344 1.6 nathanw else {
345 1.6 nathanw tvp = &tv;
346 1.6 nathanw gettimeofday(&now, NULL);
347 1.6 nathanw TIMESPEC_TO_TIMEVAL(tvp, abstime);
348 1.6 nathanw timersub(tvp, &now, tvp);
349 1.6 nathanw }
350 1.6 nathanw
351 1.6 nathanw /*
352 1.6 nathanw * The libpthread select() wrapper has cancellation tests, but
353 1.6 nathanw * we need to have the mutex locked when testing for
354 1.6 nathanw * cancellation and unlocked while we sleep. So, skip the
355 1.6 nathanw * wrapper.
356 1.6 nathanw */
357 1.6 nathanw pthread__testcancel(self);
358 1.6 nathanw pthread_mutex_unlock(mutex);
359 1.6 nathanw retval = _sys_select(0, NULL, NULL, NULL, tvp);
360 1.6 nathanw pthread_mutex_lock(mutex);
361 1.6 nathanw pthread__testcancel(self);
362 1.6 nathanw
363 1.6 nathanw if (retval == 0)
364 1.6 nathanw return ETIMEDOUT;
365 1.6 nathanw else
366 1.6 nathanw return EINTR;
367 1.2 thorpej }
368