11673e404SJohn Birrell /* 21673e404SJohn Birrell * CDDL HEADER START 31673e404SJohn Birrell * 41673e404SJohn Birrell * The contents of this file are subject to the terms of the 51673e404SJohn Birrell * Common Development and Distribution License, Version 1.0 only 61673e404SJohn Birrell * (the "License"). You may not use this file except in compliance 71673e404SJohn Birrell * with the License. 81673e404SJohn Birrell * 91673e404SJohn Birrell * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 101673e404SJohn Birrell * or http://www.opensolaris.org/os/licensing. 111673e404SJohn Birrell * See the License for the specific language governing permissions 121673e404SJohn Birrell * and limitations under the License. 131673e404SJohn Birrell * 141673e404SJohn Birrell * When distributing Covered Code, include this CDDL HEADER in each 151673e404SJohn Birrell * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 161673e404SJohn Birrell * If applicable, add the following below this CDDL HEADER, with the 171673e404SJohn Birrell * fields enclosed by brackets "[]" replaced with your own identifying 181673e404SJohn Birrell * information: Portions Copyright [yyyy] [name of copyright owner] 191673e404SJohn Birrell * 201673e404SJohn Birrell * CDDL HEADER END 211673e404SJohn Birrell */ 221673e404SJohn Birrell /* 231673e404SJohn Birrell * Copyright 2002 Sun Microsystems, Inc. All rights reserved. 241673e404SJohn Birrell * Use is subject to license terms. 251673e404SJohn Birrell */ 261673e404SJohn Birrell 271673e404SJohn Birrell #pragma ident "%Z%%M% %I% %E% SMI" 281673e404SJohn Birrell 291673e404SJohn Birrell /* 301673e404SJohn Birrell * This file implements a barrier, a synchronization primitive designed to allow 311673e404SJohn Birrell * threads to wait for each other at given points. Barriers are initialized 321673e404SJohn Birrell * with a given number of threads, n, using barrier_init(). When a thread calls 331673e404SJohn Birrell * barrier_wait(), that thread blocks until n - 1 other threads reach the 341673e404SJohn Birrell * barrier_wait() call using the same barrier_t. When n threads have reached 351673e404SJohn Birrell * the barrier, they are all awakened and sent on their way. One of the threads 361673e404SJohn Birrell * returns from barrier_wait() with a return code of 1; the remaining threads 371673e404SJohn Birrell * get a return code of 0. 381673e404SJohn Birrell */ 391673e404SJohn Birrell 401673e404SJohn Birrell #include <pthread.h> 41*bc96366cSSteven Hartland #ifdef illumos 421673e404SJohn Birrell #include <synch.h> 434cc75139SJohn Birrell #endif 441673e404SJohn Birrell #include <stdio.h> 451673e404SJohn Birrell 461673e404SJohn Birrell #include "barrier.h" 471673e404SJohn Birrell 481673e404SJohn Birrell void 491673e404SJohn Birrell barrier_init(barrier_t *bar, int nthreads) 501673e404SJohn Birrell { 511673e404SJohn Birrell pthread_mutex_init(&bar->bar_lock, NULL); 52*bc96366cSSteven Hartland #ifdef illumos 531673e404SJohn Birrell sema_init(&bar->bar_sem, 0, USYNC_THREAD, NULL); 544cc75139SJohn Birrell #else 554cc75139SJohn Birrell sem_init(&bar->bar_sem, 0, 0); 564cc75139SJohn Birrell #endif 571673e404SJohn Birrell 581673e404SJohn Birrell bar->bar_numin = 0; 591673e404SJohn Birrell bar->bar_nthr = nthreads; 601673e404SJohn Birrell } 611673e404SJohn Birrell 621673e404SJohn Birrell int 631673e404SJohn Birrell barrier_wait(barrier_t *bar) 641673e404SJohn Birrell { 651673e404SJohn Birrell pthread_mutex_lock(&bar->bar_lock); 661673e404SJohn Birrell 671673e404SJohn Birrell if (++bar->bar_numin < bar->bar_nthr) { 681673e404SJohn Birrell pthread_mutex_unlock(&bar->bar_lock); 69*bc96366cSSteven Hartland #ifdef illumos 701673e404SJohn Birrell sema_wait(&bar->bar_sem); 714cc75139SJohn Birrell #else 724cc75139SJohn Birrell sem_wait(&bar->bar_sem); 734cc75139SJohn Birrell #endif 741673e404SJohn Birrell 751673e404SJohn Birrell return (0); 761673e404SJohn Birrell 771673e404SJohn Birrell } else { 781673e404SJohn Birrell int i; 791673e404SJohn Birrell 801673e404SJohn Birrell /* reset for next use */ 811673e404SJohn Birrell bar->bar_numin = 0; 821673e404SJohn Birrell for (i = 1; i < bar->bar_nthr; i++) 83*bc96366cSSteven Hartland #ifdef illumos 841673e404SJohn Birrell sema_post(&bar->bar_sem); 854cc75139SJohn Birrell #else 864cc75139SJohn Birrell sem_post(&bar->bar_sem); 874cc75139SJohn Birrell #endif 881673e404SJohn Birrell pthread_mutex_unlock(&bar->bar_lock); 891673e404SJohn Birrell 901673e404SJohn Birrell return (1); 911673e404SJohn Birrell } 921673e404SJohn Birrell } 93