2013-12-29 Janus Weil <janus@gcc.gnu.org>
[official-gcc.git] / libgomp / config / posix / bar.c
blobbdf3978caee9b0a3e54a1721e577a1948d93ced2
1 /* Copyright (C) 2005-2013 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 General Public License as published by
8 the Free Software Foundation; either version 3, or (at your option)
9 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 General Public License for
14 more details.
16 Under Section 7 of GPL version 3, you are granted additional
17 permissions described in the GCC Runtime Library Exception, version
18 3.1, as published by the Free Software Foundation.
20 You should have received a copy of the GNU General Public License and
21 a copy of the GCC Runtime Library Exception along with this program;
22 see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
23 <http://www.gnu.org/licenses/>. */
25 /* This is the default implementation of a barrier synchronization mechanism
26 for libgomp. This type is private to the library. Note that we rely on
27 being able to adjust the barrier count while threads are blocked, so the
28 POSIX pthread_barrier_t won't work. */
30 #include "libgomp.h"
33 void
34 gomp_barrier_init (gomp_barrier_t *bar, unsigned count)
36 gomp_mutex_init (&bar->mutex1);
37 #ifndef HAVE_SYNC_BUILTINS
38 gomp_mutex_init (&bar->mutex2);
39 #endif
40 gomp_sem_init (&bar->sem1, 0);
41 gomp_sem_init (&bar->sem2, 0);
42 bar->total = count;
43 bar->arrived = 0;
44 bar->generation = 0;
45 bar->cancellable = false;
48 void
49 gomp_barrier_destroy (gomp_barrier_t *bar)
51 /* Before destroying, make sure all threads have left the barrier. */
52 gomp_mutex_lock (&bar->mutex1);
53 gomp_mutex_unlock (&bar->mutex1);
55 gomp_mutex_destroy (&bar->mutex1);
56 #ifndef HAVE_SYNC_BUILTINS
57 gomp_mutex_destroy (&bar->mutex2);
58 #endif
59 gomp_sem_destroy (&bar->sem1);
60 gomp_sem_destroy (&bar->sem2);
63 void
64 gomp_barrier_reinit (gomp_barrier_t *bar, unsigned count)
66 gomp_mutex_lock (&bar->mutex1);
67 bar->total = count;
68 gomp_mutex_unlock (&bar->mutex1);
71 void
72 gomp_barrier_wait_end (gomp_barrier_t *bar, gomp_barrier_state_t state)
74 unsigned int n;
76 if (state & BAR_WAS_LAST)
78 n = --bar->arrived;
79 if (n > 0)
82 gomp_sem_post (&bar->sem1);
83 while (--n != 0);
84 gomp_sem_wait (&bar->sem2);
86 gomp_mutex_unlock (&bar->mutex1);
88 else
90 gomp_mutex_unlock (&bar->mutex1);
91 gomp_sem_wait (&bar->sem1);
93 #ifdef HAVE_SYNC_BUILTINS
94 n = __sync_add_and_fetch (&bar->arrived, -1);
95 #else
96 gomp_mutex_lock (&bar->mutex2);
97 n = --bar->arrived;
98 gomp_mutex_unlock (&bar->mutex2);
99 #endif
101 if (n == 0)
102 gomp_sem_post (&bar->sem2);
106 void
107 gomp_barrier_wait (gomp_barrier_t *barrier)
109 gomp_barrier_wait_end (barrier, gomp_barrier_wait_start (barrier));
112 void
113 gomp_team_barrier_wait_end (gomp_barrier_t *bar, gomp_barrier_state_t state)
115 unsigned int n;
117 state &= ~BAR_CANCELLED;
118 if (state & BAR_WAS_LAST)
120 n = --bar->arrived;
121 struct gomp_thread *thr = gomp_thread ();
122 struct gomp_team *team = thr->ts.team;
124 team->work_share_cancelled = 0;
125 if (team->task_count)
127 gomp_barrier_handle_tasks (state);
128 if (n > 0)
129 gomp_sem_wait (&bar->sem2);
130 gomp_mutex_unlock (&bar->mutex1);
131 return;
134 bar->generation = state + BAR_INCR - BAR_WAS_LAST;
135 if (n > 0)
138 gomp_sem_post (&bar->sem1);
139 while (--n != 0);
140 gomp_sem_wait (&bar->sem2);
142 gomp_mutex_unlock (&bar->mutex1);
144 else
146 gomp_mutex_unlock (&bar->mutex1);
147 int gen;
150 gomp_sem_wait (&bar->sem1);
151 gen = __atomic_load_n (&bar->generation, MEMMODEL_ACQUIRE);
152 if (gen & BAR_TASK_PENDING)
154 gomp_barrier_handle_tasks (state);
155 gen = __atomic_load_n (&bar->generation, MEMMODEL_ACQUIRE);
158 while (gen != state + BAR_INCR);
160 #ifdef HAVE_SYNC_BUILTINS
161 n = __sync_add_and_fetch (&bar->arrived, -1);
162 #else
163 gomp_mutex_lock (&bar->mutex2);
164 n = --bar->arrived;
165 gomp_mutex_unlock (&bar->mutex2);
166 #endif
168 if (n == 0)
169 gomp_sem_post (&bar->sem2);
173 bool
174 gomp_team_barrier_wait_cancel_end (gomp_barrier_t *bar,
175 gomp_barrier_state_t state)
177 unsigned int n;
179 if (state & BAR_WAS_LAST)
181 bar->cancellable = false;
182 n = --bar->arrived;
183 struct gomp_thread *thr = gomp_thread ();
184 struct gomp_team *team = thr->ts.team;
186 team->work_share_cancelled = 0;
187 if (team->task_count)
189 gomp_barrier_handle_tasks (state);
190 if (n > 0)
191 gomp_sem_wait (&bar->sem2);
192 gomp_mutex_unlock (&bar->mutex1);
193 return false;
196 bar->generation = state + BAR_INCR - BAR_WAS_LAST;
197 if (n > 0)
200 gomp_sem_post (&bar->sem1);
201 while (--n != 0);
202 gomp_sem_wait (&bar->sem2);
204 gomp_mutex_unlock (&bar->mutex1);
206 else
208 if (state & BAR_CANCELLED)
210 gomp_mutex_unlock (&bar->mutex1);
211 return true;
213 bar->cancellable = true;
214 gomp_mutex_unlock (&bar->mutex1);
215 int gen;
218 gomp_sem_wait (&bar->sem1);
219 gen = __atomic_load_n (&bar->generation, MEMMODEL_ACQUIRE);
220 if (gen & BAR_CANCELLED)
221 break;
222 if (gen & BAR_TASK_PENDING)
224 gomp_barrier_handle_tasks (state);
225 gen = __atomic_load_n (&bar->generation, MEMMODEL_ACQUIRE);
226 if (gen & BAR_CANCELLED)
227 break;
230 while (gen != state + BAR_INCR);
232 #ifdef HAVE_SYNC_BUILTINS
233 n = __sync_add_and_fetch (&bar->arrived, -1);
234 #else
235 gomp_mutex_lock (&bar->mutex2);
236 n = --bar->arrived;
237 gomp_mutex_unlock (&bar->mutex2);
238 #endif
240 if (n == 0)
241 gomp_sem_post (&bar->sem2);
242 if (gen & BAR_CANCELLED)
243 return true;
245 return false;
248 void
249 gomp_team_barrier_wait (gomp_barrier_t *barrier)
251 gomp_team_barrier_wait_end (barrier, gomp_barrier_wait_start (barrier));
254 void
255 gomp_team_barrier_wake (gomp_barrier_t *bar, int count)
257 if (count == 0)
258 count = bar->total - 1;
259 while (count-- > 0)
260 gomp_sem_post (&bar->sem1);
263 bool
264 gomp_team_barrier_wait_cancel (gomp_barrier_t *bar)
266 gomp_barrier_state_t state = gomp_barrier_wait_cancel_start (bar);
267 return gomp_team_barrier_wait_cancel_end (bar, state);
270 void
271 gomp_team_barrier_cancel (struct gomp_team *team)
273 if (team->barrier.generation & BAR_CANCELLED)
274 return;
275 gomp_mutex_lock (&team->barrier.mutex1);
276 gomp_mutex_lock (&team->task_lock);
277 if (team->barrier.generation & BAR_CANCELLED)
279 gomp_mutex_unlock (&team->task_lock);
280 gomp_mutex_unlock (&team->barrier.mutex1);
281 return;
283 team->barrier.generation |= BAR_CANCELLED;
284 gomp_mutex_unlock (&team->task_lock);
285 if (team->barrier.cancellable)
287 int n = team->barrier.arrived;
288 if (n > 0)
291 gomp_sem_post (&team->barrier.sem1);
292 while (--n != 0);
293 gomp_sem_wait (&team->barrier.sem2);
295 team->barrier.cancellable = false;
297 gomp_mutex_unlock (&team->barrier.mutex1);