1 | /*! |
2 | ** |
3 | ** Copyright (c) 2009 by John W. Ratcliff mailto:jratcliffscarab@gmail.com |
4 | ** |
5 | ** Portions of this source has been released with the PhysXViewer application, as well as |
6 | ** Rocket, CreateDynamics, ODF, and as a number of sample code snippets. |
7 | ** |
8 | ** If you find this code useful or you are feeling particularily generous I would |
9 | ** ask that you please go to http://www.amillionpixels.us and make a donation |
10 | ** to Troy DeMolay. |
11 | ** |
12 | ** DeMolay is a youth group for young men between the ages of 12 and 21. |
13 | ** It teaches strong moral principles, as well as leadership skills and |
14 | ** public speaking. The donations page uses the 'pay for pixels' paradigm |
15 | ** where, in this case, a pixel is only a single penny. Donations can be |
16 | ** made for as small as $4 or as high as a $100 block. Each person who donates |
17 | ** will get a link to their own site as well as acknowledgement on the |
18 | ** donations blog located here http://www.amillionpixels.blogspot.com/ |
19 | ** |
20 | ** If you wish to contact me you can use the following methods: |
21 | ** |
22 | ** Skype ID: jratcliff63367 |
23 | ** Yahoo: jratcliff63367 |
24 | ** AOL: jratcliff1961 |
25 | ** email: jratcliffscarab@gmail.com |
26 | ** |
27 | ** |
28 | ** The MIT license: |
29 | ** |
30 | ** Permission is hereby granted, free of charge, to any person obtaining a copy |
31 | ** of this software and associated documentation files (the "Software"), to deal |
32 | ** in the Software without restriction, including without limitation the rights |
33 | ** to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
34 | ** copies of the Software, and to permit persons to whom the Software is furnished |
35 | ** to do so, subject to the following conditions: |
36 | ** |
37 | ** The above copyright notice and this permission notice shall be included in all |
38 | ** copies or substantial portions of the Software. |
39 | |
40 | ** THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
41 | ** IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
42 | ** FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
43 | ** AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, |
44 | ** WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN |
45 | ** CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
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 | //#include <sys/time.h> |
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 | // -- GODOT start -- |
75 | #if defined(__APPLE__) || !defined(__GLIBC__) |
76 | // -- GODOT end -- |
77 | #define PTHREAD_MUTEX_RECURSIVE_NP PTHREAD_MUTEX_RECURSIVE |
78 | #endif |
79 | |
80 | #define VHACD_DEBUG |
81 | |
82 | //#define VHACD_NDEBUG |
83 | #ifdef VHACD_NDEBUG |
84 | #define VHACD_VERIFY(x) (x) |
85 | #else |
86 | #define VHACD_VERIFY(x) assert((x)) |
87 | #endif |
88 | |
89 | namespace VHACD { |
90 | class Mutex { |
91 | public: |
92 | Mutex(void) |
93 | { |
94 | #if defined(WIN32) || defined(_XBOX) |
95 | InitializeCriticalSection(&m_mutex); |
96 | #elif defined(__APPLE__) || defined(__linux__) |
97 | pthread_mutexattr_t mutexAttr; // Mutex Attribute |
98 | VHACD_VERIFY(pthread_mutexattr_init(&mutexAttr) == 0); |
99 | VHACD_VERIFY(pthread_mutexattr_settype(&mutexAttr, PTHREAD_MUTEX_RECURSIVE_NP) == 0); |
100 | VHACD_VERIFY(pthread_mutex_init(&m_mutex, &mutexAttr) == 0); |
101 | VHACD_VERIFY(pthread_mutexattr_destroy(&mutexAttr) == 0); |
102 | #endif |
103 | } |
104 | ~Mutex(void) |
105 | { |
106 | #if defined(WIN32) || defined(_XBOX) |
107 | DeleteCriticalSection(&m_mutex); |
108 | #elif defined(__APPLE__) || defined(__linux__) |
109 | VHACD_VERIFY(pthread_mutex_destroy(&m_mutex) == 0); |
110 | #endif |
111 | } |
112 | void Lock(void) |
113 | { |
114 | #if defined(WIN32) || defined(_XBOX) |
115 | EnterCriticalSection(&m_mutex); |
116 | #elif defined(__APPLE__) || defined(__linux__) |
117 | VHACD_VERIFY(pthread_mutex_lock(&m_mutex) == 0); |
118 | #endif |
119 | } |
120 | bool TryLock(void) |
121 | { |
122 | #if defined(WIN32) || defined(_XBOX) |
123 | bool bRet = false; |
124 | //assert(("TryEnterCriticalSection seems to not work on XP???", 0)); |
125 | bRet = TryEnterCriticalSection(&m_mutex) ? true : false; |
126 | return bRet; |
127 | #elif defined(__APPLE__) || defined(__linux__) |
128 | int32_t result = pthread_mutex_trylock(&m_mutex); |
129 | return (result == 0); |
130 | #endif |
131 | } |
132 | |
133 | void Unlock(void) |
134 | { |
135 | #if defined(WIN32) || defined(_XBOX) |
136 | LeaveCriticalSection(&m_mutex); |
137 | #elif defined(__APPLE__) || defined(__linux__) |
138 | VHACD_VERIFY(pthread_mutex_unlock(&m_mutex) == 0); |
139 | #endif |
140 | } |
141 | |
142 | private: |
143 | #if defined(WIN32) || defined(_XBOX) |
144 | CRITICAL_SECTION m_mutex; |
145 | #elif defined(__APPLE__) || defined(__linux__) |
146 | pthread_mutex_t m_mutex; |
147 | #endif |
148 | }; |
149 | } |
150 | #endif // VHACD_MUTEX_H |
151 | |