1 /* Copyright (C) 2005-2014 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)
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
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. */
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
);
40 gomp_sem_init (&bar
->sem1
, 0);
41 gomp_sem_init (&bar
->sem2
, 0);
45 bar
->cancellable
= false;
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
);
59 gomp_sem_destroy (&bar
->sem1
);
60 gomp_sem_destroy (&bar
->sem2
);
64 gomp_barrier_reinit (gomp_barrier_t
*bar
, unsigned count
)
66 gomp_mutex_lock (&bar
->mutex1
);
68 gomp_mutex_unlock (&bar
->mutex1
);
72 gomp_barrier_wait_end (gomp_barrier_t
*bar
, gomp_barrier_state_t state
)
76 if (state
& BAR_WAS_LAST
)
82 gomp_sem_post (&bar
->sem1
);
84 gomp_sem_wait (&bar
->sem2
);
86 gomp_mutex_unlock (&bar
->mutex1
);
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);
96 gomp_mutex_lock (&bar
->mutex2
);
98 gomp_mutex_unlock (&bar
->mutex2
);
102 gomp_sem_post (&bar
->sem2
);
107 gomp_barrier_wait (gomp_barrier_t
*barrier
)
109 gomp_barrier_wait_end (barrier
, gomp_barrier_wait_start (barrier
));
113 gomp_team_barrier_wait_end (gomp_barrier_t
*bar
, gomp_barrier_state_t state
)
117 state
&= ~BAR_CANCELLED
;
118 if (state
& BAR_WAS_LAST
)
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
);
129 gomp_sem_wait (&bar
->sem2
);
130 gomp_mutex_unlock (&bar
->mutex1
);
134 bar
->generation
= state
+ BAR_INCR
- BAR_WAS_LAST
;
138 gomp_sem_post (&bar
->sem1
);
140 gomp_sem_wait (&bar
->sem2
);
142 gomp_mutex_unlock (&bar
->mutex1
);
146 gomp_mutex_unlock (&bar
->mutex1
);
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);
163 gomp_mutex_lock (&bar
->mutex2
);
165 gomp_mutex_unlock (&bar
->mutex2
);
169 gomp_sem_post (&bar
->sem2
);
174 gomp_team_barrier_wait_cancel_end (gomp_barrier_t
*bar
,
175 gomp_barrier_state_t state
)
179 if (state
& BAR_WAS_LAST
)
181 bar
->cancellable
= false;
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
);
191 gomp_sem_wait (&bar
->sem2
);
192 gomp_mutex_unlock (&bar
->mutex1
);
196 bar
->generation
= state
+ BAR_INCR
- BAR_WAS_LAST
;
200 gomp_sem_post (&bar
->sem1
);
202 gomp_sem_wait (&bar
->sem2
);
204 gomp_mutex_unlock (&bar
->mutex1
);
208 if (state
& BAR_CANCELLED
)
210 gomp_mutex_unlock (&bar
->mutex1
);
213 bar
->cancellable
= true;
214 gomp_mutex_unlock (&bar
->mutex1
);
218 gomp_sem_wait (&bar
->sem1
);
219 gen
= __atomic_load_n (&bar
->generation
, MEMMODEL_ACQUIRE
);
220 if (gen
& BAR_CANCELLED
)
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
)
230 while (gen
!= state
+ BAR_INCR
);
232 #ifdef HAVE_SYNC_BUILTINS
233 n
= __sync_add_and_fetch (&bar
->arrived
, -1);
235 gomp_mutex_lock (&bar
->mutex2
);
237 gomp_mutex_unlock (&bar
->mutex2
);
241 gomp_sem_post (&bar
->sem2
);
242 if (gen
& BAR_CANCELLED
)
249 gomp_team_barrier_wait (gomp_barrier_t
*barrier
)
251 gomp_team_barrier_wait_end (barrier
, gomp_barrier_wait_start (barrier
));
255 gomp_team_barrier_wake (gomp_barrier_t
*bar
, int count
)
258 count
= bar
->total
- 1;
260 gomp_sem_post (&bar
->sem1
);
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
);
271 gomp_team_barrier_cancel (struct gomp_team
*team
)
273 if (team
->barrier
.generation
& BAR_CANCELLED
)
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
);
283 team
->barrier
.generation
|= BAR_CANCELLED
;
284 gomp_mutex_unlock (&team
->task_lock
);
285 if (team
->barrier
.cancellable
)
287 int n
= team
->barrier
.arrived
;
291 gomp_sem_post (&team
->barrier
.sem1
);
293 gomp_sem_wait (&team
->barrier
.sem2
);
295 team
->barrier
.cancellable
= false;
297 gomp_mutex_unlock (&team
->barrier
.mutex1
);