0cde281779e97ead3181bbd3b628451fa2b1efe1vboxsync/* $Id$ */
0cde281779e97ead3181bbd3b628451fa2b1efe1vboxsync/** @file
0cde281779e97ead3181bbd3b628451fa2b1efe1vboxsync * IPRT - Multiprocessor, Generic RTMpGetArraySize.
0cde281779e97ead3181bbd3b628451fa2b1efe1vboxsync */
0cde281779e97ead3181bbd3b628451fa2b1efe1vboxsync
0cde281779e97ead3181bbd3b628451fa2b1efe1vboxsync/*
c58f1213e628a545081c70e26c6b67a841cff880vboxsync * Copyright (C) 2010-2012 Oracle Corporation
0cde281779e97ead3181bbd3b628451fa2b1efe1vboxsync *
0cde281779e97ead3181bbd3b628451fa2b1efe1vboxsync * This file is part of VirtualBox Open Source Edition (OSE), as
0cde281779e97ead3181bbd3b628451fa2b1efe1vboxsync * available from http://www.virtualbox.org. This file is free software;
0cde281779e97ead3181bbd3b628451fa2b1efe1vboxsync * you can redistribute it and/or modify it under the terms of the GNU
0cde281779e97ead3181bbd3b628451fa2b1efe1vboxsync * General Public License (GPL) as published by the Free Software
0cde281779e97ead3181bbd3b628451fa2b1efe1vboxsync * Foundation, in version 2 as it comes in the "COPYING" file of the
0cde281779e97ead3181bbd3b628451fa2b1efe1vboxsync * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
0cde281779e97ead3181bbd3b628451fa2b1efe1vboxsync * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
0cde281779e97ead3181bbd3b628451fa2b1efe1vboxsync *
0cde281779e97ead3181bbd3b628451fa2b1efe1vboxsync * The contents of this file may alternatively be used under the terms
0cde281779e97ead3181bbd3b628451fa2b1efe1vboxsync * of the Common Development and Distribution License Version 1.0
0cde281779e97ead3181bbd3b628451fa2b1efe1vboxsync * (CDDL) only, as it comes in the "COPYING.CDDL" file of the
0cde281779e97ead3181bbd3b628451fa2b1efe1vboxsync * VirtualBox OSE distribution, in which case the provisions of the
0cde281779e97ead3181bbd3b628451fa2b1efe1vboxsync * CDDL are applicable instead of those of the GPL.
0cde281779e97ead3181bbd3b628451fa2b1efe1vboxsync *
0cde281779e97ead3181bbd3b628451fa2b1efe1vboxsync * You may elect to license modified versions of this file under the
0cde281779e97ead3181bbd3b628451fa2b1efe1vboxsync * terms and conditions of either the GPL or the CDDL or both.
0cde281779e97ead3181bbd3b628451fa2b1efe1vboxsync */
0cde281779e97ead3181bbd3b628451fa2b1efe1vboxsync
0cde281779e97ead3181bbd3b628451fa2b1efe1vboxsync
0cde281779e97ead3181bbd3b628451fa2b1efe1vboxsync/*******************************************************************************
0cde281779e97ead3181bbd3b628451fa2b1efe1vboxsync* Header Files *
0cde281779e97ead3181bbd3b628451fa2b1efe1vboxsync*******************************************************************************/
0cde281779e97ead3181bbd3b628451fa2b1efe1vboxsync#include <iprt/mp.h>
0cde281779e97ead3181bbd3b628451fa2b1efe1vboxsync#include "internal/iprt.h"
0cde281779e97ead3181bbd3b628451fa2b1efe1vboxsync
cd7a8c034836a94a0c4f0adf9764f9ed661cdd01vboxsync#include <iprt/asm.h>
cd7a8c034836a94a0c4f0adf9764f9ed661cdd01vboxsync#include <iprt/cpuset.h>
cd7a8c034836a94a0c4f0adf9764f9ed661cdd01vboxsync
0cde281779e97ead3181bbd3b628451fa2b1efe1vboxsync
0cde281779e97ead3181bbd3b628451fa2b1efe1vboxsyncRTDECL(uint32_t) RTMpGetArraySize(void)
0cde281779e97ead3181bbd3b628451fa2b1efe1vboxsync{
cd7a8c034836a94a0c4f0adf9764f9ed661cdd01vboxsync /*
cd7a8c034836a94a0c4f0adf9764f9ed661cdd01vboxsync * Cache the result here. This whole point of this function is that it
cd7a8c034836a94a0c4f0adf9764f9ed661cdd01vboxsync * will always return the same value, so that should be safe.
cd7a8c034836a94a0c4f0adf9764f9ed661cdd01vboxsync *
cd7a8c034836a94a0c4f0adf9764f9ed661cdd01vboxsync * Note! Because RTCPUSET may be to small to represent all the CPUs, we
cd7a8c034836a94a0c4f0adf9764f9ed661cdd01vboxsync * check with RTMpGetCount() as well.
cd7a8c034836a94a0c4f0adf9764f9ed661cdd01vboxsync */
cd7a8c034836a94a0c4f0adf9764f9ed661cdd01vboxsync static uint32_t s_cMaxCpus = 0;
cd7a8c034836a94a0c4f0adf9764f9ed661cdd01vboxsync uint32_t cCpus = s_cMaxCpus;
cd7a8c034836a94a0c4f0adf9764f9ed661cdd01vboxsync if (RT_UNLIKELY(cCpus == 0))
cd7a8c034836a94a0c4f0adf9764f9ed661cdd01vboxsync {
cd7a8c034836a94a0c4f0adf9764f9ed661cdd01vboxsync RTCPUSET CpuSet;
cd7a8c034836a94a0c4f0adf9764f9ed661cdd01vboxsync uint32_t cCpus1 = RTCpuLastIndex(RTMpGetSet(&CpuSet)) + 1;
cd7a8c034836a94a0c4f0adf9764f9ed661cdd01vboxsync uint32_t cCpus2 = RTMpGetCount();
b993d5403e8bdf1e72ffea8450e1eb94ec298dafvboxsync cCpus = RT_MAX(cCpus1, cCpus2);
cd7a8c034836a94a0c4f0adf9764f9ed661cdd01vboxsync ASMAtomicCmpXchgU32(&s_cMaxCpus, cCpus, 0);
cd7a8c034836a94a0c4f0adf9764f9ed661cdd01vboxsync return cCpus;
cd7a8c034836a94a0c4f0adf9764f9ed661cdd01vboxsync }
cd7a8c034836a94a0c4f0adf9764f9ed661cdd01vboxsync return s_cMaxCpus;
cd7a8c034836a94a0c4f0adf9764f9ed661cdd01vboxsync
0cde281779e97ead3181bbd3b628451fa2b1efe1vboxsync}
0cde281779e97ead3181bbd3b628451fa2b1efe1vboxsyncRT_EXPORT_SYMBOL(RTMpGetArraySize);
0cde281779e97ead3181bbd3b628451fa2b1efe1vboxsync