affinity.c: Use atomic rather than sync builtin.
[official-gcc.git] / libgomp / config / linux / affinity.c
blob7a904df9492fd0905081abb5d6465b44eeb8c75a
1 /* Copyright (C) 2006, 2007, 2008, 2009, 2010, 2011
2 Free Software Foundation, Inc.
3 Contributed by Jakub Jelinek <jakub@redhat.com>.
5 This file is part of the GNU OpenMP Library (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 a Linux specific implementation of a CPU affinity setting. */
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>
36 #ifdef HAVE_PTHREAD_AFFINITY_NP
38 static unsigned int affinity_counter;
40 void
41 gomp_init_affinity (void)
43 cpu_set_t cpuset, cpusetnew;
44 size_t idx, widx;
45 unsigned long cpus = 0;
47 if (pthread_getaffinity_np (pthread_self (), sizeof (cpuset), &cpuset))
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;
56 CPU_ZERO (&cpusetnew);
57 if (gomp_cpu_affinity_len == 0)
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)
65 gomp_error ("not enough memory to store CPU affinity list");
66 return;
68 for (widx = idx = 0; widx < count && idx < 65536; idx++)
69 if (CPU_ISSET (idx, &cpuset))
71 cpus++;
72 gomp_cpu_affinity[widx++] = idx;
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))
80 if (! CPU_ISSET (gomp_cpu_affinity[idx], &cpusetnew))
82 cpus++;
83 CPU_SET (gomp_cpu_affinity[idx], &cpusetnew);
85 gomp_cpu_affinity[widx++] = gomp_cpu_affinity[idx];
88 if (widx == 0)
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;
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;
106 void
107 gomp_init_thread_affinity (pthread_attr_t *attr)
109 unsigned int cpu;
110 cpu_set_t cpuset;
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);
119 #else
121 #include "../posix/affinity.c"
123 #endif