blob: 2a38eacd2f432b2d9229f89898b76722fa8e051d (
plain) (
blame)
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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
|
/* SPDX-License-Identifier: BSL-1.0 OR BSD-3-Clause */
#ifndef MPT_MUTEX_MUTEX_HPP
#define MPT_MUTEX_MUTEX_HPP
#include "mpt/base/detect.hpp"
#include "mpt/base/namespace.hpp"
#if !MPT_PLATFORM_MULTITHREADED
#define MPT_MUTEX_NONE 1
#elif MPT_COMPILER_GENERIC
#define MPT_MUTEX_STD 1
#elif MPT_OS_WINDOWS && MPT_LIBCXX_GNU && !defined(_GLIBCXX_HAS_GTHREADS) && defined(MPT_WITH_MINGWSTDTHREADS)
#define MPT_MUTEX_STD 1
#elif MPT_OS_WINDOWS && MPT_LIBCXX_GNU && !defined(_GLIBCXX_HAS_GTHREADS)
#define MPT_MUTEX_WIN32 1
#else
#define MPT_MUTEX_STD 1
#endif
#ifndef MPT_MUTEX_STD
#define MPT_MUTEX_STD 0
#endif
#ifndef MPT_MUTEX_WIN32
#define MPT_MUTEX_WIN32 0
#endif
#ifndef MPT_MUTEX_NONE
#define MPT_MUTEX_NONE 0
#endif
#if MPT_MUTEX_STD
#if !MPT_COMPILER_GENERIC && (defined(__MINGW32__) || defined(__MINGW64__)) && !defined(_GLIBCXX_HAS_GTHREADS) && defined(MPT_WITH_MINGWSTDTHREADS)
#include <mingw.mutex.h>
#else
#include <mutex>
#ifdef MPT_COMPILER_QUIRK_COMPLEX_STD_MUTEX
#include <shared_mutex>
#include <type_traits>
#endif
#endif
#elif MPT_MUTEX_WIN32
#include <windows.h>
#endif // MPT_MUTEX
namespace mpt {
inline namespace MPT_INLINE_NS {
#if MPT_MUTEX_STD
#ifdef MPT_COMPILER_QUIRK_COMPLEX_STD_MUTEX
using mutex = std::conditional<sizeof(std::shared_mutex) < sizeof(std::mutex), std::shared_mutex, std::mutex>::type;
#else
using mutex = std::mutex;
#endif
using recursive_mutex = std::recursive_mutex;
#elif MPT_MUTEX_WIN32
#if (_WIN32_WINNT >= 0x0600) // _WIN32_WINNT_VISTA
// compatible with c++11 std::mutex, can eventually be replaced without touching any usage site
class mutex {
private:
SRWLOCK impl = SRWLOCK_INIT;
public:
mutex() = default;
~mutex() = default;
void lock() {
AcquireSRWLockExclusive(&impl);
}
bool try_lock() {
return TryAcquireSRWLockExclusive(&impl) ? true : false;
}
void unlock() {
ReleaseSRWLockExclusive(&impl);
}
};
#else // !_WIN32_WINNT_VISTA
// compatible with c++11 std::mutex, can eventually be replaced without touching any usage site
class mutex {
private:
CRITICAL_SECTION impl;
public:
mutex() {
InitializeCriticalSection(&impl);
}
~mutex() {
DeleteCriticalSection(&impl);
}
void lock() {
EnterCriticalSection(&impl);
}
bool try_lock() {
return TryEnterCriticalSection(&impl) ? true : false;
}
void unlock() {
LeaveCriticalSection(&impl);
}
};
#endif // _WIN32_WINNT_VISTA
// compatible with c++11 std::recursive_mutex, can eventually be replaced without touching any usage site
class recursive_mutex {
private:
CRITICAL_SECTION impl;
public:
recursive_mutex() {
InitializeCriticalSection(&impl);
}
~recursive_mutex() {
DeleteCriticalSection(&impl);
}
void lock() {
EnterCriticalSection(&impl);
}
bool try_lock() {
return TryEnterCriticalSection(&impl) ? true : false;
}
void unlock() {
LeaveCriticalSection(&impl);
}
};
#else // MPT_MUTEX_NONE
class mutex {
public:
mutex() {
return;
}
~mutex() {
return;
}
void lock() {
return;
}
bool try_lock() {
return true;
}
void unlock() {
return;
}
};
class recursive_mutex {
public:
recursive_mutex() {
return;
}
~recursive_mutex() {
return;
}
void lock() {
return;
}
bool try_lock() {
return true;
}
void unlock() {
return;
}
};
#endif // MPT_MUTEX
#if MPT_MUTEX_STD
template <typename T>
using lock_guard = std::lock_guard<T>;
#else // !MPT_MUTEX_STD
// compatible with c++11 std::lock_guard, can eventually be replaced without touching any usage site
template <typename mutex_type>
class lock_guard {
private:
mutex_type & mutex;
public:
lock_guard(mutex_type & m)
: mutex(m) {
mutex.lock();
}
~lock_guard() {
mutex.unlock();
}
};
#endif // MPT_MUTEX_STD
} // namespace MPT_INLINE_NS
} // namespace mpt
#endif // MPT_MUTEX_MUTEX_HPP
|