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 a Linux specific implementation of a barrier synchronization
29169695Skan   mechanism for libgomp.  This type is private to the library.  This
30169695Skan   implementation uses atomic instructions and the futex syscall.  */
31169695Skan
32169695Skan#ifndef GOMP_BARRIER_H
33169695Skan#define GOMP_BARRIER_H 1
34169695Skan
35169695Skan#include "mutex.h"
36169695Skan
37169695Skantypedef struct
38169695Skan{
39169695Skan  gomp_mutex_t mutex;
40169695Skan  unsigned total;
41169695Skan  unsigned arrived;
42169695Skan  int generation;
43169695Skan} gomp_barrier_t;
44169695Skan
45169695Skanstatic inline void gomp_barrier_init (gomp_barrier_t *bar, unsigned count)
46169695Skan{
47169695Skan  gomp_mutex_init (&bar->mutex);
48169695Skan  bar->total = count;
49169695Skan  bar->arrived = 0;
50169695Skan  bar->generation = 0;
51169695Skan}
52169695Skan
53169695Skanstatic inline void gomp_barrier_reinit (gomp_barrier_t *bar, unsigned count)
54169695Skan{
55169695Skan  gomp_mutex_lock (&bar->mutex);
56169695Skan  bar->total = count;
57169695Skan  gomp_mutex_unlock (&bar->mutex);
58169695Skan}
59169695Skan
60169695Skanstatic inline void gomp_barrier_destroy (gomp_barrier_t *bar)
61169695Skan{
62169695Skan  /* Before destroying, make sure all threads have left the barrier.  */
63169695Skan  gomp_mutex_lock (&bar->mutex);
64169695Skan}
65169695Skan
66169695Skanextern void gomp_barrier_wait (gomp_barrier_t *);
67169695Skanextern void gomp_barrier_wait_end (gomp_barrier_t *, bool);
68169695Skan
69169695Skanstatic inline bool gomp_barrier_wait_start (gomp_barrier_t *bar)
70169695Skan{
71169695Skan  gomp_mutex_lock (&bar->mutex);
72169695Skan  return ++bar->arrived == bar->total;
73169695Skan}
74169695Skan
75169695Skan#endif /* GOMP_BARRIER_H */
76