2006-08-15 Paolo Carlini <pcarlini@suse.de>
[official-gcc.git] / libgomp / env.c
blobc86ebc6d8559c2b27066624c255c5faa90ca8f93
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 defines the OpenMP internal control variables, and arranges
29 for them to be initialized from environment variables at startup. */
31 #include "libgomp.h"
32 #include "libgomp_f.h"
33 #include <stdlib.h>
34 #include <string.h>
35 #include <limits.h>
36 #include <errno.h>
39 unsigned long gomp_nthreads_var = 1;
40 bool gomp_dyn_var = false;
41 bool gomp_nest_var = false;
42 enum gomp_schedule_type gomp_run_sched_var = GFS_DYNAMIC;
43 unsigned long gomp_run_sched_chunk = 1;
45 /* Parse the OMP_SCHEDULE environment variable. */
47 static void
48 parse_schedule (void)
50 char *env, *end;
52 env = getenv ("OMP_SCHEDULE");
53 if (env == NULL)
54 return;
56 if (strncmp (env, "static", 6) == 0)
58 gomp_run_sched_var = GFS_STATIC;
59 env += 6;
61 else if (strncmp (env, "dynamic", 7) == 0)
63 gomp_run_sched_var = GFS_DYNAMIC;
64 env += 7;
66 else if (strncmp (env, "guided", 6) == 0)
68 gomp_run_sched_var = GFS_GUIDED;
69 env += 6;
71 else
72 goto unknown;
74 if (*env == '\0')
75 return;
76 if (*env != ' ' && *env != ',')
77 goto unknown;
78 while (*env == ' ')
79 env++;
80 if (*env == '\0')
81 return;
82 if (*env != ',')
83 goto unknown;
84 if (*++env == '\0')
85 goto invalid;
87 gomp_run_sched_chunk = strtoul (env, &end, 10);
88 if (*end != '\0')
89 goto invalid;
90 return;
92 unknown:
93 gomp_error ("Unknown value for environment variable OMP_SCHEDULE");
94 return;
96 invalid:
97 gomp_error ("Invalid value for chunk size in "
98 "environment variable OMP_SCHEDULE");
99 gomp_run_sched_chunk = 1;
100 return;
103 /* Parse an unsigned long environment varible. Return true if one was
104 present and it was successfully parsed. */
106 static bool
107 parse_unsigned_long (const char *name, unsigned long *pvalue)
109 char *env, *end;
110 unsigned long value;
112 env = getenv (name);
113 if (env == NULL)
114 return false;
116 if (*env == '\0')
117 goto invalid;
119 value = strtoul (env, &end, 10);
120 if (*end != '\0')
121 goto invalid;
123 *pvalue = value;
124 return true;
126 invalid:
127 gomp_error ("Invalid value for environment variable %s", name);
128 return false;
131 /* Parse a boolean value for environment variable NAME and store the
132 result in VALUE. */
134 static void
135 parse_boolean (const char *name, bool *value)
137 const char *env;
139 env = getenv (name);
140 if (env == NULL)
141 return;
143 if (strcmp (env, "true") == 0)
144 *value = true;
145 else if (strcmp (env, "false") == 0)
146 *value = false;
147 else
148 gomp_error ("Invalid value for environment variable %s", name);
151 static void __attribute__((constructor))
152 initialize_env (void)
154 unsigned long stacksize;
156 /* Do a compile time check that mkomp_h.pl did good job. */
157 omp_check_defines ();
159 parse_schedule ();
160 parse_boolean ("OMP_DYNAMIC", &gomp_dyn_var);
161 parse_boolean ("OMP_NESTED", &gomp_nest_var);
162 if (!parse_unsigned_long ("OMP_NUM_THREADS", &gomp_nthreads_var))
163 gomp_init_num_threads ();
165 /* Not strictly environment related, but ordering constructors is tricky. */
166 pthread_attr_init (&gomp_thread_attr);
167 pthread_attr_setdetachstate (&gomp_thread_attr, PTHREAD_CREATE_DETACHED);
169 if (parse_unsigned_long ("GOMP_STACKSIZE", &stacksize))
171 int err;
173 stacksize *= 1024;
174 err = pthread_attr_setstacksize (&gomp_thread_attr, stacksize);
176 #ifdef PTHREAD_STACK_MIN
177 if (err == EINVAL)
179 if (stacksize < PTHREAD_STACK_MIN)
180 gomp_error ("Stack size less than minimum of %luk",
181 PTHREAD_STACK_MIN / 1024ul
182 + (PTHREAD_STACK_MIN % 1024 != 0));
183 else
184 gomp_error ("Stack size larger than system limit");
186 else
187 #endif
188 if (err != 0)
189 gomp_error ("Stack size change failed: %s", strerror (err));
194 /* The public OpenMP API routines that access these variables. */
196 void
197 omp_set_num_threads (int n)
199 gomp_nthreads_var = n;
202 void
203 omp_set_dynamic (int val)
205 gomp_dyn_var = val;
209 omp_get_dynamic (void)
211 return gomp_dyn_var;
214 void
215 omp_set_nested (int val)
217 gomp_nest_var = val;
221 omp_get_nested (void)
223 return gomp_nest_var;
226 ialias (omp_set_dynamic)
227 ialias (omp_set_nested)
228 ialias (omp_set_num_threads)
229 ialias (omp_get_dynamic)
230 ialias (omp_get_nested)