Terraria
v1.4.4.9
Terraria source code documentation
Loading...
Searching...
No Matches
HandleCollector.cs
Go to the documentation of this file.
1
using
System.Threading
;
2
3
namespace
System.Runtime.InteropServices
;
4
5
public
sealed
class
HandleCollector
6
{
7
private
int
_threshold
;
8
9
private
int
_handleCount
;
10
11
private
readonly
int
[]
_gcCounts
=
new
int
[3];
12
13
private
int
_gcGeneration
;
14
15
public
int
Count
=>
_handleCount
;
16
17
public
int
InitialThreshold
{
get
; }
18
19
public
int
MaximumThreshold
{
get
; }
20
21
public
string
Name
{
get
; }
22
23
public
HandleCollector
(
string
? name,
int
initialThreshold)
24
: this(name, initialThreshold, int.MaxValue)
25
{
26
}
27
28
public
HandleCollector
(
string
? name,
int
initialThreshold,
int
maximumThreshold)
29
{
30
if
(initialThreshold < 0)
31
{
32
throw
new
ArgumentOutOfRangeException
(
"initialThreshold"
, initialThreshold,
System
.
SR
.
Arg_NeedNonNegNumRequired
);
33
}
34
if
(maximumThreshold < 0)
35
{
36
throw
new
ArgumentOutOfRangeException
(
"maximumThreshold"
, maximumThreshold,
System
.
SR
.
Arg_NeedNonNegNumRequired
);
37
}
38
if
(initialThreshold > maximumThreshold)
39
{
40
throw
new
ArgumentException
(
System
.
SR
.
Arg_InvalidThreshold
,
"initialThreshold"
);
41
}
42
Name
= name ??
string
.Empty;
43
InitialThreshold
= initialThreshold;
44
MaximumThreshold
= maximumThreshold;
45
_threshold
= initialThreshold;
46
_handleCount
= 0;
47
}
48
49
public
void
Add
()
50
{
51
int
num = -1;
52
Interlocked
.
Increment
(ref
_handleCount
);
53
if
(
_handleCount
< 0)
54
{
55
throw
new
InvalidOperationException
(
System
.
SR
.
InvalidOperation_HCCountOverflow
);
56
}
57
if
(
_handleCount
>
_threshold
)
58
{
59
lock (
this
)
60
{
61
_threshold
=
_handleCount
+
_handleCount
/ 10;
62
num =
_gcGeneration
;
63
if
(
_gcGeneration
< 2)
64
{
65
_gcGeneration
++;
66
}
67
}
68
}
69
if
(num >= 0 && (num == 0 ||
_gcCounts
[num] ==
GC
.
CollectionCount
(num)))
70
{
71
GC
.
Collect
(num);
72
Thread
.
Sleep
(10 * num);
73
}
74
for
(
int
i = 1; i < 3; i++)
75
{
76
_gcCounts
[i] =
GC
.
CollectionCount
(i);
77
}
78
}
79
80
public
void
Remove
()
81
{
82
Interlocked
.
Decrement
(ref
_handleCount
);
83
if
(
_handleCount
< 0)
84
{
85
throw
new
InvalidOperationException
(
System
.
SR
.
InvalidOperation_HCCountOverflow
);
86
}
87
int
num =
_handleCount
+
_handleCount
/ 10;
88
if
(num <
_threshold
-
_threshold
/ 10)
89
{
90
lock (
this
)
91
{
92
if
(num >
InitialThreshold
)
93
{
94
_threshold
= num;
95
}
96
else
97
{
98
_threshold
=
InitialThreshold
;
99
}
100
_gcGeneration
= 0;
101
}
102
}
103
for
(
int
i = 1; i < 3; i++)
104
{
105
_gcCounts
[i] =
GC
.
CollectionCount
(i);
106
}
107
}
108
}
System.ArgumentException
Definition
ArgumentException.cs:9
System.ArgumentOutOfRangeException
Definition
ArgumentOutOfRangeException.cs:9
System.GC.CollectionCount
static int CollectionCount(int generation)
Definition
GC.cs:169
System.GC.Collect
static void Collect(int generation)
Definition
GC.cs:119
System.GC
Definition
GC.cs:8
System.InvalidOperationException
Definition
InvalidOperationException.cs:9
System.Runtime.InteropServices.HandleCollector.InitialThreshold
int InitialThreshold
Definition
HandleCollector.cs:17
System.Runtime.InteropServices.HandleCollector.MaximumThreshold
int MaximumThreshold
Definition
HandleCollector.cs:19
System.Runtime.InteropServices.HandleCollector.Name
string Name
Definition
HandleCollector.cs:21
System.Runtime.InteropServices.HandleCollector.Add
void Add()
Definition
HandleCollector.cs:49
System.Runtime.InteropServices.HandleCollector._gcGeneration
int _gcGeneration
Definition
HandleCollector.cs:13
System.Runtime.InteropServices.HandleCollector.Remove
void Remove()
Definition
HandleCollector.cs:80
System.Runtime.InteropServices.HandleCollector._handleCount
int _handleCount
Definition
HandleCollector.cs:9
System.Runtime.InteropServices.HandleCollector.Count
int Count
Definition
HandleCollector.cs:15
System.Runtime.InteropServices.HandleCollector.HandleCollector
HandleCollector(string? name, int initialThreshold)
Definition
HandleCollector.cs:23
System.Runtime.InteropServices.HandleCollector._threshold
int _threshold
Definition
HandleCollector.cs:7
System.Runtime.InteropServices.HandleCollector._gcCounts
readonly int[] _gcCounts
Definition
HandleCollector.cs:11
System.Runtime.InteropServices.HandleCollector.HandleCollector
HandleCollector(string? name, int initialThreshold, int maximumThreshold)
Definition
HandleCollector.cs:28
System.Runtime.InteropServices.HandleCollector
Definition
HandleCollector.cs:6
System.SR.Arg_InvalidThreshold
static string Arg_InvalidThreshold
Definition
SR.cs:18
System.SR.Arg_NeedNonNegNumRequired
static string Arg_NeedNonNegNumRequired
Definition
SR.cs:16
System.SR.InvalidOperation_HCCountOverflow
static string InvalidOperation_HCCountOverflow
Definition
SR.cs:14
System.SR
Definition
SR.cs:7
System.Threading.Interlocked.Decrement
static int Decrement(ref int location)
Definition
Interlocked.cs:20
System.Threading.Interlocked.Increment
static int Increment(ref int location)
Definition
Interlocked.cs:10
System.Threading.Interlocked
Definition
Interlocked.cs:9
System.Threading.Thread.Sleep
static void Sleep(int millisecondsTimeout)
Definition
Thread.cs:658
System.Threading.Thread
Definition
Thread.cs:13
System.Runtime.InteropServices
Definition
SequenceMarshal.cs:4
System.Threading
Definition
TaskToApm.cs:3
System
Definition
BlockingCollection.cs:8
source
System.Runtime.InteropServices
System.Runtime.InteropServices
HandleCollector.cs
Generated by
1.10.0