[gomp] Thread pool management
[official-gcc.git] / libgomp / config / rtems / pool.h
blob0ab68d949f2b26f630e06b19cd16fbf4fced63b0
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 is the RTEMS implementation of the thread pool management
27 for libgomp. This type is private to the library. */
29 #ifndef GOMP_POOL_H
30 #define GOMP_POOL_H 1
32 #include "libgomp.h"
33 #include <sys/lock.h>
34 #include <string.h>
36 /* For each scheduler instance there may be a thread pool reservoir
37 to limit the number of thread pools used by the OpenMP master threads of this
38 scheduler instance. The reservoirs are configured via the
39 GOMP_RTEMS_THREAD_POOLS environment variable. */
40 struct gomp_thread_pool_reservoir {
41 gomp_sem_t available;
42 gomp_mutex_t lock;
43 size_t index;
44 struct gomp_thread_pool *pools[];
47 struct gomp_tls_rtems_data {
48 struct gomp_thread_pool_reservoir *thread_pool_reservoir;
51 extern struct gomp_thread_pool_reservoir **gomp_thread_pool_reservoirs;
53 extern __thread struct gomp_tls_rtems_data gomp_tls_rtems_data;
55 static inline struct gomp_thread_pool_reservoir *
56 gomp_get_thread_pool_reservoir (void)
58 struct gomp_thread_pool_reservoir *res =
59 gomp_tls_rtems_data.thread_pool_reservoir;
61 if (res == NULL && gomp_thread_pool_reservoirs != NULL)
63 struct gomp_thread *thr = gomp_thread ();
64 thr->thread_pool = gomp_malloc_cleared (sizeof (*thr->thread_pool));
65 res = gomp_thread_pool_reservoirs[_Sched_Index ()];
66 gomp_tls_rtems_data.thread_pool_reservoir = res;
69 return res;
72 static inline struct gomp_thread_pool *
73 gomp_get_own_thread_pool (struct gomp_thread *thr, unsigned nthreads)
75 struct gomp_thread_pool *pool = thr->thread_pool;
76 if (__builtin_expect (pool == NULL, 0))
78 pool = gomp_malloc_cleared (sizeof (*pool));
79 pool->threads_busy = nthreads;
80 thr->thread_pool = pool;
82 return pool;
85 static inline struct gomp_thread_pool *
86 gomp_get_thread_pool (struct gomp_thread *thr, unsigned nthreads)
88 struct gomp_thread_pool *pool;
90 if (__builtin_expect (thr->thread_pool == NULL, 0))
91 pthread_setspecific (gomp_thread_destructor, thr);
93 if (nthreads != 1)
95 struct gomp_thread_pool_reservoir *res =
96 gomp_get_thread_pool_reservoir ();
97 if (res != NULL)
99 gomp_sem_wait (&res->available);
100 gomp_mutex_lock (&res->lock);
101 pool = res->pools[--res->index];
102 gomp_mutex_unlock (&res->lock);
103 pool->threads_busy = nthreads;
104 thr->thread_pool = pool;
106 else
107 pool = gomp_get_own_thread_pool (thr, nthreads);
109 else
110 pool = NULL;
111 return pool;
114 static inline void
115 gomp_release_thread_pool (struct gomp_thread_pool *pool)
117 struct gomp_thread_pool_reservoir *res =
118 gomp_tls_rtems_data.thread_pool_reservoir;
119 if (res != NULL)
121 gomp_mutex_lock (&res->lock);
122 res->pools[res->index++] = pool;
123 gomp_mutex_unlock (&res->lock);
124 gomp_sem_post (&res->available);
128 #endif /* GOMP_POOL_H */