Factor uses of build_pairwise_scheduling.
[official-gcc/constexpr.git] / libgomp / sections.c
blob7acd441de53e908371e8647cc2c005b6c5a114d9
1 /* Copyright (C) 2005, 2007, 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 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 + 1;
38 ws->incr = 1;
39 ws->next = 1;
42 /* This routine is called when first encountering a sections construct
43 that is not bound directly to a parallel construct. The first thread
44 that arrives will create the work-share construct; subsequent threads
45 will see the construct exists and allocate work from it.
47 COUNT is the number of sections in this construct.
49 Returns the 1-based section number for this thread to perform, or 0 if
50 all work was assigned to other threads prior to this thread's arrival. */
52 unsigned
53 GOMP_sections_start (unsigned count)
55 struct gomp_thread *thr = gomp_thread ();
56 long s, e, ret;
58 if (gomp_work_share_start (false))
60 gomp_sections_init (thr->ts.work_share, count);
61 gomp_work_share_init_done ();
64 #ifdef HAVE_SYNC_BUILTINS
65 if (gomp_iter_dynamic_next (&s, &e))
66 ret = s;
67 else
68 ret = 0;
69 #else
70 gomp_mutex_lock (&thr->ts.work_share->lock);
71 if (gomp_iter_dynamic_next_locked (&s, &e))
72 ret = s;
73 else
74 ret = 0;
75 gomp_mutex_unlock (&thr->ts.work_share->lock);
76 #endif
78 return ret;
81 /* This routine is called when the thread completes processing of the
82 section currently assigned to it. If the work-share construct is
83 bound directly to a parallel construct, then the construct may have
84 been set up before the parallel. In which case, this may be the
85 first iteration for the thread.
87 Returns the 1-based section number for this thread to perform, or 0 if
88 all work was assigned to other threads prior to this thread's arrival. */
90 unsigned
91 GOMP_sections_next (void)
93 long s, e, ret;
95 #ifdef HAVE_SYNC_BUILTINS
96 if (gomp_iter_dynamic_next (&s, &e))
97 ret = s;
98 else
99 ret = 0;
100 #else
101 struct gomp_thread *thr = gomp_thread ();
103 gomp_mutex_lock (&thr->ts.work_share->lock);
104 if (gomp_iter_dynamic_next_locked (&s, &e))
105 ret = s;
106 else
107 ret = 0;
108 gomp_mutex_unlock (&thr->ts.work_share->lock);
109 #endif
111 return ret;
114 /* This routine pre-initializes a work-share construct to avoid one
115 synchronization once we get into the loop. */
117 void
118 GOMP_parallel_sections_start (void (*fn) (void *), void *data,
119 unsigned num_threads, unsigned count)
121 struct gomp_team *team;
123 num_threads = gomp_resolve_num_threads (num_threads, count);
124 team = gomp_new_team (num_threads);
125 gomp_sections_init (&team->work_shares[0], count);
126 gomp_team_start (fn, data, num_threads, team);
129 /* The GOMP_section_end* routines are called after the thread is told
130 that all sections are complete. This first version synchronizes
131 all threads; the nowait version does not. */
133 void
134 GOMP_sections_end (void)
136 gomp_work_share_end ();
139 void
140 GOMP_sections_end_nowait (void)
142 gomp_work_share_end_nowait ();