aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/common/src/std_mutex.h
blob: 27c5833f8312fbca531f8f9fb6505d43eef314c7 (plain)
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
#ifndef MUTEX_H_
#define MUTEX_H_

#define GCC_VER(x,y,z)	((x) * 10000 + (y) * 100 + (z))
#define GCC_VERSION GCC_VER(__GNUC__, __GNUC_MINOR__, __GNUC_PATCHLEVEL__)

#if GCC_VERSION >= GCC_VER(4,4,0) && __GXX_EXPERIMENTAL_CXX0X__
// GCC 4.4 provides <mutex>
#include <mutex>
#else

// partial <mutex> implementation for win32/pthread

#include <algorithm>

#if defined(_WIN32)
// WIN32
#define WIN32_LEAN_AND_MEAN
#include <Windows.h>

#else
// POSIX
#include <pthread.h>

#endif

#if (_MSC_VER >= 1600) || (GCC_VERSION >= GCC_VER(4,3,0) && __GXX_EXPERIMENTAL_CXX0X__)
#define USE_RVALUE_REFERENCES
#endif

#if defined(_WIN32) && defined(_M_X64)
#define USE_SRWLOCKS
#endif

namespace std
{

class recursive_mutex
{
#ifdef _WIN32
    typedef CRITICAL_SECTION native_type;
#else
    typedef pthread_mutex_t native_type;
#endif

public:
    typedef native_type* native_handle_type;

    recursive_mutex(const recursive_mutex&) /*= delete*/;
    recursive_mutex& operator=(const recursive_mutex&) /*= delete*/;

    recursive_mutex()
    {
#ifdef _WIN32
        InitializeCriticalSection(&m_handle);
#else
        pthread_mutexattr_t attr;
        pthread_mutexattr_init(&attr);
        pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_RECURSIVE);
        pthread_mutex_init(&m_handle, &attr);
#endif
    }

    ~recursive_mutex()
    {
#ifdef _WIN32
        DeleteCriticalSection(&m_handle);
#else
        pthread_mutex_destroy(&m_handle);
#endif
    }

    void lock()
    {
#ifdef _WIN32
        EnterCriticalSection(&m_handle);
#else
        pthread_mutex_lock(&m_handle);
#endif
    }

    void unlock()
    {
#ifdef _WIN32
        LeaveCriticalSection(&m_handle);
#else
        pthread_mutex_unlock(&m_handle);
#endif
    }

    bool try_lock()
    {
#ifdef _WIN32
        return (0 != TryEnterCriticalSection(&m_handle));
#else
        return !pthread_mutex_trylock(&m_handle);
#endif	
    }

    native_handle_type native_handle()
    {
        return &m_handle;
    }

private:
    native_type m_handle;
};

#if !defined(_WIN32) || defined(USE_SRWLOCKS)

class mutex
{
#ifdef _WIN32
    typedef SRWLOCK native_type;
#else
    typedef pthread_mutex_t native_type;
#endif

public:
    typedef native_type* native_handle_type;

    mutex(const mutex&) /*= delete*/;
    mutex& operator=(const mutex&) /*= delete*/;

    mutex()
    {
#ifdef _WIN32
        InitializeSRWLock(&m_handle);
#else
        pthread_mutex_init(&m_handle, NULL);
#endif
    }

    ~mutex()
    {
#ifdef _WIN32
#else
        pthread_mutex_destroy(&m_handle);
#endif
    }

    void lock()
    {
#ifdef _WIN32
        AcquireSRWLockExclusive(&m_handle);
#else
        pthread_mutex_lock(&m_handle);
#endif
    }

    void unlock()
    {
#ifdef _WIN32
        ReleaseSRWLockExclusive(&m_handle);
#else
        pthread_mutex_unlock(&m_handle);
#endif
    }

    bool try_lock()
    {
#ifdef _WIN32
        // XXX TryAcquireSRWLockExclusive requires Windows 7!
        // return (0 != TryAcquireSRWLockExclusive(&m_handle));
        return false;
#else
        return !pthread_mutex_trylock(&m_handle);
#endif
    }

