Introduced new OpenThreads::Affinity class to wrap up specification of thread affinity.
Simplified the OpenThreads::SetProcessorAffinityOfCurrentThread/Thread::SetProcessorAffinity() to utilize the new Affinity class
This commit is contained in:
65
include/OpenThreads/Affinity
Normal file
65
include/OpenThreads/Affinity
Normal file
@@ -0,0 +1,65 @@
|
||||
/* -*-c++-*- OpenThreads library, Copyright (C) 2016 Robert Osfield
|
||||
*
|
||||
* This library is open source and may be redistributed and/or modified under
|
||||
* the terms of the OpenSceneGraph Public License (OSGPL) version 0.0 or
|
||||
* (at your option) any later version. The full license is in LICENSE file
|
||||
* included with this distribution, and on the openscenegraph.org website.
|
||||
*
|
||||
* This library is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* OpenSceneGraph Public License for more details.
|
||||
*/
|
||||
|
||||
|
||||
//
|
||||
// Affinity - C++ Affinity class
|
||||
// ~~~~~~~~
|
||||
//
|
||||
|
||||
#ifndef _OPENTHREADS_AFFINITY_
|
||||
#define _OPENTHREADS_AFFINITY_
|
||||
|
||||
#include <set>
|
||||
#include <OpenThreads/Mutex>
|
||||
|
||||
namespace OpenThreads {
|
||||
|
||||
/**
|
||||
* @class Affinity
|
||||
* @brief Simple container for specifying which CPU a thread should have affinity with.
|
||||
* 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.
|
||||
*/
|
||||
class Affinity
|
||||
{
|
||||
public:
|
||||
|
||||
Affinity() {}
|
||||
|
||||
Affinity(unsigned int cpuNumber) { activeCPUs.insert(cpuNumber); }
|
||||
|
||||
Affinity(unsigned int cpuNumber, unsigned int cpuCount) { while(cpuCount>0) { activeCPUs.insert(cpuNumber++); --cpuCount; } }
|
||||
|
||||
Affinity(const Affinity& rhs) : activeCPUs(rhs.activeCPUs) {}
|
||||
|
||||
Affinity& operator = (const Affinity& rhs) { if (&rhs!=this) { activeCPUs = rhs.activeCPUs; } return *this; }
|
||||
|
||||
|
||||
/** add a specfied cpu core from the list to have affinity to. */
|
||||
void add(unsigned int cpuNmber) { activeCPUs.insert(cpuNmber); }
|
||||
|
||||
/** remove a specfied cpu core from the list to have affinity to. */
|
||||
void remove(unsigned int cpuNmber) { activeCPUs.erase(cpuNmber); }
|
||||
|
||||
/** return true if affinity has been provided for specific CPU cores.*/
|
||||
operator bool () const { return !activeCPUs.empty(); }
|
||||
|
||||
typedef std::set<unsigned int> ActiveCPUs;
|
||||
|
||||
/** Set of CPUs that a thread should have affinity to.*/
|
||||
ActiveCPUs activeCPUs;
|
||||
};
|
||||
|
||||
}
|
||||
|
||||
#endif // !_OPENTHREADS_THREAD_
|
||||
@@ -23,6 +23,7 @@
|
||||
#include <sys/types.h>
|
||||
|
||||
#include <OpenThreads/Mutex>
|
||||
#include <OpenThreads/Affinity>
|
||||
|
||||
namespace OpenThreads {
|
||||
|
||||
@@ -34,19 +35,11 @@ namespace OpenThreads {
|
||||
*/
|
||||
extern OPENTHREAD_EXPORT_DIRECTIVE int GetNumberOfProcessors();
|
||||
|
||||
/**
|
||||
* Set the processor affinity mask of current thread. If you want to allow thread to run on any processor core use ~0ul for the cpumask
|
||||
*/
|
||||
extern OPENTHREAD_EXPORT_DIRECTIVE int SetProcessorAffinityMaskOfCurrentThread(unsigned long cpumask);
|
||||
|
||||
/**
|
||||
* Set the processor affinity of current thread.
|
||||
*/
|
||||
inline int SetProcessorAffinityOfCurrentThread(unsigned int cpunum)
|
||||
{
|
||||
unsigned long cpumask = 1ul << cpunum;
|
||||
return SetProcessorAffinityMaskOfCurrentThread(cpumask);
|
||||
}
|
||||
int SetProcessorAffinityOfCurrentThread(const Affinity& affinity);
|
||||
|
||||
|
||||
/**
|
||||
* @class Thread
|
||||
@@ -338,27 +331,13 @@ public:
|
||||
|
||||
void* getImplementation(){ return _prvData; };
|
||||
|
||||
/** Set the Thread's processor affinity to a single CPU.
|
||||
/** Set the Thread's processor affinity to all, a single CPU or multiple CPU's
|
||||
* This call must be made before
|
||||
* start() or startThread() and has no effect after the thread
|
||||
* has been running. Returns 0 on success, implementation's
|
||||
* error on failure, or -1 if ignored.
|
||||
*/
|
||||
int setProcessorAffinity( unsigned int cpunum )
|
||||
{
|
||||
unsigned long cpumask = 1ul << cpunum;
|
||||
return setProcessorAffinityMask(cpumask);
|
||||
}
|
||||
|
||||
|
||||
/** Set the Thread's processor affinity to a one or more CPU's using mask.
|
||||
* If you want this threadd to run on any processor core then use a cpumask of ~0ul
|
||||
* This call must be made before
|
||||
* start() or startThread() and has no effect after the thread
|
||||
* has been running. Returns 0 on success, implementation's
|
||||
* error on failure, or -1 if ignored.
|
||||
*/
|
||||
int setProcessorAffinityMask( unsigned long cpumask);
|
||||
int setProcessorAffinity( const Affinity& affinity);
|
||||
|
||||
|
||||
/** microSleep method, equivalent to the posix usleep(microsec).
|
||||
|
||||
Reference in New Issue
Block a user