1 /* 2 * CDDL HEADER START 3 * 4 * The contents of this file are subject to the terms of the 5 * Common Development and Distribution License, Version 1.0 only 6 * (the "License"). You may not use this file except in compliance 7 * with the License. 8 * 9 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 10 * or http://www.opensolaris.org/os/licensing. 11 * See the License for the specific language governing permissions 12 * and limitations under the License. 13 * 14 * When distributing Covered Code, include this CDDL HEADER in each 15 * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 16 * If applicable, add the following below this CDDL HEADER, with the 17 * fields enclosed by brackets "[]" replaced with your own identifying 18 * information: Portions Copyright [yyyy] [name of copyright owner] 19 * 20 * CDDL HEADER END 21 */ 22 23 #ifdef HAVE_NBTOOL_CONFIG_H 24 #include "nbtool_config.h" 25 #endif 26 27 /* 28 * Copyright 2002 Sun Microsystems, Inc. All rights reserved. 29 * Use is subject to license terms. 30 */ 31 32 #pragma ident "%Z%%M% %I% %E% SMI" 33 34 /* 35 * This file implements a barrier, a synchronization primitive designed to allow 36 * threads to wait for each other at given points. Barriers are initialized 37 * with a given number of threads, n, using barrier_init(). When a thread calls 38 * barrier_wait(), that thread blocks until n - 1 other threads reach the 39 * barrier_wait() call using the same barrier_t. When n threads have reached 40 * the barrier, they are all awakened and sent on their way. One of the threads 41 * returns from barrier_wait() with a return code of 1; the remaining threads 42 * get a return code of 0. 43 */ 44 45 #include <errno.h> 46 #include <pthread.h> 47 #ifdef illumos 48 #include <synch.h> 49 #endif 50 #include <stdio.h> 51 52 #include "barrier.h" 53 #include "ctftools.h" 54 55 void 56 barrier_init(barrier_t *bar, int nthreads) 57 { 58 if ((errno = pthread_mutex_init(&bar->bar_lock, NULL)) != 0) 59 terminate("%s: pthread_mutex_init(bar_lock)", __func__); 60 #ifdef illumos 61 if ((errno = sema_init(&bar->bar_sem, 0, USYNC_THREAD, NULL)) != 0) 62 terminate("%s: sema_init(bar_sem)", __func__); 63 #elif defined(HAVE_DISPATCH_SEMAPHORE_CREATE) 64 if ((bar->bar_sem = dispatch_semaphore_create(0)) == NULL) 65 terminate("%s: dispatch_semaphore_create()\n", __func__); 66 #else 67 if (sem_init(&bar->bar_sem, 0, 0) == -1) 68 terminate("%s: sem_init(bar_sem)", __func__); 69 #endif 70 71 bar->bar_numin = 0; 72 bar->bar_nthr = nthreads; 73 } 74 75 int 76 barrier_wait(barrier_t *bar) 77 { 78 #if defined(HAVE_DISPATCH_SEMAPHORE_CREATE) 79 long error; 80 #endif 81 if ((errno = pthread_mutex_lock(&bar->bar_lock)) != 0) 82 terminate("%s: pthread_mutex_lock(bar_lock)", __func__); 83 84 if (++bar->bar_numin < bar->bar_nthr) { 85 if ((errno = pthread_mutex_unlock(&bar->bar_lock)) != 0) 86 terminate("%s: pthread_mutex_unlock(bar_lock)", 87 __func__); 88 #ifdef illumos 89 if ((errno = sema_wait(&bar->bar_sem)) != 0) 90 terminate("%s: sema_wait(bar_sem)", __func__); 91 #elif defined(HAVE_DISPATCH_SEMAPHORE_CREATE) 92 if ((error = dispatch_semaphore_wait(bar->bar_sem, DISPATCH_TIME_FOREVER)) != 0) 93 terminate("%s: dispatch_semaphore_wait(bar_sem) = %ld\n", 94 __func__, error); 95 #else 96 if (sem_wait(&bar->bar_sem) == -1) 97 terminate("%s: sem_wait(bar_sem)", __func__); 98 #endif 99 100 return (0); 101 102 } else { 103 int i; 104 105 /* reset for next use */ 106 bar->bar_numin = 0; 107 for (i = 1; i < bar->bar_nthr; i++) { 108 #ifdef illumos 109 if ((errno = sema_post(&bar->bar_sem)) != 0) 110 terminate("%s: sema_post(bar_sem)", __func__); 111 #elif defined(HAVE_DISPATCH_SEMAPHORE_CREATE) 112 /* return value doesn't matter */ 113 dispatch_semaphore_signal(bar->bar_sem); 114 #else 115 if (sem_post(&bar->bar_sem) == -1) 116 terminate("%s: sem_post(bar_sem)", __func__); 117 #endif 118 } 119 if ((errno = pthread_mutex_unlock(&bar->bar_lock)) != 0) 120 terminate("%s: pthread_mutex_unlock(bar_lock)", 121 __func__); 122 123 return (1); 124 } 125 } 126