tls.hpp 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  1. // This file is part of OpenCV project.
  2. // It is subject to the license terms in the LICENSE file found in the top-level directory
  3. // of this distribution and at http://opencv.org/license.html.
  4. #ifndef OPENCV_UTILS_TLS_HPP
  5. #define OPENCV_UTILS_TLS_HPP
  6. #ifndef OPENCV_CORE_UTILITY_H
  7. #error "tls.hpp must be included after opencv2/core/utility.hpp or opencv2/core.hpp"
  8. #endif
  9. namespace cv {
  10. //! @addtogroup core_utils
  11. //! @{
  12. namespace details { class TlsStorage; }
  13. /** TLS container base implementation
  14. *
  15. * Don't use directly.
  16. *
  17. * @sa TLSData, TLSDataAccumulator templates
  18. */
  19. class CV_EXPORTS TLSDataContainer
  20. {
  21. protected:
  22. TLSDataContainer();
  23. virtual ~TLSDataContainer();
  24. /// @deprecated use detachData() instead
  25. void gatherData(std::vector<void*> &data) const;
  26. /// get TLS data and detach all data from threads (similar to cleanup() call)
  27. void detachData(std::vector<void*>& data);
  28. void* getData() const;
  29. void release();
  30. protected:
  31. virtual void* createDataInstance() const = 0;
  32. virtual void deleteDataInstance(void* pData) const = 0;
  33. #if OPENCV_ABI_COMPATIBILITY > 300
  34. private:
  35. #else
  36. public:
  37. #endif
  38. int key_;
  39. friend class cv::details::TlsStorage; // core/src/system.cpp
  40. public:
  41. void cleanup(); //!< Release created TLS data container objects. It is similar to release() call, but it keeps TLS container valid.
  42. private:
  43. // Disable copy/assign (noncopyable pattern)
  44. TLSDataContainer(TLSDataContainer &);
  45. TLSDataContainer& operator =(const TLSDataContainer &);
  46. };
  47. /** @brief Simple TLS data class
  48. *
  49. * @sa TLSDataAccumulator
  50. */
  51. template <typename T>
  52. class TLSData : protected TLSDataContainer
  53. {
  54. public:
  55. inline TLSData() {}
  56. inline ~TLSData() { release(); }
  57. inline T* get() const { return (T*)getData(); } //!< Get data associated with key
  58. inline T& getRef() const { T* ptr = (T*)getData(); CV_DbgAssert(ptr); return *ptr; } //!< Get data associated with key
  59. /// Release associated thread data
  60. inline void cleanup()
  61. {
  62. TLSDataContainer::cleanup();
  63. }
  64. protected:
  65. /// Wrapper to allocate data by template
  66. virtual void* createDataInstance() const CV_OVERRIDE { return new T; }
  67. /// Wrapper to release data by template
  68. virtual void deleteDataInstance(void* pData) const CV_OVERRIDE { delete (T*)pData; }
  69. };
  70. /// TLS data accumulator with gathering methods
  71. template <typename T>
  72. class TLSDataAccumulator : public TLSData<T>
  73. {
  74. mutable cv::Mutex mutex;
  75. mutable std::vector<T*> dataFromTerminatedThreads;
  76. std::vector<T*> detachedData;
  77. bool cleanupMode;
  78. public:
  79. TLSDataAccumulator() : cleanupMode(false) {}
  80. ~TLSDataAccumulator()
  81. {
  82. release();
  83. }
  84. /** @brief Get data from all threads
  85. * @deprecated replaced by detachData()
  86. *
  87. * Lifetime of vector data is valid until next detachData()/cleanup()/release() calls
  88. *
  89. * @param[out] data result buffer (should be empty)
  90. */
  91. void gather(std::vector<T*> &data) const
  92. {
  93. CV_Assert(cleanupMode == false); // state is not valid
  94. CV_Assert(data.empty());
  95. {
  96. std::vector<void*> &dataVoid = reinterpret_cast<std::vector<void*>&>(data);
  97. TLSDataContainer::gatherData(dataVoid);
  98. }
  99. {
  100. AutoLock lock(mutex);
  101. data.reserve(data.size() + dataFromTerminatedThreads.size());
  102. for (typename std::vector<T*>::const_iterator i = dataFromTerminatedThreads.begin(); i != dataFromTerminatedThreads.end(); ++i)
  103. {
  104. data.push_back((T*)*i);
  105. }
  106. }
  107. }
  108. /** @brief Get and detach data from all threads
  109. *
  110. * Call cleanupDetachedData() when returned vector is not needed anymore.
  111. *
  112. * @return Vector with associated data. Content is preserved (including lifetime of attached data pointers) until next detachData()/cleanupDetachedData()/cleanup()/release() calls
  113. */
  114. std::vector<T*>& detachData()
  115. {
  116. CV_Assert(cleanupMode == false); // state is not valid
  117. std::vector<void*> dataVoid;
  118. {
  119. TLSDataContainer::detachData(dataVoid);
  120. }
  121. {
  122. AutoLock lock(mutex);
  123. detachedData.reserve(dataVoid.size() + dataFromTerminatedThreads.size());
  124. for (typename std::vector<T*>::const_iterator i = dataFromTerminatedThreads.begin(); i != dataFromTerminatedThreads.end(); ++i)
  125. {
  126. detachedData.push_back((T*)*i);
  127. }
  128. dataFromTerminatedThreads.clear();
  129. for (typename std::vector<void*>::const_iterator i = dataVoid.begin(); i != dataVoid.end(); ++i)
  130. {
  131. detachedData.push_back((T*)(void*)*i);
  132. }
  133. }
  134. dataVoid.clear();
  135. return detachedData;
  136. }
  137. /// Release associated thread data returned by detachData() call
  138. void cleanupDetachedData()
  139. {
  140. AutoLock lock(mutex);
  141. cleanupMode = true;
  142. _cleanupDetachedData();
  143. cleanupMode = false;
  144. }
  145. /// Release associated thread data
  146. void cleanup()
  147. {
  148. cleanupMode = true;
  149. TLSDataContainer::cleanup();
  150. AutoLock lock(mutex);
  151. _cleanupDetachedData();
  152. _cleanupTerminatedData();
  153. cleanupMode = false;
  154. }
  155. /// Release associated thread data and free TLS key
  156. void release()
  157. {
  158. cleanupMode = true;
  159. TLSDataContainer::release();
  160. {
  161. AutoLock lock(mutex);
  162. _cleanupDetachedData();
  163. _cleanupTerminatedData();
  164. }
  165. }
  166. protected:
  167. // synchronized
  168. void _cleanupDetachedData()
  169. {
  170. for (typename std::vector<T*>::iterator i = detachedData.begin(); i != detachedData.end(); ++i)
  171. {
  172. deleteDataInstance((T*)*i);
  173. }
  174. detachedData.clear();
  175. }
  176. // synchronized
  177. void _cleanupTerminatedData()
  178. {
  179. for (typename std::vector<T*>::iterator i = dataFromTerminatedThreads.begin(); i != dataFromTerminatedThreads.end(); ++i)
  180. {
  181. deleteDataInstance((T*)*i);
  182. }
  183. dataFromTerminatedThreads.clear();
  184. }
  185. protected:
  186. virtual void* createDataInstance() const CV_OVERRIDE
  187. {
  188. // Note: we can collect all allocated data here, but this would require raced mutex locks
  189. return new T;
  190. }
  191. virtual void deleteDataInstance(void* pData) const CV_OVERRIDE
  192. {
  193. if (cleanupMode)
  194. {
  195. delete (T*)pData;
  196. }
  197. else
  198. {
  199. AutoLock lock(mutex);
  200. dataFromTerminatedThreads.push_back((T*)pData);
  201. }
  202. }
  203. };
  204. //! @}
  205. } // namespace
  206. #endif // OPENCV_UTILS_TLS_HPP