forked from meganz/mingw-std-threads
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmingw.mutex.h
340 lines (318 loc) · 10.7 KB
/
mingw.mutex.h
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
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
/**
* @file mingw.mutex.h
* @brief std::mutex et al implementation for MinGW
** (c) 2013-2016 by Mega Limited, Auckland, New Zealand
* @author Alexander Vassilev
*
* @copyright Simplified (2-clause) BSD License.
* You should have received a copy of the license along with this
* program.
*
* This code is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
* @note
* This file may become part of the mingw-w64 runtime package. If/when this happens,
* the appropriate license will be added, i.e. this code will become dual-licensed,
* and the current BSD 2-clause license will stay.
*/
#ifndef WIN32STDMUTEX_H
#define WIN32STDMUTEX_H
#ifdef _GLIBCXX_HAS_GTHREADS
#error This version of MinGW seems to include a win32 port of pthreads, and probably \
already has C++11 std threading classes implemented, based on pthreads. \
You are likely to have class redefinition errors below, and unfirtunately this \
implementation can not be used standalone \
and independent of the system <mutex> header, since it relies on it for \
std::unique_lock and other utility classes. If you would still like to use this \
implementation (as it is more lightweight), you have to edit the \
c++-config.h system header of your MinGW to not define _GLIBCXX_HAS_GTHREADS. \
This will prevent system headers from defining actual threading classes while still \
defining the necessary utility classes.
#endif
// Recursion checks on non-recursive locks have some performance penalty, so the user
// may want to disable the checks in release builds. In that case, make sure they
// are always enabled in debug builds.
#if defined(STDMUTEX_NO_RECURSION_CHECKS) && !defined(NDEBUG)
#undef STDMUTEX_NO_RECURSION_CHECKS
#endif
#include <windows.h>
#include <chrono>
#include <system_error>
#include <cstdio>
#include <atomic>
#ifndef EPROTO
#define EPROTO 134
#endif
#ifndef EOWNERDEAD
#define EOWNERDEAD 133
#endif
namespace std
{
class recursive_mutex
{
protected:
CRITICAL_SECTION mHandle;
public:
typedef LPCRITICAL_SECTION native_handle_type;
native_handle_type native_handle() {return &mHandle;}
recursive_mutex() noexcept
{
InitializeCriticalSection(&mHandle);
}
recursive_mutex (const recursive_mutex&) = delete;
recursive_mutex& operator=(const recursive_mutex&) = delete;
~recursive_mutex() noexcept
{
DeleteCriticalSection(&mHandle);
}
void lock()
{
EnterCriticalSection(&mHandle);
}
void unlock()
{
LeaveCriticalSection(&mHandle);
}
bool try_lock()
{
return (TryEnterCriticalSection(&mHandle)!=0);
}
};
template <class B>
class _NonRecursive: protected B
{
protected:
typedef B base;
DWORD mOwnerThread;
public:
using typename base::native_handle_type;
using base::native_handle;
_NonRecursive() noexcept :base(), mOwnerThread(0) {}
_NonRecursive (const _NonRecursive<B>&) = delete;
_NonRecursive& operator= (const _NonRecursive<B>&) = delete;
void lock()
{
base::lock();
checkSetOwnerAfterLock();
}
protected:
void checkSetOwnerAfterLock()
{
DWORD self = GetCurrentThreadId();
if (mOwnerThread == self)
{
std::fprintf(stderr, "FATAL: Recursive locking of non-recursive mutex detected. Throwing system exception\n");
std::fflush(stderr);
throw std::system_error(EDEADLK, std::generic_category());
}
mOwnerThread = self;
}
void checkSetOwnerBeforeUnlock()
{
DWORD self = GetCurrentThreadId();
if (mOwnerThread != self)
{
std::fprintf(stderr, "FATAL: Recursive unlocking of non-recursive mutex detected. Throwing system exception\n");
std::fflush(stderr);
throw std::system_error(EDEADLK, std::generic_category());
}
mOwnerThread = 0;
}
public:
void unlock()
{
checkSetOwnerBeforeUnlock();
base::unlock();
}
bool try_lock()
{
bool ret = base::try_lock();
if (ret)
checkSetOwnerAfterLock();
return ret;
}
};
#ifndef STDMUTEX_NO_RECURSION_CHECKS
typedef _NonRecursive<recursive_mutex> mutex;
#else
typedef recursive_mutex mutex;
#endif
class recursive_timed_mutex
{
protected:
HANDLE mHandle;
public:
typedef HANDLE native_handle_type;
native_handle_type native_handle() const {return mHandle;}
recursive_timed_mutex(const recursive_timed_mutex&) = delete;
recursive_timed_mutex& operator=(const recursive_timed_mutex&) = delete;
recursive_timed_mutex(): mHandle(CreateMutex(NULL, FALSE, NULL)){}
~recursive_timed_mutex()
{
CloseHandle(mHandle);
}
void lock()
{
DWORD ret = WaitForSingleObject(mHandle, INFINITE);
if (ret != WAIT_OBJECT_0)
{
if (ret == WAIT_ABANDONED)
throw std::system_error(EOWNERDEAD, std::generic_category());
else
throw std::system_error(EPROTO, std::generic_category());
}
}
void unlock()
{
if (!ReleaseMutex(mHandle))
throw std::system_error(EDEADLK, std::generic_category());
}
bool try_lock()
{
DWORD ret = WaitForSingleObject(mHandle, 0);
if (ret == WAIT_TIMEOUT)
return false;
else if (ret == WAIT_OBJECT_0)
return true;
else if (ret == WAIT_ABANDONED)
throw std::system_error(EOWNERDEAD, std::generic_category());
else
throw std::system_error(EPROTO, std::generic_category());
}
template <class Rep, class Period>
bool try_lock_for(const std::chrono::duration<Rep,Period>& dur)
{
DWORD timeout = (DWORD)std::chrono::duration_cast<std::chrono::milliseconds>(dur).count();
DWORD ret = WaitForSingleObject(mHandle, timeout);
if (ret == WAIT_TIMEOUT)
return false;
else if (ret == WAIT_OBJECT_0)
return true;
else if (ret == WAIT_ABANDONED)
throw std::system_error(EOWNERDEAD, std::generic_category());
else
throw std::system_error(EPROTO, std::generic_category());
}
template <class Clock, class Duration>
bool try_lock_until(const std::chrono::time_point<Clock,Duration>& timeout_time)
{
return try_lock_for(timeout_time - Clock::now());
}
};
class timed_mutex: public _NonRecursive<recursive_timed_mutex>
{
protected:
typedef _NonRecursive<recursive_timed_mutex> base;
public:
using base::base;
timed_mutex(const timed_mutex&) = delete;
timed_mutex& operator=(const timed_mutex&) = delete;
template <class Rep, class Period>
bool try_lock_for(const std::chrono::duration<Rep,Period>& dur)
{
bool ret = base::try_lock_for(dur);
#ifndef STDMUTEX_NO_RECURSION_CHECKS
if (ret)
checkSetOwnerAfterLock();
#endif
return ret;
}
public:
template <class Clock, class Duration>
bool try_lock_until(const std::chrono::time_point<Clock,Duration>& timeout_time)
{
bool ret = base::try_lock_until(timeout_time);
#ifndef STDMUTEX_NO_RECURSION_CHECKS
if (ret)
checkSetOwnerAfterLock();
#endif
return ret;
}
};
// You can use the scoped locks and other helpers that are still provided by <mutex>
// In that case, you must include <mutex> before including this file, so that this
// file will not try to redefine them
#ifndef _GLIBCXX_MUTEX
/// Do not acquire ownership of the mutex.
struct defer_lock_t { };
/// Try to acquire ownership of the mutex without blocking.
struct try_to_lock_t { };
/// Assume the calling thread has already obtained mutex ownership
/// and manage it.
struct adopt_lock_t { };
constexpr defer_lock_t defer_lock { };
constexpr try_to_lock_t try_to_lock { };
constexpr adopt_lock_t adopt_lock { };
template <class M>
class lock_guard
{
protected:
M& mMutex;
public:
typedef M mutex_type;
lock_guard(const lock_guard&) = delete;
lock_guard& operator=(const lock_guard&) = delete;
explicit lock_guard(mutex_type& m): mMutex(m) { mMutex.lock(); }
lock_guard(mutex_type& m, adopt_lock_t):mMutex(m){}
~lock_guard() { mMutex.unlock(); }
};
#endif
struct once_flag {
private:
// Use a union to defer the initialization of the mutex to call_once
union {
std::mutex _M_mutex;
char _M_dummy[sizeof(_M_mutex)];
};
// Spinlock used for mutex initialization
std::atomic_flag _M_spinlock = ATOMIC_FLAG_INIT;
std::atomic_int _M_run_status; // 0: not initialized; 1: mutex initialized; 2: function called
public:
/// Default constructor
constexpr once_flag() noexcept : _M_dummy({0}), _M_run_status(0) {}
/// Deleted copy constructor
once_flag(const once_flag&) = delete;
/// Deleted assignment operator
once_flag& operator=(const once_flag&) = delete;
~once_flag() {
if(_M_run_status!=0) {
_M_mutex.~mutex();
}
}
template<typename _Callable, typename... _Args>
friend void
call_once(once_flag& __once, _Callable&& __f, _Args&&... __args);
};
/// call_once
template<typename _Callable, typename... _Args>
void
call_once(once_flag& __once, _Callable&& __f, _Args&&... __args)
{
// If the function has already run, quit immediately
if(__once._M_run_status == 2) return;
// We may not have the mutex yet
if(__once._M_run_status == 0) {
// Acquire the spinlock
while (__once._M_spinlock.test_and_set(std::memory_order_acquire));
// Check again; we may be the *second* thread to acquire the spinlock
if(__once._M_run_status==0) {
// Initialize the mutex
new(&__once._M_mutex) std::mutex;
__once._M_run_status = 1;
}
// Release the spinlock (std::mutex constructor is noexcept, so there's no
// need for fancy RAII)
__once._M_spinlock.clear(std::memory_order_release);
}
// Now that we are sure we have a mutex, acquire it
unique_lock<mutex> __l(__once._M_mutex);
// Check again; we may be the *second* thread to acquire the mutex
if(__once._M_run_status == 2) return;
// Call __f
std::bind(__f, std::forward<_Args>(__args)...)();
// All done
__once._M_run_status = 2;
}
}
#endif // WIN32STDMUTEX_H