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