cpuset.h revision 0aaf889969ebdaba8a310db13adcec8c10174796
eb8ac3f1fc3835114d2c852c5c4e1b0300ef61b9vboxsync/** @file
eb8ac3f1fc3835114d2c852c5c4e1b0300ef61b9vboxsync * IPRT - CPU Set.
eb8ac3f1fc3835114d2c852c5c4e1b0300ef61b9vboxsync */
eb8ac3f1fc3835114d2c852c5c4e1b0300ef61b9vboxsync
eb8ac3f1fc3835114d2c852c5c4e1b0300ef61b9vboxsync/*
eb8ac3f1fc3835114d2c852c5c4e1b0300ef61b9vboxsync * Copyright (C) 2008 Sun Microsystems, Inc.
eb8ac3f1fc3835114d2c852c5c4e1b0300ef61b9vboxsync *
eb8ac3f1fc3835114d2c852c5c4e1b0300ef61b9vboxsync * This file is part of VirtualBox Open Source Edition (OSE), as
eb8ac3f1fc3835114d2c852c5c4e1b0300ef61b9vboxsync * available from http://www.virtualbox.org. This file is free software;
eb8ac3f1fc3835114d2c852c5c4e1b0300ef61b9vboxsync * you can redistribute it and/or modify it under the terms of the GNU
eb8ac3f1fc3835114d2c852c5c4e1b0300ef61b9vboxsync * General Public License (GPL) as published by the Free Software
eb8ac3f1fc3835114d2c852c5c4e1b0300ef61b9vboxsync * Foundation, in version 2 as it comes in the "COPYING" file of the
eb8ac3f1fc3835114d2c852c5c4e1b0300ef61b9vboxsync * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
eb8ac3f1fc3835114d2c852c5c4e1b0300ef61b9vboxsync * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
eb8ac3f1fc3835114d2c852c5c4e1b0300ef61b9vboxsync *
eb8ac3f1fc3835114d2c852c5c4e1b0300ef61b9vboxsync * The contents of this file may alternatively be used under the terms
eb8ac3f1fc3835114d2c852c5c4e1b0300ef61b9vboxsync * of the Common Development and Distribution License Version 1.0
eb8ac3f1fc3835114d2c852c5c4e1b0300ef61b9vboxsync * (CDDL) only, as it comes in the "COPYING.CDDL" file of the
eb8ac3f1fc3835114d2c852c5c4e1b0300ef61b9vboxsync * VirtualBox OSE distribution, in which case the provisions of the
eb8ac3f1fc3835114d2c852c5c4e1b0300ef61b9vboxsync * CDDL are applicable instead of those of the GPL.
eb8ac3f1fc3835114d2c852c5c4e1b0300ef61b9vboxsync *
eb8ac3f1fc3835114d2c852c5c4e1b0300ef61b9vboxsync * You may elect to license modified versions of this file under the
eb8ac3f1fc3835114d2c852c5c4e1b0300ef61b9vboxsync * terms and conditions of either the GPL or the CDDL or both.
eb8ac3f1fc3835114d2c852c5c4e1b0300ef61b9vboxsync *
eb8ac3f1fc3835114d2c852c5c4e1b0300ef61b9vboxsync * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
eb8ac3f1fc3835114d2c852c5c4e1b0300ef61b9vboxsync * Clara, CA 95054 USA or visit http://www.sun.com if you need
eb8ac3f1fc3835114d2c852c5c4e1b0300ef61b9vboxsync * additional information or have any questions.
eb8ac3f1fc3835114d2c852c5c4e1b0300ef61b9vboxsync */
eb8ac3f1fc3835114d2c852c5c4e1b0300ef61b9vboxsync
eb8ac3f1fc3835114d2c852c5c4e1b0300ef61b9vboxsync#ifndef ___iprt_cpuset_h
eb8ac3f1fc3835114d2c852c5c4e1b0300ef61b9vboxsync#define ___iprt_cpuset_h
eb8ac3f1fc3835114d2c852c5c4e1b0300ef61b9vboxsync
eb8ac3f1fc3835114d2c852c5c4e1b0300ef61b9vboxsync#include <iprt/types.h>
eb8ac3f1fc3835114d2c852c5c4e1b0300ef61b9vboxsync#include <iprt/mp.h> /* RTMpCpuIdToSetIndex */
eb8ac3f1fc3835114d2c852c5c4e1b0300ef61b9vboxsync#include <iprt/asm.h>
eb8ac3f1fc3835114d2c852c5c4e1b0300ef61b9vboxsync
eb8ac3f1fc3835114d2c852c5c4e1b0300ef61b9vboxsync
eb8ac3f1fc3835114d2c852c5c4e1b0300ef61b9vboxsync__BEGIN_DECLS
eb8ac3f1fc3835114d2c852c5c4e1b0300ef61b9vboxsync
eb8ac3f1fc3835114d2c852c5c4e1b0300ef61b9vboxsync/** @defgroup grp_rt_mp RTCpuSet - CPU Set
eb8ac3f1fc3835114d2c852c5c4e1b0300ef61b9vboxsync * @ingroup grp_rt
eb8ac3f1fc3835114d2c852c5c4e1b0300ef61b9vboxsync * @{
eb8ac3f1fc3835114d2c852c5c4e1b0300ef61b9vboxsync */
eb8ac3f1fc3835114d2c852c5c4e1b0300ef61b9vboxsync
eb8ac3f1fc3835114d2c852c5c4e1b0300ef61b9vboxsync/**
eb8ac3f1fc3835114d2c852c5c4e1b0300ef61b9vboxsync * The maximum number of CPUs a set can contain and IPRT is able
eb8ac3f1fc3835114d2c852c5c4e1b0300ef61b9vboxsync * to reference.
eb8ac3f1fc3835114d2c852c5c4e1b0300ef61b9vboxsync * @remarks This is the maximum value of the supported platforms.
eb8ac3f1fc3835114d2c852c5c4e1b0300ef61b9vboxsync */
eb8ac3f1fc3835114d2c852c5c4e1b0300ef61b9vboxsync#define RTCPUSET_MAX_CPUS 64
eb8ac3f1fc3835114d2c852c5c4e1b0300ef61b9vboxsync
eb8ac3f1fc3835114d2c852c5c4e1b0300ef61b9vboxsync/**
eb8ac3f1fc3835114d2c852c5c4e1b0300ef61b9vboxsync * Clear all CPUs.
eb8ac3f1fc3835114d2c852c5c4e1b0300ef61b9vboxsync *
eb8ac3f1fc3835114d2c852c5c4e1b0300ef61b9vboxsync * @returns pSet.
eb8ac3f1fc3835114d2c852c5c4e1b0300ef61b9vboxsync * @param pSet Pointer to the set.
eb8ac3f1fc3835114d2c852c5c4e1b0300ef61b9vboxsync */
eb8ac3f1fc3835114d2c852c5c4e1b0300ef61b9vboxsyncDECLINLINE(PRTCPUSET) RTCpuSetEmpty(PRTCPUSET pSet)
eb8ac3f1fc3835114d2c852c5c4e1b0300ef61b9vboxsync{
eb8ac3f1fc3835114d2c852c5c4e1b0300ef61b9vboxsync *pSet = 0;
eb8ac3f1fc3835114d2c852c5c4e1b0300ef61b9vboxsync return pSet;
eb8ac3f1fc3835114d2c852c5c4e1b0300ef61b9vboxsync}
728a63f5d10493d000f641eaee97cdf62a7fd421vboxsync
eb8ac3f1fc3835114d2c852c5c4e1b0300ef61b9vboxsync
eb8ac3f1fc3835114d2c852c5c4e1b0300ef61b9vboxsync/**
eb8ac3f1fc3835114d2c852c5c4e1b0300ef61b9vboxsync * Set all CPUs.
eb8ac3f1fc3835114d2c852c5c4e1b0300ef61b9vboxsync *
eb8ac3f1fc3835114d2c852c5c4e1b0300ef61b9vboxsync * @returns pSet.
eb8ac3f1fc3835114d2c852c5c4e1b0300ef61b9vboxsync * @param pSet Pointer to the set.
eb8ac3f1fc3835114d2c852c5c4e1b0300ef61b9vboxsync */
eb8ac3f1fc3835114d2c852c5c4e1b0300ef61b9vboxsyncDECLINLINE(PRTCPUSET) RTCpuSetFill(PRTCPUSET pSet)
eb8ac3f1fc3835114d2c852c5c4e1b0300ef61b9vboxsync{
eb8ac3f1fc3835114d2c852c5c4e1b0300ef61b9vboxsync *pSet = UINT64_MAX;
eb8ac3f1fc3835114d2c852c5c4e1b0300ef61b9vboxsync return pSet;
be1d2a01ea76c7617fb635c9036f9295628de297vboxsync}
be1d2a01ea76c7617fb635c9036f9295628de297vboxsync
be1d2a01ea76c7617fb635c9036f9295628de297vboxsync
be1d2a01ea76c7617fb635c9036f9295628de297vboxsync/**
be1d2a01ea76c7617fb635c9036f9295628de297vboxsync * Adds a CPU given by it's identifier to the set.
be1d2a01ea76c7617fb635c9036f9295628de297vboxsync *
be1d2a01ea76c7617fb635c9036f9295628de297vboxsync * @returns 0 on success, -1 if idCpu isn't valid.
be1d2a01ea76c7617fb635c9036f9295628de297vboxsync * @param pSet Pointer to the set.
be1d2a01ea76c7617fb635c9036f9295628de297vboxsync * @param idCpu The identifier of the CPU to add.
be1d2a01ea76c7617fb635c9036f9295628de297vboxsync * @remarks The modification is atomic.
be1d2a01ea76c7617fb635c9036f9295628de297vboxsync */
be1d2a01ea76c7617fb635c9036f9295628de297vboxsyncDECLINLINE(int) RTCpuSetAdd(PRTCPUSET pSet, RTCPUID idCpu)
eb8ac3f1fc3835114d2c852c5c4e1b0300ef61b9vboxsync{
eb8ac3f1fc3835114d2c852c5c4e1b0300ef61b9vboxsync int iCpu = RTMpCpuIdToSetIndex(idCpu);
eb8ac3f1fc3835114d2c852c5c4e1b0300ef61b9vboxsync if (RT_UNLIKELY(iCpu < 0))
eb8ac3f1fc3835114d2c852c5c4e1b0300ef61b9vboxsync return -1;
eb8ac3f1fc3835114d2c852c5c4e1b0300ef61b9vboxsync ASMAtomicBitSet(pSet, iCpu);
eb8ac3f1fc3835114d2c852c5c4e1b0300ef61b9vboxsync return 0;
eb8ac3f1fc3835114d2c852c5c4e1b0300ef61b9vboxsync}
eb8ac3f1fc3835114d2c852c5c4e1b0300ef61b9vboxsync
eb8ac3f1fc3835114d2c852c5c4e1b0300ef61b9vboxsync
eb8ac3f1fc3835114d2c852c5c4e1b0300ef61b9vboxsync/**
eb8ac3f1fc3835114d2c852c5c4e1b0300ef61b9vboxsync * Removes a CPU given by it's identifier from the set.
eb8ac3f1fc3835114d2c852c5c4e1b0300ef61b9vboxsync *
eb8ac3f1fc3835114d2c852c5c4e1b0300ef61b9vboxsync * @returns 0 on success, -1 if idCpu isn't valid.
eb8ac3f1fc3835114d2c852c5c4e1b0300ef61b9vboxsync * @param pSet Pointer to the set.
eb8ac3f1fc3835114d2c852c5c4e1b0300ef61b9vboxsync * @param idCpu The identifier of the CPU to delete.
eb8ac3f1fc3835114d2c852c5c4e1b0300ef61b9vboxsync * @remarks The modification is atomic.
eb8ac3f1fc3835114d2c852c5c4e1b0300ef61b9vboxsync */
eb8ac3f1fc3835114d2c852c5c4e1b0300ef61b9vboxsyncDECLINLINE(int) RTCpuSetDel(PRTCPUSET pSet, RTCPUID idCpu)
eb8ac3f1fc3835114d2c852c5c4e1b0300ef61b9vboxsync{
eb8ac3f1fc3835114d2c852c5c4e1b0300ef61b9vboxsync int iCpu = RTMpCpuIdToSetIndex(idCpu);
eb8ac3f1fc3835114d2c852c5c4e1b0300ef61b9vboxsync if (RT_UNLIKELY(iCpu < 0))
eb8ac3f1fc3835114d2c852c5c4e1b0300ef61b9vboxsync return -1;
eb8ac3f1fc3835114d2c852c5c4e1b0300ef61b9vboxsync ASMAtomicBitClear(pSet, iCpu);
eb8ac3f1fc3835114d2c852c5c4e1b0300ef61b9vboxsync return 0;
eb8ac3f1fc3835114d2c852c5c4e1b0300ef61b9vboxsync}
eb8ac3f1fc3835114d2c852c5c4e1b0300ef61b9vboxsync
eb8ac3f1fc3835114d2c852c5c4e1b0300ef61b9vboxsync
eb8ac3f1fc3835114d2c852c5c4e1b0300ef61b9vboxsync/**
eb8ac3f1fc3835114d2c852c5c4e1b0300ef61b9vboxsync * Checks if a CPU given by it's identifier is a member of the set.
eb8ac3f1fc3835114d2c852c5c4e1b0300ef61b9vboxsync *
eb8ac3f1fc3835114d2c852c5c4e1b0300ef61b9vboxsync * @returns true / false accordingly.
eb8ac3f1fc3835114d2c852c5c4e1b0300ef61b9vboxsync * @param pSet Pointer to the set.
eb8ac3f1fc3835114d2c852c5c4e1b0300ef61b9vboxsync * @param idCpu The identifier of the CPU to look for.
eb8ac3f1fc3835114d2c852c5c4e1b0300ef61b9vboxsync * @remarks The test is atomic.
eb8ac3f1fc3835114d2c852c5c4e1b0300ef61b9vboxsync */
eb8ac3f1fc3835114d2c852c5c4e1b0300ef61b9vboxsyncDECLINLINE(bool) RTCpuSetIsMember(PCRTCPUSET pSet, RTCPUID idCpu)
eb8ac3f1fc3835114d2c852c5c4e1b0300ef61b9vboxsync{
eb8ac3f1fc3835114d2c852c5c4e1b0300ef61b9vboxsync int iCpu = RTMpCpuIdToSetIndex(idCpu);
eb8ac3f1fc3835114d2c852c5c4e1b0300ef61b9vboxsync if (RT_UNLIKELY(iCpu < 0))
eb8ac3f1fc3835114d2c852c5c4e1b0300ef61b9vboxsync return false;
eb8ac3f1fc3835114d2c852c5c4e1b0300ef61b9vboxsync return ASMBitTest((volatile void *)pSet, iCpu);
eb8ac3f1fc3835114d2c852c5c4e1b0300ef61b9vboxsync}
eb8ac3f1fc3835114d2c852c5c4e1b0300ef61b9vboxsync
eb8ac3f1fc3835114d2c852c5c4e1b0300ef61b9vboxsync
eb8ac3f1fc3835114d2c852c5c4e1b0300ef61b9vboxsync/**
eb8ac3f1fc3835114d2c852c5c4e1b0300ef61b9vboxsync * Converts the CPU set to a 64-bit mask.
eb8ac3f1fc3835114d2c852c5c4e1b0300ef61b9vboxsync *
eb8ac3f1fc3835114d2c852c5c4e1b0300ef61b9vboxsync * @returns The mask.
eb8ac3f1fc3835114d2c852c5c4e1b0300ef61b9vboxsync * @param pSet Pointer to the set.
eb8ac3f1fc3835114d2c852c5c4e1b0300ef61b9vboxsync */
eb8ac3f1fc3835114d2c852c5c4e1b0300ef61b9vboxsyncDECLINLINE(uint64_t) RTCpuSetToU64(PCRTCPUSET pSet)
eb8ac3f1fc3835114d2c852c5c4e1b0300ef61b9vboxsync{
eb8ac3f1fc3835114d2c852c5c4e1b0300ef61b9vboxsync return *pSet;
eb8ac3f1fc3835114d2c852c5c4e1b0300ef61b9vboxsync}
eb8ac3f1fc3835114d2c852c5c4e1b0300ef61b9vboxsync
eb8ac3f1fc3835114d2c852c5c4e1b0300ef61b9vboxsync
eb8ac3f1fc3835114d2c852c5c4e1b0300ef61b9vboxsync/**
eb8ac3f1fc3835114d2c852c5c4e1b0300ef61b9vboxsync * Initializes the CPU set from a 64-bit mask.
eb8ac3f1fc3835114d2c852c5c4e1b0300ef61b9vboxsync *
eb8ac3f1fc3835114d2c852c5c4e1b0300ef61b9vboxsync * @param pSet Pointer to the set.
eb8ac3f1fc3835114d2c852c5c4e1b0300ef61b9vboxsync * @param fMask The mask.
eb8ac3f1fc3835114d2c852c5c4e1b0300ef61b9vboxsync */
eb8ac3f1fc3835114d2c852c5c4e1b0300ef61b9vboxsyncDECLINLINE(PRTCPUSET) RTCpuSetFromU64(PRTCPUSET pSet, uint64_t fMask)
eb8ac3f1fc3835114d2c852c5c4e1b0300ef61b9vboxsync{
eb8ac3f1fc3835114d2c852c5c4e1b0300ef61b9vboxsync *pSet = fMask;
eb8ac3f1fc3835114d2c852c5c4e1b0300ef61b9vboxsync return pSet;
eb8ac3f1fc3835114d2c852c5c4e1b0300ef61b9vboxsync}
eb8ac3f1fc3835114d2c852c5c4e1b0300ef61b9vboxsync
eb8ac3f1fc3835114d2c852c5c4e1b0300ef61b9vboxsync
eb8ac3f1fc3835114d2c852c5c4e1b0300ef61b9vboxsync/**
eb8ac3f1fc3835114d2c852c5c4e1b0300ef61b9vboxsync * Count the CPUs in the set.
eb8ac3f1fc3835114d2c852c5c4e1b0300ef61b9vboxsync *
eb8ac3f1fc3835114d2c852c5c4e1b0300ef61b9vboxsync * @returns CPU count.
eb8ac3f1fc3835114d2c852c5c4e1b0300ef61b9vboxsync * @param pSet Pointer to the set.
eb8ac3f1fc3835114d2c852c5c4e1b0300ef61b9vboxsync */
eb8ac3f1fc3835114d2c852c5c4e1b0300ef61b9vboxsyncDECLINLINE(RTCPUID) RTCpuSetCount(PRTCPUSET pSet)
eb8ac3f1fc3835114d2c852c5c4e1b0300ef61b9vboxsync{
eb8ac3f1fc3835114d2c852c5c4e1b0300ef61b9vboxsync RTCPUID cCpus = 0;
eb8ac3f1fc3835114d2c852c5c4e1b0300ef61b9vboxsync RTCPUID iCpu = 64;
eb8ac3f1fc3835114d2c852c5c4e1b0300ef61b9vboxsync while (iCpu-- > 0)
eb8ac3f1fc3835114d2c852c5c4e1b0300ef61b9vboxsync if (*pSet & RT_BIT_64(iCpu))
eb8ac3f1fc3835114d2c852c5c4e1b0300ef61b9vboxsync cCpus++;
eb8ac3f1fc3835114d2c852c5c4e1b0300ef61b9vboxsync return cCpus;
eb8ac3f1fc3835114d2c852c5c4e1b0300ef61b9vboxsync}
eb8ac3f1fc3835114d2c852c5c4e1b0300ef61b9vboxsync
eb8ac3f1fc3835114d2c852c5c4e1b0300ef61b9vboxsync
eb8ac3f1fc3835114d2c852c5c4e1b0300ef61b9vboxsync/** @} */
eb8ac3f1fc3835114d2c852c5c4e1b0300ef61b9vboxsync
eb8ac3f1fc3835114d2c852c5c4e1b0300ef61b9vboxsync__END_DECLS
eb8ac3f1fc3835114d2c852c5c4e1b0300ef61b9vboxsync
be1d2a01ea76c7617fb635c9036f9295628de297vboxsync#endif
eb8ac3f1fc3835114d2c852c5c4e1b0300ef61b9vboxsync
eb8ac3f1fc3835114d2c852c5c4e1b0300ef61b9vboxsync
eb8ac3f1fc3835114d2c852c5c4e1b0300ef61b9vboxsync