]> git.saurik.com Git - apple/libdispatch.git/blob - src/shims/hw_config.h
libdispatch-339.92.1.tar.gz
[apple/libdispatch.git] / src / shims / hw_config.h
1 /*
2 * Copyright (c) 2011 Apple Inc. All rights reserved.
3 *
4 * @APPLE_APACHE_LICENSE_HEADER_START@
5 *
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
9 *
10 * http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
17 *
18 * @APPLE_APACHE_LICENSE_HEADER_END@
19 */
20
21 /*
22 * IMPORTANT: This header file describes INTERNAL interfaces to libdispatch
23 * which are subject to change in future releases of Mac OS X. Any applications
24 * relying on these interfaces WILL break.
25 */
26
27 #ifndef __DISPATCH_SHIMS_HW_CONFIG__
28 #define __DISPATCH_SHIMS_HW_CONFIG__
29
30 #if defined(__APPLE__)
31 #define DISPATCH_SYSCTL_LOGICAL_CPUS "hw.logicalcpu_max"
32 #define DISPATCH_SYSCTL_PHYSICAL_CPUS "hw.physicalcpu_max"
33 #define DISPATCH_SYSCTL_ACTIVE_CPUS "hw.activecpu"
34 #elif defined(__FreeBSD__)
35 #define DISPATCH_SYSCTL_LOGICAL_CPUS "kern.smp.cpus"
36 #define DISPATCH_SYSCTL_PHYSICAL_CPUS "kern.smp.cpus"
37 #define DISPATCH_SYSCTL_ACTIVE_CPUS "kern.smp.cpus"
38 #endif
39
40 #if !TARGET_OS_WIN32
41
42 static inline uint32_t
43 _dispatch_get_logicalcpu_max(void)
44 {
45 uint32_t val = 1;
46 #if defined(_COMM_PAGE_LOGICAL_CPUS)
47 uint8_t* u8val = (uint8_t*)(uintptr_t)_COMM_PAGE_LOGICAL_CPUS;
48 val = (uint32_t)*u8val;
49 #elif defined(DISPATCH_SYSCTL_LOGICAL_CPUS)
50 size_t valsz = sizeof(val);
51 int ret = sysctlbyname(DISPATCH_SYSCTL_LOGICAL_CPUS,
52 &val, &valsz, NULL, 0);
53 (void)dispatch_assume_zero(ret);
54 (void)dispatch_assume(valsz == sizeof(uint32_t));
55 #elif HAVE_SYSCONF && defined(_SC_NPROCESSORS_ONLN)
56 int ret = (int)sysconf(_SC_NPROCESSORS_ONLN);
57 val = ret < 0 ? 1 : ret;
58 #else
59 #warning "no supported way to query logical CPU count"
60 #endif
61 return val;
62 }
63
64 static inline uint32_t
65 _dispatch_get_physicalcpu_max(void)
66 {
67 uint32_t val = 1;
68 #if defined(_COMM_PAGE_PHYSICAL_CPUS)
69 uint8_t* u8val = (uint8_t*)(uintptr_t)_COMM_PAGE_PHYSICAL_CPUS;
70 val = (uint32_t)*u8val;
71 #elif defined(DISPATCH_SYSCTL_PHYSICAL_CPUS)
72 size_t valsz = sizeof(val);
73 int ret = sysctlbyname(DISPATCH_SYSCTL_LOGICAL_CPUS,
74 &val, &valsz, NULL, 0);
75 (void)dispatch_assume_zero(ret);
76 (void)dispatch_assume(valsz == sizeof(uint32_t));
77 #elif HAVE_SYSCONF && defined(_SC_NPROCESSORS_ONLN)
78 int ret = (int)sysconf(_SC_NPROCESSORS_ONLN);
79 val = ret < 0 ? 1 : ret;
80 #else
81 #warning "no supported way to query physical CPU count"
82 #endif
83 return val;
84 }
85
86 static inline uint32_t
87 _dispatch_get_activecpu(void)
88 {
89 uint32_t val = 1;
90 #if defined(_COMM_PAGE_ACTIVE_CPUS)
91 uint8_t* u8val = (uint8_t*)(uintptr_t)_COMM_PAGE_ACTIVE_CPUS;
92 val = (uint32_t)*u8val;
93 #elif defined(DISPATCH_SYSCTL_ACTIVE_CPUS)
94 size_t valsz = sizeof(val);
95 int ret = sysctlbyname(DISPATCH_SYSCTL_ACTIVE_CPUS,
96 &val, &valsz, NULL, 0);
97 (void)dispatch_assume_zero(ret);
98 (void)dispatch_assume(valsz == sizeof(uint32_t));
99 #elif HAVE_SYSCONF && defined(_SC_NPROCESSORS_ONLN)
100 int ret = (int)sysconf(_SC_NPROCESSORS_ONLN);
101 val = ret < 0 ? 1 : ret;
102 #else
103 #warning "no supported way to query active CPU count"
104 #endif
105 return val;
106 }
107
108 #else // TARGET_OS_WIN32
109
110 static inline long
111 _dispatch_count_bits(unsigned long value)
112 {
113 long bits = 0;
114 while (value) {
115 bits += (value & 1);
116 value = value >> 1;
117 }
118 return bits;
119 }
120
121
122 static inline uint32_t
123 _dispatch_get_ncpus(void)
124 {
125 uint32_t val;
126 DWORD_PTR procmask, sysmask;
127 if (GetProcessAffinityMask(GetCurrentProcess(), &procmask, &sysmask)) {
128 val = _dispatch_count_bits(procmask);
129 } else {
130 val = 1;
131 }
132 return val;
133 }
134 #endif // TARGET_OS_WIN32
135
136 #endif /* __DISPATCH_SHIMS_HW_CONFIG__ */