Home | History | Annotate | Line # | Download | only in libpthread
pthread_cond.c revision 1.20
      1  1.20       ad /*	$NetBSD: pthread_cond.c,v 1.20 2006/12/24 18:39: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.20       ad __RCSID("$NetBSD: pthread_cond.c,v 1.20 2006/12/24 18:39: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.20       ad 	    &cond->ptc_waiters, NULL, 0, 1);
    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.20       ad 	pthread_mutex_unlock(mutex);
    250  1.19       ad 	pthread_spinunlock(self, &self->pt_statelock);
    251  1.19       ad 	retval = pthread__park(self, &cond->ptc_lock, cond,
    252  1.20       ad 	    &cond->ptc_waiters, abstime, 0, 1);
    253  1.19       ad 	pthread_spinunlock(self, &cond->ptc_lock);
    254  1.19       ad #endif	/* PTHREAD_SA */
    255  1.19       ad 
    256  1.19       ad 	SDPRINTF(("(cond timed wait %p) Woke up on %p, mutex %p\n",
    257  1.19       ad 	    self, cond));
    258   1.2  thorpej 	SDPRINTF(("(cond timed wait %p) %s\n",
    259   1.2  thorpej 	    self, (retval == ETIMEDOUT) ? "(timed out)" : ""));
    260   1.2  thorpej 	pthread_mutex_lock(mutex);
    261  1.12  nathanw #ifdef ERRORCHECK
    262  1.16  nathanw 	pthread_spinlock(self, &cond->ptc_lock);
    263  1.16  nathanw 	if (PTQ_EMPTY(&cond->ptc_waiters))
    264  1.16  nathanw 		cond->ptc_mutex = NULL;
    265  1.16  nathanw 	pthread_spinunlock(self, &cond->ptc_lock);
    266  1.12  nathanw #endif
    267  1.16  nathanw 	if (__predict_false(self->pt_cancel))
    268  1.12  nathanw 		pthread_exit(PTHREAD_CANCELED);
    269   1.2  thorpej 
    270   1.2  thorpej 	return retval;
    271   1.2  thorpej }
    272   1.2  thorpej 
    273  1.19       ad #ifdef PTHREAD_SA
    274   1.2  thorpej static void
    275   1.2  thorpej pthread_cond_wait__callback(void *arg)
    276   1.2  thorpej {
    277   1.2  thorpej 	struct pthread_cond__waitarg *a;
    278   1.2  thorpej 	pthread_t self;
    279   1.2  thorpej 
    280   1.2  thorpej 	a = arg;
    281   1.2  thorpej 	self = pthread__self();
    282   1.2  thorpej 
    283   1.2  thorpej 	/*
    284   1.2  thorpej 	 * Don't dequeue and schedule the thread if it's already been
    285   1.2  thorpej 	 * queued up by a signal or broadcast (but hasn't yet run as far
    286   1.2  thorpej 	 * as pthread__alarm_del(), or we wouldn't be here, and hence can't
    287   1.2  thorpej 	 * have become blocked on some *other* queue).
    288   1.2  thorpej 	 */
    289   1.2  thorpej 	pthread_spinlock(self, &a->ptw_cond->ptc_lock);
    290   1.2  thorpej 	if (a->ptw_thread->pt_state == PT_STATE_BLOCKED_QUEUE) {
    291   1.2  thorpej 		PTQ_REMOVE(&a->ptw_cond->ptc_waiters, a->ptw_thread, pt_sleep);
    292   1.2  thorpej #ifdef ERRORCHECK
    293   1.2  thorpej 		if (PTQ_EMPTY(&a->ptw_cond->ptc_waiters))
    294   1.2  thorpej 			a->ptw_cond->ptc_mutex = NULL;
    295   1.2  thorpej #endif
    296   1.2  thorpej 		pthread__sched(self, a->ptw_thread);
    297   1.2  thorpej 	}
    298   1.2  thorpej 	pthread_spinunlock(self, &a->ptw_cond->ptc_lock);
    299   1.2  thorpej }
    300  1.19       ad #endif	/* PTHREAD_SA */
    301   1.2  thorpej 
    302   1.2  thorpej int
    303   1.2  thorpej pthread_cond_signal(pthread_cond_t *cond)
    304   1.2  thorpej {
    305   1.2  thorpej 	pthread_t self, signaled;
    306  1.10  nathanw 
    307  1.11  nathanw 	pthread__error(EINVAL, "Invalid condition variable",
    308  1.11  nathanw 	    cond->ptc_magic == _PT_COND_MAGIC);
    309   1.3  nathanw 	PTHREADD_ADD(PTHREADD_COND_SIGNAL);
    310   1.2  thorpej 
    311   1.2  thorpej 	SDPRINTF(("(cond signal %p) Signaling %p\n",
    312   1.4  nathanw 	    pthread__self(), cond));
    313   1.2  thorpej 
    314  1.20       ad #ifdef PTHREAD_SA
    315   1.4  nathanw 	if (!PTQ_EMPTY(&cond->ptc_waiters)) {
    316   1.4  nathanw 		self = pthread__self();
    317   1.4  nathanw 		pthread_spinlock(self, &cond->ptc_lock);
    318   1.4  nathanw 		signaled = PTQ_FIRST(&cond->ptc_waiters);
    319  1.14       cl 		if (signaled != NULL) {
    320   1.4  nathanw 			PTQ_REMOVE(&cond->ptc_waiters, signaled, pt_sleep);
    321  1.14       cl 			pthread__sched(self, signaled);
    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.13  nathanw 		pthread_spinunlock(self, &cond->ptc_lock);
    329  1.20       ad 	}
    330  1.19       ad #else	/* PTHREAD_SA */
    331  1.20       ad 	self = pthread__self();
    332  1.20       ad 	pthread_spinlock(self, &cond->ptc_lock);
    333  1.20       ad 	if ((signaled = PTQ_FIRST(&cond->ptc_waiters)) != NULL) {
    334  1.20       ad 		PTQ_REMOVE(&cond->ptc_waiters, signaled, pt_sleep);
    335  1.20       ad 		PTHREADD_ADD(PTHREADD_COND_WOKEUP);
    336  1.20       ad #ifdef ERRORCHECK
    337  1.20       ad 		if (PTQ_EMPTY(&cond->ptc_waiters))
    338  1.20       ad 			cond->ptc_mutex = NULL;
    339  1.20       ad #endif
    340   1.4  nathanw 	}
    341  1.20       ad 	pthread__unpark(self, &cond->ptc_lock, cond, signaled);
    342  1.20       ad #endif	/* PTHREAD_SA */
    343   1.2  thorpej 	return 0;
    344   1.2  thorpej }
    345   1.2  thorpej 
    346   1.2  thorpej 
    347   1.2  thorpej int
    348   1.2  thorpej pthread_cond_broadcast(pthread_cond_t *cond)
    349   1.2  thorpej {
    350   1.5  nathanw 	pthread_t self;
    351  1.20       ad #ifdef PTHREAD_SA
    352   1.2  thorpej 	struct pthread_queue_t blockedq;
    353  1.20       ad #endif
    354  1.10  nathanw 
    355  1.11  nathanw 	pthread__error(EINVAL, "Invalid condition variable", cond->ptc_magic == _PT_COND_MAGIC);
    356   1.2  thorpej 
    357   1.3  nathanw 	PTHREADD_ADD(PTHREADD_COND_BROADCAST);
    358   1.2  thorpej 	SDPRINTF(("(cond signal %p) Broadcasting %p\n",
    359   1.4  nathanw 	    pthread__self(), cond));
    360   1.2  thorpej 
    361  1.20       ad #ifdef PTHREAD_SA
    362   1.4  nathanw 	if (!PTQ_EMPTY(&cond->ptc_waiters)) {
    363   1.4  nathanw 		self = pthread__self();
    364   1.4  nathanw 		pthread_spinlock(self, &cond->ptc_lock);
    365   1.4  nathanw 		blockedq = cond->ptc_waiters;
    366   1.4  nathanw 		PTQ_INIT(&cond->ptc_waiters);
    367   1.2  thorpej #ifdef ERRORCHECK
    368   1.4  nathanw 		cond->ptc_mutex = NULL;
    369   1.2  thorpej #endif
    370   1.5  nathanw 		pthread__sched_sleepers(self, &blockedq);
    371   1.7  nathanw 		PTHREADD_ADD(PTHREADD_COND_WOKEUP);
    372  1.13  nathanw 		pthread_spinunlock(self, &cond->ptc_lock);
    373  1.20       ad 	}
    374  1.19       ad #else	/* PTHREAD_SA */
    375  1.20       ad 	self = pthread__self();
    376  1.20       ad 	pthread_spinlock(self, &cond->ptc_lock);
    377  1.20       ad #ifdef ERRORCHECK
    378  1.20       ad 	if (!PTQ_EMPTY(&cond->ptc_waiters))
    379  1.20       ad 		cond->ptc_mutex = NULL;
    380  1.20       ad #endif
    381  1.20       ad 	pthread__unpark_all(self, &cond->ptc_lock, cond, &cond->ptc_waiters);
    382  1.20       ad 	PTHREADD_ADD(PTHREADD_COND_WOKEUP);
    383  1.19       ad #endif	/* PTHREAD_SA */
    384   1.2  thorpej 
    385   1.2  thorpej 	return 0;
    386   1.2  thorpej 
    387   1.2  thorpej }
    388   1.2  thorpej 
    389   1.2  thorpej 
    390   1.2  thorpej int
    391   1.2  thorpej pthread_condattr_init(pthread_condattr_t *attr)
    392   1.2  thorpej {
    393   1.2  thorpej 
    394   1.2  thorpej 	attr->ptca_magic = _PT_CONDATTR_MAGIC;
    395   1.2  thorpej 
    396   1.2  thorpej 	return 0;
    397   1.2  thorpej }
    398   1.2  thorpej 
    399   1.2  thorpej 
    400   1.2  thorpej int
    401   1.2  thorpej pthread_condattr_destroy(pthread_condattr_t *attr)
    402   1.2  thorpej {
    403   1.2  thorpej 
    404  1.11  nathanw 	pthread__error(EINVAL, "Invalid condition variable attribute",
    405  1.11  nathanw 	    attr->ptca_magic == _PT_CONDATTR_MAGIC);
    406   1.2  thorpej 
    407   1.2  thorpej 	attr->ptca_magic = _PT_CONDATTR_DEAD;
    408   1.2  thorpej 
    409   1.2  thorpej 	return 0;
    410   1.6  nathanw }
    411   1.6  nathanw 
    412   1.6  nathanw /* Utility routine to hang out for a while if threads haven't started yet. */
    413   1.6  nathanw static int
    414   1.6  nathanw pthread_cond_wait_nothread(pthread_t self, pthread_mutex_t *mutex,
    415   1.6  nathanw     const struct timespec *abstime)
    416   1.6  nathanw {
    417  1.18  mycroft 	struct timespec now, diff;
    418   1.6  nathanw 	int retval;
    419   1.6  nathanw 
    420  1.18  mycroft 	if (abstime == NULL) {
    421  1.18  mycroft 		diff.tv_sec = 99999999;
    422  1.18  mycroft 		diff.tv_nsec = 0;
    423  1.18  mycroft 	} else {
    424  1.18  mycroft 		clock_gettime(CLOCK_REALTIME, &now);
    425  1.18  mycroft 		if  (timespeccmp(abstime, &now, <))
    426  1.18  mycroft 			timespecclear(&diff);
    427  1.17  nathanw 		else
    428  1.18  mycroft 			timespecsub(abstime, &now, &diff);
    429   1.6  nathanw 	}
    430   1.6  nathanw 
    431  1.18  mycroft 	do {
    432  1.18  mycroft 		pthread__testcancel(self);
    433  1.18  mycroft 		pthread_mutex_unlock(mutex);
    434  1.18  mycroft 		retval = _sys_nanosleep(&diff, NULL);
    435  1.18  mycroft 		pthread_mutex_lock(mutex);
    436  1.18  mycroft 	} while (abstime == NULL && retval == 0);
    437   1.6  nathanw 	pthread__testcancel(self);
    438   1.6  nathanw 
    439   1.6  nathanw 	if (retval == 0)
    440   1.6  nathanw 		return ETIMEDOUT;
    441   1.6  nathanw 	else
    442  1.15   kleink 		/* spurious wakeup */
    443  1.15   kleink 		return 0;
    444   1.2  thorpej }
    445