2 * Copyright (c) 2011-2013 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__
33 _dispatch_hw_config_logical_cpus
,
34 _dispatch_hw_config_physical_cpus
,
35 _dispatch_hw_config_active_cpus
,
36 } _dispatch_hw_config_t
;
38 #if !defined(DISPATCH_HAVE_HW_CONFIG_COMMPAGE) && \
39 defined(_COMM_PAGE_LOGICAL_CPUS) && \
40 defined(_COMM_PAGE_PHYSICAL_CPUS) && defined(_COMM_PAGE_ACTIVE_CPUS)
41 #define DISPATCH_HAVE_HW_CONFIG_COMMPAGE 1
44 #if DISPATCH_HAVE_HW_CONFIG_COMMPAGE
46 DISPATCH_ALWAYS_INLINE
47 static inline uint32_t
48 _dispatch_hw_get_config(_dispatch_hw_config_t c
)
52 case _dispatch_hw_config_logical_cpus
:
53 p
= _COMM_PAGE_LOGICAL_CPUS
; break;
54 case _dispatch_hw_config_physical_cpus
:
55 p
= _COMM_PAGE_PHYSICAL_CPUS
; break;
56 case _dispatch_hw_config_active_cpus
:
57 p
= _COMM_PAGE_ACTIVE_CPUS
; break;
62 #define dispatch_hw_config(c) \
63 _dispatch_hw_get_config(_dispatch_hw_config_##c)
65 #define DISPATCH_HW_CONFIG()
66 #define _dispatch_hw_config_init()
68 #else // DISPATCH_HAVE_HW_CONFIG_COMMPAGE
70 extern struct _dispatch_hw_configs_s
{
71 uint32_t logical_cpus
;
72 uint32_t physical_cpus
;
74 } _dispatch_hw_config
;
76 #define DISPATCH_HW_CONFIG() struct _dispatch_hw_configs_s _dispatch_hw_config
77 #define dispatch_hw_config(c) (_dispatch_hw_config.c)
79 DISPATCH_ALWAYS_INLINE
80 static inline uint32_t
81 _dispatch_hw_get_config(_dispatch_hw_config_t c
)
84 const char *name
= NULL
;
86 #if defined(__APPLE__)
88 case _dispatch_hw_config_logical_cpus
:
89 name
= "hw.logicalcpu_max"; break;
90 case _dispatch_hw_config_physical_cpus
:
91 name
= "hw.physicalcpu_max"; break;
92 case _dispatch_hw_config_active_cpus
:
93 name
= "hw.activecpu"; break;
95 #elif defined(__FreeBSD__)
96 (void)c
; name
= "kern.smp.cpus";
99 size_t valsz
= sizeof(val
);
100 r
= sysctlbyname(name
, &val
, &valsz
, NULL
, 0);
101 (void)dispatch_assume_zero(r
);
102 dispatch_assert(valsz
== sizeof(uint32_t));
104 #if HAVE_SYSCONF && defined(_SC_NPROCESSORS_ONLN)
105 r
= (int)sysconf(_SC_NPROCESSORS_ONLN
);
106 if (r
> 0) val
= (uint32_t)r
;
112 #define dispatch_hw_config_init(c) \
113 _dispatch_hw_get_config(_dispatch_hw_config_##c)
116 _dispatch_hw_config_init(void)
118 dispatch_hw_config(logical_cpus
) = dispatch_hw_config_init(logical_cpus
);
119 dispatch_hw_config(physical_cpus
) = dispatch_hw_config_init(physical_cpus
);
120 dispatch_hw_config(active_cpus
) = dispatch_hw_config_init(active_cpus
);
123 #undef dispatch_hw_config_init
125 #endif // DISPATCH_HAVE_HW_CONFIG_COMMPAGE
127 #else // TARGET_OS_WIN32
130 _dispatch_count_bits(unsigned long value
)
140 static inline uint32_t
141 _dispatch_get_ncpus(void)
144 DWORD_PTR procmask
, sysmask
;
145 if (GetProcessAffinityMask(GetCurrentProcess(), &procmask
, &sysmask
)) {
146 val
= _dispatch_count_bits(procmask
);
152 #endif // TARGET_OS_WIN32
154 #endif /* __DISPATCH_SHIMS_HW_CONFIG__ */