1 /* $NetBSD: pthread_barrier.c,v 1.22 2020/05/16 22:53:37 ad Exp $ */ 2 3 /*- 4 * Copyright (c) 2001, 2003, 2006, 2007, 2009, 2020 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, by Jason R. Thorpe, and by Andrew Doran. 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 * 19 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 20 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 21 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 22 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 23 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 24 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 27 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 29 * POSSIBILITY OF SUCH DAMAGE. 30 */ 31 32 #include <sys/cdefs.h> 33 __RCSID("$NetBSD: pthread_barrier.c,v 1.22 2020/05/16 22:53:37 ad Exp $"); 34 35 #include <errno.h> 36 37 #include "pthread.h" 38 #include "pthread_int.h" 39 40 int 41 pthread_barrier_init(pthread_barrier_t *barrier, 42 const pthread_barrierattr_t *attr, unsigned int count) 43 { 44 45 pthread__error(EINVAL, "Invalid barrier attribute", 46 attr == NULL || attr->ptba_magic == _PT_BARRIERATTR_MAGIC); 47 if (count == 0) 48 return EINVAL; 49 50 barrier->ptb_magic = _PT_BARRIER_MAGIC; 51 PTQ_INIT(&barrier->ptb_waiters); 52 barrier->ptb_initcount = count; 53 barrier->ptb_curcount = 0; 54 barrier->ptb_generation = 0; 55 return 0; 56 } 57 58 int 59 pthread_barrier_destroy(pthread_barrier_t *barrier) 60 { 61 62 pthread__error(EINVAL, "Invalid barrier", 63 barrier->ptb_magic == _PT_BARRIER_MAGIC); 64 if (barrier->ptb_curcount != 0) 65 return EBUSY; 66 67 barrier->ptb_magic = _PT_BARRIER_DEAD; 68 69 return 0; 70 } 71 72 int 73 pthread_barrier_wait(pthread_barrier_t *barrier) 74 { 75 pthread_mutex_t *interlock; 76 pthread_t self; 77 unsigned int gen; 78 79 pthread__error(EINVAL, "Invalid barrier", 80 barrier->ptb_magic == _PT_BARRIER_MAGIC); 81 82 /* 83 * A single arbitrary thread is supposed to return 84 * PTHREAD_BARRIER_SERIAL_THREAD, and everone else 85 * is supposed to return 0. Since pthread_barrier_wait() 86 * is not a cancellation point, this is trivial; we 87 * simply elect that the thread that causes the barrier 88 * to be satisfied gets the special return value. Note 89 * that this final thread does not actually need to block, 90 * but instead is responsible for waking everyone else up. 91 */ 92 self = pthread__self(); 93 interlock = pthread__hashlock(barrier); 94 pthread_mutex_lock(interlock); 95 if (barrier->ptb_curcount + 1 == barrier->ptb_initcount) { 96 barrier->ptb_generation++; 97 barrier->ptb_curcount = 0; 98 pthread__unpark_all(&barrier->ptb_waiters, self, 99 interlock); 100 pthread_mutex_unlock(interlock); 101 return PTHREAD_BARRIER_SERIAL_THREAD; 102 } 103 barrier->ptb_curcount++; 104 gen = barrier->ptb_generation; 105 for (;;) { 106 PTQ_INSERT_TAIL(&barrier->ptb_waiters, self, pt_sleep); 107 self->pt_sleepobj = &barrier->ptb_waiters; 108 (void)pthread__park(self, interlock, &barrier->ptb_waiters, 109 NULL, 0); 110 if (__predict_true(gen != barrier->ptb_generation)) { 111 break; 112 } 113 pthread_mutex_lock(interlock); 114 if (gen != barrier->ptb_generation) { 115 pthread_mutex_unlock(interlock); 116 break; 117 } 118 } 119 120 return 0; 121 } 122 123 #ifdef _PTHREAD_PSHARED 124 int 125 pthread_barrierattr_getpshared(const pthread_barrierattr_t * __restrict attr, 126 int * __restrict pshared) 127 { 128 129 pthread__error(EINVAL, "Invalid barrier attribute", 130 attr->ptba_magic == _PT_BARRIERATTR_MAGIC); 131 132 *pshared = PTHREAD_PROCESS_PRIVATE; 133 return 0; 134 } 135 136 int 137 pthread_barrierattr_setpshared(pthread_barrierattr_t *attr, int pshared) 138 { 139 140 pthread__error(EINVAL, "Invalid barrier attribute", 141 attr->ptba_magic == _PT_BARRIERATTR_MAGIC); 142 143 switch(pshared) { 144 case PTHREAD_PROCESS_PRIVATE: 145 return 0; 146 case PTHREAD_PROCESS_SHARED: 147 return ENOSYS; 148 } 149 return EINVAL; 150 } 151 #endif 152 153 int 154 pthread_barrierattr_init(pthread_barrierattr_t *attr) 155 { 156 157 attr->ptba_magic = _PT_BARRIERATTR_MAGIC; 158 return 0; 159 } 160 161 int 162 pthread_barrierattr_destroy(pthread_barrierattr_t *attr) 163 { 164 165 pthread__error(EINVAL, "Invalid barrier attribute", 166 attr->ptba_magic == _PT_BARRIERATTR_MAGIC); 167 attr->ptba_magic = _PT_BARRIERATTR_DEAD; 168 return 0; 169 } 170