Terraria v1.4.4.9
Terraria source code documentation
Loading...
Searching...
No Matches
ThreadPool.cs
Go to the documentation of this file.
7
8namespace System.Threading;
9
10public static class ThreadPool
11{
13
15
16 internal static readonly ThreadPoolWorkQueue s_workQueue = new ThreadPoolWorkQueue();
17
18 internal static readonly Action<object> s_invokeAsyncStateMachineBox = delegate(object state)
19 {
21 {
23 }
24 else
25 {
26 asyncStateMachineBox.MoveNext();
27 }
28 };
29
31
32 public static int ThreadCount => (UsePortableThreadPool ? PortableThreadPool.ThreadPoolInstance.ThreadCount : 0) + GetThreadCount();
33
34 public static long CompletedWorkItemCount
35 {
36 get
37 {
38 long num = GetCompletedWorkItemCount();
40 {
41 num += PortableThreadPool.ThreadPoolInstance.CompletedWorkItemCount;
42 }
43 return num;
44 }
45 }
46
48 {
49 get
50 {
52 {
54 }
55 return 0L;
56 }
57 }
58
59 internal static bool EnableWorkerTracking
60 {
61 get
62 {
64 {
66 }
67 return false;
68 }
69 }
70
71 public static long PendingWorkItemCount
72 {
73 get
74 {
76 return ThreadPoolWorkQueue.LocalCount + threadPoolWorkQueue.GlobalCount + PendingUnmanagedWorkItemCount;
77 }
78 }
79
81 {
82 bool result = false;
83 int configVariableIndex = 0;
84 while (true)
85 {
86 uint configValue;
87 bool isBoolean;
91 {
92 break;
93 }
95 if (appContextConfigName == null)
96 {
97 result = true;
98 continue;
99 }
100 string text = new string(appContextConfigName);
101 if (isBoolean)
102 {
104 }
105 else
106 {
108 }
109 }
110 return result;
111 }
112
113 [MethodImpl(MethodImplOptions.InternalCall)]
114 private unsafe static extern int GetNextConfigUInt32Value(int configVariableIndex, out uint configValue, out bool isBoolean, out char* appContextConfigName);
115
116 private static bool GetEnableWorkerTracking()
117 {
119 {
121 }
122 return AppContextConfigHelper.GetBooleanConfig("System.Threading.ThreadPool.EnableWorkerTracking", defaultValue: false);
123 }
124
125 [MethodImpl(MethodImplOptions.InternalCall)]
126 internal static extern bool CanSetMinIOCompletionThreads(int ioCompletionThreads);
127
129 {
131 }
132
133 [MethodImpl(MethodImplOptions.InternalCall)]
134 internal static extern bool CanSetMaxIOCompletionThreads(int ioCompletionThreads);
135
137 {
139 }
140
142 {
144 {
146 }
147 if (workerThreads >= 0 && completionPortThreads >= 0)
148 {
150 }
151 return false;
152 }
153
162
164 {
166 {
168 }
169 if (workerThreads >= 0 && completionPortThreads >= 0)
170 {
172 }
173 return false;
174 }
175
184
193
194 [MethodImpl(MethodImplOptions.InternalCall)]
195 private static extern int GetThreadCount();
196
197 [DllImport("QCall", CharSet = CharSet.Unicode)]
198 private static extern long GetCompletedWorkItemCount();
199
200 [MethodImpl(MethodImplOptions.InternalCall)]
201 private static extern long GetPendingUnmanagedWorkItemCount();
202
226
231
232 [MethodImpl(MethodImplOptions.InternalCall)]
234
235 internal static void RequestWorkerThread()
236 {
238 {
240 }
241 else
242 {
244 }
245 }
246
247 [DllImport("QCall", CharSet = CharSet.Unicode)]
249
254
259
260 [DllImport("QCall", CharSet = CharSet.Unicode)]
262
263 [MethodImpl(MethodImplOptions.InternalCall)]
264 private unsafe static extern bool PostQueuedCompletionStatus(NativeOverlapped* overlapped);
265
266 [CLSCompliant(false)]
267 [SupportedOSPlatform("windows")]
272
277
278 [MethodImpl(MethodImplOptions.InternalCall)]
279 private static extern bool SetMinThreadsNative(int workerThreads, int completionPortThreads);
280
281 [MethodImpl(MethodImplOptions.InternalCall)]
282 private static extern bool SetMaxThreadsNative(int workerThreads, int completionPortThreads);
283
284 [MethodImpl(MethodImplOptions.InternalCall)]
285 private static extern void GetMinThreadsNative(out int workerThreads, out int completionPortThreads);
286
287 [MethodImpl(MethodImplOptions.InternalCall)]
288 private static extern void GetMaxThreadsNative(out int workerThreads, out int completionPortThreads);
289
290 [MethodImpl(MethodImplOptions.InternalCall)]
292
293 [MethodImpl(MethodImplOptions.AggressiveInlining)]
294 internal static bool NotifyWorkItemComplete(object threadLocalCompletionCountObject, int currentTimeMs)
295 {
297 {
298 return PortableThreadPool.ThreadPoolInstance.NotifyWorkItemComplete(threadLocalCompletionCountObject, currentTimeMs);
299 }
301 }
302
303 [MethodImpl(MethodImplOptions.InternalCall)]
304 private static extern bool NotifyWorkItemCompleteNative();
305
306 internal static void ReportThreadStatus(bool isWorking)
307 {
309 {
311 }
312 else
313 {
315 }
316 }
317
318 [MethodImpl(MethodImplOptions.InternalCall)]
319 private static extern void ReportThreadStatusNative(bool isWorking);
320
321 internal static void NotifyWorkItemProgress()
322 {
324 {
325 PortableThreadPool.ThreadPoolInstance.NotifyWorkItemProgress();
326 }
327 else
328 {
330 }
331 }
332
333 [MethodImpl(MethodImplOptions.InternalCall)]
334 private static extern void NotifyWorkItemProgressNative();
335
336 internal static bool NotifyThreadBlocked()
337 {
339 {
340 return PortableThreadPool.ThreadPoolInstance.NotifyThreadBlocked();
341 }
342 return false;
343 }
344
345 internal static void NotifyThreadUnblocked()
346 {
347 PortableThreadPool.ThreadPoolInstance.NotifyThreadUnblocked();
348 }
349
351 {
353 {
354 return null;
355 }
356 return PortableThreadPool.ThreadPoolInstance.GetOrCreateThreadLocalCompletionCountObject();
357 }
358
359 [MethodImpl(MethodImplOptions.InternalCall)]
360 private static extern bool GetEnableWorkerTrackingNative();
361
362 [MethodImpl(MethodImplOptions.InternalCall)]
364
365 [Obsolete("ThreadPool.BindHandle(IntPtr) has been deprecated. Use ThreadPool.BindHandle(SafeHandle) instead.")]
366 [SupportedOSPlatform("windows")]
367 public static bool BindHandle(IntPtr osHandle)
368 {
370 }
371
372 [SupportedOSPlatform("windows")]
373 public static bool BindHandle(SafeHandle osHandle)
374 {
375 if (osHandle == null)
376 {
377 throw new ArgumentNullException("osHandle");
378 }
379 bool flag = false;
380 bool success = false;
381 try
382 {
383 osHandle.DangerousAddRef(ref success);
384 return BindIOCompletionCallbackNative(osHandle.DangerousGetHandle());
385 }
386 finally
387 {
388 if (success)
389 {
390 osHandle.DangerousRelease();
391 }
392 }
393 }
394
395 [MethodImpl(MethodImplOptions.InternalCall)]
396 private static extern bool BindIOCompletionCallbackNative(IntPtr fileHandle);
397
398 [CLSCompliant(false)]
399 [UnsupportedOSPlatform("browser")]
408
409 [CLSCompliant(false)]
410 [UnsupportedOSPlatform("browser")]
419
420 [UnsupportedOSPlatform("browser")]
429
430 [UnsupportedOSPlatform("browser")]
439
440 [UnsupportedOSPlatform("browser")]
453
454 [UnsupportedOSPlatform("browser")]
467
468 [UnsupportedOSPlatform("browser")]
470 {
471 long num = (long)timeout.TotalMilliseconds;
472 if (num < -1)
473 {
475 }
476 if (num > int.MaxValue)
477 {
479 }
481 }
482
483 [UnsupportedOSPlatform("browser")]
485 {
486 long num = (long)timeout.TotalMilliseconds;
487 if (num < -1)
488 {
490 }
491 if (num > int.MaxValue)
492 {
494 }
496 }
497
498 public static bool QueueUserWorkItem(WaitCallback callBack)
499 {
500 return QueueUserWorkItem(callBack, null);
501 }
502
503 public static bool QueueUserWorkItem(WaitCallback callBack, object? state)
504 {
505 if (callBack == null)
506 {
508 }
511 s_workQueue.Enqueue(callback, forceGlobal: true);
512 return true;
513 }
514
526
545
546 public static bool UnsafeQueueUserWorkItem(WaitCallback callBack, object? state)
547 {
548 if (callBack == null)
549 {
551 }
553 s_workQueue.Enqueue(callback, forceGlobal: true);
554 return true;
555 }
556
570
571 internal static void UnsafeQueueUserWorkItemInternal(object callBack, bool preferLocal)
572 {
574 }
575
580
581 internal static bool TryPopCustomWorkItem(object workItem)
582 {
584 }
585
587 {
589 {
590 foreach (IThreadPoolWorkItem item in s_workQueue.timeSensitiveWorkQueue)
591 {
592 yield return item;
593 }
594 }
595 foreach (object workItem in s_workQueue.workItems)
596 {
597 yield return workItem;
598 }
600 foreach (ThreadPoolWorkQueue.WorkStealingQueue workStealingQueue in queues)
601 {
602 if (workStealingQueue == null || workStealingQueue.m_array == null)
603 {
604 continue;
605 }
606 object[] items = workStealingQueue.m_array;
607 foreach (object obj in items)
608 {
609 if (obj != null)
610 {
611 yield return obj;
612 }
613 }
614 }
615 }
616
618 {
620 if (workStealingQueue == null || workStealingQueue.m_array == null)
621 {
622 yield break;
623 }
624 object[] items = workStealingQueue.m_array;
625 foreach (object obj in items)
626 {
627 if (obj != null)
628 {
629 yield return obj;
630 }
631 }
632 }
633
635 {
637 {
638 foreach (IThreadPoolWorkItem item in s_workQueue.timeSensitiveWorkQueue)
639 {
640 yield return item;
641 }
642 }
643 foreach (object workItem in s_workQueue.workItems)
644 {
645 yield return workItem;
646 }
647 }
648
650 {
651 int num = 0;
652 foreach (object workitem in workitems)
653 {
654 num++;
655 }
656 object[] array = new object[num];
657 num = 0;
658 foreach (object workitem2 in workitems)
659 {
660 if (num < array.Length)
661 {
662 array[num] = workitem2;
663 }
664 num++;
665 }
666 return array;
667 }
668
669 internal static object[] GetQueuedWorkItemsForDebugger()
670 {
672 }
673
674 internal static object[] GetGloballyQueuedWorkItemsForDebugger()
675 {
677 }
678
679 internal static object[] GetLocallyQueuedWorkItemsForDebugger()
680 {
682 }
683}
static bool GetBooleanConfig(string configName, bool defaultValue)
static void SetSwitch(string switchName, bool isEnabled)
static void SetData(string name, object? data)
Definition AppContext.cs:48
static string ArgumentOutOfRange_LessEqualToIntegerMaxVal
Definition SR.cs:1054
static string ArgumentOutOfRange_NeedNonNegOrNegative1
Definition SR.cs:1072
Definition SR.cs:7
static ? ExecutionContext Capture()
static readonly PortableThreadPool ThreadPoolInstance
static bool LocalFindAndPop(object callback)
static bool NotifyThreadBlocked()
static bool SetMinThreadsNative(int workerThreads, int completionPortThreads)
static void GetMaxThreadsNative(out int workerThreads, out int completionPortThreads)
static RegisteredWaitHandle RegisterWaitForSingleObject(WaitHandle waitObject, WaitOrTimerCallback callBack, object state, uint millisecondsTimeOutInterval, bool executeOnlyOnce, bool flowExecutionContext)
static bool BindIOCompletionCallbackNative(IntPtr fileHandle)
static bool SupportsTimeSensitiveWorkItems
Definition ThreadPool.cs:30
static void GetAvailableThreadsNative(out int workerThreads, out int completionPortThreads)
static readonly bool UsePortableThreadPool
Definition ThreadPool.cs:12
static Interop.BOOL PerformRuntimeSpecificGateActivitiesNative(int cpuUtilization)
static unsafe int GetNextConfigUInt32Value(int configVariableIndex, out uint configValue, out bool isBoolean, out char *appContextConfigName)
static bool QueueUserWorkItem< TState >(Action< TState > callBack, TState state, bool preferLocal)
static void EnsureGateThreadRunning()
static void SetMinIOCompletionThreads(int ioCompletionThreads)
static readonly ThreadPoolWorkQueue s_workQueue
Definition ThreadPool.cs:16
static RegisteredWaitHandle RegisterWaitForSingleObject(WaitHandle waitObject, WaitOrTimerCallback callBack, object? state, uint millisecondsTimeOutInterval, bool executeOnlyOnce)
static void NotifyWorkItemProgress()
static object[] ToObjectArray(IEnumerable< object > workitems)
static object[] GetQueuedWorkItemsForDebugger()
static bool EnableWorkerTracking
Definition ThreadPool.cs:60
static bool PerformRuntimeSpecificGateActivities(int cpuUtilization)
static object[] GetGloballyQueuedWorkItemsForDebugger()
static long GetPendingUnmanagedWorkItemCount()
static void GetMinThreadsNative(out int workerThreads, out int completionPortThreads)
static bool QueueUserWorkItem(WaitCallback callBack)
static void UnsafeQueueUnmanagedWorkItem(IntPtr callback, IntPtr state)
static bool BindHandle(IntPtr osHandle)
static void SetMaxIOCompletionThreads(int ioCompletionThreads)
static object[] GetLocallyQueuedWorkItemsForDebugger()
static long GetCompletedWorkItemCount()
static IEnumerable< object > GetQueuedWorkItems()
static bool SetMinThreads(int workerThreads, int completionPortThreads)
static void ReportThreadStatus(bool isWorking)
static bool GetEnableWorkerTracking()
static long PendingWorkItemCount
Definition ThreadPool.cs:72
static RegisteredWaitHandle UnsafeRegisterWaitForSingleObject(WaitHandle waitObject, WaitOrTimerCallback callBack, object? state, uint millisecondsTimeOutInterval, bool executeOnlyOnce)
static RegisteredWaitHandle RegisterWaitForSingleObject(WaitHandle waitObject, WaitOrTimerCallback callBack, object? state, long millisecondsTimeOutInterval, bool executeOnlyOnce)
static RegisteredWaitHandle RegisterWaitForSingleObject(WaitHandle waitObject, WaitOrTimerCallback callBack, object? state, TimeSpan timeout, bool executeOnlyOnce)
static bool UnsafeQueueUserWorkItem< TState >(Action< TState > callBack, TState state, bool preferLocal)
static IEnumerable< object > GetGloballyQueuedWorkItems()
static bool UnsafeQueueUserWorkItem(WaitCallback callBack, object? state)
static void NotifyThreadUnblocked()
static bool GetEnableWorkerTrackingNative()
static void ReportThreadStatusNative(bool isWorking)
static void UnsafeQueueWaitCompletion(CompleteWaitThreadPoolWorkItem completeWaitWorkItem)
static void UnsafeQueueTimeSensitiveWorkItemInternal(IThreadPoolWorkItem timeSensitiveWorkItem)
static bool TryPopCustomWorkItem(object workItem)
static void QueueWaitCompletionNative(CompleteWaitThreadPoolWorkItem completeWaitWorkItem)
static void NotifyWorkItemProgressNative()
static long PendingUnmanagedWorkItemCount
Definition ThreadPool.cs:48
static RegisteredWaitHandle UnsafeRegisterWaitForSingleObject(WaitHandle waitObject, WaitOrTimerCallback callBack, object? state, TimeSpan timeout, bool executeOnlyOnce)
static void GetAvailableThreads(out int workerThreads, out int completionPortThreads)
static unsafe bool InitializeConfigAndDetermineUsePortableThreadPool()
Definition ThreadPool.cs:80
static void UnsafeQueueUserWorkItemInternal(object callBack, bool preferLocal)
static bool NotifyWorkItemComplete(object threadLocalCompletionCountObject, int currentTimeMs)
static Interop.BOOL RequestWorkerThreadNative()
static bool UnsafeQueueUserWorkItem(IThreadPoolWorkItem callBack, bool preferLocal)
static RegisteredWaitHandle UnsafeRegisterWaitForSingleObject(WaitHandle waitObject, WaitOrTimerCallback callBack, object? state, int millisecondsTimeOutInterval, bool executeOnlyOnce)
static bool CanSetMinIOCompletionThreads(int ioCompletionThreads)
static bool CanSetMaxIOCompletionThreads(int ioCompletionThreads)
static unsafe bool UnsafeQueueNativeOverlapped(NativeOverlapped *overlapped)
static readonly Action< object > s_invokeAsyncStateMachineBox
Definition ThreadPool.cs:18
static bool SetMaxThreadsNative(int workerThreads, int completionPortThreads)
static bool BindHandle(SafeHandle osHandle)
static bool QueueUserWorkItem(WaitCallback callBack, object? state)
static IntPtr RegisterWaitForSingleObjectNative(WaitHandle waitHandle, object state, uint timeOutInterval, bool executeOnlyOnce, RegisteredWaitHandle registeredWaitHandle)
static bool NotifyWorkItemCompleteNative()
static void GetMaxThreads(out int workerThreads, out int completionPortThreads)
static void GetMinThreads(out int workerThreads, out int completionPortThreads)
static void RequestWorkerThread()
static unsafe bool PostQueuedCompletionStatus(NativeOverlapped *overlapped)
static readonly bool IsWorkerTrackingEnabledInConfig
Definition ThreadPool.cs:14
static long CompletedWorkItemCount
Definition ThreadPool.cs:35
static RegisteredWaitHandle RegisterWaitForSingleObject(WaitHandle waitObject, WaitOrTimerCallback callBack, object? state, int millisecondsTimeOutInterval, bool executeOnlyOnce)
static object GetOrCreateThreadLocalCompletionCountObject()
static IEnumerable< object > GetLocallyQueuedWorkItems()
static bool SetMaxThreads(int workerThreads, int completionPortThreads)
static RegisteredWaitHandle UnsafeRegisterWaitForSingleObject(WaitHandle waitObject, WaitOrTimerCallback callBack, object? state, long millisecondsTimeOutInterval, bool executeOnlyOnce)
static void ThrowArgumentOutOfRangeException(System.ExceptionArgument argument)
static void ThrowArgumentNullException(string name)
delegate void WaitOrTimerCallback(object? state, bool timedOut)