1 | /** @file
|
---|
2 | * IPRT - CPU Set.
|
---|
3 | */
|
---|
4 |
|
---|
5 | /*
|
---|
6 | * Copyright (C) 2008 Sun Microsystems, Inc.
|
---|
7 | *
|
---|
8 | * This file is part of VirtualBox Open Source Edition (OSE), as
|
---|
9 | * available from http://www.virtualbox.org. This file is free software;
|
---|
10 | * you can redistribute it and/or modify it under the terms of the GNU
|
---|
11 | * General Public License (GPL) as published by the Free Software
|
---|
12 | * Foundation, in version 2 as it comes in the "COPYING" file of the
|
---|
13 | * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
|
---|
14 | * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
|
---|
15 | *
|
---|
16 | * The contents of this file may alternatively be used under the terms
|
---|
17 | * of the Common Development and Distribution License Version 1.0
|
---|
18 | * (CDDL) only, as it comes in the "COPYING.CDDL" file of the
|
---|
19 | * VirtualBox OSE distribution, in which case the provisions of the
|
---|
20 | * CDDL are applicable instead of those of the GPL.
|
---|
21 | *
|
---|
22 | * You may elect to license modified versions of this file under the
|
---|
23 | * terms and conditions of either the GPL or the CDDL or both.
|
---|
24 | *
|
---|
25 | * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
|
---|
26 | * Clara, CA 95054 USA or visit http://www.sun.com if you need
|
---|
27 | * additional information or have any questions.
|
---|
28 | */
|
---|
29 |
|
---|
30 | #ifndef ___iprt_cpuset_h
|
---|
31 | #define ___iprt_cpuset_h
|
---|
32 |
|
---|
33 | #include <iprt/types.h>
|
---|
34 | #include <iprt/mp.h> /* RTMpCpuIdToSetIndex */
|
---|
35 | #include <iprt/asm.h>
|
---|
36 |
|
---|
37 |
|
---|
38 | RT_C_DECLS_BEGIN
|
---|
39 |
|
---|
40 | /** @defgroup grp_rt_cpuset RTCpuSet - CPU Set
|
---|
41 | * @ingroup grp_rt
|
---|
42 | * @{
|
---|
43 | */
|
---|
44 |
|
---|
45 | /**
|
---|
46 | * The maximum number of CPUs a set can contain and IPRT is able
|
---|
47 | * to reference.
|
---|
48 | * @remarks This is the maximum value of the supported platforms.
|
---|
49 | */
|
---|
50 | #define RTCPUSET_MAX_CPUS 64
|
---|
51 |
|
---|
52 | /**
|
---|
53 | * Clear all CPUs.
|
---|
54 | *
|
---|
55 | * @returns pSet.
|
---|
56 | * @param pSet Pointer to the set.
|
---|
57 | */
|
---|
58 | DECLINLINE(PRTCPUSET) RTCpuSetEmpty(PRTCPUSET pSet)
|
---|
59 | {
|
---|
60 | *pSet = 0;
|
---|
61 | return pSet;
|
---|
62 | }
|
---|
63 |
|
---|
64 |
|
---|
65 | /**
|
---|
66 | * Set all CPUs.
|
---|
67 | *
|
---|
68 | * @returns pSet.
|
---|
69 | * @param pSet Pointer to the set.
|
---|
70 | */
|
---|
71 | DECLINLINE(PRTCPUSET) RTCpuSetFill(PRTCPUSET pSet)
|
---|
72 | {
|
---|
73 | *pSet = UINT64_MAX;
|
---|
74 | return pSet;
|
---|
75 | }
|
---|
76 |
|
---|
77 |
|
---|
78 | /**
|
---|
79 | * Adds a CPU given by its identifier to the set.
|
---|
80 | *
|
---|
81 | * @returns 0 on success, -1 if idCpu isn't valid.
|
---|
82 | * @param pSet Pointer to the set.
|
---|
83 | * @param idCpu The identifier of the CPU to add.
|
---|
84 | * @remarks The modification is atomic.
|
---|
85 | */
|
---|
86 | DECLINLINE(int) RTCpuSetAdd(PRTCPUSET pSet, RTCPUID idCpu)
|
---|
87 | {
|
---|
88 | int iCpu = RTMpCpuIdToSetIndex(idCpu);
|
---|
89 | if (RT_UNLIKELY(iCpu < 0))
|
---|
90 | return -1;
|
---|
91 | ASMAtomicBitSet(pSet, iCpu);
|
---|
92 | return 0;
|
---|
93 | }
|
---|
94 |
|
---|
95 |
|
---|
96 | /**
|
---|
97 | * Removes a CPU given by its identifier from the set.
|
---|
98 | *
|
---|
99 | * @returns 0 on success, -1 if idCpu isn't valid.
|
---|
100 | * @param pSet Pointer to the set.
|
---|
101 | * @param idCpu The identifier of the CPU to delete.
|
---|
102 | * @remarks The modification is atomic.
|
---|
103 | */
|
---|
104 | DECLINLINE(int) RTCpuSetDel(PRTCPUSET pSet, RTCPUID idCpu)
|
---|
105 | {
|
---|
106 | int iCpu = RTMpCpuIdToSetIndex(idCpu);
|
---|
107 | if (RT_UNLIKELY(iCpu < 0))
|
---|
108 | return -1;
|
---|
109 | ASMAtomicBitClear(pSet, iCpu);
|
---|
110 | return 0;
|
---|
111 | }
|
---|
112 |
|
---|
113 |
|
---|
114 | /**
|
---|
115 | * Removes a CPU given by its index from the set.
|
---|
116 | *
|
---|
117 | * @returns 0 on success, -1 if idCpu isn't valid.
|
---|
118 | * @param pSet Pointer to the set.
|
---|
119 | * @param iCpu The index of the CPU to delete.
|
---|
120 | * @remarks The modification is atomic.
|
---|
121 | */
|
---|
122 | DECLINLINE(int) RTCpuSetDelByIndex(PRTCPUSET pSet, int iCpu)
|
---|
123 | {
|
---|
124 | if (RT_UNLIKELY((unsigned)iCpu >= RTCPUSET_MAX_CPUS))
|
---|
125 | return -1;
|
---|
126 | ASMAtomicBitClear(pSet, iCpu);
|
---|
127 | return 0;
|
---|
128 | }
|
---|
129 |
|
---|
130 |
|
---|
131 | /**
|
---|
132 | * Checks if a CPU given by its identifier is a member of the set.
|
---|
133 | *
|
---|
134 | * @returns true / false accordingly.
|
---|
135 | * @param pSet Pointer to the set.
|
---|
136 | * @param idCpu The identifier of the CPU to look for.
|
---|
137 | * @remarks The test is atomic.
|
---|
138 | */
|
---|
139 | DECLINLINE(bool) RTCpuSetIsMember(PCRTCPUSET pSet, RTCPUID idCpu)
|
---|
140 | {
|
---|
141 | int iCpu = RTMpCpuIdToSetIndex(idCpu);
|
---|
142 | if (RT_UNLIKELY(iCpu < 0))
|
---|
143 | return false;
|
---|
144 | return ASMBitTest((volatile void *)pSet, iCpu);
|
---|
145 | }
|
---|
146 |
|
---|
147 |
|
---|
148 | /**
|
---|
149 | * Checks if a CPU given by its index is a member of the set.
|
---|
150 | *
|
---|
151 | * @returns true / false accordingly.
|
---|
152 | * @param pSet Pointer to the set.
|
---|
153 | * @param iCpu The index of the CPU in the set.
|
---|
154 | * @remarks The test is atomic.
|
---|
155 | */
|
---|
156 | DECLINLINE(bool) RTCpuSetIsMemberByIndex(PCRTCPUSET pSet, int iCpu)
|
---|
157 | {
|
---|
158 | if (RT_UNLIKELY((unsigned)iCpu >= RTCPUSET_MAX_CPUS))
|
---|
159 | return false;
|
---|
160 | return ASMBitTest((volatile void *)pSet, iCpu);
|
---|
161 | }
|
---|
162 |
|
---|
163 |
|
---|
164 | /**
|
---|
165 | * Checks if the two sets match or not.
|
---|
166 | *
|
---|
167 | * @returns true / false accordingly.
|
---|
168 | * @param pSet1 The first set.
|
---|
169 | * @param pSet2 The second set.
|
---|
170 | */
|
---|
171 | DECLINLINE(bool) RTCpuSetIsEqual(PCRTCPUSET pSet1, PCRTCPUSET pSet2)
|
---|
172 | {
|
---|
173 | return *pSet1 == *pSet2 ? true : false;
|
---|
174 | }
|
---|
175 |
|
---|
176 |
|
---|
177 | /**
|
---|
178 | * Converts the CPU set to a 64-bit mask.
|
---|
179 | *
|
---|
180 | * @returns The mask.
|
---|
181 | * @param pSet Pointer to the set.
|
---|
182 | */
|
---|
183 | DECLINLINE(uint64_t) RTCpuSetToU64(PCRTCPUSET pSet)
|
---|
184 | {
|
---|
185 | return *pSet;
|
---|
186 | }
|
---|
187 |
|
---|
188 |
|
---|
189 | /**
|
---|
190 | * Initializes the CPU set from a 64-bit mask.
|
---|
191 | *
|
---|
192 | * @param pSet Pointer to the set.
|
---|
193 | * @param fMask The mask.
|
---|
194 | */
|
---|
195 | DECLINLINE(PRTCPUSET) RTCpuSetFromU64(PRTCPUSET pSet, uint64_t fMask)
|
---|
196 | {
|
---|
197 | *pSet = fMask;
|
---|
198 | return pSet;
|
---|
199 | }
|
---|
200 |
|
---|
201 |
|
---|
202 | /**
|
---|
203 | * Count the CPUs in the set.
|
---|
204 | *
|
---|
205 | * @returns CPU count.
|
---|
206 | * @param pSet Pointer to the set.
|
---|
207 | */
|
---|
208 | DECLINLINE(int) RTCpuSetCount(PCRTCPUSET pSet)
|
---|
209 | {
|
---|
210 | int cCpus = 0;
|
---|
211 | RTCPUID iCpu = 64;
|
---|
212 | while (iCpu-- > 0)
|
---|
213 | if (*pSet & RT_BIT_64(iCpu))
|
---|
214 | cCpus++;
|
---|
215 | return cCpus;
|
---|
216 | }
|
---|
217 |
|
---|
218 |
|
---|
219 | /**
|
---|
220 | * Get the highest set index.
|
---|
221 | *
|
---|
222 | * @returns The higest set index, -1 if all bits are clear.
|
---|
223 | * @param pSet Pointer to the set.
|
---|
224 | */
|
---|
225 | DECLINLINE(int) RTCpuLastIndex(PCRTCPUSET pSet)
|
---|
226 | {
|
---|
227 | /* There are more efficient ways to do this in asm.h... */
|
---|
228 | int iCpu = RTCPUSET_MAX_CPUS;
|
---|
229 | while (iCpu-- > 0)
|
---|
230 | if (*pSet & RT_BIT_64(iCpu))
|
---|
231 | return iCpu;
|
---|
232 | return iCpu;
|
---|
233 | }
|
---|
234 |
|
---|
235 |
|
---|
236 | /** @} */
|
---|
237 |
|
---|
238 | RT_C_DECLS_END
|
---|
239 |
|
---|
240 | #endif
|
---|
241 |
|
---|
242 |
|
---|