Merge from mainline
[official-gcc.git] / libgomp / sections.c
blobde0acd843eaf942fab344aa7c40ec82d5cbcf43c
1 /* Copyright (C) 2005 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))
62 gomp_sections_init (thr->ts.work_share, count);
64 if (gomp_iter_dynamic_next_locked (&s, &e))
65 ret = s;
66 else
67 ret = 0;
69 gomp_mutex_unlock (&thr->ts.work_share->lock);
71 return ret;
74 /* This routine is called when the thread completes processing of the
75 section currently assigned to it. If the work-share construct is
76 bound directly to a parallel construct, then the construct may have
77 been set up before the parallel. In which case, this may be the
78 first iteration for the thread.
80 Returns the 1-based section number for this thread to perform, or 0 if
81 all work was assigned to other threads prior to this thread's arrival. */
83 unsigned
84 GOMP_sections_next (void)
86 struct gomp_thread *thr = gomp_thread ();
87 long s, e, ret;
89 gomp_mutex_lock (&thr->ts.work_share->lock);
90 if (gomp_iter_dynamic_next_locked (&s, &e))
91 ret = s;
92 else
93 ret = 0;
94 gomp_mutex_unlock (&thr->ts.work_share->lock);
96 return ret;
99 /* This routine pre-initializes a work-share construct to avoid one
100 synchronization once we get into the loop. */
102 void
103 GOMP_parallel_sections_start (void (*fn) (void *), void *data,
104 unsigned num_threads, unsigned count)
106 struct gomp_work_share *ws;
108 num_threads = gomp_resolve_num_threads (num_threads);
109 if (num_threads > count)
110 num_threads = count;
112 ws = gomp_new_work_share (false, num_threads);
113 gomp_sections_init (ws, count);
114 gomp_team_start (fn, data, num_threads, ws);
117 /* The GOMP_section_end* routines are called after the thread is told
118 that all sections are complete. This first version synchronizes
119 all threads; the nowait version does not. */
121 void
122 GOMP_sections_end (void)
124 gomp_work_share_end ();
127 void
128 GOMP_sections_end_nowait (void)
130 gomp_work_share_end_nowait ();