next(core): use C++11 classes for cv::Mutex/cv::AutoLock

pull/11256/head
Alexander Alekhin 7 years ago
parent c6aa97c9aa
commit c917be2189
  1. 32
      modules/core/include/opencv2/core/utility.hpp
  2. 87
      modules/core/src/system.cpp

@ -58,6 +58,8 @@
#include <functional>
#include <mutex> // 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<cv::Mutex> AutoLock;
// TLS interface
class CV_EXPORTS TLSDataContainer

@ -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 ////////////////////////////////

Loading…
Cancel
Save