Add back main context locking

This commit is contained in:
Dean Herbert 2021-10-01 03:45:00 +09:00
parent 9fa901f6aa
commit 8557530cd5
1 changed files with 20 additions and 10 deletions

View File

@ -37,6 +37,7 @@ public class RealmContextFactory : Component, IRealmFactory
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 readonly object contextLock = new object();
private Realm? context;
public Realm Context
@ -46,14 +47,17 @@ public Realm Context
if (!ThreadSafety.IsUpdateThread)
throw new InvalidOperationException($"Use {nameof(CreateContext)} when performing realm operations from a non-update thread");
if (context == null)
lock (contextLock)
{
context = createContext();
Logger.Log($"Opened realm \"{context.Config.DatabasePath}\" at version {context.Config.SchemaVersion}");
}
if (context == null)
{
context = createContext();
Logger.Log($"Opened realm \"{context.Config.DatabasePath}\" at version {context.Config.SchemaVersion}");
}
// creating a context will ensure our schema is up-to-date and migrated.
return context;
// creating a context will ensure our schema is up-to-date and migrated.
return context;
}
}
}
@ -87,8 +91,11 @@ protected override void Update()
{
base.Update();
if (context?.Refresh() == true)
refreshes.Value++;
lock (contextLock)
{
if (context?.Refresh() == true)
refreshes.Value++;
}
}
private Realm createContext()
@ -137,8 +144,11 @@ public IDisposable BlockAllOperations()
contextCreationLock.Wait();
context?.Dispose();
context = null;
lock (contextLock)
{
context?.Dispose();
context = null;
}
return new InvokeOnDisposal<RealmContextFactory>(this, endBlockingSection);