diff --git a/osu.Game/Beatmaps/BeatmapDifficultyCache.cs b/osu.Game/Beatmaps/BeatmapDifficultyCache.cs index eb83c88318..3b58062add 100644 --- a/osu.Game/Beatmaps/BeatmapDifficultyCache.cs +++ b/osu.Game/Beatmaps/BeatmapDifficultyCache.cs @@ -30,8 +30,22 @@ namespace osu.Game.Beatmaps // 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. - private readonly LockedWeakList trackedBindables = new LockedWeakList(); + /// + /// All bindables that should be updated along with the current ruleset + mods. + /// + private readonly WeakList trackedBindables = new WeakList(); + + /// + /// Cancellation sources used by tracked bindables. + /// + private readonly List linkedCancellationSources = new List(); + + /// + /// Lock to be held when operating on or . + /// + private readonly object bindableUpdateLock = new object(); + + private CancellationTokenSource trackedUpdateCancellationSource; [Resolved] private BeatmapManager beatmapManager { get; set; } @@ -59,7 +73,10 @@ namespace osu.Game.Beatmaps public IBindable GetBindableDifficulty([NotNull] BeatmapInfo beatmapInfo, CancellationToken cancellationToken = default) { var bindable = createBindable(beatmapInfo, currentRuleset.Value, currentMods.Value, cancellationToken); - trackedBindables.Add(bindable); + + lock (bindableUpdateLock) + trackedBindables.Add(bindable); + return bindable; } @@ -86,7 +103,8 @@ namespace osu.Game.Beatmaps /// The s to get the difficulty with. /// An optional which stops computing the star difficulty. /// The . - public Task GetDifficultyAsync([NotNull] BeatmapInfo beatmapInfo, [CanBeNull] RulesetInfo rulesetInfo = null, [CanBeNull] IEnumerable mods = null, CancellationToken cancellationToken = default) + public Task GetDifficultyAsync([NotNull] BeatmapInfo beatmapInfo, [CanBeNull] RulesetInfo rulesetInfo = null, [CanBeNull] IEnumerable mods = null, + CancellationToken cancellationToken = default) { // In the case that the user hasn't given us a ruleset, use the beatmap's default ruleset. rulesetInfo ??= beatmapInfo.Ruleset; @@ -140,23 +158,23 @@ namespace osu.Game.Beatmaps return DifficultyRating.Easy; } - private CancellationTokenSource trackedUpdateCancellationSource; - private readonly List linkedCancellationSources = new List(); - /// /// Updates all tracked using the current ruleset and mods. /// private void updateTrackedBindables() { - cancelTrackedBindableUpdate(); - trackedUpdateCancellationSource = new CancellationTokenSource(); - - foreach (var b in trackedBindables) + lock (bindableUpdateLock) { - var linkedSource = CancellationTokenSource.CreateLinkedTokenSource(trackedUpdateCancellationSource.Token, b.CancellationToken); - linkedCancellationSources.Add(linkedSource); + cancelTrackedBindableUpdate(); + trackedUpdateCancellationSource = new CancellationTokenSource(); - updateBindable(b, currentRuleset.Value, currentMods.Value, linkedSource.Token); + foreach (var b in trackedBindables) + { + var linkedSource = CancellationTokenSource.CreateLinkedTokenSource(trackedUpdateCancellationSource.Token, b.CancellationToken); + linkedCancellationSources.Add(linkedSource); + + updateBindable(b, currentRuleset.Value, currentMods.Value, linkedSource.Token); + } } } @@ -165,15 +183,18 @@ namespace osu.Game.Beatmaps /// private void cancelTrackedBindableUpdate() { - trackedUpdateCancellationSource?.Cancel(); - trackedUpdateCancellationSource = null; - - if (linkedCancellationSources != null) + lock (bindableUpdateLock) { - foreach (var c in linkedCancellationSources) - c.Dispose(); + trackedUpdateCancellationSource?.Cancel(); + trackedUpdateCancellationSource = null; - linkedCancellationSources.Clear(); + if (linkedCancellationSources != null) + { + foreach (var c in linkedCancellationSources) + c.Dispose(); + + linkedCancellationSources.Clear(); + } } }