summaryrefslogtreecommitdiff
path: root/libgomp/config/rtems/pool.h
blob: 88ad417d71f39fb702105b7fe44d8bfa4ae71384 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
/* Copyright (C) 2015-2017 Free Software Foundation, Inc.
   Contributed by Sebastian Huber <sebastian.huber@embedded-brains.de>.

   This file is part of the GNU Offloading and Multi Processing Library
   (libgomp).

   Libgomp is free software; you can redistribute it and/or modify it
   under the terms of the GNU General Public License as published by
   the Free Software Foundation; either version 3, or (at your option)
   any later version.

   Libgomp is distributed in the hope that it will be useful, but WITHOUT ANY
   WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
   FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
   more details.

   Under Section 7 of GPL version 3, you are granted additional
   permissions described in the GCC Runtime Library Exception, version
   3.1, as published by the Free Software Foundation.

   You should have received a copy of the GNU General Public License and
   a copy of the GCC Runtime Library Exception along with this program;
   see the files COPYING3 and COPYING.RUNTIME respectively.  If not, see
   <http://www.gnu.org/licenses/>.  */

/* This is the RTEMS implementation of the thread pool management
   for libgomp.  This type is private to the library.  */

#ifndef GOMP_POOL_H
#define GOMP_POOL_H 1

#include "libgomp.h"
#include <sys/lock.h>
#include <string.h>

/* For each scheduler instance there may be a thread pool reservoir
   to limit the number of thread pools used by the OpenMP master threads of this
   scheduler instance.  The reservoirs are configured via the
   GOMP_RTEMS_THREAD_POOLS environment variable.  */
struct gomp_thread_pool_reservoir {
  gomp_sem_t available;
  pthread_spinlock_t lock;
  size_t index;
  int priority;
  struct gomp_thread_pool *pools[];
};

struct gomp_tls_rtems_data {
  struct gomp_thread_pool_reservoir *thread_pool_reservoir;
};

extern struct gomp_thread_pool_reservoir **gomp_thread_pool_reservoirs;

extern __thread struct gomp_tls_rtems_data gomp_tls_rtems_data;

static inline struct gomp_thread_pool_reservoir *
gomp_get_thread_pool_reservoir (void)
{
  struct gomp_thread_pool_reservoir *res =
    gomp_tls_rtems_data.thread_pool_reservoir;

  if (res == NULL && gomp_thread_pool_reservoirs != NULL)
    {
      struct gomp_thread *thr = gomp_thread ();
      thr->thread_pool = gomp_malloc_cleared (sizeof (*thr->thread_pool));
      res = gomp_thread_pool_reservoirs[_Sched_Index ()];
      gomp_tls_rtems_data.thread_pool_reservoir = res;
    }

  return res;
}

static inline struct gomp_thread_pool *
gomp_get_own_thread_pool (struct gomp_thread *thr, unsigned nthreads)
{
  struct gomp_thread_pool *pool = thr->thread_pool;
  if (__builtin_expect (pool == NULL, 0))
    {
      pool = gomp_malloc_cleared (sizeof (*pool));
      pool->threads_busy = nthreads;
      thr->thread_pool = pool;
    }
  return pool;
}

static inline struct gomp_thread_pool *
gomp_get_thread_pool (struct gomp_thread *thr, unsigned nthreads)
{
  struct gomp_thread_pool *pool;
  struct gomp_thread_pool_reservoir *res;

  if (__builtin_expect (thr->thread_pool == NULL, 0))
    pthread_setspecific (gomp_thread_destructor, thr);

  res = gomp_get_thread_pool_reservoir ();
  if (res != NULL)
    {
      gomp_sem_wait (&res->available);
      pthread_spin_lock (&res->lock);
      pool = res->pools[--res->index];
      pthread_spin_unlock (&res->lock);
      pool->threads_busy = nthreads;
      thr->thread_pool = pool;
    }
  else
    pool = gomp_get_own_thread_pool (thr, nthreads);

  return pool;
}

static inline void
gomp_release_thread_pool (struct gomp_thread_pool *pool)
{
  struct gomp_thread_pool_reservoir *res =
    gomp_tls_rtems_data.thread_pool_reservoir;
  if (res != NULL)
    {
      pthread_spin_lock (&res->lock);
      res->pools[res->index++] = pool;
      pthread_spin_unlock (&res->lock);
      gomp_sem_post (&res->available);
    }
}

static inline pthread_attr_t *
gomp_adjust_thread_attr (pthread_attr_t *attr, pthread_attr_t *mutable_attr)
{
  struct gomp_thread_pool_reservoir *res = gomp_get_thread_pool_reservoir ();
  if (res != NULL && res->priority > 0)
    {
      struct sched_param param;
      int err;
      if (attr != mutable_attr)
	{
	  attr = mutable_attr;
	  pthread_attr_init (attr);
	}
      memset (&param, 0, sizeof (param));
      param.sched_priority = res->priority;
      err = pthread_attr_setschedparam (attr, &param);
      if (err != 0)
	gomp_fatal ("Thread attribute set scheduler parameters failed: %s", strerror (err));
      err = pthread_attr_setschedpolicy (attr, SCHED_FIFO);
      if (err != 0)
	gomp_fatal ("Thread attribute set scheduler policy failed: %s", strerror (err));
      err = pthread_attr_setinheritsched (attr, PTHREAD_EXPLICIT_SCHED);
      if (err != 0)
	gomp_fatal ("Thread attribute set explicit scheduler failed: %s", strerror (err));
    }
  return attr;
}

#endif /* GOMP_POOL_H */