thread-affinity-solaris.cpp revision 9293c97e596e78708cccd910d5b7bac07fe194b1
de4157257515400c2c25373591135f110227b68cvboxsync/* $Id$ */
de4157257515400c2c25373591135f110227b68cvboxsync/** @file
de4157257515400c2c25373591135f110227b68cvboxsync * IPRT - Thread Affinity, Solaris ring-3 implementation.
de4157257515400c2c25373591135f110227b68cvboxsync */
de4157257515400c2c25373591135f110227b68cvboxsync
de4157257515400c2c25373591135f110227b68cvboxsync/*
eeb6c0c9cd7482b990f5b20f9b7c867e501dd392vboxsync * Copyright (C) 2011 Oracle Corporation
de4157257515400c2c25373591135f110227b68cvboxsync *
b263fac6f6e7fa933c7bfb2a45d598fe8e458c09vboxsync * This file is part of VirtualBox Open Source Edition (OSE), as
b263fac6f6e7fa933c7bfb2a45d598fe8e458c09vboxsync * available from http://www.virtualbox.org. This file is free software;
b263fac6f6e7fa933c7bfb2a45d598fe8e458c09vboxsync * you can redistribute it and/or modify it under the terms of the GNU
b263fac6f6e7fa933c7bfb2a45d598fe8e458c09vboxsync * General Public License (GPL) as published by the Free Software
b263fac6f6e7fa933c7bfb2a45d598fe8e458c09vboxsync * Foundation, in version 2 as it comes in the "COPYING" file of the
b263fac6f6e7fa933c7bfb2a45d598fe8e458c09vboxsync * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
b263fac6f6e7fa933c7bfb2a45d598fe8e458c09vboxsync * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
b263fac6f6e7fa933c7bfb2a45d598fe8e458c09vboxsync *
b263fac6f6e7fa933c7bfb2a45d598fe8e458c09vboxsync * The contents of this file may alternatively be used under the terms
b263fac6f6e7fa933c7bfb2a45d598fe8e458c09vboxsync * of the Common Development and Distribution License Version 1.0
b263fac6f6e7fa933c7bfb2a45d598fe8e458c09vboxsync * (CDDL) only, as it comes in the "COPYING.CDDL" file of the
de4157257515400c2c25373591135f110227b68cvboxsync * VirtualBox OSE distribution, in which case the provisions of the
de4157257515400c2c25373591135f110227b68cvboxsync * CDDL are applicable instead of those of the GPL.
de4157257515400c2c25373591135f110227b68cvboxsync *
de4157257515400c2c25373591135f110227b68cvboxsync * You may elect to license modified versions of this file under the
de4157257515400c2c25373591135f110227b68cvboxsync * terms and conditions of either the GPL or the CDDL or both.
de4157257515400c2c25373591135f110227b68cvboxsync */
de4157257515400c2c25373591135f110227b68cvboxsync
de4157257515400c2c25373591135f110227b68cvboxsync
de4157257515400c2c25373591135f110227b68cvboxsync/*******************************************************************************
de4157257515400c2c25373591135f110227b68cvboxsync* Header Files *
de4157257515400c2c25373591135f110227b68cvboxsync*******************************************************************************/
de4157257515400c2c25373591135f110227b68cvboxsync#include <iprt/thread.h>
de4157257515400c2c25373591135f110227b68cvboxsync#include "internal/iprt.h"
de4157257515400c2c25373591135f110227b68cvboxsync
de4157257515400c2c25373591135f110227b68cvboxsync#include <iprt/assert.h>
de4157257515400c2c25373591135f110227b68cvboxsync#include <iprt/cpuset.h>
de4157257515400c2c25373591135f110227b68cvboxsync#include <iprt/err.h>
de4157257515400c2c25373591135f110227b68cvboxsync#include <iprt/mp.h>
de4157257515400c2c25373591135f110227b68cvboxsync
de4157257515400c2c25373591135f110227b68cvboxsync#include <sys/types.h>
de4157257515400c2c25373591135f110227b68cvboxsync#include <sys/processor.h>
de4157257515400c2c25373591135f110227b68cvboxsync#include <sys/procset.h>
de4157257515400c2c25373591135f110227b68cvboxsync#include <unistd.h>
de4157257515400c2c25373591135f110227b68cvboxsync#include <errno.h>
de4157257515400c2c25373591135f110227b68cvboxsync
de4157257515400c2c25373591135f110227b68cvboxsync
de4157257515400c2c25373591135f110227b68cvboxsync/* Note! The current implementation can only bind to a single CPU. */
de4157257515400c2c25373591135f110227b68cvboxsync
de4157257515400c2c25373591135f110227b68cvboxsync
de4157257515400c2c25373591135f110227b68cvboxsyncRTR3DECL(int) RTThreadSetAffinity(PCRTCPUSET pCpuSet)
de4157257515400c2c25373591135f110227b68cvboxsync{
de4157257515400c2c25373591135f110227b68cvboxsync int rc;
de4157257515400c2c25373591135f110227b68cvboxsync if (pCpuSet == NULL)
de4157257515400c2c25373591135f110227b68cvboxsync rc = processor_bind(P_LWPID, P_MYID, PBIND_NONE, NULL);
de4157257515400c2c25373591135f110227b68cvboxsync else
de4157257515400c2c25373591135f110227b68cvboxsync {
de4157257515400c2c25373591135f110227b68cvboxsync RTCPUSET PresentSet;
de4157257515400c2c25373591135f110227b68cvboxsync int cCpusInSet = RTCpuSetCount(pCpuSet);
de4157257515400c2c25373591135f110227b68cvboxsync if (cCpusInSet == 1)
de4157257515400c2c25373591135f110227b68cvboxsync {
de4157257515400c2c25373591135f110227b68cvboxsync unsigned iCpu = 0;
de4157257515400c2c25373591135f110227b68cvboxsync while ( iCpu < RTCPUSET_MAX_CPUS
de4157257515400c2c25373591135f110227b68cvboxsync && !RTCpuSetIsMemberByIndex(pCpuSet, iCpu))
de4157257515400c2c25373591135f110227b68cvboxsync iCpu++;
de4157257515400c2c25373591135f110227b68cvboxsync rc = processor_bind(P_LWPID, P_MYID, iCpu, NULL);
de4157257515400c2c25373591135f110227b68cvboxsync }
de4157257515400c2c25373591135f110227b68cvboxsync else if ( cCpusInSet == RTCPUSET_MAX_CPUS
de4157257515400c2c25373591135f110227b68cvboxsync || RTCpuSetIsEqual(pCpuSet, RTMpGetPresentSet(&PresentSet)))
de4157257515400c2c25373591135f110227b68cvboxsync rc = processor_bind(P_LWPID, P_MYID, PBIND_NONE, NULL);
de4157257515400c2c25373591135f110227b68cvboxsync else
de4157257515400c2c25373591135f110227b68cvboxsync return VERR_NOT_SUPPORTED;
de4157257515400c2c25373591135f110227b68cvboxsync }
de4157257515400c2c25373591135f110227b68cvboxsync if (!rc)
de4157257515400c2c25373591135f110227b68cvboxsync return VINF_SUCCESS;
de4157257515400c2c25373591135f110227b68cvboxsync return RTErrConvertFromErrno(errno);
de4157257515400c2c25373591135f110227b68cvboxsync}
de4157257515400c2c25373591135f110227b68cvboxsync
de4157257515400c2c25373591135f110227b68cvboxsync
de4157257515400c2c25373591135f110227b68cvboxsyncRTR3DECL(int) RTThreadGetAffinity(PRTCPUSET pCpuSet)
de4157257515400c2c25373591135f110227b68cvboxsync{
de4157257515400c2c25373591135f110227b68cvboxsync processorid_t iOldCpu;
de4157257515400c2c25373591135f110227b68cvboxsync int rc = processor_bind(P_LWPID, P_MYID, PBIND_QUERY, &iOldCpu);
de4157257515400c2c25373591135f110227b68cvboxsync if (rc)
de4157257515400c2c25373591135f110227b68cvboxsync return RTErrConvertFromErrno(errno);
de4157257515400c2c25373591135f110227b68cvboxsync if (iOldCpu == PBIND_NONE)
de4157257515400c2c25373591135f110227b68cvboxsync RTMpGetPresentSet(pCpuSet);
de4157257515400c2c25373591135f110227b68cvboxsync else
de4157257515400c2c25373591135f110227b68cvboxsync {
de4157257515400c2c25373591135f110227b68cvboxsync RTCpuSetEmpty(pCpuSet);
de4157257515400c2c25373591135f110227b68cvboxsync if (RTCpuSetAdd(pCpuSet, iOldCpu) != 0)
de4157257515400c2c25373591135f110227b68cvboxsync return VERR_INTERNAL_ERROR_5;
de4157257515400c2c25373591135f110227b68cvboxsync }
de4157257515400c2c25373591135f110227b68cvboxsync return VINF_SUCCESS;
de4157257515400c2c25373591135f110227b68cvboxsync}
de4157257515400c2c25373591135f110227b68cvboxsync
de4157257515400c2c25373591135f110227b68cvboxsync