Ensure thread safety on shared contexts

Let's call this one temporary.
This commit is contained in:
Dean Herbert 2017-10-23 17:56:04 +09:00
parent e7931ef4c7
commit 1a3debc91d
1 changed files with 5 additions and 2 deletions

View File

@ -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 @@ public abstract class DatabaseBackedStore
/// </summary>
protected readonly Func<OsuDbContext> CreateContext;
private readonly Lazy<OsuDbContext> queryContext;
private readonly ThreadLocal<OsuDbContext> queryContext;
/// <summary>
/// Retrieve a shared context for performing lookups (or write operations on the update thread, for now).
@ -26,7 +27,9 @@ public abstract class DatabaseBackedStore
protected DatabaseBackedStore(Func<OsuDbContext> createContext, Storage storage = null)
{
CreateContext = createContext;
queryContext = new Lazy<OsuDbContext>(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<OsuDbContext>(CreateContext);
Storage = storage;