thread.h 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. // ======================================================================== //
  2. // Copyright 2009-2019 Intel Corporation //
  3. // //
  4. // Licensed under the Apache License, Version 2.0 (the "License"); //
  5. // you may not use this file except in compliance with the License. //
  6. // You may obtain a copy of the License at //
  7. // //
  8. // http://www.apache.org/licenses/LICENSE-2.0 //
  9. // //
  10. // Unless required by applicable law or agreed to in writing, software //
  11. // distributed under the License is distributed on an "AS IS" BASIS, //
  12. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. //
  13. // See the License for the specific language governing permissions and //
  14. // limitations under the License. //
  15. // ======================================================================== //
  16. #pragma once
  17. #include "platform.h"
  18. #if !defined(_WIN32)
  19. #include <pthread.h>
  20. #include <sched.h>
  21. #if defined(__APPLE__)
  22. #include <mach/thread_policy.h>
  23. #endif
  24. #endif
  25. #include <vector>
  26. #include <mutex>
  27. namespace oidn {
  28. // --------------------------------------------------------------------------
  29. // ThreadLocal
  30. // --------------------------------------------------------------------------
  31. // Wrapper which makes any variable thread-local
  32. template<typename T>
  33. class ThreadLocal : public Verbose
  34. {
  35. private:
  36. #if defined(_WIN32)
  37. DWORD key;
  38. #else
  39. pthread_key_t key;
  40. #endif
  41. std::vector<T*> instances;
  42. std::mutex mutex;
  43. public:
  44. ThreadLocal(int verbose = 0)
  45. : Verbose(verbose)
  46. {
  47. #if defined(_WIN32)
  48. key = TlsAlloc();
  49. if (key == TLS_OUT_OF_INDEXES)
  50. OIDN_FATAL("TlsAlloc failed");
  51. #else
  52. if (pthread_key_create(&key, nullptr) != 0)
  53. OIDN_FATAL("pthread_key_create failed");
  54. #endif
  55. }
  56. ~ThreadLocal()
  57. {
  58. std::lock_guard<std::mutex> lock(mutex);
  59. for (T* ptr : instances)
  60. delete ptr;
  61. #if defined(_WIN32)
  62. if (!TlsFree(key))
  63. OIDN_WARNING("TlsFree failed");
  64. #else
  65. if (pthread_key_delete(key) != 0)
  66. OIDN_WARNING("pthread_key_delete failed");
  67. #endif
  68. }
  69. T& get()
  70. {
  71. #if defined(_WIN32)
  72. T* ptr = (T*)TlsGetValue(key);
  73. #else
  74. T* ptr = (T*)pthread_getspecific(key);
  75. #endif
  76. if (ptr)
  77. return *ptr;
  78. ptr = new T;
  79. std::lock_guard<std::mutex> lock(mutex);
  80. instances.push_back(ptr);
  81. #if defined(_WIN32)
  82. if (!TlsSetValue(key, ptr))
  83. OIDN_FATAL("TlsSetValue failed");
  84. #else
  85. if (pthread_setspecific(key, ptr) != 0)
  86. OIDN_FATAL("pthread_setspecific failed");
  87. #endif
  88. return *ptr;
  89. }
  90. };
  91. #if defined(_WIN32)
  92. // --------------------------------------------------------------------------
  93. // ThreadAffinity - Windows
  94. // --------------------------------------------------------------------------
  95. class ThreadAffinity : public Verbose
  96. {
  97. private:
  98. typedef BOOL (WINAPI *GetLogicalProcessorInformationExFunc)(LOGICAL_PROCESSOR_RELATIONSHIP,
  99. PSYSTEM_LOGICAL_PROCESSOR_INFORMATION_EX,
  100. PDWORD);
  101. typedef BOOL (WINAPI *SetThreadGroupAffinityFunc)(HANDLE,
  102. CONST GROUP_AFFINITY*,
  103. PGROUP_AFFINITY);
  104. GetLogicalProcessorInformationExFunc pGetLogicalProcessorInformationEx = nullptr;
  105. SetThreadGroupAffinityFunc pSetThreadGroupAffinity = nullptr;
  106. std::vector<GROUP_AFFINITY> affinities; // thread affinities
  107. std::vector<GROUP_AFFINITY> oldAffinities; // original thread affinities
  108. public:
  109. ThreadAffinity(int numThreadsPerCore = INT_MAX, int verbose = 0);
  110. int getNumThreads() const
  111. {
  112. return (int)affinities.size();
  113. }
  114. // Sets the affinity (0..numThreads-1) of the thread after saving the current affinity
  115. void set(int threadIndex);
  116. // Restores the affinity of the thread
  117. void restore(int threadIndex);
  118. };
  119. #elif defined(__linux__)
  120. // --------------------------------------------------------------------------
  121. // ThreadAffinity - Linux
  122. // --------------------------------------------------------------------------
  123. class ThreadAffinity : public Verbose
  124. {
  125. private:
  126. std::vector<cpu_set_t> affinities; // thread affinities
  127. std::vector<cpu_set_t> oldAffinities; // original thread affinities
  128. public:
  129. ThreadAffinity(int numThreadsPerCore = INT_MAX, int verbose = 0);
  130. int getNumThreads() const
  131. {
  132. return (int)affinities.size();
  133. }
  134. // Sets the affinity (0..numThreads-1) of the thread after saving the current affinity
  135. void set(int threadIndex);
  136. // Restores the affinity of the thread
  137. void restore(int threadIndex);
  138. };
  139. #elif defined(__APPLE__)
  140. // --------------------------------------------------------------------------
  141. // ThreadAffinity - macOS
  142. // --------------------------------------------------------------------------
  143. class ThreadAffinity : public Verbose
  144. {
  145. private:
  146. std::vector<thread_affinity_policy> affinities; // thread affinities
  147. std::vector<thread_affinity_policy> oldAffinities; // original thread affinities
  148. public:
  149. ThreadAffinity(int numThreadsPerCore = INT_MAX, int verbose = 0);
  150. int getNumThreads() const
  151. {
  152. return (int)affinities.size();
  153. }
  154. // Sets the affinity (0..numThreads-1) of the thread after saving the current affinity
  155. void set(int threadIndex);
  156. // Restores the affinity of the thread
  157. void restore(int threadIndex);
  158. };
  159. #endif
  160. } // namespace oidn