1/* -*-c++-*- OpenThreads library, Copyright (C) 2016 Robert Osfield 2 * 3 * This library is open source and may be redistributed and/or modified under 4 * the terms of the OpenSceneGraph Public License (OSGPL) version 0.0 or 5 * (at your option) any later version. The full license is in LICENSE file 6 * included with this distribution, and on the openscenegraph.org website. 7 * 8 * This library is distributed in the hope that it will be useful, 9 * but WITHOUT ANY WARRANTY; without even the implied warranty of 10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 11 * OpenSceneGraph Public License for more details. 12*/ 13 14 15// 16// Affinity - C++ Affinity class 17// ~~~~~~~~ 18// 19 20#ifndef _OPENTHREADS_AFFINITY_ 21#define _OPENTHREADS_AFFINITY_ 22 23#include <set> 24#include <OpenThreads/Mutex> 25 26namespace OpenThreads { 27 28/** 29 * @class Affinity 30 * @brief Simple container for specifying which CPU a thread should have affinity with. 31 * An empty Affinity.activeCPUs/default constructed Affinity signifies that a thread should not have any specific affinity and be able to run on all available CPUs. 32 */ 33class Affinity 34{ 35public: 36 37 Affinity() {} 38 39 Affinity(unsigned int cpuNumber) { activeCPUs.insert(cpuNumber); } 40 41 Affinity(unsigned int cpuNumber, unsigned int cpuCount) { while(cpuCount>0) { activeCPUs.insert(cpuNumber++); --cpuCount; } } 42 43 Affinity(const Affinity& rhs) : activeCPUs(rhs.activeCPUs) {} 44 45 Affinity& operator = (const Affinity& rhs) { if (&rhs!=this) { activeCPUs = rhs.activeCPUs; } return *this; } 46 47 48 /** add a specified cpu core from the list to have affinity to. */ 49 void add(unsigned int cpuNmber) { activeCPUs.insert(cpuNmber); } 50 51 /** remove a specified cpu core from the list to have affinity to. */ 52 void remove(unsigned int cpuNmber) { activeCPUs.erase(cpuNmber); } 53 54 /** return true if affinity has been provided for specific CPU cores.*/ 55 operator bool () const { return !activeCPUs.empty(); } 56 57 typedef std::set<unsigned int> ActiveCPUs; 58 59 /** Set of CPUs that a thread should have affinity to.*/ 60 ActiveCPUs activeCPUs; 61}; 62 63} 64 65#endif // !_OPENTHREADS_THREAD_ 66