Home | History | Annotate | Line # | Download | only in libpthread
pthread_cond.c revision 1.25
      1  1.24       ad /*	$NetBSD: pthread_cond.c,v 1.25 2007/03/05 22:25:27 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.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.24       ad __RCSID("$NetBSD: pthread_cond.c,v 1.25 2007/03/05 22:25:27 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.19       ad 	pthread_mutex_unlock(mutex);
    135  1.19       ad 	(void)pthread__park(self, &cond->ptc_lock, cond,
    136  1.23       ad 	    NULL, NULL, 0, 1);
    137  1.12  nathanw #ifdef ERRORCHECK
    138  1.16  nathanw 	if (PTQ_EMPTY(&cond->ptc_waiters))
    139  1.16  nathanw 		cond->ptc_mutex = NULL;
    140  1.23       ad #endif
    141  1.16  nathanw 	pthread_spinunlock(self, &cond->ptc_lock);
    142  1.25       ad 	pthread_mutex_lock(mutex);
    143  1.23       ad 
    144  1.16  nathanw 	if (__predict_false(self->pt_cancel))
    145  1.12  nathanw 		pthread_exit(PTHREAD_CANCELED);
    146  1.12  nathanw 
    147   1.2  thorpej 	SDPRINTF(("(cond wait %p) Woke up on %p, mutex %p\n",
    148   1.2  thorpej 	    self, cond, mutex));
    149   1.2  thorpej 
    150   1.2  thorpej 	return 0;
    151   1.2  thorpej }
    152   1.2  thorpej 
    153   1.2  thorpej 
    154   1.2  thorpej struct pthread_cond__waitarg {
    155   1.2  thorpej 	pthread_t ptw_thread;
    156   1.2  thorpej 	pthread_cond_t *ptw_cond;
    157   1.2  thorpej };
    158   1.2  thorpej 
    159   1.2  thorpej int
    160   1.2  thorpej pthread_cond_timedwait(pthread_cond_t *cond, pthread_mutex_t *mutex,
    161   1.2  thorpej     const struct timespec *abstime)
    162   1.2  thorpej {
    163   1.2  thorpej 	pthread_t self;
    164   1.2  thorpej 	int retval;
    165   1.2  thorpej 
    166  1.11  nathanw 	pthread__error(EINVAL, "Invalid condition variable",
    167  1.11  nathanw 	    cond->ptc_magic == _PT_COND_MAGIC);
    168  1.11  nathanw 	pthread__error(EINVAL, "Invalid mutex",
    169  1.11  nathanw 	    mutex->ptm_magic == _PT_MUTEX_MAGIC);
    170  1.11  nathanw 	pthread__error(EPERM, "Mutex not locked in condition wait",
    171  1.11  nathanw 	    mutex->ptm_lock == __SIMPLELOCK_LOCKED);
    172  1.11  nathanw 	pthread__error(EINVAL, "Invalid wait time",
    173  1.11  nathanw 	    (abstime->tv_sec >= 0) &&
    174  1.10  nathanw 	    (abstime->tv_nsec >= 0) && (abstime->tv_nsec < 1000000000));
    175  1.10  nathanw 
    176   1.2  thorpej 	self = pthread__self();
    177   1.6  nathanw 	PTHREADD_ADD(PTHREADD_COND_TIMEDWAIT);
    178   1.6  nathanw 
    179   1.6  nathanw 	/* Just hang out for a while if threads aren't running yet. */
    180   1.6  nathanw 	if (__predict_false(pthread__started == 0))
    181   1.6  nathanw 		return pthread_cond_wait_nothread(self, mutex, abstime);
    182   1.6  nathanw 
    183  1.21       ad 	SDPRINTF(("(cond timed wait %p) Waiting on %p until %d.%06ld\n",
    184  1.21       ad 	    self, cond, abstime->tv_sec, abstime->tv_nsec/1000));
    185  1.21       ad 
    186  1.25       ad 	if (__predict_false(self->pt_cancel))
    187  1.21       ad 		pthread_exit(PTHREAD_CANCELED);
    188  1.21       ad 	pthread_spinlock(self, &cond->ptc_lock);
    189  1.21       ad #ifdef ERRORCHECK
    190  1.21       ad 	if (cond->ptc_mutex == NULL)
    191  1.21       ad 		cond->ptc_mutex = mutex;
    192  1.21       ad 	else
    193  1.21       ad 		pthread__error(EINVAL,
    194  1.21       ad 		    "Multiple mutexes used for condition wait",
    195  1.21       ad 		    cond->ptc_mutex == mutex);
    196  1.21       ad #endif
    197  1.23       ad 	PTQ_INSERT_HEAD(&cond->ptc_waiters, self, pt_sleep);
    198  1.23       ad 	self->pt_sleeponq = 1;
    199  1.20       ad 	pthread_mutex_unlock(mutex);
    200  1.19       ad 	retval = pthread__park(self, &cond->ptc_lock, cond,
    201  1.23       ad 	    NULL, abstime, 0, 1);
    202  1.23       ad #ifdef ERRORCHECK
    203  1.23       ad 	if (PTQ_EMPTY(&cond->ptc_waiters))
    204  1.23       ad 		cond->ptc_mutex = NULL;
    205  1.23       ad #endif
    206  1.19       ad 	pthread_spinunlock(self, &cond->ptc_lock);
    207  1.19       ad 
    208  1.19       ad 	SDPRINTF(("(cond timed wait %p) Woke up on %p, mutex %p\n",
    209  1.19       ad 	    self, cond));
    210   1.2  thorpej 	SDPRINTF(("(cond timed wait %p) %s\n",
    211   1.2  thorpej 	    self, (retval == ETIMEDOUT) ? "(timed out)" : ""));
    212  1.25       ad 	pthread_mutex_lock(mutex);
    213  1.16  nathanw 	if (__predict_false(self->pt_cancel))
    214  1.12  nathanw 		pthread_exit(PTHREAD_CANCELED);
    215   1.2  thorpej 
    216   1.2  thorpej 	return retval;
    217   1.2  thorpej }
    218   1.2  thorpej 
    219   1.2  thorpej int
    220   1.2  thorpej pthread_cond_signal(pthread_cond_t *cond)
    221   1.2  thorpej {
    222   1.2  thorpej 	pthread_t self, signaled;
    223  1.10  nathanw 
    224  1.11  nathanw 	pthread__error(EINVAL, "Invalid condition variable",
    225  1.11  nathanw 	    cond->ptc_magic == _PT_COND_MAGIC);
    226   1.3  nathanw 	PTHREADD_ADD(PTHREADD_COND_SIGNAL);
    227   1.2  thorpej 
    228   1.2  thorpej 	SDPRINTF(("(cond signal %p) Signaling %p\n",
    229   1.4  nathanw 	    pthread__self(), cond));
    230   1.2  thorpej 
    231  1.21       ad 	if (!PTQ_EMPTY(&cond->ptc_waiters)) {
    232  1.21       ad 		self = pthread__self();
    233  1.21       ad 		pthread_spinlock(self, &cond->ptc_lock);
    234  1.21       ad 		signaled = PTQ_FIRST(&cond->ptc_waiters);
    235  1.21       ad 		if (signaled != NULL) {
    236  1.21       ad 			PTQ_REMOVE(&cond->ptc_waiters, signaled, pt_sleep);
    237  1.21       ad #ifdef ERRORCHECK
    238  1.21       ad 			if (PTQ_EMPTY(&cond->ptc_waiters))
    239  1.21       ad 				cond->ptc_mutex = NULL;
    240  1.21       ad #endif
    241  1.21       ad 			pthread__unpark(self, &cond->ptc_lock, cond, signaled);
    242  1.21       ad 			PTHREADD_ADD(PTHREADD_COND_WOKEUP);
    243  1.21       ad 		} else {
    244  1.20       ad #ifdef ERRORCHECK
    245  1.20       ad 			cond->ptc_mutex = NULL;
    246  1.20       ad #endif
    247  1.21       ad 			pthread_spinunlock(self, &cond->ptc_lock);
    248  1.21       ad 		}
    249   1.4  nathanw 	}
    250  1.22       ad 
    251   1.2  thorpej 	return 0;
    252   1.2  thorpej }
    253   1.2  thorpej 
    254   1.2  thorpej 
    255   1.2  thorpej int
    256   1.2  thorpej pthread_cond_broadcast(pthread_cond_t *cond)
    257   1.2  thorpej {
    258   1.5  nathanw 	pthread_t self;
    259  1.10  nathanw 
    260  1.11  nathanw 	pthread__error(EINVAL, "Invalid condition variable", cond->ptc_magic == _PT_COND_MAGIC);
    261   1.2  thorpej 
    262   1.3  nathanw 	PTHREADD_ADD(PTHREADD_COND_BROADCAST);
    263   1.2  thorpej 	SDPRINTF(("(cond signal %p) Broadcasting %p\n",
    264   1.4  nathanw 	    pthread__self(), cond));
    265   1.2  thorpej 
    266   1.4  nathanw 	if (!PTQ_EMPTY(&cond->ptc_waiters)) {
    267   1.4  nathanw 		self = pthread__self();
    268   1.4  nathanw 		pthread_spinlock(self, &cond->ptc_lock);
    269   1.2  thorpej #ifdef ERRORCHECK
    270   1.4  nathanw 		cond->ptc_mutex = NULL;
    271   1.2  thorpej #endif
    272  1.21       ad 		pthread__unpark_all(self, &cond->ptc_lock, cond, &cond->ptc_waiters);
    273  1.21       ad 		PTHREADD_ADD(PTHREADD_COND_WOKEUP);
    274  1.21       ad 	}
    275   1.2  thorpej 
    276   1.2  thorpej 	return 0;
    277   1.2  thorpej 
    278   1.2  thorpej }
    279   1.2  thorpej 
    280   1.2  thorpej 
    281   1.2  thorpej int
    282   1.2  thorpej pthread_condattr_init(pthread_condattr_t *attr)
    283   1.2  thorpej {
    284   1.2  thorpej 
    285   1.2  thorpej 	attr->ptca_magic = _PT_CONDATTR_MAGIC;
    286   1.2  thorpej 
    287   1.2  thorpej 	return 0;
    288   1.2  thorpej }
    289   1.2  thorpej 
    290   1.2  thorpej 
    291   1.2  thorpej int
    292   1.2  thorpej pthread_condattr_destroy(pthread_condattr_t *attr)
    293   1.2  thorpej {
    294   1.2  thorpej 
    295  1.11  nathanw 	pthread__error(EINVAL, "Invalid condition variable attribute",
    296  1.11  nathanw 	    attr->ptca_magic == _PT_CONDATTR_MAGIC);
    297   1.2  thorpej 
    298   1.2  thorpej 	attr->ptca_magic = _PT_CONDATTR_DEAD;
    299   1.2  thorpej 
    300   1.2  thorpej 	return 0;
    301   1.6  nathanw }
    302   1.6  nathanw 
    303   1.6  nathanw /* Utility routine to hang out for a while if threads haven't started yet. */
    304   1.6  nathanw static int
    305   1.6  nathanw pthread_cond_wait_nothread(pthread_t self, pthread_mutex_t *mutex,
    306   1.6  nathanw     const struct timespec *abstime)
    307   1.6  nathanw {
    308  1.18  mycroft 	struct timespec now, diff;
    309   1.6  nathanw 	int retval;
    310   1.6  nathanw 
    311  1.18  mycroft 	if (abstime == NULL) {
    312  1.18  mycroft 		diff.tv_sec = 99999999;
    313  1.18  mycroft 		diff.tv_nsec = 0;
    314  1.18  mycroft 	} else {
    315  1.18  mycroft 		clock_gettime(CLOCK_REALTIME, &now);
    316  1.18  mycroft 		if  (timespeccmp(abstime, &now, <))
    317  1.18  mycroft 			timespecclear(&diff);
    318  1.17  nathanw 		else
    319  1.18  mycroft 			timespecsub(abstime, &now, &diff);
    320   1.6  nathanw 	}
    321   1.6  nathanw 
    322  1.18  mycroft 	do {
    323  1.18  mycroft 		pthread__testcancel(self);
    324  1.18  mycroft 		pthread_mutex_unlock(mutex);
    325  1.18  mycroft 		retval = _sys_nanosleep(&diff, NULL);
    326  1.18  mycroft 		pthread_mutex_lock(mutex);
    327  1.18  mycroft 	} while (abstime == NULL && retval == 0);
    328   1.6  nathanw 	pthread__testcancel(self);
    329   1.6  nathanw 
    330   1.6  nathanw 	if (retval == 0)
    331   1.6  nathanw 		return ETIMEDOUT;
    332   1.6  nathanw 	else
    333  1.15   kleink 		/* spurious wakeup */
    334  1.15   kleink 		return 0;
    335   1.2  thorpej }
    336