bar.h revision 1.1.1.2 1 1.1.1.2 mrg /* Copyright (C) 2005-2013 Free Software Foundation, Inc.
2 1.1 mrg Contributed by Richard Henderson <rth (at) redhat.com>.
3 1.1 mrg
4 1.1 mrg This file is part of the GNU OpenMP Library (libgomp).
5 1.1 mrg
6 1.1 mrg Libgomp is free software; you can redistribute it and/or modify it
7 1.1 mrg under the terms of the GNU General Public License as published by
8 1.1 mrg the Free Software Foundation; either version 3, or (at your option)
9 1.1 mrg any later version.
10 1.1 mrg
11 1.1 mrg Libgomp is distributed in the hope that it will be useful, but WITHOUT ANY
12 1.1 mrg WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
13 1.1 mrg FOR A PARTICULAR PURPOSE. See the GNU General Public License for
14 1.1 mrg more details.
15 1.1 mrg
16 1.1 mrg Under Section 7 of GPL version 3, you are granted additional
17 1.1 mrg permissions described in the GCC Runtime Library Exception, version
18 1.1 mrg 3.1, as published by the Free Software Foundation.
19 1.1 mrg
20 1.1 mrg You should have received a copy of the GNU General Public License and
21 1.1 mrg a copy of the GCC Runtime Library Exception along with this program;
22 1.1 mrg see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
23 1.1 mrg <http://www.gnu.org/licenses/>. */
24 1.1 mrg
25 1.1 mrg /* This is a Linux specific implementation of a barrier synchronization
26 1.1 mrg mechanism for libgomp. This type is private to the library. This
27 1.1 mrg implementation uses atomic instructions and the futex syscall. */
28 1.1 mrg
29 1.1 mrg #ifndef GOMP_BARRIER_H
30 1.1 mrg #define GOMP_BARRIER_H 1
31 1.1 mrg
32 1.1 mrg #include "mutex.h"
33 1.1 mrg
34 1.1 mrg typedef struct
35 1.1 mrg {
36 1.1 mrg /* Make sure total/generation is in a mostly read cacheline, while
37 1.1 mrg awaited in a separate cacheline. */
38 1.1 mrg unsigned total __attribute__((aligned (64)));
39 1.1 mrg unsigned generation;
40 1.1 mrg unsigned awaited __attribute__((aligned (64)));
41 1.1 mrg } gomp_barrier_t;
42 1.1 mrg typedef unsigned int gomp_barrier_state_t;
43 1.1 mrg
44 1.1 mrg static inline void gomp_barrier_init (gomp_barrier_t *bar, unsigned count)
45 1.1 mrg {
46 1.1 mrg bar->total = count;
47 1.1 mrg bar->awaited = count;
48 1.1 mrg bar->generation = 0;
49 1.1 mrg }
50 1.1 mrg
51 1.1 mrg static inline void gomp_barrier_reinit (gomp_barrier_t *bar, unsigned count)
52 1.1 mrg {
53 1.1.1.2 mrg __atomic_add_fetch (&bar->awaited, count - bar->total, MEMMODEL_ACQ_REL);
54 1.1 mrg bar->total = count;
55 1.1 mrg }
56 1.1 mrg
57 1.1 mrg static inline void gomp_barrier_destroy (gomp_barrier_t *bar)
58 1.1 mrg {
59 1.1 mrg }
60 1.1 mrg
61 1.1 mrg extern void gomp_barrier_wait (gomp_barrier_t *);
62 1.1 mrg extern void gomp_barrier_wait_last (gomp_barrier_t *);
63 1.1 mrg extern void gomp_barrier_wait_end (gomp_barrier_t *, gomp_barrier_state_t);
64 1.1 mrg extern void gomp_team_barrier_wait (gomp_barrier_t *);
65 1.1 mrg extern void gomp_team_barrier_wait_end (gomp_barrier_t *,
66 1.1 mrg gomp_barrier_state_t);
67 1.1 mrg extern void gomp_team_barrier_wake (gomp_barrier_t *, int);
68 1.1 mrg
69 1.1 mrg static inline gomp_barrier_state_t
70 1.1 mrg gomp_barrier_wait_start (gomp_barrier_t *bar)
71 1.1 mrg {
72 1.1.1.2 mrg unsigned int ret = __atomic_load_n (&bar->generation, MEMMODEL_ACQUIRE) & ~3;
73 1.1.1.2 mrg /* A memory barrier is needed before exiting from the various forms
74 1.1.1.2 mrg of gomp_barrier_wait, to satisfy OpenMP API version 3.1 section
75 1.1.1.2 mrg 2.8.6 flush Construct, which says there is an implicit flush during
76 1.1.1.2 mrg a barrier region. This is a convenient place to add the barrier,
77 1.1.1.2 mrg so we use MEMMODEL_ACQ_REL here rather than MEMMODEL_ACQUIRE. */
78 1.1.1.2 mrg ret += __atomic_add_fetch (&bar->awaited, -1, MEMMODEL_ACQ_REL) == 0;
79 1.1 mrg return ret;
80 1.1 mrg }
81 1.1 mrg
82 1.1 mrg static inline bool
83 1.1 mrg gomp_barrier_last_thread (gomp_barrier_state_t state)
84 1.1 mrg {
85 1.1 mrg return state & 1;
86 1.1 mrg }
87 1.1 mrg
88 1.1 mrg /* All the inlines below must be called with team->task_lock
89 1.1 mrg held. */
90 1.1 mrg
91 1.1 mrg static inline void
92 1.1 mrg gomp_team_barrier_set_task_pending (gomp_barrier_t *bar)
93 1.1 mrg {
94 1.1 mrg bar->generation |= 1;
95 1.1 mrg }
96 1.1 mrg
97 1.1 mrg static inline void
98 1.1 mrg gomp_team_barrier_clear_task_pending (gomp_barrier_t *bar)
99 1.1 mrg {
100 1.1 mrg bar->generation &= ~1;
101 1.1 mrg }
102 1.1 mrg
103 1.1 mrg static inline void
104 1.1 mrg gomp_team_barrier_set_waiting_for_tasks (gomp_barrier_t *bar)
105 1.1 mrg {
106 1.1 mrg bar->generation |= 2;
107 1.1 mrg }
108 1.1 mrg
109 1.1 mrg static inline bool
110 1.1 mrg gomp_team_barrier_waiting_for_tasks (gomp_barrier_t *bar)
111 1.1 mrg {
112 1.1 mrg return (bar->generation & 2) != 0;
113 1.1 mrg }
114 1.1 mrg
115 1.1 mrg static inline void
116 1.1 mrg gomp_team_barrier_done (gomp_barrier_t *bar, gomp_barrier_state_t state)
117 1.1 mrg {
118 1.1 mrg bar->generation = (state & ~3) + 4;
119 1.1 mrg }
120 1.1 mrg
121 1.1 mrg #endif /* GOMP_BARRIER_H */
122