From 1a3debc91dc814fad0854a7899bc9860cc5748f1 Mon Sep 17 00:00:00 2001 From: Dean Herbert Date: Mon, 23 Oct 2017 17:56:04 +0900 Subject: [PATCH] Ensure thread safety on shared contexts Let's call this one temporary. --- osu.Game/Database/DatabaseBackedStore.cs | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/osu.Game/Database/DatabaseBackedStore.cs b/osu.Game/Database/DatabaseBackedStore.cs index 35700e90fc..68f412eee6 100644 --- a/osu.Game/Database/DatabaseBackedStore.cs +++ b/osu.Game/Database/DatabaseBackedStore.cs @@ -2,6 +2,7 @@ // Licensed under the MIT Licence - https://raw.githubusercontent.com/ppy/osu/master/LICENCE using System; +using System.Threading; using osu.Framework.Logging; using osu.Framework.Platform; @@ -16,7 +17,7 @@ namespace osu.Game.Database /// protected readonly Func CreateContext; - private readonly Lazy queryContext; + private readonly ThreadLocal queryContext; /// /// Retrieve a shared context for performing lookups (or write operations on the update thread, for now). @@ -26,7 +27,9 @@ namespace osu.Game.Database protected DatabaseBackedStore(Func createContext, Storage storage = null) { CreateContext = createContext; - queryContext = new Lazy(CreateContext); + + // todo: while this seems to work quite well, we need to consider that contexts could enter a state where they are never cleaned up. + queryContext = new ThreadLocal(CreateContext); Storage = storage;