OSDN Git Service

Add NIOS2 support. Code from SourceyG++.
[pf3gnuchains/gcc-fork.git] / libgomp / config / linux / affinity.c
1 /* Copyright (C) 2006, 2007, 2008, 2009, 2010 Free Software Foundation, Inc.
2    Contributed by Jakub Jelinek <jakub@redhat.com>.
3
4    This file is part of the GNU OpenMP Library (libgomp).
5
6    Libgomp is free software; you can redistribute it and/or modify it
7    under the terms of the GNU General Public License as published by
8    the Free Software Foundation; either version 3, or (at your option)
9    any later version.
10
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 General Public License for
14    more details.
15
16    Under Section 7 of GPL version 3, you are granted additional
17    permissions described in the GCC Runtime Library Exception, version
18    3.1, as published by the Free Software Foundation.
19
20    You should have received a copy of the GNU General Public License and
21    a copy of the GCC Runtime Library Exception along with this program;
22    see the files COPYING3 and COPYING.RUNTIME respectively.  If not, see
23    <http://www.gnu.org/licenses/>.  */
24
25 /* This is a Linux specific implementation of a CPU affinity setting.  */
26
27 #ifndef _GNU_SOURCE
28 #define _GNU_SOURCE 1
29 #endif
30 #include "libgomp.h"
31 #include <sched.h>
32 #include <stdlib.h>
33 #include <unistd.h>
34
35 #ifdef HAVE_PTHREAD_AFFINITY_NP
36
37 static unsigned int affinity_counter;
38
39 void
40 gomp_init_affinity (void)
41 {
42   cpu_set_t cpuset, cpusetnew;
43   size_t idx, widx;
44   unsigned long cpus = 0;
45
46   if (pthread_getaffinity_np (pthread_self (), sizeof (cpuset), &cpuset))
47     {
48       gomp_error ("could not get CPU affinity set");
49       free (gomp_cpu_affinity);
50       gomp_cpu_affinity = NULL;
51       gomp_cpu_affinity_len = 0;
52       return;
53     }
54
55   CPU_ZERO (&cpusetnew);
56   for (widx = idx = 0; idx < gomp_cpu_affinity_len; idx++)
57     if (gomp_cpu_affinity[idx] < CPU_SETSIZE
58         && CPU_ISSET (gomp_cpu_affinity[idx], &cpuset))
59       {
60         if (! CPU_ISSET (gomp_cpu_affinity[idx], &cpusetnew))
61           {
62             cpus++;
63             CPU_SET (gomp_cpu_affinity[idx], &cpusetnew);
64           }
65         gomp_cpu_affinity[widx++] = gomp_cpu_affinity[idx];
66       }
67
68   if (widx == 0)
69     {
70       gomp_error ("no CPUs left for affinity setting");
71       free (gomp_cpu_affinity);
72       gomp_cpu_affinity = NULL;
73       gomp_cpu_affinity_len = 0;
74       return;
75     }
76
77   gomp_cpu_affinity_len = widx;
78   if (cpus < gomp_available_cpus)
79     gomp_available_cpus = cpus;
80   CPU_ZERO (&cpuset);
81   CPU_SET (gomp_cpu_affinity[0], &cpuset);
82   pthread_setaffinity_np (pthread_self (), sizeof (cpuset), &cpuset);
83   affinity_counter = 1;
84 }
85
86 void
87 gomp_init_thread_affinity (pthread_attr_t *attr)
88 {
89   unsigned int cpu;
90   cpu_set_t cpuset;
91
92   cpu = __sync_fetch_and_add (&affinity_counter, 1);
93   cpu %= gomp_cpu_affinity_len;
94   CPU_ZERO (&cpuset);
95   CPU_SET (gomp_cpu_affinity[cpu], &cpuset);
96   pthread_attr_setaffinity_np (attr, sizeof (cpu_set_t), &cpuset);
97 }
98
99 #else
100
101 #include "../posix/affinity.c"
102
103 #endif