]> git.saurik.com Git - apple/libdispatch.git/blob - src/shims/hw_config.h
libdispatch-187.5.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 static inline uint32_t
41 _dispatch_get_logicalcpu_max()
42 {
43 uint32_t val = 1;
44 #if defined(_COMM_PAGE_LOGICAL_CPUS)
45 uint8_t* u8val = (uint8_t*)(uintptr_t)_COMM_PAGE_LOGICAL_CPUS;
46 val = (uint32_t)*u8val;
47 #elif defined(DISPATCH_SYSCTL_LOGICAL_CPUS)
48 size_t valsz = sizeof(val);
49 int ret = sysctlbyname(DISPATCH_SYSCTL_LOGICAL_CPUS,
50 &val, &valsz, NULL, 0);
51 (void)dispatch_assume_zero(ret);
52 (void)dispatch_assume(valsz == sizeof(uint32_t));
53 #elif HAVE_SYSCONF && defined(_SC_NPROCESSORS_ONLN)
54 int ret = (int)sysconf(_SC_NPROCESSORS_ONLN);
55 val = ret < 0 ? 1 : ret;
56 #else
57 #warning "no supported way to query logical CPU count"
58 #endif
59 return val;
60 }
61
62 static inline uint32_t
63 _dispatch_get_physicalcpu_max()
64 {
65 uint32_t val = 1;
66 #if defined(_COMM_PAGE_PHYSICAL_CPUS)
67 uint8_t* u8val = (uint8_t*)(uintptr_t)_COMM_PAGE_PHYSICAL_CPUS;
68 val = (uint32_t)*u8val;
69 #elif defined(DISPATCH_SYSCTL_PHYSICAL_CPUS)
70 size_t valsz = sizeof(val);
71 int ret = sysctlbyname(DISPATCH_SYSCTL_LOGICAL_CPUS,
72 &val, &valsz, NULL, 0);
73 (void)dispatch_assume_zero(ret);
74 (void)dispatch_assume(valsz == sizeof(uint32_t));
75 #elif HAVE_SYSCONF && defined(_SC_NPROCESSORS_ONLN)
76 int ret = (int)sysconf(_SC_NPROCESSORS_ONLN);
77 val = ret < 0 ? 1 : ret;
78 #else
79 #warning "no supported way to query physical CPU count"
80 #endif
81 return val;
82 }
83
84 static inline uint32_t
85 _dispatch_get_activecpu()
86 {
87 uint32_t val = 1;
88 #if defined(_COMM_PAGE_ACTIVE_CPUS)
89 uint8_t* u8val = (uint8_t*)(uintptr_t)_COMM_PAGE_ACTIVE_CPUS;
90 val = (uint32_t)*u8val;
91 #elif defined(DISPATCH_SYSCTL_ACTIVE_CPUS)
92 size_t valsz = sizeof(val);
93 int ret = sysctlbyname(DISPATCH_SYSCTL_ACTIVE_CPUS,
94 &val, &valsz, NULL, 0);
95 (void)dispatch_assume_zero(ret);
96 (void)dispatch_assume(valsz == sizeof(uint32_t));
97 #elif HAVE_SYSCONF && defined(_SC_NPROCESSORS_ONLN)
98 int ret = (int)sysconf(_SC_NPROCESSORS_ONLN);
99 val = ret < 0 ? 1 : ret;
100 #else
101 #warning "no supported way to query active CPU count"
102 #endif
103 return val;
104 }
105
106 #endif /* __DISPATCH_SHIMS_HW_CONFIG__ */