From c917be2189fed923f2458f7e1ef972b0a4706259 Mon Sep 17 00:00:00 2001 From: Alexander Alekhin Date: Wed, 4 Apr 2018 16:03:26 +0300 Subject: [PATCH] next(core): use C++11 classes for cv::Mutex/cv::AutoLock --- modules/core/include/opencv2/core/utility.hpp | 32 +------ modules/core/src/system.cpp | 87 ------------------- 2 files changed, 4 insertions(+), 115 deletions(-) diff --git a/modules/core/include/opencv2/core/utility.hpp b/modules/core/include/opencv2/core/utility.hpp index 092f732c78..e37f261a82 100644 --- a/modules/core/include/opencv2/core/utility.hpp +++ b/modules/core/include/opencv2/core/utility.hpp @@ -58,6 +58,8 @@ #include +#include // std::mutex, std::lock_guard + namespace cv { @@ -648,34 +650,8 @@ void Mat::forEach_impl(const Functor& operation) { /////////////////////////// Synchronization Primitives /////////////////////////////// -class CV_EXPORTS Mutex -{ -public: - Mutex(); - ~Mutex(); - Mutex(const Mutex& m); - Mutex& operator = (const Mutex& m); - - void lock(); - bool trylock(); - void unlock(); - - struct Impl; -protected: - Impl* impl; -}; - -class CV_EXPORTS AutoLock -{ -public: - AutoLock(Mutex& m) : mutex(&m) { mutex->lock(); } - ~AutoLock() { mutex->unlock(); } -protected: - Mutex* mutex; -private: - AutoLock(const AutoLock&); - AutoLock& operator = (const AutoLock&); -}; +typedef std::recursive_mutex Mutex; +typedef std::lock_guard AutoLock; // TLS interface class CV_EXPORTS TLSDataContainer diff --git a/modules/core/src/system.cpp b/modules/core/src/system.cpp index 1d8d8557ee..574622a267 100644 --- a/modules/core/src/system.cpp +++ b/modules/core/src/system.cpp @@ -1101,93 +1101,6 @@ cvErrorFromIppStatus( int status ) namespace cv { bool __termination = false; -} - -namespace cv -{ - -#if defined _WIN32 || defined WINCE - -struct Mutex::Impl -{ - Impl() - { -#if (_WIN32_WINNT >= 0x0600) - ::InitializeCriticalSectionEx(&cs, 1000, 0); -#else - ::InitializeCriticalSection(&cs); -#endif - refcount = 1; - } - ~Impl() { DeleteCriticalSection(&cs); } - - void lock() { EnterCriticalSection(&cs); } - bool trylock() { return TryEnterCriticalSection(&cs) != 0; } - void unlock() { LeaveCriticalSection(&cs); } - - CRITICAL_SECTION cs; - int refcount; -}; - -#else - -struct Mutex::Impl -{ - Impl() - { - pthread_mutexattr_t attr; - pthread_mutexattr_init(&attr); - pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_RECURSIVE); - pthread_mutex_init(&mt, &attr); - pthread_mutexattr_destroy(&attr); - - refcount = 1; - } - ~Impl() { pthread_mutex_destroy(&mt); } - - void lock() { pthread_mutex_lock(&mt); } - bool trylock() { return pthread_mutex_trylock(&mt) == 0; } - void unlock() { pthread_mutex_unlock(&mt); } - - pthread_mutex_t mt; - int refcount; -}; - -#endif - -Mutex::Mutex() -{ - impl = new Mutex::Impl; -} - -Mutex::~Mutex() -{ - if( CV_XADD(&impl->refcount, -1) == 1 ) - delete impl; - impl = 0; -} - -Mutex::Mutex(const Mutex& m) -{ - impl = m.impl; - CV_XADD(&impl->refcount, 1); -} - -Mutex& Mutex::operator = (const Mutex& m) -{ - if (this != &m) - { - CV_XADD(&m.impl->refcount, 1); - if( CV_XADD(&impl->refcount, -1) == 1 ) - delete impl; - impl = m.impl; - } - return *this; -} - -void Mutex::lock() { impl->lock(); } -void Mutex::unlock() { impl->unlock(); } -bool Mutex::trylock() { return impl->trylock(); } //////////////////////////////// thread-local storage ////////////////////////////////