annotate libgomp/config/linux/bar.c @ 111:04ced10e8804

gcc 7
author kono
date Fri, 27 Oct 2017 22:46:09 +0900
parents a06113de4d67
children 84e7813d76e9
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
111
kono
parents: 0
diff changeset
1 /* Copyright (C) 2005-2017 Free Software Foundation, Inc.
0
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
2 Contributed by Richard Henderson <rth@redhat.com>.
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
3
111
kono
parents: 0
diff changeset
4 This file is part of the GNU Offloading and Multi Processing Library
kono
parents: 0
diff changeset
5 (libgomp).
0
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
6
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
7 Libgomp is free software; you can redistribute it and/or modify it
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
8 under the terms of the GNU General Public License as published by
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
9 the Free Software Foundation; either version 3, or (at your option)
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
10 any later version.
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
11
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
12 Libgomp is distributed in the hope that it will be useful, but WITHOUT ANY
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
13 WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
14 FOR A PARTICULAR PURPOSE. See the GNU General Public License for
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
15 more details.
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
16
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
17 Under Section 7 of GPL version 3, you are granted additional
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
18 permissions described in the GCC Runtime Library Exception, version
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
19 3.1, as published by the Free Software Foundation.
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
20
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
21 You should have received a copy of the GNU General Public License and
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
22 a copy of the GCC Runtime Library Exception along with this program;
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
23 see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
24 <http://www.gnu.org/licenses/>. */
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
25
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
26 /* This is a Linux specific implementation of a barrier synchronization
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
27 mechanism for libgomp. This type is private to the library. This
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
28 implementation uses atomic instructions and the futex syscall. */
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
29
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
30 #include <limits.h>
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
31 #include "wait.h"
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
32
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
33
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
34 void
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
35 gomp_barrier_wait_end (gomp_barrier_t *bar, gomp_barrier_state_t state)
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
36 {
111
kono
parents: 0
diff changeset
37 if (__builtin_expect (state & BAR_WAS_LAST, 0))
0
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
38 {
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
39 /* Next time we'll be awaiting TOTAL threads again. */
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
40 bar->awaited = bar->total;
111
kono
parents: 0
diff changeset
41 __atomic_store_n (&bar->generation, bar->generation + BAR_INCR,
kono
parents: 0
diff changeset
42 MEMMODEL_RELEASE);
0
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
43 futex_wake ((int *) &bar->generation, INT_MAX);
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
44 }
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
45 else
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
46 {
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
47 do
111
kono
parents: 0
diff changeset
48 do_wait ((int *) &bar->generation, state);
kono
parents: 0
diff changeset
49 while (__atomic_load_n (&bar->generation, MEMMODEL_ACQUIRE) == state);
0
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
50 }
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
51 }
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
52
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
53 void
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
54 gomp_barrier_wait (gomp_barrier_t *bar)
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
55 {
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
56 gomp_barrier_wait_end (bar, gomp_barrier_wait_start (bar));
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
57 }
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
58
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
59 /* Like gomp_barrier_wait, except that if the encountering thread
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
60 is not the last one to hit the barrier, it returns immediately.
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
61 The intended usage is that a thread which intends to gomp_barrier_destroy
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
62 this barrier calls gomp_barrier_wait, while all other threads
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
63 call gomp_barrier_wait_last. When gomp_barrier_wait returns,
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
64 the barrier can be safely destroyed. */
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
65
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
66 void
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
67 gomp_barrier_wait_last (gomp_barrier_t *bar)
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
68 {
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
69 gomp_barrier_state_t state = gomp_barrier_wait_start (bar);
111
kono
parents: 0
diff changeset
70 if (state & BAR_WAS_LAST)
0
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
71 gomp_barrier_wait_end (bar, state);
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
72 }
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
73
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
74 void
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
75 gomp_team_barrier_wake (gomp_barrier_t *bar, int count)
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
76 {
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
77 futex_wake ((int *) &bar->generation, count == 0 ? INT_MAX : count);
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
78 }
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
79
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
80 void
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
81 gomp_team_barrier_wait_end (gomp_barrier_t *bar, gomp_barrier_state_t state)
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
82 {
111
kono
parents: 0
diff changeset
83 unsigned int generation, gen;
0
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
84
111
kono
parents: 0
diff changeset
85 if (__builtin_expect (state & BAR_WAS_LAST, 0))
0
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
86 {
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
87 /* Next time we'll be awaiting TOTAL threads again. */
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
88 struct gomp_thread *thr = gomp_thread ();
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
89 struct gomp_team *team = thr->ts.team;
111
kono
parents: 0
diff changeset
90
0
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
91 bar->awaited = bar->total;
111
kono
parents: 0
diff changeset
92 team->work_share_cancelled = 0;
0
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
93 if (__builtin_expect (team->task_count, 0))
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
94 {
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
95 gomp_barrier_handle_tasks (state);
111
kono
parents: 0
diff changeset
96 state &= ~BAR_WAS_LAST;
0
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
97 }
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
98 else
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
99 {
111
kono
parents: 0
diff changeset
100 state &= ~BAR_CANCELLED;
kono
parents: 0
diff changeset
101 state += BAR_INCR - BAR_WAS_LAST;
kono
parents: 0
diff changeset
102 __atomic_store_n (&bar->generation, state, MEMMODEL_RELEASE);
0
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
103 futex_wake ((int *) &bar->generation, INT_MAX);
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
104 return;
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
105 }
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
106 }
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
107
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
108 generation = state;
111
kono
parents: 0
diff changeset
109 state &= ~BAR_CANCELLED;
0
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
110 do
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
111 {
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
112 do_wait ((int *) &bar->generation, generation);
111
kono
parents: 0
diff changeset
113 gen = __atomic_load_n (&bar->generation, MEMMODEL_ACQUIRE);
kono
parents: 0
diff changeset
114 if (__builtin_expect (gen & BAR_TASK_PENDING, 0))
kono
parents: 0
diff changeset
115 {
kono
parents: 0
diff changeset
116 gomp_barrier_handle_tasks (state);
kono
parents: 0
diff changeset
117 gen = __atomic_load_n (&bar->generation, MEMMODEL_ACQUIRE);
kono
parents: 0
diff changeset
118 }
kono
parents: 0
diff changeset
119 generation |= gen & BAR_WAITING_FOR_TASK;
0
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
120 }
111
kono
parents: 0
diff changeset
121 while (gen != state + BAR_INCR);
0
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
122 }
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
123
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
124 void
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
125 gomp_team_barrier_wait (gomp_barrier_t *bar)
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
126 {
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
127 gomp_team_barrier_wait_end (bar, gomp_barrier_wait_start (bar));
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
128 }
111
kono
parents: 0
diff changeset
129
kono
parents: 0
diff changeset
130 void
kono
parents: 0
diff changeset
131 gomp_team_barrier_wait_final (gomp_barrier_t *bar)
kono
parents: 0
diff changeset
132 {
kono
parents: 0
diff changeset
133 gomp_barrier_state_t state = gomp_barrier_wait_final_start (bar);
kono
parents: 0
diff changeset
134 if (__builtin_expect (state & BAR_WAS_LAST, 0))
kono
parents: 0
diff changeset
135 bar->awaited_final = bar->total;
kono
parents: 0
diff changeset
136 gomp_team_barrier_wait_end (bar, state);
kono
parents: 0
diff changeset
137 }
kono
parents: 0
diff changeset
138
kono
parents: 0
diff changeset
139 bool
kono
parents: 0
diff changeset
140 gomp_team_barrier_wait_cancel_end (gomp_barrier_t *bar,
kono
parents: 0
diff changeset
141 gomp_barrier_state_t state)
kono
parents: 0
diff changeset
142 {
kono
parents: 0
diff changeset
143 unsigned int generation, gen;
kono
parents: 0
diff changeset
144
kono
parents: 0
diff changeset
145 if (__builtin_expect (state & BAR_WAS_LAST, 0))
kono
parents: 0
diff changeset
146 {
kono
parents: 0
diff changeset
147 /* Next time we'll be awaiting TOTAL threads again. */
kono
parents: 0
diff changeset
148 /* BAR_CANCELLED should never be set in state here, because
kono
parents: 0
diff changeset
149 cancellation means that at least one of the threads has been
kono
parents: 0
diff changeset
150 cancelled, thus on a cancellable barrier we should never see
kono
parents: 0
diff changeset
151 all threads to arrive. */
kono
parents: 0
diff changeset
152 struct gomp_thread *thr = gomp_thread ();
kono
parents: 0
diff changeset
153 struct gomp_team *team = thr->ts.team;
kono
parents: 0
diff changeset
154
kono
parents: 0
diff changeset
155 bar->awaited = bar->total;
kono
parents: 0
diff changeset
156 team->work_share_cancelled = 0;
kono
parents: 0
diff changeset
157 if (__builtin_expect (team->task_count, 0))
kono
parents: 0
diff changeset
158 {
kono
parents: 0
diff changeset
159 gomp_barrier_handle_tasks (state);
kono
parents: 0
diff changeset
160 state &= ~BAR_WAS_LAST;
kono
parents: 0
diff changeset
161 }
kono
parents: 0
diff changeset
162 else
kono
parents: 0
diff changeset
163 {
kono
parents: 0
diff changeset
164 state += BAR_INCR - BAR_WAS_LAST;
kono
parents: 0
diff changeset
165 __atomic_store_n (&bar->generation, state, MEMMODEL_RELEASE);
kono
parents: 0
diff changeset
166 futex_wake ((int *) &bar->generation, INT_MAX);
kono
parents: 0
diff changeset
167 return false;
kono
parents: 0
diff changeset
168 }
kono
parents: 0
diff changeset
169 }
kono
parents: 0
diff changeset
170
kono
parents: 0
diff changeset
171 if (__builtin_expect (state & BAR_CANCELLED, 0))
kono
parents: 0
diff changeset
172 return true;
kono
parents: 0
diff changeset
173
kono
parents: 0
diff changeset
174 generation = state;
kono
parents: 0
diff changeset
175 do
kono
parents: 0
diff changeset
176 {
kono
parents: 0
diff changeset
177 do_wait ((int *) &bar->generation, generation);
kono
parents: 0
diff changeset
178 gen = __atomic_load_n (&bar->generation, MEMMODEL_ACQUIRE);
kono
parents: 0
diff changeset
179 if (__builtin_expect (gen & BAR_CANCELLED, 0))
kono
parents: 0
diff changeset
180 return true;
kono
parents: 0
diff changeset
181 if (__builtin_expect (gen & BAR_TASK_PENDING, 0))
kono
parents: 0
diff changeset
182 {
kono
parents: 0
diff changeset
183 gomp_barrier_handle_tasks (state);
kono
parents: 0
diff changeset
184 gen = __atomic_load_n (&bar->generation, MEMMODEL_ACQUIRE);
kono
parents: 0
diff changeset
185 }
kono
parents: 0
diff changeset
186 generation |= gen & BAR_WAITING_FOR_TASK;
kono
parents: 0
diff changeset
187 }
kono
parents: 0
diff changeset
188 while (gen != state + BAR_INCR);
kono
parents: 0
diff changeset
189
kono
parents: 0
diff changeset
190 return false;
kono
parents: 0
diff changeset
191 }
kono
parents: 0
diff changeset
192
kono
parents: 0
diff changeset
193 bool
kono
parents: 0
diff changeset
194 gomp_team_barrier_wait_cancel (gomp_barrier_t *bar)
kono
parents: 0
diff changeset
195 {
kono
parents: 0
diff changeset
196 return gomp_team_barrier_wait_cancel_end (bar, gomp_barrier_wait_start (bar));
kono
parents: 0
diff changeset
197 }
kono
parents: 0
diff changeset
198
kono
parents: 0
diff changeset
199 void
kono
parents: 0
diff changeset
200 gomp_team_barrier_cancel (struct gomp_team *team)
kono
parents: 0
diff changeset
201 {
kono
parents: 0
diff changeset
202 gomp_mutex_lock (&team->task_lock);
kono
parents: 0
diff changeset
203 if (team->barrier.generation & BAR_CANCELLED)
kono
parents: 0
diff changeset
204 {
kono
parents: 0
diff changeset
205 gomp_mutex_unlock (&team->task_lock);
kono
parents: 0
diff changeset
206 return;
kono
parents: 0
diff changeset
207 }
kono
parents: 0
diff changeset
208 team->barrier.generation |= BAR_CANCELLED;
kono
parents: 0
diff changeset
209 gomp_mutex_unlock (&team->task_lock);
kono
parents: 0
diff changeset
210 futex_wake ((int *) &team->barrier.generation, INT_MAX);
kono
parents: 0
diff changeset
211 }