Add explicit lock object and some xmldoc for clarity

This commit is contained in:
Dean Herbert 2020-11-10 14:31:27 +09:00
parent dc21aebdc7
commit 6cc0bf17a9
1 changed files with 18 additions and 7 deletions

View File

@ -30,9 +30,23 @@ public class BeatmapDifficultyCache : MemoryCachingComponent<BeatmapDifficultyCa
// Too many simultaneous updates can lead to stutters. One thread seems to work fine for song select display purposes.
private readonly ThreadedTaskScheduler updateScheduler = new ThreadedTaskScheduler(1, nameof(BeatmapDifficultyCache));
// All bindables that should be updated along with the current ruleset + mods.
/// <summary>
/// All bindables that should be updated along with the current ruleset + mods.
/// </summary>
private readonly WeakList<BindableStarDifficulty> trackedBindables = new WeakList<BindableStarDifficulty>();
/// <summary>
/// Cancellation sources used by tracked bindables.
/// </summary>
private readonly List<CancellationTokenSource> linkedCancellationSources = new List<CancellationTokenSource>();
/// <summary>
/// Lock to be held when operating on <see cref="trackedBindables"/> or <see cref="linkedCancellationSources"/>.
/// </summary>
private readonly object bindableUpdateLock = new object();
private CancellationTokenSource trackedUpdateCancellationSource;
[Resolved]
private BeatmapManager beatmapManager { get; set; }
@ -60,7 +74,7 @@ public IBindable<StarDifficulty> GetBindableDifficulty([NotNull] BeatmapInfo bea
{
var bindable = createBindable(beatmapInfo, currentRuleset.Value, currentMods.Value, cancellationToken);
lock (trackedBindables)
lock (bindableUpdateLock)
trackedBindables.Add(bindable);
return bindable;
@ -144,15 +158,12 @@ public static DifficultyRating GetDifficultyRating(double starRating)
return DifficultyRating.Easy;
}
private CancellationTokenSource trackedUpdateCancellationSource;
private readonly List<CancellationTokenSource> linkedCancellationSources = new List<CancellationTokenSource>();
/// <summary>
/// Updates all tracked <see cref="BindableStarDifficulty"/> using the current ruleset and mods.
/// </summary>
private void updateTrackedBindables()
{
lock (trackedBindables)
lock (bindableUpdateLock)
{
cancelTrackedBindableUpdate();
trackedUpdateCancellationSource = new CancellationTokenSource();
@ -172,7 +183,7 @@ private void updateTrackedBindables()
/// </summary>
private void cancelTrackedBindableUpdate()
{
lock (trackedBindables)
lock (bindableUpdateLock)
{
trackedUpdateCancellationSource?.Cancel();
trackedUpdateCancellationSource = null;