-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMutex.h
118 lines (91 loc) · 2.66 KB
/
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
/***************************************************************************
Mutex.h - description
-------------------
begin : Sat Mar 12 2011
copyright : (C) 2011 by Knut-Helge Vik
email : knuthelge@vik.gs
***************************************************************************/
#pragma once
#include"BaseLib/CommonDefines.h"
#include"BaseLib/MutexPolicy.h"
#include"BaseLib/Export.h"
namespace BaseLib
{
class Thread;
// --------------------------------------------------------------
// MutexVariableHolder
// --------------------------------------------------------------
class DLL_STATE MutexVariableHolder
: public ENABLE_SHARED_FROM_THIS(MutexVariableHolder)
{
public:
MutexVariableHolder();
virtual ~MutexVariableHolder();
#ifdef USE_GCC
pthread_mutex_t& GetMutex() { return mutex_; }
#else
CRITICAL_SECTION& GetMutex() { return criticalSection_; }
#endif
private:
#ifdef USE_GCC
pthread_mutex_t mutex_;
#else
CRITICAL_SECTION criticalSection_;
#endif
private:
MutexVariableHolder(const MutexVariableHolder &) { }
MutexVariableHolder& operator=(const MutexVariableHolder&) { return *this; }
};
// --------------------------------------------------------------
// Mutex
// --------------------------------------------------------------
class DLL_STATE Mutex
{
public:
explicit Mutex(MutexPolicy policy = MutexPolicy::Debug());
Mutex(const Mutex &other);
~Mutex();
Mutex& operator=(const Mutex&);
friend class WaitCondition;
void lock();
void unlock();
bool tryLock(int64 milliseconds = 0);
bool isDebug() const
{
return policy_.IsDebug();
}
private:
bool tryLockPrivate();
private:
Thread* debugMutexLockEntry();
void debugMutexLockExit(Thread *self);
private:
Thread* debugMutexUnlockEntry();
void debugMutexUnlockExit(Thread *self);
private:
Thread* debugMutexTrylockEntry();
void debugMutexTrylockFailedExit(Thread *self);
private:
const MutexPolicy policy_;
private:
int64 mutexOwnerThreadId_;
private:
std::shared_ptr<MutexVariableHolder> mutexHolder_;
};
/**
* @brief The NullMutex class is useful when used as template argument
* into an object with varying needs of synchronization.
*
* This can be used to create an unsynchronized object and maintain portability.
*/
class DLL_STATE NullMutex
{
public:
explicit NullMutex(MutexPolicy = MutexPolicy::No());
~NullMutex();
friend class WaitCondition;
void lock() const;
void unlock() const;
bool tryLock(int64 milliseconds = 0) const;
};
}