Affinity 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  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. // Affinity - C++ Affinity class
  15. // ~~~~~~~~
  16. //
  17. #ifndef _OPENTHREADS_AFFINITY_
  18. #define _OPENTHREADS_AFFINITY_
  19. #include <set>
  20. #include <OpenThreads/Mutex>
  21. namespace OpenThreads {
  22. /**
  23. * @class Affinity
  24. * @brief Simple container for specifying which CPU a thread should have affinity with.
  25. * 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.
  26. */
  27. class Affinity
  28. {
  29. public:
  30. Affinity() {}
  31. Affinity(unsigned int cpuNumber) { activeCPUs.insert(cpuNumber); }
  32. Affinity(unsigned int cpuNumber, unsigned int cpuCount) { while(cpuCount>0) { activeCPUs.insert(cpuNumber++); --cpuCount; } }
  33. Affinity(const Affinity& rhs) : activeCPUs(rhs.activeCPUs) {}
  34. Affinity& operator = (const Affinity& rhs) { if (&rhs!=this) { activeCPUs = rhs.activeCPUs; } return *this; }
  35. /** add a specified cpu core from the list to have affinity to. */
  36. void add(unsigned int cpuNmber) { activeCPUs.insert(cpuNmber); }
  37. /** remove a specified cpu core from the list to have affinity to. */
  38. void remove(unsigned int cpuNmber) { activeCPUs.erase(cpuNmber); }
  39. /** return true if affinity has been provided for specific CPU cores.*/
  40. operator bool () const { return !activeCPUs.empty(); }
  41. typedef std::set<unsigned int> ActiveCPUs;
  42. /** Set of CPUs that a thread should have affinity to.*/
  43. ActiveCPUs activeCPUs;
  44. };
  45. }
  46. #endif // !_OPENTHREADS_THREAD_