2006-03-29 Paul Brook <paul@codesourcery.com>
[official-gcc.git] / libgomp / libgomp.h
blob927dea5b6b7096d8316e02b2f7a18707dae15bf8
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 contains data types and function declarations that are not
29 part of the official OpenMP user interface. There are declarations
30 in here that are part of the GNU OpenMP ABI, in that the compiler is
31 required to know about them and use them.
33 The convention is that the all caps prefix "GOMP" is used group items
34 that are part of the external ABI, and the lower case prefix "gomp"
35 is used group items that are completely private to the library. */
37 #ifndef LIBGOMP_H
38 #define LIBGOMP_H 1
40 #include "config.h"
41 #include "gstdint.h"
43 #include <pthread.h>
44 #include <stdbool.h>
46 #ifdef HAVE_ATTRIBUTE_VISIBILITY
47 # pragma GCC visibility push(hidden)
48 #endif
50 #include "sem.h"
51 #include "mutex.h"
52 #include "bar.h"
55 /* This structure contains the data to control one work-sharing construct,
56 either a LOOP (FOR/DO) or a SECTIONS. */
58 enum gomp_schedule_type
60 GFS_STATIC,
61 GFS_DYNAMIC,
62 GFS_GUIDED,
63 GFS_RUNTIME
66 struct gomp_work_share
68 /* This member records the SCHEDULE clause to be used for this construct.
69 The user specification of "runtime" will already have been resolved.
70 If this is a SECTIONS construct, this value will always be DYNAMIC. */
71 enum gomp_schedule_type sched;
73 /* This is the chunk_size argument to the SCHEDULE clause. */
74 long chunk_size;
76 /* This is the iteration end point. If this is a SECTIONS construct,
77 this is the number of contained sections. */
78 long end;
80 /* This is the iteration step. If this is a SECTIONS construct, this
81 is always 1. */
82 long incr;
84 /* This lock protects the update of the following members. */
85 gomp_mutex_t lock;
87 union {
88 /* This is the next iteration value to be allocated. In the case of
89 GFS_STATIC loops, this the iteration start point and never changes. */
90 long next;
92 /* This is the returned data structure for SINGLE COPYPRIVATE. */
93 void *copyprivate;
96 /* This is the count of the number of threads that have exited the work
97 share construct. If the construct was marked nowait, they have moved on
98 to other work; otherwise they're blocked on a barrier. The last member
99 of the team to exit the work share construct must deallocate it. */
100 unsigned threads_completed;
102 /* This is the index into the circular queue ordered_team_ids of the
103 current thread that's allowed into the ordered reason. */
104 unsigned ordered_cur;
106 /* This is the number of threads that have registered themselves in
107 the circular queue ordered_team_ids. */
108 unsigned ordered_num_used;
110 /* This is the team_id of the currently acknoledged owner of the ordered
111 section, or -1u if the ordered section has not been acknowledged by
112 any thread. This is distinguished from the thread that is *allowed*
113 to take the section next. */
114 unsigned ordered_owner;
116 /* This is a circular queue that details which threads will be allowed
117 into the ordered region and in which order. When a thread allocates
118 iterations on which it is going to work, it also registers itself at
119 the end of the array. When a thread reaches the ordered region, it
120 checks to see if it is the one at the head of the queue. If not, it
121 blocks on its RELEASE semaphore. */
122 unsigned ordered_team_ids[];
125 /* This structure contains all of the thread-local data associated with
126 a thread team. This is the data that must be saved when a thread
127 encounters a nested PARALLEL construct. */
129 struct gomp_team_state
131 /* This is the team of which the thread is currently a member. */
132 struct gomp_team *team;
134 /* This is the work share construct which this thread is currently
135 processing. Recall that with NOWAIT, not all threads may be
136 processing the same construct. This value is NULL when there
137 is no construct being processed. */
138 struct gomp_work_share *work_share;
140 /* This is the ID of this thread within the team. This value is
141 guaranteed to be between 0 and N-1, where N is the number of
142 threads in the team. */
143 unsigned team_id;
145 /* The work share "generation" is a number that increases by one for
146 each work share construct encountered in the dynamic flow of the
147 program. It is used to find the control data for the work share
148 when encountering it for the first time. This particular number
149 reflects the generation of the work_share member of this struct. */
150 unsigned work_share_generation;
152 /* For GFS_RUNTIME loops that resolved to GFS_STATIC, this is the
153 trip number through the loop. So first time a particular loop
154 is encountered this number is 0, the second time through the loop
155 is 1, etc. This is unused when the compiler knows in advance that
156 the loop is statically scheduled. */
157 unsigned long static_trip;
160 /* This structure describes a "team" of threads. These are the threads
161 that are spawned by a PARALLEL constructs, as well as the work sharing
162 constructs that the team encounters. */
164 struct gomp_team
166 /* This lock protects access to the following work shares data structures. */
167 gomp_mutex_t work_share_lock;
169 /* This is a dynamically sized array containing pointers to the control
170 structs for all "live" work share constructs. Here "live" means that
171 the construct has been encountered by at least one thread, and not
172 completed by all threads. */
173 struct gomp_work_share **work_shares;
175 /* The work_shares array is indexed by "generation & generation_mask".
176 The mask will be 2**N - 1, where 2**N is the size of the array. */
177 unsigned generation_mask;
179 /* These two values define the bounds of the elements of the work_shares
180 array that are currently in use. */
181 unsigned oldest_live_gen;
182 unsigned num_live_gen;
184 /* This is the number of threads in the current team. */
185 unsigned nthreads;
187 /* This is the saved team state that applied to a master thread before
188 the current thread was created. */
189 struct gomp_team_state prev_ts;
191 /* This barrier is used for most synchronization of the team. */
192 gomp_barrier_t barrier;
194 /* This semaphore should be used by the master thread instead of its
195 "native" semaphore in the thread structure. Required for nested
196 parallels, as the master is a member of two teams. */
197 gomp_sem_t master_release;
199 /* This array contains pointers to the release semaphore of the threads
200 in the team. */
201 gomp_sem_t *ordered_release[];
204 /* This structure contains all data that is private to libgomp and is
205 allocated per thread. */
207 struct gomp_thread
209 /* This is the function that the thread should run upon launch. */
210 void (*fn) (void *data);
211 void *data;
213 /* This is the current team state for this thread. The ts.team member
214 is NULL only if the thread is idle. */
215 struct gomp_team_state ts;
217 /* This semaphore is used for ordered loops. */
218 gomp_sem_t release;
221 /* ... and here is that TLS data. */
223 #ifdef HAVE_TLS
224 extern __thread struct gomp_thread gomp_tls_data;
225 static inline struct gomp_thread *gomp_thread (void)
227 return &gomp_tls_data;
229 #else
230 extern pthread_key_t gomp_tls_key;
231 static inline struct gomp_thread *gomp_thread (void)
233 return pthread_getspecific (gomp_tls_key);
235 #endif
237 /* These are the OpenMP 2.5 internal control variables described in
238 section 2.3. At least those that correspond to environment variables. */
240 extern unsigned gomp_nthreads_var;
241 extern bool gomp_dyn_var;
242 extern bool gomp_nest_var;
243 extern enum gomp_schedule_type gomp_run_sched_var;
244 extern unsigned gomp_run_sched_chunk;
246 /* Function prototypes. */
248 /* alloc.c */
250 extern void *gomp_malloc (size_t) __attribute__((malloc));
251 extern void *gomp_malloc_cleared (size_t) __attribute__((malloc));
252 extern void *gomp_realloc (void *, size_t);
254 /* Avoid conflicting prototypes of alloca() in system headers by using
255 GCC's builtin alloca(). */
256 #define gomp_alloca(x) __builtin_alloca(x)
258 /* error.c */
260 extern void gomp_error (const char *, ...)
261 __attribute__((format (printf, 1, 2)));
262 extern void gomp_fatal (const char *, ...)
263 __attribute__((noreturn, format (printf, 1, 2)));
265 /* iter.c */
267 extern int gomp_iter_static_next (long *, long *);
268 extern bool gomp_iter_dynamic_next_locked (long *, long *);
269 extern bool gomp_iter_guided_next_locked (long *, long *);
271 #ifdef HAVE_SYNC_BUILTINS
272 extern bool gomp_iter_dynamic_next (long *, long *);
273 extern bool gomp_iter_guided_next (long *, long *);
274 #endif
276 /* ordered.c */
278 extern void gomp_ordered_first (void);
279 extern void gomp_ordered_last (void);
280 extern void gomp_ordered_next (void);
281 extern void gomp_ordered_static_init (void);
282 extern void gomp_ordered_static_next (void);
283 extern void gomp_ordered_sync (void);
285 /* parallel.c */
287 extern unsigned gomp_resolve_num_threads (unsigned);
289 /* proc.c (in config/) */
291 extern void gomp_init_num_threads (void);
292 extern unsigned gomp_dynamic_max_threads (void);
294 /* team.c */
296 extern void gomp_team_start (void (*) (void *), void *, unsigned,
297 struct gomp_work_share *);
298 extern void gomp_team_end (void);
300 /* work.c */
302 extern struct gomp_work_share * gomp_new_work_share (bool, unsigned);
303 extern bool gomp_work_share_start (bool);
304 extern void gomp_work_share_end (void);
305 extern void gomp_work_share_end_nowait (void);
307 #ifdef HAVE_ATTRIBUTE_VISIBILITY
308 # pragma GCC visibility pop
309 #endif
311 /* Now that we're back to default visibility, include the globals. */
312 #include "libgomp_g.h"
314 /* Include omp.h by parts. */
315 #include "omp-lock.h"
316 #define _LIBGOMP_OMP_LOCK_DEFINED 1
317 #include "omp.h.in"
319 #ifdef HAVE_ATTRIBUTE_VISIBILITY
320 # define attribute_hidden __attribute__ ((visibility ("hidden")))
321 #else
322 # define attribute_hidden
323 #endif
325 #ifdef HAVE_ATTRIBUTE_ALIAS
326 # define ialias(fn) \
327 extern __typeof (fn) gomp_ialias_##fn \
328 __attribute__ ((alias (#fn))) attribute_hidden;
329 #else
330 # define ialias(fn)
331 #endif
333 #endif /* LIBGOMP_H */