1
0
mirror of https://github.com/ppy/osu.git synced 2025-01-28 06:42:54 +08:00

Merge branch 'update-realm-context-factory' into realm-context-factory-safer-blocking

This commit is contained in:
Dean Herbert 2021-10-01 03:53:33 +09:00
commit 74841cf1a9

View File

@ -37,6 +37,7 @@ namespace osu.Game.Database
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,9 +47,11 @@ namespace osu.Game.Database
if (!ThreadSafety.IsUpdateThread)
throw new InvalidOperationException($"Use {nameof(CreateContext)} when performing realm operations from a non-update thread");
lock (contextLock)
{
if (context == null)
{
context = createContext();
context = CreateContext();
Logger.Log($"Opened realm \"{context.Config.DatabasePath}\" at version {context.Config.SchemaVersion}");
}
@ -56,6 +59,7 @@ namespace osu.Game.Database
return context;
}
}
}
public RealmContextFactory(Storage storage, string filename)
{
@ -69,14 +73,6 @@ namespace osu.Game.Database
Filename += realm_extension;
}
public Realm CreateContext()
{
if (IsDisposed)
throw new ObjectDisposedException(nameof(RealmContextFactory));
return createContext();
}
/// <summary>
/// Compact this realm.
/// </summary>
@ -87,12 +83,18 @@ namespace osu.Game.Database
{
base.Update();
lock (contextLock)
{
if (context?.Refresh() == true)
refreshes.Value++;
}
}
private Realm createContext()
public Realm CreateContext()
{
if (IsDisposed)
throw new ObjectDisposedException(nameof(RealmContextFactory));
try
{
contextCreationLock.Wait();
@ -143,11 +145,14 @@ namespace osu.Game.Database
{
contextCreationLock.Wait();
const int sleep_length = 200;
int timeout = 5000;
lock (contextLock)
{
context?.Dispose();
context = null;
}
const int sleep_length = 200;
int timeout = 5000;
// see https://github.com/realm/realm-dotnet/discussions/2657
while (!Compact())
@ -173,8 +178,11 @@ namespace osu.Game.Database
}
protected override void Dispose(bool isDisposing)
{
lock (contextLock)
{
context?.Dispose();
}
if (!IsDisposed)
{