VirtualBox

source: vbox/trunk/include/iprt/cpuset.h@ 47791

Last change on this file since 47791 was 36262, checked in by vboxsync, 14 years ago

SUPDrv,IPRT,++: Enabled the code for supporting up to 256 host CPUs/cores/threads.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 6.6 KB
Line 
1/** @file
2 * IPRT - CPU Set.
3 */
4
5/*
6 * Copyright (C) 2008-2011 Oracle Corporation
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
26#ifndef ___iprt_cpuset_h
27#define ___iprt_cpuset_h
28
29#include <iprt/types.h>
30#include <iprt/mp.h> /* RTMpCpuIdToSetIndex */
31#include <iprt/asm.h>
32
33
34RT_C_DECLS_BEGIN
35
36/** @defgroup grp_rt_cpuset RTCpuSet - CPU Set
37 * @ingroup grp_rt
38 * @{
39 */
40
41
42/**
43 * Clear all CPUs.
44 *
45 * @returns pSet.
46 * @param pSet Pointer to the set.
47 */
48DECLINLINE(PRTCPUSET) RTCpuSetEmpty(PRTCPUSET pSet)
49{
50 unsigned i;
51 for (i = 0; i < RT_ELEMENTS(pSet->bmSet); i++)
52 pSet->bmSet[i] = 0;
53 return pSet;
54}
55
56
57/**
58 * Set all CPUs.
59 *
60 * @returns pSet.
61 * @param pSet Pointer to the set.
62 */
63DECLINLINE(PRTCPUSET) RTCpuSetFill(PRTCPUSET pSet)
64{
65 unsigned i;
66 for (i = 0; i < RT_ELEMENTS(pSet->bmSet); i++)
67 pSet->bmSet[i] = UINT64_MAX;
68 return pSet;
69}
70
71
72/**
73 * Adds a CPU given by its identifier to the set.
74 *
75 * @returns 0 on success, -1 if idCpu isn't valid.
76 * @param pSet Pointer to the set.
77 * @param idCpu The identifier of the CPU to add.
78 * @remarks The modification is atomic.
79 */
80DECLINLINE(int) RTCpuSetAdd(PRTCPUSET pSet, RTCPUID idCpu)
81{
82 int iCpu = RTMpCpuIdToSetIndex(idCpu);
83 if (RT_UNLIKELY(iCpu < 0))
84 return -1;
85 ASMAtomicBitSet(pSet, iCpu);
86 return 0;
87}
88
89
90/**
91 * Adds a CPU given by its identifier to the set.
92 *
93 * @returns 0 on success, -1 if iCpu isn't valid.
94 * @param pSet Pointer to the set.
95 * @param iCpu The index of the CPU to add.
96 * @remarks The modification is atomic.
97 */
98DECLINLINE(int) RTCpuSetAddByIndex(PRTCPUSET pSet, int iCpu)
99{
100 if (RT_UNLIKELY((unsigned)iCpu >= RTCPUSET_MAX_CPUS))
101 return -1;
102 ASMAtomicBitSet(pSet, iCpu);
103 return 0;
104}
105
106
107/**
108 * Removes a CPU given by its identifier from the set.
109 *
110 * @returns 0 on success, -1 if idCpu isn't valid.
111 * @param pSet Pointer to the set.
112 * @param idCpu The identifier of the CPU to delete.
113 * @remarks The modification is atomic.
114 */
115DECLINLINE(int) RTCpuSetDel(PRTCPUSET pSet, RTCPUID idCpu)
116{
117 int iCpu = RTMpCpuIdToSetIndex(idCpu);
118 if (RT_UNLIKELY(iCpu < 0))
119 return -1;
120 ASMAtomicBitClear(pSet, iCpu);
121 return 0;
122}
123
124
125/**
126 * Removes a CPU given by its index from the set.
127 *
128 * @returns 0 on success, -1 if iCpu isn't valid.
129 * @param pSet Pointer to the set.
130 * @param iCpu The index of the CPU to delete.
131 * @remarks The modification is atomic.
132 */
133DECLINLINE(int) RTCpuSetDelByIndex(PRTCPUSET pSet, int iCpu)
134{
135 if (RT_UNLIKELY((unsigned)iCpu >= RTCPUSET_MAX_CPUS))
136 return -1;
137 ASMAtomicBitClear(pSet, iCpu);
138 return 0;
139}
140
141
142/**
143 * Checks if a CPU given by its identifier is a member of the set.
144 *
145 * @returns true / false accordingly.
146 * @param pSet Pointer to the set.
147 * @param idCpu The identifier of the CPU to look for.
148 * @remarks The test is atomic.
149 */
150DECLINLINE(bool) RTCpuSetIsMember(PCRTCPUSET pSet, RTCPUID idCpu)
151{
152 int iCpu = RTMpCpuIdToSetIndex(idCpu);
153 if (RT_UNLIKELY(iCpu < 0))
154 return false;
155 return ASMBitTest((volatile void *)pSet, iCpu);
156}
157
158
159/**
160 * Checks if a CPU given by its index is a member of the set.
161 *
162 * @returns true / false accordingly.
163 * @param pSet Pointer to the set.
164 * @param iCpu The index of the CPU in the set.
165 * @remarks The test is atomic.
166 */
167DECLINLINE(bool) RTCpuSetIsMemberByIndex(PCRTCPUSET pSet, int iCpu)
168{
169 if (RT_UNLIKELY((unsigned)iCpu >= RTCPUSET_MAX_CPUS))
170 return false;
171 return ASMBitTest((volatile void *)pSet, iCpu);
172}
173
174
175/**
176 * Checks if the two sets match or not.
177 *
178 * @returns true / false accordingly.
179 * @param pSet1 The first set.
180 * @param pSet2 The second set.
181 */
182DECLINLINE(bool) RTCpuSetIsEqual(PCRTCPUSET pSet1, PCRTCPUSET pSet2)
183{
184 unsigned i;
185 for (i = 0; i < RT_ELEMENTS(pSet1->bmSet); i++)
186 if (pSet1->bmSet[i] != pSet2->bmSet[i])
187 return false;
188 return true;
189}
190
191
192/**
193 * Converts the CPU set to a 64-bit mask.
194 *
195 * @returns The mask.
196 * @param pSet Pointer to the set.
197 * @remarks Use with extreme care as it may lose information!
198 */
199DECLINLINE(uint64_t) RTCpuSetToU64(PCRTCPUSET pSet)
200{
201 return pSet->bmSet[0];
202}
203
204
205/**
206 * Initializes the CPU set from a 64-bit mask.
207 *
208 * @param pSet Pointer to the set.
209 * @param fMask The mask.
210 */
211DECLINLINE(PRTCPUSET) RTCpuSetFromU64(PRTCPUSET pSet, uint64_t fMask)
212{
213 unsigned i;
214
215 pSet->bmSet[0] = fMask;
216 for (i = 1; i < RT_ELEMENTS(pSet->bmSet); i++)
217 pSet->bmSet[i] = 0;
218
219 return pSet;
220}
221
222
223/**
224 * Count the CPUs in the set.
225 *
226 * @returns CPU count.
227 * @param pSet Pointer to the set.
228 */
229DECLINLINE(int) RTCpuSetCount(PCRTCPUSET pSet)
230{
231 int cCpus = 0;
232 unsigned i;
233
234 for (i = 0; i < RT_ELEMENTS(pSet->bmSet); i++)
235 {
236 uint64_t u64 = pSet->bmSet[i];
237 if (u64 != 0)
238 {
239 unsigned iCpu = 64;
240 while (iCpu-- > 0)
241 {
242 if (u64 & 1)
243 cCpus++;
244 u64 >>= 1;
245 }
246 }
247 }
248 return cCpus;
249}
250
251
252/**
253 * Get the highest set index.
254 *
255 * @returns The higest set index, -1 if all bits are clear.
256 * @param pSet Pointer to the set.
257 */
258DECLINLINE(int) RTCpuLastIndex(PCRTCPUSET pSet)
259{
260 unsigned i = RT_ELEMENTS(pSet->bmSet);
261 while (i-- > 0)
262 {
263 uint64_t u64 = pSet->bmSet[i];
264 if (u64)
265 {
266 /* There are more efficient ways to do this in asm.h... */
267 unsigned iBit;
268 for (iBit = 63; iBit > 0; iBit--)
269 {
270 if (u64 & RT_BIT_64(63))
271 break;
272 u64 <<= 1;
273 }
274 return i * 64 + iBit;
275 }
276 }
277 return 0;
278}
279
280
281/** @} */
282
283RT_C_DECLS_END
284
285#endif
286
Note: See TracBrowser for help on using the repository browser.

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette