1// Licensed to the .NET Foundation under one or more agreements.
2// The .NET Foundation licenses this file to you under the MIT license.
3// See the LICENSE file in the project root for more information.
4#ifndef __GCENV_SYNC_H__
5#define __GCENV_SYNC_H__
6
7// -----------------------------------------------------------------------------------------------------------
8//
9// Helper classes expected by the GC
10//
11#define CRST_REENTRANCY 0
12#define CRST_UNSAFE_SAMELEVEL 0
13#define CRST_UNSAFE_ANYMODE 0
14#define CRST_DEBUGGER_THREAD 0
15#define CRST_DEFAULT 0
16
17#define CrstHandleTable 0
18
19typedef int CrstFlags;
20typedef int CrstType;
21
22class CrstStatic
23{
24 CLRCriticalSection m_cs;
25#ifdef _DEBUG
26 EEThreadId m_holderThreadId;
27#endif
28
29public:
30 bool InitNoThrow(CrstType eType, CrstFlags eFlags = CRST_DEFAULT)
31 {
32 m_cs.Initialize();
33 return true;
34 }
35
36 void Destroy()
37 {
38 m_cs.Destroy();
39 }
40
41 void Enter()
42 {
43 m_cs.Enter();
44#ifdef _DEBUG
45 m_holderThreadId.SetToCurrentThread();
46#endif
47 }
48
49 void Leave()
50 {
51#ifdef _DEBUG
52 m_holderThreadId.Clear();
53#endif
54 m_cs.Leave();
55 }
56
57#ifdef _DEBUG
58 EEThreadId GetHolderThreadId()
59 {
60 return m_holderThreadId;
61 }
62
63 bool OwnedByCurrentThread()
64 {
65 return GetHolderThreadId().IsCurrentThread();
66 }
67#endif
68};
69
70class CrstHolder
71{
72 CrstStatic * m_pLock;
73
74public:
75 CrstHolder(CrstStatic * pLock)
76 : m_pLock(pLock)
77 {
78 m_pLock->Enter();
79 }
80
81 ~CrstHolder()
82 {
83 m_pLock->Leave();
84 }
85};
86
87class CrstHolderWithState
88{
89 CrstStatic * m_pLock;
90 bool m_fAcquired;
91
92public:
93 CrstHolderWithState(CrstStatic * pLock, bool fAcquire = true)
94 : m_pLock(pLock), m_fAcquired(fAcquire)
95 {
96 if (fAcquire)
97 m_pLock->Enter();
98 }
99
100 ~CrstHolderWithState()
101 {
102 if (m_fAcquired)
103 m_pLock->Leave();
104 }
105
106 void Acquire()
107 {
108 if (!m_fAcquired)
109 {
110 m_pLock->Enter();
111 m_fAcquired = true;
112 }
113 }
114
115 void Release()
116 {
117 if (m_fAcquired)
118 {
119 m_pLock->Leave();
120 m_fAcquired = false;
121 }
122 }
123
124 CrstStatic * GetValue()
125 {
126 return m_pLock;
127 }
128};
129
130class CLREventStatic
131{
132public:
133 bool CreateAutoEventNoThrow(bool bInitialState);
134 bool CreateManualEventNoThrow(bool bInitialState);
135 bool CreateOSAutoEventNoThrow(bool bInitialState);
136 bool CreateOSManualEventNoThrow(bool bInitialState);
137
138 void CloseEvent();
139 bool IsValid() const;
140 bool Set();
141 bool Reset();
142 uint32_t Wait(uint32_t dwMilliseconds, bool bAlertable);
143
144private:
145 HANDLE m_hEvent;
146 bool m_fInitialized;
147};
148
149#endif // __GCENV_SYNC_H__
150