cpu.h
Go to the documentation of this file.
1 /*
2  * Copyright (C) 1996-2023 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 #include <cerrno>
13 
14 #if HAVE_SCHED_H
15 #include <sched.h>
16 #endif
17 
18 #if !HAVE_CPU_AFFINITY
19 /* failing replacements to minimize the number of if-HAVE_CPU_AFFINITYs */
20 #if !HAVE_CPU_SET_T
21 typedef struct {
22  int bits;
23 } cpu_set_t;
24 #endif
25 inline int sched_setaffinity(int, size_t, cpu_set_t *) { return ENOTSUP; }
26 inline int sched_getaffinity(int, size_t, cpu_set_t *) { return ENOTSUP; }
27 #endif /* HAVE_CPU_AFFINITY */
28 
29 #if !defined(CPU_SETSIZE)
30 #define CPU_SETSIZE 0
31 #endif
32 
33 #if !defined(CPU_ZERO)
34 #define CPU_ZERO(set) (void)0
35 #endif
36 
37 #if !defined(CPU_SET)
38 #define CPU_SET(cpunum, cpuset) CpuSet(cpunum, cpuset)
39 inline void CpuSet(int, const cpu_set_t *) {}
40 #endif
41 
42 #if !defined(CPU_CLR)
43 #define CPU_CLR(cpu, set) (void)0
44 #endif
45 
46 #if !defined(CPU_ISSET)
47 #define CPU_ISSET(cpunum, cpuset) CpuIsSet(cpunum, cpuset)
48 inline bool CpuIsSet(int, const cpu_set_t *) { return false; }
49 #endif
50 
51 // glibc prior to 2.6 lacks CPU_COUNT
52 #ifndef CPU_COUNT
53 #define CPU_COUNT(set) CpuCount(set)
54 inline int
56 CpuCount(const cpu_set_t *set)
57 {
58  int count = 0;
59  for (int i = 0; i < CPU_SETSIZE; ++i) {
60  if (CPU_ISSET(i, set))
61  ++count;
62  }
63  return count;
64 }
65 #endif /* CPU_COUNT */
66 
67 // glibc prior to 2.7 lacks CPU_AND
68 #ifndef CPU_AND
69 #define CPU_AND(destset, srcset1, srcset2) CpuAnd((destset), (srcset1), (srcset2))
70 inline void
72 CpuAnd(cpu_set_t *destset, const cpu_set_t *srcset1, const cpu_set_t *srcset2)
73 {
74  for (int i = 0; i < CPU_SETSIZE; ++i) {
75  if (CPU_ISSET(i, srcset1) && CPU_ISSET(i, srcset2))
76  CPU_SET(i, destset);
77  else
78  CPU_CLR(i, destset);
79  }
80 }
81 #endif /* CPU_AND */
82 
83 #endif /* SQUID_COMPAT_CPU_H */
84 
bool CpuIsSet(int, const cpu_set_t *)
Definition: cpu.h:48
void CpuAnd(cpu_set_t *destset, const cpu_set_t *srcset1, const cpu_set_t *srcset2)
CPU_AND replacement.
Definition: cpu.h:72
int sched_getaffinity(int, size_t, cpu_set_t *)
Definition: cpu.h:26
void CpuSet(int, const cpu_set_t *)
Definition: cpu.h:39
int sched_setaffinity(int, size_t, cpu_set_t *)
Definition: cpu.h:25
#define CPU_ISSET(cpunum, cpuset)
Definition: cpu.h:47
#define CPU_SET(cpunum, cpuset)
Definition: cpu.h:38
#define CPU_SETSIZE
Definition: cpu.h:30
#define CPU_CLR(cpu, set)
Definition: cpu.h:43
int CpuCount(const cpu_set_t *set)
CPU_COUNT replacement.
Definition: cpu.h:56
Definition: cpu.h:21
int bits
Definition: cpu.h:22

 

Introduction

Documentation

Support

Miscellaneous