Terraria v1.4.4.9
Terraria source code documentation
Loading...
Searching...
No Matches

◆ GateThreadStart()

static void System.Threading.PortableThreadPool.GateThread.GateThreadStart ( )
inlinestaticprivate

Definition at line 156 of file PortableThreadPool.cs.

157 {
158 bool booleanConfig = AppContextConfigHelper.GetBooleanConfig("System.Threading.ThreadPool.DisableStarvationDetection", defaultValue: false);
159 bool booleanConfig2 = AppContextConfigHelper.GetBooleanConfig("System.Threading.ThreadPool.DebugBreakOnWorkerStarvation", defaultValue: false);
160 CpuUtilizationReader cpuUtilizationReader = default(CpuUtilizationReader);
161 _ = cpuUtilizationReader.CurrentUtilization;
162 PortableThreadPool threadPoolInstance = ThreadPoolInstance;
163 LowLevelLock threadAdjustmentLock = threadPoolInstance._threadAdjustmentLock;
164 DelayHelper delayHelper = default(DelayHelper);
165 if (BlockingConfig.IsCooperativeBlockingEnabled)
166 {
167 threadPoolInstance.OnGen2GCCallback();
168 Gen2GcCallback.Register(threadPoolInstance.OnGen2GCCallback);
169 }
170 while (true)
171 {
172 RunGateThreadEvent.WaitOne();
173 int tickCount = Environment.TickCount;
174 delayHelper.SetGateActivitiesTime(tickCount);
175 while (true)
176 {
177 bool wasSignaledToWake = DelayEvent.WaitOne((int)delayHelper.GetNextDelay(tickCount));
178 tickCount = Environment.TickCount;
179 PendingBlockingAdjustment pendingBlockingAdjustment = threadPoolInstance._pendingBlockingAdjustment;
180 if (pendingBlockingAdjustment == PendingBlockingAdjustment.None)
181 {
182 delayHelper.ClearBlockingAdjustmentDelay();
183 }
184 else
185 {
186 bool flag = false;
187 if (delayHelper.HasBlockingAdjustmentDelay)
188 {
189 flag = delayHelper.HasBlockingAdjustmentDelayElapsed(tickCount, wasSignaledToWake);
190 if (pendingBlockingAdjustment == PendingBlockingAdjustment.WithDelayIfNecessary && !flag)
191 {
192 goto IL_00ee;
193 }
194 }
195 uint num = threadPoolInstance.PerformBlockingAdjustment(flag);
196 if (num == 0)
197 {
198 delayHelper.ClearBlockingAdjustmentDelay();
199 }
200 else
201 {
202 delayHelper.SetBlockingAdjustmentTimeAndDelay(tickCount, num);
203 }
204 }
205 goto IL_00ee;
206 IL_00ee:
207 if (!delayHelper.ShouldPerformGateActivities(tickCount, wasSignaledToWake))
208 {
209 continue;
210 }
211 if (ThreadPool.EnableWorkerTracking && NativeRuntimeEventSource.Log.IsEnabled())
212 {
213 NativeRuntimeEventSource.Log.ThreadPoolWorkingThreadCount((uint)threadPoolInstance.GetAndResetHighWatermarkCountOfThreadsProcessingUserCallbacks(), 0);
214 }
215 bool flag2 = ThreadPool.PerformRuntimeSpecificGateActivities(threadPoolInstance._cpuUtilization = cpuUtilizationReader.CurrentUtilization);
216 if (!booleanConfig && threadPoolInstance._pendingBlockingAdjustment == PendingBlockingAdjustment.None && threadPoolInstance._separated.numRequestedWorkers > 0 && SufficientDelaySinceLastDequeue(threadPoolInstance))
217 {
218 bool flag3 = false;
219 threadAdjustmentLock.Acquire();
220 try
221 {
222 ThreadCounts threadCounts = threadPoolInstance._separated.counts;
223 while (threadCounts.NumProcessingWork < threadPoolInstance._maxThreads && threadCounts.NumProcessingWork >= threadCounts.NumThreadsGoal)
224 {
225 if (booleanConfig2)
226 {
227 Debugger.Break();
228 }
229 ThreadCounts newCounts = threadCounts;
230 short newThreadCount = (newCounts.NumThreadsGoal = (short)(threadCounts.NumProcessingWork + 1));
231 ThreadCounts threadCounts2 = threadPoolInstance._separated.counts.InterlockedCompareExchange(newCounts, threadCounts);
232 if (threadCounts2 == threadCounts)
233 {
234 HillClimbing.ThreadPoolHillClimber.ForceChange(newThreadCount, HillClimbing.StateOrTransition.Starvation);
235 flag3 = true;
236 break;
237 }
238 threadCounts = threadCounts2;
239 }
240 }
241 finally
242 {
243 threadAdjustmentLock.Release();
244 }
245 if (flag3)
246 {
247 WorkerThread.MaybeAddWorkingWorker(threadPoolInstance);
248 }
249 }
250 if (!flag2 && threadPoolInstance._separated.numRequestedWorkers <= 0 && threadPoolInstance._pendingBlockingAdjustment == PendingBlockingAdjustment.None && Interlocked.Decrement(ref threadPoolInstance._separated.gateThreadRunningState) <= GetRunningStateForNumRuns(0))
251 {
252 break;
253 }
254 }
255 }
256 }
static readonly AutoResetEvent RunGateThreadEvent
static bool SufficientDelaySinceLastDequeue(PortableThreadPool threadPoolInstance)
static readonly PortableThreadPool ThreadPoolInstance

