Home | History | Annotate | Line # | Download | only in libpthread
pthread_cond.c revision 1.67
      1  1.67     kamil /*	$NetBSD: pthread_cond.c,v 1.67 2020/01/29 15:07:46 kamil 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.67     kamil __RCSID("$NetBSD: pthread_cond.c,v 1.67 2020/01/29 15:07:46 kamil 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.67     kamil 
     82  1.67     kamil 	pthread__error(EINVAL, "Invalid condition variable",
     83  1.67     kamil 	    cond->ptc_magic == _PT_COND_MAGIC);
     84  1.67     kamil 
     85  1.60  christos 	return cond->ptc_private ?
     86  1.60  christos 	    *(clockid_t *)cond->ptc_private : CLOCK_REALTIME;
     87  1.60  christos }
     88  1.60  christos 
     89   1.2   thorpej int
     90   1.2   thorpej pthread_cond_init(pthread_cond_t *cond, const pthread_condattr_t *attr)
     91   1.2   thorpej {
     92  1.59  christos 	if (__predict_false(__uselibcstub))
     93  1.59  christos 		return __libc_cond_init_stub(cond, attr);
     94   1.2   thorpej 
     95  1.11   nathanw 	pthread__error(EINVAL, "Invalid condition variable attribute",
     96  1.11   nathanw 	    (attr == NULL) || (attr->ptca_magic == _PT_CONDATTR_MAGIC));
     97   1.2   thorpej 
     98   1.2   thorpej 	cond->ptc_magic = _PT_COND_MAGIC;
     99   1.2   thorpej 	pthread_lockinit(&cond->ptc_lock);
    100   1.2   thorpej 	PTQ_INIT(&cond->ptc_waiters);
    101   1.2   thorpej 	cond->ptc_mutex = NULL;
    102  1.58  christos 	if (attr && attr->ptca_private) {
    103  1.58  christos 		cond->ptc_private = malloc(sizeof(clockid_t));
    104  1.58  christos 		if (cond->ptc_private == NULL)
    105  1.58  christos 			return errno;
    106  1.58  christos 		*(clockid_t *)cond->ptc_private =
    107  1.58  christos 		    *(clockid_t *)attr->ptca_private;
    108  1.58  christos 	} else
    109  1.58  christos 		cond->ptc_private = NULL;
    110   1.2   thorpej 
    111   1.2   thorpej 	return 0;
    112   1.2   thorpej }
    113   1.2   thorpej 
    114   1.2   thorpej 
    115   1.2   thorpej int
    116   1.2   thorpej pthread_cond_destroy(pthread_cond_t *cond)
    117   1.2   thorpej {
    118  1.59  christos 	if (__predict_false(__uselibcstub))
    119  1.59  christos 		return __libc_cond_destroy_stub(cond);
    120   1.2   thorpej 
    121  1.11   nathanw 	pthread__error(EINVAL, "Invalid condition variable",
    122  1.11   nathanw 	    cond->ptc_magic == _PT_COND_MAGIC);
    123  1.11   nathanw 	pthread__error(EBUSY, "Destroying condition variable in use",
    124  1.11   nathanw 	    cond->ptc_mutex == NULL);
    125   1.2   thorpej 
    126   1.2   thorpej 	cond->ptc_magic = _PT_COND_DEAD;
    127  1.58  christos 	free(cond->ptc_private);
    128   1.2   thorpej 
    129   1.2   thorpej 	return 0;
    130   1.2   thorpej }
    131   1.2   thorpej 
    132  1.57     joerg int
    133  1.43        ad pthread_cond_timedwait(pthread_cond_t *cond, pthread_mutex_t *mutex,
    134  1.43        ad 		       const struct timespec *abstime)
    135   1.2   thorpej {
    136   1.2   thorpej 	pthread_t self;
    137  1.43        ad 	int retval;
    138  1.63  christos 	clockid_t clkid = pthread_cond_getclock(cond);
    139  1.10   nathanw 
    140  1.59  christos 	if (__predict_false(__uselibcstub))
    141  1.59  christos 		return __libc_cond_timedwait_stub(cond, mutex, abstime);
    142  1.59  christos 
    143  1.11   nathanw 	pthread__error(EINVAL, "Invalid condition variable",
    144  1.11   nathanw 	    cond->ptc_magic == _PT_COND_MAGIC);
    145  1.11   nathanw 	pthread__error(EINVAL, "Invalid mutex",
    146  1.11   nathanw 	    mutex->ptm_magic == _PT_MUTEX_MAGIC);
    147  1.11   nathanw 	pthread__error(EPERM, "Mutex not locked in condition wait",
    148  1.35        ad 	    mutex->ptm_owner != NULL);
    149  1.10   nathanw 
    150   1.2   thorpej 	self = pthread__self();
    151   1.6   nathanw 
    152   1.6   nathanw 	/* Just hang out for a while if threads aren't running yet. */
    153  1.43        ad 	if (__predict_false(pthread__started == 0)) {
    154  1.58  christos 		return pthread_cond_wait_nothread(self, mutex, cond, abstime);
    155  1.43        ad 	}
    156  1.43        ad 	if (__predict_false(self->pt_cancel)) {
    157  1.40        ad 		pthread__cancelled();
    158  1.43        ad 	}
    159  1.32        ad 
    160  1.43        ad 	/* Note this thread as waiting on the CV. */
    161  1.38        ad 	pthread__spinlock(self, &cond->ptc_lock);
    162  1.36        ad 	cond->ptc_mutex = mutex;
    163  1.23        ad 	PTQ_INSERT_HEAD(&cond->ptc_waiters, self, pt_sleep);
    164  1.43        ad 	self->pt_sleepobj = cond;
    165  1.38        ad 	pthread__spinunlock(self, &cond->ptc_lock);
    166  1.32        ad 
    167  1.43        ad 	do {
    168  1.44        ad 		self->pt_willpark = 1;
    169  1.43        ad 		pthread_mutex_unlock(mutex);
    170  1.44        ad 		self->pt_willpark = 0;
    171  1.62  christos 		do {
    172  1.65  christos 			retval = _lwp_park(clkid, TIMER_ABSTIME,
    173  1.65  christos 			    __UNCONST(abstime), self->pt_unpark,
    174  1.65  christos 			    __UNVOLATILE(&mutex->ptm_waiters),
    175  1.62  christos 			    __UNVOLATILE(&mutex->ptm_waiters));
    176  1.62  christos 			self->pt_unpark = 0;
    177  1.62  christos 		} while (retval == -1 && errno == ESRCH);
    178  1.43        ad 		pthread_mutex_lock(mutex);
    179  1.32        ad 
    180  1.43        ad 		/*
    181  1.43        ad 		 * If we have cancelled then exit.  POSIX dictates that
    182  1.43        ad 		 * the mutex must be held when we action the cancellation.
    183  1.43        ad 		 *
    184  1.43        ad 		 * If we absorbed a pthread_cond_signal() and cannot take
    185  1.43        ad 		 * the wakeup, we must ensure that another thread does.
    186  1.43        ad 		 *
    187  1.46        ad 		 * If awoke early, we may still be on the sleep queue and
    188  1.46        ad 		 * must remove ourself.
    189  1.43        ad 		 */
    190  1.46        ad 		if (__predict_false(retval != 0)) {
    191  1.46        ad 			switch (errno) {
    192  1.46        ad 			case EINTR:
    193  1.46        ad 			case EALREADY:
    194  1.46        ad 				retval = 0;
    195  1.46        ad 				break;
    196  1.46        ad 			default:
    197  1.46        ad 				retval = errno;
    198  1.46        ad 				break;
    199  1.46        ad 			}
    200  1.45        ad 		}
    201  1.45        ad 		if (__predict_false(self->pt_cancel | retval)) {
    202  1.48        ad 			pthread_cond_signal(cond);
    203  1.43        ad 			if (self->pt_cancel) {
    204  1.43        ad 				pthread__cancelled();
    205  1.43        ad 			}
    206  1.43        ad 			break;
    207  1.43        ad 		}
    208  1.43        ad 	} while (self->pt_sleepobj != NULL);
    209  1.32        ad 
    210  1.43        ad 	return retval;
    211   1.2   thorpej }
    212   1.2   thorpej 
    213   1.2   thorpej int
    214  1.43        ad pthread_cond_wait(pthread_cond_t *cond, pthread_mutex_t *mutex)
    215   1.2   thorpej {
    216  1.59  christos 	if (__predict_false(__uselibcstub))
    217  1.59  christos 		return __libc_cond_wait_stub(cond, mutex);
    218  1.32        ad 
    219  1.43        ad 	return pthread_cond_timedwait(cond, mutex, NULL);
    220   1.2   thorpej }
    221   1.2   thorpej 
    222  1.49        ad static int __noinline
    223  1.49        ad pthread__cond_wake_one(pthread_cond_t *cond)
    224   1.2   thorpej {
    225   1.2   thorpej 	pthread_t self, signaled;
    226  1.28        ad 	pthread_mutex_t *mutex;
    227  1.43        ad 	lwpid_t lid;
    228  1.43        ad 
    229  1.48        ad 	/*
    230  1.48        ad 	 * Pull the first thread off the queue.  If the current thread
    231  1.48        ad 	 * is associated with the condition variable, remove it without
    232  1.48        ad 	 * awakening (error case in pthread_cond_timedwait()).
    233  1.48        ad 	 */
    234  1.27        ad 	self = pthread__self();
    235  1.38        ad 	pthread__spinlock(self, &cond->ptc_lock);
    236  1.48        ad 	if (self->pt_sleepobj == cond) {
    237  1.48        ad 		PTQ_REMOVE(&cond->ptc_waiters, self, pt_sleep);
    238  1.48        ad 		self->pt_sleepobj = NULL;
    239  1.48        ad 	}
    240  1.43        ad 	signaled = PTQ_FIRST(&cond->ptc_waiters);
    241  1.28        ad 	if (__predict_false(signaled == NULL)) {
    242  1.50        ad 		cond->ptc_mutex = NULL;
    243  1.46        ad 		pthread__spinunlock(self, &cond->ptc_lock);
    244  1.28        ad 		return 0;
    245  1.28        ad 	}
    246  1.28        ad 	mutex = cond->ptc_mutex;
    247  1.43        ad 	if (PTQ_NEXT(signaled, pt_sleep) == NULL) {
    248  1.28        ad 		cond->ptc_mutex = NULL;
    249  1.43        ad 		PTQ_INIT(&cond->ptc_waiters);
    250  1.43        ad 	} else {
    251  1.43        ad 		PTQ_REMOVE(&cond->ptc_waiters, signaled, pt_sleep);
    252  1.43        ad 	}
    253  1.43        ad 	signaled->pt_sleepobj = NULL;
    254  1.43        ad 	lid = signaled->pt_lid;
    255  1.43        ad 	pthread__spinunlock(self, &cond->ptc_lock);
    256  1.28        ad 
    257  1.28        ad 	/*
    258  1.28        ad 	 * For all valid uses of pthread_cond_signal(), the caller will
    259  1.28        ad 	 * hold the mutex that the target is using to synchronize with.
    260  1.56     skrll 	 * To avoid the target awakening and immediately blocking on the
    261  1.35        ad 	 * mutex, transfer the thread to be awoken to the current thread's
    262  1.35        ad 	 * deferred wakeup list.  The waiter will be set running when the
    263  1.35        ad 	 * caller (this thread) releases the mutex.
    264  1.28        ad 	 */
    265  1.54     lukem 	if (__predict_false(self->pt_nwaiters == (size_t)pthread__unpark_max)) {
    266  1.43        ad 		(void)_lwp_unpark_all(self->pt_waiters, self->pt_nwaiters,
    267  1.43        ad 		    __UNVOLATILE(&mutex->ptm_waiters));
    268  1.43        ad 		self->pt_nwaiters = 0;
    269   1.4   nathanw 	}
    270  1.43        ad 	self->pt_waiters[self->pt_nwaiters++] = lid;
    271  1.43        ad 	pthread__mutex_deferwake(self, mutex);
    272   1.2   thorpej 	return 0;
    273   1.2   thorpej }
    274   1.2   thorpej 
    275   1.2   thorpej int
    276  1.49        ad pthread_cond_signal(pthread_cond_t *cond)
    277  1.49        ad {
    278  1.49        ad 
    279  1.59  christos 	if (__predict_false(__uselibcstub))
    280  1.59  christos 		return __libc_cond_signal_stub(cond);
    281  1.59  christos 
    282  1.67     kamil 	pthread__error(EINVAL, "Invalid condition variable",
    283  1.67     kamil 	    cond->ptc_magic == _PT_COND_MAGIC);
    284  1.67     kamil 
    285  1.49        ad 	if (__predict_true(PTQ_EMPTY(&cond->ptc_waiters)))
    286  1.49        ad 		return 0;
    287  1.49        ad 	return pthread__cond_wake_one(cond);
    288  1.49        ad }
    289  1.49        ad 
    290  1.49        ad static int __noinline
    291  1.49        ad pthread__cond_wake_all(pthread_cond_t *cond)
    292   1.2   thorpej {
    293  1.43        ad 	pthread_t self, signaled;
    294  1.28        ad 	pthread_mutex_t *mutex;
    295  1.52      matt 	u_int max;
    296  1.52      matt 	size_t nwaiters;
    297   1.2   thorpej 
    298  1.28        ad 	/*
    299  1.35        ad 	 * Try to defer waking threads (see pthread_cond_signal()).
    300  1.35        ad 	 * Only transfer waiters for which there is no pending wakeup.
    301  1.28        ad 	 */
    302  1.43        ad 	self = pthread__self();
    303  1.43        ad 	pthread__spinlock(self, &cond->ptc_lock);
    304  1.43        ad 	max = pthread__unpark_max;
    305  1.43        ad 	mutex = cond->ptc_mutex;
    306  1.43        ad 	nwaiters = self->pt_nwaiters;
    307  1.43        ad 	PTQ_FOREACH(signaled, &cond->ptc_waiters, pt_sleep) {
    308  1.48        ad 		if (__predict_false(nwaiters == max)) {
    309  1.43        ad 			/* Overflow. */
    310  1.43        ad 			(void)_lwp_unpark_all(self->pt_waiters,
    311  1.43        ad 			    nwaiters, __UNVOLATILE(&mutex->ptm_waiters));
    312  1.43        ad 			nwaiters = 0;
    313  1.28        ad 		}
    314  1.43        ad 		signaled->pt_sleepobj = NULL;
    315  1.43        ad 		self->pt_waiters[nwaiters++] = signaled->pt_lid;
    316  1.35        ad 	}
    317  1.43        ad 	PTQ_INIT(&cond->ptc_waiters);
    318  1.43        ad 	self->pt_nwaiters = nwaiters;
    319  1.43        ad 	cond->ptc_mutex = NULL;
    320  1.43        ad 	pthread__spinunlock(self, &cond->ptc_lock);
    321  1.43        ad 	pthread__mutex_deferwake(self, mutex);
    322  1.43        ad 
    323   1.2   thorpej 	return 0;
    324   1.2   thorpej }
    325   1.2   thorpej 
    326  1.49        ad int
    327  1.49        ad pthread_cond_broadcast(pthread_cond_t *cond)
    328  1.49        ad {
    329  1.59  christos 	if (__predict_false(__uselibcstub))
    330  1.59  christos 		return __libc_cond_broadcast_stub(cond);
    331  1.49        ad 
    332  1.67     kamil 	pthread__error(EINVAL, "Invalid condition variable",
    333  1.67     kamil 	    cond->ptc_magic == _PT_COND_MAGIC);
    334  1.67     kamil 
    335  1.49        ad 	if (__predict_true(PTQ_EMPTY(&cond->ptc_waiters)))
    336  1.49        ad 		return 0;
    337  1.49        ad 	return pthread__cond_wake_all(cond);
    338  1.49        ad }
    339   1.2   thorpej 
    340   1.2   thorpej int
    341  1.51     pooka _pthread_cond_has_waiters_np(pthread_cond_t *cond)
    342  1.51     pooka {
    343  1.51     pooka 
    344  1.51     pooka 	return !PTQ_EMPTY(&cond->ptc_waiters);
    345  1.51     pooka }
    346  1.51     pooka 
    347  1.51     pooka int
    348   1.2   thorpej pthread_condattr_init(pthread_condattr_t *attr)
    349   1.2   thorpej {
    350   1.2   thorpej 
    351   1.2   thorpej 	attr->ptca_magic = _PT_CONDATTR_MAGIC;
    352  1.58  christos 	attr->ptca_private = NULL;
    353   1.2   thorpej 
    354   1.2   thorpej 	return 0;
    355   1.2   thorpej }
    356   1.2   thorpej 
    357   1.2   thorpej int
    358  1.58  christos pthread_condattr_setclock(pthread_condattr_t *attr, clockid_t clck)
    359  1.58  christos {
    360  1.67     kamil 
    361  1.67     kamil 	pthread__error(EINVAL, "Invalid condition variable attribute",
    362  1.67     kamil 	    attr->ptca_magic == _PT_CONDATTR_MAGIC);
    363  1.67     kamil 
    364  1.58  christos 	switch (clck) {
    365  1.58  christos 	case CLOCK_MONOTONIC:
    366  1.58  christos 	case CLOCK_REALTIME:
    367  1.58  christos 		if (attr->ptca_private == NULL)
    368  1.58  christos 			attr->ptca_private = malloc(sizeof(clockid_t));
    369  1.58  christos 		if (attr->ptca_private == NULL)
    370  1.58  christos 			return errno;
    371  1.58  christos 		*(clockid_t *)attr->ptca_private = clck;
    372  1.58  christos 		return 0;
    373  1.58  christos 	default:
    374  1.58  christos 		return EINVAL;
    375  1.58  christos 	}
    376  1.58  christos }
    377  1.58  christos 
    378  1.58  christos int
    379  1.64  christos pthread_condattr_getclock(const pthread_condattr_t *__restrict attr,
    380  1.64  christos     clockid_t *__restrict clock_id)
    381  1.64  christos {
    382  1.67     kamil 
    383  1.67     kamil 	pthread__error(EINVAL, "Invalid condition variable attribute",
    384  1.67     kamil 	    attr->ptca_magic == _PT_CONDATTR_MAGIC);
    385  1.67     kamil 
    386  1.64  christos 	if (attr == NULL || attr->ptca_private == NULL)
    387  1.64  christos 		return EINVAL;
    388  1.64  christos 	*clock_id = *(clockid_t *)attr->ptca_private;
    389  1.64  christos 	return 0;
    390  1.64  christos }
    391  1.64  christos 
    392  1.64  christos int
    393   1.2   thorpej pthread_condattr_destroy(pthread_condattr_t *attr)
    394   1.2   thorpej {
    395   1.2   thorpej 
    396  1.11   nathanw 	pthread__error(EINVAL, "Invalid condition variable attribute",
    397  1.11   nathanw 	    attr->ptca_magic == _PT_CONDATTR_MAGIC);
    398   1.2   thorpej 
    399   1.2   thorpej 	attr->ptca_magic = _PT_CONDATTR_DEAD;
    400  1.58  christos 	free(attr->ptca_private);
    401   1.2   thorpej 
    402   1.2   thorpej 	return 0;
    403   1.6   nathanw }
    404   1.6   nathanw 
    405  1.64  christos #ifdef _PTHREAD_PSHARED
    406  1.64  christos int
    407  1.64  christos pthread_condattr_getpshared(const pthread_condattr_t * __restrict attr,
    408  1.64  christos     int * __restrict pshared)
    409  1.64  christos {
    410  1.64  christos 
    411  1.67     kamil 	pthread__error(EINVAL, "Invalid condition variable attribute",
    412  1.67     kamil 	    attr->ptca_magic == _PT_CONDATTR_MAGIC);
    413  1.67     kamil 
    414  1.64  christos 	*pshared = PTHREAD_PROCESS_PRIVATE;
    415  1.64  christos 	return 0;
    416  1.64  christos }
    417  1.64  christos 
    418  1.64  christos int
    419  1.64  christos pthread_condattr_setpshared(pthread_condattr_t *attr, int pshared)
    420  1.64  christos {
    421  1.64  christos 
    422  1.67     kamil 	pthread__error(EINVAL, "Invalid condition variable attribute",
    423  1.67     kamil 	    attr->ptca_magic == _PT_CONDATTR_MAGIC);
    424  1.67     kamil 
    425  1.64  christos 	switch(pshared) {
    426  1.64  christos 	case PTHREAD_PROCESS_PRIVATE:
    427  1.64  christos 		return 0;
    428  1.64  christos 	case PTHREAD_PROCESS_SHARED:
    429  1.64  christos 		return ENOSYS;
    430  1.64  christos 	}
    431  1.64  christos 	return EINVAL;
    432  1.64  christos }
    433  1.64  christos #endif
    434  1.64  christos 
    435   1.6   nathanw /* Utility routine to hang out for a while if threads haven't started yet. */
    436   1.6   nathanw static int
    437   1.6   nathanw pthread_cond_wait_nothread(pthread_t self, pthread_mutex_t *mutex,
    438  1.58  christos     pthread_cond_t *cond, const struct timespec *abstime)
    439   1.6   nathanw {
    440  1.18   mycroft 	struct timespec now, diff;
    441   1.6   nathanw 	int retval;
    442   1.6   nathanw 
    443  1.18   mycroft 	if (abstime == NULL) {
    444  1.18   mycroft 		diff.tv_sec = 99999999;
    445  1.18   mycroft 		diff.tv_nsec = 0;
    446  1.18   mycroft 	} else {
    447  1.60  christos 		clockid_t clck = pthread_cond_getclock(cond);
    448  1.58  christos 		clock_gettime(clck, &now);
    449  1.18   mycroft 		if  (timespeccmp(abstime, &now, <))
    450  1.18   mycroft 			timespecclear(&diff);
    451  1.17   nathanw 		else
    452  1.18   mycroft 			timespecsub(abstime, &now, &diff);
    453   1.6   nathanw 	}
    454   1.6   nathanw 
    455  1.18   mycroft 	do {
    456  1.18   mycroft 		pthread__testcancel(self);
    457  1.18   mycroft 		pthread_mutex_unlock(mutex);
    458  1.55  drochner 		retval = _sys___nanosleep50(&diff, NULL);
    459  1.18   mycroft 		pthread_mutex_lock(mutex);
    460  1.18   mycroft 	} while (abstime == NULL && retval == 0);
    461   1.6   nathanw 	pthread__testcancel(self);
    462   1.6   nathanw 
    463   1.6   nathanw 	if (retval == 0)
    464   1.6   nathanw 		return ETIMEDOUT;
    465   1.6   nathanw 	else
    466  1.15    kleink 		/* spurious wakeup */
    467  1.15    kleink 		return 0;
    468   1.2   thorpej }
    469