1/* -*-c++-*- OpenThreads library, Copyright (C) 2016 Robert Osfield
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.
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.
16// Affinity - C++ Affinity class
20#ifndef _OPENTHREADS_AFFINITY_
21#define _OPENTHREADS_AFFINITY_
24#include <OpenThreads/Mutex>
26namespace OpenThreads {
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.
39 Affinity(unsigned int cpuNumber) { activeCPUs.insert(cpuNumber); }
41 Affinity(unsigned int cpuNumber, unsigned int cpuCount) { while(cpuCount>0) { activeCPUs.insert(cpuNumber++); --cpuCount; } }
43 Affinity(const Affinity& rhs) : activeCPUs(rhs.activeCPUs) {}
45 Affinity& operator = (const Affinity& rhs) { if (&rhs!=this) { activeCPUs = rhs.activeCPUs; } return *this; }
48 /** add a specified cpu core from the list to have affinity to. */
49 void add(unsigned int cpuNmber) { activeCPUs.insert(cpuNmber); }
51 /** remove a specified cpu core from the list to have affinity to. */
52 void remove(unsigned int cpuNmber) { activeCPUs.erase(cpuNmber); }
54 /** return true if affinity has been provided for specific CPU cores.*/
55 operator bool () const { return !activeCPUs.empty(); }
57 typedef std::set<unsigned int> ActiveCPUs;
59 /** Set of CPUs that a thread should have affinity to.*/
60 ActiveCPUs activeCPUs;
65#endif // !_OPENTHREADS_THREAD_