Home | History | Annotate | Line # | Download | only in libpthread
pthread_barrier.c revision 1.16.6.1
      1  1.16.6.1     yamt /*	$NetBSD: pthread_barrier.c,v 1.16.6.1 2008/05/18 12:30:39 yamt Exp $	*/
      2       1.2  thorpej 
      3       1.2  thorpej /*-
      4      1.10       ad  * Copyright (c) 2001, 2003, 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.11       ad  * by Nathan J. Williams, by Jason R. Thorpe, and by 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.6    lukem 
     32       1.6    lukem #include <sys/cdefs.h>
     33  1.16.6.1     yamt __RCSID("$NetBSD: pthread_barrier.c,v 1.16.6.1 2008/05/18 12:30:39 yamt Exp $");
     34       1.2  thorpej 
     35       1.2  thorpej #include <errno.h>
     36       1.2  thorpej 
     37       1.2  thorpej #include "pthread.h"
     38       1.2  thorpej #include "pthread_int.h"
     39       1.2  thorpej 
     40       1.2  thorpej int
     41       1.2  thorpej pthread_barrier_init(pthread_barrier_t *barrier,
     42       1.2  thorpej     const pthread_barrierattr_t *attr, unsigned int count)
     43       1.2  thorpej {
     44      1.15       ad 	pthread_t self;
     45       1.2  thorpej 
     46       1.2  thorpej #ifdef ERRORCHECK
     47       1.2  thorpej 	if ((barrier == NULL) ||
     48       1.2  thorpej 	    (attr && (attr->ptba_magic != _PT_BARRIERATTR_MAGIC)))
     49       1.2  thorpej 		return EINVAL;
     50       1.2  thorpej #endif
     51       1.2  thorpej 
     52       1.2  thorpej 	if (count == 0)
     53       1.2  thorpej 		return EINVAL;
     54       1.2  thorpej 
     55       1.2  thorpej 	if (barrier->ptb_magic == _PT_BARRIER_MAGIC) {
     56      1.15       ad 		self = pthread__self();
     57      1.15       ad 
     58       1.2  thorpej 		/*
     59       1.2  thorpej 		 * We're simply reinitializing the barrier to a
     60       1.2  thorpej 		 * new count.
     61       1.2  thorpej 		 */
     62      1.15       ad 		pthread__spinlock(self, &barrier->ptb_lock);
     63       1.2  thorpej 
     64       1.2  thorpej 		if (barrier->ptb_magic != _PT_BARRIER_MAGIC) {
     65      1.15       ad 			pthread__spinunlock(self, &barrier->ptb_lock);
     66       1.2  thorpej 			return EINVAL;
     67       1.2  thorpej 		}
     68       1.2  thorpej 
     69       1.2  thorpej 		if (!PTQ_EMPTY(&barrier->ptb_waiters)) {
     70      1.15       ad 			pthread__spinunlock(self, &barrier->ptb_lock);
     71       1.2  thorpej 			return EBUSY;
     72       1.2  thorpej 		}
     73       1.2  thorpej 
     74       1.2  thorpej 		barrier->ptb_initcount = count;
     75       1.2  thorpej 		barrier->ptb_curcount = 0;
     76       1.3  nathanw 		barrier->ptb_generation = 0;
     77       1.2  thorpej 
     78      1.15       ad 		pthread__spinunlock(self, &barrier->ptb_lock);
     79       1.2  thorpej 
     80       1.2  thorpej 		return 0;
     81       1.2  thorpej 	}
     82       1.2  thorpej 
     83       1.2  thorpej 	barrier->ptb_magic = _PT_BARRIER_MAGIC;
     84       1.2  thorpej 	pthread_lockinit(&barrier->ptb_lock);
     85       1.2  thorpej 	PTQ_INIT(&barrier->ptb_waiters);
     86       1.2  thorpej 	barrier->ptb_initcount = count;
     87       1.2  thorpej 	barrier->ptb_curcount = 0;
     88       1.3  nathanw 	barrier->ptb_generation = 0;
     89       1.2  thorpej 
     90       1.2  thorpej 	return 0;
     91       1.2  thorpej }
     92       1.2  thorpej 
     93       1.2  thorpej 
     94       1.2  thorpej int
     95       1.2  thorpej pthread_barrier_destroy(pthread_barrier_t *barrier)
     96       1.2  thorpej {
     97      1.15       ad 	pthread_t self;
     98       1.2  thorpej 
     99       1.2  thorpej #ifdef ERRORCHECK
    100       1.2  thorpej 	if ((barrier == NULL) || (barrier->ptb_magic != _PT_BARRIER_MAGIC))
    101       1.2  thorpej 		return EINVAL;
    102       1.2  thorpej #endif
    103       1.2  thorpej 
    104      1.15       ad 	self = pthread__self();
    105      1.15       ad 	pthread__spinlock(self, &barrier->ptb_lock);
    106       1.2  thorpej 
    107       1.2  thorpej 	if (barrier->ptb_magic != _PT_BARRIER_MAGIC) {
    108      1.15       ad 		pthread__spinunlock(self, &barrier->ptb_lock);
    109       1.2  thorpej 		return EINVAL;
    110       1.2  thorpej 	}
    111       1.2  thorpej 
    112       1.2  thorpej 	if (!PTQ_EMPTY(&barrier->ptb_waiters)) {
    113      1.15       ad 		pthread__spinunlock(self, &barrier->ptb_lock);
    114       1.2  thorpej 		return EBUSY;
    115       1.2  thorpej 	}
    116       1.2  thorpej 
    117       1.2  thorpej 	barrier->ptb_magic = _PT_BARRIER_DEAD;
    118       1.2  thorpej 
    119      1.15       ad 	pthread__spinunlock(self, &barrier->ptb_lock);
    120       1.2  thorpej 
    121       1.2  thorpej 	return 0;
    122       1.2  thorpej }
    123       1.2  thorpej 
    124       1.2  thorpej 
    125       1.2  thorpej int
    126       1.2  thorpej pthread_barrier_wait(pthread_barrier_t *barrier)
    127       1.2  thorpej {
    128       1.2  thorpej 	pthread_t self;
    129       1.3  nathanw 	unsigned int gen;
    130       1.2  thorpej 
    131       1.2  thorpej #ifdef ERRORCHECK
    132       1.2  thorpej 	if ((barrier == NULL) || (barrier->ptb_magic != _PT_BARRIER_MAGIC))
    133       1.2  thorpej 		return EINVAL;
    134       1.2  thorpej #endif
    135       1.2  thorpej 	self = pthread__self();
    136       1.2  thorpej 
    137      1.15       ad 	pthread__spinlock(self, &barrier->ptb_lock);
    138       1.2  thorpej 
    139       1.2  thorpej 	/*
    140       1.2  thorpej 	 * A single arbitrary thread is supposed to return
    141       1.2  thorpej 	 * PTHREAD_BARRIER_SERIAL_THREAD, and everone else
    142       1.2  thorpej 	 * is supposed to return 0.  Since pthread_barrier_wait()
    143       1.2  thorpej 	 * is not a cancellation point, this is trivial; we
    144       1.2  thorpej 	 * simply elect that the thread that causes the barrier
    145       1.2  thorpej 	 * to be satisfied gets the special return value.  Note
    146       1.2  thorpej 	 * that this final thread does not actually need to block,
    147       1.2  thorpej 	 * but instead is responsible for waking everyone else up.
    148       1.2  thorpej 	 */
    149       1.2  thorpej 	if (barrier->ptb_curcount + 1 == barrier->ptb_initcount) {
    150       1.8       ad 		barrier->ptb_generation++;
    151      1.11       ad 		pthread__unpark_all(self, &barrier->ptb_lock,
    152       1.8       ad 		    &barrier->ptb_waiters);
    153       1.2  thorpej 		return PTHREAD_BARRIER_SERIAL_THREAD;
    154       1.2  thorpej 	}
    155       1.2  thorpej 
    156       1.3  nathanw 	barrier->ptb_curcount++;
    157       1.3  nathanw 	gen = barrier->ptb_generation;
    158       1.3  nathanw 	while (gen == barrier->ptb_generation) {
    159      1.11       ad 		PTQ_INSERT_TAIL(&barrier->ptb_waiters, self, pt_sleep);
    160      1.11       ad 		self->pt_sleeponq = 1;
    161      1.11       ad 		self->pt_sleepobj = &barrier->ptb_waiters;
    162      1.15       ad 		pthread__spinunlock(self, &barrier->ptb_lock);
    163      1.11       ad 		(void)pthread__park(self, &barrier->ptb_lock,
    164      1.12       ad 		    &barrier->ptb_waiters, NULL, 0,
    165      1.12       ad 		    &barrier->ptb_waiters);
    166      1.15       ad 		pthread__spinlock(self, &barrier->ptb_lock);
    167       1.3  nathanw 	}
    168      1.15       ad 	pthread__spinunlock(self, &barrier->ptb_lock);
    169       1.2  thorpej 
    170       1.2  thorpej 	return 0;
    171       1.2  thorpej }
    172       1.2  thorpej 
    173       1.2  thorpej 
    174       1.2  thorpej int
    175       1.2  thorpej pthread_barrierattr_init(pthread_barrierattr_t *attr)
    176       1.2  thorpej {
    177       1.2  thorpej 
    178       1.2  thorpej #ifdef ERRORCHECK
    179       1.2  thorpej 	if (attr == NULL)
    180       1.2  thorpej 		return EINVAL;
    181       1.2  thorpej #endif
    182       1.2  thorpej 
    183       1.2  thorpej 	attr->ptba_magic = _PT_BARRIERATTR_MAGIC;
    184       1.2  thorpej 
    185       1.2  thorpej 	return 0;
    186       1.2  thorpej }
    187       1.2  thorpej 
    188       1.2  thorpej 
    189       1.2  thorpej int
    190       1.2  thorpej pthread_barrierattr_destroy(pthread_barrierattr_t *attr)
    191       1.2  thorpej {
    192       1.2  thorpej 
    193       1.2  thorpej #ifdef ERRORCHECK
    194       1.2  thorpej 	if ((attr == NULL) ||
    195       1.2  thorpej 	    (attr->ptba_magic != _PT_BARRIERATTR_MAGIC))
    196       1.2  thorpej 		return EINVAL;
    197       1.2  thorpej #endif
    198       1.2  thorpej 
    199       1.2  thorpej 	attr->ptba_magic = _PT_BARRIERATTR_DEAD;
    200       1.2  thorpej 
    201       1.2  thorpej 	return 0;
    202       1.2  thorpej }
    203