-
Notifications
You must be signed in to change notification settings - Fork 119
/
GCLatencyModeScope.cs
42 lines (36 loc) · 1.26 KB
/
GCLatencyModeScope.cs
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
using System.Runtime;
using System.Runtime.InteropServices;
namespace DotNext.Runtime;
/// <summary>
/// Represents lexical scope of the specific GC latency mode.
/// </summary>
[StructLayout(LayoutKind.Auto)]
public readonly struct GCLatencyModeScope : IDisposable
{
private readonly GCLatencyMode? currentMode;
/// <summary>
/// Initializes a new scope that affects GC intrusion level.
/// </summary>
/// <param name="mode">GC latency mode.</param>
public GCLatencyModeScope(GCLatencyMode mode)
{
currentMode = GCSettings.LatencyMode;
GCSettings.LatencyMode = mode;
}
/// <summary>
/// Cancels previously defined GC latency.
/// </summary>
public void Dispose()
{
if (currentMode.HasValue)
GCSettings.LatencyMode = currentMode.GetValueOrDefault();
}
/// <summary>
/// Creates a scope with <see cref="GCLatencyMode.SustainedLowLatency"/> GC intrusion level.
/// </summary>
public static GCLatencyModeScope SustainedLowLatency => new(GCLatencyMode.SustainedLowLatency);
/// <summary>
/// Creates a scope with <see cref="GCLatencyMode.LowLatency"/> GC intrusion level.
/// </summary>
public static GCLatencyModeScope LowLatency => new(GCLatencyMode.LowLatency);
}