2013-09-05 02:17:46 +02:00
|
|
|
|
|
|
|
#ifndef MUTEX_H_
|
|
|
|
#define MUTEX_H_
|
|
|
|
|
2014-04-02 00:20:08 +02:00
|
|
|
#define GCC_VER(x,y,z) ((x) * 10000 + (y) * 100 + (z))
|
2013-09-05 02:17:46 +02:00
|
|
|
#define GCC_VERSION GCC_VER(__GNUC__, __GNUC_MINOR__, __GNUC_PATCHLEVEL__)
|
|
|
|
|
|
|
|
#ifndef __has_include
|
|
|
|
#define __has_include(s) 0
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#if GCC_VERSION >= GCC_VER(4,4,0) && __GXX_EXPERIMENTAL_CXX0X__
|
|
|
|
// GCC 4.4 provides <mutex>
|
|
|
|
#include <mutex>
|
|
|
|
#elif __has_include(<mutex>) && !ANDROID
|
|
|
|
// Clang + libc++
|
|
|
|
#include <mutex>
|
|
|
|
#else
|
|
|
|
|
|
|
|
// partial <mutex> implementation for win32/pthread
|
|
|
|
|
|
|
|
#include <algorithm>
|
|
|
|
|
|
|
|
#if defined(_WIN32)
|
|
|
|
// WIN32
|
|
|
|
#define WIN32_LEAN_AND_MEAN
|
|
|
|
#include <Windows.h>
|
|
|
|
|
|
|
|
#else
|
|
|
|
// POSIX
|
|
|
|
#include <pthread.h>
|
|
|
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#if (_MSC_VER >= 1600) || (GCC_VERSION >= GCC_VER(4,3,0) && __GXX_EXPERIMENTAL_CXX0X__)
|
|
|
|
#define USE_RVALUE_REFERENCES
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#if defined(_WIN32) && defined(_M_X64)
|
|
|
|
#define USE_SRWLOCKS
|
|
|
|
#endif
|
|
|
|
|
|
|
|
namespace std
|
|
|
|
{
|
|
|
|
|
|
|
|
class recursive_mutex
|
|
|
|
{
|
|
|
|
#ifdef _WIN32
|
2014-04-02 00:20:08 +02:00
|
|
|
typedef CRITICAL_SECTION native_type;
|
2013-09-05 02:17:46 +02:00
|
|
|
#else
|
2014-04-02 00:20:08 +02:00
|
|
|
typedef pthread_mutex_t native_type;
|
2013-09-05 02:17:46 +02:00
|
|
|
#endif
|
|
|
|
|
|
|
|
public:
|
2014-04-02 00:20:08 +02:00
|
|
|
typedef native_type* native_handle_type;
|
2013-09-05 02:17:46 +02:00
|
|
|
|
2014-04-02 00:20:08 +02:00
|
|
|
recursive_mutex(const recursive_mutex&) /*= delete*/;
|
|
|
|
recursive_mutex& operator=(const recursive_mutex&) /*= delete*/;
|
2013-09-05 02:17:46 +02:00
|
|
|
|
2014-04-02 00:20:08 +02:00
|
|
|
recursive_mutex()
|
|
|
|
{
|
2013-09-05 02:17:46 +02:00
|
|
|
#ifdef _WIN32
|
2014-04-02 00:20:08 +02:00
|
|
|
InitializeCriticalSection(&m_handle);
|
2013-09-05 02:17:46 +02:00
|
|
|
#else
|
2014-04-02 00:20:08 +02:00
|
|
|
pthread_mutexattr_t attr;
|
|
|
|
pthread_mutexattr_init(&attr);
|
|
|
|
pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_RECURSIVE);
|
|
|
|
pthread_mutex_init(&m_handle, &attr);
|
2013-09-05 02:17:46 +02:00
|
|
|
#endif
|
2014-04-02 00:20:08 +02:00
|
|
|
}
|
2013-09-05 02:17:46 +02:00
|
|
|
|
2014-04-02 00:20:08 +02:00
|
|
|
~recursive_mutex()
|
|
|
|
{
|
2013-09-05 02:17:46 +02:00
|
|
|
#ifdef _WIN32
|
2014-04-02 00:20:08 +02:00
|
|
|
DeleteCriticalSection(&m_handle);
|
2013-09-05 02:17:46 +02:00
|
|
|
#else
|
2014-04-02 00:20:08 +02:00
|
|
|
pthread_mutex_destroy(&m_handle);
|
2013-09-05 02:17:46 +02:00
|
|
|
#endif
|
2014-04-02 00:20:08 +02:00
|
|
|
}
|
2013-09-05 02:17:46 +02:00
|
|
|
|
2014-04-02 00:20:08 +02:00
|
|
|
void lock()
|
|
|
|
{
|
2013-09-05 02:17:46 +02:00
|
|
|
#ifdef _WIN32
|
2014-04-02 00:20:08 +02:00
|
|
|
EnterCriticalSection(&m_handle);
|
2013-09-05 02:17:46 +02:00
|
|
|
#else
|
2014-04-02 00:20:08 +02:00
|
|
|
pthread_mutex_lock(&m_handle);
|
2013-09-05 02:17:46 +02:00
|
|
|
#endif
|
2014-04-02 00:20:08 +02:00
|
|
|
}
|
2013-09-05 02:17:46 +02:00
|
|
|
|
2014-04-02 00:20:08 +02:00
|
|
|
void unlock()
|
|
|
|
{
|
2013-09-05 02:17:46 +02:00
|
|
|
#ifdef _WIN32
|
2014-04-02 00:20:08 +02:00
|
|
|
LeaveCriticalSection(&m_handle);
|
2013-09-05 02:17:46 +02:00
|
|
|
#else
|
2014-04-02 00:20:08 +02:00
|
|
|
pthread_mutex_unlock(&m_handle);
|
2013-09-05 02:17:46 +02:00
|
|
|
#endif
|
2014-04-02 00:20:08 +02:00
|
|
|
}
|
2013-09-05 02:17:46 +02:00
|
|
|
|
2014-04-02 00:20:08 +02:00
|
|
|
bool try_lock()
|
|
|
|
{
|
2013-09-05 02:17:46 +02:00
|
|
|
#ifdef _WIN32
|
2014-04-02 00:20:08 +02:00
|
|
|
return (0 != TryEnterCriticalSection(&m_handle));
|
2013-09-05 02:17:46 +02:00
|
|
|
#else
|
2014-04-02 00:20:08 +02:00
|
|
|
return !pthread_mutex_trylock(&m_handle);
|
|
|
|
#endif
|
|
|
|
}
|
2013-09-05 02:17:46 +02:00
|
|
|
|
2014-04-02 00:20:08 +02:00
|
|
|
native_handle_type native_handle()
|
|
|
|
{
|
|
|
|
return &m_handle;
|
|
|
|
}
|
2013-09-05 02:17:46 +02:00
|
|
|
|
|
|
|
private:
|
2014-04-02 00:20:08 +02:00
|
|
|
native_type m_handle;
|
2013-09-05 02:17:46 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
#if !defined(_WIN32) || defined(USE_SRWLOCKS)
|
|
|
|
|
|
|
|
class mutex
|
|
|
|
{
|
|
|
|
#ifdef _WIN32
|
2014-04-02 00:20:08 +02:00
|
|
|
typedef SRWLOCK native_type;
|
2013-09-05 02:17:46 +02:00
|
|
|
#else
|
2014-04-02 00:20:08 +02:00
|
|
|
typedef pthread_mutex_t native_type;
|
2013-09-05 02:17:46 +02:00
|
|
|
#endif
|
|
|
|
|
|
|
|
public:
|
2014-04-02 00:20:08 +02:00
|
|
|
typedef native_type* native_handle_type;
|
2013-09-05 02:17:46 +02:00
|
|
|
|
2014-04-02 00:20:08 +02:00
|
|
|
mutex(const mutex&) /*= delete*/;
|
|
|
|
mutex& operator=(const mutex&) /*= delete*/;
|
2013-09-05 02:17:46 +02:00
|
|
|
|
2014-04-02 00:20:08 +02:00
|
|
|
mutex()
|
|
|
|
{
|
2013-09-05 02:17:46 +02:00
|
|
|
#ifdef _WIN32
|
2014-04-02 00:20:08 +02:00
|
|
|
InitializeSRWLock(&m_handle);
|
2013-09-05 02:17:46 +02:00
|
|
|
#else
|
2014-04-02 00:20:08 +02:00
|
|
|
pthread_mutex_init(&m_handle, NULL);
|
2013-09-05 02:17:46 +02:00
|
|
|
#endif
|
2014-04-02 00:20:08 +02:00
|
|
|
}
|
2013-09-05 02:17:46 +02:00
|
|
|
|
2014-04-02 00:20:08 +02:00
|
|
|
~mutex()
|
|
|
|
{
|
2013-09-05 02:17:46 +02:00
|
|
|
#ifdef _WIN32
|
|
|
|
#else
|
2014-04-02 00:20:08 +02:00
|
|
|
pthread_mutex_destroy(&m_handle);
|
2013-09-05 02:17:46 +02:00
|
|
|
#endif
|
2014-04-02 00:20:08 +02:00
|
|
|
}
|
2013-09-05 02:17:46 +02:00
|
|
|
|
2014-04-02 00:20:08 +02:00
|
|
|
void lock()
|
|
|
|
{
|
2013-09-05 02:17:46 +02:00
|
|
|
#ifdef _WIN32
|
2014-04-02 00:20:08 +02:00
|
|
|
AcquireSRWLockExclusive(&m_handle);
|
2013-09-05 02:17:46 +02:00
|
|
|
#else
|
2014-04-02 00:20:08 +02:00
|
|
|
pthread_mutex_lock(&m_handle);
|
2013-09-05 02:17:46 +02:00
|
|
|
#endif
|
2014-04-02 00:20:08 +02:00
|
|
|
}
|
2013-09-05 02:17:46 +02:00
|
|
|
|
2014-04-02 00:20:08 +02:00
|
|
|
void unlock()
|
|
|
|
{
|
2013-09-05 02:17:46 +02:00
|
|
|
#ifdef _WIN32
|
2014-04-02 00:20:08 +02:00
|
|
|
ReleaseSRWLockExclusive(&m_handle);
|
2013-09-05 02:17:46 +02:00
|
|
|
#else
|
2014-04-02 00:20:08 +02:00
|
|
|
pthread_mutex_unlock(&m_handle);
|
2013-09-05 02:17:46 +02:00
|
|
|
#endif
|
2014-04-02 00:20:08 +02:00
|
|
|
}
|
2013-09-05 02:17:46 +02:00
|
|
|
|
2014-04-02 00:20:08 +02:00
|
|
|
bool try_lock()
|
|
|
|
{
|
2013-09-05 02:17:46 +02:00
|
|
|
#ifdef _WIN32
|
2014-04-02 00:20:08 +02:00
|
|
|
// XXX TryAcquireSRWLockExclusive requires Windows 7!
|
|
|
|
// return (0 != TryAcquireSRWLockExclusive(&m_handle));
|
|
|
|
return false;
|
2013-09-05 02:17:46 +02:00
|
|
|
#else
|
2014-04-02 00:20:08 +02:00
|
|
|
return !pthread_mutex_trylock(&m_handle);
|
2013-09-05 02:17:46 +02:00
|
|
|
#endif
|
2014-04-02 00:20:08 +02:00
|
|
|
}
|
2013-09-05 02:17:46 +02:00
|
|
|
|
2014-04-02 00:20:08 +02:00
|
|
|
native_handle_type native_handle()
|
|
|
|
{
|
|
|
|
return &m_handle;
|
|
|
|
}
|
2013-09-05 02:17:46 +02:00
|
|
|
|
|
|
|
private:
|
2014-04-02 00:20:08 +02:00
|
|
|
native_type m_handle;
|
2013-09-05 02:17:46 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
#else
|
2014-04-02 00:20:08 +02:00
|
|
|
typedef recursive_mutex mutex; // just use CriticalSections
|
2013-09-05 02:17:46 +02:00
|
|
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
enum defer_lock_t { defer_lock };
|
|
|
|
enum try_to_lock_t { try_to_lock };
|
|
|
|
enum adopt_lock_t { adopt_lock };
|
|
|
|
|
|
|
|
template <class Mutex>
|
|
|
|
class lock_guard
|
|
|
|
{
|
|
|
|
public:
|
2014-04-02 00:20:08 +02:00
|
|
|
typedef Mutex mutex_type;
|
2013-09-05 02:17:46 +02:00
|
|
|
|
2014-04-02 00:20:08 +02:00
|
|
|
explicit lock_guard(mutex_type& m)
|
|
|
|
: pm(m)
|
|
|
|
{
|
|
|
|
m.lock();
|
|
|
|
}
|
2013-09-05 02:17:46 +02:00
|
|
|
|
2014-04-02 00:20:08 +02:00
|
|
|
lock_guard(mutex_type& m, adopt_lock_t)
|
|
|
|
: pm(m)
|
|
|
|
{
|
|
|
|
}
|
2013-09-05 02:17:46 +02:00
|
|
|
|
2014-04-02 00:20:08 +02:00
|
|
|
~lock_guard()
|
|
|
|
{
|
|
|
|
pm.unlock();
|
|
|
|
}
|
2013-09-05 02:17:46 +02:00
|
|
|
|
2014-04-02 00:20:08 +02:00
|
|
|
lock_guard(lock_guard const&) /*= delete*/;
|
|
|
|
lock_guard& operator=(lock_guard const&) /*= delete*/;
|
2013-09-05 02:17:46 +02:00
|
|
|
|
|
|
|
private:
|
2014-04-02 00:20:08 +02:00
|
|
|
mutex_type& pm;
|
2013-09-05 02:17:46 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
template <class Mutex>
|
|
|
|
class unique_lock
|
|
|
|
{
|
|
|
|
public:
|
2014-04-02 00:20:08 +02:00
|
|
|
typedef Mutex mutex_type;
|
2013-09-05 02:17:46 +02:00
|
|
|
|
2014-04-02 00:20:08 +02:00
|
|
|
unique_lock()
|
|
|
|
: pm(NULL), owns(false)
|
|
|
|
{}
|
2013-09-05 02:17:46 +02:00
|
|
|
|
2014-04-02 00:20:08 +02:00
|
|
|
/*explicit*/ unique_lock(mutex_type& m)
|
|
|
|
: pm(&m), owns(true)
|
|
|
|
{
|
|
|
|
m.lock();
|
|
|
|
}
|
2013-09-05 02:17:46 +02:00
|
|
|
|
2014-04-02 00:20:08 +02:00
|
|
|
unique_lock(mutex_type& m, defer_lock_t)
|
|
|
|
: pm(&m), owns(false)
|
|
|
|
{}
|
2013-09-05 02:17:46 +02:00
|
|
|
|
2014-04-02 00:20:08 +02:00
|
|
|
unique_lock(mutex_type& m, try_to_lock_t)
|
|
|
|
: pm(&m), owns(m.try_lock())
|
|
|
|
{}
|
2013-09-05 02:17:46 +02:00
|
|
|
|
2014-04-02 00:20:08 +02:00
|
|
|
unique_lock(mutex_type& m, adopt_lock_t)
|
|
|
|
: pm(&m), owns(true)
|
|
|
|
{}
|
2013-09-05 02:17:46 +02:00
|
|
|
|
2014-04-02 00:20:08 +02:00
|
|
|
//template <class Clock, class Duration>
|
|
|
|
//unique_lock(mutex_type& m, const chrono::time_point<Clock, Duration>& abs_time);
|
2013-09-05 02:17:46 +02:00
|
|
|
|
2014-04-02 00:20:08 +02:00
|
|
|
//template <class Rep, class Period>
|
|
|
|
//unique_lock(mutex_type& m, const chrono::duration<Rep, Period>& rel_time);
|
2013-09-05 02:17:46 +02:00
|
|
|
|
2014-04-02 00:20:08 +02:00
|
|
|
~unique_lock()
|
|
|
|
{
|
|
|
|
if (owns_lock())
|
|
|
|
mutex()->unlock();
|
|
|
|
}
|
2013-09-05 02:17:46 +02:00
|
|
|
|
|
|
|
#ifdef USE_RVALUE_REFERENCES
|
2014-04-02 00:20:08 +02:00
|
|
|
unique_lock& operator=(const unique_lock&) /*= delete*/;
|
2013-09-05 02:17:46 +02:00
|
|
|
|
2014-04-02 00:20:08 +02:00
|
|
|
unique_lock& operator=(unique_lock&& other)
|
|
|
|
{
|
2013-09-05 02:17:46 +02:00
|
|
|
#else
|
2014-04-02 00:20:08 +02:00
|
|
|
unique_lock& operator=(const unique_lock& u)
|
|
|
|
{
|
|
|
|
// ugly const_cast to get around lack of rvalue references
|
|
|
|
unique_lock& other = const_cast<unique_lock&>(u);
|
2013-09-05 02:17:46 +02:00
|
|
|
#endif
|
2014-04-02 00:20:08 +02:00
|
|
|
swap(other);
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2013-09-05 02:17:46 +02:00
|
|
|
#ifdef USE_RVALUE_REFERENCES
|
2014-04-02 00:20:08 +02:00
|
|
|
unique_lock(const unique_lock&) /*= delete*/;
|
|
|
|
|
|
|
|
unique_lock(unique_lock&& other)
|
|
|
|
: pm(NULL), owns(false)
|
|
|
|
{
|
2013-09-05 02:17:46 +02:00
|
|
|
#else
|
2014-04-02 00:20:08 +02:00
|
|
|
unique_lock(const unique_lock& u)
|
|
|
|
: pm(NULL), owns(false)
|
|
|
|
{
|
|
|
|
// ugly const_cast to get around lack of rvalue references
|
|
|
|
unique_lock& other = const_cast<unique_lock&>(u);
|
2013-09-05 02:17:46 +02:00
|
|
|
#endif
|
2014-04-02 00:20:08 +02:00
|
|
|
swap(other);
|
|
|
|
}
|
|
|
|
|
|
|
|
void lock()
|
|
|
|
{
|
|
|
|
mutex()->lock();
|
|
|
|
owns = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool try_lock()
|
|
|
|
{
|
|
|
|
owns = mutex()->try_lock();
|
|
|
|
return owns;
|
|
|
|
}
|
|
|
|
|
|
|
|
//template <class Rep, class Period>
|
|
|
|
//bool try_lock_for(const chrono::duration<Rep, Period>& rel_time);
|
|
|
|
//template <class Clock, class Duration>
|
|
|
|
//bool try_lock_until(const chrono::time_point<Clock, Duration>& abs_time);
|
|
|
|
|
|
|
|
void unlock()
|
|
|
|
{
|
|
|
|
mutex()->unlock();
|
|
|
|
owns = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
void swap(unique_lock& u)
|
|
|
|
{
|
|
|
|
std::swap(pm, u.pm);
|
|
|
|
std::swap(owns, u.owns);
|
|
|
|
}
|
|
|
|
|
|
|
|
mutex_type* release()
|
|
|
|
{
|
|
|
|
auto const ret = mutex();
|
|
|
|
|
|
|
|
pm = NULL;
|
|
|
|
owns = false;
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool owns_lock() const
|
|
|
|
{
|
|
|
|
return owns;
|
|
|
|
}
|
|
|
|
|
|
|
|
//explicit operator bool () const
|
|
|
|
//{
|
|
|
|
// return owns_lock();
|
|
|
|
//}
|
|
|
|
|
|
|
|
mutex_type* mutex() const
|
|
|
|
{
|
|
|
|
return pm;
|
|
|
|
}
|
2013-09-05 02:17:46 +02:00
|
|
|
|
|
|
|
private:
|
2014-04-02 00:20:08 +02:00
|
|
|
mutex_type* pm;
|
|
|
|
bool owns;
|
2013-09-05 02:17:46 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
template <class Mutex>
|
|
|
|
void swap(unique_lock<Mutex>& x, unique_lock<Mutex>& y)
|
|
|
|
{
|
2014-04-02 00:20:08 +02:00
|
|
|
x.swap(y);
|
2013-09-05 02:17:46 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|
|
|
|
#endif
|