OpenCV 4.5.3(日本語機械翻訳)
tls.hpp
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
5 #ifndef OPENCV_UTILS_TLS_HPP
6 #define OPENCV_UTILS_TLS_HPP
7
8 #ifndef OPENCV_CORE_UTILITY_H
9 #error "tls.hpp must be included after opencv2/core/utility.hpp or opencv2/core.hpp"
10 #endif
11
12 namespace cv {
13
16
17 namespace details { class TlsStorage; }
18
25 class CV_EXPORTS TLSDataContainer
26{
27 protected:
29 virtual ~TLSDataContainer();
30
32 void gatherData(std::vector<void*> &data) const;
34 void detachData(std::vector<void*>& data);
35
36 void* getData() const;
37 void release();
38
39 protected:
40 virtual void* createDataInstance() const = 0;
41 virtual void deleteDataInstance(void* pData) const = 0;
42
43 private:
44 int key_;
45
46 friend class cv::details::TlsStorage; // core/src/system.cpp
47
48 public:
49 void cleanup();
50
51 private:
52 // Disable copy/assign (noncopyable pattern)
54 TLSDataContainer& operator =(const TLSDataContainer &) = delete;
55};
56
57
62 template <typename T>
63 class TLSData : protected TLSDataContainer
64{
65 public:
66 inline TLSData() {}
67 inline ~TLSData() { release(); }
68
69 inline T* get() const { return (T*)getData(); }
70 inline T& getRef() const { T* ptr = (T*)getData(); CV_DbgAssert(ptr); return *ptr; }
71
73 inline void cleanup()
74 {
76 }
77
78 protected:
80 virtual void* createDataInstance() const CV_OVERRIDE { return new T; }
82 virtual void deleteDataInstance(void* pData) const CV_OVERRIDE { delete (T*)pData; }
83};
84
85
87 template <typename T>
88 class TLSDataAccumulator : public TLSData<T>
89{
90 mutable cv::Mutex mutex;
91 mutable std::vector<T*> dataFromTerminatedThreads;
92 std::vector<T*> detachedData;
93 bool cleanupMode;
94 public:
95 TLSDataAccumulator() : cleanupMode(false) {}
97 {
98 release();
99 }
100
108 void gather(std::vector<T*> &data) const
109 {
110 CV_Assert(cleanupMode == false); // state is not valid
111 CV_Assert(data.empty());
112 {
113 std::vector<void*> &dataVoid = reinterpret_cast<std::vector<void*>&>(data);
115 }
116 {
117 AutoLock lock(mutex);
118 data.reserve(data.size() + dataFromTerminatedThreads.size());
119 for (typename std::vector<T*>::const_iterator i = dataFromTerminatedThreads.begin(); i != dataFromTerminatedThreads.end(); ++i)
120 {
121 data.push_back((T*)*i);
122 }
123 }
124 }
125
132 std::vector<T*>& detachData()
133 {
134 CV_Assert(cleanupMode == false); // state is not valid
135 std::vector<void*> dataVoid;
136 {
138 }
139 {
140 AutoLock lock(mutex);
141 detachedData.reserve(dataVoid.size() + dataFromTerminatedThreads.size());
142 for (typename std::vector<T*>::const_iterator i = dataFromTerminatedThreads.begin(); i != dataFromTerminatedThreads.end(); ++i)
143 {
144 detachedData.push_back((T*)*i);
145 }
146 dataFromTerminatedThreads.clear();
147 for (typename std::vector<void*>::const_iterator i = dataVoid.begin(); i != dataVoid.end(); ++i)
148 {
149 detachedData.push_back((T*)(void*)*i);
150 }
151 }
152 dataVoid.clear();
153 return detachedData;
154 }
155
158 {
159 AutoLock lock(mutex);
160 cleanupMode = true;
161 _cleanupDetachedData();
162 cleanupMode = false;
163 }
164
166 void cleanup()
167 {
168 cleanupMode = true;
170
171 AutoLock lock(mutex);
172 _cleanupDetachedData();
173 _cleanupTerminatedData();
174 cleanupMode = false;
175 }
176
178 void release()
179 {
180 cleanupMode = true;
181 TLSDataContainer::release();
182 {
183 AutoLock lock(mutex);
184 _cleanupDetachedData();
185 _cleanupTerminatedData();
186 }
187 }
188
189 protected:
190 // synchronized
191 void _cleanupDetachedData()
192 {
193 for (typename std::vector<T*>::iterator i = detachedData.begin(); i != detachedData.end(); ++i)
194 {
195 deleteDataInstance((T*)*i);
196 }
197 detachedData.clear();
198 }
199
200 // synchronized
201 void _cleanupTerminatedData()
202 {
203 for (typename std::vector<T*>::iterator i = dataFromTerminatedThreads.begin(); i != dataFromTerminatedThreads.end(); ++i)
204 {
205 deleteDataInstance((T*)*i);
206 }
207 dataFromTerminatedThreads.clear();
208 }
209
210 protected:
211 virtual void* createDataInstance() const CV_OVERRIDE
212 {
213 // Note: we can collect all allocated data here, but this would require raced mutex locks
214 return new T;
215 }
216 virtual void deleteDataInstance(void* pData) const CV_OVERRIDE
217 {
218 if (cleanupMode)
219 {
220 delete (T*)pData;
221 }
222 else
223 {
224 AutoLock lock(mutex);
225 dataFromTerminatedThreads.push_back((T*)pData);
226 }
227 }
228};
229
230
232
233} // namespace
234
235 #endif // OPENCV_UTILS_TLS_HPP
TLS data accumulator with gathering methods
Definition: tls.hpp:89
void cleanup()
Release associated thread data
Definition: tls.hpp:166
std::vector< T * > & detachData()
Get and detach data from all threads
Definition: tls.hpp:132
void cleanupDetachedData()
Release associated thread data returned by detachData() call
Definition: tls.hpp:157
void release()
Release associated thread data and free TLS key
Definition: tls.hpp:178
void gather(std::vector< T * > &data) const
Get data from all threads
Definition: tls.hpp:108
virtual void * createDataInstance() const CV_OVERRIDE
Wrapper to allocate data by template
Definition: tls.hpp:211
virtual void deleteDataInstance(void *pData) const CV_OVERRIDE
Wrapper to release data by template
Definition: tls.hpp:216
Definition: tls.hpp:26
void detachData(std::vector< void * > &data)
get TLS data and detach all data from threads (similar to cleanup() call)
void gatherData(std::vector< void * > &data) const
void cleanup()
Release created TLS data container objects. It is similar to release() call, but it keeps TLS contain...
Simple TLS data class
Definition: tls.hpp:64
void cleanup()
Release associated thread data
Definition: tls.hpp:73
virtual void deleteDataInstance(void *pData) const CV_OVERRIDE
Wrapper to release data by template
Definition: tls.hpp:82
virtual void * createDataInstance() const CV_OVERRIDE
Wrapper to allocate data by template
Definition: tls.hpp:80
T & getRef() const
Get data associated with key
Definition: tls.hpp:70
T * get() const
Get data associated with key
Definition: tls.hpp:69
#define CV_Assert(expr)
Checks a condition at runtime and throws exception if it fails
Definition: base.hpp:342
#define CV_DbgAssert(expr)
Definition: base.hpp:375
cv
"black box" representation of the file storage associated with a file on disk.
Definition: aruco.hpp:75