summaryrefslogtreecommitdiff
path: root/src/common/std_mutex.h
diff options
context:
space:
mode:
authorGravatar archshift2014-09-02 22:05:45 -0700
committerGravatar archshift2014-09-07 14:31:07 -0700
commit498d1a37f16d36b0c6341b074e5390a9194567a7 (patch)
treeadbf248d2b6d18e475f2d5390ff8ed7d37b6a877 /src/common/std_mutex.h
parentMerge pull request #88 from archshift/remove-atomic (diff)
downloadyuzu-498d1a37f16d36b0c6341b074e5390a9194567a7.tar.gz
yuzu-498d1a37f16d36b0c6341b074e5390a9194567a7.tar.xz
yuzu-498d1a37f16d36b0c6341b074e5390a9194567a7.zip
Removed common/std_xyz, instead using the std header
Diffstat (limited to 'src/common/std_mutex.h')
-rw-r--r--src/common/std_mutex.h362
1 files changed, 0 insertions, 362 deletions
diff --git a/src/common/std_mutex.h b/src/common/std_mutex.h
deleted file mode 100644
index 5711d791b..000000000
--- a/src/common/std_mutex.h
+++ /dev/null
@@ -1,362 +0,0 @@
1#pragma once
2
3#define GCC_VER(x,y,z) ((x) * 10000 + (y) * 100 + (z))
4#define GCC_VERSION GCC_VER(__GNUC__, __GNUC_MINOR__, __GNUC_PATCHLEVEL__)
5
6#ifndef __has_include
7#define __has_include(s) 0
8#endif
9
10#if GCC_VERSION >= GCC_VER(4,4,0) && __GXX_EXPERIMENTAL_CXX0X__
11// GCC 4.4 provides <mutex>
12#include <mutex>
13#elif __has_include(<mutex>) && !ANDROID
14// Clang + libc++
15#include <mutex>
16#else
17
18// partial <mutex> implementation for win32/pthread
19
20#include <algorithm>
21
22#if defined(_WIN32)
23// WIN32
24#define WIN32_LEAN_AND_MEAN
25#include <Windows.h>
26
27#else
28// POSIX
29#include <pthread.h>
30
31#endif
32
33#if (_MSC_VER >= 1600) || (GCC_VERSION >= GCC_VER(4,3,0) && __GXX_EXPERIMENTAL_CXX0X__)
34#define USE_RVALUE_REFERENCES
35#endif
36
37#if defined(_WIN32) && defined(_M_X64)
38#define USE_SRWLOCKS
39#endif
40
41namespace std
42{
43
44class recursive_mutex
45{
46#ifdef _WIN32
47 typedef CRITICAL_SECTION native_type;
48#else
49 typedef pthread_mutex_t native_type;
50#endif
51
52public:
53 typedef native_type* native_handle_type;
54
55 recursive_mutex(const recursive_mutex&) /*= delete*/;
56 recursive_mutex& operator=(const recursive_mutex&) /*= delete*/;
57
58 recursive_mutex()
59 {
60#ifdef _WIN32
61 InitializeCriticalSection(&m_handle);
62#else
63 pthread_mutexattr_t attr;
64 pthread_mutexattr_init(&attr);
65 pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_RECURSIVE);
66 pthread_mutex_init(&m_handle, &attr);
67#endif
68 }
69
70 ~recursive_mutex()
71 {
72#ifdef _WIN32
73 DeleteCriticalSection(&m_handle);
74#else
75 pthread_mutex_destroy(&m_handle);
76#endif
77 }
78
79 void lock()
80 {
81#ifdef _WIN32
82 EnterCriticalSection(&m_handle);
83#else
84 pthread_mutex_lock(&m_handle);
85#endif
86 }
87
88 void unlock()
89 {
90#ifdef _WIN32
91 LeaveCriticalSection(&m_handle);
92#else
93 pthread_mutex_unlock(&m_handle);
94#endif
95 }
96
97 bool try_lock()
98 {
99#ifdef _WIN32
100 return (0 != TryEnterCriticalSection(&m_handle));
101#else
102 return !pthread_mutex_trylock(&m_handle);
103#endif
104 }
105
106 native_handle_type native_handle()
107 {
108 return &m_handle;
109 }
110
111private:
112 native_type m_handle;
113};
114
115#if !defined(_WIN32) || defined(USE_SRWLOCKS)
116
117class mutex
118{
119#ifdef _WIN32
120 typedef SRWLOCK native_type;
121#else
122 typedef pthread_mutex_t native_type;
123#endif
124
125public:
126 typedef native_type* native_handle_type;
127
128 mutex(const mutex&) /*= delete*/;
129 mutex& operator=(const mutex&) /*= delete*/;
130
131 mutex()
132 {
133#ifdef _WIN32
134 InitializeSRWLock(&m_handle);
135#else
136 pthread_mutex_init(&m_handle, NULL);
137#endif
138 }
139
140 ~mutex()
141 {
142#ifdef _WIN32
143#else
144 pthread_mutex_destroy(&m_handle);
145#endif
146 }
147
148 void lock()
149 {
150#ifdef _WIN32
151 AcquireSRWLockExclusive(&m_handle);
152#else
153 pthread_mutex_lock(&m_handle);
154#endif
155 }
156
157 void unlock()
158 {
159#ifdef _WIN32
160 ReleaseSRWLockExclusive(&m_handle);
161#else
162 pthread_mutex_unlock(&m_handle);
163#endif
164 }
165
166 bool try_lock()
167 {
168#ifdef _WIN32
169 // XXX TryAcquireSRWLockExclusive requires Windows 7!
170 // return (0 != TryAcquireSRWLockExclusive(&m_handle));
171 return false;
172#else
173 return !pthread_mutex_trylock(&m_handle);
174#endif
175 }
176
177 native_handle_type native_handle()
178 {
179 return &m_handle;
180 }
181
182private:
183 native_type m_handle;
184};
185
186#else
187typedef recursive_mutex mutex; // just use CriticalSections
188
189#endif
190
191enum defer_lock_t { defer_lock };
192enum try_to_lock_t { try_to_lock };
193enum adopt_lock_t { adopt_lock };
194
195template <class Mutex>
196class lock_guard
197{
198public:
199 typedef Mutex mutex_type;
200
201 explicit lock_guard(mutex_type& m)
202 : pm(m)
203 {
204 m.lock();
205 }
206
207 lock_guard(mutex_type& m, adopt_lock_t)
208 : pm(m)
209 {
210 }
211
212 ~lock_guard()
213 {
214 pm.unlock();
215 }
216
217 lock_guard(lock_guard const&) /*= delete*/;
218 lock_guard& operator=(lock_guard const&) /*= delete*/;
219
220private:
221 mutex_type& pm;
222};
223
224template <class Mutex>
225class unique_lock
226{
227public:
228 typedef Mutex mutex_type;
229
230 unique_lock()
231 : pm(NULL), owns(false)
232 {}
233
234 /*explicit*/ unique_lock(mutex_type& m)
235 : pm(&m), owns(true)
236 {
237 m.lock();
238 }
239
240 unique_lock(mutex_type& m, defer_lock_t)
241 : pm(&m), owns(false)
242 {}
243
244 unique_lock(mutex_type& m, try_to_lock_t)
245 : pm(&m), owns(m.try_lock())
246 {}
247
248 unique_lock(mutex_type& m, adopt_lock_t)
249 : pm(&m), owns(true)
250 {}
251
252 //template <class Clock, class Duration>
253 //unique_lock(mutex_type& m, const chrono::time_point<Clock, Duration>& abs_time);
254
255 //template <class Rep, class Period>
256 //unique_lock(mutex_type& m, const chrono::duration<Rep, Period>& rel_time);
257
258 ~unique_lock()
259 {
260 if (owns_lock())
261 mutex()->unlock();
262 }
263
264#ifdef USE_RVALUE_REFERENCES
265 unique_lock& operator=(const unique_lock&) /*= delete*/;
266
267 unique_lock& operator=(unique_lock&& other)
268 {
269#else
270 unique_lock& operator=(const unique_lock& u)
271 {
272 // ugly const_cast to get around lack of rvalue references
273 unique_lock& other = const_cast<unique_lock&>(u);
274#endif
275 swap(other);
276 return *this;
277 }
278
279#ifdef USE_RVALUE_REFERENCES
280 unique_lock(const unique_lock&) /*= delete*/;
281
282 unique_lock(unique_lock&& other)
283 : pm(NULL), owns(false)
284 {
285#else
286 unique_lock(const unique_lock& u)
287 : pm(NULL), owns(false)
288 {
289 // ugly const_cast to get around lack of rvalue references
290 unique_lock& other = const_cast<unique_lock&>(u);
291#endif
292 swap(other);
293 }
294
295 void lock()
296 {
297 mutex()->lock();
298 owns = true;
299 }
300
301 bool try_lock()
302 {
303 owns = mutex()->try_lock();
304 return owns;
305 }
306
307 //template <class Rep, class Period>
308 //bool try_lock_for(const chrono::duration<Rep, Period>& rel_time);
309 //template <class Clock, class Duration>
310 //bool try_lock_until(const chrono::time_point<Clock, Duration>& abs_time);
311
312 void unlock()
313 {
314 mutex()->unlock();
315 owns = false;
316 }
317
318 void swap(unique_lock& u)
319 {
320 std::swap(pm, u.pm);
321 std::swap(owns, u.owns);
322 }
323
324 mutex_type* release()
325 {
326 auto const ret = mutex();
327
328 pm = NULL;
329 owns = false;
330
331 return ret;
332 }
333
334 bool owns_lock() const
335 {
336 return owns;
337 }
338
339 //explicit operator bool () const
340 //{
341 // return owns_lock();
342 //}
343
344 mutex_type* mutex() const
345 {
346 return pm;
347 }
348
349private:
350 mutex_type* pm;
351 bool owns;
352};
353
354template <class Mutex>
355void swap(unique_lock<Mutex>& x, unique_lock<Mutex>& y)
356{
357 x.swap(y);
358}
359
360}
361
362#endif