Fix for PR39557
[official-gcc.git] / libgomp / sections.c
blob27625efec3e837e6e4f22862ebde2fb44e512c9d
1 /* Copyright (C) 2005, 2007, 2008 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 Lesser General Public License as published by
8 the Free Software Foundation; either version 2.1 of the License, or
9 (at your option) 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 Lesser General Public License for
14 more details.
16 You should have received a copy of the GNU Lesser General Public License
17 along with libgomp; see the file COPYING.LIB. If not, write to the
18 Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
19 MA 02110-1301, USA. */
21 /* As a special exception, if you link this library with other files, some
22 of which are compiled with GCC, to produce an executable, this library
23 does not by itself cause the resulting executable to be covered by the
24 GNU General Public License. This exception does not however invalidate
25 any other reasons why the executable file might be covered by the GNU
26 General Public License. */
28 /* This file handles the SECTIONS construct. */
30 #include "libgomp.h"
33 /* Initialize the given work share construct from the given arguments. */
35 static inline void
36 gomp_sections_init (struct gomp_work_share *ws, unsigned count)
38 ws->sched = GFS_DYNAMIC;
39 ws->chunk_size = 1;
40 ws->end = count + 1;
41 ws->incr = 1;
42 ws->next = 1;
45 /* This routine is called when first encountering a sections construct
46 that is not bound directly to a parallel construct. The first thread
47 that arrives will create the work-share construct; subsequent threads
48 will see the construct exists and allocate work from it.
50 COUNT is the number of sections in this construct.
52 Returns the 1-based section number for this thread to perform, or 0 if
53 all work was assigned to other threads prior to this thread's arrival. */
55 unsigned
56 GOMP_sections_start (unsigned count)
58 struct gomp_thread *thr = gomp_thread ();
59 long s, e, ret;
61 if (gomp_work_share_start (false))
63 gomp_sections_init (thr->ts.work_share, count);
64 gomp_work_share_init_done ();
67 #ifdef HAVE_SYNC_BUILTINS
68 if (gomp_iter_dynamic_next (&s, &e))
69 ret = s;
70 else
71 ret = 0;
72 #else
73 gomp_mutex_lock (&thr->ts.work_share->lock);
74 if (gomp_iter_dynamic_next_locked (&s, &e))
75 ret = s;
76 else
77 ret = 0;
78 gomp_mutex_unlock (&thr->ts.work_share->lock);
79 #endif
81 return ret;
84 /* This routine is called when the thread completes processing of the
85 section currently assigned to it. If the work-share construct is
86 bound directly to a parallel construct, then the construct may have
87 been set up before the parallel. In which case, this may be the
88 first iteration for the thread.
90 Returns the 1-based section number for this thread to perform, or 0 if
91 all work was assigned to other threads prior to this thread's arrival. */
93 unsigned
94 GOMP_sections_next (void)
96 long s, e, ret;
98 #ifdef HAVE_SYNC_BUILTINS
99 if (gomp_iter_dynamic_next (&s, &e))
100 ret = s;
101 else
102 ret = 0;
103 #else
104 struct gomp_thread *thr = gomp_thread ();
106 gomp_mutex_lock (&thr->ts.work_share->lock);
107 if (gomp_iter_dynamic_next_locked (&s, &e))
108 ret = s;
109 else
110 ret = 0;
111 gomp_mutex_unlock (&thr->ts.work_share->lock);
112 #endif
114 return ret;
117 /* This routine pre-initializes a work-share construct to avoid one
118 synchronization once we get into the loop. */
120 void
121 GOMP_parallel_sections_start (void (*fn) (void *), void *data,
122 unsigned num_threads, unsigned count)
124 struct gomp_team *team;
126 num_threads = gomp_resolve_num_threads (num_threads, count);
127 team = gomp_new_team (num_threads);
128 gomp_sections_init (&team->work_shares[0], count);
129 gomp_team_start (fn, data, num_threads, team);
132 /* The GOMP_section_end* routines are called after the thread is told
133 that all sections are complete. This first version synchronizes
134 all threads; the nowait version does not. */
136 void
137 GOMP_sections_end (void)
139 gomp_work_share_end ();
142 void
143 GOMP_sections_end_nowait (void)
145 gomp_work_share_end_nowait ();