pthread_cond.c revision 1.23 1 /* $NetBSD: pthread_cond.c,v 1.23 2007/03/02 19:56:47 ad Exp $ */
2
3 /*-
4 * Copyright (c) 2001, 2006, 2007 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.23 2007/03/02 19:56:47 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 static int pthread_cond_wait_nothread(pthread_t, pthread_mutex_t *,
60 const struct timespec *);
61
62 __strong_alias(__libc_cond_init,pthread_cond_init)
63 __strong_alias(__libc_cond_signal,pthread_cond_signal)
64 __strong_alias(__libc_cond_broadcast,pthread_cond_broadcast)
65 __strong_alias(__libc_cond_wait,pthread_cond_wait)
66 __strong_alias(__libc_cond_timedwait,pthread_cond_timedwait)
67 __strong_alias(__libc_cond_destroy,pthread_cond_destroy)
68
69 int
70 pthread_cond_init(pthread_cond_t *cond, const pthread_condattr_t *attr)
71 {
72
73 pthread__error(EINVAL, "Invalid condition variable attribute",
74 (attr == NULL) || (attr->ptca_magic == _PT_CONDATTR_MAGIC));
75
76 cond->ptc_magic = _PT_COND_MAGIC;
77 pthread_lockinit(&cond->ptc_lock);
78 PTQ_INIT(&cond->ptc_waiters);
79 cond->ptc_mutex = NULL;
80
81 return 0;
82 }
83
84
85 int
86 pthread_cond_destroy(pthread_cond_t *cond)
87 {
88
89 pthread__error(EINVAL, "Invalid condition variable",
90 cond->ptc_magic == _PT_COND_MAGIC);
91 pthread__error(EBUSY, "Destroying condition variable in use",
92 cond->ptc_mutex == NULL);
93
94 cond->ptc_magic = _PT_COND_DEAD;
95
96 return 0;
97 }
98
99
100 int
101 pthread_cond_wait(pthread_cond_t *cond, pthread_mutex_t *mutex)
102 {
103 pthread_t self;
104
105 pthread__error(EINVAL, "Invalid condition variable",
106 cond->ptc_magic == _PT_COND_MAGIC);
107 pthread__error(EINVAL, "Invalid mutex",
108 mutex->ptm_magic == _PT_MUTEX_MAGIC);
109 pthread__error(EPERM, "Mutex not locked in condition wait",
110 mutex->ptm_lock == __SIMPLELOCK_LOCKED);
111
112 self = pthread__self();
113 PTHREADD_ADD(PTHREADD_COND_WAIT);
114
115 /* Just hang out for a while if threads aren't running yet. */
116 if (__predict_false(pthread__started == 0))
117 return pthread_cond_wait_nothread(self, mutex, NULL);
118
119 SDPRINTF(("(cond wait %p) Waiting on %p, mutex %p\n",
120 self, cond, mutex));
121 if (__predict_false(self->pt_cancel))
122 pthread_exit(PTHREAD_CANCELED);
123 pthread_spinlock(self, &cond->ptc_lock);
124 #ifdef ERRORCHECK
125 if (cond->ptc_mutex == NULL)
126 cond->ptc_mutex = mutex;
127 else
128 pthread__error(EINVAL,
129 "Multiple mutexes used for condition wait",
130 cond->ptc_mutex == mutex);
131 #endif
132 PTQ_INSERT_HEAD(&cond->ptc_waiters, self, pt_sleep);
133 self->pt_sleeponq = 1;
134 pthread_mutex_unlock(mutex);
135 (void)pthread__park(self, &cond->ptc_lock, cond,
136 NULL, NULL, 0, 1);
137 #ifdef ERRORCHECK
138 if (PTQ_EMPTY(&cond->ptc_waiters))
139 cond->ptc_mutex = NULL;
140 #endif
141 pthread_spinunlock(self, &cond->ptc_lock);
142 pthread_mutex_lock(mutex);
143
144 if (__predict_false(self->pt_cancel))
145 pthread_exit(PTHREAD_CANCELED);
146
147 SDPRINTF(("(cond wait %p) Woke up on %p, mutex %p\n",
148 self, cond, mutex));
149
150 return 0;
151 }
152
153
154 struct pthread_cond__waitarg {
155 pthread_t ptw_thread;
156 pthread_cond_t *ptw_cond;
157 };
158
159 int
160 pthread_cond_timedwait(pthread_cond_t *cond, pthread_mutex_t *mutex,
161 const struct timespec *abstime)
162 {
163 pthread_t self;
164 int retval;
165
166 pthread__error(EINVAL, "Invalid condition variable",
167 cond->ptc_magic == _PT_COND_MAGIC);
168 pthread__error(EINVAL, "Invalid mutex",
169 mutex->ptm_magic == _PT_MUTEX_MAGIC);
170 pthread__error(EPERM, "Mutex not locked in condition wait",
171 mutex->ptm_lock == __SIMPLELOCK_LOCKED);
172 pthread__error(EINVAL, "Invalid wait time",
173 (abstime->tv_sec >= 0) &&
174 (abstime->tv_nsec >= 0) && (abstime->tv_nsec < 1000000000));
175
176 self = pthread__self();
177 PTHREADD_ADD(PTHREADD_COND_TIMEDWAIT);
178
179 /* Just hang out for a while if threads aren't running yet. */
180 if (__predict_false(pthread__started == 0))
181 return pthread_cond_wait_nothread(self, mutex, abstime);
182
183 SDPRINTF(("(cond timed wait %p) Waiting on %p until %d.%06ld\n",
184 self, cond, abstime->tv_sec, abstime->tv_nsec/1000));
185
186 if (__predict_false(self->pt_cancel))
187 pthread_exit(PTHREAD_CANCELED);
188 pthread_spinlock(self, &cond->ptc_lock);
189 #ifdef ERRORCHECK
190 if (cond->ptc_mutex == NULL)
191 cond->ptc_mutex = mutex;
192 else
193 pthread__error(EINVAL,
194 "Multiple mutexes used for condition wait",
195 cond->ptc_mutex == mutex);
196 #endif
197 PTQ_INSERT_HEAD(&cond->ptc_waiters, self, pt_sleep);
198 self->pt_sleeponq = 1;
199 pthread_mutex_unlock(mutex);
200 retval = pthread__park(self, &cond->ptc_lock, cond,
201 NULL, abstime, 0, 1);
202 #ifdef ERRORCHECK
203 if (PTQ_EMPTY(&cond->ptc_waiters))
204 cond->ptc_mutex = NULL;
205 #endif
206 pthread_spinunlock(self, &cond->ptc_lock);
207
208 SDPRINTF(("(cond timed wait %p) Woke up on %p, mutex %p\n",
209 self, cond));
210 SDPRINTF(("(cond timed wait %p) %s\n",
211 self, (retval == ETIMEDOUT) ? "(timed out)" : ""));
212 pthread_mutex_lock(mutex);
213 if (__predict_false(self->pt_cancel))
214 pthread_exit(PTHREAD_CANCELED);
215
216 return retval;
217 }
218
219 int
220 pthread_cond_signal(pthread_cond_t *cond)
221 {
222 pthread_t self, signaled;
223
224 pthread__error(EINVAL, "Invalid condition variable",
225 cond->ptc_magic == _PT_COND_MAGIC);
226 PTHREADD_ADD(PTHREADD_COND_SIGNAL);
227
228 SDPRINTF(("(cond signal %p) Signaling %p\n",
229 pthread__self(), cond));
230
231 if (!PTQ_EMPTY(&cond->ptc_waiters)) {
232 self = pthread__self();
233 pthread_spinlock(self, &cond->ptc_lock);
234 signaled = PTQ_FIRST(&cond->ptc_waiters);
235 if (signaled != NULL) {
236 PTQ_REMOVE(&cond->ptc_waiters, signaled, pt_sleep);
237 #ifdef ERRORCHECK
238 if (PTQ_EMPTY(&cond->ptc_waiters))
239 cond->ptc_mutex = NULL;
240 #endif
241 pthread__unpark(self, &cond->ptc_lock, cond, signaled);
242 PTHREADD_ADD(PTHREADD_COND_WOKEUP);
243 } else {
244 #ifdef ERRORCHECK
245 cond->ptc_mutex = NULL;
246 #endif
247 pthread_spinunlock(self, &cond->ptc_lock);
248 }
249 }
250
251 return 0;
252 }
253
254
255 int
256 pthread_cond_broadcast(pthread_cond_t *cond)
257 {
258 pthread_t self;
259
260 pthread__error(EINVAL, "Invalid condition variable", cond->ptc_magic == _PT_COND_MAGIC);
261
262 PTHREADD_ADD(PTHREADD_COND_BROADCAST);
263 SDPRINTF(("(cond signal %p) Broadcasting %p\n",
264 pthread__self(), cond));
265
266 if (!PTQ_EMPTY(&cond->ptc_waiters)) {
267 self = pthread__self();
268 pthread_spinlock(self, &cond->ptc_lock);
269 #ifdef ERRORCHECK
270 cond->ptc_mutex = NULL;
271 #endif
272 pthread__unpark_all(self, &cond->ptc_lock, cond, &cond->ptc_waiters);
273 PTHREADD_ADD(PTHREADD_COND_WOKEUP);
274 }
275
276 return 0;
277
278 }
279
280
281 int
282 pthread_condattr_init(pthread_condattr_t *attr)
283 {
284
285 attr->ptca_magic = _PT_CONDATTR_MAGIC;
286
287 return 0;
288 }
289
290
291 int
292 pthread_condattr_destroy(pthread_condattr_t *attr)
293 {
294
295 pthread__error(EINVAL, "Invalid condition variable attribute",
296 attr->ptca_magic == _PT_CONDATTR_MAGIC);
297
298 attr->ptca_magic = _PT_CONDATTR_DEAD;
299
300 return 0;
301 }
302
303 /* Utility routine to hang out for a while if threads haven't started yet. */
304 static int
305 pthread_cond_wait_nothread(pthread_t self, pthread_mutex_t *mutex,
306 const struct timespec *abstime)
307 {
308 struct timespec now, diff;
309 int retval;
310
311 if (abstime == NULL) {
312 diff.tv_sec = 99999999;
313 diff.tv_nsec = 0;
314 } else {
315 clock_gettime(CLOCK_REALTIME, &now);
316 if (timespeccmp(abstime, &now, <))
317 timespecclear(&diff);
318 else
319 timespecsub(abstime, &now, &diff);
320 }
321
322 do {
323 pthread__testcancel(self);
324 pthread_mutex_unlock(mutex);
325 retval = _sys_nanosleep(&diff, NULL);
326 pthread_mutex_lock(mutex);
327 } while (abstime == NULL && retval == 0);
328 pthread__testcancel(self);
329
330 if (retval == 0)
331 return ETIMEDOUT;
332 else
333 /* spurious wakeup */
334 return 0;
335 }
336