Home | History | Annotate | Line # | Download | only in libpthread
pthread_cond.c revision 1.46
      1  1.46       ad /*	$NetBSD: pthread_cond.c,v 1.46 2008/05/26 00:16:35 ad 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.8    lukem #include <sys/cdefs.h>
     33  1.46       ad __RCSID("$NetBSD: pthread_cond.c,v 1.46 2008/05/26 00:16:35 ad Exp $");
     34   1.8    lukem 
     35   1.2  thorpej #include <errno.h>
     36   1.6  nathanw #include <sys/time.h>
     37   1.6  nathanw #include <sys/types.h>
     38   1.2  thorpej 
     39   1.2  thorpej #include "pthread.h"
     40   1.2  thorpej #include "pthread_int.h"
     41   1.2  thorpej 
     42  1.18  mycroft int	_sys_nanosleep(const struct timespec *, struct timespec *);
     43   1.6  nathanw 
     44   1.6  nathanw extern int pthread__started;
     45   1.6  nathanw 
     46   1.6  nathanw static int pthread_cond_wait_nothread(pthread_t, pthread_mutex_t *,
     47   1.6  nathanw     const struct timespec *);
     48   1.2  thorpej 
     49   1.2  thorpej __strong_alias(__libc_cond_init,pthread_cond_init)
     50   1.2  thorpej __strong_alias(__libc_cond_signal,pthread_cond_signal)
     51   1.2  thorpej __strong_alias(__libc_cond_broadcast,pthread_cond_broadcast)
     52   1.2  thorpej __strong_alias(__libc_cond_wait,pthread_cond_wait)
     53   1.2  thorpej __strong_alias(__libc_cond_timedwait,pthread_cond_timedwait)
     54   1.2  thorpej __strong_alias(__libc_cond_destroy,pthread_cond_destroy)
     55   1.2  thorpej 
     56   1.2  thorpej int
     57   1.2  thorpej pthread_cond_init(pthread_cond_t *cond, const pthread_condattr_t *attr)
     58   1.2  thorpej {
     59   1.2  thorpej 
     60  1.11  nathanw 	pthread__error(EINVAL, "Invalid condition variable attribute",
     61  1.11  nathanw 	    (attr == NULL) || (attr->ptca_magic == _PT_CONDATTR_MAGIC));
     62   1.2  thorpej 
     63   1.2  thorpej 	cond->ptc_magic = _PT_COND_MAGIC;
     64   1.2  thorpej 	pthread_lockinit(&cond->ptc_lock);
     65   1.2  thorpej 	PTQ_INIT(&cond->ptc_waiters);
     66   1.2  thorpej 	cond->ptc_mutex = NULL;
     67   1.2  thorpej 
     68   1.2  thorpej 	return 0;
     69   1.2  thorpej }
     70   1.2  thorpej 
     71   1.2  thorpej 
     72   1.2  thorpej int
     73   1.2  thorpej pthread_cond_destroy(pthread_cond_t *cond)
     74   1.2  thorpej {
     75   1.2  thorpej 
     76  1.11  nathanw 	pthread__error(EINVAL, "Invalid condition variable",
     77  1.11  nathanw 	    cond->ptc_magic == _PT_COND_MAGIC);
     78  1.11  nathanw 	pthread__error(EBUSY, "Destroying condition variable in use",
     79  1.11  nathanw 	    cond->ptc_mutex == NULL);
     80   1.2  thorpej 
     81   1.2  thorpej 	cond->ptc_magic = _PT_COND_DEAD;
     82   1.2  thorpej 
     83   1.2  thorpej 	return 0;
     84   1.2  thorpej }
     85   1.2  thorpej 
     86  1.43       ad inline int
     87  1.43       ad pthread_cond_timedwait(pthread_cond_t *cond, pthread_mutex_t *mutex,
     88  1.43       ad 		       const struct timespec *abstime)
     89   1.2  thorpej {
     90   1.2  thorpej 	pthread_t self;
     91  1.43       ad 	int retval;
     92  1.10  nathanw 
     93  1.11  nathanw 	pthread__error(EINVAL, "Invalid condition variable",
     94  1.11  nathanw 	    cond->ptc_magic == _PT_COND_MAGIC);
     95  1.11  nathanw 	pthread__error(EINVAL, "Invalid mutex",
     96  1.11  nathanw 	    mutex->ptm_magic == _PT_MUTEX_MAGIC);
     97  1.11  nathanw 	pthread__error(EPERM, "Mutex not locked in condition wait",
     98  1.35       ad 	    mutex->ptm_owner != NULL);
     99  1.43       ad 	if (abstime != NULL) {
    100  1.43       ad 		pthread__error(EINVAL, "Invalid wait time",
    101  1.43       ad 		    (abstime->tv_sec >= 0) &&
    102  1.43       ad 		    (abstime->tv_nsec >= 0) &&
    103  1.43       ad 		    (abstime->tv_nsec < 1000000000));
    104  1.43       ad 	}
    105  1.10  nathanw 
    106   1.2  thorpej 	self = pthread__self();
    107   1.6  nathanw 
    108   1.6  nathanw 	/* Just hang out for a while if threads aren't running yet. */
    109  1.43       ad 	if (__predict_false(pthread__started == 0)) {
    110  1.43       ad 		return pthread_cond_wait_nothread(self, mutex, abstime);
    111  1.43       ad 	}
    112  1.43       ad 	if (__predict_false(self->pt_cancel)) {
    113  1.40       ad 		pthread__cancelled();
    114  1.43       ad 	}
    115  1.32       ad 
    116  1.43       ad 	/* Note this thread as waiting on the CV. */
    117  1.38       ad 	pthread__spinlock(self, &cond->ptc_lock);
    118  1.36       ad 	cond->ptc_mutex = mutex;
    119  1.23       ad 	PTQ_INSERT_HEAD(&cond->ptc_waiters, self, pt_sleep);
    120  1.43       ad 	self->pt_sleepobj = cond;
    121  1.38       ad 	pthread__spinunlock(self, &cond->ptc_lock);
    122  1.32       ad 
    123  1.43       ad 	do {
    124  1.44       ad 		self->pt_willpark = 1;
    125  1.43       ad 		pthread_mutex_unlock(mutex);
    126  1.44       ad 		self->pt_willpark = 0;
    127  1.43       ad 		self->pt_blocking++;
    128  1.43       ad 		retval = _lwp_park(abstime, self->pt_unpark,
    129  1.43       ad 		    __UNVOLATILE(&mutex->ptm_waiters),
    130  1.43       ad 		    __UNVOLATILE(&mutex->ptm_waiters));
    131  1.43       ad 		self->pt_unpark = 0;
    132  1.43       ad 		self->pt_blocking--;
    133  1.43       ad 		membar_sync();
    134  1.43       ad 		pthread_mutex_lock(mutex);
    135  1.32       ad 
    136  1.43       ad 		/*
    137  1.43       ad 		 * If we have cancelled then exit.  POSIX dictates that
    138  1.43       ad 		 * the mutex must be held when we action the cancellation.
    139  1.43       ad 		 *
    140  1.43       ad 		 * If we absorbed a pthread_cond_signal() and cannot take
    141  1.43       ad 		 * the wakeup, we must ensure that another thread does.
    142  1.43       ad 		 *
    143  1.46       ad 		 * If awoke early, we may still be on the sleep queue and
    144  1.46       ad 		 * must remove ourself.
    145  1.43       ad 		 */
    146  1.46       ad 		if (__predict_false(retval != 0)) {
    147  1.46       ad 			switch (errno) {
    148  1.46       ad 			case EINTR:
    149  1.46       ad 			case EALREADY:
    150  1.46       ad 				retval = 0;
    151  1.46       ad 				break;
    152  1.46       ad 			default:
    153  1.46       ad 				retval = errno;
    154  1.46       ad 				break;
    155  1.46       ad 			}
    156  1.45       ad 		}
    157  1.45       ad 		if (__predict_false(self->pt_cancel | retval)) {
    158  1.43       ad 			pthread_cond_broadcast(cond);
    159  1.43       ad 			if (self->pt_cancel) {
    160  1.43       ad 				pthread__cancelled();
    161  1.43       ad 			}
    162  1.43       ad 			break;
    163  1.43       ad 		}
    164  1.43       ad 	} while (self->pt_sleepobj != NULL);
    165  1.32       ad 
    166  1.43       ad 	return retval;
    167   1.2  thorpej }
    168   1.2  thorpej 
    169   1.2  thorpej int
    170  1.43       ad pthread_cond_wait(pthread_cond_t *cond, pthread_mutex_t *mutex)
    171   1.2  thorpej {
    172  1.32       ad 
    173  1.43       ad 	return pthread_cond_timedwait(cond, mutex, NULL);
    174   1.2  thorpej }
    175   1.2  thorpej 
    176   1.2  thorpej int
    177   1.2  thorpej pthread_cond_signal(pthread_cond_t *cond)
    178   1.2  thorpej {
    179   1.2  thorpej 	pthread_t self, signaled;
    180  1.28       ad 	pthread_mutex_t *mutex;
    181  1.43       ad 	lwpid_t lid;
    182  1.43       ad 
    183  1.43       ad 	if (PTQ_EMPTY(&cond->ptc_waiters))
    184  1.43       ad 		return 0;
    185  1.10  nathanw 
    186  1.11  nathanw 	pthread__error(EINVAL, "Invalid condition variable",
    187  1.11  nathanw 	    cond->ptc_magic == _PT_COND_MAGIC);
    188   1.2  thorpej 
    189  1.43       ad 	/* Pull the first thread off the queue. */
    190  1.27       ad 	self = pthread__self();
    191  1.38       ad 	pthread__spinlock(self, &cond->ptc_lock);
    192  1.43       ad 	signaled = PTQ_FIRST(&cond->ptc_waiters);
    193  1.28       ad 	if (__predict_false(signaled == NULL)) {
    194  1.46       ad 		pthread__spinunlock(self, &cond->ptc_lock);
    195  1.28       ad 		return 0;
    196  1.28       ad 	}
    197  1.28       ad 	mutex = cond->ptc_mutex;
    198  1.43       ad 	if (PTQ_NEXT(signaled, pt_sleep) == NULL) {
    199  1.28       ad 		cond->ptc_mutex = NULL;
    200  1.43       ad 		PTQ_INIT(&cond->ptc_waiters);
    201  1.43       ad 	} else {
    202  1.43       ad 		PTQ_REMOVE(&cond->ptc_waiters, signaled, pt_sleep);
    203  1.43       ad 	}
    204  1.43       ad 	signaled->pt_sleepobj = NULL;
    205  1.43       ad 	lid = signaled->pt_lid;
    206  1.43       ad 	pthread__spinunlock(self, &cond->ptc_lock);
    207  1.28       ad 
    208  1.28       ad 	/*
    209  1.28       ad 	 * For all valid uses of pthread_cond_signal(), the caller will
    210  1.28       ad 	 * hold the mutex that the target is using to synchronize with.
    211  1.28       ad 	 * To avoid the target awakening and immediatley blocking on the
    212  1.35       ad 	 * mutex, transfer the thread to be awoken to the current thread's
    213  1.35       ad 	 * deferred wakeup list.  The waiter will be set running when the
    214  1.35       ad 	 * caller (this thread) releases the mutex.
    215  1.28       ad 	 */
    216  1.43       ad 	if (__predict_false(self->pt_nwaiters == pthread__unpark_max)) {
    217  1.43       ad 		(void)_lwp_unpark_all(self->pt_waiters, self->pt_nwaiters,
    218  1.43       ad 		    __UNVOLATILE(&mutex->ptm_waiters));
    219  1.43       ad 		self->pt_nwaiters = 0;
    220   1.4  nathanw 	}
    221  1.43       ad 	self->pt_waiters[self->pt_nwaiters++] = lid;
    222  1.43       ad 	pthread__mutex_deferwake(self, mutex);
    223   1.2  thorpej 	return 0;
    224   1.2  thorpej }
    225   1.2  thorpej 
    226   1.2  thorpej int
    227   1.2  thorpej pthread_cond_broadcast(pthread_cond_t *cond)
    228   1.2  thorpej {
    229  1.43       ad 	pthread_t self, signaled;
    230  1.28       ad 	pthread_mutex_t *mutex;
    231  1.43       ad 	u_int max, nwaiters;
    232   1.2  thorpej 
    233  1.27       ad 	if (PTQ_EMPTY(&cond->ptc_waiters))
    234  1.27       ad 		return 0;
    235  1.27       ad 
    236  1.43       ad 	pthread__error(EINVAL, "Invalid condition variable",
    237  1.43       ad 	    cond->ptc_magic == _PT_COND_MAGIC);
    238  1.28       ad 
    239  1.28       ad 	/*
    240  1.35       ad 	 * Try to defer waking threads (see pthread_cond_signal()).
    241  1.35       ad 	 * Only transfer waiters for which there is no pending wakeup.
    242  1.28       ad 	 */
    243  1.43       ad 	self = pthread__self();
    244  1.43       ad 	pthread__spinlock(self, &cond->ptc_lock);
    245  1.43       ad 	max = pthread__unpark_max;
    246  1.43       ad 	mutex = cond->ptc_mutex;
    247  1.43       ad 	nwaiters = self->pt_nwaiters;
    248  1.43       ad 	PTQ_FOREACH(signaled, &cond->ptc_waiters, pt_sleep) {
    249  1.43       ad 		if (nwaiters == max) {
    250  1.43       ad 			/* Overflow. */
    251  1.43       ad 			(void)_lwp_unpark_all(self->pt_waiters,
    252  1.43       ad 			    nwaiters, __UNVOLATILE(&mutex->ptm_waiters));
    253  1.43       ad 			nwaiters = 0;
    254  1.28       ad 		}
    255  1.43       ad 		signaled->pt_sleepobj = NULL;
    256  1.43       ad 		self->pt_waiters[nwaiters++] = signaled->pt_lid;
    257  1.35       ad 	}
    258  1.43       ad 	PTQ_INIT(&cond->ptc_waiters);
    259  1.43       ad 	self->pt_nwaiters = nwaiters;
    260  1.43       ad 	cond->ptc_mutex = NULL;
    261  1.43       ad 	pthread__spinunlock(self, &cond->ptc_lock);
    262  1.43       ad 	pthread__mutex_deferwake(self, mutex);
    263  1.43       ad 
    264   1.2  thorpej 	return 0;
    265   1.2  thorpej }
    266   1.2  thorpej 
    267   1.2  thorpej 
    268   1.2  thorpej int
    269   1.2  thorpej pthread_condattr_init(pthread_condattr_t *attr)
    270   1.2  thorpej {
    271   1.2  thorpej 
    272   1.2  thorpej 	attr->ptca_magic = _PT_CONDATTR_MAGIC;
    273   1.2  thorpej 
    274   1.2  thorpej 	return 0;
    275   1.2  thorpej }
    276   1.2  thorpej 
    277   1.2  thorpej 
    278   1.2  thorpej int
    279   1.2  thorpej pthread_condattr_destroy(pthread_condattr_t *attr)
    280   1.2  thorpej {
    281   1.2  thorpej 
    282  1.11  nathanw 	pthread__error(EINVAL, "Invalid condition variable attribute",
    283  1.11  nathanw 	    attr->ptca_magic == _PT_CONDATTR_MAGIC);
    284   1.2  thorpej 
    285   1.2  thorpej 	attr->ptca_magic = _PT_CONDATTR_DEAD;
    286   1.2  thorpej 
    287   1.2  thorpej 	return 0;
    288   1.6  nathanw }
    289   1.6  nathanw 
    290   1.6  nathanw /* Utility routine to hang out for a while if threads haven't started yet. */
    291   1.6  nathanw static int
    292   1.6  nathanw pthread_cond_wait_nothread(pthread_t self, pthread_mutex_t *mutex,
    293   1.6  nathanw     const struct timespec *abstime)
    294   1.6  nathanw {
    295  1.18  mycroft 	struct timespec now, diff;
    296   1.6  nathanw 	int retval;
    297   1.6  nathanw 
    298  1.18  mycroft 	if (abstime == NULL) {
    299  1.18  mycroft 		diff.tv_sec = 99999999;
    300  1.18  mycroft 		diff.tv_nsec = 0;
    301  1.18  mycroft 	} else {
    302  1.18  mycroft 		clock_gettime(CLOCK_REALTIME, &now);
    303  1.18  mycroft 		if  (timespeccmp(abstime, &now, <))
    304  1.18  mycroft 			timespecclear(&diff);
    305  1.17  nathanw 		else
    306  1.18  mycroft 			timespecsub(abstime, &now, &diff);
    307   1.6  nathanw 	}
    308   1.6  nathanw 
    309  1.18  mycroft 	do {
    310  1.18  mycroft 		pthread__testcancel(self);
    311  1.18  mycroft 		pthread_mutex_unlock(mutex);
    312  1.18  mycroft 		retval = _sys_nanosleep(&diff, NULL);
    313  1.18  mycroft 		pthread_mutex_lock(mutex);
    314  1.18  mycroft 	} while (abstime == NULL && retval == 0);
    315   1.6  nathanw 	pthread__testcancel(self);
    316   1.6  nathanw 
    317   1.6  nathanw 	if (retval == 0)
    318   1.6  nathanw 		return ETIMEDOUT;
    319   1.6  nathanw 	else
    320  1.15   kleink 		/* spurious wakeup */
    321  1.15   kleink 		return 0;
    322   1.2  thorpej }
    323