@brief A mutex wrapper for compiler that dont yet support std::recursive_mutex
| 951 | namespace internal { |
| 952 | /// @brief A mutex wrapper for compiler that dont yet support std::recursive_mutex |
| 953 | class Mutex : base::NoCopy { |
| 954 | public: |
| 955 | Mutex(void) { |
| 956 | # if ELPP_OS_UNIX |
| 957 | pthread_mutexattr_t attr; |
| 958 | pthread_mutexattr_init(&attr); |
| 959 | pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_RECURSIVE); |
| 960 | pthread_mutex_init(&m_underlyingMutex, &attr); |
| 961 | pthread_mutexattr_destroy(&attr); |
| 962 | # elif ELPP_OS_WINDOWS |
| 963 | InitializeCriticalSection(&m_underlyingMutex); |
| 964 | # endif // ELPP_OS_UNIX |
| 965 | } |
| 966 | |
| 967 | virtual ~Mutex(void) { |
| 968 | # if ELPP_OS_UNIX |
| 969 | pthread_mutex_destroy(&m_underlyingMutex); |
| 970 | # elif ELPP_OS_WINDOWS |
| 971 | DeleteCriticalSection(&m_underlyingMutex); |
| 972 | # endif // ELPP_OS_UNIX |
| 973 | } |
| 974 | |
| 975 | inline void lock(void) { |
| 976 | # if ELPP_OS_UNIX |
| 977 | pthread_mutex_lock(&m_underlyingMutex); |
| 978 | # elif ELPP_OS_WINDOWS |
| 979 | EnterCriticalSection(&m_underlyingMutex); |
| 980 | # endif // ELPP_OS_UNIX |
| 981 | } |
| 982 | |
| 983 | inline bool try_lock(void) { |
| 984 | # if ELPP_OS_UNIX |
| 985 | return (pthread_mutex_trylock(&m_underlyingMutex) == 0); |
| 986 | # elif ELPP_OS_WINDOWS |
| 987 | return TryEnterCriticalSection(&m_underlyingMutex); |
| 988 | # endif // ELPP_OS_UNIX |
| 989 | } |
| 990 | |
| 991 | inline void unlock(void) { |
| 992 | # if ELPP_OS_UNIX |
| 993 | pthread_mutex_unlock(&m_underlyingMutex); |
| 994 | # elif ELPP_OS_WINDOWS |
| 995 | LeaveCriticalSection(&m_underlyingMutex); |
| 996 | # endif // ELPP_OS_UNIX |
| 997 | } |
| 998 | |
| 999 | private: |
| 1000 | # if ELPP_OS_UNIX |
| 1001 | pthread_mutex_t m_underlyingMutex; |
| 1002 | # elif ELPP_OS_WINDOWS |
| 1003 | CRITICAL_SECTION m_underlyingMutex; |
| 1004 | # endif // ELPP_OS_UNIX |
| 1005 | }; |
| 1006 | /// @brief Scoped lock for compiler that dont yet support std::lock_guard |
| 1007 | template <typename M> |
| 1008 | class ScopedLock : base::NoCopy { |
nothing calls this directly
no outgoing calls
no test coverage detected