2 * Copyright (c) 2011 Apple Inc. All rights reserved.
4 * @APPLE_APACHE_LICENSE_HEADER_START@
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
10 * http://www.apache.org/licenses/LICENSE-2.0
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.
18 * @APPLE_APACHE_LICENSE_HEADER_END@
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.
27 #ifndef __DISPATCH_SHIMS_HW_CONFIG__
28 #define __DISPATCH_SHIMS_HW_CONFIG__
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"
42 static inline uint32_t
43 _dispatch_get_logicalcpu_max(void)
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
;
59 #warning "no supported way to query logical CPU count"
64 static inline uint32_t
65 _dispatch_get_physicalcpu_max(void)
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
;
81 #warning "no supported way to query physical CPU count"
86 static inline uint32_t
87 _dispatch_get_activecpu(void)
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
;
103 #warning "no supported way to query active CPU count"
108 #else // TARGET_OS_WIN32
111 _dispatch_count_bits(unsigned long value
)
122 static inline uint32_t
123 _dispatch_get_ncpus(void)
126 DWORD_PTR procmask
, sysmask
;
127 if (GetProcessAffinityMask(GetCurrentProcess(), &procmask
, &sysmask
)) {
128 val
= _dispatch_count_bits(procmask
);
134 #endif // TARGET_OS_WIN32
136 #endif /* __DISPATCH_SHIMS_HW_CONFIG__ */