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 * Copyright 2002 Sun Microsystems, Inc. All rights reserved. 24 * Use is subject to license terms. 25 */ 26 27#pragma ident "@(#)barrier.c 1.2 05/06/08 SMI" 28 29/* 30 * This file implements a barrier, a synchronization primitive designed to allow 31 * threads to wait for each other at given points. Barriers are initialized 32 * with a given number of threads, n, using barrier_init(). When a thread calls 33 * barrier_wait(), that thread blocks until n - 1 other threads reach the 34 * barrier_wait() call using the same barrier_t. When n threads have reached 35 * the barrier, they are all awakened and sent on their way. One of the threads 36 * returns from barrier_wait() with a return code of 1; the remaining threads 37 * get a return code of 0. 38 */ 39#if !defined(__APPLE__) 40#include <pthread.h> 41#include <synch.h> 42#include <stdio.h> 43 44#include "barrier.h" 45 46void 47barrier_init(barrier_t *bar, int nthreads) 48{ 49 pthread_mutex_init(&bar->bar_lock, NULL); 50 sema_init(&bar->bar_sem, 0, USYNC_THREAD, NULL); 51 52 bar->bar_numin = 0; 53 bar->bar_nthr = nthreads; 54} 55 56int 57barrier_wait(barrier_t *bar) 58{ 59 pthread_mutex_lock(&bar->bar_lock); 60 61 if (++bar->bar_numin < bar->bar_nthr) { 62 pthread_mutex_unlock(&bar->bar_lock); 63 sema_wait(&bar->bar_sem); 64 65 return (0); 66 67 } else { 68 int i; 69 70 /* reset for next use */ 71 bar->bar_numin = 0; 72 for (i = 1; i < bar->bar_nthr; i++) 73 sema_post(&bar->bar_sem); 74 pthread_mutex_unlock(&bar->bar_lock); 75 76 return (1); 77 } 78} 79#else 80#include <pthread.h> 81#include <semaphore.h> 82#include <stdio.h> 83 84#include "barrier.h" 85 86void 87barrier_init(barrier_t *bar, int nthreads) 88{ 89 pthread_mutex_init(&bar->bar_lock, NULL); 90 bar->bar_sem = sem_open("ctfmerge_barrier", O_CREAT | O_EXCL); 91 92 bar->bar_numin = 0; 93 bar->bar_nthr = nthreads; 94} 95 96int 97barrier_wait(barrier_t *bar) 98{ 99 pthread_mutex_lock(&bar->bar_lock); 100 101 if (++bar->bar_numin < bar->bar_nthr) { 102 pthread_mutex_unlock(&bar->bar_lock); 103 sem_wait(bar->bar_sem); 104 105 return (0); 106 107 } else { 108 int i; 109 110 /* reset for next use */ 111 bar->bar_numin = 0; 112 for (i = 1; i < bar->bar_nthr; i++) 113 sem_post(bar->bar_sem); 114 pthread_mutex_unlock(&bar->bar_lock); 115 116 return (1); 117 } 118} 119#endif /* __APPLE__ */ 120