]> git.ipfire.org Git - thirdparty/squid.git/blob - compat/cpu.h
SourceFormat Enforcement
[thirdparty/squid.git] / compat / cpu.h
1 /*
2 * Copyright (C) 1996-2015 The Squid Software Foundation and contributors
3 *
4 * Squid software is distributed under GPLv2+ license and includes
5 * contributions from numerous individuals and organizations.
6 * Please see the COPYING and CONTRIBUTORS files for details.
7 */
8
9 #ifndef SQUID_COMPAT_CPU_H
10 #define SQUID_COMPAT_CPU_H
11
12 #if HAVE_CPU_AFFINITY
13
14 #if HAVE_SCHED_H
15 #include <sched.h>
16 #endif
17
18 // glibc prior to 2.6 lacks CPU_COUNT
19 #ifndef CPU_COUNT
20 #define CPU_COUNT(set) CpuCount(set)
21 /// CPU_COUNT replacement
22 inline int
23 CpuCount(const cpu_set_t *set)
24 {
25 int count = 0;
26 for (int i = 0; i < CPU_SETSIZE; ++i) {
27 if (CPU_ISSET(i, set))
28 ++count;
29 }
30 return count;
31 }
32 #endif /* CPU_COUNT */
33
34 // glibc prior to 2.7 lacks CPU_AND
35 #ifndef CPU_AND
36 #define CPU_AND(destset, srcset1, srcset2) CpuAnd((destset), (srcset1), (srcset2))
37 /// CPU_AND replacement
38 inline void
39 CpuAnd(cpu_set_t *destset, const cpu_set_t *srcset1, const cpu_set_t *srcset2)
40 {
41 for (int i = 0; i < CPU_SETSIZE; ++i) {
42 if (CPU_ISSET(i, srcset1) && CPU_ISSET(i, srcset2))
43 CPU_SET(i, destset);
44 else
45 CPU_CLR(i, destset);
46 }
47 }
48 #endif /* CPU_AND */
49
50 #else /* HAVE_CPU_AFFINITY */
51
52 #if HAVE_ERRNO_H
53 #include <errno.h> /* for ENOTSUP */
54 #endif
55
56 /* failing replacements to minimize the number of if-HAVE_CPU_AFFINITYs */
57 typedef struct {
58 int bits;
59 } cpu_set_t;
60 #define CPU_SETSIZE 0
61 #define CPU_COUNT(set) 0
62 #define CPU_AND(destset, srcset1, srcset2) (void)0
63 #define CPU_ZERO(set) (void)0
64 #define CPU_SET(cpu, set) (void)0
65 #define CPU_CLR(cpu, set) (void)0
66 inline int sched_setaffinity(int, size_t, cpu_set_t *) { return ENOTSUP; }
67 inline int sched_getaffinity(int, size_t, cpu_set_t *) { return ENOTSUP; }
68
69 #endif /* HAVE_CPU_AFFINITY */
70
71 #endif /* SQUID_COMPAT_CPU_H */
72