OSDN Git Service

* configure: Regenerate for new libtool.
[pf3gnuchains/gcc-fork.git] / libgomp / sections.c
1 /* Copyright (C) 2005, 2007, 2008 Free Software Foundation, Inc.
2    Contributed by Richard Henderson <rth@redhat.com>.
3
4    This file is part of the GNU OpenMP Library (libgomp).
5
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.
10
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.
15
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.  */
20
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.  */
27
28 /* This file handles the SECTIONS construct.  */
29
30 #include "libgomp.h"
31
32
33 /* Initialize the given work share construct from the given arguments.  */
34
35 static inline void
36 gomp_sections_init (struct gomp_work_share *ws, unsigned count)
37 {
38   ws->sched = GFS_DYNAMIC;
39   ws->chunk_size = 1;
40   ws->end = count + 1;
41   ws->incr = 1;
42   ws->next = 1;
43 }
44
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.
49
50    COUNT is the number of sections in this construct.
51
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.  */
54
55 unsigned
56 GOMP_sections_start (unsigned count)
57 {
58   struct gomp_thread *thr = gomp_thread ();
59   long s, e, ret;
60
61   if (gomp_work_share_start (false))
62     {
63       gomp_sections_init (thr->ts.work_share, count);
64       gomp_work_share_init_done ();
65     }
66
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
80
81   return ret;
82 }
83
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.
89
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.  */
92
93 unsigned
94 GOMP_sections_next (void)
95 {
96   long s, e, ret;
97
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 ();
105
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
113
114   return ret;
115 }
116
117 /* This routine pre-initializes a work-share construct to avoid one
118    synchronization once we get into the loop.  */
119
120 void
121 GOMP_parallel_sections_start (void (*fn) (void *), void *data,
122                               unsigned num_threads, unsigned count)
123 {
124   struct gomp_team *team;
125
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);
130 }
131
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.  */
135
136 void
137 GOMP_sections_end (void)
138 {
139   gomp_work_share_end ();
140 }
141
142 void
143 GOMP_sections_end_nowait (void)
144 {
145   gomp_work_share_end_nowait ();
146 }