2017-11-20 François Dumont <fdumont@gcc.gnu.org>
[official-gcc.git] / libgo / runtime / getncpu-linux.c
blobde6606ff47cb2eee4ae943b28200fb9bb60d77fb
1 // Copyright 2012 The Go Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style
3 // license that can be found in the LICENSE file.
5 #include <features.h>
6 #include <sched.h>
8 // CPU_COUNT is only provided by glibc 2.6 or higher
9 #ifndef CPU_COUNT
10 #define CPU_COUNT(set) _CPU_COUNT((unsigned int *)(set), sizeof(*(set))/sizeof(unsigned int))
11 static int _CPU_COUNT(unsigned int *set, size_t len) {
12 int cnt;
14 cnt = 0;
15 while (len--)
16 cnt += __builtin_popcount(*set++);
17 return cnt;
19 #endif
21 #include "runtime.h"
22 #include "defs.h"
24 int32
25 getproccount(void)
27 cpu_set_t set;
28 int32 r, cnt;
30 cnt = 0;
31 r = sched_getaffinity(0, sizeof(set), &set);
32 if(r == 0)
33 cnt += CPU_COUNT(&set);
35 return cnt ? cnt : 1;