2012-05-01 François Dumont <fdumont@gcc.gnu.org>
[official-gcc.git] / libgomp / single.c
blob8c5ade11f47f7e4bc67bc5d5a1b957e286f19110
1 /* Copyright (C) 2005, 2008, 2009 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 SINGLE construct. */
27 #include "libgomp.h"
30 /* This routine is called when first encountering a SINGLE construct that
31 doesn't have a COPYPRIVATE clause. Returns true if this is the thread
32 that should execute the clause. */
34 bool
35 GOMP_single_start (void)
37 #ifdef HAVE_SYNC_BUILTINS
38 struct gomp_thread *thr = gomp_thread ();
39 struct gomp_team *team = thr->ts.team;
40 unsigned long single_count;
42 if (__builtin_expect (team == NULL, 0))
43 return true;
45 single_count = thr->ts.single_count++;
46 return __sync_bool_compare_and_swap (&team->single_count, single_count,
47 single_count + 1L);
48 #else
49 bool ret = gomp_work_share_start (false);
50 if (ret)
51 gomp_work_share_init_done ();
52 gomp_work_share_end_nowait ();
53 return ret;
54 #endif
57 /* This routine is called when first encountering a SINGLE construct that
58 does have a COPYPRIVATE clause. Returns NULL if this is the thread
59 that should execute the clause; otherwise the return value is pointer
60 given to GOMP_single_copy_end by the thread that did execute the clause. */
62 void *
63 GOMP_single_copy_start (void)
65 struct gomp_thread *thr = gomp_thread ();
67 bool first;
68 void *ret;
70 first = gomp_work_share_start (false);
72 if (first)
74 gomp_work_share_init_done ();
75 ret = NULL;
77 else
79 gomp_team_barrier_wait (&thr->ts.team->barrier);
81 ret = thr->ts.work_share->copyprivate;
82 gomp_work_share_end_nowait ();
85 return ret;
88 /* This routine is called when the thread that entered a SINGLE construct
89 with a COPYPRIVATE clause gets to the end of the construct. */
91 void
92 GOMP_single_copy_end (void *data)
94 struct gomp_thread *thr = gomp_thread ();
95 struct gomp_team *team = thr->ts.team;
97 if (team != NULL)
99 thr->ts.work_share->copyprivate = data;
100 gomp_team_barrier_wait (&team->barrier);
103 gomp_work_share_end_nowait ();