Merged r158465 through r158660 into branch.
[official-gcc.git] / libgomp / config / linux / affinity.c
blobda9f3d8fdcb0c1a492911a7d5f5df8d735397657
1 /* Copyright (C) 2006, 2007, 2008, 2009, 2010 Free Software Foundation, Inc.
2 Contributed by Jakub Jelinek <jakub@redhat.com>.
4 This file is part of the GNU OpenMP Library (libgomp).
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.
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.
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.
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/>. */
25 /* This is a Linux specific implementation of a CPU affinity setting. */
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>
35 #ifdef HAVE_PTHREAD_AFFINITY_NP
37 static unsigned int affinity_counter;
39 void
40 gomp_init_affinity (void)
42 cpu_set_t cpuset, cpusetnew;
43 size_t idx, widx;
44 unsigned long cpus = 0;
46 if (pthread_getaffinity_np (pthread_self (), sizeof (cpuset), &cpuset))
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;
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))
60 if (! CPU_ISSET (gomp_cpu_affinity[idx], &cpusetnew))
62 cpus++;
63 CPU_SET (gomp_cpu_affinity[idx], &cpusetnew);
65 gomp_cpu_affinity[widx++] = gomp_cpu_affinity[idx];
68 if (widx == 0)
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;
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;
86 void
87 gomp_init_thread_affinity (pthread_attr_t *attr)
89 unsigned int cpu;
90 cpu_set_t cpuset;
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);
99 #else
101 #include "../posix/affinity.c"
103 #endif