Home | History | Annotate | Line # | Download | only in libpthread
pthread_cond.c revision 1.27
      1  1.27       ad /*	$NetBSD: pthread_cond.c,v 1.27 2007/03/14 23:34:48 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.27       ad __RCSID("$NetBSD: pthread_cond.c,v 1.27 2007/03/14 23:34:48 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.6  nathanw static int pthread_cond_wait_nothread(pthread_t, pthread_mutex_t *,
     60   1.6  nathanw     const struct timespec *);
     61   1.2  thorpej 
     62   1.2  thorpej __strong_alias(__libc_cond_init,pthread_cond_init)
     63   1.2  thorpej __strong_alias(__libc_cond_signal,pthread_cond_signal)
     64   1.2  thorpej __strong_alias(__libc_cond_broadcast,pthread_cond_broadcast)
     65   1.2  thorpej __strong_alias(__libc_cond_wait,pthread_cond_wait)
     66   1.2  thorpej __strong_alias(__libc_cond_timedwait,pthread_cond_timedwait)
     67   1.2  thorpej __strong_alias(__libc_cond_destroy,pthread_cond_destroy)
     68   1.2  thorpej 
     69   1.2  thorpej int
     70   1.2  thorpej pthread_cond_init(pthread_cond_t *cond, const pthread_condattr_t *attr)
     71   1.2  thorpej {
     72   1.2  thorpej 
     73  1.11  nathanw 	pthread__error(EINVAL, "Invalid condition variable attribute",
     74  1.11  nathanw 	    (attr == NULL) || (attr->ptca_magic == _PT_CONDATTR_MAGIC));
     75   1.2  thorpej 
     76   1.2  thorpej 	cond->ptc_magic = _PT_COND_MAGIC;
     77   1.2  thorpej 	pthread_lockinit(&cond->ptc_lock);
     78   1.2  thorpej 	PTQ_INIT(&cond->ptc_waiters);
     79   1.2  thorpej 	cond->ptc_mutex = NULL;
     80   1.2  thorpej 
     81   1.2  thorpej 	return 0;
     82   1.2  thorpej }
     83   1.2  thorpej 
     84   1.2  thorpej 
     85   1.2  thorpej int
     86   1.2  thorpej pthread_cond_destroy(pthread_cond_t *cond)
     87   1.2  thorpej {
     88   1.2  thorpej 
     89  1.11  nathanw 	pthread__error(EINVAL, "Invalid condition variable",
     90  1.11  nathanw 	    cond->ptc_magic == _PT_COND_MAGIC);
     91  1.11  nathanw 	pthread__error(EBUSY, "Destroying condition variable in use",
     92  1.11  nathanw 	    cond->ptc_mutex == NULL);
     93   1.2  thorpej 
     94   1.2  thorpej 	cond->ptc_magic = _PT_COND_DEAD;
     95   1.2  thorpej 
     96   1.2  thorpej 	return 0;
     97   1.2  thorpej }
     98   1.2  thorpej 
     99   1.2  thorpej 
    100   1.2  thorpej int
    101   1.2  thorpej pthread_cond_wait(pthread_cond_t *cond, pthread_mutex_t *mutex)
    102   1.2  thorpej {
    103   1.2  thorpej 	pthread_t self;
    104  1.10  nathanw 
    105  1.11  nathanw 	pthread__error(EINVAL, "Invalid condition variable",
    106  1.11  nathanw 	    cond->ptc_magic == _PT_COND_MAGIC);
    107  1.11  nathanw 	pthread__error(EINVAL, "Invalid mutex",
    108  1.11  nathanw 	    mutex->ptm_magic == _PT_MUTEX_MAGIC);
    109  1.11  nathanw 	pthread__error(EPERM, "Mutex not locked in condition wait",
    110  1.11  nathanw 	    mutex->ptm_lock == __SIMPLELOCK_LOCKED);
    111  1.10  nathanw 
    112   1.2  thorpej 	self = pthread__self();
    113   1.3  nathanw 	PTHREADD_ADD(PTHREADD_COND_WAIT);
    114   1.6  nathanw 
    115   1.6  nathanw 	/* Just hang out for a while if threads aren't running yet. */
    116   1.6  nathanw 	if (__predict_false(pthread__started == 0))
    117   1.6  nathanw 		return pthread_cond_wait_nothread(self, mutex, NULL);
    118   1.6  nathanw 
    119  1.21       ad 	SDPRINTF(("(cond wait %p) Waiting on %p, mutex %p\n",
    120  1.21       ad 	    self, cond, mutex));
    121  1.25       ad 	if (__predict_false(self->pt_cancel))
    122  1.21       ad 		pthread_exit(PTHREAD_CANCELED);
    123  1.21       ad 	pthread_spinlock(self, &cond->ptc_lock);
    124  1.21       ad #ifdef ERRORCHECK
    125  1.21       ad 	if (cond->ptc_mutex == NULL)
    126  1.21       ad 		cond->ptc_mutex = mutex;
    127  1.21       ad 	else
    128  1.21       ad 		pthread__error(EINVAL,
    129  1.21       ad 		    "Multiple mutexes used for condition wait",
    130  1.21       ad 		    cond->ptc_mutex == mutex);
    131  1.21       ad #endif
    132  1.23       ad 	PTQ_INSERT_HEAD(&cond->ptc_waiters, self, pt_sleep);
    133  1.23       ad 	self->pt_sleeponq = 1;
    134  1.26       ad 	self->pt_sleepobj = &cond->ptc_waiters;
    135  1.19       ad 	pthread_mutex_unlock(mutex);
    136  1.26       ad 	(void)pthread__park(self, &cond->ptc_lock, &cond->ptc_waiters,
    137  1.26       ad 	    NULL, 1);
    138  1.12  nathanw #ifdef ERRORCHECK
    139  1.16  nathanw 	if (PTQ_EMPTY(&cond->ptc_waiters))
    140  1.16  nathanw 		cond->ptc_mutex = NULL;
    141  1.23       ad #endif
    142  1.16  nathanw 	pthread_spinunlock(self, &cond->ptc_lock);
    143  1.25       ad 	pthread_mutex_lock(mutex);
    144  1.23       ad 
    145  1.16  nathanw 	if (__predict_false(self->pt_cancel))
    146  1.12  nathanw 		pthread_exit(PTHREAD_CANCELED);
    147  1.12  nathanw 
    148   1.2  thorpej 	SDPRINTF(("(cond wait %p) Woke up on %p, mutex %p\n",
    149   1.2  thorpej 	    self, cond, mutex));
    150   1.2  thorpej 
    151   1.2  thorpej 	return 0;
    152   1.2  thorpej }
    153   1.2  thorpej 
    154   1.2  thorpej int
    155   1.2  thorpej pthread_cond_timedwait(pthread_cond_t *cond, pthread_mutex_t *mutex,
    156   1.2  thorpej     const struct timespec *abstime)
    157   1.2  thorpej {
    158   1.2  thorpej 	pthread_t self;
    159   1.2  thorpej 	int retval;
    160   1.2  thorpej 
    161  1.11  nathanw 	pthread__error(EINVAL, "Invalid condition variable",
    162  1.11  nathanw 	    cond->ptc_magic == _PT_COND_MAGIC);
    163  1.11  nathanw 	pthread__error(EINVAL, "Invalid mutex",
    164  1.11  nathanw 	    mutex->ptm_magic == _PT_MUTEX_MAGIC);
    165  1.11  nathanw 	pthread__error(EPERM, "Mutex not locked in condition wait",
    166  1.11  nathanw 	    mutex->ptm_lock == __SIMPLELOCK_LOCKED);
    167  1.11  nathanw 	pthread__error(EINVAL, "Invalid wait time",
    168  1.11  nathanw 	    (abstime->tv_sec >= 0) &&
    169  1.10  nathanw 	    (abstime->tv_nsec >= 0) && (abstime->tv_nsec < 1000000000));
    170  1.10  nathanw 
    171   1.2  thorpej 	self = pthread__self();
    172   1.6  nathanw 	PTHREADD_ADD(PTHREADD_COND_TIMEDWAIT);
    173   1.6  nathanw 
    174   1.6  nathanw 	/* Just hang out for a while if threads aren't running yet. */
    175   1.6  nathanw 	if (__predict_false(pthread__started == 0))
    176   1.6  nathanw 		return pthread_cond_wait_nothread(self, mutex, abstime);
    177   1.6  nathanw 
    178  1.21       ad 	SDPRINTF(("(cond timed wait %p) Waiting on %p until %d.%06ld\n",
    179  1.21       ad 	    self, cond, abstime->tv_sec, abstime->tv_nsec/1000));
    180  1.21       ad 
    181  1.25       ad 	if (__predict_false(self->pt_cancel))
    182  1.21       ad 		pthread_exit(PTHREAD_CANCELED);
    183  1.21       ad 	pthread_spinlock(self, &cond->ptc_lock);
    184  1.21       ad #ifdef ERRORCHECK
    185  1.21       ad 	if (cond->ptc_mutex == NULL)
    186  1.21       ad 		cond->ptc_mutex = mutex;
    187  1.21       ad 	else
    188  1.21       ad 		pthread__error(EINVAL,
    189  1.21       ad 		    "Multiple mutexes used for condition wait",
    190  1.21       ad 		    cond->ptc_mutex == mutex);
    191  1.21       ad #endif
    192  1.23       ad 	PTQ_INSERT_HEAD(&cond->ptc_waiters, self, pt_sleep);
    193  1.23       ad 	self->pt_sleeponq = 1;
    194  1.26       ad 	self->pt_sleepobj = &cond->ptc_waiters;
    195  1.20       ad 	pthread_mutex_unlock(mutex);
    196  1.26       ad 	retval = pthread__park(self, &cond->ptc_lock, &cond->ptc_waiters,
    197  1.26       ad 	    abstime, 1);
    198  1.23       ad #ifdef ERRORCHECK
    199  1.23       ad 	if (PTQ_EMPTY(&cond->ptc_waiters))
    200  1.23       ad 		cond->ptc_mutex = NULL;
    201  1.23       ad #endif
    202  1.19       ad 	pthread_spinunlock(self, &cond->ptc_lock);
    203  1.19       ad 
    204  1.19       ad 	SDPRINTF(("(cond timed wait %p) Woke up on %p, mutex %p\n",
    205  1.19       ad 	    self, cond));
    206   1.2  thorpej 	SDPRINTF(("(cond timed wait %p) %s\n",
    207   1.2  thorpej 	    self, (retval == ETIMEDOUT) ? "(timed out)" : ""));
    208  1.25       ad 	pthread_mutex_lock(mutex);
    209  1.16  nathanw 	if (__predict_false(self->pt_cancel))
    210  1.12  nathanw 		pthread_exit(PTHREAD_CANCELED);
    211   1.2  thorpej 
    212   1.2  thorpej 	return retval;
    213   1.2  thorpej }
    214   1.2  thorpej 
    215   1.2  thorpej int
    216   1.2  thorpej pthread_cond_signal(pthread_cond_t *cond)
    217   1.2  thorpej {
    218   1.2  thorpej 	pthread_t self, signaled;
    219  1.10  nathanw 
    220  1.11  nathanw 	pthread__error(EINVAL, "Invalid condition variable",
    221  1.11  nathanw 	    cond->ptc_magic == _PT_COND_MAGIC);
    222   1.3  nathanw 	PTHREADD_ADD(PTHREADD_COND_SIGNAL);
    223   1.2  thorpej 
    224   1.2  thorpej 	SDPRINTF(("(cond signal %p) Signaling %p\n",
    225   1.4  nathanw 	    pthread__self(), cond));
    226   1.2  thorpej 
    227  1.27       ad 	if (PTQ_EMPTY(&cond->ptc_waiters))
    228  1.27       ad 		return 0;
    229  1.27       ad 
    230  1.27       ad 	self = pthread__self();
    231  1.27       ad 	pthread_spinlock(self, &cond->ptc_lock);
    232  1.27       ad 	signaled = PTQ_FIRST(&cond->ptc_waiters);
    233  1.27       ad 	if (signaled != NULL) {
    234  1.27       ad 		PTQ_REMOVE(&cond->ptc_waiters, signaled, pt_sleep);
    235  1.27       ad 		if (PTQ_EMPTY(&cond->ptc_waiters))
    236  1.20       ad 			cond->ptc_mutex = NULL;
    237  1.27       ad 		pthread__unpark(self, &cond->ptc_lock,
    238  1.27       ad 		    &cond->ptc_waiters, signaled);
    239  1.27       ad 		PTHREADD_ADD(PTHREADD_COND_WOKEUP);
    240  1.27       ad 		return 0;
    241   1.4  nathanw 	}
    242  1.27       ad 	cond->ptc_mutex = NULL;
    243  1.27       ad 	pthread_spinunlock(self, &cond->ptc_lock);
    244  1.22       ad 
    245   1.2  thorpej 	return 0;
    246   1.2  thorpej }
    247   1.2  thorpej 
    248   1.2  thorpej 
    249   1.2  thorpej int
    250   1.2  thorpej pthread_cond_broadcast(pthread_cond_t *cond)
    251   1.2  thorpej {
    252   1.5  nathanw 	pthread_t self;
    253  1.10  nathanw 
    254  1.11  nathanw 	pthread__error(EINVAL, "Invalid condition variable", cond->ptc_magic == _PT_COND_MAGIC);
    255   1.2  thorpej 
    256   1.3  nathanw 	PTHREADD_ADD(PTHREADD_COND_BROADCAST);
    257   1.2  thorpej 	SDPRINTF(("(cond signal %p) Broadcasting %p\n",
    258   1.4  nathanw 	    pthread__self(), cond));
    259   1.2  thorpej 
    260  1.27       ad 	if (PTQ_EMPTY(&cond->ptc_waiters))
    261  1.27       ad 		return 0;
    262  1.27       ad 
    263  1.27       ad 	self = pthread__self();
    264  1.27       ad 	pthread_spinlock(self, &cond->ptc_lock);
    265  1.27       ad 	cond->ptc_mutex = NULL;
    266  1.27       ad 	pthread__unpark_all(self, &cond->ptc_lock, &cond->ptc_waiters);
    267  1.27       ad 	PTHREADD_ADD(PTHREADD_COND_WOKEUP);
    268   1.2  thorpej 
    269   1.2  thorpej 	return 0;
    270   1.2  thorpej 
    271   1.2  thorpej }
    272   1.2  thorpej 
    273   1.2  thorpej 
    274   1.2  thorpej int
    275   1.2  thorpej pthread_condattr_init(pthread_condattr_t *attr)
    276   1.2  thorpej {
    277   1.2  thorpej 
    278   1.2  thorpej 	attr->ptca_magic = _PT_CONDATTR_MAGIC;
    279   1.2  thorpej 
    280   1.2  thorpej 	return 0;
    281   1.2  thorpej }
    282   1.2  thorpej 
    283   1.2  thorpej 
    284   1.2  thorpej int
    285   1.2  thorpej pthread_condattr_destroy(pthread_condattr_t *attr)
    286   1.2  thorpej {
    287   1.2  thorpej 
    288  1.11  nathanw 	pthread__error(EINVAL, "Invalid condition variable attribute",
    289  1.11  nathanw 	    attr->ptca_magic == _PT_CONDATTR_MAGIC);
    290   1.2  thorpej 
    291   1.2  thorpej 	attr->ptca_magic = _PT_CONDATTR_DEAD;
    292   1.2  thorpej 
    293   1.2  thorpej 	return 0;
    294   1.6  nathanw }
    295   1.6  nathanw 
    296   1.6  nathanw /* Utility routine to hang out for a while if threads haven't started yet. */
    297   1.6  nathanw static int
    298   1.6  nathanw pthread_cond_wait_nothread(pthread_t self, pthread_mutex_t *mutex,
    299   1.6  nathanw     const struct timespec *abstime)
    300   1.6  nathanw {
    301  1.18  mycroft 	struct timespec now, diff;
    302   1.6  nathanw 	int retval;
    303   1.6  nathanw 
    304  1.18  mycroft 	if (abstime == NULL) {
    305  1.18  mycroft 		diff.tv_sec = 99999999;
    306  1.18  mycroft 		diff.tv_nsec = 0;
    307  1.18  mycroft 	} else {
    308  1.18  mycroft 		clock_gettime(CLOCK_REALTIME, &now);
    309  1.18  mycroft 		if  (timespeccmp(abstime, &now, <))
    310  1.18  mycroft 			timespecclear(&diff);
    311  1.17  nathanw 		else
    312  1.18  mycroft 			timespecsub(abstime, &now, &diff);
    313   1.6  nathanw 	}
    314   1.6  nathanw 
    315  1.18  mycroft 	do {
    316  1.18  mycroft 		pthread__testcancel(self);
    317  1.18  mycroft 		pthread_mutex_unlock(mutex);
    318  1.18  mycroft 		retval = _sys_nanosleep(&diff, NULL);
    319  1.18  mycroft 		pthread_mutex_lock(mutex);
    320  1.18  mycroft 	} while (abstime == NULL && retval == 0);
    321   1.6  nathanw 	pthread__testcancel(self);
    322   1.6  nathanw 
    323   1.6  nathanw 	if (retval == 0)
    324   1.6  nathanw 		return ETIMEDOUT;
    325   1.6  nathanw 	else
    326  1.15   kleink 		/* spurious wakeup */
    327  1.15   kleink 		return 0;
    328   1.2  thorpej }
    329