1 /* Copyright (C) 2005, 2008 Free Software Foundation, Inc.
2 Contributed by Richard Henderson <rth@redhat.com>.
4 This file is part of the GNU OpenMP Library (libgomp).
6 Libgomp is free software; you can redistribute it and/or modify it
7 under the terms of the GNU Lesser General Public License as published by
8 the Free Software Foundation; either version 2.1 of the License, or
9 (at your option) any later version.
11 Libgomp is distributed in the hope that it will be useful, but WITHOUT ANY
12 WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
13 FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for
16 You should have received a copy of the GNU Lesser General Public License
17 along with libgomp; see the file COPYING.LIB. If not, write to the
18 Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
19 MA 02110-1301, USA. */
21 /* As a special exception, if you link this library with other files, some
22 of which are compiled with GCC, to produce an executable, this library
23 does not by itself cause the resulting executable to be covered by the
24 GNU General Public License. This exception does not however invalidate
25 any other reasons why the executable file might be covered by the GNU
26 General Public License. */
28 /* This is a Linux specific implementation of a barrier synchronization
29 mechanism for libgomp. This type is private to the library. This
30 implementation uses atomic instructions and the futex syscall. */
32 #ifndef GOMP_BARRIER_H
33 #define GOMP_BARRIER_H 1
39 /* Make sure total/generation is in a mostly read cacheline, while
40 awaited in a separate cacheline. */
41 unsigned total __attribute__((aligned (64)));
43 unsigned awaited __attribute__((aligned (64)));
45 typedef unsigned int gomp_barrier_state_t;
47 static inline void gomp_barrier_init (gomp_barrier_t *bar, unsigned count)
54 static inline void gomp_barrier_reinit (gomp_barrier_t *bar, unsigned count)
56 __sync_fetch_and_add (&bar->awaited, count - bar->total);
60 static inline void gomp_barrier_destroy (gomp_barrier_t *bar)
64 extern void gomp_barrier_wait (gomp_barrier_t *);
65 extern void gomp_barrier_wait_last (gomp_barrier_t *);
66 extern void gomp_barrier_wait_end (gomp_barrier_t *, gomp_barrier_state_t);
67 extern void gomp_team_barrier_wait (gomp_barrier_t *);
68 extern void gomp_team_barrier_wait_end (gomp_barrier_t *,
69 gomp_barrier_state_t);
70 extern void gomp_team_barrier_wake (gomp_barrier_t *, int);
72 static inline gomp_barrier_state_t
73 gomp_barrier_wait_start (gomp_barrier_t *bar)
75 unsigned int ret = bar->generation & ~3;
76 /* Do we need any barrier here or is __sync_add_and_fetch acting
77 as the needed LoadLoad barrier already? */
78 ret += __sync_add_and_fetch (&bar->awaited, -1) == 0;
83 gomp_barrier_last_thread (gomp_barrier_state_t state)
88 /* All the inlines below must be called with team->task_lock
92 gomp_team_barrier_set_task_pending (gomp_barrier_t *bar)
98 gomp_team_barrier_clear_task_pending (gomp_barrier_t *bar)
100 bar->generation &= ~1;
104 gomp_team_barrier_set_waiting_for_tasks (gomp_barrier_t *bar)
106 bar->generation |= 2;
110 gomp_team_barrier_waiting_for_tasks (gomp_barrier_t *bar)
112 return (bar->generation & 2) != 0;
116 gomp_team_barrier_done (gomp_barrier_t *bar, gomp_barrier_state_t state)
118 bar->generation = (state & ~3) + 4;
121 #endif /* GOMP_BARRIER_H */