Home | History | Annotate | Line # | Download | only in libpthread
pthread_barrier.c revision 1.9
      1 /*	$NetBSD: pthread_barrier.c,v 1.9 2007/01/17 23:24:22 hubertf Exp $	*/
      2 
      3 /*-
      4  * Copyright (c) 2001, 2003 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, and by Jason R. Thorpe.
      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_barrier.c,v 1.9 2007/01/17 23:24:22 hubertf Exp $");
     41 
     42 #include <errno.h>
     43 
     44 #include "pthread.h"
     45 #include "pthread_int.h"
     46 
     47 #undef PTHREAD_BARRIER_DEBUG
     48 
     49 #ifdef PTHREAD_BARRIER_DEBUG
     50 #define SDPRINTF(x) DPRINTF(x)
     51 #else
     52 #define SDPRINTF(x)
     53 #endif
     54 
     55 int
     56 pthread_barrier_init(pthread_barrier_t *barrier,
     57     const pthread_barrierattr_t *attr, unsigned int count)
     58 {
     59 	pthread_t self;
     60 
     61 #ifdef ERRORCHECK
     62 	if ((barrier == NULL) ||
     63 	    (attr && (attr->ptba_magic != _PT_BARRIERATTR_MAGIC)))
     64 		return EINVAL;
     65 #endif
     66 
     67 	if (count == 0)
     68 		return EINVAL;
     69 
     70 	self = pthread__self();
     71 
     72 	if (barrier->ptb_magic == _PT_BARRIER_MAGIC) {
     73 		/*
     74 		 * We're simply reinitializing the barrier to a
     75 		 * new count.
     76 		 */
     77 		pthread_spinlock(self, &barrier->ptb_lock);
     78 
     79 		if (barrier->ptb_magic != _PT_BARRIER_MAGIC) {
     80 			pthread_spinunlock(self, &barrier->ptb_lock);
     81 			return EINVAL;
     82 		}
     83 
     84 		if (!PTQ_EMPTY(&barrier->ptb_waiters)) {
     85 			pthread_spinunlock(self, &barrier->ptb_lock);
     86 			return EBUSY;
     87 		}
     88 
     89 		barrier->ptb_initcount = count;
     90 		barrier->ptb_curcount = 0;
     91 		barrier->ptb_generation = 0;
     92 
     93 		pthread_spinunlock(self, &barrier->ptb_lock);
     94 
     95 		return 0;
     96 	}
     97 
     98 	barrier->ptb_magic = _PT_BARRIER_MAGIC;
     99 	pthread_lockinit(&barrier->ptb_lock);
    100 	PTQ_INIT(&barrier->ptb_waiters);
    101 	barrier->ptb_initcount = count;
    102 	barrier->ptb_curcount = 0;
    103 	barrier->ptb_generation = 0;
    104 
    105 	return 0;
    106 }
    107 
    108 
    109 int
    110 pthread_barrier_destroy(pthread_barrier_t *barrier)
    111 {
    112 	pthread_t self;
    113 
    114 #ifdef ERRORCHECK
    115 	if ((barrier == NULL) || (barrier->ptb_magic != _PT_BARRIER_MAGIC))
    116 		return EINVAL;
    117 #endif
    118 
    119 	self = pthread__self();
    120 
    121 	pthread_spinlock(self, &barrier->ptb_lock);
    122 
    123 	if (barrier->ptb_magic != _PT_BARRIER_MAGIC) {
    124 		pthread_spinunlock(self, &barrier->ptb_lock);
    125 		return EINVAL;
    126 	}
    127 
    128 	if (!PTQ_EMPTY(&barrier->ptb_waiters)) {
    129 		pthread_spinunlock(self, &barrier->ptb_lock);
    130 		return EBUSY;
    131 	}
    132 
    133 	barrier->ptb_magic = _PT_BARRIER_DEAD;
    134 
    135 	pthread_spinunlock(self, &barrier->ptb_lock);
    136 
    137 	return 0;
    138 }
    139 
    140 
    141 int
    142 pthread_barrier_wait(pthread_barrier_t *barrier)
    143 {
    144 	pthread_t self;
    145 	unsigned int gen;
    146 
    147 #ifdef ERRORCHECK
    148 	if ((barrier == NULL) || (barrier->ptb_magic != _PT_BARRIER_MAGIC))
    149 		return EINVAL;
    150 #endif
    151 	self = pthread__self();
    152 
    153 	pthread_spinlock(self, &barrier->ptb_lock);
    154 
    155 	/*
    156 	 * A single arbitrary thread is supposed to return
    157 	 * PTHREAD_BARRIER_SERIAL_THREAD, and everone else
    158 	 * is supposed to return 0.  Since pthread_barrier_wait()
    159 	 * is not a cancellation point, this is trivial; we
    160 	 * simply elect that the thread that causes the barrier
    161 	 * to be satisfied gets the special return value.  Note
    162 	 * that this final thread does not actually need to block,
    163 	 * but instead is responsible for waking everyone else up.
    164 	 */
    165 	if (barrier->ptb_curcount + 1 == barrier->ptb_initcount) {
    166 #ifdef PTHREAD_SA
    167 		struct pthread_queue_t blockedq;
    168 
    169 		SDPRINTF(("(barrier wait %p) Satisfied %p\n",
    170 		    self, barrier));
    171 
    172 		blockedq = barrier->ptb_waiters;
    173 		PTQ_INIT(&barrier->ptb_waiters);
    174 		barrier->ptb_curcount = 0;
    175 		barrier->ptb_generation++;
    176 
    177 		pthread__sched_sleepers(self, &blockedq);
    178 
    179 		pthread_spinunlock(self, &barrier->ptb_lock);
    180 #else
    181 		SDPRINTF(("(barrier wait %p) Satisfied %p\n",
    182 		    self, barrier));
    183 
    184 		barrier->ptb_generation++;
    185 		pthread__unpark_all(self, &barrier->ptb_lock, barrier,
    186 		    &barrier->ptb_waiters);
    187 #endif
    188 		return PTHREAD_BARRIER_SERIAL_THREAD;
    189 	}
    190 
    191 	barrier->ptb_curcount++;
    192 	gen = barrier->ptb_generation;
    193 	while (gen == barrier->ptb_generation) {
    194 		SDPRINTF(("(barrier wait %p) Waiting on %p\n",
    195 		    self, barrier));
    196 
    197 #ifdef PTHREAD_SA
    198 		pthread_spinlock(self, &self->pt_statelock);
    199 
    200 		self->pt_state = PT_STATE_BLOCKED_QUEUE;
    201 		self->pt_sleepobj = barrier;
    202 		self->pt_sleepq = &barrier->ptb_waiters;
    203 		self->pt_sleeplock = &barrier->ptb_lock;
    204 
    205 		pthread_spinunlock(self, &self->pt_statelock);
    206 
    207 		PTQ_INSERT_TAIL(&barrier->ptb_waiters, self, pt_sleep);
    208 
    209 		pthread__block(self, &barrier->ptb_lock);
    210 		/* Spinlock is unlocked on return */
    211 		pthread_spinlock(self, &barrier->ptb_lock);
    212 #else	/* PTHREAD_SA */
    213 		(void)pthread__park(self, &barrier->ptb_lock, barrier,
    214 		    &barrier->ptb_waiters, NULL, 1, 0);
    215 #endif	/* PTHREAD_SA */
    216 		SDPRINTF(("(barrier wait %p) Woke up on %p\n",
    217 		    self, barrier));
    218 	}
    219 	pthread_spinunlock(self, &barrier->ptb_lock);
    220 
    221 	return 0;
    222 }
    223 
    224 
    225 int
    226 pthread_barrierattr_init(pthread_barrierattr_t *attr)
    227 {
    228 
    229 #ifdef ERRORCHECK
    230 	if (attr == NULL)
    231 		return EINVAL;
    232 #endif
    233 
    234 	attr->ptba_magic = _PT_BARRIERATTR_MAGIC;
    235 
    236 	return 0;
    237 }
    238 
    239 
    240 int
    241 pthread_barrierattr_destroy(pthread_barrierattr_t *attr)
    242 {
    243 
    244 #ifdef ERRORCHECK
    245 	if ((attr == NULL) ||
    246 	    (attr->ptba_magic != _PT_BARRIERATTR_MAGIC))
    247 		return EINVAL;
    248 #endif
    249 
    250 	attr->ptba_magic = _PT_BARRIERATTR_DEAD;
    251 
    252 	return 0;
    253 }
    254