Daily bump.
[official-gcc.git] / libgomp / config / nvptx / bar.c
blobe3a955f8b304b0f881d359d169c71eaa0ad30ac3
1 /* Copyright (C) 2015-2017 Free Software Foundation, Inc.
2 Contributed by Alexander Monakov <amonakov@ispras.ru>
4 This file is part of the GNU Offloading and Multi Processing Library
5 (libgomp).
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)
10 any later version.
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
15 more details.
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 an NVPTX specific implementation of a barrier synchronization
27 mechanism for libgomp. This type is private to the library. This
28 implementation uses atomic instructions and bar.sync instruction. */
30 #include <limits.h>
31 #include "libgomp.h"
34 void
35 gomp_barrier_wait_end (gomp_barrier_t *bar, gomp_barrier_state_t state)
37 if (__builtin_expect (state & BAR_WAS_LAST, 0))
39 /* Next time we'll be awaiting TOTAL threads again. */
40 bar->awaited = bar->total;
41 __atomic_store_n (&bar->generation, bar->generation + BAR_INCR,
42 MEMMODEL_RELEASE);
44 asm ("bar.sync 1, %0;" : : "r" (32 * bar->total));
47 void
48 gomp_barrier_wait (gomp_barrier_t *bar)
50 gomp_barrier_wait_end (bar, gomp_barrier_wait_start (bar));
53 /* Like gomp_barrier_wait, except that if the encountering thread
54 is not the last one to hit the barrier, it returns immediately.
55 The intended usage is that a thread which intends to gomp_barrier_destroy
56 this barrier calls gomp_barrier_wait, while all other threads
57 call gomp_barrier_wait_last. When gomp_barrier_wait returns,
58 the barrier can be safely destroyed. */
60 void
61 gomp_barrier_wait_last (gomp_barrier_t *bar)
63 /* Deferring to gomp_barrier_wait does not use the optimization opportunity
64 allowed by the interface contract for all-but-last participants. The
65 original implementation in config/linux/bar.c handles this better. */
66 gomp_barrier_wait (bar);
69 void
70 gomp_team_barrier_wake (gomp_barrier_t *bar, int count)
72 asm ("bar.sync 1, %0;" : : "r" (32 * bar->total));
75 void
76 gomp_team_barrier_wait_end (gomp_barrier_t *bar, gomp_barrier_state_t state)
78 unsigned int generation, gen;
80 if (__builtin_expect (state & BAR_WAS_LAST, 0))
82 /* Next time we'll be awaiting TOTAL threads again. */
83 struct gomp_thread *thr = gomp_thread ();
84 struct gomp_team *team = thr->ts.team;
86 bar->awaited = bar->total;
87 team->work_share_cancelled = 0;
88 if (__builtin_expect (team->task_count, 0))
90 gomp_barrier_handle_tasks (state);
91 state &= ~BAR_WAS_LAST;
93 else
95 state &= ~BAR_CANCELLED;
96 state += BAR_INCR - BAR_WAS_LAST;
97 __atomic_store_n (&bar->generation, state, MEMMODEL_RELEASE);
98 asm ("bar.sync 1, %0;" : : "r" (32 * bar->total));
99 return;
103 generation = state;
104 state &= ~BAR_CANCELLED;
107 asm ("bar.sync 1, %0;" : : "r" (32 * bar->total));
108 gen = __atomic_load_n (&bar->generation, MEMMODEL_ACQUIRE);
109 if (__builtin_expect (gen & BAR_TASK_PENDING, 0))
111 gomp_barrier_handle_tasks (state);
112 gen = __atomic_load_n (&bar->generation, MEMMODEL_ACQUIRE);
114 generation |= gen & BAR_WAITING_FOR_TASK;
116 while (gen != state + BAR_INCR);
119 void
120 gomp_team_barrier_wait (gomp_barrier_t *bar)
122 gomp_team_barrier_wait_end (bar, gomp_barrier_wait_start (bar));
125 void
126 gomp_team_barrier_wait_final (gomp_barrier_t *bar)
128 gomp_barrier_state_t state = gomp_barrier_wait_final_start (bar);
129 if (__builtin_expect (state & BAR_WAS_LAST, 0))
130 bar->awaited_final = bar->total;
131 gomp_team_barrier_wait_end (bar, state);
134 bool
135 gomp_team_barrier_wait_cancel_end (gomp_barrier_t *bar,
136 gomp_barrier_state_t state)
138 unsigned int generation, gen;
140 if (__builtin_expect (state & BAR_WAS_LAST, 0))
142 /* Next time we'll be awaiting TOTAL threads again. */
143 /* BAR_CANCELLED should never be set in state here, because
144 cancellation means that at least one of the threads has been
145 cancelled, thus on a cancellable barrier we should never see
146 all threads to arrive. */
147 struct gomp_thread *thr = gomp_thread ();
148 struct gomp_team *team = thr->ts.team;
150 bar->awaited = bar->total;
151 team->work_share_cancelled = 0;
152 if (__builtin_expect (team->task_count, 0))
154 gomp_barrier_handle_tasks (state);
155 state &= ~BAR_WAS_LAST;
157 else
159 state += BAR_INCR - BAR_WAS_LAST;
160 __atomic_store_n (&bar->generation, state, MEMMODEL_RELEASE);
161 asm ("bar.sync 1, %0;" : : "r" (32 * bar->total));
162 return false;
166 if (__builtin_expect (state & BAR_CANCELLED, 0))
167 return true;
169 generation = state;
172 asm ("bar.sync 1, %0;" : : "r" (32 * bar->total));
173 gen = __atomic_load_n (&bar->generation, MEMMODEL_ACQUIRE);
174 if (__builtin_expect (gen & BAR_CANCELLED, 0))
175 return true;
176 if (__builtin_expect (gen & BAR_TASK_PENDING, 0))
178 gomp_barrier_handle_tasks (state);
179 gen = __atomic_load_n (&bar->generation, MEMMODEL_ACQUIRE);
181 generation |= gen & BAR_WAITING_FOR_TASK;
183 while (gen != state + BAR_INCR);
185 return false;
188 bool
189 gomp_team_barrier_wait_cancel (gomp_barrier_t *bar)
191 return gomp_team_barrier_wait_cancel_end (bar, gomp_barrier_wait_start (bar));
194 void
195 gomp_team_barrier_cancel (struct gomp_team *team)
197 gomp_mutex_lock (&team->task_lock);
198 if (team->barrier.generation & BAR_CANCELLED)
200 gomp_mutex_unlock (&team->task_lock);
201 return;
203 team->barrier.generation |= BAR_CANCELLED;
204 gomp_mutex_unlock (&team->task_lock);
205 gomp_team_barrier_wake (&team->barrier, INT_MAX);