cpuset.h revision 5c1381fc884d30a749517579368ff6cb4b43e809
5c1381fc884d30a749517579368ff6cb4b43e809vboxsync/** @file
5c1381fc884d30a749517579368ff6cb4b43e809vboxsync * innotek Portable Runtime - CPU Set.
5c1381fc884d30a749517579368ff6cb4b43e809vboxsync */
5c1381fc884d30a749517579368ff6cb4b43e809vboxsync
5c1381fc884d30a749517579368ff6cb4b43e809vboxsync/*
5c1381fc884d30a749517579368ff6cb4b43e809vboxsync * Copyright (C) 2008 innotek GmbH
5c1381fc884d30a749517579368ff6cb4b43e809vboxsync *
5c1381fc884d30a749517579368ff6cb4b43e809vboxsync * This file is part of VirtualBox Open Source Edition (OSE), as
5c1381fc884d30a749517579368ff6cb4b43e809vboxsync * available from http://www.virtualbox.org. This file is free software;
5c1381fc884d30a749517579368ff6cb4b43e809vboxsync * you can redistribute it and/or modify it under the terms of the GNU
5c1381fc884d30a749517579368ff6cb4b43e809vboxsync * General Public License (GPL) as published by the Free Software
5c1381fc884d30a749517579368ff6cb4b43e809vboxsync * Foundation, in version 2 as it comes in the "COPYING" file of the
5c1381fc884d30a749517579368ff6cb4b43e809vboxsync * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
5c1381fc884d30a749517579368ff6cb4b43e809vboxsync * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
5c1381fc884d30a749517579368ff6cb4b43e809vboxsync *
5c1381fc884d30a749517579368ff6cb4b43e809vboxsync * The contents of this file may alternatively be used under the terms
5c1381fc884d30a749517579368ff6cb4b43e809vboxsync * of the Common Development and Distribution License Version 1.0
5c1381fc884d30a749517579368ff6cb4b43e809vboxsync * (CDDL) only, as it comes in the "COPYING.CDDL" file of the
5c1381fc884d30a749517579368ff6cb4b43e809vboxsync * VirtualBox OSE distribution, in which case the provisions of the
5c1381fc884d30a749517579368ff6cb4b43e809vboxsync * CDDL are applicable instead of those of the GPL.
5c1381fc884d30a749517579368ff6cb4b43e809vboxsync *
5c1381fc884d30a749517579368ff6cb4b43e809vboxsync * You may elect to license modified versions of this file under the
5c1381fc884d30a749517579368ff6cb4b43e809vboxsync * terms and conditions of either the GPL or the CDDL or both.
5c1381fc884d30a749517579368ff6cb4b43e809vboxsync */
5c1381fc884d30a749517579368ff6cb4b43e809vboxsync
5c1381fc884d30a749517579368ff6cb4b43e809vboxsync#ifndef ___iprt_cpuset_h
5c1381fc884d30a749517579368ff6cb4b43e809vboxsync#define ___iprt_cpuset_h
5c1381fc884d30a749517579368ff6cb4b43e809vboxsync
5c1381fc884d30a749517579368ff6cb4b43e809vboxsync#include <iprt/types.h>
5c1381fc884d30a749517579368ff6cb4b43e809vboxsync#include <iprt/mp.h> /* RTMpCpuIdToSetIndex */
5c1381fc884d30a749517579368ff6cb4b43e809vboxsync
5c1381fc884d30a749517579368ff6cb4b43e809vboxsync
5c1381fc884d30a749517579368ff6cb4b43e809vboxsync__BEGIN_DECLS
5c1381fc884d30a749517579368ff6cb4b43e809vboxsync
5c1381fc884d30a749517579368ff6cb4b43e809vboxsync/** @defgroup grp_rt_mp RTCpuSet - CPU Set
5c1381fc884d30a749517579368ff6cb4b43e809vboxsync * @ingroup grp_rt
5c1381fc884d30a749517579368ff6cb4b43e809vboxsync * @{
5c1381fc884d30a749517579368ff6cb4b43e809vboxsync */
5c1381fc884d30a749517579368ff6cb4b43e809vboxsync
5c1381fc884d30a749517579368ff6cb4b43e809vboxsync/**
5c1381fc884d30a749517579368ff6cb4b43e809vboxsync * Clear all CPUs.
5c1381fc884d30a749517579368ff6cb4b43e809vboxsync *
5c1381fc884d30a749517579368ff6cb4b43e809vboxsync * @param pSet Pointer to the set.
5c1381fc884d30a749517579368ff6cb4b43e809vboxsync */
5c1381fc884d30a749517579368ff6cb4b43e809vboxsyncDECLINLINE(void) RTCpuSetEmpty(PRTCPUSET pSet)
5c1381fc884d30a749517579368ff6cb4b43e809vboxsync{
5c1381fc884d30a749517579368ff6cb4b43e809vboxsync *pSet = 0;
5c1381fc884d30a749517579368ff6cb4b43e809vboxsync}
5c1381fc884d30a749517579368ff6cb4b43e809vboxsync
5c1381fc884d30a749517579368ff6cb4b43e809vboxsync
5c1381fc884d30a749517579368ff6cb4b43e809vboxsync/**
5c1381fc884d30a749517579368ff6cb4b43e809vboxsync * Set all CPUs.
5c1381fc884d30a749517579368ff6cb4b43e809vboxsync *
5c1381fc884d30a749517579368ff6cb4b43e809vboxsync * @param pSet Pointer to the set.
5c1381fc884d30a749517579368ff6cb4b43e809vboxsync */
5c1381fc884d30a749517579368ff6cb4b43e809vboxsyncDECLINLINE(void) RTCpuSetFill(PRTCPUSET pSet)
5c1381fc884d30a749517579368ff6cb4b43e809vboxsync{
5c1381fc884d30a749517579368ff6cb4b43e809vboxsync *pSet = UINT64_MAX;
5c1381fc884d30a749517579368ff6cb4b43e809vboxsync}
5c1381fc884d30a749517579368ff6cb4b43e809vboxsync
5c1381fc884d30a749517579368ff6cb4b43e809vboxsync
5c1381fc884d30a749517579368ff6cb4b43e809vboxsync/**
5c1381fc884d30a749517579368ff6cb4b43e809vboxsync * Adds a CPU given by it's identifier to the set.
5c1381fc884d30a749517579368ff6cb4b43e809vboxsync *
5c1381fc884d30a749517579368ff6cb4b43e809vboxsync * @returns 0 on success, -1 if idCpu isn't valid.
5c1381fc884d30a749517579368ff6cb4b43e809vboxsync * @param pSet Pointer to the set.
5c1381fc884d30a749517579368ff6cb4b43e809vboxsync * @param idCpu The identifier of the CPU to add.
5c1381fc884d30a749517579368ff6cb4b43e809vboxsync */
5c1381fc884d30a749517579368ff6cb4b43e809vboxsyncDECLINLINE(int) RTCpuSetAdd(PRTCPUSET pSet, RTCPUID idCpu)
5c1381fc884d30a749517579368ff6cb4b43e809vboxsync{
5c1381fc884d30a749517579368ff6cb4b43e809vboxsync int iCpu = RTMpCpuIdToSetIndex(idCpu);
5c1381fc884d30a749517579368ff6cb4b43e809vboxsync if (RT_UNLIKELY(iCpu < 0))
5c1381fc884d30a749517579368ff6cb4b43e809vboxsync return -1;
5c1381fc884d30a749517579368ff6cb4b43e809vboxsync *pSet |= RT_BIT_64(iCpu);
5c1381fc884d30a749517579368ff6cb4b43e809vboxsync return 0;
5c1381fc884d30a749517579368ff6cb4b43e809vboxsync}
5c1381fc884d30a749517579368ff6cb4b43e809vboxsync
5c1381fc884d30a749517579368ff6cb4b43e809vboxsync
5c1381fc884d30a749517579368ff6cb4b43e809vboxsync/**
5c1381fc884d30a749517579368ff6cb4b43e809vboxsync * Removes a CPU given by it's identifier from the set.
5c1381fc884d30a749517579368ff6cb4b43e809vboxsync *
5c1381fc884d30a749517579368ff6cb4b43e809vboxsync * @returns 0 on success, -1 if idCpu isn't valid.
5c1381fc884d30a749517579368ff6cb4b43e809vboxsync * @param pSet Pointer to the set.
5c1381fc884d30a749517579368ff6cb4b43e809vboxsync * @param idCpu The identifier of the CPU to delete.
5c1381fc884d30a749517579368ff6cb4b43e809vboxsync */
5c1381fc884d30a749517579368ff6cb4b43e809vboxsyncDECLINLINE(int) RTCpuSetDel(PRTCPUSET pSet, RTCPUID idCpu)
5c1381fc884d30a749517579368ff6cb4b43e809vboxsync{
5c1381fc884d30a749517579368ff6cb4b43e809vboxsync int iCpu = RTMpCpuIdToSetIndex(idCpu);
5c1381fc884d30a749517579368ff6cb4b43e809vboxsync if (RT_UNLIKELY(iCpu < 0))
5c1381fc884d30a749517579368ff6cb4b43e809vboxsync return -1;
5c1381fc884d30a749517579368ff6cb4b43e809vboxsync *pSet &= ~RT_BIT_64(iCpu);
5c1381fc884d30a749517579368ff6cb4b43e809vboxsync return 0;
5c1381fc884d30a749517579368ff6cb4b43e809vboxsync}
5c1381fc884d30a749517579368ff6cb4b43e809vboxsync
5c1381fc884d30a749517579368ff6cb4b43e809vboxsync
5c1381fc884d30a749517579368ff6cb4b43e809vboxsync/**
5c1381fc884d30a749517579368ff6cb4b43e809vboxsync * Checks if a CPU given by it's identifier is a member of the set.
5c1381fc884d30a749517579368ff6cb4b43e809vboxsync *
5c1381fc884d30a749517579368ff6cb4b43e809vboxsync * @returns true / false accordingly.
5c1381fc884d30a749517579368ff6cb4b43e809vboxsync * @param pSet Pointer to the set.
5c1381fc884d30a749517579368ff6cb4b43e809vboxsync * @param idCpu The identifier of the CPU to look for.
5c1381fc884d30a749517579368ff6cb4b43e809vboxsync */
5c1381fc884d30a749517579368ff6cb4b43e809vboxsyncDECLINLINE(int) RTCpuSetIsMember(PCRTCPUSET pSet, RTCPUID idCpu)
5c1381fc884d30a749517579368ff6cb4b43e809vboxsync{
5c1381fc884d30a749517579368ff6cb4b43e809vboxsync int iCpu = RTMpCpuIdToSetIndex(idCpu);
5c1381fc884d30a749517579368ff6cb4b43e809vboxsync if (RT_UNLIKELY(iCpu < 0))
5c1381fc884d30a749517579368ff6cb4b43e809vboxsync return false;
5c1381fc884d30a749517579368ff6cb4b43e809vboxsync return !!(*pSet & RT_BIT_64(iCpu));
5c1381fc884d30a749517579368ff6cb4b43e809vboxsync}
5c1381fc884d30a749517579368ff6cb4b43e809vboxsync
5c1381fc884d30a749517579368ff6cb4b43e809vboxsync
5c1381fc884d30a749517579368ff6cb4b43e809vboxsync/**
5c1381fc884d30a749517579368ff6cb4b43e809vboxsync * Converts the CPU set to a 64-bit mask.
5c1381fc884d30a749517579368ff6cb4b43e809vboxsync *
5c1381fc884d30a749517579368ff6cb4b43e809vboxsync * @returns The mask.
5c1381fc884d30a749517579368ff6cb4b43e809vboxsync * @param pSet Pointer to the set.
5c1381fc884d30a749517579368ff6cb4b43e809vboxsync */
5c1381fc884d30a749517579368ff6cb4b43e809vboxsyncDECLINLINE(uint64_t) RTCpuSetToU64(PCRTCPUSET pSet)
5c1381fc884d30a749517579368ff6cb4b43e809vboxsync{
5c1381fc884d30a749517579368ff6cb4b43e809vboxsync return *pSet;
5c1381fc884d30a749517579368ff6cb4b43e809vboxsync}
5c1381fc884d30a749517579368ff6cb4b43e809vboxsync
5c1381fc884d30a749517579368ff6cb4b43e809vboxsync
5c1381fc884d30a749517579368ff6cb4b43e809vboxsync/**
5c1381fc884d30a749517579368ff6cb4b43e809vboxsync * Initializes the CPU set from a 64-bit mask.
5c1381fc884d30a749517579368ff6cb4b43e809vboxsync *
5c1381fc884d30a749517579368ff6cb4b43e809vboxsync * @param pSet Pointer to the set.
5c1381fc884d30a749517579368ff6cb4b43e809vboxsync * @param fMask The mask.
5c1381fc884d30a749517579368ff6cb4b43e809vboxsync */
5c1381fc884d30a749517579368ff6cb4b43e809vboxsyncDECLINLINE(void) RTCpuSetFromU64(PRTCPUSET pSet, uint64_t fMask)
5c1381fc884d30a749517579368ff6cb4b43e809vboxsync{
5c1381fc884d30a749517579368ff6cb4b43e809vboxsync *pSet = fMask;
5c1381fc884d30a749517579368ff6cb4b43e809vboxsync}
5c1381fc884d30a749517579368ff6cb4b43e809vboxsync
5c1381fc884d30a749517579368ff6cb4b43e809vboxsync
5c1381fc884d30a749517579368ff6cb4b43e809vboxsync/**
5c1381fc884d30a749517579368ff6cb4b43e809vboxsync * Gets the identifier of the CPU executing the call.
5c1381fc884d30a749517579368ff6cb4b43e809vboxsync *
5c1381fc884d30a749517579368ff6cb4b43e809vboxsync * When called from a system mode where scheduling is active, like ring-3 or
5c1381fc884d30a749517579368ff6cb4b43e809vboxsync * kernel mode with interrupts enabled on some systems, no assumptions should
5c1381fc884d30a749517579368ff6cb4b43e809vboxsync * be made about the current CPU when the call returns.
5c1381fc884d30a749517579368ff6cb4b43e809vboxsync *
5c1381fc884d30a749517579368ff6cb4b43e809vboxsync * @returns CPU Id.
5c1381fc884d30a749517579368ff6cb4b43e809vboxsync */
5c1381fc884d30a749517579368ff6cb4b43e809vboxsyncRTDECL(RTCPUID) RTMpCpuId(void);
5c1381fc884d30a749517579368ff6cb4b43e809vboxsync
5c1381fc884d30a749517579368ff6cb4b43e809vboxsync/**
5c1381fc884d30a749517579368ff6cb4b43e809vboxsync * Checks if a CPU is online or not.
5c1381fc884d30a749517579368ff6cb4b43e809vboxsync *
5c1381fc884d30a749517579368ff6cb4b43e809vboxsync * @returns true/false accordingly.
5c1381fc884d30a749517579368ff6cb4b43e809vboxsync * @param idCpu The identifier of the CPU.
5c1381fc884d30a749517579368ff6cb4b43e809vboxsync */
5c1381fc884d30a749517579368ff6cb4b43e809vboxsyncRTDECL(bool) RTMpIsCpuOnline(RTCPUID idCpu);
5c1381fc884d30a749517579368ff6cb4b43e809vboxsync
5c1381fc884d30a749517579368ff6cb4b43e809vboxsync/**
5c1381fc884d30a749517579368ff6cb4b43e809vboxsync * Checks if a CPU exist or not / validates a CPU id.
5c1381fc884d30a749517579368ff6cb4b43e809vboxsync *
5c1381fc884d30a749517579368ff6cb4b43e809vboxsync * @returns true/false accordingly.
5c1381fc884d30a749517579368ff6cb4b43e809vboxsync * @param idCpu The identifier of the CPU.
5c1381fc884d30a749517579368ff6cb4b43e809vboxsync */
5c1381fc884d30a749517579368ff6cb4b43e809vboxsyncRTDECL(bool) RTMpDoesCpuExist(RTCPUID idCpu);
5c1381fc884d30a749517579368ff6cb4b43e809vboxsync
5c1381fc884d30a749517579368ff6cb4b43e809vboxsync/**
5c1381fc884d30a749517579368ff6cb4b43e809vboxsync * Converts a CPU identifier to a CPU set index.
5c1381fc884d30a749517579368ff6cb4b43e809vboxsync *
5c1381fc884d30a749517579368ff6cb4b43e809vboxsync * @returns The CPU set index on success, -1 on failure.
5c1381fc884d30a749517579368ff6cb4b43e809vboxsync * @param idCpu The identifier of the CPU.
5c1381fc884d30a749517579368ff6cb4b43e809vboxsync */
5c1381fc884d30a749517579368ff6cb4b43e809vboxsyncRTDECL(int) RTMpCpuIdToSetIndex(RTCPUID idCpu);
5c1381fc884d30a749517579368ff6cb4b43e809vboxsync
5c1381fc884d30a749517579368ff6cb4b43e809vboxsync
5c1381fc884d30a749517579368ff6cb4b43e809vboxsync/** @} */
5c1381fc884d30a749517579368ff6cb4b43e809vboxsync
5c1381fc884d30a749517579368ff6cb4b43e809vboxsync__END_DECLS
5c1381fc884d30a749517579368ff6cb4b43e809vboxsync
5c1381fc884d30a749517579368ff6cb4b43e809vboxsync#endif
5c1381fc884d30a749517579368ff6cb4b43e809vboxsync
5c1381fc884d30a749517579368ff6cb4b43e809vboxsync