diff --git a/modules/core/include/opencv2/core/utility.hpp b/modules/core/include/opencv2/core/utility.hpp index f89560a809..e6dfd7a4f0 100644 --- a/modules/core/include/opencv2/core/utility.hpp +++ b/modules/core/include/opencv2/core/utility.hpp @@ -61,7 +61,7 @@ CV_EXPORTS void addImpl(int flag, const char* func = 0); // add implementation a // Each implementation entry correspond to function name entry, so you can find which implementation was executed in which fucntion CV_EXPORTS int getImpl(std::vector &impl, std::vector &funName); -CV_EXPORTS bool useCollection(); // return implementation colelction state +CV_EXPORTS bool useCollection(); // return implementation collection state CV_EXPORTS void setUseCollection(bool flag); // set implementation collection state #define CV_IMPL_PLAIN 0x01 // native CPU OpenCV implementation diff --git a/modules/core/src/precomp.hpp b/modules/core/src/precomp.hpp index a57849012c..fa61203710 100644 --- a/modules/core/src/precomp.hpp +++ b/modules/core/src/precomp.hpp @@ -232,15 +232,30 @@ inline bool checkScalar(InputArray sc, int atype, int sckind, int akind) void convertAndUnrollScalar( const Mat& sc, int buftype, uchar* scbuf, size_t blocksize ); +#ifdef CV_COLLECT_IMPL_DATA +struct ImplCollector +{ + ImplCollector() + { + useCollection = false; + implFlags = 0; + } + bool useCollection; // enable/disable impl data collection + + int implFlags; + std::vector implCode; + std::vector implFun; + + cv::Mutex mutex; +}; +#endif + struct CoreTLSData { - CoreTLSData() : device(0), useOpenCL(-1), useIPP(-1), useCollection(false) + CoreTLSData() : device(0), useOpenCL(-1), useIPP(-1) { #ifdef HAVE_TEGRA_OPTIMIZATION useTegra = -1; -#endif -#ifdef CV_COLLECT_IMPL_DATA - implFlags = 0; #endif } @@ -251,13 +266,6 @@ struct CoreTLSData int useIPP; // 1 - use, 0 - do not use, -1 - auto/not initialized #ifdef HAVE_TEGRA_OPTIMIZATION int useTegra; // 1 - use, 0 - do not use, -1 - auto/not initialized -#endif - bool useCollection; // enable/disable impl data collection - -#ifdef CV_COLLECT_IMPL_DATA - int implFlags; - std::vector implCode; - std::vector implFun; #endif }; diff --git a/modules/core/src/system.cpp b/modules/core/src/system.cpp index cefae8cb88..46f41dcca9 100644 --- a/modules/core/src/system.cpp +++ b/modules/core/src/system.cpp @@ -1163,47 +1163,56 @@ TLSData& getCoreTlsData() #ifdef CV_COLLECT_IMPL_DATA +ImplCollector& getImplData() +{ + static ImplCollector *value = new ImplCollector(); + return *value; +} + void setImpl(int flags) { - CoreTLSData* data = getCoreTlsData().get(); - data->implFlags = flags; - data->implCode.clear(); - data->implFun.clear(); + cv::AutoLock lock(getImplData().mutex); + + getImplData().implFlags = flags; + getImplData().implCode.clear(); + getImplData().implFun.clear(); } void addImpl(int flag, const char* func) { - CoreTLSData* data = getCoreTlsData().get(); - data->implFlags |= flag; + cv::AutoLock lock(getImplData().mutex); + + getImplData().implFlags |= flag; if(func) // use lazy collection if name was not specified { - size_t index = data->implCode.size(); - if(!index || (data->implCode[index-1] != flag || data->implFun[index-1].compare(func))) // avoid duplicates + size_t index = getImplData().implCode.size(); + if(!index || (getImplData().implCode[index-1] != flag || getImplData().implFun[index-1].compare(func))) // avoid duplicates { - data->implCode.push_back(flag); - data->implFun.push_back(func); + getImplData().implCode.push_back(flag); + getImplData().implFun.push_back(func); } } } int getImpl(std::vector &impl, std::vector &funName) { - CoreTLSData* data = getCoreTlsData().get(); - impl = data->implCode; - funName = data->implFun; - return data->implFlags; // return actual flags for lazy collection + cv::AutoLock lock(getImplData().mutex); + + impl = getImplData().implCode; + funName = getImplData().implFun; + return getImplData().implFlags; // return actual flags for lazy collection } bool useCollection() { - CoreTLSData* data = getCoreTlsData().get(); - return data->useCollection; + return getImplData().useCollection; } void setUseCollection(bool flag) { - CoreTLSData* data = getCoreTlsData().get(); - data->useCollection = flag; + cv::AutoLock lock(getImplData().mutex); + + getImplData().useCollection = flag; } #endif