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