PR rtl-optimization/59278
[official-gcc.git] / libgomp / sections.c
blobfb746c7613a4022f403892de42579df3064007a7
1 /* Copyright (C) 2005-2014 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 SECTIONS construct. */
27 #include "libgomp.h"
30 /* Initialize the given work share construct from the given arguments. */
32 static inline void
33 gomp_sections_init (struct gomp_work_share *ws, unsigned count)
35 ws->sched = GFS_DYNAMIC;
36 ws->chunk_size = 1;
37 ws->end = count + 1L;
38 ws->incr = 1;
39 ws->next = 1;
40 #ifdef HAVE_SYNC_BUILTINS
41 /* Prepare things to make each iteration faster. */
42 if (sizeof (long) > sizeof (unsigned))
43 ws->mode = 1;
44 else
46 struct gomp_thread *thr = gomp_thread ();
47 struct gomp_team *team = thr->ts.team;
48 long nthreads = team ? team->nthreads : 1;
50 ws->mode = ((nthreads | ws->end)
51 < 1UL << (sizeof (long) * __CHAR_BIT__ / 2 - 1));
53 #else
54 ws->mode = 0;
55 #endif
58 /* This routine is called when first encountering a sections construct
59 that is not bound directly to a parallel construct. The first thread
60 that arrives will create the work-share construct; subsequent threads
61 will see the construct exists and allocate work from it.
63 COUNT is the number of sections in this construct.
65 Returns the 1-based section number for this thread to perform, or 0 if
66 all work was assigned to other threads prior to this thread's arrival. */
68 unsigned
69 GOMP_sections_start (unsigned count)
71 struct gomp_thread *thr = gomp_thread ();
72 long s, e, ret;
74 if (gomp_work_share_start (false))
76 gomp_sections_init (thr->ts.work_share, count);
77 gomp_work_share_init_done ();
80 #ifdef HAVE_SYNC_BUILTINS
81 if (gomp_iter_dynamic_next (&s, &e))
82 ret = s;
83 else
84 ret = 0;
85 #else
86 gomp_mutex_lock (&thr->ts.work_share->lock);
87 if (gomp_iter_dynamic_next_locked (&s, &e))
88 ret = s;
89 else
90 ret = 0;
91 gomp_mutex_unlock (&thr->ts.work_share->lock);
92 #endif
94 return ret;
97 /* This routine is called when the thread completes processing of the
98 section currently assigned to it. If the work-share construct is
99 bound directly to a parallel construct, then the construct may have
100 been set up before the parallel. In which case, this may be the
101 first iteration for the thread.
103 Returns the 1-based section number for this thread to perform, or 0 if
104 all work was assigned to other threads prior to this thread's arrival. */
106 unsigned
107 GOMP_sections_next (void)
109 long s, e, ret;
111 #ifdef HAVE_SYNC_BUILTINS
112 if (gomp_iter_dynamic_next (&s, &e))
113 ret = s;
114 else
115 ret = 0;
116 #else
117 struct gomp_thread *thr = gomp_thread ();
119 gomp_mutex_lock (&thr->ts.work_share->lock);
120 if (gomp_iter_dynamic_next_locked (&s, &e))
121 ret = s;
122 else
123 ret = 0;
124 gomp_mutex_unlock (&thr->ts.work_share->lock);
125 #endif
127 return ret;
130 /* This routine pre-initializes a work-share construct to avoid one
131 synchronization once we get into the loop. */
133 void
134 GOMP_parallel_sections_start (void (*fn) (void *), void *data,
135 unsigned num_threads, unsigned count)
137 struct gomp_team *team;
139 num_threads = gomp_resolve_num_threads (num_threads, count);
140 team = gomp_new_team (num_threads);
141 gomp_sections_init (&team->work_shares[0], count);
142 gomp_team_start (fn, data, num_threads, 0, team);
145 ialias_redirect (GOMP_parallel_end)
147 void
148 GOMP_parallel_sections (void (*fn) (void *), void *data,
149 unsigned num_threads, unsigned count, unsigned flags)
151 struct gomp_team *team;
153 num_threads = gomp_resolve_num_threads (num_threads, count);
154 team = gomp_new_team (num_threads);
155 gomp_sections_init (&team->work_shares[0], count);
156 gomp_team_start (fn, data, num_threads, flags, team);
157 fn (data);
158 GOMP_parallel_end ();
161 /* The GOMP_section_end* routines are called after the thread is told
162 that all sections are complete. The first two versions synchronize
163 all threads; the nowait version does not. */
165 void
166 GOMP_sections_end (void)
168 gomp_work_share_end ();
171 bool
172 GOMP_sections_end_cancel (void)
174 return gomp_work_share_end_cancel ();
177 void
178 GOMP_sections_end_nowait (void)
180 gomp_work_share_end_nowait ();