]>
git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blob - ubuntu/vbox/vboxguest/include/iprt/cpuset.h
6 * Copyright (C) 2008-2017 Oracle Corporation
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.
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.
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.
26 #ifndef ___iprt_cpuset_h
27 #define ___iprt_cpuset_h
29 #include <iprt/types.h>
30 #include <iprt/mp.h> /* RTMpCpuIdToSetIndex */
36 /** @defgroup grp_rt_cpuset RTCpuSet - CPU Set
46 * @param pSet Pointer to the set.
48 DECLINLINE(PRTCPUSET
) RTCpuSetEmpty(PRTCPUSET pSet
)
51 for (i
= 0; i
< RT_ELEMENTS(pSet
->bmSet
); i
++)
61 * @param pSet Pointer to the set.
63 DECLINLINE(PRTCPUSET
) RTCpuSetFill(PRTCPUSET pSet
)
66 for (i
= 0; i
< RT_ELEMENTS(pSet
->bmSet
); i
++)
67 pSet
->bmSet
[i
] = UINT64_MAX
;
73 * Copies one set to another.
75 * @param pDst Pointer to the destination set.
76 * @param pSrc Pointer to the source set.
78 DECLINLINE(void) RTCpuSetCopy(PRTCPUSET pDst
, PRTCPUSET pSrc
)
81 for (i
= 0; i
< RT_ELEMENTS(pDst
->bmSet
); i
++)
82 pDst
->bmSet
[i
] = pSrc
->bmSet
[i
];
87 * ANDs the given CPU set with another.
90 * @param pSet Pointer to the set.
91 * @param pAndMaskSet Pointer to the AND-mask set.
93 DECLINLINE(PRTCPUSET
) RTCpuSetAnd(PRTCPUSET pSet
, PRTCPUSET pAndMaskSet
)
96 for (i
= 0; i
< RT_ELEMENTS(pSet
->bmSet
); i
++)
97 ASMAtomicAndU64((volatile uint64_t *)&pSet
->bmSet
[i
], pAndMaskSet
->bmSet
[i
]);
103 * Adds a CPU given by its identifier to the set.
105 * @returns 0 on success, -1 if idCpu isn't valid.
106 * @param pSet Pointer to the set.
107 * @param idCpu The identifier of the CPU to add.
108 * @remarks The modification is atomic.
110 DECLINLINE(int) RTCpuSetAdd(PRTCPUSET pSet
, RTCPUID idCpu
)
112 int iCpu
= RTMpCpuIdToSetIndex(idCpu
);
113 if (RT_LIKELY(iCpu
>= 0))
115 ASMAtomicBitSet(pSet
, iCpu
);
123 * Adds a CPU given by its identifier to the set.
125 * @returns 0 on success, -1 if iCpu isn't valid.
126 * @param pSet Pointer to the set.
127 * @param iCpu The index of the CPU to add.
128 * @remarks The modification is atomic.
130 DECLINLINE(int) RTCpuSetAddByIndex(PRTCPUSET pSet
, int iCpu
)
132 if (RT_LIKELY((unsigned)iCpu
< RTCPUSET_MAX_CPUS
))
134 ASMAtomicBitSet(pSet
, iCpu
);
142 * Removes a CPU given by its identifier from the set.
144 * @returns 0 on success, -1 if idCpu isn't valid.
145 * @param pSet Pointer to the set.
146 * @param idCpu The identifier of the CPU to delete.
147 * @remarks The modification is atomic.
149 DECLINLINE(int) RTCpuSetDel(PRTCPUSET pSet
, RTCPUID idCpu
)
151 int iCpu
= RTMpCpuIdToSetIndex(idCpu
);
152 if (RT_LIKELY(iCpu
>= 0))
154 ASMAtomicBitClear(pSet
, iCpu
);
162 * Removes a CPU given by its index from the set.
164 * @returns 0 on success, -1 if iCpu isn't valid.
165 * @param pSet Pointer to the set.
166 * @param iCpu The index of the CPU to delete.
167 * @remarks The modification is atomic.
169 DECLINLINE(int) RTCpuSetDelByIndex(PRTCPUSET pSet
, int iCpu
)
171 if (RT_LIKELY((unsigned)iCpu
< RTCPUSET_MAX_CPUS
))
173 ASMAtomicBitClear(pSet
, iCpu
);
181 * Checks if a CPU given by its identifier is a member of the set.
183 * @returns true / false accordingly.
184 * @param pSet Pointer to the set.
185 * @param idCpu The identifier of the CPU to look for.
186 * @remarks The test is atomic.
188 DECLINLINE(bool) RTCpuSetIsMember(PCRTCPUSET pSet
, RTCPUID idCpu
)
190 int iCpu
= RTMpCpuIdToSetIndex(idCpu
);
191 if (RT_LIKELY(iCpu
>= 0))
192 return ASMBitTest((volatile void *)pSet
, iCpu
);
198 * Checks if a CPU given by its index is a member of the set.
200 * @returns true / false accordingly.
201 * @param pSet Pointer to the set.
202 * @param iCpu The index of the CPU in the set.
203 * @remarks The test is atomic.
205 DECLINLINE(bool) RTCpuSetIsMemberByIndex(PCRTCPUSET pSet
, int iCpu
)
207 if (RT_LIKELY((unsigned)iCpu
< RTCPUSET_MAX_CPUS
))
208 return ASMBitTest((volatile void *)pSet
, iCpu
);
214 * Checks if the two sets match or not.
216 * @returns true / false accordingly.
217 * @param pSet1 The first set.
218 * @param pSet2 The second set.
220 DECLINLINE(bool) RTCpuSetIsEqual(PCRTCPUSET pSet1
, PCRTCPUSET pSet2
)
223 for (i
= 0; i
< RT_ELEMENTS(pSet1
->bmSet
); i
++)
224 if (pSet1
->bmSet
[i
] != pSet2
->bmSet
[i
])
231 * Checks if the CPU set is empty or not.
233 * @returns true / false accordingly.
234 * @param pSet Pointer to the set.
236 DECLINLINE(bool) RTCpuSetIsEmpty(PRTCPUSET pSet
)
239 for (i
= 0; i
< RT_ELEMENTS(pSet
->bmSet
); i
++)
247 * Converts the CPU set to a 64-bit mask.
250 * @param pSet Pointer to the set.
251 * @remarks Use with extreme care as it may lose information!
253 DECLINLINE(uint64_t) RTCpuSetToU64(PCRTCPUSET pSet
)
255 return pSet
->bmSet
[0];
260 * Initializes the CPU set from a 64-bit mask.
262 * @param pSet Pointer to the set.
263 * @param fMask The mask.
265 DECLINLINE(PRTCPUSET
) RTCpuSetFromU64(PRTCPUSET pSet
, uint64_t fMask
)
269 pSet
->bmSet
[0] = fMask
;
270 for (i
= 1; i
< RT_ELEMENTS(pSet
->bmSet
); i
++)
278 * Count the CPUs in the set.
280 * @returns CPU count.
281 * @param pSet Pointer to the set.
283 DECLINLINE(int) RTCpuSetCount(PCRTCPUSET pSet
)
288 for (i
= 0; i
< RT_ELEMENTS(pSet
->bmSet
); i
++)
290 uint64_t u64
= pSet
->bmSet
[i
];
307 * Get the highest set index.
309 * @returns The higest set index, -1 if all bits are clear.
310 * @param pSet Pointer to the set.
312 DECLINLINE(int) RTCpuLastIndex(PCRTCPUSET pSet
)
314 size_t i
= RT_ELEMENTS(pSet
->bmSet
);
317 uint64_t u64
= pSet
->bmSet
[i
];
320 /* There are more efficient ways to do this in asm.h... */
322 for (iBit
= 63; iBit
> 0; iBit
--)
324 if (u64
& RT_BIT_64(63))
328 return (int)i
* 64 + iBit
;