1
0
mirror of https://github.com/ppy/osu.git synced 2024-11-11 15:47:26 +08:00

Rework logic to avoid custom disposal early return handling

This commit is contained in:
Dean Herbert 2021-01-25 16:53:58 +09:00
parent 366f074f86
commit 10e8b7082e

View File

@ -2,7 +2,6 @@
// See the LICENCE file in the repository root for full licence text.
using System;
using osu.Framework.Allocation;
using osu.Framework.Bindables;
using osu.Framework.Graphics;
@ -43,42 +42,45 @@ namespace osu.Game.Screens.OnlinePlay
leasedInProgress = inProgress.BeginLease(true);
leasedInProgress.Value = true;
// for extra safety, marshal the end of operation back to the update thread if necessary.
return new OngoingOperation(() => Scheduler.Add(endOperation, false));
return new OngoingOperation(this, leasedInProgress);
}
private void endOperation()
private void endOperationWithKnownLease(LeasedBindable<bool> lease)
{
leasedInProgress?.Return();
leasedInProgress = null;
if (lease != leasedInProgress)
return;
// for extra safety, marshal the end of operation back to the update thread if necessary.
Scheduler.Add(() =>
{
leasedInProgress?.Return();
leasedInProgress = null;
}, false);
}
protected override void Dispose(bool isDisposing)
{
base.Dispose(isDisposing);
// base call does an UnbindAllBindables().
// clean up the leased reference here so that it doesn't get returned twice.
leasedInProgress = null;
}
private class OngoingOperation : InvokeOnDisposal
private class OngoingOperation : IDisposable
{
private bool isDisposed;
private readonly OngoingOperationTracker tracker;
private readonly LeasedBindable<bool> lease;
public OngoingOperation(Action action)
: base(action)
public OngoingOperation(OngoingOperationTracker tracker, LeasedBindable<bool> lease)
{
this.tracker = tracker;
this.lease = lease;
}
public override void Dispose()
public void Dispose()
{
// base class does not check disposal state for performance reasons which aren't relevant here.
// track locally, to avoid interfering with other operations in case of a potential double-disposal.
if (isDisposed)
return;
base.Dispose();
isDisposed = true;
tracker.endOperationWithKnownLease(lease);
}
}
}