| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
#pragma once |
| 50 |
#ifndef VHACD_MUTEX_H |
| 51 |
#define VHACD_MUTEX_H |
| 52 |
|
| 53 |
#if defined(WIN32) |
| 54 |
|
| 55 |
#ifndef _WIN32_WINNT |
| 56 |
#define _WIN32_WINNT 0x400 |
| 57 |
#endif |
| 58 |
#include <windows.h> |
| 59 |
#pragma comment(lib, "winmm.lib") |
| 60 |
#endif |
| 61 |
|
| 62 |
#if defined(__linux__) |
| 63 |
|
| 64 |
#include <errno.h> |
| 65 |
#include <time.h> |
| 66 |
#include <unistd.h> |
| 67 |
#define __stdcall |
| 68 |
#endif |
| 69 |
|
| 70 |
#if defined(__APPLE__) || defined(__linux__) |
| 71 |
#include <pthread.h> |
| 72 |
#endif |
| 73 |
|
| 74 |
#if defined(__APPLE__) || !defined(__GLIBC__) |
| 75 |
#define PTHREAD_MUTEX_RECURSIVE_NP PTHREAD_MUTEX_RECURSIVE |
| 76 |
#endif |
| 77 |
|
| 78 |
#define VHACD_DEBUG |
| 79 |
|
| 80 |
|
| 81 |
#ifdef VHACD_NDEBUG |
| 82 |
#define VHACD_VERIFY(x) (x) |
| 83 |
#else |
| 84 |
#define VHACD_VERIFY(x) assert((x)) |
| 85 |
#endif |
| 86 |
|
| 87 |
namespace VHACD { |
| 88 |
class Mutex { |
| 89 |
public: |
| 90 |
Mutex(void) |
| 91 |
{ |
| 92 |
#if defined(WIN32) || defined(_XBOX) |
| 93 |
InitializeCriticalSection(&m_mutex); |
| 94 |
#elif defined(__APPLE__) || defined(__linux__) |
| 95 |
pthread_mutexattr_t mutexAttr; |
| 96 |
VHACD_VERIFY(pthread_mutexattr_init(&mutexAttr) == 0); |
| 97 |
VHACD_VERIFY(pthread_mutexattr_settype(&mutexAttr, PTHREAD_MUTEX_RECURSIVE_NP) == 0); |
| 98 |
VHACD_VERIFY(pthread_mutex_init(&m_mutex, &mutexAttr) == 0); |
| 99 |
VHACD_VERIFY(pthread_mutexattr_destroy(&mutexAttr) == 0); |
| 100 |
#endif |
| 101 |
} |
| 102 |
~Mutex(void) |
| 103 |
{ |
| 104 |
#if defined(WIN32) || defined(_XBOX) |
| 105 |
DeleteCriticalSection(&m_mutex); |
| 106 |
#elif defined(__APPLE__) || defined(__linux__) |
| 107 |
VHACD_VERIFY(pthread_mutex_destroy(&m_mutex) == 0); |
| 108 |
#endif |
| 109 |
} |
| 110 |
void Lock(void) |
| 111 |
{ |
| 112 |
#if defined(WIN32) || defined(_XBOX) |
| 113 |
EnterCriticalSection(&m_mutex); |
| 114 |
#elif defined(__APPLE__) || defined(__linux__) |
| 115 |
VHACD_VERIFY(pthread_mutex_lock(&m_mutex) == 0); |
| 116 |
#endif |
| 117 |
} |
| 118 |
bool TryLock(void) |
| 119 |
{ |
| 120 |
#if defined(WIN32) || defined(_XBOX) |
| 121 |
bool bRet = false; |
| 122 |
|
| 123 |
bRet = TryEnterCriticalSection(&m_mutex) ? true : false; |
| 124 |
return bRet; |
| 125 |
#elif defined(__APPLE__) || defined(__linux__) |
| 126 |
int32_t result = pthread_mutex_trylock(&m_mutex); |
| 127 |
return (result == 0); |
| 128 |
#endif |
| 129 |
} |
| 130 |
|
| 131 |
void Unlock(void) |
| 132 |
{ |
| 133 |
#if defined(WIN32) || defined(_XBOX) |
| 134 |
LeaveCriticalSection(&m_mutex); |
| 135 |
#elif defined(__APPLE__) || defined(__linux__) |
| 136 |
VHACD_VERIFY(pthread_mutex_unlock(&m_mutex) == 0); |
| 137 |
#endif |
| 138 |
} |
| 139 |
|
| 140 |
private: |
| 141 |
#if defined(WIN32) || defined(_XBOX) |
| 142 |
CRITICAL_SECTION m_mutex; |
| 143 |
#elif defined(__APPLE__) || defined(__linux__) |
| 144 |
pthread_mutex_t m_mutex; |
| 145 |
#endif |
| 146 |
}; |
| 147 |
} |
| 148 |
#endif |