ppsspp/Common/StdMutex.h
Jan Beich 59a21dbfcb Common: try to unbreak StdMutex.h on more C++11 compilers
Clang still pretends as GCC 4.2.1 even with libc++ or newer libstdc++.

In file included from Common/ConsoleListener.cpp:33:
In file included from Common/ConsoleListener.h:20:
In file included from Common/LogManager.h:26:
Common/StdMutex.h:197:28: error:
      reference to 'adopt_lock_t' is ambiguous
        lock_guard(mutex_type& m, adopt_lock_t)
                                  ^
Common/StdMutex.h:183:6: note:
      candidate found by name lookup is 'std::adopt_lock_t'
enum adopt_lock_t { adopt_lock };
     ^
/usr/include/c++/v1/__mutex_base:55:25: note: candidate found by name lookup is
      'std::__1::adopt_lock_t'
struct _LIBCPP_TYPE_VIS adopt_lock_t {};
                        ^
In file included from Common/ConsoleListener.cpp:33:
In file included from Common/ConsoleListener.h:20:
In file included from Common/LogManager.h:26:
Common/StdMutex.h:230:29: error:
      reference to 'defer_lock_t' is ambiguous
        unique_lock(mutex_type& m, defer_lock_t)
                                   ^
Common/StdMutex.h:181:6: note:
      candidate found by name lookup is 'std::defer_lock_t'
enum defer_lock_t { defer_lock };
     ^
/usr/include/c++/v1/__mutex_base:53:25: note: candidate found by name lookup is
      'std::__1::defer_lock_t'
struct _LIBCPP_TYPE_VIS defer_lock_t {};
                        ^
In file included from Common/ConsoleListener.cpp:33:
In file included from Common/ConsoleListener.h:20:
In file included from Common/LogManager.h:26:
Common/StdMutex.h:234:29: error:
      reference to 'try_to_lock_t' is ambiguous
        unique_lock(mutex_type& m, try_to_lock_t)
                                   ^
Common/StdMutex.h:182:6: note:
      candidate found by name lookup is 'std::try_to_lock_t'
enum try_to_lock_t { try_to_lock };
     ^
/usr/include/c++/v1/__mutex_base:54:25: note: candidate found by name lookup is
      'std::__1::try_to_lock_t'
struct _LIBCPP_TYPE_VIS try_to_lock_t {};
                        ^
In file included from Common/ConsoleListener.cpp:33:
In file included from Common/ConsoleListener.h:20:
In file included from Common/LogManager.h:26:
Common/StdMutex.h:238:29: error:
      reference to 'adopt_lock_t' is ambiguous
        unique_lock(mutex_type& m, adopt_lock_t)
                                   ^
Common/StdMutex.h:183:6: note:
      candidate found by name lookup is 'std::adopt_lock_t'
enum adopt_lock_t { adopt_lock };
     ^
/usr/include/c++/v1/__mutex_base:55:25: note: candidate found by name lookup is
      'std::__1::adopt_lock_t'
struct _LIBCPP_TYPE_VIS adopt_lock_t {};
                        ^
In file included from Common/ConsoleListener.cpp:33:
In file included from Common/ConsoleListener.h:20:
In file included from Common/LogManager.h:26:
Common/StdMutex.h:345:6: warning:
      variable templates are a C++14 extension [-Wc++14-extensions]
void swap(unique_lock<Mutex>& x, unique_lock<Mutex>& y)
     ^
Common/StdMutex.h:345:6: error:
      variable has incomplete type 'void'
Common/StdMutex.h:345:11: error:
      reference to 'unique_lock' is ambiguous
void swap(unique_lock<Mutex>& x, unique_lock<Mutex>& y)
          ^
Common/StdMutex.h:215:7: note:
      candidate found by name lookup is 'std::unique_lock'
class unique_lock
      ^
/usr/include/c++/v1/__mutex_base:96:29: note: candidate found by name lookup is
      'std::__1::unique_lock'
class _LIBCPP_TYPE_VIS_ONLY unique_lock
                            ^
In file included from Common/ConsoleListener.cpp:33:
In file included from Common/ConsoleListener.h:20:
In file included from Common/LogManager.h:26:
Common/StdMutex.h:345:23: error:
      'Mutex' does not refer to a value
void swap(unique_lock<Mutex>& x, unique_lock<Mutex>& y)
                      ^
Common/StdMutex.h:344:17: note:
      declared here
template <class Mutex>
                ^
Common/StdMutex.h:345:31: error:
      use of undeclared identifier 'x'
