1169695Skan/* Copyright (C) 2005 Free Software Foundation, Inc. 2169695Skan Contributed by Richard Henderson <rth@redhat.com>. 3169695Skan 4169695Skan This file is part of the GNU OpenMP Library (libgomp). 5169695Skan 6169695Skan Libgomp is free software; you can redistribute it and/or modify it 7169695Skan under the terms of the GNU Lesser General Public License as published by 8169695Skan the Free Software Foundation; either version 2.1 of the License, or 9169695Skan (at your option) any later version. 10169695Skan 11169695Skan Libgomp is distributed in the hope that it will be useful, but WITHOUT ANY 12169695Skan WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS 13169695Skan FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for 14169695Skan more details. 15169695Skan 16169695Skan You should have received a copy of the GNU Lesser General Public License 17169695Skan along with libgomp; see the file COPYING.LIB. If not, write to the 18169695Skan Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, 19169695Skan MA 02110-1301, USA. */ 20169695Skan 21169695Skan/* As a special exception, if you link this library with other files, some 22169695Skan of which are compiled with GCC, to produce an executable, this library 23169695Skan does not by itself cause the resulting executable to be covered by the 24169695Skan GNU General Public License. This exception does not however invalidate 25169695Skan any other reasons why the executable file might be covered by the GNU 26169695Skan General Public License. */ 27169695Skan 28169695Skan/* This is the default implementation of a barrier synchronization mechanism 29169695Skan for libgomp. This type is private to the library. Note that we rely on 30169695Skan being able to adjust the barrier count while threads are blocked, so the 31169695Skan POSIX pthread_barrier_t won't work. */ 32169695Skan 33169695Skan#include "libgomp.h" 34169695Skan 35169695Skan 36169695Skanvoid 37169695Skangomp_barrier_init (gomp_barrier_t *bar, unsigned count) 38169695Skan{ 39169695Skan gomp_mutex_init (&bar->mutex1); 40169695Skan#ifndef HAVE_SYNC_BUILTINS 41169695Skan gomp_mutex_init (&bar->mutex2); 42169695Skan#endif 43169695Skan gomp_sem_init (&bar->sem1, 0); 44169695Skan gomp_sem_init (&bar->sem2, 0); 45169695Skan bar->total = count; 46169695Skan bar->arrived = 0; 47169695Skan} 48169695Skan 49169695Skanvoid 50169695Skangomp_barrier_destroy (gomp_barrier_t *bar) 51169695Skan{ 52169695Skan /* Before destroying, make sure all threads have left the barrier. */ 53169695Skan gomp_mutex_lock (&bar->mutex1); 54169695Skan gomp_mutex_unlock (&bar->mutex1); 55169695Skan 56169695Skan gomp_mutex_destroy (&bar->mutex1); 57169695Skan#ifndef HAVE_SYNC_BUILTINS 58169695Skan gomp_mutex_destroy (&bar->mutex2); 59169695Skan#endif 60169695Skan gomp_sem_destroy (&bar->sem1); 61169695Skan gomp_sem_destroy (&bar->sem2); 62169695Skan} 63169695Skan 64169695Skanvoid 65169695Skangomp_barrier_reinit (gomp_barrier_t *bar, unsigned count) 66169695Skan{ 67169695Skan gomp_mutex_lock (&bar->mutex1); 68169695Skan bar->total = count; 69169695Skan gomp_mutex_unlock (&bar->mutex1); 70169695Skan} 71169695Skan 72169695Skanvoid 73169695Skangomp_barrier_wait_end (gomp_barrier_t *bar, bool last) 74169695Skan{ 75169695Skan unsigned int n; 76169695Skan 77169695Skan if (last) 78169695Skan { 79169695Skan n = --bar->arrived; 80169695Skan if (n > 0) 81169695Skan { 82169695Skan do 83169695Skan gomp_sem_post (&bar->sem1); 84169695Skan while (--n != 0); 85169695Skan gomp_sem_wait (&bar->sem2); 86169695Skan } 87169695Skan gomp_mutex_unlock (&bar->mutex1); 88169695Skan } 89169695Skan else 90169695Skan { 91169695Skan gomp_mutex_unlock (&bar->mutex1); 92169695Skan gomp_sem_wait (&bar->sem1); 93169695Skan 94169695Skan#ifdef HAVE_SYNC_BUILTINS 95169695Skan n = __sync_add_and_fetch (&bar->arrived, -1); 96169695Skan#else 97169695Skan gomp_mutex_lock (&bar->mutex2); 98169695Skan n = --bar->arrived; 99169695Skan gomp_mutex_unlock (&bar->mutex2); 100169695Skan#endif 101169695Skan 102169695Skan if (n == 0) 103169695Skan gomp_sem_post (&bar->sem2); 104169695Skan } 105169695Skan} 106169695Skan 107169695Skanvoid 108169695Skangomp_barrier_wait (gomp_barrier_t *barrier) 109169695Skan{ 110169695Skan gomp_barrier_wait_end (barrier, gomp_barrier_wait_start (barrier)); 111169695Skan} 112