Allow nested high performance sessions

Mostly just for safety, since I noticed this would pretty much fall over
in this scenario until now.
This commit is contained in:
Dean Herbert 2024-04-16 09:50:51 +08:00
parent 0e218ee271
commit 7e4782d4b1
No known key found for this signature in database
1 changed files with 18 additions and 4 deletions

View File

@ -11,16 +11,24 @@ namespace osu.Desktop.Performance
{ {
public class HighPerformanceSessionManager : IHighPerformanceSessionManager public class HighPerformanceSessionManager : IHighPerformanceSessionManager
{ {
private int activeSessions;
private GCLatencyMode originalGCMode; private GCLatencyMode originalGCMode;
public IDisposable BeginSession() public IDisposable BeginSession()
{ {
enableHighPerformanceSession(); enterSession();
return new InvokeOnDisposal<HighPerformanceSessionManager>(this, static m => m.disableHighPerformanceSession()); return new InvokeOnDisposal<HighPerformanceSessionManager>(this, static m => m.exitSession());
} }
private void enableHighPerformanceSession() private void enterSession()
{ {
if (Interlocked.Increment(ref activeSessions) > 1)
{
Logger.Log($"High performance session requested ({activeSessions} others already running)");
return;
}
Logger.Log("Starting high performance session"); Logger.Log("Starting high performance session");
originalGCMode = GCSettings.LatencyMode; originalGCMode = GCSettings.LatencyMode;
@ -30,8 +38,14 @@ private void enableHighPerformanceSession()
GC.Collect(0); GC.Collect(0);
} }
private void disableHighPerformanceSession() private void exitSession()
{ {
if (Interlocked.Decrement(ref activeSessions) > 0)
{
Logger.Log($"High performance session finished ({activeSessions} others remain)");
return;
}
Logger.Log("Ending high performance session"); Logger.Log("Ending high performance session");
if (GCSettings.LatencyMode == GCLatencyMode.LowLatency) if (GCSettings.LatencyMode == GCLatencyMode.LowLatency)