pthread_cond.c revision 1.19 1 1.18 mycroft /* $NetBSD: pthread_cond.c,v 1.19 2006/12/23 05:14:46 ad 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.18 mycroft __RCSID("$NetBSD: pthread_cond.c,v 1.19 2006/12/23 05:14:46 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.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.18 mycroft int _sys_nanosleep(const struct timespec *, struct timespec *);
56 1.6 nathanw
57 1.6 nathanw extern int pthread__started;
58 1.6 nathanw
59 1.19 ad #ifdef PTHREAD_SA
60 1.2 thorpej static void pthread_cond_wait__callback(void *);
61 1.19 ad #endif
62 1.6 nathanw static int pthread_cond_wait_nothread(pthread_t, pthread_mutex_t *,
63 1.6 nathanw const struct timespec *);
64 1.2 thorpej
65 1.2 thorpej __strong_alias(__libc_cond_init,pthread_cond_init)
66 1.2 thorpej __strong_alias(__libc_cond_signal,pthread_cond_signal)
67 1.2 thorpej __strong_alias(__libc_cond_broadcast,pthread_cond_broadcast)
68 1.2 thorpej __strong_alias(__libc_cond_wait,pthread_cond_wait)
69 1.2 thorpej __strong_alias(__libc_cond_timedwait,pthread_cond_timedwait)
70 1.2 thorpej __strong_alias(__libc_cond_destroy,pthread_cond_destroy)
71 1.2 thorpej
72 1.2 thorpej int
73 1.2 thorpej pthread_cond_init(pthread_cond_t *cond, const pthread_condattr_t *attr)
74 1.2 thorpej {
75 1.2 thorpej
76 1.11 nathanw pthread__error(EINVAL, "Invalid condition variable attribute",
77 1.11 nathanw (attr == NULL) || (attr->ptca_magic == _PT_CONDATTR_MAGIC));
78 1.2 thorpej
79 1.2 thorpej cond->ptc_magic = _PT_COND_MAGIC;
80 1.2 thorpej pthread_lockinit(&cond->ptc_lock);
81 1.2 thorpej PTQ_INIT(&cond->ptc_waiters);
82 1.2 thorpej cond->ptc_mutex = NULL;
83 1.2 thorpej
84 1.2 thorpej return 0;
85 1.2 thorpej }
86 1.2 thorpej
87 1.2 thorpej
88 1.2 thorpej int
89 1.2 thorpej pthread_cond_destroy(pthread_cond_t *cond)
90 1.2 thorpej {
91 1.2 thorpej
92 1.11 nathanw pthread__error(EINVAL, "Invalid condition variable",
93 1.11 nathanw cond->ptc_magic == _PT_COND_MAGIC);
94 1.11 nathanw pthread__error(EBUSY, "Destroying condition variable in use",
95 1.11 nathanw cond->ptc_mutex == NULL);
96 1.2 thorpej
97 1.2 thorpej cond->ptc_magic = _PT_COND_DEAD;
98 1.2 thorpej
99 1.2 thorpej return 0;
100 1.2 thorpej }
101 1.2 thorpej
102 1.2 thorpej
103 1.2 thorpej int
104 1.2 thorpej pthread_cond_wait(pthread_cond_t *cond, pthread_mutex_t *mutex)
105 1.2 thorpej {
106 1.2 thorpej pthread_t self;
107 1.10 nathanw
108 1.11 nathanw pthread__error(EINVAL, "Invalid condition variable",
109 1.11 nathanw cond->ptc_magic == _PT_COND_MAGIC);
110 1.11 nathanw pthread__error(EINVAL, "Invalid mutex",
111 1.11 nathanw mutex->ptm_magic == _PT_MUTEX_MAGIC);
112 1.11 nathanw pthread__error(EPERM, "Mutex not locked in condition wait",
113 1.11 nathanw mutex->ptm_lock == __SIMPLELOCK_LOCKED);
114 1.10 nathanw
115 1.2 thorpej self = pthread__self();
116 1.3 nathanw PTHREADD_ADD(PTHREADD_COND_WAIT);
117 1.6 nathanw
118 1.6 nathanw /* Just hang out for a while if threads aren't running yet. */
119 1.6 nathanw if (__predict_false(pthread__started == 0))
120 1.6 nathanw return pthread_cond_wait_nothread(self, mutex, NULL);
121 1.6 nathanw
122 1.2 thorpej pthread_spinlock(self, &cond->ptc_lock);
123 1.12 nathanw SDPRINTF(("(cond wait %p) Waiting on %p, mutex %p\n",
124 1.12 nathanw self, cond, mutex));
125 1.12 nathanw pthread_spinlock(self, &self->pt_statelock);
126 1.12 nathanw if (__predict_false(self->pt_cancel)) {
127 1.12 nathanw pthread_spinunlock(self, &self->pt_statelock);
128 1.12 nathanw pthread_spinunlock(self, &cond->ptc_lock);
129 1.12 nathanw pthread_exit(PTHREAD_CANCELED);
130 1.12 nathanw }
131 1.2 thorpej #ifdef ERRORCHECK
132 1.2 thorpej if (cond->ptc_mutex == NULL)
133 1.2 thorpej cond->ptc_mutex = mutex;
134 1.10 nathanw else
135 1.11 nathanw pthread__error(EINVAL,
136 1.11 nathanw "Multiple mutexes used for condition wait",
137 1.11 nathanw cond->ptc_mutex == mutex);
138 1.2 thorpej #endif
139 1.19 ad
140 1.19 ad #ifdef PTHREAD_SA
141 1.19 ad self->pt_sleepobj = cond;
142 1.2 thorpej self->pt_state = PT_STATE_BLOCKED_QUEUE;
143 1.2 thorpej self->pt_sleepq = &cond->ptc_waiters;
144 1.2 thorpej self->pt_sleeplock = &cond->ptc_lock;
145 1.2 thorpej pthread_spinunlock(self, &self->pt_statelock);
146 1.9 nathanw PTQ_INSERT_HEAD(&cond->ptc_waiters, self, pt_sleep);
147 1.2 thorpej pthread_mutex_unlock(mutex);
148 1.2 thorpej
149 1.2 thorpej pthread__block(self, &cond->ptc_lock);
150 1.2 thorpej /* Spinlock is unlocked on return */
151 1.19 ad #else /* PTHREAD_SA */
152 1.19 ad pthread_mutex_unlock(mutex);
153 1.19 ad pthread_spinunlock(self, &self->pt_statelock);
154 1.19 ad (void)pthread__park(self, &cond->ptc_lock, cond,
155 1.19 ad &cond->ptc_waiters, NULL, 0);
156 1.19 ad pthread_spinunlock(self, &cond->ptc_lock);
157 1.19 ad #endif /* PTHREAD_SA */
158 1.19 ad
159 1.2 thorpej pthread_mutex_lock(mutex);
160 1.12 nathanw #ifdef ERRORCHECK
161 1.16 nathanw pthread_spinlock(self, &cond->ptc_lock);
162 1.16 nathanw if (PTQ_EMPTY(&cond->ptc_waiters))
163 1.16 nathanw cond->ptc_mutex = NULL;
164 1.16 nathanw pthread_spinunlock(self, &cond->ptc_lock);
165 1.12 nathanw #endif
166 1.16 nathanw if (__predict_false(self->pt_cancel))
167 1.12 nathanw pthread_exit(PTHREAD_CANCELED);
168 1.12 nathanw
169 1.2 thorpej SDPRINTF(("(cond wait %p) Woke up on %p, mutex %p\n",
170 1.2 thorpej self, cond, mutex));
171 1.2 thorpej
172 1.2 thorpej return 0;
173 1.2 thorpej }
174 1.2 thorpej
175 1.2 thorpej
176 1.2 thorpej struct pthread_cond__waitarg {
177 1.2 thorpej pthread_t ptw_thread;
178 1.2 thorpej pthread_cond_t *ptw_cond;
179 1.2 thorpej };
180 1.2 thorpej
181 1.2 thorpej int
182 1.2 thorpej pthread_cond_timedwait(pthread_cond_t *cond, pthread_mutex_t *mutex,
183 1.2 thorpej const struct timespec *abstime)
184 1.2 thorpej {
185 1.2 thorpej pthread_t self;
186 1.2 thorpej struct pthread_cond__waitarg wait;
187 1.19 ad #ifdef PTHREAD_SA
188 1.2 thorpej struct pt_alarm_t alarm;
189 1.19 ad #endif
190 1.2 thorpej int retval;
191 1.2 thorpej
192 1.11 nathanw pthread__error(EINVAL, "Invalid condition variable",
193 1.11 nathanw cond->ptc_magic == _PT_COND_MAGIC);
194 1.11 nathanw pthread__error(EINVAL, "Invalid mutex",
195 1.11 nathanw mutex->ptm_magic == _PT_MUTEX_MAGIC);
196 1.11 nathanw pthread__error(EPERM, "Mutex not locked in condition wait",
197 1.11 nathanw mutex->ptm_lock == __SIMPLELOCK_LOCKED);
198 1.11 nathanw pthread__error(EINVAL, "Invalid wait time",
199 1.11 nathanw (abstime->tv_sec >= 0) &&
200 1.10 nathanw (abstime->tv_nsec >= 0) && (abstime->tv_nsec < 1000000000));
201 1.10 nathanw
202 1.2 thorpej self = pthread__self();
203 1.6 nathanw PTHREADD_ADD(PTHREADD_COND_TIMEDWAIT);
204 1.6 nathanw
205 1.6 nathanw /* Just hang out for a while if threads aren't running yet. */
206 1.6 nathanw if (__predict_false(pthread__started == 0))
207 1.6 nathanw return pthread_cond_wait_nothread(self, mutex, abstime);
208 1.6 nathanw
209 1.2 thorpej pthread_spinlock(self, &cond->ptc_lock);
210 1.2 thorpej wait.ptw_thread = self;
211 1.2 thorpej wait.ptw_cond = cond;
212 1.2 thorpej retval = 0;
213 1.2 thorpej SDPRINTF(("(cond timed wait %p) Waiting on %p until %d.%06ld\n",
214 1.2 thorpej self, cond, abstime->tv_sec, abstime->tv_nsec/1000));
215 1.2 thorpej
216 1.2 thorpej pthread_spinlock(self, &self->pt_statelock);
217 1.12 nathanw if (__predict_false(self->pt_cancel)) {
218 1.2 thorpej pthread_spinunlock(self, &self->pt_statelock);
219 1.2 thorpej pthread_spinunlock(self, &cond->ptc_lock);
220 1.2 thorpej pthread_exit(PTHREAD_CANCELED);
221 1.2 thorpej }
222 1.12 nathanw #ifdef ERRORCHECK
223 1.12 nathanw if (cond->ptc_mutex == NULL)
224 1.12 nathanw cond->ptc_mutex = mutex;
225 1.12 nathanw else
226 1.12 nathanw pthread__error(EINVAL,
227 1.12 nathanw "Multiple mutexes used for condition wait",
228 1.12 nathanw cond->ptc_mutex == mutex);
229 1.12 nathanw #endif
230 1.19 ad
231 1.19 ad #ifdef PTHREAD_SA
232 1.2 thorpej pthread__alarm_add(self, &alarm, abstime, pthread_cond_wait__callback,
233 1.2 thorpej &wait);
234 1.2 thorpej self->pt_state = PT_STATE_BLOCKED_QUEUE;
235 1.2 thorpej self->pt_sleepobj = cond;
236 1.2 thorpej self->pt_sleepq = &cond->ptc_waiters;
237 1.2 thorpej self->pt_sleeplock = &cond->ptc_lock;
238 1.2 thorpej pthread_spinunlock(self, &self->pt_statelock);
239 1.2 thorpej
240 1.9 nathanw PTQ_INSERT_HEAD(&cond->ptc_waiters, self, pt_sleep);
241 1.2 thorpej pthread_mutex_unlock(mutex);
242 1.2 thorpej
243 1.2 thorpej pthread__block(self, &cond->ptc_lock);
244 1.2 thorpej /* Spinlock is unlocked on return */
245 1.2 thorpej pthread__alarm_del(self, &alarm);
246 1.2 thorpej if (pthread__alarm_fired(&alarm))
247 1.2 thorpej retval = ETIMEDOUT;
248 1.19 ad #else /* PTHREAD_SA */
249 1.19 ad pthread_spinunlock(self, &self->pt_statelock);
250 1.19 ad retval = pthread__park(self, &cond->ptc_lock, cond,
251 1.19 ad &cond->ptc_waiters, abstime, 0);
252 1.19 ad pthread_spinunlock(self, &cond->ptc_lock);
253 1.19 ad #endif /* PTHREAD_SA */
254 1.19 ad
255 1.19 ad SDPRINTF(("(cond timed wait %p) Woke up on %p, mutex %p\n",
256 1.19 ad self, cond));
257 1.2 thorpej SDPRINTF(("(cond timed wait %p) %s\n",
258 1.2 thorpej self, (retval == ETIMEDOUT) ? "(timed out)" : ""));
259 1.2 thorpej pthread_mutex_lock(mutex);
260 1.12 nathanw #ifdef ERRORCHECK
261 1.16 nathanw pthread_spinlock(self, &cond->ptc_lock);
262 1.16 nathanw if (PTQ_EMPTY(&cond->ptc_waiters))
263 1.16 nathanw cond->ptc_mutex = NULL;
264 1.16 nathanw pthread_spinunlock(self, &cond->ptc_lock);
265 1.12 nathanw #endif
266 1.16 nathanw if (__predict_false(self->pt_cancel))
267 1.12 nathanw pthread_exit(PTHREAD_CANCELED);
268 1.2 thorpej
269 1.2 thorpej return retval;
270 1.2 thorpej }
271 1.2 thorpej
272 1.19 ad #ifdef PTHREAD_SA
273 1.2 thorpej static void
274 1.2 thorpej pthread_cond_wait__callback(void *arg)
275 1.2 thorpej {
276 1.2 thorpej struct pthread_cond__waitarg *a;
277 1.2 thorpej pthread_t self;
278 1.2 thorpej
279 1.2 thorpej a = arg;
280 1.2 thorpej self = pthread__self();
281 1.2 thorpej
282 1.2 thorpej /*
283 1.2 thorpej * Don't dequeue and schedule the thread if it's already been
284 1.2 thorpej * queued up by a signal or broadcast (but hasn't yet run as far
285 1.2 thorpej * as pthread__alarm_del(), or we wouldn't be here, and hence can't
286 1.2 thorpej * have become blocked on some *other* queue).
287 1.2 thorpej */
288 1.2 thorpej pthread_spinlock(self, &a->ptw_cond->ptc_lock);
289 1.2 thorpej if (a->ptw_thread->pt_state == PT_STATE_BLOCKED_QUEUE) {
290 1.2 thorpej PTQ_REMOVE(&a->ptw_cond->ptc_waiters, a->ptw_thread, pt_sleep);
291 1.2 thorpej #ifdef ERRORCHECK
292 1.2 thorpej if (PTQ_EMPTY(&a->ptw_cond->ptc_waiters))
293 1.2 thorpej a->ptw_cond->ptc_mutex = NULL;
294 1.2 thorpej #endif
295 1.2 thorpej pthread__sched(self, a->ptw_thread);
296 1.2 thorpej }
297 1.2 thorpej pthread_spinunlock(self, &a->ptw_cond->ptc_lock);
298 1.2 thorpej }
299 1.19 ad #endif /* PTHREAD_SA */
300 1.2 thorpej
301 1.2 thorpej int
302 1.2 thorpej pthread_cond_signal(pthread_cond_t *cond)
303 1.2 thorpej {
304 1.2 thorpej pthread_t self, signaled;
305 1.10 nathanw
306 1.11 nathanw pthread__error(EINVAL, "Invalid condition variable",
307 1.11 nathanw cond->ptc_magic == _PT_COND_MAGIC);
308 1.3 nathanw PTHREADD_ADD(PTHREADD_COND_SIGNAL);
309 1.2 thorpej
310 1.2 thorpej SDPRINTF(("(cond signal %p) Signaling %p\n",
311 1.4 nathanw pthread__self(), cond));
312 1.2 thorpej
313 1.4 nathanw if (!PTQ_EMPTY(&cond->ptc_waiters)) {
314 1.4 nathanw self = pthread__self();
315 1.4 nathanw pthread_spinlock(self, &cond->ptc_lock);
316 1.4 nathanw signaled = PTQ_FIRST(&cond->ptc_waiters);
317 1.14 cl if (signaled != NULL) {
318 1.4 nathanw PTQ_REMOVE(&cond->ptc_waiters, signaled, pt_sleep);
319 1.19 ad #ifdef PTHREAD_SA
320 1.14 cl pthread__sched(self, signaled);
321 1.19 ad #endif /* PTHREAD_SA */
322 1.14 cl PTHREADD_ADD(PTHREADD_COND_WOKEUP);
323 1.14 cl }
324 1.2 thorpej #ifdef ERRORCHECK
325 1.4 nathanw if (PTQ_EMPTY(&cond->ptc_waiters))
326 1.4 nathanw cond->ptc_mutex = NULL;
327 1.2 thorpej #endif
328 1.19 ad #ifdef PTHREAD_SA
329 1.13 nathanw pthread_spinunlock(self, &cond->ptc_lock);
330 1.19 ad #else /* PTHREAD_SA */
331 1.19 ad pthread__unpark(self, &cond->ptc_lock, cond, signaled);
332 1.19 ad #endif /* !PTHREAD_SA */
333 1.4 nathanw }
334 1.2 thorpej
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.5 nathanw pthread_t self;
343 1.2 thorpej struct pthread_queue_t blockedq;
344 1.10 nathanw
345 1.11 nathanw pthread__error(EINVAL, "Invalid condition variable", cond->ptc_magic == _PT_COND_MAGIC);
346 1.2 thorpej
347 1.3 nathanw PTHREADD_ADD(PTHREADD_COND_BROADCAST);
348 1.2 thorpej SDPRINTF(("(cond signal %p) Broadcasting %p\n",
349 1.4 nathanw pthread__self(), cond));
350 1.2 thorpej
351 1.4 nathanw if (!PTQ_EMPTY(&cond->ptc_waiters)) {
352 1.4 nathanw self = pthread__self();
353 1.4 nathanw pthread_spinlock(self, &cond->ptc_lock);
354 1.4 nathanw blockedq = cond->ptc_waiters;
355 1.4 nathanw PTQ_INIT(&cond->ptc_waiters);
356 1.2 thorpej #ifdef ERRORCHECK
357 1.4 nathanw cond->ptc_mutex = NULL;
358 1.2 thorpej #endif
359 1.19 ad #ifdef PTHREAD_SA
360 1.5 nathanw pthread__sched_sleepers(self, &blockedq);
361 1.7 nathanw PTHREADD_ADD(PTHREADD_COND_WOKEUP);
362 1.13 nathanw pthread_spinunlock(self, &cond->ptc_lock);
363 1.19 ad #else /* PTHREAD_SA */
364 1.19 ad pthread__unpark_all(self, &cond->ptc_lock, cond, &blockedq);
365 1.19 ad PTHREADD_ADD(PTHREADD_COND_WOKEUP);
366 1.19 ad #endif /* PTHREAD_SA */
367 1.4 nathanw }
368 1.2 thorpej
369 1.2 thorpej return 0;
370 1.2 thorpej
371 1.2 thorpej }
372 1.2 thorpej
373 1.2 thorpej
374 1.2 thorpej int
375 1.2 thorpej pthread_condattr_init(pthread_condattr_t *attr)
376 1.2 thorpej {
377 1.2 thorpej
378 1.2 thorpej attr->ptca_magic = _PT_CONDATTR_MAGIC;
379 1.2 thorpej
380 1.2 thorpej return 0;
381 1.2 thorpej }
382 1.2 thorpej
383 1.2 thorpej
384 1.2 thorpej int
385 1.2 thorpej pthread_condattr_destroy(pthread_condattr_t *attr)
386 1.2 thorpej {
387 1.2 thorpej
388 1.11 nathanw pthread__error(EINVAL, "Invalid condition variable attribute",
389 1.11 nathanw attr->ptca_magic == _PT_CONDATTR_MAGIC);
390 1.2 thorpej
391 1.2 thorpej attr->ptca_magic = _PT_CONDATTR_DEAD;
392 1.2 thorpej
393 1.2 thorpej return 0;
394 1.6 nathanw }
395 1.6 nathanw
396 1.6 nathanw /* Utility routine to hang out for a while if threads haven't started yet. */
397 1.6 nathanw static int
398 1.6 nathanw pthread_cond_wait_nothread(pthread_t self, pthread_mutex_t *mutex,
399 1.6 nathanw const struct timespec *abstime)
400 1.6 nathanw {
401 1.18 mycroft struct timespec now, diff;
402 1.6 nathanw int retval;
403 1.6 nathanw
404 1.18 mycroft if (abstime == NULL) {
405 1.18 mycroft diff.tv_sec = 99999999;
406 1.18 mycroft diff.tv_nsec = 0;
407 1.18 mycroft } else {
408 1.18 mycroft clock_gettime(CLOCK_REALTIME, &now);
409 1.18 mycroft if (timespeccmp(abstime, &now, <))
410 1.18 mycroft timespecclear(&diff);
411 1.17 nathanw else
412 1.18 mycroft timespecsub(abstime, &now, &diff);
413 1.6 nathanw }
414 1.6 nathanw
415 1.18 mycroft do {
416 1.18 mycroft pthread__testcancel(self);
417 1.18 mycroft pthread_mutex_unlock(mutex);
418 1.18 mycroft retval = _sys_nanosleep(&diff, NULL);
419 1.18 mycroft pthread_mutex_lock(mutex);
420 1.18 mycroft } while (abstime == NULL && retval == 0);
421 1.6 nathanw pthread__testcancel(self);
422 1.6 nathanw
423 1.6 nathanw if (retval == 0)
424 1.6 nathanw return ETIMEDOUT;
425 1.6 nathanw else
426 1.15 kleink /* spurious wakeup */
427 1.15 kleink return 0;
428 1.2 thorpej }
429