From ab34123ba81fe37c62aaf8e500ec42346a17ea23 Mon Sep 17 00:00:00 2001 From: Dean Herbert Date: Tue, 13 Feb 2018 14:58:15 +0900 Subject: [PATCH] Remove unnecessary class variable --- osu.Game/Database/DatabaseContextFactory.cs | 20 +++++++++----------- 1 file changed, 9 insertions(+), 11 deletions(-) diff --git a/osu.Game/Database/DatabaseContextFactory.cs b/osu.Game/Database/DatabaseContextFactory.cs index 002e9e456d..d8044e6eb1 100644 --- a/osu.Game/Database/DatabaseContextFactory.cs +++ b/osu.Game/Database/DatabaseContextFactory.cs @@ -16,8 +16,6 @@ namespace osu.Game.Database private readonly object writeLock = new object(); - private OsuDbContext writeContext; - private bool currentWriteDidWrite; private volatile int currentWriteUsages; @@ -43,7 +41,7 @@ namespace osu.Game.Database Interlocked.Increment(ref currentWriteUsages); - return new DatabaseWriteUsage(writeContext ?? (writeContext = threadContexts.Value), usageCompleted); + return new DatabaseWriteUsage(threadContexts.Value, usageCompleted); } private void usageCompleted(DatabaseWriteUsage usage) @@ -56,19 +54,12 @@ namespace osu.Game.Database if (usages > 0) return; - if (currentWriteDidWrite) { - writeContext.Dispose(); currentWriteDidWrite = false; - // once all writes are complete, we want to refresh thread-specific contexts to make sure they don't have stale local caches. recycleThreadContexts(); } - - // always set to null (even when a write didn't occur) so we get the correct thread context on next write request. - writeContext = null; - } finally { @@ -76,7 +67,14 @@ namespace osu.Game.Database } } - private void recycleThreadContexts() => threadContexts = new ThreadLocal(CreateContext); + private void recycleThreadContexts() + { + if (threadContexts != null) + foreach (var context in threadContexts.Values) + context.Dispose(); + + threadContexts = new ThreadLocal(CreateContext, true); + } protected virtual OsuDbContext CreateContext() {