-
Notifications
You must be signed in to change notification settings - Fork 4.9k
/
Copy paththread.inl
230 lines (191 loc) · 7.34 KB
/
thread.inl
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
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
#ifndef DACCESS_COMPILE
#include "eventtracebase.h"
const uint32_t SamplingDistributionMean = (100 * 1024);
inline gc_alloc_context* ee_alloc_context::GetGCAllocContext()
{
return (gc_alloc_context*)&m_rgbAllocContextBuffer;
}
inline uint8_t* ee_alloc_context::GetCombinedLimit()
{
return combined_limit;
}
// Workaround for https://github.com/dotnet/runtime/issues/96081
struct _thread_inl_gc_alloc_context
{
uint8_t* alloc_ptr;
uint8_t* alloc_limit;
};
inline bool ee_alloc_context::IsRandomizedSamplingEnabled()
{
#ifdef FEATURE_EVENT_TRACE
return IsRuntimeProviderEnabled(TRACE_LEVEL_INFORMATION, CLR_ALLOCATIONSAMPLING_KEYWORD);
#else
return false;
#endif // FEATURE_EVENT_TRACE
}
inline void ee_alloc_context::UpdateCombinedLimit(bool samplingEnabled)
{
_thread_inl_gc_alloc_context* gc_alloc_context = (_thread_inl_gc_alloc_context*)GetGCAllocContext();
if (!samplingEnabled)
{
combined_limit = gc_alloc_context->alloc_limit;
}
else
{
// compute the next sampling budget based on a geometric distribution
size_t samplingBudget = ComputeGeometricRandom();
// if the sampling limit is larger than the allocation context, no sampling will occur in this AC
// We do Min() prior to adding to alloc_ptr to ensure alloc_ptr+samplingBudget doesn't cause an overflow.
size_t size = gc_alloc_context->alloc_limit - gc_alloc_context->alloc_ptr;
combined_limit = gc_alloc_context->alloc_ptr + min(samplingBudget, size);
}
}
inline uint32_t ee_alloc_context::ComputeGeometricRandom()
{
// compute a random sample from the Geometric distribution.
double probability = t_random.NextDouble();
uint32_t threshold = (uint32_t)(-log(1 - probability) * SamplingDistributionMean);
return threshold;
}
// Returns a random double in the range [0, 1).
inline double ee_alloc_context::PerThreadRandom::NextDouble()
{
uint32_t value = minipal_xoshiro128pp_next(&random_state);
return value * (1.0/(UINT32_MAX+1.0));
}
// Set the m_pDeferredTransitionFrame field for GC allocation helpers that setup transition frame
// in assembly code. Do not use anywhere else.
inline void Thread::SetDeferredTransitionFrame(PInvokeTransitionFrame* pTransitionFrame)
{
ASSERT(ThreadStore::GetCurrentThread() == this);
ASSERT(Thread::IsCurrentThreadInCooperativeMode());
ASSERT(!Thread::IsHijackTarget(pTransitionFrame->m_RIP));
m_pDeferredTransitionFrame = pTransitionFrame;
}
// Setup the m_pDeferredTransitionFrame field for GC helpers entered via regular PInvoke.
// Do not use anywhere else.
inline void Thread::DeferTransitionFrame()
{
ASSERT(ThreadStore::GetCurrentThread() == this);
ASSERT(!Thread::IsCurrentThreadInCooperativeMode());
m_pDeferredTransitionFrame = m_pTransitionFrame;
}
#endif // DACCESS_COMPILE
inline bool Thread::IsWithinStackBounds(PTR_VOID p)
{
ASSERT((m_pStackLow != 0) && (m_pStackHigh != 0));
return (m_pStackLow <= p) && (p < m_pStackHigh);
}
inline void Thread::GetStackBounds(PTR_VOID * ppStackLow, PTR_VOID * ppStackHigh)
{
ASSERT((m_pStackLow != 0) && (m_pStackHigh != 0));
*ppStackLow = m_pStackLow;
*ppStackHigh = m_pStackHigh;
}
#ifndef DACCESS_COMPILE
inline void Thread::SetThreadStressLog(void* ptsl)
{
m_pThreadStressLog = ptsl;
}
#endif // DACCESS_COMPILE
inline PTR_VOID Thread::GetThreadStressLog() const
{
return m_pThreadStressLog;
}
inline void Thread::PushGCFrameRegistration(GCFrameRegistration* pRegistration)
{
pRegistration->m_pNext = m_pGCFrameRegistrations;
m_pGCFrameRegistrations = pRegistration;
}
inline void Thread::PopGCFrameRegistration(GCFrameRegistration* pRegistration)
{
ASSERT(m_pGCFrameRegistrations == pRegistration);
m_pGCFrameRegistrations = pRegistration->m_pNext;
}
inline ee_alloc_context* Thread::GetEEAllocContext()
{
return &m_eeAllocContext;
}
inline gc_alloc_context* Thread::GetAllocContext()
{
return GetEEAllocContext()->GetGCAllocContext();
}
inline bool Thread::IsStateSet(ThreadStateFlags flags)
{
return ((m_ThreadStateFlags & flags) == (uint32_t)flags);
}
inline bool Thread::IsDoNotTriggerGcSet()
{
return IsStateSet(TSF_DoNotTriggerGc);
}
inline bool Thread::IsCurrentThreadInCooperativeMode()
{
#ifndef DACCESS_COMPILE
ASSERT(ThreadStore::GetCurrentThread() == this);
#endif // !DACCESS_COMPILE
return (m_pTransitionFrame == NULL);
}
// define a specialized version locally so that we do not need to depend on entire gcenv.h
inline void VolatileStoreWithoutBarrier(PInvokeTransitionFrame** frameRef, PInvokeTransitionFrame* val)
{
*(PInvokeTransitionFrame* volatile*)frameRef = val;
}
FORCEINLINE void Thread::InlineReversePInvokeReturn(ReversePInvokeFrame* pFrame)
{
// set our mode to preemptive
VolatileStoreWithoutBarrier(&m_pTransitionFrame, pFrame->m_savedPInvokeTransitionFrame);
}
FORCEINLINE void Thread::InlinePInvoke(PInvokeTransitionFrame* pFrame)
{
ASSERT(!IsDoNotTriggerGcSet() || ThreadStore::IsTrapThreadsRequested());
pFrame->m_pThread = this;
// set our mode to preemptive
VolatileStoreWithoutBarrier(&m_pTransitionFrame, pFrame);
}
EXTERN_C void FASTCALL RhpWaitForGC2(PInvokeTransitionFrame* pFrame);
FORCEINLINE void Thread::InlinePInvokeReturn(PInvokeTransitionFrame* pFrame)
{
// must be in cooperative mode when checking the trap flag
VolatileStoreWithoutBarrier(&m_pTransitionFrame, (PInvokeTransitionFrame*)nullptr);
if (ThreadStore::IsTrapThreadsRequested())
{
RhpWaitForGC2(pFrame);
}
}
FORCEINLINE bool Thread::InlineTryFastReversePInvoke(ReversePInvokeFrame* pFrame)
{
// remember the current transition frame, so it will be restored when we return from reverse pinvoke
pFrame->m_savedPInvokeTransitionFrame = m_pTransitionFrame;
// If the thread is already in cooperative mode, this is a bad transition that will be a fail fast unless we are in
// a do not trigger mode. The exception to the rule allows us to have [UnmanagedCallersOnly] methods that are called via
// the "restricted GC callouts" as well as from native, which is necessary because the methods are CCW vtable
// methods on interfaces passed to native.
// We will allow threads in DoNotTriggerGc mode to do reverse PInvoke regardless of their coop state.
if (IsDoNotTriggerGcSet())
{
// We expect this scenario only when EE is stopped.
ASSERT(ThreadStore::IsTrapThreadsRequested());
// no need to do anything
return true;
}
// Do we need to attach the thread?
if (!IsStateSet(TSF_Attached))
return false; // thread is not attached
if (IsCurrentThreadInCooperativeMode())
return false; // bad transition
// this is an ordinary transition to managed code
// GC threads should not do that
ASSERT(!IsGCSpecial());
// must be in cooperative mode when checking the trap flag
VolatileStoreWithoutBarrier(&m_pTransitionFrame, (PInvokeTransitionFrame*)nullptr);
// now check if we need to trap the thread
if (ThreadStore::IsTrapThreadsRequested())
{
// put the previous frame back (sets us back to preemptive mode)
m_pTransitionFrame = pFrame->m_savedPInvokeTransitionFrame;
return false; // need to trap the thread
}
return true;
}