Debian Codegraph @

vendor/Heap-Layers/threads/cpuinfo.h

raw symbols SCIP
1 // -*- C++ -*-
2
3 /*
4
5 Heap Layers: An Extensible Memory Allocation Infrastructure
6
7 Copyright (C) 2000-2020 by Emery Berger
8 http://www.emeryberger.com
9 [email protected]
10
11 Heap Layers is distributed under the terms of the Apache 2.0 license.
12
13 You may obtain a copy of the License at
14 http://www.apache.org/licenses/LICENSE-2.0
15
16 */
17
18 #ifndef HL_CPUINFO_H
19 #define HL_CPUINFO_H
20
21 #include "utility/arch.h"
22
23 #if defined(_WIN32)
24 #include <windows.h>
25 #include <process.h>
26 #else
27 #include <unistd.h>
28 #endif
29
30
31 #if !defined(_WIN32)
32 #include <pthread.h>
33 #endif
34
35 #if defined(__SVR4) // Solaris
36 #include <sys/lwp.h>
37 extern "C" unsigned int lwp_self(void);
38 #include <thread.h>
39 extern "C" int _thr_self(void);
40 #endif
41
42 #if defined(__linux)
43 #include <sys/syscall.h>
44 #include <sys/types.h>
45 #include <sys/stat.h>
46 #include <fcntl.h>
47 #include <string.h>
48 #include <unistd.h>
49 #endif
50
51 #if defined(__APPLE__)
52 #include <sys/types.h>
53 #include <sys/sysctl.h>
54 #endif
55
56 #if defined(__sgi)
57 #include <sys/types.h>
58 #include <sys/sysmp.h>
59 #include <sys/sysinfo.h>
60 #endif
61
62 #if defined(hpux)
63 #include <sys/mpctl.h>
64 #endif
65
66 #if defined(_WIN32)
67 extern __declspec(thread) int localThreadId;
68 #endif
69
70 #if defined(__SVR4) && defined(MAP_ALIGN)
71 extern volatile int anyThreadStackCreated;
72 #endif
73
74 namespace HL {
75
76 /**
77 * @class CPUInfo
78 * @author Emery Berger <http://www.cs.umass.edu/~emery>
79 *
80 * @brief Architecture-independent wrapper to get number of CPUs.
81 */
82
83 class CPUInfo {
84 public:
85
86 #ifdef HL_APPLE_SILICON
87 enum { PageSize = 16384UL };
88 #else
89 // Good for practically all platforms.
90 enum { PageSize = 4096UL };
91 #endif
92
93 inline static int getNumProcessors() {
94 static int _numProcessors = computeNumProcessors();
95 return _numProcessors;
96 }
97
98 static inline unsigned int getThreadId();
99 inline static int computeNumProcessors();
100
101 };
102
103
104 int CPUInfo::computeNumProcessors (void)
105 {
106 static int np = 0;
107 if (!np) {
108 #if defined(_SC_NPROCESSORS_ONLN)
109 np = (int) sysconf(_SC_NPROCESSORS_ONLN);
110 #elif defined(_WIN32)
111 SYSTEM_INFO infoReturn[1];
112 GetSystemInfo (infoReturn);
113 np = (int) (infoReturn->dwNumberOfProcessors);
114 #elif defined(__sgi)
115 np = (int) sysmp(MP_NAPROCS);
116 #elif defined(hpux)
117 np = mpctl(MPC_GETNUMSPUS, NULL, NULL); // or pthread_num_processors_np()?
118 #else
119 np = 2;
120 // Unsupported platform.
121 // Pretend we have at least two processors. This approach avoids the risk of assuming
122 // we're on a uniprocessor, which might lead clever allocators to avoid using atomic
123 // operations for all locks.
124 #endif
125 return np;
126 } else {
127 return np;
128 }
129 }
130
131 #if defined(USE_THREAD_KEYWORD)
132 extern __thread int localThreadId;
133 #endif
134
135 unsigned int CPUInfo::getThreadId() {
136 #if defined(__SVR4)
137 return (unsigned int) pthread_self();
138 #elif defined(_WIN32)
139 // It looks like thread id's are always multiples of 4, so...
140 return GetCurrentThreadId() >> 2;
141 #elif defined(__APPLE__)
142 // FIXME: is this page size dependent?
143 // Consecutive thread id's in Mac OS are 4096 apart;
144 // dividing off the 4096 gives us an appropriate thread id.
145 unsigned int tid = (unsigned int) (((size_t) pthread_self()) >> 12);
146 return tid;
147 #elif defined(__BEOS__)
148 return find_thread(0);
149 #elif defined(__linux)
150 return (unsigned int) ((size_t) pthread_self()) >> 12;
151 // return (unsigned int) syscall (SYS_gettid);
152 #elif defined(PTHREAD_KEYS_MAX)
153 // As with Apple, above.
154 return (unsigned int) ((size_t) pthread_self()) >> 12;
155 #elif defined(POSIX)
156 return (unsigned int) pthread_self();
157 #elif USE_SPROC
158 // This hairiness has the same effect as calling getpid(),
159 // but it's MUCH faster since it avoids making a system call
160 // and just accesses the sproc-local data directly.
161 unsigned int pid = (unsigned int) PRDA->sys_prda.prda_sys.t_pid;
162 return pid;
163 #else
164 #error "This platform is not currently supported."
165 return 0;
166 #endif
167 }
168
169 }
170
171 #endif