* g++.dg/cpp0x/constexpr-53094-2.C: Ignore non-standard ABI
[official-gcc.git] / libgomp / task.c
blob937f266cef375be99d4cca193d518eba9926f280
1 /* Copyright (C) 2007-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 file handles the maintainence of tasks in response to task
26 creation and termination. */
28 #include "libgomp.h"
29 #include <stdlib.h>
30 #include <string.h>
33 /* Create a new task data structure. */
35 void
36 gomp_init_task (struct gomp_task *task, struct gomp_task *parent_task,
37 struct gomp_task_icv *prev_icv)
39 task->parent = parent_task;
40 task->icv = *prev_icv;
41 task->kind = GOMP_TASK_IMPLICIT;
42 task->in_taskwait = false;
43 task->in_tied_task = false;
44 task->final_task = false;
45 task->children = NULL;
46 gomp_sem_init (&task->taskwait_sem, 0);
49 /* Clean up a task, after completing it. */
51 void
52 gomp_end_task (void)
54 struct gomp_thread *thr = gomp_thread ();
55 struct gomp_task *task = thr->task;
57 gomp_finish_task (task);
58 thr->task = task->parent;
61 static inline void
62 gomp_clear_parent (struct gomp_task *children)
64 struct gomp_task *task = children;
66 if (task)
69 task->parent = NULL;
70 task = task->next_child;
72 while (task != children);
75 /* Called when encountering an explicit task directive. If IF_CLAUSE is
76 false, then we must not delay in executing the task. If UNTIED is true,
77 then the task may be executed by any member of the team. */
79 void
80 GOMP_task (void (*fn) (void *), void *data, void (*cpyfn) (void *, void *),
81 long arg_size, long arg_align, bool if_clause, unsigned flags)
83 struct gomp_thread *thr = gomp_thread ();
84 struct gomp_team *team = thr->ts.team;
86 #ifdef HAVE_BROKEN_POSIX_SEMAPHORES
87 /* If pthread_mutex_* is used for omp_*lock*, then each task must be
88 tied to one thread all the time. This means UNTIED tasks must be
89 tied and if CPYFN is non-NULL IF(0) must be forced, as CPYFN
90 might be running on different thread than FN. */
91 if (cpyfn)
92 if_clause = false;
93 if (flags & 1)
94 flags &= ~1;
95 #endif
97 if (!if_clause || team == NULL
98 || (thr->task && thr->task->final_task)
99 || team->task_count > 64 * team->nthreads)
101 struct gomp_task task;
103 gomp_init_task (&task, thr->task, gomp_icv (false));
104 task.kind = GOMP_TASK_IFFALSE;
105 task.final_task = (thr->task && thr->task->final_task) || (flags & 2);
106 if (thr->task)
107 task.in_tied_task = thr->task->in_tied_task;
108 thr->task = &task;
109 if (__builtin_expect (cpyfn != NULL, 0))
111 char buf[arg_size + arg_align - 1];
112 char *arg = (char *) (((uintptr_t) buf + arg_align - 1)
113 & ~(uintptr_t) (arg_align - 1));
114 cpyfn (arg, data);
115 fn (arg);
117 else
118 fn (data);
119 if (task.children != NULL)
121 gomp_mutex_lock (&team->task_lock);
122 gomp_clear_parent (task.children);
123 gomp_mutex_unlock (&team->task_lock);
125 gomp_end_task ();
127 else
129 struct gomp_task *task;
130 struct gomp_task *parent = thr->task;
131 char *arg;
132 bool do_wake;
134 task = gomp_malloc (sizeof (*task) + arg_size + arg_align - 1);
135 arg = (char *) (((uintptr_t) (task + 1) + arg_align - 1)
136 & ~(uintptr_t) (arg_align - 1));
137 gomp_init_task (task, parent, gomp_icv (false));
138 task->kind = GOMP_TASK_IFFALSE;
139 task->in_tied_task = parent->in_tied_task;
140 thr->task = task;
141 if (cpyfn)
142 cpyfn (arg, data);
143 else
144 memcpy (arg, data, arg_size);
145 thr->task = parent;
146 task->kind = GOMP_TASK_WAITING;
147 task->fn = fn;
148 task->fn_data = arg;
149 task->in_tied_task = true;
150 task->final_task = (flags & 2) >> 1;
151 gomp_mutex_lock (&team->task_lock);
152 if (parent->children)
154 task->next_child = parent->children;
155 task->prev_child = parent->children->prev_child;
156 task->next_child->prev_child = task;
157 task->prev_child->next_child = task;
159 else
161 task->next_child = task;
162 task->prev_child = task;
164 parent->children = task;
165 if (team->task_queue)
167 task->next_queue = team->task_queue;
168 task->prev_queue = team->task_queue->prev_queue;
169 task->next_queue->prev_queue = task;
170 task->prev_queue->next_queue = task;
172 else
174 task->next_queue = task;
175 task->prev_queue = task;
176 team->task_queue = task;
178 ++team->task_count;
179 gomp_team_barrier_set_task_pending (&team->barrier);
180 do_wake = team->task_running_count + !parent->in_tied_task
181 < team->nthreads;
182 gomp_mutex_unlock (&team->task_lock);
183 if (do_wake)
184 gomp_team_barrier_wake (&team->barrier, 1);
188 void
189 gomp_barrier_handle_tasks (gomp_barrier_state_t state)
191 struct gomp_thread *thr = gomp_thread ();
192 struct gomp_team *team = thr->ts.team;
193 struct gomp_task *task = thr->task;
194 struct gomp_task *child_task = NULL;
195 struct gomp_task *to_free = NULL;
197 gomp_mutex_lock (&team->task_lock);
198 if (gomp_barrier_last_thread (state))
200 if (team->task_count == 0)
202 gomp_team_barrier_done (&team->barrier, state);
203 gomp_mutex_unlock (&team->task_lock);
204 gomp_team_barrier_wake (&team->barrier, 0);
205 return;
207 gomp_team_barrier_set_waiting_for_tasks (&team->barrier);
210 while (1)
212 if (team->task_queue != NULL)
214 struct gomp_task *parent;
216 child_task = team->task_queue;
217 parent = child_task->parent;
218 if (parent && parent->children == child_task)
219 parent->children = child_task->next_child;
220 child_task->prev_queue->next_queue = child_task->next_queue;
221 child_task->next_queue->prev_queue = child_task->prev_queue;
222 if (child_task->next_queue != child_task)
223 team->task_queue = child_task->next_queue;
224 else
225 team->task_queue = NULL;
226 child_task->kind = GOMP_TASK_TIED;
227 team->task_running_count++;
228 if (team->task_count == team->task_running_count)
229 gomp_team_barrier_clear_task_pending (&team->barrier);
231 gomp_mutex_unlock (&team->task_lock);
232 if (to_free)
234 gomp_finish_task (to_free);
235 free (to_free);
236 to_free = NULL;
238 if (child_task)
240 thr->task = child_task;
241 child_task->fn (child_task->fn_data);
242 thr->task = task;
244 else
245 return;
246 gomp_mutex_lock (&team->task_lock);
247 if (child_task)
249 struct gomp_task *parent = child_task->parent;
250 if (parent)
252 child_task->prev_child->next_child = child_task->next_child;
253 child_task->next_child->prev_child = child_task->prev_child;
254 if (parent->children == child_task)
256 if (child_task->next_child != child_task)
257 parent->children = child_task->next_child;
258 else
260 /* We access task->children in GOMP_taskwait
261 outside of the task lock mutex region, so
262 need a release barrier here to ensure memory
263 written by child_task->fn above is flushed
264 before the NULL is written. */
265 __atomic_store_n (&parent->children, NULL,
266 MEMMODEL_RELEASE);
267 if (parent->in_taskwait)
268 gomp_sem_post (&parent->taskwait_sem);
272 gomp_clear_parent (child_task->children);
273 to_free = child_task;
274 child_task = NULL;
275 team->task_running_count--;
276 if (--team->task_count == 0
277 && gomp_team_barrier_waiting_for_tasks (&team->barrier))
279 gomp_team_barrier_done (&team->barrier, state);
280 gomp_mutex_unlock (&team->task_lock);
281 gomp_team_barrier_wake (&team->barrier, 0);
282 gomp_mutex_lock (&team->task_lock);
288 /* Called when encountering a taskwait directive. */
290 void
291 GOMP_taskwait (void)
293 struct gomp_thread *thr = gomp_thread ();
294 struct gomp_team *team = thr->ts.team;
295 struct gomp_task *task = thr->task;
296 struct gomp_task *child_task = NULL;
297 struct gomp_task *to_free = NULL;
299 if (task == NULL
300 || __atomic_load_n (&task->children, MEMMODEL_ACQUIRE) == NULL)
301 return;
303 gomp_mutex_lock (&team->task_lock);
304 while (1)
306 if (task->children == NULL)
308 gomp_mutex_unlock (&team->task_lock);
309 if (to_free)
311 gomp_finish_task (to_free);
312 free (to_free);
314 return;
316 if (task->children->kind == GOMP_TASK_WAITING)
318 child_task = task->children;
319 task->children = child_task->next_child;
320 child_task->prev_queue->next_queue = child_task->next_queue;
321 child_task->next_queue->prev_queue = child_task->prev_queue;
322 if (team->task_queue == child_task)
324 if (child_task->next_queue != child_task)
325 team->task_queue = child_task->next_queue;
326 else
327 team->task_queue = NULL;
329 child_task->kind = GOMP_TASK_TIED;
330 team->task_running_count++;
331 if (team->task_count == team->task_running_count)
332 gomp_team_barrier_clear_task_pending (&team->barrier);
334 else
335 /* All tasks we are waiting for are already running
336 in other threads. Wait for them. */
337 task->in_taskwait = true;
338 gomp_mutex_unlock (&team->task_lock);
339 if (to_free)
341 gomp_finish_task (to_free);
342 free (to_free);
343 to_free = NULL;
345 if (child_task)
347 thr->task = child_task;
348 child_task->fn (child_task->fn_data);
349 thr->task = task;
351 else
353 gomp_sem_wait (&task->taskwait_sem);
354 task->in_taskwait = false;
355 return;
357 gomp_mutex_lock (&team->task_lock);
358 if (child_task)
360 child_task->prev_child->next_child = child_task->next_child;
361 child_task->next_child->prev_child = child_task->prev_child;
362 if (task->children == child_task)
364 if (child_task->next_child != child_task)
365 task->children = child_task->next_child;
366 else
367 task->children = NULL;
369 gomp_clear_parent (child_task->children);
370 to_free = child_task;
371 child_task = NULL;
372 team->task_count--;
373 team->task_running_count--;
378 /* Called when encountering a taskyield directive. */
380 void
381 GOMP_taskyield (void)
383 /* Nothing at the moment. */
387 omp_in_final (void)
389 struct gomp_thread *thr = gomp_thread ();
390 return thr->task && thr->task->final_task;
393 ialias (omp_in_final)