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
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
|
/**************************************************************************
*
* Copyright 2011-2012 Jose Fonseca
* All Rights Reserved.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*
**************************************************************************/
/*
* OS native thread abstraction.
*
* Mimics/leverages C++11 threads.
*/
#pragma once
/* XXX: We still use our own implementation:
*
* - MSVC's C++11 threads implementation are hardcoded to use C++ exceptions
*
* - MinGW's C++11 threads implementation is often either missing or relies on
* winpthreads
*
* - clang 3.4 (used in Travis) fails to compile some of libstdc++ C++11 thread
* headers
*/
#ifdef HAVE_CXX11_THREADS
#include <thread>
#include <mutex>
#include <condition_variable>
namespace os {
using std::mutex;
using std::recursive_mutex;
using std::unique_lock;
using std::condition_variable;
using std::thread;
} /* namespace os */
#else /* !HAVE_CXX11_THREADS */
#ifdef _WIN32
# include <process.h>
# include <windows.h>
# if _WIN32_WINNT >= 0x0600
# define HAVE_WIN32_CONDITION_VARIABLES
# endif
#else
# include <pthread.h>
#endif
namespace os {
/**
* Base class for mutex and recursive_mutex.
*/
class _base_mutex
{
public:
#ifdef _WIN32
typedef CRITICAL_SECTION native_handle_type;
#else
typedef pthread_mutex_t native_handle_type;
#endif
_base_mutex(void) {
#ifdef _WIN32
InitializeCriticalSection(&_native_handle);
#else
pthread_mutexattr_t attr;
pthread_mutexattr_init(&attr);
pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_RECURSIVE);
pthread_mutex_init(&_native_handle, &attr);
pthread_mutexattr_destroy(&attr);
#endif
}
~_base_mutex() {
#ifdef _WIN32
DeleteCriticalSection(&_native_handle);
#else
pthread_mutex_destroy(&_native_handle);
#endif
}
inline void
lock(void) {
#ifdef _WIN32
EnterCriticalSection(&_native_handle);
#else
pthread_mutex_lock(&_native_handle);
#endif
}
inline void
unlock(void) {
#ifdef _WIN32
LeaveCriticalSection(&_native_handle);
#else
pthread_mutex_unlock(&_native_handle);
#endif
}
native_handle_type & native_handle() {
return _native_handle;
}
protected:
native_handle_type _native_handle;
};
/**
* Same interface as std::mutex.
*/
class mutex : public _base_mutex
{
public:
inline
mutex(void) {
#ifdef _WIN32
InitializeCriticalSection(&_native_handle);
#else
pthread_mutex_init(&_native_handle, NULL);
#endif
}
};
/**
* Same interface as std::recursive_mutex.
*/
class recursive_mutex : public _base_mutex
{
public:
inline
recursive_mutex(void) {
#ifdef _WIN32
InitializeCriticalSection(&_native_handle);
#else
pthread_mutexattr_t attr;
pthread_mutexattr_init(&attr);
pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_RECURSIVE);
pthread_mutex_init(&_native_handle, &attr);
pthread_mutexattr_destroy(&attr);
#endif
}
};
/**
* Same interface as std::unique_lock;
*/
template< class Mutex >
class unique_lock
{
public:
typedef Mutex mutex_type;
inline explicit
unique_lock(mutex_type &m) :
_mutex(m)
{
_mutex.lock();
}
inline
~unique_lock() {
_mutex.unlock();
}
inline void
lock() {
_mutex.lock();
}
inline void
unlock() {
_mutex.unlock();
}
mutex_type *
mutex() const {
return &_mutex;
}
protected:
mutex_type &_mutex;
};
/**
* Same interface as std::condition_variable
*/
class condition_variable
{
private:
#ifdef _WIN32
# ifdef HAVE_WIN32_CONDITION_VARIABLES
// Only supported on Vista an higher. Not yet supported by WINE.
typedef CONDITION_VARIABLE native_handle_type;
native_handle_type _native_handle;
#else
// http://www.cs.wustl.edu/~schmidt/win32-cv-1.html
LONG cWaiters;
HANDLE hEvent;
#endif
#else
typedef pthread_cond_t native_handle_type;
native_handle_type _native_handle;
#endif
public:
condition_variable() {
#ifdef _WIN32
# ifdef HAVE_WIN32_CONDITION_VARIABLES
InitializeConditionVariable(&_native_handle);
# else
cWaiters = 0;
hEvent = CreateEvent(NULL, FALSE, FALSE, NULL);
# endif
#else
pthread_cond_init(&_native_handle, NULL);
#endif
}
~condition_variable() {
#ifdef _WIN32
# ifdef HAVE_WIN32_CONDITION_VARIABLES
/* No-op */
# else
CloseHandle(hEvent);
# endif
#else
pthread_cond_destroy(&_native_handle);
#endif
}
inline void
notify_one(void) {
#ifdef _WIN32
# ifdef HAVE_WIN32_CONDITION_VARIABLES
WakeConditionVariable(&_native_handle);
# else
if (cWaiters) {
SetEvent(hEvent);
}
# endif
#else
pthread_cond_signal(&_native_handle);
#endif
}
inline void
wait(unique_lock<mutex> & lock) {
mutex::native_handle_type & mutex_native_handle = lock.mutex()->native_handle();
#ifdef _WIN32
# ifdef HAVE_WIN32_CONDITION_VARIABLES
SleepConditionVariableCS(&_native_handle, &mutex_native_handle, INFINITE);
# else
InterlockedIncrement(&cWaiters);
LeaveCriticalSection(&mutex_native_handle);
WaitForSingleObject(hEvent, INFINITE);
EnterCriticalSection(&mutex_native_handle);
InterlockedDecrement(&cWaiters);
# endif
#else
pthread_cond_wait(&_native_handle, &mutex_native_handle);
#endif
}
};
/**
* Implement TLS through OS threading API.
*
* This will only work when T is a pointer, intptr_t, or uintptr_t.
*/
template <typename T>
class thread_specific
{
private:
#ifdef _WIN32
DWORD dwTlsIndex;
#else
pthread_key_t key;
#endif
public:
thread_specific(void) {
#ifdef _WIN32
dwTlsIndex = TlsAlloc();
#else
pthread_key_create(&key, NULL);
#endif
}
~thread_specific() {
#ifdef _WIN32
TlsFree(dwTlsIndex);
#else
pthread_key_delete(key);
#endif
}
inline T
get(void) const {
void *ptr;
#ifdef _WIN32
ptr = TlsGetValue(dwTlsIndex);
#else
ptr = pthread_getspecific(key);
#endif
return reinterpret_cast<T>(ptr);
}
inline
operator T (void) const
{
return get();
}
inline T
operator -> (void) const
{
return get();
}
inline T
operator = (T new_value)
{
set(new_value);
return new_value;
}
inline void
set(T new_value) {
void *new_ptr = reinterpret_cast<void *>(new_value);
#ifdef _WIN32
TlsSetValue(dwTlsIndex, new_ptr);
#else
pthread_setspecific(key, new_ptr);
#endif
}
};
/**
* Same interface as std::thread
*/
class thread {
public:
#ifdef _WIN32
typedef HANDLE native_handle_type;
#else
typedef pthread_t native_handle_type;
#endif
inline
thread() :
_native_handle(0)
{
}
inline
thread(const thread &other) :
_native_handle(other._native_handle)
{
}
inline
~thread() {
}
template< class Function, class Arg >
explicit thread( Function &f, Arg arg ) {
typedef CallbackParam< Function, Arg > Param;
Param *pParam = new Param(f, arg);
_native_handle = _create(pParam);
}
inline thread &
operator =(const thread &other) {
_native_handle = other._native_handle;
return *this;
}
inline bool
joinable(void) const {
return _native_handle != 0;
}
inline void
join() {
#ifdef _WIN32
WaitForSingleObject(_native_handle, INFINITE);
#else
pthread_join(_native_handle, NULL);
#endif
}
private:
native_handle_type _native_handle;
template< typename Function, typename Arg >
struct CallbackParam {
Function &f;
Arg arg;
inline
CallbackParam(Function &_f, Arg _arg) :
f(_f),
arg(_arg)
{}
inline void
operator () (void) {
f(arg);
}
};
template< typename Param >
static
#ifdef _WIN32
unsigned __stdcall
#else
void *
#endif
_callback(void *lpParameter) {
Param *pParam = static_cast<Param *>(lpParameter);
(*pParam)();
delete pParam;
return 0;
}
template< typename Param >
static inline native_handle_type
_create(Param *pParam) {
#ifdef _WIN32
uintptr_t handle =_beginthreadex(NULL, 0, &_callback<Param>, static_cast<void *>(pParam), 0, NULL);
return reinterpret_cast<HANDLE>(handle);
#else
pthread_t t;
pthread_create(&t, NULL, &_callback<Param>, static_cast<void *>(pParam));
return t;
#endif
}
};
} /* namespace os */
#endif /* !HAVE_CXX11_THREADS */
/**
* Compiler TLS.
*
* See also:
* - http://gcc.gnu.org/onlinedocs/gcc-4.6.3/gcc/Thread_002dLocal.html
* - http://msdn.microsoft.com/en-us/library/9w1sdazb.aspx
*/
#if defined(_WIN32_WINNT) && _WIN32_WINNT < 0x0600
# define OS_THREAD_SPECIFIC(_type) os::thread_specific< _type >
#elif defined(__GNUC__)
# define OS_THREAD_SPECIFIC(_type) __thread _type
#elif defined(_MSC_VER)
# define OS_THREAD_SPECIFIC(_type) __declspec(thread) _type
#else
# define OS_THREAD_SPECIFIC(_type) os::thread_specific< _type >
#endif
#define OS_THREAD_SPECIFIC_PTR(_type) OS_THREAD_SPECIFIC(_type *)
|