pthread_cond.c revision 1.35 1 1.35 ad /* $NetBSD: pthread_cond.c,v 1.35 2007/09/07 14:09:27 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.35 ad __RCSID("$NetBSD: pthread_cond.c,v 1.35 2007/09/07 14:09:27 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.35 ad mutex->ptm_owner != NULL);
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.34 ad pthread_spinlock(&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.34 ad pthread_spinunlock(&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.35 ad pthread_mutex_lock(mutex);
149 1.32 ad
150 1.32 ad /*
151 1.32 ad * If we awoke abnormally the waiters list will have been
152 1.32 ad * made empty by the current thread (in pthread__park()),
153 1.32 ad * so we can check the value safely without locking.
154 1.32 ad *
155 1.32 ad * Otherwise, it will have been updated by whichever thread
156 1.32 ad * last issued a wakeup.
157 1.32 ad */
158 1.32 ad if (PTQ_EMPTY(&cond->ptc_waiters) && cond->ptc_mutex != NULL) {
159 1.34 ad pthread_spinlock(&cond->ptc_lock);
160 1.32 ad if (PTQ_EMPTY(&cond->ptc_waiters))
161 1.32 ad cond->ptc_mutex = NULL;
162 1.34 ad pthread_spinunlock(&cond->ptc_lock);
163 1.32 ad }
164 1.32 ad
165 1.32 ad /*
166 1.35 ad * If we have cancelled then exit. POSIX dictates that the
167 1.35 ad * mutex must be held when we action the cancellation.
168 1.32 ad */
169 1.31 ad if (__predict_false(self->pt_cancel)) {
170 1.31 ad if (self->pt_signalled)
171 1.31 ad pthread_cond_signal(cond);
172 1.12 nathanw pthread_exit(PTHREAD_CANCELED);
173 1.31 ad }
174 1.12 nathanw
175 1.2 thorpej return 0;
176 1.2 thorpej }
177 1.2 thorpej
178 1.2 thorpej int
179 1.2 thorpej pthread_cond_timedwait(pthread_cond_t *cond, pthread_mutex_t *mutex,
180 1.2 thorpej const struct timespec *abstime)
181 1.2 thorpej {
182 1.2 thorpej pthread_t self;
183 1.2 thorpej int retval;
184 1.2 thorpej
185 1.11 nathanw pthread__error(EINVAL, "Invalid condition variable",
186 1.11 nathanw cond->ptc_magic == _PT_COND_MAGIC);
187 1.11 nathanw pthread__error(EINVAL, "Invalid mutex",
188 1.11 nathanw mutex->ptm_magic == _PT_MUTEX_MAGIC);
189 1.11 nathanw pthread__error(EPERM, "Mutex not locked in condition wait",
190 1.35 ad mutex->ptm_owner != NULL);
191 1.11 nathanw pthread__error(EINVAL, "Invalid wait time",
192 1.11 nathanw (abstime->tv_sec >= 0) &&
193 1.10 nathanw (abstime->tv_nsec >= 0) && (abstime->tv_nsec < 1000000000));
194 1.10 nathanw
195 1.2 thorpej self = pthread__self();
196 1.6 nathanw PTHREADD_ADD(PTHREADD_COND_TIMEDWAIT);
197 1.6 nathanw
198 1.6 nathanw /* Just hang out for a while if threads aren't running yet. */
199 1.6 nathanw if (__predict_false(pthread__started == 0))
200 1.6 nathanw return pthread_cond_wait_nothread(self, mutex, abstime);
201 1.6 nathanw
202 1.25 ad if (__predict_false(self->pt_cancel))
203 1.21 ad pthread_exit(PTHREAD_CANCELED);
204 1.32 ad
205 1.32 ad /*
206 1.32 ad * Note this thread as waiting on the CV. To ensure good
207 1.32 ad * performance it's critical that the spinlock is held for
208 1.32 ad * as short a time as possible - that means no system calls.
209 1.32 ad */
210 1.34 ad pthread_spinlock(&cond->ptc_lock);
211 1.21 ad if (cond->ptc_mutex == NULL)
212 1.21 ad cond->ptc_mutex = mutex;
213 1.28 ad else {
214 1.28 ad #ifdef ERRORCHECK
215 1.21 ad pthread__error(EINVAL,
216 1.21 ad "Multiple mutexes used for condition wait",
217 1.21 ad cond->ptc_mutex == mutex);
218 1.21 ad #endif
219 1.28 ad }
220 1.23 ad PTQ_INSERT_HEAD(&cond->ptc_waiters, self, pt_sleep);
221 1.31 ad self->pt_signalled = 0;
222 1.23 ad self->pt_sleeponq = 1;
223 1.26 ad self->pt_sleepobj = &cond->ptc_waiters;
224 1.34 ad pthread_spinunlock(&cond->ptc_lock);
225 1.32 ad
226 1.33 ad /*
227 1.33 ad * Before releasing the mutex, note that this thread is
228 1.33 ad * about to block by setting the willpark flag. If there
229 1.33 ad * is a single waiter on the mutex, setting the flag will
230 1.33 ad * defer restarting it until calling into the kernel to
231 1.33 ad * park, saving a syscall & involuntary context switch.
232 1.33 ad */
233 1.33 ad self->pt_willpark = 1;
234 1.20 ad pthread_mutex_unlock(mutex);
235 1.26 ad retval = pthread__park(self, &cond->ptc_lock, &cond->ptc_waiters,
236 1.30 ad abstime, 1, &mutex->ptm_blocked);
237 1.35 ad pthread_mutex_lock(mutex);
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.34 ad pthread_spinlock(&cond->ptc_lock);
249 1.32 ad if (PTQ_EMPTY(&cond->ptc_waiters))
250 1.32 ad cond->ptc_mutex = NULL;
251 1.34 ad pthread_spinunlock(&cond->ptc_lock);
252 1.32 ad }
253 1.32 ad
254 1.32 ad /*
255 1.35 ad * If we have cancelled then exit. POSIX dictates that the
256 1.35 ad * mutex must be held when we action the cancellation.
257 1.32 ad */
258 1.31 ad if (__predict_false(self->pt_cancel | retval)) {
259 1.31 ad if (self->pt_signalled)
260 1.31 ad pthread_cond_signal(cond);
261 1.31 ad if (self->pt_cancel)
262 1.31 ad pthread_exit(PTHREAD_CANCELED);
263 1.31 ad }
264 1.2 thorpej
265 1.2 thorpej return retval;
266 1.2 thorpej }
267 1.2 thorpej
268 1.2 thorpej int
269 1.2 thorpej pthread_cond_signal(pthread_cond_t *cond)
270 1.2 thorpej {
271 1.2 thorpej pthread_t self, signaled;
272 1.28 ad pthread_mutex_t *mutex;
273 1.10 nathanw
274 1.11 nathanw pthread__error(EINVAL, "Invalid condition variable",
275 1.11 nathanw cond->ptc_magic == _PT_COND_MAGIC);
276 1.3 nathanw PTHREADD_ADD(PTHREADD_COND_SIGNAL);
277 1.2 thorpej
278 1.27 ad if (PTQ_EMPTY(&cond->ptc_waiters))
279 1.27 ad return 0;
280 1.27 ad
281 1.27 ad self = pthread__self();
282 1.34 ad pthread_spinlock(&cond->ptc_lock);
283 1.28 ad
284 1.28 ad /*
285 1.28 ad * Find a thread that is still blocked (no pending wakeup).
286 1.28 ad * A wakeup can be pending if we have interrupted unpark_all
287 1.28 ad * as it releases the interlock.
288 1.28 ad */
289 1.28 ad PTQ_FOREACH(signaled, &cond->ptc_waiters, pt_sleep) {
290 1.28 ad if (signaled->pt_sleepobj != NULL)
291 1.28 ad break;
292 1.28 ad }
293 1.28 ad if (__predict_false(signaled == NULL)) {
294 1.28 ad cond->ptc_mutex = NULL;
295 1.34 ad pthread_spinunlock(&cond->ptc_lock);
296 1.28 ad return 0;
297 1.28 ad }
298 1.28 ad
299 1.28 ad /*
300 1.31 ad * Pull the thread off the queue, and set pt_signalled.
301 1.31 ad *
302 1.31 ad * After resuming execution, the thread must check to see if it
303 1.31 ad * has been restarted as a result of pthread_cond_signal(). If it
304 1.32 ad * has, but cannot take the wakeup (because of eg a timeout) then
305 1.32 ad * try to ensure that another thread sees it. This is necessary
306 1.32 ad * because there may be multiple waiters, and at least one should
307 1.32 ad * take the wakeup if possible.
308 1.28 ad */
309 1.28 ad PTQ_REMOVE(&cond->ptc_waiters, signaled, pt_sleep);
310 1.28 ad mutex = cond->ptc_mutex;
311 1.28 ad if (PTQ_EMPTY(&cond->ptc_waiters))
312 1.28 ad cond->ptc_mutex = NULL;
313 1.31 ad signaled->pt_signalled = 1;
314 1.28 ad
315 1.28 ad /*
316 1.28 ad * For all valid uses of pthread_cond_signal(), the caller will
317 1.28 ad * hold the mutex that the target is using to synchronize with.
318 1.28 ad * To avoid the target awakening and immediatley blocking on the
319 1.35 ad * mutex, transfer the thread to be awoken to the current thread's
320 1.35 ad * deferred wakeup list. The waiter will be set running when the
321 1.35 ad * caller (this thread) releases the mutex.
322 1.28 ad */
323 1.35 ad if (self->pt_mutexhint != NULL && self->pt_mutexhint == mutex &&
324 1.35 ad self->pt_nwaiters < pthread__unpark_max) {
325 1.35 ad signaled->pt_sleepobj = NULL;
326 1.35 ad signaled->pt_sleeponq = 0;
327 1.34 ad pthread_spinunlock(&cond->ptc_lock);
328 1.35 ad self->pt_waiters[self->pt_nwaiters++] = signaled->pt_lid;
329 1.28 ad } else {
330 1.27 ad pthread__unpark(self, &cond->ptc_lock,
331 1.27 ad &cond->ptc_waiters, signaled);
332 1.4 nathanw }
333 1.29 ad PTHREADD_ADD(PTHREADD_COND_WOKEUP);
334 1.22 ad
335 1.2 thorpej return 0;
336 1.2 thorpej }
337 1.2 thorpej
338 1.2 thorpej
339 1.2 thorpej int
340 1.2 thorpej pthread_cond_broadcast(pthread_cond_t *cond)
341 1.2 thorpej {
342 1.28 ad pthread_t self, signaled, next;
343 1.28 ad pthread_mutex_t *mutex;
344 1.10 nathanw
345 1.28 ad pthread__error(EINVAL, "Invalid condition variable",
346 1.28 ad cond->ptc_magic == _PT_COND_MAGIC);
347 1.2 thorpej
348 1.3 nathanw PTHREADD_ADD(PTHREADD_COND_BROADCAST);
349 1.2 thorpej
350 1.27 ad if (PTQ_EMPTY(&cond->ptc_waiters))
351 1.27 ad return 0;
352 1.27 ad
353 1.27 ad self = pthread__self();
354 1.34 ad pthread_spinlock(&cond->ptc_lock);
355 1.28 ad mutex = cond->ptc_mutex;
356 1.27 ad cond->ptc_mutex = NULL;
357 1.28 ad
358 1.28 ad /*
359 1.35 ad * Try to defer waking threads (see pthread_cond_signal()).
360 1.35 ad * Only transfer waiters for which there is no pending wakeup.
361 1.28 ad */
362 1.28 ad if (self->pt_mutexhint != NULL && self->pt_mutexhint == mutex) {
363 1.28 ad for (signaled = PTQ_FIRST(&cond->ptc_waiters);
364 1.28 ad signaled != NULL;
365 1.28 ad signaled = next) {
366 1.28 ad next = PTQ_NEXT(signaled, pt_sleep);
367 1.28 ad if (__predict_false(signaled->pt_sleepobj == NULL))
368 1.28 ad continue;
369 1.35 ad if (self->pt_nwaiters == pthread__unpark_max) {
370 1.35 ad /* Overflow, take the slow path. */
371 1.35 ad break;
372 1.35 ad }
373 1.28 ad PTQ_REMOVE(&cond->ptc_waiters, signaled, pt_sleep);
374 1.35 ad signaled->pt_sleepobj = NULL;
375 1.35 ad signaled->pt_sleeponq = 0;
376 1.35 ad self->pt_waiters[self->pt_nwaiters++] =
377 1.35 ad signaled->pt_lid;
378 1.35 ad }
379 1.35 ad if (signaled == NULL) {
380 1.35 ad /* Anything more to do? */
381 1.35 ad pthread_spinunlock(&cond->ptc_lock);
382 1.35 ad return 0;
383 1.28 ad }
384 1.35 ad }
385 1.35 ad pthread__unpark_all(self, &cond->ptc_lock, &cond->ptc_waiters);
386 1.28 ad
387 1.27 ad PTHREADD_ADD(PTHREADD_COND_WOKEUP);
388 1.2 thorpej
389 1.2 thorpej return 0;
390 1.2 thorpej
391 1.2 thorpej }
392 1.2 thorpej
393 1.2 thorpej
394 1.2 thorpej int
395 1.2 thorpej pthread_condattr_init(pthread_condattr_t *attr)
396 1.2 thorpej {
397 1.2 thorpej
398 1.2 thorpej attr->ptca_magic = _PT_CONDATTR_MAGIC;
399 1.2 thorpej
400 1.2 thorpej return 0;
401 1.2 thorpej }
402 1.2 thorpej
403 1.2 thorpej
404 1.2 thorpej int
405 1.2 thorpej pthread_condattr_destroy(pthread_condattr_t *attr)
406 1.2 thorpej {
407 1.2 thorpej
408 1.11 nathanw pthread__error(EINVAL, "Invalid condition variable attribute",
409 1.11 nathanw attr->ptca_magic == _PT_CONDATTR_MAGIC);
410 1.2 thorpej
411 1.2 thorpej attr->ptca_magic = _PT_CONDATTR_DEAD;
412 1.2 thorpej
413 1.2 thorpej return 0;
414 1.6 nathanw }
415 1.6 nathanw
416 1.6 nathanw /* Utility routine to hang out for a while if threads haven't started yet. */
417 1.6 nathanw static int
418 1.6 nathanw pthread_cond_wait_nothread(pthread_t self, pthread_mutex_t *mutex,
419 1.6 nathanw const struct timespec *abstime)
420 1.6 nathanw {
421 1.18 mycroft struct timespec now, diff;
422 1.6 nathanw int retval;
423 1.6 nathanw
424 1.18 mycroft if (abstime == NULL) {
425 1.18 mycroft diff.tv_sec = 99999999;
426 1.18 mycroft diff.tv_nsec = 0;
427 1.18 mycroft } else {
428 1.18 mycroft clock_gettime(CLOCK_REALTIME, &now);
429 1.18 mycroft if (timespeccmp(abstime, &now, <))
430 1.18 mycroft timespecclear(&diff);
431 1.17 nathanw else
432 1.18 mycroft timespecsub(abstime, &now, &diff);
433 1.6 nathanw }
434 1.6 nathanw
435 1.18 mycroft do {
436 1.18 mycroft pthread__testcancel(self);
437 1.18 mycroft pthread_mutex_unlock(mutex);
438 1.18 mycroft retval = _sys_nanosleep(&diff, NULL);
439 1.18 mycroft pthread_mutex_lock(mutex);
440 1.18 mycroft } while (abstime == NULL && retval == 0);
441 1.6 nathanw pthread__testcancel(self);
442 1.6 nathanw
443 1.6 nathanw if (retval == 0)
444 1.6 nathanw return ETIMEDOUT;
445 1.6 nathanw else
446 1.15 kleink /* spurious wakeup */
447 1.15 kleink return 0;
448 1.2 thorpej }
449