    native_handle_type native_handle()
    {
        return &m_handle;
    }

private:
    native_type m_handle;
};

#else
typedef recursive_mutex mutex;	// just use CriticalSections

#endif

enum defer_lock_t { defer_lock };
enum try_to_lock_t { try_to_lock };
enum adopt_lock_t { adopt_lock };

template <class Mutex>
class lock_guard
{
public:
    typedef Mutex mutex_type;

    explicit lock_guard(mutex_type& m)
        : pm(m)
    {
        m.lock();
    }

    lock_guard(mutex_type& m, adopt_lock_t)
        : pm(m)
    {
    }

    ~lock_guard()
    {
        pm.unlock();
    }

    lock_guard(lock_guard const&) /*= delete*/;
    lock_guard& operator=(lock_guard const&) /*= delete*/;

private:
    mutex_type& pm;
};

template <class Mutex>
class unique_lock
{
public:
    typedef Mutex mutex_type;

    unique_lock()
        : pm(NULL), owns(false)
    {}

    /*explicit*/ unique_lock(mutex_type& m)
        : pm(&m), owns(true)
    {
        m.lock();
    }

    unique_lock(mutex_type& m, defer_lock_t)
        : pm(&m), owns(false)
    {}

    unique_lock(mutex_type& m, try_to_lock_t)
        : pm(&m), owns(m.try_lock())
    {}

    unique_lock(mutex_type& m, adopt_lock_t)
        : pm(&m), owns(true)
    {}

    //template <class Clock, class Duration>
    //unique_lock(mutex_type& m, const chrono::time_point<Clock, Duration>& abs_time);

    //template <class Rep, class Period>
    //unique_lock(mutex_type& m, const chrono::duration<Rep, Period>& rel_time);

    ~unique_lock()
    {
        if (owns_lock())
            mutex()->unlock();
    }

#ifdef USE_RVALUE_REFERENCES
    unique_lock& operator=(const unique_lock&) /*= delete*/;

    unique_lock& operator=(unique_lock&& other)
    {
#else
    unique_lock& operator=(const unique_lock& u)
    {
        // ugly const_cast to get around lack of rvalue references
        unique_lock& other = const_cast<unique_lock&>(u);
#endif
        swap(other);
        return *this;
    }

#ifdef USE_RVALUE_REFERENCES
    unique_lock(const unique_lock&) /*= delete*/;

    unique_lock(unique_lock&& other)
        : pm(NULL), owns(false)
    {
#else
    unique_lock(const unique_lock& u)
        : pm(NULL), owns(false)
    {
        // ugly const_cast to get around lack of rvalue references
        unique_lock& other = const_cast<unique_lock&>(u);	
#endif
        swap(other);
    }

    void lock()
    {
        mutex()->lock();
        owns = true;
    }

    bool try_lock()
    {
        owns = mutex()->try_lock();
        return owns;
    }

    //template <class Rep, class Period>
    //bool try_lock_for(const chrono::duration<Rep, Period>& rel_time);
    //template <class Clock, class Duration>
    //bool try_lock_until(const chrono::time_point<Clock, Duration>& abs_time);

    void unlock()
    {
        mutex()->unlock();
        owns = false;
    }

    void swap(unique_lock& u)
    {
        std::swap(pm, u.pm);
        std::swap(owns, u.owns);
    }

    mutex_type* release()
    {
        return mutex();
        pm = NULL;
        owns = false;
    }

    bool owns_lock() const
    {
        return owns;
    }

    //explicit operator bool () const
    //{
    //	return owns_lock();
    //}

    mutex_type* mutex() const
    {
        return pm;
    }

private:
    mutex_type* pm;
    bool owns;
};

template <class Mutex>
void swap(unique_lock<Mutex>& x, unique_lock<Mutex>& y)
{
    x.swap(y);
}

}

#endif
#endif