void swap(unique_lock<Mutex>& x, unique_lock<Mutex>& y)
                              ^
Common/StdMutex.h:345:34: error:
      reference to 'unique_lock' is ambiguous
void swap(unique_lock<Mutex>& x, unique_lock<Mutex>& y)
                                 ^
Common/StdMutex.h:215:7: note:
      candidate found by name lookup is 'std::unique_lock'
class unique_lock
      ^
/usr/include/c++/v1/__mutex_base:96:29: note: candidate found by name lookup is
      'std::__1::unique_lock'
class _LIBCPP_TYPE_VIS_ONLY unique_lock
                            ^
In file included from Common/ConsoleListener.cpp:33:
In file included from Common/ConsoleListener.h:20:
In file included from Common/LogManager.h:26:
Common/StdMutex.h:345:46: error:
      'Mutex' does not refer to a value
void swap(unique_lock<Mutex>& x, unique_lock<Mutex>& y)
                                             ^
Common/StdMutex.h:344:17: note:
      declared here
template <class Mutex>
                ^
Common/StdMutex.h:345:54: error:
      use of undeclared identifier 'y'
void swap(unique_lock<Mutex>& x, unique_lock<Mutex>& y)
                                                     ^
Common/StdMutex.h:345:56: error:
      expected ';' at end of declaration
void swap(unique_lock<Mutex>& x, unique_lock<Mutex>& y)
                                                       ^
                                                       ;
