/* * Copyright (c) 2003, Daniel M. Eischen * All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * 2. Neither the name of the author nor the names of any co-contributors * may be used to endorse or promote products derived from this software * without specific prior written permission. * * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. */ #include __FBSDID("$FreeBSD$"); #include "namespace.h" #include #include #include #include "un-namespace.h" #include "thr_private.h" #define PTHREAD_BARRIER_SERIAL_THREAD -1 struct pthread_barrierattr { int pshared; }; typedef struct pthread_barrierattr *pthread_barrierattr_t; struct pthread_barrier { pthread_mutex_t b_mutex; pthread_cond_t b_cv; int b_shared; unsigned b_count; int b_waiting; }; typedef struct pthread_barrier *pthread_barrier_t; /* * Prototypes */ int pthread_barrierattr_destroy(pthread_barrierattr_t *); int pthread_barrierattr_init(pthread_barrierattr_t *); int pthread_barrierattr_getpshared( const pthread_barrierattr_t *restrict, int *); int pthread_barrierattr_setpshared(pthread_barrierattr_t *, int); int pthread_barrier_destroy(pthread_barrier_t *); int pthread_barrier_init(pthread_barrier_t *, const pthread_barrierattr_t *, unsigned); int pthread_barrier_wait(pthread_barrier_t *); __weak_reference(_pthread_barrierattr_destroy, pthread_barrierattr_destroy); __weak_reference(_pthread_barrierattr_init, pthread_barrierattr_init); __weak_reference(_pthread_barrierattr_getpshared, pthread_barrierattr_getpshared); __weak_reference(_pthread_barrierattr_setpshared, pthread_barrier_attrsetpshared); __weak_reference(_pthread_barrier_init, pthread_barrier_init); __weak_reference(_pthread_barrier_destroy, pthread_barrier_destroy); __weak_reference(_pthread_barrier_wait, pthread_barrier_wait); int _pthread_barrierattr_init(pthread_barrierattr_t *battr) { struct pthread_barrierattr *attr; int ret; if (battr == NULL) ret = EINVAL; else if ((attr = malloc(sizeof(struct pthread_barrierattr))) == NULL) { *battr = NULL; ret = EINVAL; } else { attr->pshared = 0; *battr = attr; ret = 0; } return (ret); } int _pthread_barrierattr_destroy(pthread_barrierattr_t *battr) { int ret; if ((battr == NULL) || (*battr == NULL)) ret = EINVAL; else { free(*battr); *battr = NULL; ret = 0; } return (ret); } int _pthread_barrierattr_getpshared(pthread_barrierattr_t *battr) { if ((battr == NULL) || (*battr == NULL)) return (EINVAL); else return ((*battr)->pshared); } int _pthread_barrierattr_setpshared(pthread_barrierattr_t *battr, int shared) { if ((battr == NULL) || (*battr == NULL)) return (EINVAL); else { (*battr)->pshared = shared; return (0); } } int _pthread_barrier_init(pthread_barrier_t *barrier, pthread_barrierattr_t *attr, unsigned count) { struct pthread_barrier *bar; int ret; if (count == 0) { ret = EINVAL; *barrier = NULL; } else if ((bar = (pthread_barrier_t) malloc(sizeof(struct pthread_barrier))) == NULL) { ret = ENOMEM; *barrier = NULL; } else if ((ret = _pthread_mutex_init(&bar->b_mutex, NULL)) != 0) { free(bar); *barrier = NULL; } else if ((ret = (_pthread_cond_init(&bar->b_cv, NULL)) != 0)) { _pthread_mutex_destroy(&bar->b_mutex); free(bar); *barrier = NULL; } else { bar->b_count = count; if ((attr != NULL) && (*attr != NULL)) bar->b_shared = (*attr)->pshared; else bar->b_shared = 0; bar->b_waiting = 0; *barrier = bar; ret = 0; } /* Return the completion status: */ return (ret); } int _pthread_barrier_destroy(pthread_barrier_t *barrier) { struct pthread_barrier *bar; int ret = 0; if (barrier == NULL || *barrier == NULL) ret = EINVAL; else { bar = NULL; /* Lock the barrier structure: */ _pthread_mutex_lock(&(*barrier)->b_mutex); if ((*barrier)->b_waiting > 0) ret = EBUSY; else { bar = *barrier; *barrier = NULL; ret = 0; } _pthread_mutex_unlock(&(*barrier)->b_mutex); if (bar != NULL) { _pthread_mutex_destroy(&(*barrier)->b_mutex); _pthread_cond_destroy(&(*barrier)->b_cv); free(bar); } } return (ret); } int _pthread_barrier_wait(pthread_barrier_t *barrier) { int ret; if ((barrier == NULL) || (*barrier == NULL)) ret = EINVAL; else if ((ret = _pthread_mutex_lock(&(*barrier)->b_mutex)) == 0) { (*barrier)->b_waiting++; if ((*barrier)->b_waiting >= (*barrier)->b_count) { (*barrier)->b_waiting = 0; ret = _pthread_cond_broadcast(&(*barrier)->b_cv); if (ret = 0) ret = PTHREAD_BARRIER_SERIAL_THREAD; } else ret = _pthread_cond_wait(&(*barrier)->b_cv, &(*barrier)->b_mutex); (void)_pthread_mutex_unlock(&(*barrier)->b_mutex); } return (ret); }