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
|
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.
//
// -----------------------------------------------------------------------------------------------------------
//
// Minimal Crst implementation based on CRITICAL_SECTION. Doesn't support much except for the basic locking
// functionality (in particular there is no rank violation checking).
//
enum CrstType
{
CrstHandleTable,
CrstDispatchCache,
CrstAllocHeap,
CrstGenericInstHashtab,
CrstMemAccessMgr,
CrstInterfaceDispatchGlobalLists,
CrstStressLog,
CrstRestrictedCallouts,
CrstGcStressControl,
CrstSuspendEE,
CrstCastCache,
};
enum CrstFlags
{
CRST_DEFAULT = 0x0,
CRST_REENTRANCY = 0x0,
CRST_UNSAFE_SAMELEVEL = 0x0,
CRST_UNSAFE_ANYMODE = 0x0,
CRST_DEBUGGER_THREAD = 0x0,
};
// Static version of Crst with no default constructor (user must call Init() before use).
class CrstStatic
{
public:
void Init(CrstType eType, CrstFlags eFlags = CRST_DEFAULT);
bool InitNoThrow(CrstType eType, CrstFlags eFlags = CRST_DEFAULT) { Init(eType, eFlags); return true; }
void Destroy();
void Enter() { CrstStatic::Enter(this); }
void Leave() { CrstStatic::Leave(this); }
static void Enter(CrstStatic *pCrst);
static void Leave(CrstStatic *pCrst);
#if defined(_DEBUG)
bool OwnedByCurrentThread();
EEThreadId GetHolderThreadId();
#endif // _DEBUG
private:
CRITICAL_SECTION m_sCritSec;
#if defined(_DEBUG)
EEThreadId m_uiOwnerId;
#endif // _DEBUG
};
// Non-static version that will initialize itself during construction.
class Crst : public CrstStatic
{
public:
Crst(CrstType eType, CrstFlags eFlags = CRST_DEFAULT)
: CrstStatic()
{ Init(eType, eFlags); }
};
// Holder for a Crst instance.
class CrstHolder
{
CrstStatic * m_pLock;
public:
CrstHolder(CrstStatic * pLock)
: m_pLock(pLock)
{
m_pLock->Enter();
}
~CrstHolder()
{
m_pLock->Leave();
}
};
class CrstHolderWithState
{
CrstStatic * m_pLock;
bool m_fAcquired;
public:
CrstHolderWithState(CrstStatic * pLock, bool fAcquire = true)
: m_pLock(pLock), m_fAcquired(fAcquire)
{
if (fAcquire)
m_pLock->Enter();
}
~CrstHolderWithState()
{
if (m_fAcquired)
m_pLock->Leave();
}
void Acquire()
{
if (!m_fAcquired)
{
m_pLock->Enter();
m_fAcquired = true;
}
}
void Release()
{
if (m_fAcquired)
{
m_pLock->Leave();
m_fAcquired = false;
}
}
CrstStatic * GetValue()
{
return m_pLock;
}
};
|