1
0
mirror of https://github.com/ppy/osu.git synced 2025-01-19 13:02:54 +08:00

Use ThreadLocal to avoid potential threading issues

This commit is contained in:
Dean Herbert 2021-11-29 17:38:42 +09:00
parent c98451a7bf
commit 673481ebc5

View File

@ -52,7 +52,7 @@ namespace osu.Game.Database
/// </summary> /// </summary>
private readonly SemaphoreSlim contextCreationLock = new SemaphoreSlim(1); private readonly SemaphoreSlim contextCreationLock = new SemaphoreSlim(1);
private bool canCreateContexts; private ThreadLocal<bool> currentThreadCanCreateContexts = new ThreadLocal<bool>();
private static readonly GlobalStatistic<int> refreshes = GlobalStatistics.Get<int>(@"Realm", @"Dirty Refreshes"); private static readonly GlobalStatistic<int> refreshes = GlobalStatistics.Get<int>(@"Realm", @"Dirty Refreshes");
private static readonly GlobalStatistic<int> contexts_created = GlobalStatistics.Get<int>(@"Realm", @"Contexts (Created)"); private static readonly GlobalStatistic<int> contexts_created = GlobalStatistics.Get<int>(@"Realm", @"Contexts (Created)");
@ -155,13 +155,13 @@ namespace osu.Game.Database
try try
{ {
if (!canCreateContexts) if (!currentThreadCanCreateContexts.Value)
contextCreationLock.Wait(); contextCreationLock.Wait();
// the semaphore is used to stop all context creation. // the semaphore is used to stop all context creation.
// once the semaphore has been taken by this code section, it is safe to create further contexts. // once the semaphore has been taken by this code section, it is safe to create further contexts.
// this can happen if a realm subscription is active and triggers a callback which has user code that calls `CreateContext`. // this can happen if a realm subscription is active and triggers a callback which has user code that calls `CreateContext`.
canCreateContexts = true; currentThreadCanCreateContexts.Value = true;
contexts_created.Value++; contexts_created.Value++;
@ -170,7 +170,7 @@ namespace osu.Game.Database
finally finally
{ {
contextCreationLock.Release(); contextCreationLock.Release();
canCreateContexts = false; currentThreadCanCreateContexts.Value = false;
} }
} }