References System.Threading.PortableThreadPool._cpuUtilization, System.Threading.PortableThreadPool._maxThreads, System.Threading.PortableThreadPool._pendingBlockingAdjustment, System.Threading.PortableThreadPool._separated, System.Threading.PortableThreadPool._threadAdjustmentLock, System.Threading.LowLevelLock.Acquire(), System.Diagnostics.Debugger.Break(), System.Threading.PortableThreadPool.GateThread.DelayHelper.ClearBlockingAdjustmentDelay(), System.Threading.PortableThreadPool.CacheLineSeparated.counts, System.Threading.PortableThreadPool.CpuUtilizationReader.CurrentUtilization, System.Threading.Interlocked.Decrement(), System.Threading.PortableThreadPool.GateThread.DelayEvent, System.Threading.ThreadPool.EnableWorkerTracking, System.Threading.PortableThreadPool.CacheLineSeparated.gateThreadRunningState, System.Threading.PortableThreadPool.GetAndResetHighWatermarkCountOfThreadsProcessingUserCallbacks(), System.AppContextConfigHelper.GetBooleanConfig(), System.Threading.PortableThreadPool.GateThread.DelayHelper.GetNextDelay(), System.Threading.PortableThreadPool.GateThread.GetRunningStateForNumRuns(), System.Threading.PortableThreadPool.GateThread.DelayHelper.HasBlockingAdjustmentDelay, System.Threading.PortableThreadPool.GateThread.DelayHelper.HasBlockingAdjustmentDelayElapsed(), System.Threading.PortableThreadPool.ThreadCounts.InterlockedCompareExchange(), System.Threading.PortableThreadPool.BlockingConfig.IsCooperativeBlockingEnabled, System.Diagnostics.Tracing.NativeRuntimeEventSource.Log, System.Threading.PortableThreadPool.WorkerThread.MaybeAddWorkingWorker(), System.Threading.PortableThreadPool.ThreadCounts.NumProcessingWork, System.Threading.PortableThreadPool.CacheLineSeparated.numRequestedWorkers, System.Threading.PortableThreadPool.ThreadCounts.NumThreadsGoal, System.Threading.PortableThreadPool.OnGen2GCCallback(), System.Threading.PortableThreadPool.PerformBlockingAdjustment(), System.Threading.ThreadPool.PerformRuntimeSpecificGateActivities(), System.Gen2GcCallback.Register(), System.Threading.LowLevelLock.Release(), System.Threading.PortableThreadPool.GateThread.RunGateThreadEvent, System.Threading.PortableThreadPool.GateThread.DelayHelper.SetBlockingAdjustmentTimeAndDelay(), System.Threading.PortableThreadPool.GateThread.DelayHelper.SetGateActivitiesTime(), System.Threading.PortableThreadPool.GateThread.DelayHelper.ShouldPerformGateActivities(), System.Threading.PortableThreadPool.GateThread.SufficientDelaySinceLastDequeue(), System.Threading.PortableThreadPool.HillClimbing.ThreadPoolHillClimber, System.Threading.PortableThreadPool.ThreadPoolInstance, and System.Environment.TickCount.

Referenced by System.Threading.PortableThreadPool.GateThread.CreateGateThread().