pthread_cond.c revision 1.22 1 /* $NetBSD: pthread_cond.c,v 1.22 2007/03/02 18:53:52 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.22 2007/03/02 18:53:52 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 pthread_mutex_unlock(mutex);
133 (void)pthread__park(self, &cond->ptc_lock, cond,
134 &cond->ptc_waiters, NULL, 0, 1);
135 pthread_spinunlock(self, &cond->ptc_lock);
136
137 pthread_mutex_lock(mutex);
138 #ifdef ERRORCHECK
139 pthread_spinlock(self, &cond->ptc_lock);
140 if (PTQ_EMPTY(&cond->ptc_waiters))
141 cond->ptc_mutex = NULL;
142 pthread_spinunlock(self, &cond->ptc_lock);
143 #endif
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 struct pthread_cond__waitarg wait;
165 int retval;
166
167 pthread__error(EINVAL, "Invalid condition variable",
168 cond->ptc_magic == _PT_COND_MAGIC);
169 pthread__error(EINVAL, "Invalid mutex",
170 mutex->ptm_magic == _PT_MUTEX_MAGIC);
171 pthread__error(EPERM, "Mutex not locked in condition wait",
172 mutex->ptm_lock == __SIMPLELOCK_LOCKED);
173 pthread__error(EINVAL, "Invalid wait time",
174 (abstime->tv_sec >= 0) &&
175 (abstime->tv_nsec >= 0) && (abstime->tv_nsec < 1000000000));
176
177 self = pthread__self();
178 PTHREADD_ADD(PTHREADD_COND_TIMEDWAIT);
179
180 /* Just hang out for a while if threads aren't running yet. */
181 if (__predict_false(pthread__started == 0))
182 return pthread_cond_wait_nothread(self, mutex, abstime);
183
184 wait.ptw_thread = self;
185 wait.ptw_cond = cond;
186 retval = 0;
187
188 SDPRINTF(("(cond timed wait %p) Waiting on %p until %d.%06ld\n",
189 self, cond, abstime->tv_sec, abstime->tv_nsec/1000));
190
191 if (__predict_false(self->pt_cancel))
192 pthread_exit(PTHREAD_CANCELED);
193 pthread_spinlock(self, &cond->ptc_lock);
194 #ifdef ERRORCHECK
195 if (cond->ptc_mutex == NULL)
196 cond->ptc_mutex = mutex;
197 else
198 pthread__error(EINVAL,
199 "Multiple mutexes used for condition wait",
200 cond->ptc_mutex == mutex);
201 #endif
202 pthread_mutex_unlock(mutex);
203 retval = pthread__park(self, &cond->ptc_lock, cond,
204 &cond->ptc_waiters, abstime, 0, 1);
205 pthread_spinunlock(self, &cond->ptc_lock);
206
207 SDPRINTF(("(cond timed wait %p) Woke up on %p, mutex %p\n",
208 self, cond));
209 SDPRINTF(("(cond timed wait %p) %s\n",
210 self, (retval == ETIMEDOUT) ? "(timed out)" : ""));
211 pthread_mutex_lock(mutex);
212 #ifdef ERRORCHECK
213 pthread_spinlock(self, &cond->ptc_lock);
214 if (PTQ_EMPTY(&cond->ptc_waiters))
215 cond->ptc_mutex = NULL;
216 pthread_spinunlock(self, &cond->ptc_lock);
217 #endif
218 if (__predict_false(self->pt_cancel))
219 pthread_exit(PTHREAD_CANCELED);
220
221 return retval;
222 }
223
224 int
225 pthread_cond_signal(pthread_cond_t *cond)
226 {
227 pthread_t self, signaled;
228
229 pthread__error(EINVAL, "Invalid condition variable",
230 cond->ptc_magic == _PT_COND_MAGIC);
231 PTHREADD_ADD(PTHREADD_COND_SIGNAL);
232
233 SDPRINTF(("(cond signal %p) Signaling %p\n",
234 pthread__self(), cond));
235
236 if (!PTQ_EMPTY(&cond->ptc_waiters)) {
237 self = pthread__self();
238 pthread_spinlock(self, &cond->ptc_lock);
239 signaled = PTQ_FIRST(&cond->ptc_waiters);
240 if (signaled != NULL) {
241 PTQ_REMOVE(&cond->ptc_waiters, signaled, pt_sleep);
242 #ifdef ERRORCHECK
243 if (PTQ_EMPTY(&cond->ptc_waiters))
244 cond->ptc_mutex = NULL;
245 #endif
246 pthread__unpark(self, &cond->ptc_lock, cond, signaled);
247 PTHREADD_ADD(PTHREADD_COND_WOKEUP);
248 } else {
249 #ifdef ERRORCHECK
250 cond->ptc_mutex = NULL;
251 #endif
252 pthread_spinunlock(self, &cond->ptc_lock);
253 }
254 }
255
256 return 0;
257 }
258
259
260 int
261 pthread_cond_broadcast(pthread_cond_t *cond)
262 {
263 pthread_t self;
264
265 pthread__error(EINVAL, "Invalid condition variable", cond->ptc_magic == _PT_COND_MAGIC);
266
267 PTHREADD_ADD(PTHREADD_COND_BROADCAST);
268 SDPRINTF(("(cond signal %p) Broadcasting %p\n",
269 pthread__self(), cond));
270
271 if (!PTQ_EMPTY(&cond->ptc_waiters)) {
272 self = pthread__self();
273 pthread_spinlock(self, &cond->ptc_lock);
274 #ifdef ERRORCHECK
275 cond->ptc_mutex = NULL;
276 #endif
277 pthread__unpark_all(self, &cond->ptc_lock, cond, &cond->ptc_waiters);
278 PTHREADD_ADD(PTHREADD_COND_WOKEUP);
279 }
280
281 return 0;
282
283 }
284
285
286 int
287 pthread_condattr_init(pthread_condattr_t *attr)
288 {
289
290 attr->ptca_magic = _PT_CONDATTR_MAGIC;
291
292 return 0;
293 }
294
295
296 int
297 pthread_condattr_destroy(pthread_condattr_t *attr)
298 {
299
300 pthread__error(EINVAL, "Invalid condition variable attribute",
301 attr->ptca_magic == _PT_CONDATTR_MAGIC);
302
303 attr->ptca_magic = _PT_CONDATTR_DEAD;
304
305 return 0;
306 }
307
308 /* Utility routine to hang out for a while if threads haven't started yet. */
309 static int
310 pthread_cond_wait_nothread(pthread_t self, pthread_mutex_t *mutex,
311 const struct timespec *abstime)
312 {
313 struct timespec now, diff;
314 int retval;
315
316 if (abstime == NULL) {
317 diff.tv_sec = 99999999;
318 diff.tv_nsec = 0;
319 } else {
320 clock_gettime(CLOCK_REALTIME, &now);
321 if (timespeccmp(abstime, &now, <))
322 timespecclear(&diff);
323 else
324 timespecsub(abstime, &now, &diff);
325 }
326
327 do {
328 pthread__testcancel(self);
329 pthread_mutex_unlock(mutex);
330 retval = _sys_nanosleep(&diff, NULL);
331 pthread_mutex_lock(mutex);
332 } while (abstime == NULL && retval == 0);
333 pthread__testcancel(self);
334
335 if (retval == 0)
336 return ETIMEDOUT;
337 else
338 /* spurious wakeup */
339 return 0;
340 }
341