1 /* Copyright (C) 2005-2018 Free Software Foundation, Inc.
2 Contributed by Richard Henderson <rth@redhat.com>.
4 This file is part of the GNU Offloading and Multi Processing Library
7 Libgomp is free software; you can redistribute it and/or modify it
8 under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3, or (at your option)
12 Libgomp is distributed in the hope that it will be useful, but WITHOUT ANY
13 WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
14 FOR A PARTICULAR PURPOSE. See the GNU General Public License for
17 Under Section 7 of GPL version 3, you are granted additional
18 permissions described in the GCC Runtime Library Exception, version
19 3.1, as published by the Free Software Foundation.
21 You should have received a copy of the GNU General Public License and
22 a copy of the GCC Runtime Library Exception along with this program;
23 see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
24 <http://www.gnu.org/licenses/>. */
26 /* This is the default implementation of a barrier synchronization mechanism
27 for libgomp. This type is private to the library. Note that we rely on
28 being able to adjust the barrier count while threads are blocked, so the
29 POSIX pthread_barrier_t won't work. */
35 gomp_barrier_init (gomp_barrier_t
*bar
, unsigned count
)
37 gomp_mutex_init (&bar
->mutex1
);
38 #ifndef HAVE_SYNC_BUILTINS
39 gomp_mutex_init (&bar
->mutex2
);
41 gomp_sem_init (&bar
->sem1
, 0);
42 gomp_sem_init (&bar
->sem2
, 0);
46 bar
->cancellable
= false;
50 gomp_barrier_destroy (gomp_barrier_t
*bar
)
52 /* Before destroying, make sure all threads have left the barrier. */
53 gomp_mutex_lock (&bar
->mutex1
);
54 gomp_mutex_unlock (&bar
->mutex1
);
56 gomp_mutex_destroy (&bar
->mutex1
);
57 #ifndef HAVE_SYNC_BUILTINS
58 gomp_mutex_destroy (&bar
->mutex2
);
60 gomp_sem_destroy (&bar
->sem1
);
61 gomp_sem_destroy (&bar
->sem2
);
65 gomp_barrier_reinit (gomp_barrier_t
*bar
, unsigned count
)
67 gomp_mutex_lock (&bar
->mutex1
);
69 gomp_mutex_unlock (&bar
->mutex1
);
73 gomp_barrier_wait_end (gomp_barrier_t
*bar
, gomp_barrier_state_t state
)
77 if (state
& BAR_WAS_LAST
)
83 gomp_sem_post (&bar
->sem1
);
85 gomp_sem_wait (&bar
->sem2
);
87 gomp_mutex_unlock (&bar
->mutex1
);
91 gomp_mutex_unlock (&bar
->mutex1
);
92 gomp_sem_wait (&bar
->sem1
);
94 #ifdef HAVE_SYNC_BUILTINS
95 n
= __sync_add_and_fetch (&bar
->arrived
, -1);
97 gomp_mutex_lock (&bar
->mutex2
);
99 gomp_mutex_unlock (&bar
->mutex2
);
103 gomp_sem_post (&bar
->sem2
);
108 gomp_barrier_wait (gomp_barrier_t
*barrier
)
110 gomp_barrier_wait_end (barrier
, gomp_barrier_wait_start (barrier
));
114 gomp_team_barrier_wait_end (gomp_barrier_t
*bar
, gomp_barrier_state_t state
)
118 state
&= ~BAR_CANCELLED
;
119 if (state
& BAR_WAS_LAST
)
122 struct gomp_thread
*thr
= gomp_thread ();
123 struct gomp_team
*team
= thr
->ts
.team
;
125 team
->work_share_cancelled
= 0;
126 if (team
->task_count
)
128 gomp_barrier_handle_tasks (state
);
130 gomp_sem_wait (&bar
->sem2
);
131 gomp_mutex_unlock (&bar
->mutex1
);
135 bar
->generation
= state
+ BAR_INCR
- BAR_WAS_LAST
;
139 gomp_sem_post (&bar
->sem1
);
141 gomp_sem_wait (&bar
->sem2
);
143 gomp_mutex_unlock (&bar
->mutex1
);
147 gomp_mutex_unlock (&bar
->mutex1
);
151 gomp_sem_wait (&bar
->sem1
);
152 gen
= __atomic_load_n (&bar
->generation
, MEMMODEL_ACQUIRE
);
153 if (gen
& BAR_TASK_PENDING
)
155 gomp_barrier_handle_tasks (state
);
156 gen
= __atomic_load_n (&bar
->generation
, MEMMODEL_ACQUIRE
);
159 while (gen
!= state
+ BAR_INCR
);
161 #ifdef HAVE_SYNC_BUILTINS
162 n
= __sync_add_and_fetch (&bar
->arrived
, -1);
164 gomp_mutex_lock (&bar
->mutex2
);
166 gomp_mutex_unlock (&bar
->mutex2
);
170 gomp_sem_post (&bar
->sem2
);
175 gomp_team_barrier_wait_cancel_end (gomp_barrier_t
*bar
,
176 gomp_barrier_state_t state
)
180 if (state
& BAR_WAS_LAST
)
182 bar
->cancellable
= false;
184 struct gomp_thread
*thr
= gomp_thread ();
185 struct gomp_team
*team
= thr
->ts
.team
;
187 team
->work_share_cancelled
= 0;
188 if (team
->task_count
)
190 gomp_barrier_handle_tasks (state
);
192 gomp_sem_wait (&bar
->sem2
);
193 gomp_mutex_unlock (&bar
->mutex1
);
197 bar
->generation
= state
+ BAR_INCR
- BAR_WAS_LAST
;
201 gomp_sem_post (&bar
->sem1
);
203 gomp_sem_wait (&bar
->sem2
);
205 gomp_mutex_unlock (&bar
->mutex1
);
209 if (state
& BAR_CANCELLED
)
211 gomp_mutex_unlock (&bar
->mutex1
);
214 bar
->cancellable
= true;
215 gomp_mutex_unlock (&bar
->mutex1
);
219 gomp_sem_wait (&bar
->sem1
);
220 gen
= __atomic_load_n (&bar
->generation
, MEMMODEL_ACQUIRE
);
221 if (gen
& BAR_CANCELLED
)
223 if (gen
& BAR_TASK_PENDING
)
225 gomp_barrier_handle_tasks (state
);
226 gen
= __atomic_load_n (&bar
->generation
, MEMMODEL_ACQUIRE
);
227 if (gen
& BAR_CANCELLED
)
231 while (gen
!= state
+ BAR_INCR
);
233 #ifdef HAVE_SYNC_BUILTINS
234 n
= __sync_add_and_fetch (&bar
->arrived
, -1);
236 gomp_mutex_lock (&bar
->mutex2
);
238 gomp_mutex_unlock (&bar
->mutex2
);
242 gomp_sem_post (&bar
->sem2
);
243 if (gen
& BAR_CANCELLED
)
250 gomp_team_barrier_wait (gomp_barrier_t
*barrier
)
252 gomp_team_barrier_wait_end (barrier
, gomp_barrier_wait_start (barrier
));
256 gomp_team_barrier_wake (gomp_barrier_t
*bar
, int count
)
259 count
= bar
->total
- 1;
261 gomp_sem_post (&bar
->sem1
);
265 gomp_team_barrier_wait_cancel (gomp_barrier_t
*bar
)
267 gomp_barrier_state_t state
= gomp_barrier_wait_cancel_start (bar
);
268 return gomp_team_barrier_wait_cancel_end (bar
, state
);
272 gomp_team_barrier_cancel (struct gomp_team
*team
)
274 if (team
->barrier
.generation
& BAR_CANCELLED
)
276 gomp_mutex_lock (&team
->barrier
.mutex1
);
277 gomp_mutex_lock (&team
->task_lock
);
278 if (team
->barrier
.generation
& BAR_CANCELLED
)
280 gomp_mutex_unlock (&team
->task_lock
);
281 gomp_mutex_unlock (&team
->barrier
.mutex1
);
284 team
->barrier
.generation
|= BAR_CANCELLED
;
285 gomp_mutex_unlock (&team
->task_lock
);
286 if (team
->barrier
.cancellable
)
288 int n
= team
->barrier
.arrived
;
292 gomp_sem_post (&team
->barrier
.sem1
);
294 gomp_sem_wait (&team
->barrier
.sem2
);
296 team
->barrier
.cancellable
= false;
298 gomp_mutex_unlock (&team
->barrier
.mutex1
);