Skip to content

Commit

Permalink
run pp state initialization async
Browse files Browse the repository at this point in the history
  • Loading branch information
rushiiMachine committed Mar 27, 2024
1 parent e7008d1 commit a6e6130
Showing 1 changed file with 15 additions and 6 deletions.
21 changes: 15 additions & 6 deletions Osu.Performance/OsuPerformance.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,19 @@ public class OsuPerformance : IDisposable
{
private readonly ConcurrentQueue<PendingCalculation> _queue;
private readonly CancellationTokenSource _queueTaskCancellation;
private readonly IntPtr _state;
private IntPtr _state = IntPtr.Zero;

public OsuPerformance(string mapPath, uint mods)
{
_state = Native.InitializeOsuGradualPerformance(mapPath, mods);
_queue = new ConcurrentQueue<PendingCalculation>();

_queueTaskCancellation = new CancellationTokenSource();

Task.Factory.StartNew(
ProcessQueue,
() =>
{
_state = Native.InitializeOsuGradualPerformance(mapPath, mods);
ProcessQueue();
},
_queueTaskCancellation.Token,
TaskCreationOptions.LongRunning,
TaskScheduler.Default
Expand All @@ -30,8 +33,9 @@ public OsuPerformance(string mapPath, uint mods)
public void Dispose()
{
_queueTaskCancellation.Cancel();
OnNewCalculation = null;
Native.DisposeGradualOsuPerformance(_state);
OnNewCalculation = null;
_state = IntPtr.Zero;
}

[UsedImplicitly]
Expand Down Expand Up @@ -62,6 +66,11 @@ private async void ProcessQueue()
while (true)
{
if (_queueTaskCancellation.IsCancellationRequested) return;
if (_state == IntPtr.Zero) // Not yet initialized
{
await Task.Delay(300);
continue;
}

while (_queue.TryDequeue(out var item))
{
Expand All @@ -78,7 +87,7 @@ private async void ProcessQueue()
if (_queueTaskCancellation.IsCancellationRequested) return;
}

await Task.Delay(200);
await Task.Delay(100);
}
}

Expand Down

0 comments on commit a6e6130

Please sign in to comment.