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 | __BEGIN_DECLS
|
---|
39 |
|
---|
40 | /** @defgroup grp_rt_mp 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 it's 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 it's 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 | * Checks if a CPU given by it's identifier is a member of the set.
|
---|
116 | *
|
---|
117 | * @returns true / false accordingly.
|
---|
118 | * @param pSet Pointer to the set.
|
---|
119 | * @param idCpu The identifier of the CPU to look for.
|
---|
120 | * @remarks The test is atomic.
|
---|
121 | */
|
---|
122 | DECLINLINE(bool) RTCpuSetIsMember(PCRTCPUSET pSet, RTCPUID idCpu)
|
---|
123 | {
|
---|
124 | int iCpu = RTMpCpuIdToSetIndex(idCpu);
|
---|
125 | if (RT_UNLIKELY(iCpu < 0))
|
---|
126 | return false;
|
---|
127 | return ASMBitTest((volatile void *)pSet, iCpu);
|
---|
128 | }
|
---|
129 |
|
---|
130 |
|
---|
131 | /**
|
---|
132 | * Checks if the two sets match or not.
|
---|
133 | *
|
---|
134 | * @returns true / false accordingly.
|
---|
135 | * @param pSet1 The first set.
|
---|
136 | * @param pSet2 The second set.
|
---|
137 | */
|
---|
138 | DECLINLINE(bool) RTCpuSetIsEqual(PCRTCPUSET pSet1, PCRTCPUSET pSet2)
|
---|
139 | {
|
---|
140 | return *pSet1 == *pSet2;
|
---|
141 | }
|
---|
142 |
|
---|
143 |
|
---|
144 | /**
|
---|
145 | * Converts the CPU set to a 64-bit mask.
|
---|
146 | *
|
---|
147 | * @returns The mask.
|
---|
148 | * @param pSet Pointer to the set.
|
---|
149 | */
|
---|
150 | DECLINLINE(uint64_t) RTCpuSetToU64(PCRTCPUSET pSet)
|
---|
151 | {
|
---|
152 | return *pSet;
|
---|
153 | }
|
---|
154 |
|
---|
155 |
|
---|
156 | /**
|
---|
157 | * Initializes the CPU set from a 64-bit mask.
|
---|
158 | *
|
---|
159 | * @param pSet Pointer to the set.
|
---|
160 | * @param fMask The mask.
|
---|
161 | */
|
---|
162 | DECLINLINE(PRTCPUSET) RTCpuSetFromU64(PRTCPUSET pSet, uint64_t fMask)
|
---|
163 | {
|
---|
164 | *pSet = fMask;
|
---|
165 | return pSet;
|
---|
166 | }
|
---|
167 |
|
---|
168 |
|
---|
169 | /**
|
---|
170 | * Count the CPUs in the set.
|
---|
171 | *
|
---|
172 | * @returns CPU count.
|
---|
173 | * @param pSet Pointer to the set.
|
---|
174 | */
|
---|
175 | DECLINLINE(RTCPUID) RTCpuSetCount(PRTCPUSET pSet)
|
---|
176 | {
|
---|
177 | RTCPUID cCpus = 0;
|
---|
178 | RTCPUID iCpu = 64;
|
---|
179 | while (iCpu-- > 0)
|
---|
180 | if (*pSet & RT_BIT_64(iCpu))
|
---|
181 | cCpus++;
|
---|
182 | return cCpus;
|
---|
183 | }
|
---|
184 |
|
---|
185 |
|
---|
186 | /** @} */
|
---|
187 |
|
---|
188 | __END_DECLS
|
---|
189 |
|
---|
190 | #endif
|
---|
191 |
|
---|
192 |
|
---|