OSDN Git Service

libgomp: Fix default futex vs errno
[pf3gnuchains/gcc-fork.git] / libgomp / config / linux / affinity.c
1 /* Copyright (C) 2006, 2007, 2008, 2009, 2010, 2011
2    Free Software Foundation, Inc.
3    Contributed by Jakub Jelinek <jakub@redhat.com>.
4
5    This file is part of the GNU OpenMP Library (libgomp).
6
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.
11
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.
16
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.
20
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/>.  */
25
26 /* This is a Linux specific implementation of a CPU affinity setting.  */
27
28 #ifndef _GNU_SOURCE
29 #define _GNU_SOURCE 1
30 #endif
31 #include "libgomp.h"
32 #include "proc.h"
33 #include <stdlib.h>
34 #include <unistd.h>
35
36 #ifdef HAVE_PTHREAD_AFFINITY_NP
37
38 static unsigned int affinity_counter;
39
40 void
41 gomp_init_affinity (void)
42 {
43   cpu_set_t cpuset, cpusetnew;
44   size_t idx, widx;
45   unsigned long cpus = 0;
46
47   if (pthread_getaffinity_np (pthread_self (), sizeof (cpuset), &cpuset))
48     {
49       gomp_error ("could not get CPU affinity set");
50       free (gomp_cpu_affinity);
51       gomp_cpu_affinity = NULL;
52       gomp_cpu_affinity_len = 0;
53       return;
54     }
55
56   CPU_ZERO (&cpusetnew);
57   if (gomp_cpu_affinity_len == 0)
58     {
59       unsigned long count = gomp_cpuset_popcount (&cpuset);
60       if (count >= 65536)
61         count = 65536;
62       gomp_cpu_affinity = malloc (count * sizeof (unsigned short));
63       if (gomp_cpu_affinity == NULL)
64         {
65           gomp_error ("not enough memory to store CPU affinity list");
66           return;
67         }
68       for (widx = idx = 0; widx < count && idx < 65536; idx++)
69         if (CPU_ISSET (idx, &cpuset))
70           {
71             cpus++;
72             gomp_cpu_affinity[widx++] = idx;
73           }
74     }
75   else
76     for (widx = idx = 0; idx < gomp_cpu_affinity_len; idx++)
77       if (gomp_cpu_affinity[idx] < CPU_SETSIZE
78           && CPU_ISSET (gomp_cpu_affinity[idx], &cpuset))
79         {
80           if (! CPU_ISSET (gomp_cpu_affinity[idx], &cpusetnew))
81             {
82               cpus++;
83               CPU_SET (gomp_cpu_affinity[idx], &cpusetnew);
84             }
85           gomp_cpu_affinity[widx++] = gomp_cpu_affinity[idx];
86         }
87
88   if (widx == 0)
89     {
90       gomp_error ("no CPUs left for affinity setting");
91       free (gomp_cpu_affinity);
92       gomp_cpu_affinity = NULL;
93       gomp_cpu_affinity_len = 0;
94       return;
95     }
96
97   gomp_cpu_affinity_len = widx;
98   if (cpus < gomp_available_cpus)
99     gomp_available_cpus = cpus;
100   CPU_ZERO (&cpuset);
101   CPU_SET (gomp_cpu_affinity[0], &cpuset);
102   pthread_setaffinity_np (pthread_self (), sizeof (cpuset), &cpuset);
103   affinity_counter = 1;
104 }
105
106 void
107 gomp_init_thread_affinity (pthread_attr_t *attr)
108 {
109   unsigned int cpu;
110   cpu_set_t cpuset;
111
112   cpu = __atomic_fetch_add (&affinity_counter, 1, MEMMODEL_RELAXED);
113   cpu %= gomp_cpu_affinity_len;
114   CPU_ZERO (&cpuset);
115   CPU_SET (gomp_cpu_affinity[cpu], &cpuset);
116   pthread_attr_setaffinity_np (attr, sizeof (cpu_set_t), &cpuset);
117 }
118
119 #else
120
121 #include "../posix/affinity.c"
122
123 #endif