annotate libgomp/config/rtems/bar.h @ 150:26042f4007d5 current

fix examples
author Shinji KONO <kono@ie.u-ryukyu.ac.jp>
date Sat, 23 May 2020 07:51:47 +0900
parents 1830386684a0
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
145
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
1 /* Copyright (C) 2005-2020 Free Software Foundation, Inc.
111
kono
parents:
diff changeset
2 Contributed by Sebastian Huber <sebastian.huber@embedded-brains.de>.
kono
parents:
diff changeset
3
kono
parents:
diff changeset
4 This file is part of the GNU OpenMP Library (libgomp).
kono
parents:
diff changeset
5
kono
parents:
diff changeset
6 Libgomp is free software; you can redistribute it and/or modify it
kono
parents:
diff changeset
7 under the terms of the GNU General Public License as published by
kono
parents:
diff changeset
8 the Free Software Foundation; either version 3, or (at your option)
kono
parents:
diff changeset
9 any later version.
kono
parents:
diff changeset
10
kono
parents:
diff changeset
11 Libgomp is distributed in the hope that it will be useful, but WITHOUT ANY
kono
parents:
diff changeset
12 WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
kono
parents:
diff changeset
13 FOR A PARTICULAR PURPOSE. See the GNU General Public License for
kono
parents:
diff changeset
14 more details.
kono
parents:
diff changeset
15
kono
parents:
diff changeset
16 Under Section 7 of GPL version 3, you are granted additional
kono
parents:
diff changeset
17 permissions described in the GCC Runtime Library Exception, version
kono
parents:
diff changeset
18 3.1, as published by the Free Software Foundation.
kono
parents:
diff changeset
19
kono
parents:
diff changeset
20 You should have received a copy of the GNU General Public License and
kono
parents:
diff changeset
21 a copy of the GCC Runtime Library Exception along with this program;
kono
parents:
diff changeset
22 see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
kono
parents:
diff changeset
23 <http://www.gnu.org/licenses/>. */
kono
parents:
diff changeset
24
kono
parents:
diff changeset
25 /* This is the RTEMS implementation of a barrier synchronization
kono
parents:
diff changeset
26 mechanism for libgomp. It is identical to the Linux implementation, except
kono
parents:
diff changeset
27 that the futex API is slightly different. This type is private to the
kono
parents:
diff changeset
28 library. */
kono
parents:
diff changeset
29
kono
parents:
diff changeset
30 #ifndef GOMP_BARRIER_H
kono
parents:
diff changeset
31 #define GOMP_BARRIER_H 1
kono
parents:
diff changeset
32
kono
parents:
diff changeset
33 #include <sys/lock.h>
kono
parents:
diff changeset
34
kono
parents:
diff changeset
35 typedef struct
kono
parents:
diff changeset
36 {
kono
parents:
diff changeset
37 /* Make sure total/generation is in a mostly read cacheline, while
kono
parents:
diff changeset
38 awaited in a separate cacheline. */
kono
parents:
diff changeset
39 unsigned total __attribute__((aligned (64)));
kono
parents:
diff changeset
40 unsigned generation;
kono
parents:
diff changeset
41 struct _Futex_Control futex;
kono
parents:
diff changeset
42 unsigned awaited __attribute__((aligned (64)));
kono
parents:
diff changeset
43 unsigned awaited_final;
kono
parents:
diff changeset
44 } gomp_barrier_t;
kono
parents:
diff changeset
45
kono
parents:
diff changeset
46 typedef unsigned int gomp_barrier_state_t;
kono
parents:
diff changeset
47
kono
parents:
diff changeset
48 /* The generation field contains a counter in the high bits, with a few
kono
parents:
diff changeset
49 low bits dedicated to flags. Note that TASK_PENDING and WAS_LAST can
kono
parents:
diff changeset
50 share space because WAS_LAST is never stored back to generation. */
kono
parents:
diff changeset
51 #define BAR_TASK_PENDING 1
kono
parents:
diff changeset
52 #define BAR_WAS_LAST 1
kono
parents:
diff changeset
53 #define BAR_WAITING_FOR_TASK 2
kono
parents:
diff changeset
54 #define BAR_CANCELLED 4
kono
parents:
diff changeset
55 #define BAR_INCR 8
kono
parents:
diff changeset
56
kono
parents:
diff changeset
57 static inline void gomp_barrier_init (gomp_barrier_t *bar, unsigned count)
kono
parents:
diff changeset
58 {
kono
parents:
diff changeset
59 bar->total = count;
kono
parents:
diff changeset
60 bar->awaited = count;
kono
parents:
diff changeset
61 bar->awaited_final = count;
kono
parents:
diff changeset
62 bar->generation = 0;
kono
parents:
diff changeset
63 _Futex_Initialize (&bar->futex);
kono
parents:
diff changeset
64 }
kono
parents:
diff changeset
65
kono
parents:
diff changeset
66 static inline void gomp_barrier_reinit (gomp_barrier_t *bar, unsigned count)
kono
parents:
diff changeset
67 {
kono
parents:
diff changeset
68 __atomic_add_fetch (&bar->awaited, count - bar->total, MEMMODEL_ACQ_REL);
kono
parents:
diff changeset
69 bar->total = count;
kono
parents:
diff changeset
70 }
kono
parents:
diff changeset
71
kono
parents:
diff changeset
72 static inline void gomp_barrier_destroy (gomp_barrier_t *bar)
kono
parents:
diff changeset
73 {
kono
parents:
diff changeset
74 }
kono
parents:
diff changeset
75
kono
parents:
diff changeset
76 extern void gomp_barrier_wait (gomp_barrier_t *);
kono
parents:
diff changeset
77 extern void gomp_barrier_wait_last (gomp_barrier_t *);
kono
parents:
diff changeset
78 extern void gomp_barrier_wait_end (gomp_barrier_t *, gomp_barrier_state_t);
kono
parents:
diff changeset
79 extern void gomp_team_barrier_wait (gomp_barrier_t *);
kono
parents:
diff changeset
80 extern void gomp_team_barrier_wait_final (gomp_barrier_t *);
kono
parents:
diff changeset
81 extern void gomp_team_barrier_wait_end (gomp_barrier_t *,
kono
parents:
diff changeset
82 gomp_barrier_state_t);
kono
parents:
diff changeset
83 extern bool gomp_team_barrier_wait_cancel (gomp_barrier_t *);
kono
parents:
diff changeset
84 extern bool gomp_team_barrier_wait_cancel_end (gomp_barrier_t *,
kono
parents:
diff changeset
85 gomp_barrier_state_t);
kono
parents:
diff changeset
86 extern void gomp_team_barrier_wake (gomp_barrier_t *, int);
kono
parents:
diff changeset
87 struct gomp_team;
kono
parents:
diff changeset
88 extern void gomp_team_barrier_cancel (struct gomp_team *);
kono
parents:
diff changeset
89
kono
parents:
diff changeset
90 static inline gomp_barrier_state_t
kono
parents:
diff changeset
91 gomp_barrier_wait_start (gomp_barrier_t *bar)
kono
parents:
diff changeset
92 {
kono
parents:
diff changeset
93 unsigned int ret = __atomic_load_n (&bar->generation, MEMMODEL_ACQUIRE);
kono
parents:
diff changeset
94 ret &= -BAR_INCR | BAR_CANCELLED;
kono
parents:
diff changeset
95 /* A memory barrier is needed before exiting from the various forms
kono
parents:
diff changeset
96 of gomp_barrier_wait, to satisfy OpenMP API version 3.1 section
kono
parents:
diff changeset
97 2.8.6 flush Construct, which says there is an implicit flush during
kono
parents:
diff changeset
98 a barrier region. This is a convenient place to add the barrier,
kono
parents:
diff changeset
99 so we use MEMMODEL_ACQ_REL here rather than MEMMODEL_ACQUIRE. */
kono
parents:
diff changeset
100 if (__atomic_add_fetch (&bar->awaited, -1, MEMMODEL_ACQ_REL) == 0)
kono
parents:
diff changeset
101 ret |= BAR_WAS_LAST;
kono
parents:
diff changeset
102 return ret;
kono
parents:
diff changeset
103 }
kono
parents:
diff changeset
104
kono
parents:
diff changeset
105 static inline gomp_barrier_state_t
kono
parents:
diff changeset
106 gomp_barrier_wait_cancel_start (gomp_barrier_t *bar)
kono
parents:
diff changeset
107 {
kono
parents:
diff changeset
108 return gomp_barrier_wait_start (bar);
kono
parents:
diff changeset
109 }
kono
parents:
diff changeset
110
kono
parents:
diff changeset
111 /* This is like gomp_barrier_wait_start, except it decrements
kono
parents:
diff changeset
112 bar->awaited_final rather than bar->awaited and should be used
kono
parents:
diff changeset
113 for the gomp_team_end barrier only. */
kono
parents:
diff changeset
114 static inline gomp_barrier_state_t
kono
parents:
diff changeset
115 gomp_barrier_wait_final_start (gomp_barrier_t *bar)
kono
parents:
diff changeset
116 {
kono
parents:
diff changeset
117 unsigned int ret = __atomic_load_n (&bar->generation, MEMMODEL_ACQUIRE);
kono
parents:
diff changeset
118 ret &= -BAR_INCR | BAR_CANCELLED;
kono
parents:
diff changeset
119 /* See above gomp_barrier_wait_start comment. */
kono
parents:
diff changeset
120 if (__atomic_add_fetch (&bar->awaited_final, -1, MEMMODEL_ACQ_REL) == 0)
kono
parents:
diff changeset
121 ret |= BAR_WAS_LAST;
kono
parents:
diff changeset
122 return ret;
kono
parents:
diff changeset
123 }
kono
parents:
diff changeset
124
kono
parents:
diff changeset
125 static inline bool
kono
parents:
diff changeset
126 gomp_barrier_last_thread (gomp_barrier_state_t state)
kono
parents:
diff changeset
127 {
kono
parents:
diff changeset
128 return state & BAR_WAS_LAST;
kono
parents:
diff changeset
129 }
kono
parents:
diff changeset
130
kono
parents:
diff changeset
131 /* All the inlines below must be called with team->task_lock
kono
parents:
diff changeset
132 held. */
kono
parents:
diff changeset
133
kono
parents:
diff changeset
134 static inline void
kono
parents:
diff changeset
135 gomp_team_barrier_set_task_pending (gomp_barrier_t *bar)
kono
parents:
diff changeset
136 {
kono
parents:
diff changeset
137 bar->generation |= BAR_TASK_PENDING;
kono
parents:
diff changeset
138 }
kono
parents:
diff changeset
139
kono
parents:
diff changeset
140 static inline void
kono
parents:
diff changeset
141 gomp_team_barrier_clear_task_pending (gomp_barrier_t *bar)
kono
parents:
diff changeset
142 {
kono
parents:
diff changeset
143 bar->generation &= ~BAR_TASK_PENDING;
kono
parents:
diff changeset
144 }
kono
parents:
diff changeset
145
kono
parents:
diff changeset
146 static inline void
kono
parents:
diff changeset
147 gomp_team_barrier_set_waiting_for_tasks (gomp_barrier_t *bar)
kono
parents:
diff changeset
148 {
kono
parents:
diff changeset
149 bar->generation |= BAR_WAITING_FOR_TASK;
kono
parents:
diff changeset
150 }
kono
parents:
diff changeset
151
kono
parents:
diff changeset
152 static inline bool
kono
parents:
diff changeset
153 gomp_team_barrier_waiting_for_tasks (gomp_barrier_t *bar)
kono
parents:
diff changeset
154 {
kono
parents:
diff changeset
155 return (bar->generation & BAR_WAITING_FOR_TASK) != 0;
kono
parents:
diff changeset
156 }
kono
parents:
diff changeset
157
kono
parents:
diff changeset
158 static inline bool
kono
parents:
diff changeset
159 gomp_team_barrier_cancelled (gomp_barrier_t *bar)
kono
parents:
diff changeset
160 {
kono
parents:
diff changeset
161 return __builtin_expect ((bar->generation & BAR_CANCELLED) != 0, 0);
kono
parents:
diff changeset
162 }
kono
parents:
diff changeset
163
kono
parents:
diff changeset
164 static inline void
kono
parents:
diff changeset
165 gomp_team_barrier_done (gomp_barrier_t *bar, gomp_barrier_state_t state)
kono
parents:
diff changeset
166 {
kono
parents:
diff changeset
167 bar->generation = (state & -BAR_INCR) + BAR_INCR;
kono
parents:
diff changeset
168 }
kono
parents:
diff changeset
169
kono
parents:
diff changeset
170 #endif /* GOMP_BARRIER_H */