pthread_cond.c revision 1.33 1 1.33 ad /* $NetBSD: pthread_cond.c,v 1.33 2007/08/07 19:04:21 ad Exp $ */
2 1.2 thorpej
3 1.2 thorpej /*-
4 1.22 ad * Copyright (c) 2001, 2006, 2007 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.26 ad * by Nathan J. Williams and Andrew Doran.
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.33 ad __RCSID("$NetBSD: pthread_cond.c,v 1.33 2007/08/07 19:04:21 ad 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.18 mycroft int _sys_nanosleep(const struct timespec *, struct timespec *);
50 1.6 nathanw
51 1.6 nathanw extern int pthread__started;
52 1.6 nathanw
53 1.6 nathanw static int pthread_cond_wait_nothread(pthread_t, pthread_mutex_t *,
54 1.6 nathanw const struct timespec *);
55 1.2 thorpej
56 1.2 thorpej __strong_alias(__libc_cond_init,pthread_cond_init)
57 1.2 thorpej __strong_alias(__libc_cond_signal,pthread_cond_signal)
58 1.2 thorpej __strong_alias(__libc_cond_broadcast,pthread_cond_broadcast)
59 1.2 thorpej __strong_alias(__libc_cond_wait,pthread_cond_wait)
60 1.2 thorpej __strong_alias(__libc_cond_timedwait,pthread_cond_timedwait)
61 1.2 thorpej __strong_alias(__libc_cond_destroy,pthread_cond_destroy)
62 1.2 thorpej
63 1.2 thorpej int
64 1.2 thorpej pthread_cond_init(pthread_cond_t *cond, const pthread_condattr_t *attr)
65 1.2 thorpej {
66 1.2 thorpej
67 1.11 nathanw pthread__error(EINVAL, "Invalid condition variable attribute",
68 1.11 nathanw (attr == NULL) || (attr->ptca_magic == _PT_CONDATTR_MAGIC));
69 1.2 thorpej
70 1.2 thorpej cond->ptc_magic = _PT_COND_MAGIC;
71 1.2 thorpej pthread_lockinit(&cond->ptc_lock);
72 1.2 thorpej PTQ_INIT(&cond->ptc_waiters);
73 1.2 thorpej cond->ptc_mutex = NULL;
74 1.2 thorpej
75 1.2 thorpej return 0;
76 1.2 thorpej }
77 1.2 thorpej
78 1.2 thorpej
79 1.2 thorpej int
80 1.2 thorpej pthread_cond_destroy(pthread_cond_t *cond)
81 1.2 thorpej {
82 1.2 thorpej
83 1.11 nathanw pthread__error(EINVAL, "Invalid condition variable",
84 1.11 nathanw cond->ptc_magic == _PT_COND_MAGIC);
85 1.11 nathanw pthread__error(EBUSY, "Destroying condition variable in use",
86 1.11 nathanw cond->ptc_mutex == NULL);
87 1.2 thorpej
88 1.2 thorpej cond->ptc_magic = _PT_COND_DEAD;
89 1.2 thorpej
90 1.2 thorpej return 0;
91 1.2 thorpej }
92 1.2 thorpej
93 1.2 thorpej
94 1.2 thorpej int
95 1.2 thorpej pthread_cond_wait(pthread_cond_t *cond, pthread_mutex_t *mutex)
96 1.2 thorpej {
97 1.2 thorpej pthread_t self;
98 1.10 nathanw
99 1.11 nathanw pthread__error(EINVAL, "Invalid condition variable",
100 1.11 nathanw cond->ptc_magic == _PT_COND_MAGIC);
101 1.11 nathanw pthread__error(EINVAL, "Invalid mutex",
102 1.11 nathanw mutex->ptm_magic == _PT_MUTEX_MAGIC);
103 1.11 nathanw pthread__error(EPERM, "Mutex not locked in condition wait",
104 1.11 nathanw mutex->ptm_lock == __SIMPLELOCK_LOCKED);
105 1.10 nathanw
106 1.2 thorpej self = pthread__self();
107 1.3 nathanw PTHREADD_ADD(PTHREADD_COND_WAIT);
108 1.6 nathanw
109 1.6 nathanw /* Just hang out for a while if threads aren't running yet. */
110 1.6 nathanw if (__predict_false(pthread__started == 0))
111 1.6 nathanw return pthread_cond_wait_nothread(self, mutex, NULL);
112 1.6 nathanw
113 1.25 ad if (__predict_false(self->pt_cancel))
114 1.21 ad pthread_exit(PTHREAD_CANCELED);
115 1.32 ad
116 1.32 ad /*
117 1.32 ad * Note this thread as waiting on the CV. To ensure good
118 1.32 ad * performance it's critical that the spinlock is held for
119 1.32 ad * as short a time as possible - that means no system calls.
120 1.32 ad */
121 1.21 ad pthread_spinlock(self, &cond->ptc_lock);
122 1.21 ad if (cond->ptc_mutex == NULL)
123 1.21 ad cond->ptc_mutex = mutex;
124 1.28 ad else {
125 1.28 ad #ifdef ERRORCHECK
126 1.21 ad pthread__error(EINVAL,
127 1.21 ad "Multiple mutexes used for condition wait",
128 1.21 ad cond->ptc_mutex == mutex);
129 1.21 ad #endif
130 1.28 ad }
131 1.23 ad PTQ_INSERT_HEAD(&cond->ptc_waiters, self, pt_sleep);
132 1.31 ad self->pt_signalled = 0;
133 1.23 ad self->pt_sleeponq = 1;
134 1.26 ad self->pt_sleepobj = &cond->ptc_waiters;
135 1.32 ad pthread_spinunlock(self, &cond->ptc_lock);
136 1.32 ad
137 1.33 ad /*
138 1.33 ad * Before releasing the mutex, note that this thread is
139 1.33 ad * about to block by setting the willpark flag. If there
140 1.33 ad * is a single waiter on the mutex, setting the flag will
141 1.33 ad * defer restarting it until calling into the kernel to
142 1.33 ad * park, saving a syscall & involuntary context switch.
143 1.33 ad */
144 1.33 ad self->pt_willpark = 1;
145 1.19 ad pthread_mutex_unlock(mutex);
146 1.26 ad (void)pthread__park(self, &cond->ptc_lock, &cond->ptc_waiters,
147 1.30 ad NULL, 1, &mutex->ptm_blocked);
148 1.32 ad
149 1.32 ad /*
150 1.32 ad * If we awoke abnormally the waiters list will have been
151 1.32 ad * made empty by the current thread (in pthread__park()),
152 1.32 ad * so we can check the value safely without locking.
153 1.32 ad *
154 1.32 ad * Otherwise, it will have been updated by whichever thread
155 1.32 ad * last issued a wakeup.
156 1.32 ad */
157 1.32 ad if (PTQ_EMPTY(&cond->ptc_waiters) && cond->ptc_mutex != NULL) {
158 1.32 ad pthread_spinlock(self, &cond->ptc_lock);
159 1.32 ad if (PTQ_EMPTY(&cond->ptc_waiters))
160 1.32 ad cond->ptc_mutex = NULL;
161 1.32 ad pthread_spinunlock(self, &cond->ptc_lock);
162 1.32 ad }
163 1.32 ad
164 1.32 ad /*
165 1.32 ad * Re-acquire the mutex and return to the caller. If we
166 1.32 ad * have cancelled then exit. POSIX dictates that the mutex
167 1.32 ad * must be held when we action the cancellation.
168 1.32 ad */
169 1.25 ad pthread_mutex_lock(mutex);
170 1.31 ad if (__predict_false(self->pt_cancel)) {
171 1.31 ad if (self->pt_signalled)
172 1.31 ad pthread_cond_signal(cond);
173 1.12 nathanw pthread_exit(PTHREAD_CANCELED);
174 1.31 ad }
175 1.12 nathanw
176 1.2 thorpej return 0;
177 1.2 thorpej }
178 1.2 thorpej
179 1.2 thorpej int
180 1.2 thorpej pthread_cond_timedwait(pthread_cond_t *cond, pthread_mutex_t *mutex,
181 1.2 thorpej const struct timespec *abstime)
182 1.2 thorpej {
183 1.2 thorpej pthread_t self;
184 1.2 thorpej int retval;
185 1.2 thorpej
186 1.11 nathanw pthread__error(EINVAL, "Invalid condition variable",
187 1.11 nathanw cond->ptc_magic == _PT_COND_MAGIC);
188 1.11 nathanw pthread__error(EINVAL, "Invalid mutex",
189 1.11 nathanw mutex->ptm_magic == _PT_MUTEX_MAGIC);
190 1.11 nathanw pthread__error(EPERM, "Mutex not locked in condition wait",
191 1.11 nathanw mutex->ptm_lock == __SIMPLELOCK_LOCKED);
192 1.11 nathanw pthread__error(EINVAL, "Invalid wait time",
193 1.11 nathanw (abstime->tv_sec >= 0) &&
194 1.10 nathanw (abstime->tv_nsec >= 0) && (abstime->tv_nsec < 1000000000));
195 1.10 nathanw
196 1.2 thorpej self = pthread__self();
197 1.6 nathanw PTHREADD_ADD(PTHREADD_COND_TIMEDWAIT);
198 1.6 nathanw
199 1.6 nathanw /* Just hang out for a while if threads aren't running yet. */
200 1.6 nathanw if (__predict_false(pthread__started == 0))
201 1.6 nathanw return pthread_cond_wait_nothread(self, mutex, abstime);
202 1.6 nathanw
203 1.25 ad if (__predict_false(self->pt_cancel))
204 1.21 ad pthread_exit(PTHREAD_CANCELED);
205 1.32 ad
206 1.32 ad /*
207 1.32 ad * Note this thread as waiting on the CV. To ensure good
208 1.32 ad * performance it's critical that the spinlock is held for
209 1.32 ad * as short a time as possible - that means no system calls.
210 1.32 ad */
211 1.21 ad pthread_spinlock(self, &cond->ptc_lock);
212 1.21 ad if (cond->ptc_mutex == NULL)
213 1.21 ad cond->ptc_mutex = mutex;
214 1.28 ad else {
215 1.28 ad #ifdef ERRORCHECK
216 1.21 ad pthread__error(EINVAL,
217 1.21 ad "Multiple mutexes used for condition wait",
218 1.21 ad cond->ptc_mutex == mutex);
219 1.21 ad #endif
220 1.28 ad }
221 1.23 ad PTQ_INSERT_HEAD(&cond->ptc_waiters, self, pt_sleep);
222 1.31 ad self->pt_signalled = 0;
223 1.23 ad self->pt_sleeponq = 1;
224 1.26 ad self->pt_sleepobj = &cond->ptc_waiters;
225 1.32 ad pthread_spinunlock(self, &cond->ptc_lock);
226 1.32 ad
227 1.33 ad /*
228 1.33 ad * Before releasing the mutex, note that this thread is
229 1.33 ad * about to block by setting the willpark flag. If there
230 1.33 ad * is a single waiter on the mutex, setting the flag will
231 1.33 ad * defer restarting it until calling into the kernel to
232 1.33 ad * park, saving a syscall & involuntary context switch.
233 1.33 ad */
234 1.33 ad self->pt_willpark = 1;
235 1.20 ad pthread_mutex_unlock(mutex);
236 1.26 ad retval = pthread__park(self, &cond->ptc_lock, &cond->ptc_waiters,
237 1.30 ad abstime, 1, &mutex->ptm_blocked);
238 1.19 ad
239 1.32 ad /*
240 1.32 ad * If we awoke abnormally the waiters list will have been
241 1.32 ad * made empty by the current thread (in pthread__park()),
242 1.32 ad * so we can check the value safely without locking.
243 1.32 ad *
244 1.32 ad * Otherwise, it will have been updated by whichever thread
245 1.32 ad * last issued a wakeup.
246 1.32 ad */
247 1.32 ad if (PTQ_EMPTY(&cond->ptc_waiters) && cond->ptc_mutex != NULL) {
248 1.32 ad pthread_spinlock(self, &cond->ptc_lock);
249 1.32 ad if (PTQ_EMPTY(&cond->ptc_waiters))
250 1.32 ad cond->ptc_mutex = NULL;
251 1.32 ad pthread_spinunlock(self, &cond->ptc_lock);
252 1.32 ad }
253 1.32 ad
254 1.32 ad /*
255 1.32 ad * Re-acquire the mutex and return to the caller. If we
256 1.32 ad * have cancelled then exit. POSIX dictates that the mutex
257 1.32 ad * must be held when we action the cancellation.
258 1.32 ad */
259 1.25 ad pthread_mutex_lock(mutex);
260 1.31 ad if (__predict_false(self->pt_cancel | retval)) {
261 1.31 ad if (self->pt_signalled)
262 1.31 ad pthread_cond_signal(cond);
263 1.31 ad if (self->pt_cancel)
264 1.31 ad pthread_exit(PTHREAD_CANCELED);
265 1.31 ad }
266 1.2 thorpej
267 1.2 thorpej return retval;
268 1.2 thorpej }
269 1.2 thorpej
270 1.2 thorpej int
271 1.2 thorpej pthread_cond_signal(pthread_cond_t *cond)
272 1.2 thorpej {
273 1.2 thorpej pthread_t self, signaled;
274 1.28 ad pthread_mutex_t *mutex;
275 1.10 nathanw
276 1.11 nathanw pthread__error(EINVAL, "Invalid condition variable",
277 1.11 nathanw cond->ptc_magic == _PT_COND_MAGIC);
278 1.3 nathanw PTHREADD_ADD(PTHREADD_COND_SIGNAL);
279 1.2 thorpej
280 1.27 ad if (PTQ_EMPTY(&cond->ptc_waiters))
281 1.27 ad return 0;
282 1.27 ad
283 1.27 ad self = pthread__self();
284 1.27 ad pthread_spinlock(self, &cond->ptc_lock);
285 1.28 ad
286 1.28 ad /*
287 1.28 ad * Find a thread that is still blocked (no pending wakeup).
288 1.28 ad * A wakeup can be pending if we have interrupted unpark_all
289 1.28 ad * as it releases the interlock.
290 1.28 ad */
291 1.28 ad PTQ_FOREACH(signaled, &cond->ptc_waiters, pt_sleep) {
292 1.28 ad if (signaled->pt_sleepobj != NULL)
293 1.28 ad break;
294 1.28 ad }
295 1.28 ad if (__predict_false(signaled == NULL)) {
296 1.28 ad cond->ptc_mutex = NULL;
297 1.28 ad pthread_spinunlock(self, &cond->ptc_lock);
298 1.28 ad return 0;
299 1.28 ad }
300 1.28 ad
301 1.28 ad /*
302 1.31 ad * Pull the thread off the queue, and set pt_signalled.
303 1.31 ad *
304 1.31 ad * After resuming execution, the thread must check to see if it
305 1.31 ad * has been restarted as a result of pthread_cond_signal(). If it
306 1.32 ad * has, but cannot take the wakeup (because of eg a timeout) then
307 1.32 ad * try to ensure that another thread sees it. This is necessary
308 1.32 ad * because there may be multiple waiters, and at least one should
309 1.32 ad * take the wakeup if possible.
310 1.28 ad */
311 1.28 ad PTQ_REMOVE(&cond->ptc_waiters, signaled, pt_sleep);
312 1.28 ad mutex = cond->ptc_mutex;
313 1.28 ad if (PTQ_EMPTY(&cond->ptc_waiters))
314 1.28 ad cond->ptc_mutex = NULL;
315 1.31 ad signaled->pt_signalled = 1;
316 1.28 ad
317 1.28 ad /*
318 1.28 ad * For all valid uses of pthread_cond_signal(), the caller will
319 1.28 ad * hold the mutex that the target is using to synchronize with.
320 1.28 ad * To avoid the target awakening and immediatley blocking on the
321 1.28 ad * mutex, transfer the thread to be awoken to the mutex's waiters
322 1.28 ad * list. The waiter will be set running when the caller (this
323 1.28 ad * thread) releases the mutex.
324 1.28 ad */
325 1.28 ad if (self->pt_mutexhint != NULL && self->pt_mutexhint == mutex) {
326 1.28 ad pthread_spinunlock(self, &cond->ptc_lock);
327 1.28 ad pthread_spinlock(self, &mutex->ptm_interlock);
328 1.28 ad PTQ_INSERT_HEAD(&mutex->ptm_blocked, signaled, pt_sleep);
329 1.28 ad pthread_spinunlock(self, &mutex->ptm_interlock);
330 1.28 ad } else {
331 1.27 ad pthread__unpark(self, &cond->ptc_lock,
332 1.27 ad &cond->ptc_waiters, signaled);
333 1.4 nathanw }
334 1.29 ad PTHREADD_ADD(PTHREADD_COND_WOKEUP);
335 1.22 ad
336 1.2 thorpej return 0;
337 1.2 thorpej }
338 1.2 thorpej
339 1.2 thorpej
340 1.2 thorpej int
341 1.2 thorpej pthread_cond_broadcast(pthread_cond_t *cond)
342 1.2 thorpej {
343 1.28 ad pthread_t self, signaled, next;
344 1.28 ad pthread_mutex_t *mutex;
345 1.10 nathanw
346 1.28 ad pthread__error(EINVAL, "Invalid condition variable",
347 1.28 ad cond->ptc_magic == _PT_COND_MAGIC);
348 1.2 thorpej
349 1.3 nathanw PTHREADD_ADD(PTHREADD_COND_BROADCAST);
350 1.2 thorpej
351 1.27 ad if (PTQ_EMPTY(&cond->ptc_waiters))
352 1.27 ad return 0;
353 1.27 ad
354 1.27 ad self = pthread__self();
355 1.27 ad pthread_spinlock(self, &cond->ptc_lock);
356 1.28 ad mutex = cond->ptc_mutex;
357 1.27 ad cond->ptc_mutex = NULL;
358 1.28 ad
359 1.28 ad /*
360 1.28 ad * Try to transfer waiters to the mutex's waiters list (see
361 1.28 ad * pthread_cond_signal()). Only transfer waiters for which
362 1.28 ad * there is no pending wakeup.
363 1.28 ad */
364 1.28 ad if (self->pt_mutexhint != NULL && self->pt_mutexhint == mutex) {
365 1.28 ad pthread_spinlock(self, &mutex->ptm_interlock);
366 1.28 ad for (signaled = PTQ_FIRST(&cond->ptc_waiters);
367 1.28 ad signaled != NULL;
368 1.28 ad signaled = next) {
369 1.28 ad next = PTQ_NEXT(signaled, pt_sleep);
370 1.28 ad if (__predict_false(signaled->pt_sleepobj == NULL))
371 1.28 ad continue;
372 1.28 ad PTQ_REMOVE(&cond->ptc_waiters, signaled, pt_sleep);
373 1.28 ad PTQ_INSERT_HEAD(&mutex->ptm_blocked, signaled,
374 1.28 ad pt_sleep);
375 1.28 ad }
376 1.28 ad pthread_spinunlock(self, &mutex->ptm_interlock);
377 1.28 ad pthread_spinunlock(self, &cond->ptc_lock);
378 1.28 ad } else
379 1.28 ad pthread__unpark_all(self, &cond->ptc_lock, &cond->ptc_waiters);
380 1.28 ad
381 1.27 ad PTHREADD_ADD(PTHREADD_COND_WOKEUP);
382 1.2 thorpej
383 1.2 thorpej return 0;
384 1.2 thorpej
385 1.2 thorpej }
386 1.2 thorpej
387 1.2 thorpej
388 1.2 thorpej int
389 1.2 thorpej pthread_condattr_init(pthread_condattr_t *attr)
390 1.2 thorpej {
391 1.2 thorpej
392 1.2 thorpej attr->ptca_magic = _PT_CONDATTR_MAGIC;
393 1.2 thorpej
394 1.2 thorpej return 0;
395 1.2 thorpej }
396 1.2 thorpej
397 1.2 thorpej
398 1.2 thorpej int
399 1.2 thorpej pthread_condattr_destroy(pthread_condattr_t *attr)
400 1.2 thorpej {
401 1.2 thorpej
402 1.11 nathanw pthread__error(EINVAL, "Invalid condition variable attribute",
403 1.11 nathanw attr->ptca_magic == _PT_CONDATTR_MAGIC);
404 1.2 thorpej
405 1.2 thorpej attr->ptca_magic = _PT_CONDATTR_DEAD;
406 1.2 thorpej
407 1.2 thorpej return 0;
408 1.6 nathanw }
409 1.6 nathanw
410 1.6 nathanw /* Utility routine to hang out for a while if threads haven't started yet. */
411 1.6 nathanw static int
412 1.6 nathanw pthread_cond_wait_nothread(pthread_t self, pthread_mutex_t *mutex,
413 1.6 nathanw const struct timespec *abstime)
414 1.6 nathanw {
415 1.18 mycroft struct timespec now, diff;
416 1.6 nathanw int retval;
417 1.6 nathanw
418 1.18 mycroft if (abstime == NULL) {
419 1.18 mycroft diff.tv_sec = 99999999;
420 1.18 mycroft diff.tv_nsec = 0;
421 1.18 mycroft } else {
422 1.18 mycroft clock_gettime(CLOCK_REALTIME, &now);
423 1.18 mycroft if (timespeccmp(abstime, &now, <))
424 1.18 mycroft timespecclear(&diff);
425 1.17 nathanw else
426 1.18 mycroft timespecsub(abstime, &now, &diff);
427 1.6 nathanw }
428 1.6 nathanw
429 1.18 mycroft do {
430 1.18 mycroft pthread__testcancel(self);
431 1.18 mycroft pthread_mutex_unlock(mutex);
432 1.18 mycroft retval = _sys_nanosleep(&diff, NULL);
433 1.18 mycroft pthread_mutex_lock(mutex);
434 1.18 mycroft } while (abstime == NULL && retval == 0);
435 1.6 nathanw pthread__testcancel(self);
436 1.6 nathanw
437 1.6 nathanw if (retval == 0)
438 1.6 nathanw return ETIMEDOUT;
439 1.6 nathanw else
440 1.15 kleink /* spurious wakeup */
441 1.15 kleink return 0;
442 1.2 thorpej }
443