[gomp] Thread pool management
[official-gcc.git] / libgomp / config / rtems / proc.c
blobe879a9d41bb1ac34c70146e835a225faa902b1fe
1 /* Copyright (C) 2015 Free Software Foundation, Inc.
2 Contributed by Sebastian Huber <sebastian.huber@embedded-brains.de>.
4 This file is part of the GNU Offloading and Multi Processing Library
5 (libgomp).
7 Libgomp is free software; you can redistribute it and/or modify it
8 under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3, or (at your option)
10 any later version.
12 Libgomp is distributed in the hope that it will be useful, but WITHOUT ANY
13 WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
14 FOR A PARTICULAR PURPOSE. See the GNU General Public License for
15 more details.
17 Under Section 7 of GPL version 3, you are granted additional
18 permissions described in the GCC Runtime Library Exception, version
19 3.1, as published by the Free Software Foundation.
21 You should have received a copy of the GNU General Public License and
22 a copy of the GCC Runtime Library Exception along with this program;
23 see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
24 <http://www.gnu.org/licenses/>. */
26 /* This file contains RTEMS specific routines related to counting
27 online processors and dynamic load balancing. */
29 #include "libgomp.h"
30 #include "pool.h"
31 #include <errno.h>
32 #include <stdlib.h>
33 #include <string.h>
34 #include <unistd.h>
36 struct gomp_thread_pool_reservoir **gomp_thread_pool_reservoirs;
38 __thread struct gomp_tls_rtems_data gomp_tls_rtems_data;
40 static void
41 allocate_thread_pool_reservoirs (void)
43 struct gomp_thread_pool_reservoir **reservoirs;
44 size_t size = _Sched_Count () * sizeof (*reservoirs);
45 reservoirs = gomp_malloc (size);
46 gomp_thread_pool_reservoirs = reservoirs;
47 memset (reservoirs, 0, size);
50 static void
51 allocate_thread_pool_reservoir (unsigned long count, unsigned long scheduler)
53 struct gomp_thread_pool_reservoir *res;
54 struct gomp_thread_pool *pools;
55 unsigned long i;
56 size_t size;
58 res = gomp_thread_pool_reservoirs[scheduler];
59 if (res != NULL)
60 gomp_fatal ("Multiple thread pool reservoir initialization");
61 size = sizeof (*res) + count * (sizeof(pools) + sizeof(*pools));
62 pools = gomp_malloc (size);
63 memset (pools, 0, size);
64 res = (struct gomp_thread_pool_reservoir *) (pools + count);
65 res->index = count;
66 gomp_sem_init (&res->available, count);
67 gomp_mutex_init (&res->lock);
68 for (i = 0; i < count; ++i)
69 res->pools[i] = &pools[i];
70 gomp_thread_pool_reservoirs[scheduler] = res;
73 static char *
74 parse_thread_pools (char *env, unsigned long *count, unsigned long *scheduler)
76 size_t len;
77 int i;
79 if (*env == ':')
80 ++env;
82 errno = 0;
83 *count = strtoul (env, &env, 10);
84 if (errno != 0)
85 gomp_fatal ("Invalid thread pool count");
87 if (*env != '@')
88 gomp_fatal ("Invalid thread pool scheduler prefix");
89 ++env;
91 len = 0;
92 while (env[len] != '\0' && env[len] != ':')
93 ++len;
94 i = _Sched_Name_to_index (env, len);
95 if (i < 0)
96 gomp_fatal ("Invalid thread pool scheduler");
97 *scheduler = i;
98 env += len;
100 return env;
103 static void
104 init_thread_pool_reservoirs (void)
106 char *env = getenv ("GOMP_RTEMS_THREAD_POOLS");
107 if (env != NULL)
109 allocate_thread_pool_reservoirs ();
110 while (*env != '\0')
112 unsigned long count;
113 unsigned long scheduler;
114 env = parse_thread_pools (env, &count, &scheduler);
115 allocate_thread_pool_reservoir (count, scheduler);
120 void
121 gomp_init_num_threads (void)
123 gomp_global_icv.nthreads_var = omp_get_num_procs();
124 init_thread_pool_reservoirs ();
127 unsigned
128 gomp_dynamic_max_threads (void)
130 unsigned n_onln = (unsigned) omp_get_num_procs();
131 unsigned nthreads_var = gomp_icv (false)->nthreads_var;
133 if (n_onln > nthreads_var)
134 return nthreads_var;
135 else
136 return n_onln;
140 omp_get_num_procs (void)
142 return sysconf (_SC_NPROCESSORS_ONLN);
145 ialias (omp_get_num_procs)