Common/StdMutex.h:346:1: error:
      expected unqualified-id
{
^
In file included from Common/ConsoleListener.cpp:33:
In file included from Common/ConsoleListener.h:20:
Common/LogManager.h:54:7: error:
      reference to 'mutex' is ambiguous
        std::mutex m_log_lock;
             ^
/usr/include/c++/v1/__mutex_base:27:24: note: candidate found by name lookup is
      'std::__1::mutex'
class _LIBCPP_TYPE_VIS mutex
                       ^
Common/StdMutex.h:107:7: note:
      candidate found by name lookup is 'std::mutex'
class mutex
      ^
In file included from Common/ConsoleListener.cpp:33:
In file included from Common/ConsoleListener.h:20:
Common/LogManager.h:116:7: error:
      reference to 'mutex' is ambiguous
        std::mutex m_listeners_lock;
             ^
/usr/include/c++/v1/__mutex_base:27:24: note: candidate found by name lookup is
      'std::__1::mutex'
class _LIBCPP_TYPE_VIS mutex
                       ^
Common/StdMutex.h:107:7: note:
      candidate found by name lookup is 'std::mutex'
class mutex
      ^
In file included from Common/ConsoleListener.cpp:33:
In file included from Common/ConsoleListener.h:20:
Common/LogManager.h:131:7: error:
      reference to 'mutex' is ambiguous
        std::mutex log_lock_;
             ^
/usr/include/c++/v1/__mutex_base:27:24: note: candidate found by name lookup is
      'std::__1::mutex'
class _LIBCPP_TYPE_VIS mutex
                       ^
Common/StdMutex.h:107:7: note:
      candidate found by name lookup is 'std::mutex'
class mutex
      ^
1 warning and 16 errors generated.
2015-05-26 15:20:11 +03:00

354 lines
5.8 KiB
C++

#pragma once
#define GCC_VER(x,y,z) ((x) * 10000 + (y) * 100 + (z))
#define GCC_VERSION GCC_VER(__GNUC__, __GNUC_MINOR__, __GNUC_PATCHLEVEL__)
// Note: __MAC_10_7 is defined on 10.7+.
#if (__cplusplus >= 201103L || defined(__APPLE__) \
|| (GCC_VERSION >= GCC_VER(4,4,0) && __GXX_EXPERIMENTAL_CXX0X__)) \
/* GCC 4.4 provides <mutex>, except on these platforms: */ \
&& !defined(ANDROID) && !defined(__SYMBIAN32__) && !defined(MACGNUSTD)
#include <mutex>
#else
// partial <mutex> implementation for win32/pthread
#include <algorithm>
#if defined(_WIN32) // WIN32
#include "CommonWindows.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
typedef CRITICAL_SECTION native_type;
#else
typedef pthread_mutex_t native_type;
#endif
public:
typedef native_type* native_handle_type;
recursive_mutex(const recursive_mutex&) /*= delete*/;
recursive_mutex& operator=(const recursive_mutex&) /*= delete*/;
recursive_mutex()
{
#ifdef _WIN32
InitializeCriticalSection(&m_handle);
#else
pthread_mutexattr_t attr;
pthread_mutexattr_init(&attr);
pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_RECURSIVE);
pthread_mutex_init(&m_handle, &attr);
#endif
}
~recursive_mutex()
{
#ifdef _WIN32
DeleteCriticalSection(&m_handle);
#else
pthread_mutex_destroy(&m_handle);
#endif
}
void lock()
{
#ifdef _WIN32
EnterCriticalSection(&m_handle);
#else
pthread_mutex_lock(&m_handle);
#endif
}
void unlock()
{
#ifdef _WIN32
LeaveCriticalSection(&m_handle);
#else
pthread_mutex_unlock(&m_handle);
#endif
}
bool try_lock()
{
#ifdef _WIN32
return (0 != TryEnterCriticalSection(&m_handle));
#else
return !pthread_mutex_trylock(&m_handle);
#endif
}
native_handle_type native_handle()
{
return &m_handle;
}
private:
native_type m_handle;
};
#if !defined(_WIN32) || defined(USE_SRWLOCKS)
class mutex
{
#ifdef _WIN32
typedef SRWLOCK native_type;
#else
typedef pthread_mutex_t native_type;
#endif
public:
typedef native_type* native_handle_type;
mutex(const mutex&) /*= delete*/;
mutex& operator=(const mutex&) /*= delete*/;
mutex()
{
#ifdef _WIN32
InitializeSRWLock(&m_handle);
#else
pthread_mutex_init(&m_handle, NULL);
#endif
}
~mutex()
{
#ifdef _WIN32
#else
pthread_mutex_destroy(&m_handle);
#endif
}
void lock()
{
#ifdef _WIN32
AcquireSRWLockExclusive(&m_handle);
#else
pthread_mutex_lock(&m_handle);
#endif
}
void unlock()
{
#ifdef _WIN32
ReleaseSRWLockExclusive(&m_handle);
#else
pthread_mutex_unlock(&m_handle);
#endif
}
bool try_lock()
{
#ifdef _WIN32
// XXX TryAcquireSRWLockExclusive requires Windows 7!
// return (0 != TryAcquireSRWLockExclusive(&m_handle));
return false;
#else
return !pthread_mutex_trylock(&m_handle);
#endif
}
native_handle_type native_handle()
{
return &m_handle;
}
private:
native_type m_handle;
};
#else
typedef recursive_mutex mutex; // just use CriticalSections
#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:
typedef Mutex mutex_type;
explicit lock_guard(mutex_type& m)
: pm(m)
{
m.lock();
}
lock_guard(mutex_type& m, adopt_lock_t)
: pm(m)
{
}
~lock_guard()
{
pm.unlock();
}
lock_guard(lock_guard const&) /*= delete*/;
lock_guard& operator=(lock_guard const&) /*= delete*/;
private:
mutex_type& pm;
};
template <class Mutex>
class unique_lock
{
public:
typedef Mutex mutex_type;
unique_lock()
: pm(NULL), owns(false)
{}
/*explicit*/ unique_lock(mutex_type& m)
: pm(&m), owns(true)
{
m.lock();
}
unique_lock(mutex_type& m, defer_lock_t)
: pm(&m), owns(false)
{}
unique_lock(mutex_type& m, try_to_lock_t)
: pm(&m), owns(m.try_lock())
{}
unique_lock(mutex_type& m, adopt_lock_t)
: pm(&m), owns(true)
{}
//template <class Clock, class Duration>
//unique_lock(mutex_type& m, const chrono::time_point<Clock, Duration>& abs_time);
//template <class Rep, class Period>
//unique_lock(mutex_type& m, const chrono::duration<Rep, Period>& rel_time);
~unique_lock()
{
if (owns_lock())
mutex()->unlock();
}
#ifdef USE_RVALUE_REFERENCES
unique_lock& operator=(const unique_lock&) /*= delete*/;
unique_lock& operator=(unique_lock&& other)
{
#else
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);
#endif
swap(other);
return *this;
}
#ifdef USE_RVALUE_REFERENCES
unique_lock(const unique_lock&) /*= delete*/;
unique_lock(unique_lock&& other)
: pm(NULL), owns(false)
{
#else
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);
#endif
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()
{
mutex_type* 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;
}
private:
mutex_type* pm;
bool owns;
};
template <class Mutex>
void swap(unique_lock<Mutex>& x, unique_lock<Mutex>& y)
{
x.swap(y);
}
}
#endif