Home | History | Annotate | Line # | Download | only in libpthread
pthread_barrier.c revision 1.6.18.1
      1  1.6.18.1  wrstuden /*	$NetBSD: pthread_barrier.c,v 1.6.18.1 2007/09/10 05:24:52 wrstuden Exp $	*/
      2       1.2   thorpej 
      3       1.2   thorpej /*-
      4       1.2   thorpej  * Copyright (c) 2001, 2003 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.2   thorpej  * by Nathan J. Williams, and by Jason R. Thorpe.
      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  * 3. All advertising materials mentioning features or use of this software
     19       1.2   thorpej  *    must display the following acknowledgement:
     20       1.2   thorpej  *        This product includes software developed by the NetBSD
     21       1.2   thorpej  *        Foundation, Inc. and its contributors.
     22       1.2   thorpej  * 4. Neither the name of The NetBSD Foundation nor the names of its
     23       1.2   thorpej  *    contributors may be used to endorse or promote products derived
     24       1.2   thorpej  *    from this software without specific prior written permission.
     25       1.2   thorpej  *
     26       1.2   thorpej  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     27       1.2   thorpej  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     28       1.2   thorpej  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     29       1.2   thorpej  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     30       1.2   thorpej  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     31       1.2   thorpej  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     32       1.2   thorpej  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     33       1.2   thorpej  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     34       1.2   thorpej  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     35       1.2   thorpej  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     36       1.2   thorpej  * POSSIBILITY OF SUCH DAMAGE.
     37       1.2   thorpej  */
     38       1.6     lukem 
     39       1.6     lukem #include <sys/cdefs.h>
     40  1.6.18.1  wrstuden __RCSID("$NetBSD: pthread_barrier.c,v 1.6.18.1 2007/09/10 05:24:52 wrstuden Exp $");
     41       1.2   thorpej 
     42       1.2   thorpej #include <errno.h>
     43       1.2   thorpej #include <sys/cdefs.h>
     44       1.2   thorpej 
     45       1.2   thorpej #include "pthread.h"
     46       1.2   thorpej #include "pthread_int.h"
     47       1.2   thorpej 
     48       1.2   thorpej #undef PTHREAD_BARRIER_DEBUG
     49       1.2   thorpej 
     50       1.2   thorpej #ifdef PTHREAD_BARRIER_DEBUG
     51       1.2   thorpej #define SDPRINTF(x) DPRINTF(x)
     52       1.2   thorpej #else
     53       1.2   thorpej #define SDPRINTF(x)
     54       1.2   thorpej #endif
     55       1.2   thorpej 
     56       1.2   thorpej int
     57       1.2   thorpej pthread_barrier_init(pthread_barrier_t *barrier,
     58       1.2   thorpej     const pthread_barrierattr_t *attr, unsigned int count)
     59       1.2   thorpej {
     60       1.2   thorpej 	pthread_t self;
     61       1.2   thorpej 
     62       1.2   thorpej #ifdef ERRORCHECK
     63       1.2   thorpej 	if ((barrier == NULL) ||
     64       1.2   thorpej 	    (attr && (attr->ptba_magic != _PT_BARRIERATTR_MAGIC)))
     65       1.2   thorpej 		return EINVAL;
     66       1.2   thorpej #endif
     67       1.2   thorpej 
     68       1.2   thorpej 	if (count == 0)
     69       1.2   thorpej 		return EINVAL;
     70       1.2   thorpej 
     71       1.2   thorpej 	self = pthread__self();
     72       1.2   thorpej 
     73       1.2   thorpej 	if (barrier->ptb_magic == _PT_BARRIER_MAGIC) {
     74       1.2   thorpej 		/*
     75       1.2   thorpej 		 * We're simply reinitializing the barrier to a
     76       1.2   thorpej 		 * new count.
     77       1.2   thorpej 		 */
     78       1.2   thorpej 		pthread_spinlock(self, &barrier->ptb_lock);
     79       1.2   thorpej 
     80       1.2   thorpej 		if (barrier->ptb_magic != _PT_BARRIER_MAGIC) {
     81       1.2   thorpej 			pthread_spinunlock(self, &barrier->ptb_lock);
     82       1.2   thorpej 			return EINVAL;
     83       1.2   thorpej 		}
     84       1.2   thorpej 
     85       1.2   thorpej 		if (!PTQ_EMPTY(&barrier->ptb_waiters)) {
     86       1.2   thorpej 			pthread_spinunlock(self, &barrier->ptb_lock);
     87       1.2   thorpej 			return EBUSY;
     88       1.2   thorpej 		}
     89       1.2   thorpej 
     90       1.2   thorpej 		barrier->ptb_initcount = count;
     91       1.2   thorpej 		barrier->ptb_curcount = 0;
     92       1.3   nathanw 		barrier->ptb_generation = 0;
     93       1.2   thorpej 
     94       1.2   thorpej 		pthread_spinunlock(self, &barrier->ptb_lock);
     95       1.2   thorpej 
     96       1.2   thorpej 		return 0;
     97       1.2   thorpej 	}
     98       1.2   thorpej 
     99       1.2   thorpej 	barrier->ptb_magic = _PT_BARRIER_MAGIC;
    100       1.2   thorpej 	pthread_lockinit(&barrier->ptb_lock);
    101       1.2   thorpej 	PTQ_INIT(&barrier->ptb_waiters);
    102       1.2   thorpej 	barrier->ptb_initcount = count;
    103       1.2   thorpej 	barrier->ptb_curcount = 0;
    104       1.3   nathanw 	barrier->ptb_generation = 0;
    105       1.2   thorpej 
    106       1.2   thorpej 	return 0;
    107       1.2   thorpej }
    108       1.2   thorpej 
    109       1.2   thorpej 
    110       1.2   thorpej int
    111       1.2   thorpej pthread_barrier_destroy(pthread_barrier_t *barrier)
    112       1.2   thorpej {
    113       1.2   thorpej 	pthread_t self;
    114       1.2   thorpej 
    115       1.2   thorpej #ifdef ERRORCHECK
    116       1.2   thorpej 	if ((barrier == NULL) || (barrier->ptb_magic != _PT_BARRIER_MAGIC))
    117       1.2   thorpej 		return EINVAL;
    118       1.2   thorpej #endif
    119       1.2   thorpej 
    120       1.2   thorpej 	self = pthread__self();
    121       1.2   thorpej 
    122       1.2   thorpej 	pthread_spinlock(self, &barrier->ptb_lock);
    123       1.2   thorpej 
    124       1.2   thorpej 	if (barrier->ptb_magic != _PT_BARRIER_MAGIC) {
    125       1.2   thorpej 		pthread_spinunlock(self, &barrier->ptb_lock);
    126       1.2   thorpej 		return EINVAL;
    127       1.2   thorpej 	}
    128       1.2   thorpej 
    129       1.2   thorpej 	if (!PTQ_EMPTY(&barrier->ptb_waiters)) {
    130       1.2   thorpej 		pthread_spinunlock(self, &barrier->ptb_lock);
    131       1.2   thorpej 		return EBUSY;
    132       1.2   thorpej 	}
    133       1.2   thorpej 
    134       1.2   thorpej 	barrier->ptb_magic = _PT_BARRIER_DEAD;
    135       1.2   thorpej 
    136       1.2   thorpej 	pthread_spinunlock(self, &barrier->ptb_lock);
    137       1.2   thorpej 
    138       1.2   thorpej 	return 0;
    139       1.2   thorpej }
    140       1.2   thorpej 
    141       1.2   thorpej 
    142       1.2   thorpej int
    143       1.2   thorpej pthread_barrier_wait(pthread_barrier_t *barrier)
    144       1.2   thorpej {
    145       1.2   thorpej 	pthread_t self;
    146       1.3   nathanw 	unsigned int gen;
    147       1.2   thorpej 
    148       1.2   thorpej #ifdef ERRORCHECK
    149       1.2   thorpej 	if ((barrier == NULL) || (barrier->ptb_magic != _PT_BARRIER_MAGIC))
    150       1.2   thorpej 		return EINVAL;
    151       1.2   thorpej #endif
    152       1.2   thorpej 	self = pthread__self();
    153       1.2   thorpej 
    154       1.2   thorpej 	pthread_spinlock(self, &barrier->ptb_lock);
    155       1.2   thorpej 
    156       1.2   thorpej 	/*
    157       1.2   thorpej 	 * A single arbitrary thread is supposed to return
    158       1.2   thorpej 	 * PTHREAD_BARRIER_SERIAL_THREAD, and everone else
    159       1.2   thorpej 	 * is supposed to return 0.  Since pthread_barrier_wait()
    160       1.2   thorpej 	 * is not a cancellation point, this is trivial; we
    161       1.2   thorpej 	 * simply elect that the thread that causes the barrier
    162       1.2   thorpej 	 * to be satisfied gets the special return value.  Note
    163       1.2   thorpej 	 * that this final thread does not actually need to block,
    164       1.2   thorpej 	 * but instead is responsible for waking everyone else up.
    165       1.2   thorpej 	 */
    166       1.2   thorpej 	if (barrier->ptb_curcount + 1 == barrier->ptb_initcount) {
    167       1.2   thorpej 		struct pthread_queue_t blockedq;
    168       1.2   thorpej 
    169       1.2   thorpej 		SDPRINTF(("(barrier wait %p) Satisfied %p\n",
    170       1.2   thorpej 		    self, barrier));
    171       1.2   thorpej 
    172       1.2   thorpej 		blockedq = barrier->ptb_waiters;
    173       1.2   thorpej 		PTQ_INIT(&barrier->ptb_waiters);
    174       1.2   thorpej 		barrier->ptb_curcount = 0;
    175       1.3   nathanw 		barrier->ptb_generation++;
    176       1.2   thorpej 
    177       1.4   nathanw 		pthread__sched_sleepers(self, &blockedq);
    178       1.2   thorpej 
    179       1.2   thorpej 		pthread_spinunlock(self, &barrier->ptb_lock);
    180       1.2   thorpej 
    181       1.2   thorpej 		return PTHREAD_BARRIER_SERIAL_THREAD;
    182       1.2   thorpej 	}
    183       1.2   thorpej 
    184       1.3   nathanw 	barrier->ptb_curcount++;
    185       1.3   nathanw 	gen = barrier->ptb_generation;
    186       1.3   nathanw 	while (gen == barrier->ptb_generation) {
    187       1.3   nathanw 		SDPRINTF(("(barrier wait %p) Waiting on %p\n",
    188       1.3   nathanw 		    self, barrier));
    189       1.2   thorpej 
    190       1.3   nathanw 		pthread_spinlock(self, &self->pt_statelock);
    191       1.2   thorpej 
    192  1.6.18.1  wrstuden 		if (pthread_check_defsig(self)) {
    193  1.6.18.1  wrstuden 			pthread_spinunlock(self, &self->pt_statelock);
    194  1.6.18.1  wrstuden 			pthread_spinunlock(self, &barrier->ptb_lock);
    195  1.6.18.1  wrstuden 			SDPRINTF(("(barrier wait %p) Caught defsig on %p\n",
    196  1.6.18.1  wrstuden 			    self, barrier));
    197  1.6.18.1  wrstuden 			pthread__signal_deferred(self, self);
    198  1.6.18.1  wrstuden 			pthread_spinlock(self, &barrier->ptb_lock);
    199  1.6.18.1  wrstuden 			continue;
    200  1.6.18.1  wrstuden 		}
    201  1.6.18.1  wrstuden 
    202       1.3   nathanw 		self->pt_state = PT_STATE_BLOCKED_QUEUE;
    203       1.3   nathanw 		self->pt_sleepobj = barrier;
    204       1.3   nathanw 		self->pt_sleepq = &barrier->ptb_waiters;
    205       1.3   nathanw 		self->pt_sleeplock = &barrier->ptb_lock;
    206       1.3   nathanw 
    207       1.3   nathanw 		pthread_spinunlock(self, &self->pt_statelock);
    208       1.2   thorpej 
    209       1.3   nathanw 		PTQ_INSERT_TAIL(&barrier->ptb_waiters, self, pt_sleep);
    210       1.2   thorpej 
    211       1.3   nathanw 		pthread__block(self, &barrier->ptb_lock);
    212       1.3   nathanw 		SDPRINTF(("(barrier wait %p) Woke up on %p\n",
    213       1.3   nathanw 		    self, barrier));
    214       1.3   nathanw 		/* Spinlock is unlocked on return */
    215       1.3   nathanw 		pthread_spinlock(self, &barrier->ptb_lock);
    216       1.3   nathanw 	}
    217       1.3   nathanw 	pthread_spinunlock(self, &barrier->ptb_lock);
    218       1.2   thorpej 
    219       1.2   thorpej 	return 0;
    220       1.2   thorpej }
    221       1.2   thorpej 
    222       1.2   thorpej 
    223       1.2   thorpej int
    224       1.2   thorpej pthread_barrierattr_init(pthread_barrierattr_t *attr)
    225       1.2   thorpej {
    226       1.2   thorpej 
    227       1.2   thorpej #ifdef ERRORCHECK
    228       1.2   thorpej 	if (attr == NULL)
    229       1.2   thorpej 		return EINVAL;
    230       1.2   thorpej #endif
    231       1.2   thorpej 
    232       1.2   thorpej 	attr->ptba_magic = _PT_BARRIERATTR_MAGIC;
    233       1.2   thorpej 
    234       1.2   thorpej 	return 0;
    235       1.2   thorpej }
    236       1.2   thorpej 
    237       1.2   thorpej 
    238       1.2   thorpej int
    239       1.2   thorpej pthread_barrierattr_destroy(pthread_barrierattr_t *attr)
    240       1.2   thorpej {
    241       1.2   thorpej 
    242       1.2   thorpej #ifdef ERRORCHECK
    243       1.2   thorpej 	if ((attr == NULL) ||
    244       1.2   thorpej 	    (attr->ptba_magic != _PT_BARRIERATTR_MAGIC))
    245       1.2   thorpej 		return EINVAL;
    246       1.2   thorpej #endif
    247       1.2   thorpej 
    248       1.2   thorpej 	attr->ptba_magic = _PT_BARRIERATTR_DEAD;
    249       1.2   thorpej 
    250       1.2   thorpej 	return 0;
    251       1.2   thorpej }
    252