Importing Upstream version 4.8.2
[platform/upstream/gcc48.git] / libgo / runtime / getncpu-linux.c
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.
4
5 #include <features.h>
6 #include <sched.h>
7
8 // CPU_COUNT is only provided by glibc 2.6 or higher
9 #if !defined(__GLIBC_PREREQ) || !__GLIBC_PREREQ(2, 6)
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;
13
14         cnt = 0;
15         while (len--)
16                 cnt += __builtin_popcount(*set++);
17         return cnt;
18 }
19 #endif
20
21 #include "runtime.h"
22 #include "defs.h"
23
24 int32
25 getproccount(void)
26 {
27         cpu_set_t set;
28         int32 r, cnt;
29
30         cnt = 0;
31         r = sched_getaffinity(0, sizeof(set), &set);
32         if(r == 0)
33                 cnt += CPU_COUNT(&set);
34
35         return cnt ? cnt : 1;
36 }