Home | History | Annotate | Line # | Download | only in libpthread
pthread_cond.c revision 1.10
      1 /*	$NetBSD: pthread_cond.c,v 1.10 2003/04/18 21:36:38 nathanw Exp $	*/
      2 
      3 /*-
      4  * Copyright (c) 2001 The NetBSD Foundation, Inc.
      5  * All rights reserved.
      6  *
      7  * This code is derived from software contributed to The NetBSD Foundation
      8  * by Nathan J. Williams.
      9  *
     10  * Redistribution and use in source and binary forms, with or without
     11  * modification, are permitted provided that the following conditions
     12  * are met:
     13  * 1. Redistributions of source code must retain the above copyright
     14  *    notice, this list of conditions and the following disclaimer.
     15  * 2. Redistributions in binary form must reproduce the above copyright
     16  *    notice, this list of conditions and the following disclaimer in the
     17  *    documentation and/or other materials provided with the distribution.
     18  * 3. All advertising materials mentioning features or use of this software
     19  *    must display the following acknowledgement:
     20  *        This product includes software developed by the NetBSD
     21  *        Foundation, Inc. and its contributors.
     22  * 4. Neither the name of The NetBSD Foundation nor the names of its
     23  *    contributors may be used to endorse or promote products derived
     24  *    from this software without specific prior written permission.
     25  *
     26  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     27  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     28  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     29  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     30  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     31  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     32  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     33  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     34  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     35  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     36  * POSSIBILITY OF SUCH DAMAGE.
     37  */
     38 
     39 #include <sys/cdefs.h>
     40 __RCSID("$NetBSD: pthread_cond.c,v 1.10 2003/04/18 21:36:38 nathanw Exp $");
     41 
     42 #include <errno.h>
     43 #include <sys/time.h>
     44 #include <sys/types.h>
     45 
     46 #include "pthread.h"
     47 #include "pthread_int.h"
     48 
     49 #ifdef PTHREAD_COND_DEBUG
     50 #define SDPRINTF(x) DPRINTF(x)
     51 #else
     52 #define SDPRINTF(x)
     53 #endif
     54 
     55 int	_sys_select(int, fd_set *, fd_set *, fd_set *, struct timeval *);
     56 
     57 extern int pthread__started;
     58 
     59 static void pthread_cond_wait__callback(void *);
     60 static int pthread_cond_wait_nothread(pthread_t, pthread_mutex_t *,
     61     const struct timespec *);
     62 
     63 __strong_alias(__libc_cond_init,pthread_cond_init)
     64 __strong_alias(__libc_cond_signal,pthread_cond_signal)
     65 __strong_alias(__libc_cond_broadcast,pthread_cond_broadcast)
     66 __strong_alias(__libc_cond_wait,pthread_cond_wait)
     67 __strong_alias(__libc_cond_timedwait,pthread_cond_timedwait)
     68 __strong_alias(__libc_cond_destroy,pthread_cond_destroy)
     69 
     70 int
     71 pthread_cond_init(pthread_cond_t *cond, const pthread_condattr_t *attr)
     72 {
     73 
     74 	pthread__assert((attr == NULL) ||
     75 	    (attr->ptca_magic == _PT_CONDATTR_MAGIC));
     76 
     77 	cond->ptc_magic = _PT_COND_MAGIC;
     78 	pthread_lockinit(&cond->ptc_lock);
     79 	PTQ_INIT(&cond->ptc_waiters);
     80 	cond->ptc_mutex = NULL;
     81 
     82 	return 0;
     83 }
     84 
     85 
     86 int
     87 pthread_cond_destroy(pthread_cond_t *cond)
     88 {
     89 
     90 	pthread__assert(cond->ptc_magic == _PT_COND_MAGIC);
     91 	pthread__assert(cond->ptc_mutex == NULL);
     92 
     93 	cond->ptc_magic = _PT_COND_DEAD;
     94 
     95 	return 0;
     96 }
     97 
     98 
     99 int
    100 pthread_cond_wait(pthread_cond_t *cond, pthread_mutex_t *mutex)
    101 {
    102 	pthread_t self;
    103 
    104 	pthread__assert(cond->ptc_magic == _PT_COND_MAGIC);
    105 	pthread__assert(mutex->ptm_magic == _PT_MUTEX_MAGIC);
    106 	pthread__assert(mutex->ptm_lock == __SIMPLELOCK_LOCKED);
    107 
    108 	self = pthread__self();
    109 	PTHREADD_ADD(PTHREADD_COND_WAIT);
    110 
    111 	/* Just hang out for a while if threads aren't running yet. */
    112 	if (__predict_false(pthread__started == 0))
    113 		return pthread_cond_wait_nothread(self, mutex, NULL);
    114 
    115 	pthread_spinlock(self, &cond->ptc_lock);
    116 #ifdef ERRORCHECK
    117 	if (cond->ptc_mutex == NULL)
    118 		cond->ptc_mutex = mutex;
    119 	else
    120 		pthread__assert(cond->ptc_mutex == mutex);
    121 #endif
    122 
    123 	SDPRINTF(("(cond wait %p) Waiting on %p, mutex %p\n",
    124 	    self, cond, mutex));
    125 	pthread_spinlock(self, &self->pt_statelock);
    126 	if (self->pt_cancel) {
    127 		pthread_spinunlock(self, &self->pt_statelock);
    128 		pthread_spinunlock(self, &cond->ptc_lock);
    129 		pthread_exit(PTHREAD_CANCELED);
    130 	}
    131 	self->pt_state = PT_STATE_BLOCKED_QUEUE;
    132 	self->pt_sleepobj = cond;
    133 	self->pt_sleepq = &cond->ptc_waiters;
    134 	self->pt_sleeplock = &cond->ptc_lock;
    135 	pthread_spinunlock(self, &self->pt_statelock);
    136 
    137 	PTQ_INSERT_HEAD(&cond->ptc_waiters, self, pt_sleep);
    138 	pthread_mutex_unlock(mutex);
    139 
    140 	pthread__block(self, &cond->ptc_lock);
    141 	/* Spinlock is unlocked on return */
    142 	pthread_mutex_lock(mutex);
    143 	pthread__testcancel(self);
    144 	SDPRINTF(("(cond wait %p) Woke up on %p, mutex %p\n",
    145 	    self, cond, mutex));
    146 
    147 	return 0;
    148 }
    149 
    150 
    151 struct pthread_cond__waitarg {
    152 	pthread_t ptw_thread;
    153 	pthread_cond_t *ptw_cond;
    154 };
    155 
    156 int
    157 pthread_cond_timedwait(pthread_cond_t *cond, pthread_mutex_t *mutex,
    158     const struct timespec *abstime)
    159 {
    160 	pthread_t self;
    161 	struct pthread_cond__waitarg wait;
    162 	struct pt_alarm_t alarm;
    163 	int retval;
    164 
    165 	pthread__assert(cond->ptc_magic == _PT_COND_MAGIC);
    166 	pthread__assert(mutex->ptm_magic == _PT_MUTEX_MAGIC);
    167 	pthread__assert(mutex->ptm_lock == __SIMPLELOCK_LOCKED);
    168 	pthread__assert((abstime->tv_sec >= 0) &&
    169 	    (abstime->tv_nsec >= 0) && (abstime->tv_nsec < 1000000000));
    170 
    171 	self = pthread__self();
    172 	PTHREADD_ADD(PTHREADD_COND_TIMEDWAIT);
    173 
    174 	/* Just hang out for a while if threads aren't running yet. */
    175 	if (__predict_false(pthread__started == 0))
    176 		return pthread_cond_wait_nothread(self, mutex, abstime);
    177 
    178 	pthread_spinlock(self, &cond->ptc_lock);
    179 #ifdef ERRORCHECK
    180 	if (cond->ptc_mutex == NULL)
    181 		cond->ptc_mutex = mutex;
    182 	else
    183 		pthread__assert(cond->ptc_mutex == mutex);
    184 #endif
    185 
    186 	wait.ptw_thread = self;
    187 	wait.ptw_cond = cond;
    188 	retval = 0;
    189 	SDPRINTF(("(cond timed wait %p) Waiting on %p until %d.%06ld\n",
    190 	    self, cond, abstime->tv_sec, abstime->tv_nsec/1000));
    191 
    192 	pthread_spinlock(self, &self->pt_statelock);
    193 	if (self->pt_cancel) {
    194 		pthread_spinunlock(self, &self->pt_statelock);
    195 		pthread_spinunlock(self, &cond->ptc_lock);
    196 		pthread_exit(PTHREAD_CANCELED);
    197 	}
    198 	pthread__alarm_add(self, &alarm, abstime, pthread_cond_wait__callback,
    199 	    &wait);
    200 	self->pt_state = PT_STATE_BLOCKED_QUEUE;
    201 	self->pt_sleepobj = cond;
    202 	self->pt_sleepq = &cond->ptc_waiters;
    203 	self->pt_sleeplock = &cond->ptc_lock;
    204 	pthread_spinunlock(self, &self->pt_statelock);
    205 
    206 	PTQ_INSERT_HEAD(&cond->ptc_waiters, self, pt_sleep);
    207 	pthread_mutex_unlock(mutex);
    208 
    209 	pthread__block(self, &cond->ptc_lock);
    210 	/* Spinlock is unlocked on return */
    211 	SDPRINTF(("(cond timed wait %p) Woke up on %p, mutex %p\n",
    212 	    self, cond));
    213 	pthread__alarm_del(self, &alarm);
    214 	if (pthread__alarm_fired(&alarm))
    215 		retval = ETIMEDOUT;
    216 	SDPRINTF(("(cond timed wait %p) %s\n",
    217 	    self, (retval == ETIMEDOUT) ? "(timed out)" : ""));
    218 	pthread_mutex_lock(mutex);
    219 	pthread__testcancel(self);
    220 
    221 	return retval;
    222 }
    223 
    224 static void
    225 pthread_cond_wait__callback(void *arg)
    226 {
    227 	struct pthread_cond__waitarg *a;
    228 	pthread_t self;
    229 
    230 	a = arg;
    231 	self = pthread__self();
    232 
    233 	/*
    234 	 * Don't dequeue and schedule the thread if it's already been
    235 	 * queued up by a signal or broadcast (but hasn't yet run as far
    236 	 * as pthread__alarm_del(), or we wouldn't be here, and hence can't
    237 	 * have become blocked on some *other* queue).
    238 	 */
    239 	pthread_spinlock(self, &a->ptw_cond->ptc_lock);
    240 	if (a->ptw_thread->pt_state == PT_STATE_BLOCKED_QUEUE) {
    241 		PTQ_REMOVE(&a->ptw_cond->ptc_waiters, a->ptw_thread, pt_sleep);
    242 #ifdef ERRORCHECK
    243 		if (PTQ_EMPTY(&a->ptw_cond->ptc_waiters))
    244 			a->ptw_cond->ptc_mutex = NULL;
    245 #endif
    246 		pthread__sched(self, a->ptw_thread);
    247 	}
    248 	pthread_spinunlock(self, &a->ptw_cond->ptc_lock);
    249 }
    250 
    251 int
    252 pthread_cond_signal(pthread_cond_t *cond)
    253 {
    254 	pthread_t self, signaled;
    255 
    256 	pthread__assert(cond->ptc_magic == _PT_COND_MAGIC);
    257 	PTHREADD_ADD(PTHREADD_COND_SIGNAL);
    258 
    259 	SDPRINTF(("(cond signal %p) Signaling %p\n",
    260 	    pthread__self(), cond));
    261 
    262 	if (!PTQ_EMPTY(&cond->ptc_waiters)) {
    263 		self = pthread__self();
    264 		pthread_spinlock(self, &cond->ptc_lock);
    265 		signaled = PTQ_FIRST(&cond->ptc_waiters);
    266 		if (signaled != NULL)
    267 			PTQ_REMOVE(&cond->ptc_waiters, signaled, pt_sleep);
    268 #ifdef ERRORCHECK
    269 		if (PTQ_EMPTY(&cond->ptc_waiters))
    270 			cond->ptc_mutex = NULL;
    271 #endif
    272 		pthread_spinunlock(self, &cond->ptc_lock);
    273 		if (signaled != NULL) {
    274 			pthread__sched(self, signaled);
    275 			PTHREADD_ADD(PTHREADD_COND_WOKEUP);
    276 		}
    277 	}
    278 
    279 	return 0;
    280 }
    281 
    282 
    283 int
    284 pthread_cond_broadcast(pthread_cond_t *cond)
    285 {
    286 	pthread_t self;
    287 	struct pthread_queue_t blockedq;
    288 
    289 	pthread__assert(cond->ptc_magic == _PT_COND_MAGIC);
    290 
    291 	PTHREADD_ADD(PTHREADD_COND_BROADCAST);
    292 	SDPRINTF(("(cond signal %p) Broadcasting %p\n",
    293 	    pthread__self(), cond));
    294 
    295 	if (!PTQ_EMPTY(&cond->ptc_waiters)) {
    296 		self = pthread__self();
    297 		pthread_spinlock(self, &cond->ptc_lock);
    298 		blockedq = cond->ptc_waiters;
    299 		PTQ_INIT(&cond->ptc_waiters);
    300 #ifdef ERRORCHECK
    301 		cond->ptc_mutex = NULL;
    302 #endif
    303 		pthread_spinunlock(self, &cond->ptc_lock);
    304 		pthread__sched_sleepers(self, &blockedq);
    305 		PTHREADD_ADD(PTHREADD_COND_WOKEUP);
    306 	}
    307 
    308 	return 0;
    309 
    310 }
    311 
    312 
    313 int
    314 pthread_condattr_init(pthread_condattr_t *attr)
    315 {
    316 
    317 	attr->ptca_magic = _PT_CONDATTR_MAGIC;
    318 
    319 	return 0;
    320 }
    321 
    322 
    323 int
    324 pthread_condattr_destroy(pthread_condattr_t *attr)
    325 {
    326 
    327 	pthread__assert(attr->ptca_magic == _PT_CONDATTR_MAGIC);
    328 
    329 	attr->ptca_magic = _PT_CONDATTR_DEAD;
    330 
    331 	return 0;
    332 }
    333 
    334 /* Utility routine to hang out for a while if threads haven't started yet. */
    335 static int
    336 pthread_cond_wait_nothread(pthread_t self, pthread_mutex_t *mutex,
    337     const struct timespec *abstime)
    338 {
    339 	struct timeval now, tv, *tvp;
    340 	int retval;
    341 
    342 	if (abstime == NULL)
    343 		tvp = NULL;
    344 	else {
    345 		tvp = &tv;
    346 		gettimeofday(&now, NULL);
    347 		TIMESPEC_TO_TIMEVAL(tvp, abstime);
    348 		timersub(tvp, &now, tvp);
    349 	}
    350 
    351 	/*
    352 	 * The libpthread select() wrapper has cancellation tests, but
    353 	 * we need to have the mutex locked when testing for
    354 	 * cancellation and unlocked while we sleep.  So, skip the
    355 	 * wrapper.
    356 	 */
    357 	pthread__testcancel(self);
    358 	pthread_mutex_unlock(mutex);
    359 	retval = _sys_select(0, NULL, NULL, NULL, tvp);
    360 	pthread_mutex_lock(mutex);
    361 	pthread__testcancel(self);
    362 
    363 	if (retval == 0)
    364 		return ETIMEDOUT;
    365 	else
    366 		return EINTR;
    367 }
    368