PR sanitizer/85029
[official-gcc.git] / libgomp / single.c
blob24a7780ad93acf9f6dff61bbcf605b7e693de4a2
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
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 file handles the SINGLE construct. */
28 #include "libgomp.h"
31 /* This routine is called when first encountering a SINGLE construct that
32 doesn't have a COPYPRIVATE clause. Returns true if this is the thread
33 that should execute the clause. */
35 bool
36 GOMP_single_start (void)
38 #ifdef HAVE_SYNC_BUILTINS
39 struct gomp_thread *thr = gomp_thread ();
40 struct gomp_team *team = thr->ts.team;
41 unsigned long single_count;
43 if (__builtin_expect (team == NULL, 0))
44 return true;
46 single_count = thr->ts.single_count++;
47 return __sync_bool_compare_and_swap (&team->single_count, single_count,
48 single_count + 1L);
49 #else
50 bool ret = gomp_work_share_start (false);
51 if (ret)
52 gomp_work_share_init_done ();
53 gomp_work_share_end_nowait ();
54 return ret;
55 #endif
58 /* This routine is called when first encountering a SINGLE construct that
59 does have a COPYPRIVATE clause. Returns NULL if this is the thread
60 that should execute the clause; otherwise the return value is pointer
61 given to GOMP_single_copy_end by the thread that did execute the clause. */
63 void *
64 GOMP_single_copy_start (void)
66 struct gomp_thread *thr = gomp_thread ();
68 bool first;
69 void *ret;
71 first = gomp_work_share_start (false);
73 if (first)
75 gomp_work_share_init_done ();
76 ret = NULL;
78 else
80 gomp_team_barrier_wait (&thr->ts.team->barrier);
82 ret = thr->ts.work_share->copyprivate;
83 gomp_work_share_end_nowait ();
86 return ret;
89 /* This routine is called when the thread that entered a SINGLE construct
90 with a COPYPRIVATE clause gets to the end of the construct. */
92 void
93 GOMP_single_copy_end (void *data)
95 struct gomp_thread *thr = gomp_thread ();
96 struct gomp_team *team = thr->ts.team;
98 if (team != NULL)
100 thr->ts.work_share->copyprivate = data;
101 gomp_team_barrier_wait (&team->barrier);
104 gomp_work_share_end_nowait ();