Home | History | Annotate | Line # | Download | only in libpthread
pthread_cond.c revision 1.61
      1  1.61  christos /*	$NetBSD: pthread_cond.c,v 1.61 2013/04/01 13:28:21 christos Exp $	*/
      2   1.2   thorpej 
      3   1.2   thorpej /*-
      4  1.43        ad  * Copyright (c) 2001, 2006, 2007, 2008 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  *
     19   1.2   thorpej  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     20   1.2   thorpej  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     21   1.2   thorpej  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     22   1.2   thorpej  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     23   1.2   thorpej  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     24   1.2   thorpej  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     25   1.2   thorpej  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     26   1.2   thorpej  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     27   1.2   thorpej  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     28   1.2   thorpej  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     29   1.2   thorpej  * POSSIBILITY OF SUCH DAMAGE.
     30   1.2   thorpej  */
     31   1.2   thorpej 
     32  1.47        ad /*
     33  1.47        ad  * We assume that there will be no contention on pthread_cond_t::ptc_lock
     34  1.47        ad  * because functioning applications must call both the wait and wakeup
     35  1.47        ad  * functions while holding the same application provided mutex.  The
     36  1.47        ad  * spinlock is present only to prevent libpthread causing the application
     37  1.47        ad  * to crash or malfunction as a result of corrupted data structures, in
     38  1.47        ad  * the event that the application is buggy.
     39  1.47        ad  *
     40  1.47        ad  * If there is contention on spinlock when real-time threads are in use,
     41  1.47        ad  * it could cause a deadlock due to priority inversion: the thread holding
     42  1.47        ad  * the spinlock may not get CPU time to make forward progress and release
     43  1.47        ad  * the spinlock to a higher priority thread that is waiting for it.
     44  1.47        ad  * Contention on the spinlock will only occur with buggy applications,
     45  1.47        ad  * so at the time of writing it's not considered a major bug in libpthread.
     46  1.47        ad  */
     47  1.47        ad 
     48   1.8     lukem #include <sys/cdefs.h>
     49  1.61  christos __RCSID("$NetBSD: pthread_cond.c,v 1.61 2013/04/01 13:28:21 christos Exp $");
     50   1.8     lukem 
     51  1.59  christos #include <stdlib.h>
     52   1.2   thorpej #include <errno.h>
     53   1.6   nathanw #include <sys/time.h>
     54   1.6   nathanw #include <sys/types.h>
     55   1.2   thorpej 
     56   1.2   thorpej #include "pthread.h"
     57   1.2   thorpej #include "pthread_int.h"
     58  1.59  christos #include "reentrant.h"
     59   1.2   thorpej 
     60  1.55  drochner int	_sys___nanosleep50(const struct timespec *, struct timespec *);
     61   1.6   nathanw 
     62   1.6   nathanw extern int pthread__started;
     63   1.6   nathanw 
     64   1.6   nathanw static int pthread_cond_wait_nothread(pthread_t, pthread_mutex_t *,
     65  1.58  christos     pthread_cond_t *, const struct timespec *);
     66   1.2   thorpej 
     67  1.51     pooka int	_pthread_cond_has_waiters_np(pthread_cond_t *);
     68  1.51     pooka 
     69  1.53      yamt __weak_alias(pthread_cond_has_waiters_np,_pthread_cond_has_waiters_np)
     70  1.51     pooka 
     71   1.2   thorpej __strong_alias(__libc_cond_init,pthread_cond_init)
     72   1.2   thorpej __strong_alias(__libc_cond_signal,pthread_cond_signal)
     73   1.2   thorpej __strong_alias(__libc_cond_broadcast,pthread_cond_broadcast)
     74   1.2   thorpej __strong_alias(__libc_cond_wait,pthread_cond_wait)
     75   1.2   thorpej __strong_alias(__libc_cond_timedwait,pthread_cond_timedwait)
     76   1.2   thorpej __strong_alias(__libc_cond_destroy,pthread_cond_destroy)
     77   1.2   thorpej 
     78  1.60  christos static clockid_t
     79  1.60  christos pthread_cond_getclock(const pthread_cond_t *cond)
     80  1.60  christos {
     81  1.60  christos 	return cond->ptc_private ?
     82  1.60  christos 	    *(clockid_t *)cond->ptc_private : CLOCK_REALTIME;
     83  1.60  christos }
     84  1.60  christos 
     85   1.2   thorpej int
     86   1.2   thorpej pthread_cond_init(pthread_cond_t *cond, const pthread_condattr_t *attr)
     87   1.2   thorpej {
     88  1.59  christos 	if (__predict_false(__uselibcstub))
     89  1.59  christos 		return __libc_cond_init_stub(cond, attr);
     90   1.2   thorpej 
     91  1.11   nathanw 	pthread__error(EINVAL, "Invalid condition variable attribute",
     92  1.11   nathanw 	    (attr == NULL) || (attr->ptca_magic == _PT_CONDATTR_MAGIC));
     93   1.2   thorpej 
     94   1.2   thorpej 	cond->ptc_magic = _PT_COND_MAGIC;
     95   1.2   thorpej 	pthread_lockinit(&cond->ptc_lock);
     96   1.2   thorpej 	PTQ_INIT(&cond->ptc_waiters);
     97   1.2   thorpej 	cond->ptc_mutex = NULL;
     98  1.58  christos 	if (attr && attr->ptca_private) {
     99  1.58  christos 		cond->ptc_private = malloc(sizeof(clockid_t));
    100  1.58  christos 		if (cond->ptc_private == NULL)
    101  1.58  christos 			return errno;
    102  1.58  christos 		*(clockid_t *)cond->ptc_private =
    103  1.58  christos 		    *(clockid_t *)attr->ptca_private;
    104  1.58  christos 	} else
    105  1.58  christos 		cond->ptc_private = NULL;
    106   1.2   thorpej 
    107   1.2   thorpej 	return 0;
    108   1.2   thorpej }
    109   1.2   thorpej 
    110   1.2   thorpej 
    111   1.2   thorpej int
    112   1.2   thorpej pthread_cond_destroy(pthread_cond_t *cond)
    113   1.2   thorpej {
    114  1.59  christos 	if (__predict_false(__uselibcstub))
    115  1.59  christos 		return __libc_cond_destroy_stub(cond);
    116   1.2   thorpej 
    117  1.11   nathanw 	pthread__error(EINVAL, "Invalid condition variable",
    118  1.11   nathanw 	    cond->ptc_magic == _PT_COND_MAGIC);
    119  1.11   nathanw 	pthread__error(EBUSY, "Destroying condition variable in use",
    120  1.11   nathanw 	    cond->ptc_mutex == NULL);
    121   1.2   thorpej 
    122   1.2   thorpej 	cond->ptc_magic = _PT_COND_DEAD;
    123  1.58  christos 	free(cond->ptc_private);
    124   1.2   thorpej 
    125   1.2   thorpej 	return 0;
    126   1.2   thorpej }
    127   1.2   thorpej 
    128  1.57     joerg int
    129  1.43        ad pthread_cond_timedwait(pthread_cond_t *cond, pthread_mutex_t *mutex,
    130  1.43        ad 		       const struct timespec *abstime)
    131   1.2   thorpej {
    132   1.2   thorpej 	pthread_t self;
    133  1.43        ad 	int retval;
    134  1.61  christos 	struct timespec mono;
    135  1.10   nathanw 
    136  1.59  christos 	if (__predict_false(__uselibcstub))
    137  1.59  christos 		return __libc_cond_timedwait_stub(cond, mutex, abstime);
    138  1.59  christos 
    139  1.11   nathanw 	pthread__error(EINVAL, "Invalid condition variable",
    140  1.11   nathanw 	    cond->ptc_magic == _PT_COND_MAGIC);
    141  1.11   nathanw 	pthread__error(EINVAL, "Invalid mutex",
    142  1.11   nathanw 	    mutex->ptm_magic == _PT_MUTEX_MAGIC);
    143  1.11   nathanw 	pthread__error(EPERM, "Mutex not locked in condition wait",
    144  1.35        ad 	    mutex->ptm_owner != NULL);
    145  1.43        ad 	if (abstime != NULL) {
    146  1.60  christos 		/*
    147  1.60  christos 		 * XXX: This should be done in the kernel to avoid
    148  1.60  christos 		 * extra system calls!
    149  1.60  christos 		 */
    150  1.60  christos 		if (pthread_cond_getclock(cond) == CLOCK_MONOTONIC) {
    151  1.61  christos 			struct timespec real;
    152  1.60  christos 			if (clock_gettime(CLOCK_REALTIME, &real) == -1 ||
    153  1.60  christos 			    clock_gettime(CLOCK_MONOTONIC, &mono) == -1)
    154  1.60  christos 				return errno;
    155  1.60  christos 			timespecsub(abstime, &mono, &mono);
    156  1.60  christos 			timespecadd(&mono, &real, &mono);
    157  1.60  christos 			abstime = &mono;
    158  1.60  christos 		}
    159  1.43        ad 		pthread__error(EINVAL, "Invalid wait time",
    160  1.43        ad 		    (abstime->tv_sec >= 0) &&
    161  1.43        ad 		    (abstime->tv_nsec >= 0) &&
    162  1.43        ad 		    (abstime->tv_nsec < 1000000000));
    163  1.43        ad 	}
    164  1.10   nathanw 
    165   1.2   thorpej 	self = pthread__self();
    166   1.6   nathanw 
    167   1.6   nathanw 	/* Just hang out for a while if threads aren't running yet. */
    168  1.43        ad 	if (__predict_false(pthread__started == 0)) {
    169  1.58  christos 		return pthread_cond_wait_nothread(self, mutex, cond, abstime);
    170  1.43        ad 	}
    171  1.43        ad 	if (__predict_false(self->pt_cancel)) {
    172  1.40        ad 		pthread__cancelled();
    173  1.43        ad 	}
    174  1.32        ad 
    175  1.43        ad 	/* Note this thread as waiting on the CV. */
    176  1.38        ad 	pthread__spinlock(self, &cond->ptc_lock);
    177  1.36        ad 	cond->ptc_mutex = mutex;
    178  1.23        ad 	PTQ_INSERT_HEAD(&cond->ptc_waiters, self, pt_sleep);
    179  1.43        ad 	self->pt_sleepobj = cond;
    180  1.38        ad 	pthread__spinunlock(self, &cond->ptc_lock);
    181  1.32        ad 
    182  1.43        ad 	do {
    183  1.44        ad 		self->pt_willpark = 1;
    184  1.43        ad 		pthread_mutex_unlock(mutex);
    185  1.44        ad 		self->pt_willpark = 0;
    186  1.43        ad 		self->pt_blocking++;
    187  1.43        ad 		retval = _lwp_park(abstime, self->pt_unpark,
    188  1.43        ad 		    __UNVOLATILE(&mutex->ptm_waiters),
    189  1.43        ad 		    __UNVOLATILE(&mutex->ptm_waiters));
    190  1.43        ad 		self->pt_unpark = 0;
    191  1.43        ad 		self->pt_blocking--;
    192  1.43        ad 		membar_sync();
    193  1.43        ad 		pthread_mutex_lock(mutex);
    194  1.32        ad 
    195  1.43        ad 		/*
    196  1.43        ad 		 * If we have cancelled then exit.  POSIX dictates that
    197  1.43        ad 		 * the mutex must be held when we action the cancellation.
    198  1.43        ad 		 *
    199  1.43        ad 		 * If we absorbed a pthread_cond_signal() and cannot take
    200  1.43        ad 		 * the wakeup, we must ensure that another thread does.
    201  1.43        ad 		 *
    202  1.46        ad 		 * If awoke early, we may still be on the sleep queue and
    203  1.46        ad 		 * must remove ourself.
    204  1.43        ad 		 */
    205  1.46        ad 		if (__predict_false(retval != 0)) {
    206  1.46        ad 			switch (errno) {
    207  1.46        ad 			case EINTR:
    208  1.46        ad 			case EALREADY:
    209  1.46        ad 				retval = 0;
    210  1.46        ad 				break;
    211  1.46        ad 			default:
    212  1.46        ad 				retval = errno;
    213  1.46        ad 				break;
    214  1.46        ad 			}
    215  1.45        ad 		}
    216  1.45        ad 		if (__predict_false(self->pt_cancel | retval)) {
    217  1.48        ad 			pthread_cond_signal(cond);
    218  1.43        ad 			if (self->pt_cancel) {
    219  1.43        ad 				pthread__cancelled();
    220  1.43        ad 			}
    221  1.43        ad 			break;
    222  1.43        ad 		}
    223  1.43        ad 	} while (self->pt_sleepobj != NULL);
    224  1.32        ad 
    225  1.43        ad 	return retval;
    226   1.2   thorpej }
    227   1.2   thorpej 
    228   1.2   thorpej int
    229  1.43        ad pthread_cond_wait(pthread_cond_t *cond, pthread_mutex_t *mutex)
    230   1.2   thorpej {
    231  1.59  christos 	if (__predict_false(__uselibcstub))
    232  1.59  christos 		return __libc_cond_wait_stub(cond, mutex);
    233  1.32        ad 
    234  1.43        ad 	return pthread_cond_timedwait(cond, mutex, NULL);
    235   1.2   thorpej }
    236   1.2   thorpej 
    237  1.49        ad static int __noinline
    238  1.49        ad pthread__cond_wake_one(pthread_cond_t *cond)
    239   1.2   thorpej {
    240   1.2   thorpej 	pthread_t self, signaled;
    241  1.28        ad 	pthread_mutex_t *mutex;
    242  1.43        ad 	lwpid_t lid;
    243  1.43        ad 
    244  1.11   nathanw 	pthread__error(EINVAL, "Invalid condition variable",
    245  1.11   nathanw 	    cond->ptc_magic == _PT_COND_MAGIC);
    246   1.2   thorpej 
    247  1.48        ad 	/*
    248  1.48        ad 	 * Pull the first thread off the queue.  If the current thread
    249  1.48        ad 	 * is associated with the condition variable, remove it without
    250  1.48        ad 	 * awakening (error case in pthread_cond_timedwait()).
    251  1.48        ad 	 */
    252  1.27        ad 	self = pthread__self();
    253  1.38        ad 	pthread__spinlock(self, &cond->ptc_lock);
    254  1.48        ad 	if (self->pt_sleepobj == cond) {
    255  1.48        ad 		PTQ_REMOVE(&cond->ptc_waiters, self, pt_sleep);
    256  1.48        ad 		self->pt_sleepobj = NULL;
    257  1.48        ad 	}
    258  1.43        ad 	signaled = PTQ_FIRST(&cond->ptc_waiters);
    259  1.28        ad 	if (__predict_false(signaled == NULL)) {
    260  1.50        ad 		cond->ptc_mutex = NULL;
    261  1.46        ad 		pthread__spinunlock(self, &cond->ptc_lock);
    262  1.28        ad 		return 0;
    263  1.28        ad 	}
    264  1.28        ad 	mutex = cond->ptc_mutex;
    265  1.43        ad 	if (PTQ_NEXT(signaled, pt_sleep) == NULL) {
    266  1.28        ad 		cond->ptc_mutex = NULL;
    267  1.43        ad 		PTQ_INIT(&cond->ptc_waiters);
    268  1.43        ad 	} else {
    269  1.43        ad 		PTQ_REMOVE(&cond->ptc_waiters, signaled, pt_sleep);
    270  1.43        ad 	}
    271  1.43        ad 	signaled->pt_sleepobj = NULL;
    272  1.43        ad 	lid = signaled->pt_lid;
    273  1.43        ad 	pthread__spinunlock(self, &cond->ptc_lock);
    274  1.28        ad 
    275  1.28        ad 	/*
    276  1.28        ad 	 * For all valid uses of pthread_cond_signal(), the caller will
    277  1.28        ad 	 * hold the mutex that the target is using to synchronize with.
    278  1.56     skrll 	 * To avoid the target awakening and immediately blocking on the
    279  1.35        ad 	 * mutex, transfer the thread to be awoken to the current thread's
    280  1.35        ad 	 * deferred wakeup list.  The waiter will be set running when the
    281  1.35        ad 	 * caller (this thread) releases the mutex.
    282  1.28        ad 	 */
    283  1.54     lukem 	if (__predict_false(self->pt_nwaiters == (size_t)pthread__unpark_max)) {
    284  1.43        ad 		(void)_lwp_unpark_all(self->pt_waiters, self->pt_nwaiters,
    285  1.43        ad 		    __UNVOLATILE(&mutex->ptm_waiters));
    286  1.43        ad 		self->pt_nwaiters = 0;
    287   1.4   nathanw 	}
    288  1.43        ad 	self->pt_waiters[self->pt_nwaiters++] = lid;
    289  1.43        ad 	pthread__mutex_deferwake(self, mutex);
    290   1.2   thorpej 	return 0;
    291   1.2   thorpej }
    292   1.2   thorpej 
    293   1.2   thorpej int
    294  1.49        ad pthread_cond_signal(pthread_cond_t *cond)
    295  1.49        ad {
    296  1.49        ad 
    297  1.59  christos 	if (__predict_false(__uselibcstub))
    298  1.59  christos 		return __libc_cond_signal_stub(cond);
    299  1.59  christos 
    300  1.49        ad 	if (__predict_true(PTQ_EMPTY(&cond->ptc_waiters)))
    301  1.49        ad 		return 0;
    302  1.49        ad 	return pthread__cond_wake_one(cond);
    303  1.49        ad }
    304  1.49        ad 
    305  1.49        ad static int __noinline
    306  1.49        ad pthread__cond_wake_all(pthread_cond_t *cond)
    307   1.2   thorpej {
    308  1.43        ad 	pthread_t self, signaled;
    309  1.28        ad 	pthread_mutex_t *mutex;
    310  1.52      matt 	u_int max;
    311  1.52      matt 	size_t nwaiters;
    312   1.2   thorpej 
    313  1.43        ad 	pthread__error(EINVAL, "Invalid condition variable",
    314  1.43        ad 	    cond->ptc_magic == _PT_COND_MAGIC);
    315  1.28        ad 
    316  1.28        ad 	/*
    317  1.35        ad 	 * Try to defer waking threads (see pthread_cond_signal()).
    318  1.35        ad 	 * Only transfer waiters for which there is no pending wakeup.
    319  1.28        ad 	 */
    320  1.43        ad 	self = pthread__self();
    321  1.43        ad 	pthread__spinlock(self, &cond->ptc_lock);
    322  1.43        ad 	max = pthread__unpark_max;
    323  1.43        ad 	mutex = cond->ptc_mutex;
    324  1.43        ad 	nwaiters = self->pt_nwaiters;
    325  1.43        ad 	PTQ_FOREACH(signaled, &cond->ptc_waiters, pt_sleep) {
    326  1.48        ad 		if (__predict_false(nwaiters == max)) {
    327  1.43        ad 			/* Overflow. */
    328  1.43        ad 			(void)_lwp_unpark_all(self->pt_waiters,
    329  1.43        ad 			    nwaiters, __UNVOLATILE(&mutex->ptm_waiters));
    330  1.43        ad 			nwaiters = 0;
    331  1.28        ad 		}
    332  1.43        ad 		signaled->pt_sleepobj = NULL;
    333  1.43        ad 		self->pt_waiters[nwaiters++] = signaled->pt_lid;
    334  1.35        ad 	}
    335  1.43        ad 	PTQ_INIT(&cond->ptc_waiters);
    336  1.43        ad 	self->pt_nwaiters = nwaiters;
    337  1.43        ad 	cond->ptc_mutex = NULL;
    338  1.43        ad 	pthread__spinunlock(self, &cond->ptc_lock);
    339  1.43        ad 	pthread__mutex_deferwake(self, mutex);
    340  1.43        ad 
    341   1.2   thorpej 	return 0;
    342   1.2   thorpej }
    343   1.2   thorpej 
    344  1.49        ad int
    345  1.49        ad pthread_cond_broadcast(pthread_cond_t *cond)
    346  1.49        ad {
    347  1.59  christos 	if (__predict_false(__uselibcstub))
    348  1.59  christos 		return __libc_cond_broadcast_stub(cond);
    349  1.49        ad 
    350  1.49        ad 	if (__predict_true(PTQ_EMPTY(&cond->ptc_waiters)))
    351  1.49        ad 		return 0;
    352  1.49        ad 	return pthread__cond_wake_all(cond);
    353  1.49        ad }
    354   1.2   thorpej 
    355   1.2   thorpej int
    356  1.51     pooka _pthread_cond_has_waiters_np(pthread_cond_t *cond)
    357  1.51     pooka {
    358  1.51     pooka 
    359  1.51     pooka 	return !PTQ_EMPTY(&cond->ptc_waiters);
    360  1.51     pooka }
    361  1.51     pooka 
    362  1.51     pooka int
    363   1.2   thorpej pthread_condattr_init(pthread_condattr_t *attr)
    364   1.2   thorpej {
    365   1.2   thorpej 
    366   1.2   thorpej 	attr->ptca_magic = _PT_CONDATTR_MAGIC;
    367  1.58  christos 	attr->ptca_private = NULL;
    368   1.2   thorpej 
    369   1.2   thorpej 	return 0;
    370   1.2   thorpej }
    371   1.2   thorpej 
    372   1.2   thorpej int
    373  1.58  christos pthread_condattr_setclock(pthread_condattr_t *attr, clockid_t clck)
    374  1.58  christos {
    375  1.58  christos 	switch (clck) {
    376  1.58  christos 	case CLOCK_MONOTONIC:
    377  1.58  christos 	case CLOCK_REALTIME:
    378  1.58  christos 		if (attr->ptca_private == NULL)
    379  1.58  christos 			attr->ptca_private = malloc(sizeof(clockid_t));
    380  1.58  christos 		if (attr->ptca_private == NULL)
    381  1.58  christos 			return errno;
    382  1.58  christos 		*(clockid_t *)attr->ptca_private = clck;
    383  1.58  christos 		return 0;
    384  1.58  christos 	default:
    385  1.58  christos 		return EINVAL;
    386  1.58  christos 	}
    387  1.58  christos }
    388  1.58  christos 
    389  1.58  christos int
    390   1.2   thorpej pthread_condattr_destroy(pthread_condattr_t *attr)
    391   1.2   thorpej {
    392   1.2   thorpej 
    393  1.11   nathanw 	pthread__error(EINVAL, "Invalid condition variable attribute",
    394  1.11   nathanw 	    attr->ptca_magic == _PT_CONDATTR_MAGIC);
    395   1.2   thorpej 
    396   1.2   thorpej 	attr->ptca_magic = _PT_CONDATTR_DEAD;
    397  1.58  christos 	free(attr->ptca_private);
    398   1.2   thorpej 
    399   1.2   thorpej 	return 0;
    400   1.6   nathanw }
    401   1.6   nathanw 
    402   1.6   nathanw /* Utility routine to hang out for a while if threads haven't started yet. */
    403   1.6   nathanw static int
    404   1.6   nathanw pthread_cond_wait_nothread(pthread_t self, pthread_mutex_t *mutex,
    405  1.58  christos     pthread_cond_t *cond, const struct timespec *abstime)
    406   1.6   nathanw {
    407  1.18   mycroft 	struct timespec now, diff;
    408   1.6   nathanw 	int retval;
    409   1.6   nathanw 
    410  1.18   mycroft 	if (abstime == NULL) {
    411  1.18   mycroft 		diff.tv_sec = 99999999;
    412  1.18   mycroft 		diff.tv_nsec = 0;
    413  1.18   mycroft 	} else {
    414  1.60  christos 		clockid_t clck = pthread_cond_getclock(cond);
    415  1.58  christos 		clock_gettime(clck, &now);
    416  1.18   mycroft 		if  (timespeccmp(abstime, &now, <))
    417  1.18   mycroft 			timespecclear(&diff);
    418  1.17   nathanw 		else
    419  1.18   mycroft 			timespecsub(abstime, &now, &diff);
    420   1.6   nathanw 	}
    421   1.6   nathanw 
    422  1.18   mycroft 	do {
    423  1.18   mycroft 		pthread__testcancel(self);
    424  1.18   mycroft 		pthread_mutex_unlock(mutex);
    425  1.55  drochner 		retval = _sys___nanosleep50(&diff, NULL);
    426  1.18   mycroft 		pthread_mutex_lock(mutex);
    427  1.18   mycroft 	} while (abstime == NULL && retval == 0);
    428   1.6   nathanw 	pthread__testcancel(self);
    429   1.6   nathanw 
    430   1.6   nathanw 	if (retval == 0)
    431   1.6   nathanw 		return ETIMEDOUT;
    432   1.6   nathanw 	else
    433  1.15    kleink 		/* spurious wakeup */
    434  1.15    kleink 		return 0;
    435   1.2   thorpej }
    436