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
|
// 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.
#ifndef _SYNCBLK_INL_
#define _SYNCBLK_INL_
#ifndef DACCESS_COMPILE
FORCEINLINE AwareLock::EnterHelperResult AwareLock::EnterHelper(Thread* pCurThread)
{
CONTRACTL {
SO_TOLERANT;
NOTHROW;
GC_NOTRIGGER;
MODE_ANY;
} CONTRACTL_END;
for (;;)
{
LONG state = m_MonitorHeld.LoadWithoutBarrier();
if (state == 0)
{
if (InterlockedCompareExchangeAcquire((LONG*)&m_MonitorHeld, 1, 0) == 0)
{
m_HoldingThread = pCurThread;
m_Recursion = 1;
pCurThread->IncLockCount();
return AwareLock::EnterHelperResult_Entered;
}
}
else
{
if (GetOwningThread() == pCurThread) /* monitor is held, but it could be a recursive case */
{
m_Recursion++;
return AwareLock::EnterHelperResult_Entered;
}
return AwareLock::EnterHelperResult_Contention;
}
}
}
FORCEINLINE AwareLock::EnterHelperResult ObjHeader::EnterObjMonitorHelper(Thread* pCurThread)
{
CONTRACTL {
SO_TOLERANT;
NOTHROW;
GC_NOTRIGGER;
MODE_COOPERATIVE;
} CONTRACTL_END;
DWORD tid = pCurThread->GetThreadId();
LONG oldvalue = m_SyncBlockValue.LoadWithoutBarrier();
if ((oldvalue & (BIT_SBLK_IS_HASH_OR_SYNCBLKINDEX +
BIT_SBLK_SPIN_LOCK +
SBLK_MASK_LOCK_THREADID +
SBLK_MASK_LOCK_RECLEVEL)) == 0)
{
if (tid > SBLK_MASK_LOCK_THREADID)
{
return AwareLock::EnterHelperResult_UseSlowPath;
}
LONG newvalue = oldvalue | tid;
if (InterlockedCompareExchangeAcquire((LONG*)&m_SyncBlockValue, newvalue, oldvalue) == oldvalue)
{
pCurThread->IncLockCount();
return AwareLock::EnterHelperResult_Entered;
}
}
else
if (oldvalue & BIT_SBLK_IS_HASH_OR_SYNCBLKINDEX)
{
// If we have a hash code already, we need to create a sync block
if (oldvalue & BIT_SBLK_IS_HASHCODE)
{
return AwareLock::EnterHelperResult_UseSlowPath;
}
SyncBlock *syncBlock = g_pSyncTable [oldvalue & MASK_SYNCBLOCKINDEX].m_SyncBlock;
_ASSERTE(syncBlock != NULL);
return syncBlock->m_Monitor.EnterHelper(pCurThread);
}
else
{
// The header is transitioning - treat this as if the lock was taken
if (oldvalue & BIT_SBLK_SPIN_LOCK)
{
return AwareLock::EnterHelperResult_Contention;
}
// Here we know we have the "thin lock" layout, but the lock is not free.
// It could still be the recursion case - compare the thread id to check
if (tid == (DWORD) (oldvalue & SBLK_MASK_LOCK_THREADID))
{
// Ok, the thread id matches, it's the recursion case.
// Bump up the recursion level and check for overflow
LONG newvalue = oldvalue + SBLK_LOCK_RECLEVEL_INC;
if ((newvalue & SBLK_MASK_LOCK_RECLEVEL) == 0)
{
return AwareLock::EnterHelperResult_UseSlowPath;
}
if (InterlockedCompareExchangeAcquire((LONG*)&m_SyncBlockValue, newvalue, oldvalue) == oldvalue)
{
return AwareLock::EnterHelperResult_Entered;
}
}
}
return AwareLock::EnterHelperResult_Contention;
}
inline AwareLock::EnterHelperResult ObjHeader::EnterObjMonitorHelperSpin(Thread* pCurThread)
{
CONTRACTL {
SO_TOLERANT;
NOTHROW;
GC_NOTRIGGER;
MODE_COOPERATIVE;
} CONTRACTL_END;
if (1 == g_SystemInfo.dwNumberOfProcessors)
{
return AwareLock::EnterHelperResult_Contention;
}
DWORD spincount = g_SpinConstants.dwInitialDuration;
for (;;)
{
//
// exponential backoff
//
for (DWORD i = 0; i < spincount; i++)
{
YieldProcessor();
}
AwareLock::EnterHelperResult result = EnterObjMonitorHelper(pCurThread);
if (result != AwareLock::EnterHelperResult_Contention)
{
return result;
}
spincount *= g_SpinConstants.dwBackoffFactor;
if (spincount > g_SpinConstants.dwMaximumDuration)
{
break;
}
}
return AwareLock::EnterHelperResult_Contention;
}
// Helper encapsulating the core logic for releasing monitor. Returns what kind of
// follow up action is necessary. This is FORCEINLINE to make it provide a very efficient implementation.
FORCEINLINE AwareLock::LeaveHelperAction AwareLock::LeaveHelper(Thread* pCurThread)
{
CONTRACTL {
SO_TOLERANT;
NOTHROW;
GC_NOTRIGGER;
MODE_ANY;
} CONTRACTL_END;
if (m_HoldingThread != pCurThread)
return AwareLock::LeaveHelperAction_Error;
_ASSERTE((size_t)m_MonitorHeld & 1);
_ASSERTE(m_Recursion >= 1);
#if defined(_DEBUG) && defined(TRACK_SYNC)
// The best place to grab this is from the ECall frame
Frame *pFrame = pCurThread->GetFrame();
int caller = (pFrame && pFrame != FRAME_TOP ? (int) pFrame->GetReturnAddress() : -1);
pCurThread->m_pTrackSync->LeaveSync(caller, this);
#endif
if (--m_Recursion != 0)
{
return AwareLock::LeaveHelperAction_None;
}
m_HoldingThread->DecLockCount();
m_HoldingThread = NULL;
for (;;)
{
// Read existing lock state
LONG state = m_MonitorHeld.LoadWithoutBarrier();
// Clear lock bit.
if (InterlockedCompareExchangeRelease((LONG*)&m_MonitorHeld, state - 1, state) == state)
{
// If wait count is non-zero on successful clear, we must signal the event.
if (state & ~1)
{
return AwareLock::LeaveHelperAction_Signal;
}
break;
}
}
return AwareLock::LeaveHelperAction_None;
}
// Helper encapsulating the core logic for releasing monitor. Returns what kind of
// follow up action is necessary. This is FORCEINLINE to make it provide a very efficient implementation.
FORCEINLINE AwareLock::LeaveHelperAction ObjHeader::LeaveObjMonitorHelper(Thread* pCurThread)
{
CONTRACTL {
SO_TOLERANT;
NOTHROW;
GC_NOTRIGGER;
MODE_COOPERATIVE;
} CONTRACTL_END;
DWORD syncBlockValue = m_SyncBlockValue.LoadWithoutBarrier();
if ((syncBlockValue & (BIT_SBLK_SPIN_LOCK + BIT_SBLK_IS_HASH_OR_SYNCBLKINDEX)) == 0)
{
if ((syncBlockValue & SBLK_MASK_LOCK_THREADID) != pCurThread->GetThreadId())
{
// This thread does not own the lock.
return AwareLock::LeaveHelperAction_Error;
}
if (syncBlockValue & SBLK_MASK_LOCK_RECLEVEL)
{
// recursion and ThinLock
DWORD newValue = syncBlockValue - SBLK_LOCK_RECLEVEL_INC;
if (InterlockedCompareExchangeRelease((LONG*)&m_SyncBlockValue, newValue, syncBlockValue) != (LONG)syncBlockValue)
{
return AwareLock::LeaveHelperAction_Yield;
}
}
else
{
// We are leaving the lock
DWORD newValue = (syncBlockValue & (~SBLK_MASK_LOCK_THREADID));
if (InterlockedCompareExchangeRelease((LONG*)&m_SyncBlockValue, newValue, syncBlockValue) != (LONG)syncBlockValue)
{
return AwareLock::LeaveHelperAction_Yield;
}
pCurThread->DecLockCount();
}
return AwareLock::LeaveHelperAction_None;
}
if ((syncBlockValue & (BIT_SBLK_SPIN_LOCK + BIT_SBLK_IS_HASH_OR_SYNCBLKINDEX + BIT_SBLK_IS_HASHCODE)) == BIT_SBLK_IS_HASH_OR_SYNCBLKINDEX)
{
SyncBlock *syncBlock = g_pSyncTable [syncBlockValue & MASK_SYNCBLOCKINDEX].m_SyncBlock;
_ASSERTE(syncBlock != NULL);
return syncBlock->m_Monitor.LeaveHelper(pCurThread);
}
if (syncBlockValue & BIT_SBLK_SPIN_LOCK)
{
return AwareLock::LeaveHelperAction_Contention;
}
// This thread does not own the lock.
return AwareLock::LeaveHelperAction_Error;
}
#endif // DACCESS_COMPILE
// Provide access to the object associated with this awarelock, so client can
// protect it.
inline OBJECTREF AwareLock::GetOwningObject()
{
LIMITED_METHOD_CONTRACT;
SUPPORTS_DAC;
// gcc on mac needs these intermediate casts to avoid some ambiuous overloading in the DAC case
PTR_SyncTableEntry table = SyncTableEntry::GetSyncTableEntry();
return (OBJECTREF)(Object*)(PTR_Object)table[(m_dwSyncIndex & ~SyncBlock::SyncBlockPrecious)].m_Object;
}
#endif // _SYNCBLK_INL_
|