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)
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);
48 gomp_barrier_destroy (gomp_barrier_t *bar)
50 /* Before destroying, make sure all threads have left the barrier. */
51 gomp_mutex_lock (&bar->mutex1);
52 gomp_mutex_unlock (&bar->mutex1);
54 gomp_mutex_destroy (&bar->mutex1);
55 #ifndef HAVE_SYNC_BUILTINS
56 gomp_mutex_destroy (&bar->mutex2);
58 gomp_sem_destroy (&bar->sem1);
59 gomp_sem_destroy (&bar->sem2);
63 gomp_barrier_reinit (gomp_barrier_t *bar, unsigned count)
65 gomp_mutex_lock (&bar->mutex1);
67 gomp_mutex_unlock (&bar->mutex1);
71 gomp_barrier_wait_end (gomp_barrier_t *bar, gomp_barrier_state_t state)
81 gomp_sem_post (&bar->sem1);
83 gomp_sem_wait (&bar->sem2);
85 gomp_mutex_unlock (&bar->mutex1);
89 gomp_mutex_unlock (&bar->mutex1);
90 gomp_sem_wait (&bar->sem1);
92 #ifdef HAVE_SYNC_BUILTINS
93 n = __sync_add_and_fetch (&bar->arrived, -1);
95 gomp_mutex_lock (&bar->mutex2);
97 gomp_mutex_unlock (&bar->mutex2);
101 gomp_sem_post (&bar->sem2);
106 gomp_barrier_wait (gomp_barrier_t *barrier)
108 gomp_barrier_wait_end (barrier, gomp_barrier_wait_start (barrier));
112 gomp_team_barrier_wait_end (gomp_barrier_t *bar, gomp_barrier_state_t state)
119 struct gomp_thread *thr = gomp_thread ();
120 struct gomp_team *team = thr->ts.team;
122 if (team->task_count)
124 gomp_barrier_handle_tasks (state);
126 gomp_sem_wait (&bar->sem2);
127 gomp_mutex_unlock (&bar->mutex1);
131 bar->generation = state + 3;
135 gomp_sem_post (&bar->sem1);
137 gomp_sem_wait (&bar->sem2);
139 gomp_mutex_unlock (&bar->mutex1);
143 gomp_mutex_unlock (&bar->mutex1);
146 gomp_sem_wait (&bar->sem1);
147 if (bar->generation & 1)
148 gomp_barrier_handle_tasks (state);
150 while (bar->generation != state + 4);
152 #ifdef HAVE_SYNC_BUILTINS
153 n = __sync_add_and_fetch (&bar->arrived, -1);
155 gomp_mutex_lock (&bar->mutex2);
157 gomp_mutex_unlock (&bar->mutex2);
161 gomp_sem_post (&bar->sem2);
166 gomp_team_barrier_wait (gomp_barrier_t *barrier)
168 gomp_team_barrier_wait_end (barrier, gomp_barrier_wait_start (barrier));
172 gomp_team_barrier_wake (gomp_barrier_t *bar, int count)
175 count = bar->total - 1;
177 gomp_sem_post (&bar->sem1);