From d51ce896f94db37c8cd050bc8624907373c5d600 Mon Sep 17 00:00:00 2001 From: Dean Herbert Date: Fri, 28 Jul 2017 12:46:54 +0900 Subject: [PATCH] Add locking to all BeatmapManager operations --- osu.Game/Beatmaps/BeatmapManager.cs | 60 ++++++++++++++++++----------- 1 file changed, 38 insertions(+), 22 deletions(-) diff --git a/osu.Game/Beatmaps/BeatmapManager.cs b/osu.Game/Beatmaps/BeatmapManager.cs index 97031547c0..e62fd2fc5d 100644 --- a/osu.Game/Beatmaps/BeatmapManager.cs +++ b/osu.Game/Beatmaps/BeatmapManager.cs @@ -108,9 +108,13 @@ namespace osu.Game.Beatmaps /// The beatmap to be imported. public BeatmapSetInfo Import(ArchiveReader archiveReader) { - BeatmapSetInfo set = importToStorage(archiveReader); - Import(set); - return set; + // let's only allow one concurrent import at a time for now. + lock (this) + { + BeatmapSetInfo set = importToStorage(archiveReader); + Import(set); + return set; + } } /// @@ -132,10 +136,13 @@ namespace osu.Game.Beatmaps /// The beatmap to delete. public void Delete(BeatmapSetInfo beatmapSet) { - if (!beatmaps.Delete(beatmapSet)) return; + lock (this) + { + if (!beatmaps.Delete(beatmapSet)) return; - if (!beatmapSet.Protected) - files.Dereference(beatmapSet.Files); + if (!beatmapSet.Protected) + files.Dereference(beatmapSet.Files); + } } /// @@ -145,9 +152,12 @@ namespace osu.Game.Beatmaps /// The beatmap to restore. public void Undelete(BeatmapSetInfo beatmapSet) { - if (!beatmaps.Undelete(beatmapSet)) return; + lock (this) + { + if (!beatmaps.Undelete(beatmapSet)) return; - files.Reference(beatmapSet.Files); + files.Reference(beatmapSet.Files); + } } /// @@ -158,22 +168,25 @@ namespace osu.Game.Beatmaps /// A instance correlating to the provided . public WorkingBeatmap GetWorkingBeatmap(BeatmapInfo beatmapInfo, WorkingBeatmap previous = null) { - if (beatmapInfo == null || beatmapInfo == DefaultBeatmap?.BeatmapInfo) - return DefaultBeatmap; + lock (this) + { + if (beatmapInfo == null || beatmapInfo == DefaultBeatmap?.BeatmapInfo) + return DefaultBeatmap; - beatmaps.Populate(beatmapInfo); + beatmaps.Populate(beatmapInfo); - if (beatmapInfo.BeatmapSet == null) - throw new InvalidOperationException($@"Beatmap set {beatmapInfo.BeatmapSetInfoID} is not in the local database."); + if (beatmapInfo.BeatmapSet == null) + throw new InvalidOperationException($@"Beatmap set {beatmapInfo.BeatmapSetInfoID} is not in the local database."); - if (beatmapInfo.Metadata == null) - beatmapInfo.Metadata = beatmapInfo.BeatmapSet.Metadata; + if (beatmapInfo.Metadata == null) + beatmapInfo.Metadata = beatmapInfo.BeatmapSet.Metadata; - WorkingBeatmap working = new BeatmapManagerWorkingBeatmap(files.Store, beatmapInfo); + WorkingBeatmap working = new BeatmapManagerWorkingBeatmap(files.Store, beatmapInfo); - previous?.TransferTo(working); + previous?.TransferTo(working); - return working; + return working; + } } /// @@ -325,10 +338,13 @@ namespace osu.Game.Beatmaps /// A list of available . public List GetAllUsableBeatmapSets(bool populate = true) { - if (populate) - return beatmaps.QueryAndPopulate(b => !b.DeletePending).ToList(); - else - return beatmaps.Query(b => !b.DeletePending).ToList(); + lock (this) + { + if (populate) + return beatmaps.QueryAndPopulate(b => !b.DeletePending).ToList(); + else + return beatmaps.Query(b => !b.DeletePending).ToList(); + } } protected class BeatmapManagerWorkingBeatmap : WorkingBeatmap