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> 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)");
private readonly object contextLock = new object();
private Realm? context; private Realm? context;
public Realm Context public Realm Context
@ -46,14 +47,17 @@ namespace osu.Game.Database
if (!ThreadSafety.IsUpdateThread) if (!ThreadSafety.IsUpdateThread)
throw new InvalidOperationException($"Use {nameof(CreateContext)} when performing realm operations from a non-update thread"); throw new InvalidOperationException($"Use {nameof(CreateContext)} when performing realm operations from a non-update thread");
if (context == null) lock (contextLock)
{ {
context = createContext(); if (context == null)
Logger.Log($"Opened realm \"{context.Config.DatabasePath}\" at version {context.Config.SchemaVersion}"); {
} 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. // creating a context will ensure our schema is up-to-date and migrated.
return context; return context;
}
} }
} }
@ -69,14 +73,6 @@ namespace osu.Game.Database
Filename += realm_extension; Filename += realm_extension;
} }
public Realm CreateContext()
{
if (IsDisposed)
throw new ObjectDisposedException(nameof(RealmContextFactory));
return createContext();
}
/// <summary> /// <summary>
/// Compact this realm. /// Compact this realm.
/// </summary> /// </summary>
@ -87,12 +83,18 @@ namespace osu.Game.Database
{ {
base.Update(); base.Update();
if (context?.Refresh() == true) lock (contextLock)
refreshes.Value++; {
if (context?.Refresh() == true)
refreshes.Value++;
}
} }
private Realm createContext() public Realm CreateContext()
{ {
if (IsDisposed)
throw new ObjectDisposedException(nameof(RealmContextFactory));
try try
{ {
contextCreationLock.Wait(); contextCreationLock.Wait();
@ -143,12 +145,15 @@ namespace osu.Game.Database
{ {
contextCreationLock.Wait(); contextCreationLock.Wait();
lock (contextLock)
{
context?.Dispose();
context = null;
}
const int sleep_length = 200; const int sleep_length = 200;
int timeout = 5000; int timeout = 5000;
context?.Dispose();
context = null;
// see https://github.com/realm/realm-dotnet/discussions/2657 // see https://github.com/realm/realm-dotnet/discussions/2657
while (!Compact()) while (!Compact())
{ {
@ -174,7 +179,10 @@ namespace osu.Game.Database
protected override void Dispose(bool isDisposing) protected override void Dispose(bool isDisposing)
{ {
context?.Dispose(); lock (contextLock)
{
context?.Dispose();
}
if (!IsDisposed) if (!IsDisposed)
{ {