]> git.ipfire.org Git - thirdparty/gcc.git/blame - libgo/runtime/getncpu-linux.c
runtime: Check for CPU_COUNT itself, don't check glibc version.
[thirdparty/gcc.git] / libgo / runtime / getncpu-linux.c
CommitLineData
233115ea
ILT
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
938ff79a
ILT
5#include <features.h>
6#include <sched.h>
233115ea 7
938ff79a 8// CPU_COUNT is only provided by glibc 2.6 or higher
60d9e9fc 9#ifndef CPU_COUNT
938ff79a
ILT
10#define CPU_COUNT(set) _CPU_COUNT((unsigned int *)(set), sizeof(*(set))/sizeof(unsigned int))
11static int _CPU_COUNT(unsigned int *set, size_t len) {
12 int cnt;
233115ea 13
938ff79a
ILT
14 cnt = 0;
15 while (len--)
16 cnt += __builtin_popcount(*set++);
17 return cnt;
18}
233115ea
ILT
19#endif
20
938ff79a
ILT
21#include "runtime.h"
22#include "defs.h"
23
233115ea
ILT
24int32
25getproccount(void)
26{
938ff79a
ILT
27 cpu_set_t set;
28 int32 r, cnt;
233115ea 29
233115ea 30 cnt = 0;
938ff79a
ILT
31 r = sched_getaffinity(0, sizeof(set), &set);
32 if(r == 0)
33 cnt += CPU_COUNT(&set);
34
233115ea
ILT
35 return cnt ? cnt : 1;
36}